utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 2017
  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 ES32F0xx 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. ald_cmu_clock_config_default();
  102. ald_tick_init(TICK_INT_PRIORITY);
  103. #ifdef ALD_DMA
  104. ald_dma_init(DMA0);
  105. #endif
  106. return;
  107. }
  108. /**
  109. * @brief This function configures the source of the time base.
  110. * The time source is configured to have 1ms time base with a dedicated
  111. * Tick interrupt priority.
  112. * @note In the default implementation, SysTick timer is the source of time base.
  113. * It is used to generate interrupts at regular time intervals.
  114. * Care must be taken if ald_delay_ms() is called from a peripheral ISR process,
  115. * The SysTick interrupt must have higher priority (numerically lower)
  116. * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
  117. * The function is declared as __weak to be overwritten in case of other
  118. * implementation in user file.
  119. * @param prio: Tick interrupt priority.
  120. * @retval None
  121. */
  122. __weak void ald_tick_init(uint32_t prio)
  123. {
  124. /* Configure the SysTick IRQ */
  125. SysTick_Config(ald_cmu_get_sys_clock() / SYSTICK_INTERVAL_1MS);
  126. if (prio != 3)
  127. NVIC_SetPriority(SysTick_IRQn, prio);
  128. return;
  129. }
  130. /**
  131. * @brief Selects the interval of systick interrupt.
  132. * @param value: The value of interval:
  133. * @arg @ref SYSTICK_INTERVAL_1MS 1 millisecond
  134. * @arg @ref SYSTICK_INTERVAL_10MS 10 milliseconds
  135. * @arg @ref SYSTICK_INTERVAL_100MS 100 milliseconds
  136. * @arg @ref SYSTICK_INTERVAL_1000MS 1 second
  137. * @retval None
  138. */
  139. void ald_systick_interval_select(systick_interval_t value)
  140. {
  141. assert_param(IS_SYSTICK_INTERVAL(value));
  142. SysTick_Config(ald_cmu_get_sys_clock() / value);
  143. __systick_interval = value;
  144. if (TICK_INT_PRIORITY != 3)
  145. NVIC_SetPriority(SysTick_IRQn, TICK_INT_PRIORITY);
  146. return;
  147. }
  148. /**
  149. * @}
  150. */
  151. /** @defgroup ALD_Public_Functions_Group2 Control functions
  152. * @brief Control functions
  153. *
  154. * @verbatim
  155. ===============================================================================
  156. ##### Control functions #####
  157. ===============================================================================
  158. [..] This section provides functions allowing to:
  159. (+) Provide a tick value in millisecond
  160. (+) Provide a blocking delay in millisecond
  161. (+) Suspend the time base source interrupt
  162. (+) Resume the time base source interrupt
  163. (+) Get the ALD version
  164. (+) Waiting for flag
  165. (+) Configure the interrupt
  166. (+) Provide system tick value
  167. (+) Get CPU ID
  168. @endverbatim
  169. * @{
  170. */
  171. /**
  172. * @brief This function is called to increment a global variable "lib_tick"
  173. * used as application time base.
  174. * @note In the default implementation, this variable is incremented each 1ms
  175. * in Systick ISR.
  176. * @note This function is declared as __weak to be overwritten in case of other
  177. * implementations in user file.
  178. * @retval None
  179. */
  180. __weak void ald_inc_tick_weak(void)
  181. {
  182. ++lib_tick;
  183. }
  184. /**
  185. * @brief This function invoked by Systick ISR.
  186. * @note This function is declared as __weak to be overwritten in case of
  187. * other implementations in user file.
  188. * @retval None
  189. */
  190. __weak void ald_systick_irq_cbk(void)
  191. {
  192. /* do nothing */
  193. return;
  194. }
  195. /**
  196. * @brief This function invoked by Systick ISR each 1ms.
  197. * @retval None
  198. */
  199. __isr__ void ald_inc_tick(void)
  200. {
  201. ald_inc_tick_weak();
  202. ald_systick_irq_cbk();
  203. return;
  204. }
  205. /**
  206. * @brief Provides a tick value in millisecond.
  207. * @note This function is declared as __weak to be overwritten in case of other
  208. * implementations in user file.
  209. * @retval tick value
  210. */
  211. __weak uint32_t ald_get_tick(void)
  212. {
  213. return lib_tick;
  214. }
  215. /**
  216. * @brief This function provides accurate delay (in milliseconds) based
  217. * on variable incremented.
  218. * @note In the default implementation, SysTick timer is the source of time base.
  219. * It is used to generate interrupts at regular time intervals where lib_tick
  220. * is incremented.
  221. * @note This function is declared as __weak to be overwritten in case of other
  222. * implementations in user file.
  223. * @param delay: specifies the delay time length, in milliseconds.
  224. * @retval None
  225. */
  226. __weak void ald_delay_ms(__IO uint32_t delay)
  227. {
  228. uint32_t tick, __delay;
  229. switch (__systick_interval)
  230. {
  231. case SYSTICK_INTERVAL_1MS:
  232. __delay = delay;
  233. break;
  234. case SYSTICK_INTERVAL_10MS:
  235. __delay = delay / 10;
  236. break;
  237. case SYSTICK_INTERVAL_100MS:
  238. __delay = delay / 100;
  239. break;
  240. case SYSTICK_INTERVAL_1000MS:
  241. __delay = delay / 1000;
  242. break;
  243. default:
  244. __delay = delay;
  245. break;
  246. }
  247. tick = ald_get_tick();
  248. __delay = __delay == 0 ? 1 : __delay;
  249. while ((ald_get_tick() - tick) < __delay)
  250. ;
  251. }
  252. /**
  253. * @brief Suspend Tick increment.
  254. * @note In the default implementation, SysTick timer is the source of time base.
  255. * It is used to generate interrupts at regular time intervals.
  256. * Once ald_suspend_tick() is called, the the SysTick interrupt
  257. * will be disabled and so Tick increment is suspended.
  258. * @note This function is declared as __weak to be overwritten
  259. * in case of other implementations in user file.
  260. * @retval None
  261. */
  262. __weak void ald_suspend_tick(void)
  263. {
  264. CLEAR_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
  265. }
  266. /**
  267. * @brief Resume Tick increment.
  268. * @note In the default implementation, SysTick timer is the source of
  269. * time base. It is used to generate interrupts at regular time
  270. * intervals. Once ald_resume_tick() is called, the the SysTick
  271. * interrupt will be enabled and so Tick increment is resumed.
  272. * @note This function is declared as __weak to be overwritten
  273. * in case of other implementations in user file.
  274. * @retval None
  275. */
  276. __weak void ald_resume_tick(void)
  277. {
  278. SET_BIT(SysTick->CTRL, SysTick_CTRL_TICKINT_Msk);
  279. }
  280. /**
  281. * @brief This method returns the ALD revision
  282. * @retval version: 0xXYZR (8bits for each decimal, R for RC)
  283. */
  284. uint32_t ald_get_ald_version(void)
  285. {
  286. return __ALD_VERSION;
  287. }
  288. /**
  289. * @brief Waiting the specified bit in the register change to SET/RESET.
  290. * @param reg: The register address.
  291. * @param bit: The specified bit.
  292. * @param status: The status for waiting.
  293. * @param timeout: Timeout duration.
  294. * @retval Status, see @ref ald_status_t.
  295. */
  296. ald_status_t ald_wait_flag(uint32_t *reg, uint32_t bit, flag_status_t status, uint32_t timeout)
  297. {
  298. uint32_t tick = ald_get_tick();
  299. assert_param(timeout > 0);
  300. if (status == SET)
  301. {
  302. while (!(IS_BIT_SET(*reg, bit)))
  303. {
  304. if (((ald_get_tick()) - tick) > timeout)
  305. return TIMEOUT;
  306. }
  307. }
  308. else
  309. {
  310. while ((IS_BIT_SET(*reg, bit)))
  311. {
  312. if (((ald_get_tick()) - tick) > timeout)
  313. return TIMEOUT;
  314. }
  315. }
  316. return OK;
  317. }
  318. /**
  319. * @brief Configure interrupt.
  320. * @param irq: Interrunpt type.
  321. * @param prio: preempt priority(0-3).
  322. * @param status: Status.
  323. * @arg ENABLE
  324. * @arg DISABLE
  325. * @retval None
  326. */
  327. void ald_mcu_irq_config(IRQn_Type irq, uint8_t prio, type_func_t status)
  328. {
  329. assert_param(IS_FUNC_STATE(status));
  330. assert_param(IS_PRIO(prio));
  331. if (status == ENABLE)
  332. {
  333. NVIC_SetPriority(irq, prio);
  334. NVIC_EnableIRQ(irq);
  335. }
  336. else
  337. {
  338. NVIC_DisableIRQ(irq);
  339. }
  340. return;
  341. }
  342. /**
  343. * @brief Get the system tick.
  344. * @retval The value of current tick.
  345. */
  346. uint32_t ald_mcu_get_tick(void)
  347. {
  348. uint32_t load = SysTick->LOAD;
  349. uint32_t val = SysTick->VAL;
  350. return (load - val);
  351. }
  352. /**
  353. * @brief Get the CPU ID.
  354. * @retval CPU ID.
  355. */
  356. uint32_t ald_mcu_get_cpu_id(void)
  357. {
  358. return SCB->CPUID;
  359. }
  360. /**
  361. * @}
  362. */
  363. /**
  364. * @}
  365. */
  366. /**
  367. * @}
  368. */
  369. /**
  370. * @}
  371. */