utils.c 12 KB

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