utils.c 12 KB

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