utils.c 12 KB

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