/* * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and * its subsidiaries and affiliates (collectly called MKSEMI). * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form, except as embedded into an MKSEMI * integrated circuit in a product or a software update for such product, * must reproduce the above copyright notice, this list of conditions and * the following disclaimer in the documentation and/or other materials * provided with the distribution. * * 3. Neither the name of MKSEMI nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * 4. This software, with or without modification, must only be used with a * MKSEMI integrated circuit. * * 5. Any software provided in binary form under this license must not be * reverse engineered, decompiled, modified and/or disassembled. * * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef MK_MISC_H_ #define MK_MISC_H_ #include "mk_common.h" /** * @addtogroup MK8000_MISC * @{ */ /** us to system tick converter, us < 68,829us */ #define __US_TO_TICKS(us) (((us) * ((sys_timer_freq) / 1000)) / 1000) /** system tick to us converter */ #define __TICKS_TO_US(tick) ((tick) / (((sys_timer_freq) / 1000) / 1000)) /** system tick to us converter, allow long long type multiply operation to promote precision */ #define __TICKS_TO_US_LL(tick) (uint32_t)(((uint64_t)(tick)*1000) / (sys timer freg / 1000)) /** ms to system tick converter, ms < 68,829ms */ #define __MS_TO_TICKS(ms) ((ms) * ((sys_timer_freq) / 1000)) /** system tick to ms converter */ #define __TICKS_TO_MS(tick) ((tick) / ((sys_timer_freq) / 1000)) /** ms to sleep timer count converter */ #define __MS_TO_32K_CNT(ms) ((uint32_t)((float)(ms)*32768.0f / 1000.0f)) /** us to sleep timer count converter */ #define __US_TO_32K_CNT(us) ((uint32_t)((float)(us)*32768.0f / 1000000.0f)) /** Threshold configuration for BOD and BOR */ enum BOD_BOR_TH_LVL_T { /* 1.7V for BOD and 1.65V for BOR*/ BOD_BOR_TH_LVL0 = 0, /* 2.0V */ BOD_BOR_TH_LVL1, /* 2.2V */ BOD_BOR_TH_LVL2, /* 2.5V */ BOD_BOR_TH_LVL3, /* 2.8V */ BOD_BOR_TH_LVL4, }; /** Hystereisis configuration for BOD and BOR */ enum BOD_BOR_HYST_LVL_T { /* 0mV */ BOD_BOR_HYST_LVL0 = 0, /* 100mV */ BOD_BOR_HYST_LVL1, /* 200mV */ BOD_BOR_HYST_LVL2, }; /** Brown out detection configuration */ struct BOD_BOR_CFG_T { enum BOD_BOR_TH_LVL_T level; enum BOD_BOR_HYST_LVL_T hyst; }; #ifdef __cplusplus extern "C" { #endif /** * @brief Read chip ID. * @return Chip ID * - [9:8] Major version * 00 : A * 01 : B * 10 : C * 11 : MPW * - [11:10] | [5] Minor version * 00 | 0 : 0 * 01 | 0 : 1 * 10 | 0 : 2 * 11 | 0 : 3 * 00 | 1 : 4 * */ uint32_t mk_chip_id(void); /** * @brief Read 64-bits chip UUID. * @param[out] uuid 64-bits universally unique identifier */ void mk_chip_uuid(uint8_t *uuid); /* BOD */ /** * @brief Open brown out detection. * @param[in] cfg Brown out detection configuration @ref BOD_BOR_CFG_T */ void bod_open(const struct BOD_BOR_CFG_T *cfg); /** * @brief Close brown out detection. */ void bod_close(void); /** * @brief Brown out detection interrupt handler. */ void BOD_IRQHandler(void); /* BOR */ /** * @brief Open brown out reset. * @param[in] cfg Brown out reset configuration @ref BOD_BOR_CFG_T */ void bor_open(const struct BOD_BOR_CFG_T *cfg); /** * @brief Close brown out reset. */ void bor_close(void); /* SYS Timer */ /** System timer frequency */ extern uint32_t sys_timer_freq; /** * @brief Open the system timer, system timer is a free running counter, using the Dual timer 0. */ void sys_timer_open(void); /** * @brief Close the system timer. */ void sys_timer_close(void); /** * @brief Get the system timer count. * @return the system timer count */ uint32_t sys_timer_get(void); /** * @brief Delay us. * @param[in] time_us Time to be delayed */ void sys_timer_delay_us(uint32_t time_us); /** * @brief Delay ms. * @param[in] time_ms Time to be delayed */ void sys_timer_delay_ms(uint32_t time_ms); /* MAC Timer */ /** * @brief Open the MAC timer, MAC timer is a one-shot count down counter, using the Dual timer 1. */ void mac_timer_open(drv_callback_t callback); /** * @brief Close the MAC timer. */ void mac_timer_close(void); /** * @brief Start the MAC timer. * @param[in] count Counter value */ void mac_timer_start(uint32_t count); /** * @brief Stop the MAC timer. */ void mac_timer_stop(void); /** OS Tick data structure*/ struct SYS_TICK_ENV_T { volatile uint32_t count; uint32_t load; uint32_t val; uint32_t elapse; void (*callback)(void); }; /** * @brief Configure the systick timer. * @param[in] ticks Counter value */ void sys_tick_start(uint32_t ticks); /** * @brief Get the time of systick timer. * @return time in us */ uint32_t sys_tick_us(void); /** * @brief Get the time of systick timer. * @return time in ms */ uint32_t sys_tick_ms(void); /** * @brief Get the count of systick timer. * @return counter value */ uint32_t sys_tick_get(void); /** * @brief Get the elapsed time of systick timer. * @return elapsed time in ms */ uint32_t sys_tick_elapse_ms(void); /** * @brief Register callback function for systick timer. * @return callback systick timer callback function */ void sys_tick_callback_set(void (*callback)(void)); /** * @brief Pause the systick timer. */ void sys_tick_pause(void); /** * @brief Resume the systick timer. */ void sys_tick_resume(void); /** * @brief systick timer interrupt handler. */ void SysTick_Handler(void); /** * @brief System reset * @param[in] error reason for the system to reboot */ void sys_reset(uint32_t error); /** * @brief delay us. * @param[in] cnt Time in us * @note @p cnt should less than 2.15 seconds */ void RAM_FUNC delay_us(uint32_t cnt); /** * @brief Calculate how many bits are set * * @param[in] value Input data * @return bits number * */ uint8_t count_bits(uint32_t value); /** * @brief Search for the lowest bit 1 in a byte * * @param[in] value Input data * @return the lowest bit 1 position, 1 ~ 8 means bit position 0 ~ 7, 0 means input data has no bit 1 * */ uint8_t search_byte_right_one(uint8_t value); /** * @brief Output the lowest bit 1 in a byte * * @param[in] value Input data * @return the lowest bit 1 mask * */ uint8_t byte_right_one_mask(uint8_t value); /** * @brief moving average filter * * @param[in] value Input data * @return average value * */ int32_t average_filter(int32_t input); /** * @brief Transform Q9.7 format to signed integer value. * @param[in] data Q9.7 format data * @return a signed integer value */ int16_t mk_q7_to_s16(int16_t data); /** * @brief Transform signed integer value to Q9.7 format (for UCI) * @param[in] data signed integer value * @return Q9.7 format */ int16_t mk_s16_to_q7(int16_t data); /** * @brief Transform Q9.7 format to float value. * @param[in] data Q9.7 format data * @return a float value */ float mk_q7_to_f32(int16_t data); /** * @brief Transform float value to Q9.7 format (for UCI) * @param[in] data float value * @return Q9.7 format */ int16_t mk_f32_to_q7(float data); #ifdef __cplusplus } #endif /** * @} */ #endif /* MK_MISC_H_ */