1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * 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_ */