utils.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file utils.c
  5. * @brief This file contains the Utilities functions/types for the driver.
  6. *
  7. * @version V1.0
  8. * @date 07 Nov 2019
  9. * @author AE Team
  10. * @note
  11. *
  12. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  13. *
  14. *********************************************************************************
  15. */
  16. #include <string.h>
  17. #include "utils.h"
  18. #include "ald_dma.h"
  19. #include "ald_cmu.h"
  20. /** @defgroup ES32FXXX_ALD EASTSOFT ES32F3xx ALD
  21. * @brief Shanghai Eastsoft Microelectronics Cortex-M Chip Abstraction Layer Driver(ALD)
  22. * @{
  23. */
  24. /** @defgroup UTILS Utils
  25. * @brief Utils module driver
  26. * @{
  27. */
  28. /** @defgroup ALD_Private_Constants Private Constants
  29. * @brief ALD Private Constants
  30. * @{
  31. */
  32. /**
  33. * @brief ALD version number
  34. */
  35. #define __ALD_VERSION_MAIN (0x01) /**< [31:24] main version */
  36. #define __ALD_VERSION_SUB1 (0x00) /**< [23:16] sub1 version */
  37. #define __ALD_VERSION_SUB2 (0x00) /**< [15:8] sub2 version */
  38. #define __ALD_VERSION_RC (0x00) /**< [7:0] release candidate */
  39. #define __ALD_VERSION ((__ALD_VERSION_MAIN << 24) | \
  40. (__ALD_VERSION_SUB1 << 16) | \
  41. (__ALD_VERSION_SUB2 << 8 ) | \
  42. (__ALD_VERSION_RC))
  43. /**
  44. * @}
  45. */
  46. /** @defgroup ALD_Private_Variables Private Variables
  47. * @{
  48. */
  49. /** @brief lib_tick: Increase by one millisecond
  50. */
  51. static __IO uint32_t lib_tick;
  52. uint32_t __systick_interval = SYSTICK_INTERVAL_1MS;
  53. /**
  54. * @}
  55. */
  56. /** @defgroup ALD_Public_Functions Public Functions
  57. * @{
  58. */
  59. /** @defgroup ALD_Public_Functions_Group1 Initialization Function
  60. * @brief Initialization functions
  61. *
  62. * @verbatim
  63. ===============================================================================
  64. ##### Initialization functions #####
  65. ===============================================================================
  66. [..] This section provides functions allowing to:
  67. (+) Initializes interface, the NVIC allocation and initial clock
  68. configuration. It initializes the source of time base also when timeout
  69. is needed and the backup domain when enabled.
  70. (+) Configure The time base source to have 1ms time base with a dedicated
  71. Tick interrupt priority.
  72. (++) Systick timer is used by default as source of time base, but user
  73. can eventually implement his proper time base source (a general purpose
  74. timer for example or other time source), keeping in mind that Time base
  75. duration should be kept 1ms.
  76. (++) Time base configuration function (ald_tick_init()) is called automatically
  77. at the beginning of the program after reset by ald_cmu_init() or at
  78. any time when clock is configured.
  79. (++) Source of time base is configured to generate interrupts at regular
  80. time intervals. Care must be taken if ald_delay_ms() is called from a
  81. peripheral ISR process, the Tick interrupt line must have higher priority
  82. (numerically lower) than the peripheral interrupt. Otherwise the caller
  83. ISR process will be blocked.
  84. (++) functions affecting time base configurations are declared as __weak
  85. to make override possible in case of other implementations in user file.
  86. (+) Configure the interval of Systick interrupt.
  87. @endverbatim
  88. * @{
  89. */
  90. /**
  91. * @brief This function Configures time base source, NVIC and DMA.
  92. * @note This function is called at the beginning of program after reset and before
  93. * the clock configuration.
  94. * @note The time base configuration is based on MSI clock when exiting from Reset.
  95. * Once done, time base tick start incrementing.
  96. * In the default implementation, Systick is used as source of time base.
  97. * The tick variable is incremented each 1ms in its ISR.
  98. * @retval None
  99. */
  100. void ald_cmu_init(void)
  101. {
  102. NVIC_SetPriorityGrouping(NVIC_PRIORITY_GROUP_2);
  103. ald_cmu_clock_config_default();
  104. ald_tick_init(TICK_INT_PRIORITY);
  105. #ifdef ALD_DMA
  106. ald_cmu_perh_clock_config(CMU_PERH_DMA, ENABLE);
  107. ald_dma_init(DMA0);
  108. #endif
  109. return;
  110. }
  111. /**
  112. * @brief This function configures the source of the time base.
  113. * The time source is configured to have 1ms time base with a dedicated
  114. * Tick interrupt priority.
  115. * @note In the default implementation, SysTick timer is the source of time base.
  116. * It is used to generate interrupts at regular time intervals.
  117. * Care must be taken if ald_delay_ms() is called from a peripheral ISR process,
  118. * The SysTick interrupt must have higher priority (numerically lower)
  119. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  120. * The function is declared as __weak to be overwritten in case of other
  121. * implementation in user file.
  122. * @param prio: Tick interrupt priority.
  123. * @retval None
  124. */
  125. __weak void ald_tick_init(uint32_t prio)
  126. {
  127. /* Configure the SysTick IRQ */
  128. NVIC_SetPriority(SysTick_IRQn, prio);
  129. SysTick_Config(ald_cmu_get_sys_clock() / SYSTICK_INTERVAL_1MS);
  130. return;
  131. }
  132. /**
  133. * @brief Selects the interval of systick interrupt.
  134. * @param value: The value of interval:
  135. * @arg @ref SYSTICK_INTERVAL_1MS 1 millisecond
  136. * @arg @ref SYSTICK_INTERVAL_10MS 10 milliseconds
  137. * @arg @ref SYSTICK_INTERVAL_100MS 100 milliseconds
  138. * @arg @ref SYSTICK_INTERVAL_1000MS 1 second
  139. * @retval None
  140. */
  141. void ald_systick_interval_select(systick_interval_t value)
  142. {
  143. assert_param(IS_SYSTICK_INTERVAL(value));
  144. SysTick_Config(ald_cmu_get_sys_clock() / value);
  145. __systick_interval = value;
  146. if (TICK_INT_PRIORITY != 15)
  147. NVIC_SetPriority(SysTick_IRQn, TICK_INT_PRIORITY);
  148. return;
  149. }
  150. /**
  151. * @}
  152. */
  153. /** @defgroup ALD_Public_Functions_Group2 Control functions
  154. * @brief Control functions
  155. *
  156. * @verbatim
  157. ===============================================================================
  158. ##### Control functions #####
  159. ===============================================================================
  160. [..] This section provides functions allowing to:
  161. (+) Provide a tick value in millisecond
  162. (+) Provide a blocking delay in millisecond
  163. (+) Suspend the time base source interrupt
  164. (+) Resume the time base source interrupt
  165. (+) Get the ALD version
  166. (+) Waiting for flag
  167. (+) Configure the interrupt
  168. (+) Provide system tick value
  169. (+) Initialize core timestamp
  170. (+) Get core timestamp
  171. (+) Get CPU ID
  172. (+) Get UID
  173. (+) Get CHIPID
  174. @endverbatim
  175. * @{
  176. */
  177. /**
  178. * @brief This function invoked by Systick ISR.
  179. * @note This function is declared as __weak to be overwritten in case of
  180. * other implementations in user file.
  181. * @retval None
  182. */
  183. __weak void ald_systick_irq_cbk(void)
  184. {
  185. /* do nothing */
  186. return;
  187. }
  188. /**
  189. * @brief This function is called to increment a global variable "lib_tick"
  190. * used as application time base.
  191. * @note In the default implementation, this variable is incremented each 1ms
  192. * in Systick ISR.
  193. * @note This function is declared as __weak to be overwritten in case of other
  194. * implementations in user file.
  195. * @retval None
  196. */
  197. __weak void ald_inc_tick(void)
  198. {
  199. ++lib_tick;
  200. ald_systick_irq_cbk();
  201. }
  202. /**
  203. * @brief Provides a tick value in millisecond.
  204. * @note This function is declared as __weak to be overwritten in case of other
  205. * implementations in user file.
  206. * @retval tick value
  207. */
  208. __weak uint32_t ald_get_tick(void)
  209. {
  210. return lib_tick;
  211. }
  212. /**
  213. * @brief This function provides accurate delay (in milliseconds) based
  214. * on variable incremented.
  215. * @note In the default implementation, SysTick timer is the source of time base.
  216. * It is used to generate interrupts at regular time intervals where lib_tick
  217. * is incremented.
  218. * @note This function is declared as __weak to be overwritten in case of other
  219. * implementations in user file.
  220. * @param delay: specifies the delay time length, in milliseconds.
  221. * @retval None
  222. */
  223. __weak void ald_delay_ms(__IO uint32_t delay)
  224. {
  225. uint32_t tick, __delay;
  226. switch (__systick_interval) {
  227. case SYSTICK_INTERVAL_1MS:
  228. __delay = delay;
  229. break;
  230. case SYSTICK_INTERVAL_10MS:
  231. __delay = delay / 10;
  232. break;
  233. case SYSTICK_INTERVAL_100MS:
  234. __delay = delay / 100;
  235. break;
  236. case SYSTICK_INTERVAL_1000MS:
  237. __delay = delay / 1000;
  238. break;
  239. default:
  240. __delay = delay;
  241. break;
  242. }
  243. tick = ald_get_tick();
  244. __delay = __delay == 0 ? 1 : __delay;
  245. while ((ald_get_tick() - tick) < __delay)
  246. ;
  247. }
  248. /**
  249. * @brief Suspend Tick increment.
  250. * @note In the default implementation, SysTick timer is the source of time base.
  251. * It is used to generate interrupts at regular time intervals.
  252. * Once ald_suspend_tick() is called, the the SysTick interrupt
  253. * will be disabled and so Tick increment is suspended.
  254. * @note This function is declared as __weak to be overwritten
  255. * in case of other implementations in user file.
  256. * @retval None
  257. */
  258. __weak void ald_suspend_tick(void)
  259. {
  260. CLEAR_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
  261. }
  262. /**
  263. * @brief Resume Tick increment.
  264. * @note In the default implementation, SysTick timer is the source of
  265. * time base. It is used to generate interrupts at regular time
  266. * intervals. Once ald_resume_tick() is called, the the SysTick
  267. * interrupt will be enabled and so Tick increment is resumed.
  268. * @note This function is declared as __weak to be overwritten
  269. * in case of other implementations in user file.
  270. * @retval None
  271. */
  272. __weak void ald_resume_tick(void)
  273. {
  274. SET_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
  275. }
  276. /**
  277. * @brief This method returns the ALD revision
  278. * @retval version: 0xXYZR (8bits for each decimal, R for RC)
  279. */
  280. uint32_t ald_get_ald_version(void)
  281. {
  282. return __ALD_VERSION;
  283. }
  284. /**
  285. * @brief Configure the flash wait period.
  286. * @param cycle: The period.
  287. * @retval None
  288. */
  289. void ald_flash_wait_config(uint8_t cycle)
  290. {
  291. uint32_t tmp;
  292. tmp = MSC->MEMWAIT;
  293. MODIFY_REG(tmp, MSC_MEMWAIT_FLASH_W_MSK, (0x30U | cycle) << MSC_MEMWAIT_FLASH_W_POSS);
  294. MSC->MEMWAIT = tmp;
  295. return;
  296. }
  297. /**
  298. * @brief Waiting the specified bit in the register change to SET/RESET.
  299. * @param reg: The register address.
  300. * @param bit: The specified bit.
  301. * @param status: The status for waiting.
  302. * @param timeout: Timeout duration.
  303. * @retval Status, see @ref ald_status_t.
  304. */
  305. ald_status_t ald_wait_flag(uint32_t *reg, uint32_t bit, flag_status_t status, uint32_t timeout)
  306. {
  307. uint32_t tick = ald_get_tick();
  308. assert_param(timeout > 0);
  309. if (status == SET) {
  310. while (!(IS_BIT_SET(*reg, bit))) {
  311. if (((ald_get_tick()) - tick) > timeout)
  312. return TIMEOUT;
  313. }
  314. }
  315. else {
  316. while ((IS_BIT_SET(*reg, bit))) {
  317. if (((ald_get_tick()) - tick) > timeout)
  318. return TIMEOUT;
  319. }
  320. }
  321. return OK;
  322. }
  323. /**
  324. * @brief Configure interrupt.
  325. * @param irq: Interrunpt type.
  326. * @param preempt_prio: preempt priority(0-3).
  327. * @param sub_prio: sub-priority(0-3).
  328. * @param status: Status.
  329. * @arg ENABLE
  330. * @arg DISABLE
  331. * @retval None
  332. */
  333. void ald_mcu_irq_config(IRQn_Type irq, uint8_t preempt_prio, uint8_t sub_prio, type_func_t status)
  334. {
  335. uint32_t pri;
  336. uint8_t sub_bw, pre_bw;
  337. uint8_t sub_mask = 0xF;
  338. assert_param(IS_FUNC_STATE(status));
  339. assert_param(IS_PREEMPT_PRIO(preempt_prio));
  340. assert_param(IS_SUB_PRIO(sub_prio));
  341. if (status == ENABLE) {
  342. pre_bw = 7 - (((SCB->AIRCR) >> 8) & 7);
  343. sub_bw = 4 - pre_bw;
  344. sub_mask >>= pre_bw;
  345. pri = preempt_prio << sub_bw;
  346. pri |= sub_prio & sub_mask;
  347. NVIC_SetPriority(irq, pri);
  348. NVIC_EnableIRQ(irq);
  349. }
  350. else {
  351. NVIC_DisableIRQ(irq);
  352. }
  353. return;
  354. }
  355. /**
  356. * @brief Initialize core timestamp.
  357. * @retval None
  358. */
  359. void ald_mcu_timestamp_init(void)
  360. {
  361. DEM_CR |= (uint32_t)DEM_CR_TRCENA;
  362. DWT_CYCCNT = 0x0;
  363. DWT_CR |= (uint32_t)DWT_CR_CYCCNTEA;
  364. return;
  365. }
  366. /**
  367. * @brief Get core timestamp.
  368. * @retval None
  369. */
  370. uint32_t ald_mcu_get_timestamp(void)
  371. {
  372. return (uint32_t)DWT_CYCCNT;
  373. }
  374. /**
  375. * @brief Get the CPU ID.
  376. * @retval CPU ID.
  377. */
  378. uint32_t ald_mcu_get_cpu_id(void)
  379. {
  380. return SCB->CPUID;
  381. }
  382. /**
  383. * @brief Get the UID.
  384. * @param buf: Pointer to UID, len: 12Bytes(96-bits)
  385. * @retval None
  386. */
  387. void ald_mcu_get_uid(uint8_t *buf)
  388. {
  389. memcpy(&buf[0], (void *)MCU_UID0_ADDR, 4);
  390. memcpy(&buf[4], (void *)MCU_UID1_ADDR, 4);
  391. memcpy(&buf[8], (void *)MCU_UID2_ADDR, 4);
  392. return;
  393. }
  394. /**
  395. * @brief Get the CHIPID
  396. * @retval CHPID
  397. */
  398. uint32_t ald_mcu_get_chipid(void)
  399. {
  400. return (uint32_t)*(uint32_t *)MCU_CHIPID_ADDR;
  401. }
  402. /**
  403. * @}
  404. */
  405. /**
  406. * @}
  407. */
  408. /**
  409. * @}
  410. */
  411. /**
  412. * @}
  413. */