System_ACM32F4.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. ******************************************************************************
  3. * @file System_ACM32F4.c
  4. * @version V1.0.0
  5. * @date 2021
  6. * @brief System Source File, includes clock management, reset management
  7. * and IO configuration, ...
  8. ******************************************************************************
  9. */
  10. #include "ACM32Fxx_HAL.h"
  11. uint32_t gu32_SystemClock;
  12. uint32_t gu32_APBClock;
  13. /* System count in SysTick_Handler */
  14. volatile uint32_t gu32_SystemCount;
  15. /************************* Miscellaneous Configuration ************************/
  16. /*!< Uncomment the following line if you need to relocate your vector Table in
  17. Internal SRAM. */
  18. /* #define VECT_TAB_SRAM */
  19. #define VECT_TAB_OFFSET 0x0U /*!< Vector Table base offset field.
  20. This value must be a multiple of 0x200. */
  21. /******************************************************************************/
  22. /*********************************************************************************
  23. * Function : HardFault_Handler
  24. * Description : Hard Fault handle, while(1) loop, wait for debug
  25. * Input : none
  26. * Output : none
  27. * Author : xwl
  28. **********************************************************************************/
  29. //void HardFault_Handler(void) //implemented in context_rvds.S
  30. //{
  31. // while(1);
  32. //}
  33. /*********************************************************************************
  34. * Function : SysTick_Handler
  35. * Description : System tick handler
  36. * Input : none
  37. * Output : none
  38. * Author : Chris_Kyle
  39. **********************************************************************************/
  40. //void SysTick_Handler(void) //implemented in board.c
  41. //{
  42. // gu32_SystemCount++;
  43. //}
  44. /*********************************************************************************
  45. * Function : System_SysTick_Init
  46. * Description : System Tick Init. Period is 1 ms
  47. * Input : none
  48. * Output : none
  49. * Author : Chris_Kyle
  50. **********************************************************************************/
  51. //void System_SysTick_Init(void) //implemented in board.c/rt_hw_board_init()
  52. //{
  53. // gu32_SystemCount = 0;
  54. // SysTick_Config(gu32_SystemClock / 1000); //1ms/tick
  55. //}
  56. /*********************************************************************************
  57. * Function : System_SysTick_Off
  58. * Description : Turn off System Tick
  59. * Input : none
  60. * Output : none
  61. * Author : xwl
  62. **********************************************************************************/
  63. //void System_SysTick_Off(void)
  64. //{
  65. // SysTick->CTRL = 0;
  66. //}
  67. /*********************************************************************************
  68. * Function : System_Init
  69. * Description : Initialize the system clock, accelerate function and system tick.
  70. * Input : none
  71. * Output : none
  72. * Author : Chris_Kyle
  73. **********************************************************************************/
  74. void System_Init(void)
  75. {
  76. SCU->RCR |= SCU_RCR_REMAP_EN; // always remap enabled
  77. System_Set_Buzzer_Divider(80, FUNC_DISABLE); // disable clock out
  78. /* 3 bits for pre-emption priority, 0 bits for subpriority */
  79. NVIC_SetPriorityGrouping(NVIC_PRIORITY_GROUP_3);
  80. /* Initialize the system clock */
  81. if (false == System_Clock_Init(DEFAULT_SYSTEM_CLOCK))
  82. {
  83. while(1);
  84. }
  85. #if (__ACCELERATE_PRESENT == 1)
  86. System_EnableIAccelerate();
  87. #endif
  88. #if (__ACCELERATE_EH_PRESENT == 1)
  89. System_EnableDAccelerate();
  90. #endif
  91. #ifdef HAL_SYSTICK_ENABLED // To activate macro in ACM32Fxx_HAL.h
  92. //System_SysTick_Init();
  93. #endif
  94. }
  95. /*********************************************************************************
  96. * Function : System_Core_Config
  97. * Description : configure FPU and vector table address
  98. * Input : none
  99. * Output : none
  100. * Author : Chris_Kyle
  101. **********************************************************************************/
  102. void System_Core_Config(void)
  103. {
  104. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  105. /* set CP10 and CP11 Full Access */
  106. SCB->CPACR |= ((3UL << 10*2) | (3UL << 11*2));
  107. #endif
  108. /* Configure the Vector Table location add offset address ------------------*/
  109. #ifdef VECT_TAB_SRAM
  110. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  111. #else
  112. SCB->VTOR = EFLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  113. #endif
  114. }
  115. /*********************************************************************************
  116. * Function : System_Clock_Init
  117. * Description : Clock init
  118. * Input : fu32_Clock: System core clock frequency, measured as Hz
  119. * Output : 0: success, other value: fail reason
  120. * Author : xwl
  121. **********************************************************************************/
  122. bool System_Clock_Init(uint32_t fu32_Clock)
  123. {
  124. volatile uint32_t lu32_sysdiv, lu32_pclkdiv, lu32_timeout, lu32_pll_src, lu32_pclk_div_para, lu32_result;
  125. SET_EFC_RD_WAIT(RD_WAIT_ENSURE_OK)
  126. lu32_result = 0;
  127. lu32_pll_src = PLL_SOURCE_FROM;
  128. if(0 == (SCU->RCHCR & SCU_RCHCR_RCHRDY))
  129. {
  130. SCU->RCHCR |= SCU_RCHCR_RCH_EN;
  131. while(0 == (SCU->RCHCR & SCU_RCHCR_RCHRDY)); // wait RCH ready
  132. }
  133. SCU->CCR1 = 0; // select RC64M as default
  134. if (fu32_Clock <= 64000000)
  135. {
  136. if ((SCU->RCHCR) & SCU_RCHCR_RCH_DIV)
  137. {
  138. SCU->RCHCR &= (~SCU_RCHCR_RCH_DIV);
  139. while(0 == (SCU->RCHCR & SCU_RCHCR_RCHRDY));
  140. }
  141. if (fu32_Clock == 32000000)
  142. {
  143. gu32_SystemClock = fu32_Clock;
  144. lu32_sysdiv = 2;
  145. lu32_pclkdiv = 1; // pclk = hclk
  146. }
  147. else
  148. {
  149. gu32_SystemClock = 64000000;
  150. lu32_sysdiv = 1;
  151. lu32_pclkdiv = 1;
  152. }
  153. gu32_APBClock = gu32_SystemClock/lu32_pclkdiv;
  154. }
  155. // select pll as system clock
  156. else
  157. {
  158. if (PLLCLK_SRC_RC4M == lu32_pll_src)
  159. {
  160. SCU->RCHCR |= SCU_RCHCR_RCH_DIV;
  161. while(!(SCU->RCHCR & SCU_RCHCR_RCHRDY));
  162. SCU->PLLCR |= SCU_PLLCR_PLL_EN;
  163. SCU->PLLCR &= ~(SCU_PLLCR_PLL_SLEEP);
  164. while(!(SCU->PLLCR & SCU_PLLCR_PLL_FREE_RUN));
  165. switch(fu32_Clock)
  166. {
  167. case 180000000: // 180M
  168. {
  169. SCU->PLLCR = (SCU->PLLCR & ~(0xFFFF8)) | (33 << 3);
  170. SCU->PLLCR |= SCU_PLLCR_PLL_UPDATE_EN;
  171. while(!(SCU->PLLCR & (SCU_PLLCR_PLL_FREE_RUN) ) );
  172. lu32_sysdiv = 1;
  173. lu32_pclkdiv = 2; // pclk = hclk/2
  174. }break;
  175. case 120000000: // 120M
  176. {
  177. SCU->PLLCR = (SCU->PLLCR & ~(0xFFFF8)) | (18U << 3);
  178. SCU->PLLCR |= SCU_PLLCR_PLL_UPDATE_EN;
  179. while(!(SCU->PLLCR & (SCU_PLLCR_PLL_FREE_RUN) ) );
  180. lu32_sysdiv = 1;
  181. lu32_pclkdiv = 2; // pclk = hclk/2
  182. }break;
  183. default: lu32_result = 1; break;
  184. }
  185. gu32_SystemClock = fu32_Clock;
  186. gu32_APBClock = gu32_SystemClock/lu32_pclkdiv;
  187. SCU->CCR1 = SCU_CCR1_SYS_PLL; // configure system clock as PLL clock
  188. }
  189. else if (SCU_XTHCR_XTH_EN == lu32_pll_src)
  190. {
  191. lu32_timeout = 0;
  192. SCU->XTHCR = SCU_XTHCR_XTH_EN | SCU_XTHCR_READYTIME_32768;
  193. while (0 == (SCU->XTHCR & SCU_XTHCR_XTHRDY))
  194. {
  195. if (lu32_timeout == SYSTEM_TIMEOUT)
  196. {
  197. lu32_result = 2;
  198. break;
  199. }
  200. lu32_timeout++;
  201. }
  202. if (0 == lu32_result)
  203. {
  204. SCU->PLLCR |= SCU_PLLCR_PLL_EN;
  205. SCU->PLLCR &= ~(SCU_PLLCR_PLL_SLEEP);
  206. while(!(SCU->PLLCR & (SCU_PLLCR_PLL_FREE_RUN) ));
  207. switch(fu32_Clock)
  208. {
  209. case 180000000: // 180M
  210. {
  211. SCU->PLLCR = (SCU->PLLCR &(~(0x1FFFFU << 3))) | (18U << 3) | (1U << 12) | (0U << 16);
  212. SCU->PLLCR = (SCU->PLLCR & (~(0x3U << 1)) ) | (3 << 1);
  213. SCU->PLLCR |= SCU_PLLCR_PLL_UPDATE_EN;
  214. while(!(SCU->PLLCR & (SCU_PLLCR_PLL_FREE_RUN) ) );
  215. lu32_sysdiv = 1;
  216. lu32_pclkdiv = 2; // pclk = hclk/2
  217. }break;
  218. case 120000000: // 120M
  219. {
  220. SCU->PLLCR = (SCU->PLLCR &(~(0x1FFFFU << 3))) | (18U << 3) | (2U << 12) | (0U << 16);
  221. SCU->PLLCR = (SCU->PLLCR & (~(0x3U << 1)) ) | (3 << 1); // select XTH
  222. SCU->PLLCR |= SCU_PLLCR_PLL_UPDATE_EN;
  223. while(!(SCU->PLLCR & (SCU_PLLCR_PLL_FREE_RUN) ) );
  224. lu32_sysdiv = 1;
  225. lu32_pclkdiv = 2; // pclk = hclk/2
  226. }break;
  227. default: lu32_result = 1; break;
  228. }
  229. }
  230. gu32_SystemClock = fu32_Clock;
  231. gu32_APBClock = gu32_SystemClock/lu32_pclkdiv;
  232. SCU->CCR1 = SCU_CCR1_SYS_PLL; // configure system clock as PLL clock
  233. }
  234. else
  235. {
  236. lu32_result = 3;
  237. }
  238. }
  239. if (0 == lu32_result)
  240. {
  241. if (1 == lu32_pclkdiv)
  242. {
  243. lu32_pclk_div_para = 0;
  244. }
  245. else if (2 == lu32_pclkdiv)
  246. {
  247. lu32_pclk_div_para = 4; // pclk = hclk/2
  248. }
  249. else
  250. {
  251. lu32_pclk_div_para = 5; // pclk = hclk/4
  252. }
  253. }
  254. else
  255. {
  256. lu32_sysdiv = 1;
  257. lu32_pclk_div_para = 0;
  258. }
  259. SCU->CCR2 = (SCU->CCR2 & (~0x7FFU)) | (lu32_sysdiv-1) | (lu32_pclk_div_para << 8);
  260. while((SCU->CCR2 & SCU_CCR2_DIVDONE) == 0x00); // wait divide done
  261. HAL_EFlash_Init(gu32_SystemClock);
  262. return (lu32_result == 0);
  263. }
  264. /*********************************************************************************
  265. * Function : System_Get_SystemClock
  266. * Description : get AHB clock frequency
  267. * Input : none
  268. * Output : frequency, measured as Hz
  269. * Author : Chris_Kyle
  270. **********************************************************************************/
  271. uint32_t System_Get_SystemClock(void)
  272. {
  273. return gu32_SystemClock;
  274. }
  275. /*********************************************************************************
  276. * Function : System_Get_APBClock
  277. * Description : get APB clock frequency
  278. * Input : none
  279. * Output : frequency, measured as Hz
  280. * Author : Chris_Kyle
  281. **********************************************************************************/
  282. uint32_t System_Get_APBClock(void)
  283. {
  284. return gu32_APBClock;
  285. }
  286. /*********************************************************************************
  287. * Function : System_Module_Reset
  288. * Description : reset module
  289. * Input : module id
  290. * Output : none
  291. * Author : Chris_Kyle
  292. **********************************************************************************/
  293. void System_Module_Reset(enum_RST_ID_t fe_ID_Index)
  294. {
  295. if (fe_ID_Index > 31)
  296. {
  297. SCU->IPRST2 &= ~(1 << (fe_ID_Index - 32));
  298. System_Delay(5);
  299. SCU->IPRST2 |= 1 << (fe_ID_Index - 32);
  300. }
  301. else
  302. {
  303. SCU->IPRST1 &= ~(1 << fe_ID_Index);
  304. System_Delay(5);
  305. SCU->IPRST1 |= 1 << fe_ID_Index;
  306. }
  307. }
  308. /*********************************************************************************
  309. * Function : System_Module_Enable
  310. * Description : enable module clock
  311. * Input : module id
  312. * Output : none
  313. * Author : Chris_Kyle
  314. **********************************************************************************/
  315. void System_Module_Enable(enum_Enable_ID_t fe_ID_Index)
  316. {
  317. if (fe_ID_Index > 13)
  318. {
  319. SCU->IPCKENR1 |= 1 << (fe_ID_Index - 14);
  320. }
  321. else
  322. {
  323. SCU->IPCKENR2 |= 1 << fe_ID_Index;
  324. }
  325. System_Delay(2);
  326. }
  327. /*********************************************************************************
  328. * Function : System_Module_Disable
  329. * Description : disable module clock
  330. * Input : module id
  331. * Output : none
  332. * Author : Chris_Kyle
  333. **********************************************************************************/
  334. void System_Module_Disable(enum_Enable_ID_t fe_ID_Index)
  335. {
  336. if (fe_ID_Index > 13)
  337. {
  338. SCU->IPCKENR1 &= ~(1 << (fe_ID_Index - 14));
  339. }
  340. else
  341. {
  342. SCU->IPCKENR2 &= ~(1 << fe_ID_Index);
  343. }
  344. }
  345. /*********************************************************************************
  346. * Function : System_Delay
  347. * Description : NOP delay
  348. * Input : count
  349. * Output : none
  350. * Author : Chris_Kyle
  351. **********************************************************************************/
  352. void System_Delay(volatile uint32_t fu32_Delay)
  353. {
  354. while (fu32_Delay--);
  355. }
  356. /*********************************************************************************
  357. * Function : System_Delay_MS
  358. * Description : ms delay. Use this Function must call System_SysTick_Init()
  359. * Input : delay period, measured as ms
  360. * Output : none
  361. * Author : Chris_Kyle
  362. **********************************************************************************/
  363. void System_Delay_MS(volatile uint32_t fu32_Delay)
  364. {
  365. uint32_t lu32_SystemCountBackup;
  366. lu32_SystemCountBackup = gu32_SystemCount;
  367. while ( (gu32_SystemCount - lu32_SystemCountBackup) < fu32_Delay);
  368. }
  369. /*********************************************************************************
  370. * Function : System_Enable_RC32K
  371. * Description : Enable RC32K, make sure RTC Domain Access is allowed
  372. * Input : none
  373. * Output : none
  374. * Author : CWT
  375. **********************************************************************************/
  376. void System_Enable_RC32K(void)
  377. {
  378. PMU->ANACR |= (1 << 8);
  379. while(0 == ((PMU->ANACR) & (1U << 9)));
  380. }
  381. /*********************************************************************************
  382. * Function : System_Disable_RC32K
  383. * Description : Disable RC32K
  384. * Input : none
  385. * Output : none
  386. * Author : CWT
  387. **********************************************************************************/
  388. void System_Disable_RC32K(void)
  389. {
  390. PMU->ANACR &= (~(1 << 8));
  391. }
  392. /*********************************************************************************
  393. * Function : System_Enable_XTAL
  394. * Description : Enable XTAL, make sure RTC Domain Access is allowed
  395. * Input : none
  396. * Output : none
  397. * Author : CWT
  398. **********************************************************************************/
  399. void System_Enable_XTAL(void)
  400. {
  401. PMU->ANACR = (PMU->ANACR & ~RPMU_ANACR_XTLDRV) | (RPMU_ANACR_XTLDRV_1 | RPMU_ANACR_XTLDRV_0);
  402. PMU->ANACR |= RPMU_ANACR_XTLEN;
  403. while(!(PMU->ANACR & RPMU_ANACR_XTLRDY));
  404. PMU->CR1 |= RTC_CLOCK_XTL;
  405. }
  406. /*********************************************************************************
  407. * Function : System_Disable_XTAL
  408. * Description : Disable XTAL
  409. * Input : none
  410. * Output : none
  411. * Author : CWT
  412. **********************************************************************************/
  413. void System_Disable_XTAL(void)
  414. {
  415. PMU->ANACR &= (~(RPMU_ANACR_XTLEN));
  416. }
  417. /*********************************************************************************
  418. * Function : System_Enable_Disable_RTC_Domain_Access
  419. * Description : Enable or Disable RTC Domain Access.
  420. * Input : enable or disable
  421. * Output : none
  422. * Author : CWT
  423. **********************************************************************************/
  424. void System_Enable_Disable_RTC_Domain_Access(FUNC_DISABLE_ENABLE enable_disable)
  425. {
  426. if (FUNC_DISABLE == enable_disable)
  427. {
  428. SCU->STOPCFG &= (~SCU_STOPCFG_RTC_WE);
  429. }
  430. else
  431. {
  432. SCU->STOPCFG |= SCU_STOPCFG_RTC_WE;
  433. System_Delay(1);
  434. RTC->WP = 0xCA53CA53U;
  435. }
  436. }
  437. /*********************************************************************************
  438. * Function : System_Enable_Disable_Reset
  439. * Description : Enable or Disable System Reset source.
  440. * Input : none
  441. * Output : none
  442. * Author : CWT
  443. **********************************************************************************/
  444. void System_Enable_Disable_Reset(RESET_ENABLE_SOURCE source, FUNC_DISABLE_ENABLE enable_disable)
  445. {
  446. switch(source)
  447. {
  448. /* reset source: from bit0 to bit3 */
  449. case RESET_ENABLE_SOURCE_LVD:
  450. case RESET_ENABLE_SOURCE_WDT:
  451. case RESET_ENABLE_SOURCE_IWDT:
  452. case RESET_ENABLE_SOURCE_LOCKUP:
  453. if (FUNC_DISABLE == enable_disable)
  454. {
  455. SCU->RCR &= (~(1U << source));
  456. }
  457. else
  458. {
  459. SCU->RCR |= (1U << source);
  460. }
  461. break;
  462. default: break;
  463. }
  464. }
  465. /*********************************************************************************
  466. * Function : System_Reset_MCU
  467. * Description : reset mcu
  468. * Input : reset source
  469. * Output : none
  470. * Author : xwl
  471. **********************************************************************************/
  472. void System_Reset_MCU(RESET_SOURCE source)
  473. {
  474. switch(source)
  475. {
  476. case RESET_SOURCE_EFC:
  477. {
  478. SCU->RCR &= (~BIT29);
  479. while(1);
  480. }break;
  481. case RESET_SOURCE_NVIC_RESET:
  482. {
  483. NVIC_SystemReset();
  484. while(1);
  485. }break;
  486. case RESET_SOFT_RESET:
  487. {
  488. SCU->RCR &= (~BIT30);
  489. while(1);
  490. }break;
  491. default: break;
  492. }
  493. }
  494. /*********************************************************************************
  495. * Function : System_Enter_Standby_Mode
  496. * Description : try to enter standby mode
  497. * Input : none
  498. * Output : none
  499. * Author : xwl Date : 2021
  500. **********************************************************************************/
  501. void System_Enter_Standby_Mode(void)
  502. {
  503. __set_PRIMASK(1); // disable interrupt
  504. SysTick->CTRL = 0; // disable systick
  505. SCU->STOPCFG |= BIT11; // set PDDS=1
  506. /* Set SLEEPDEEP bit of Cortex System Control Register */
  507. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  508. __WFI();
  509. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  510. System_Delay(100);
  511. printfS("Enter Standby Mode Failed! \n"); // should not go here
  512. }
  513. /*********************************************************************************
  514. * Function : System_Clear_Stop_Wakeup
  515. * Description : clear all stop setting and status
  516. * Input : none
  517. * Output : none
  518. * Author : CWT Date : 2021?¨º
  519. **********************************************************************************/
  520. void System_Clear_Stop_Wakeup(void)
  521. {
  522. EXTI->IENR = 0;
  523. EXTI->RTENR = 0;
  524. EXTI->FTENR = 0;
  525. EXTI->SWIER = 0;
  526. EXTI->PDR = 0x7FFFFFU;
  527. }
  528. /*********************************************************************************
  529. * Function : System_Enter_Stop_Mode
  530. * Description : try to enter stop mode
  531. * Input : STOPEntry: STOPENTRY_WFI or STOPENTRY_WFE
  532. * Output : none
  533. * Author : CWT Date : 2021
  534. **********************************************************************************/
  535. void System_Enter_Stop_Mode(uint8_t STOPEntry)
  536. {
  537. /* Set SLEEPDEEP bit of Cortex System Control Register */
  538. SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  539. SCU->STOPCFG &= (~BIT11); // PDDS=0
  540. //System_SysTick_Off();
  541. /* Select Stop mode entry */
  542. if(STOPEntry == STOPENTRY_WFI)
  543. {
  544. /* Wait For Interrupt */
  545. __WFI();
  546. }
  547. else
  548. {
  549. __SEV();
  550. __WFE();
  551. __WFE(); /* Wait For Event */
  552. }
  553. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  554. #ifdef HAL_SYSTICK_ENABLED // To activate macro in ACM32Fxx_HAL.h
  555. //System_SysTick_Init();
  556. #endif
  557. }
  558. /*********************************************************************************
  559. * Function : System_Enter_Sleep_Mode
  560. * Description : try to enter sleep mode
  561. * Input : SleepEntry: SLEEPENTRY_WFI or SLEEPENTRY_WFE
  562. * Output : none
  563. * Author : CWT Date : 2021
  564. **********************************************************************************/
  565. void System_Enter_Sleep_Mode(uint8_t SleepEntry)
  566. {
  567. /* clear SLEEPDEEP bit of Cortex System Control Register */
  568. CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));
  569. /* Select Stop mode entry */
  570. if(SleepEntry == SLEEPENTRY_WFI)
  571. {
  572. /* Wait For Interrupt */
  573. __WFI();
  574. }
  575. else
  576. {
  577. __SEV();
  578. __WFE();
  579. __WFE(); /*Wait For Event */
  580. }
  581. }
  582. /*********************************************************************************
  583. * Function : System_Return_Last_Reset_Reason
  584. * Description : Get System Last Reset Reason
  585. * Input : none
  586. * Output : RESET_REASON
  587. * Author : CWT Date : 2021?¨º
  588. **********************************************************************************/
  589. RESET_REASON System_Return_Last_Reset_Reason(void)
  590. {
  591. RESET_REASON Reset_Reason_Save;
  592. RESET_REASON i = RESET_REASON_POR;
  593. for(i = RESET_REASON_POR; i >= RESET_REASON_POR12; i--)
  594. {
  595. if ((SCU->RSR) & (1U << i))
  596. {
  597. SCU->RSR |= SCU_RSR_RSTFLAG_CLR; // clear reset reason flags
  598. Reset_Reason_Save = i;
  599. return i;
  600. }
  601. }
  602. for(i = RESET_REASON_LOW_VOLTAGE; i <= RESET_REASON_SOFT; i++)
  603. {
  604. if ((SCU->RSR) & (1U << i))
  605. {
  606. SCU->RSR |= SCU_RSR_RSTFLAG_CLR; // clear reset reason flags
  607. Reset_Reason_Save = i;
  608. return i;
  609. }
  610. }
  611. return RESET_REASON_INVALID; // this should not happen
  612. }
  613. /*********************************************************************************
  614. * Function : System_Set_Buzzer_Divider
  615. * Description : set buzzer divide factor
  616. * Input :
  617. div: div factor, if div = 80 then output buzzer freq=HCLK/80
  618. enable: FUNC_DISABLE and FUNC_ENABLE
  619. * Output : none
  620. * Author : xwl Date : 2021?¡§o
  621. **********************************************************************************/
  622. void System_Set_Buzzer_Divider(uint32_t div, FUNC_DISABLE_ENABLE enable)
  623. {
  624. if (FUNC_ENABLE == enable)
  625. {
  626. SCU->CLKOCR = (SCU->CLKOCR & (~(0x1FFFFU << 5) ) ) | (div << 5);
  627. SCU->CLKOCR |= BIT23;
  628. }
  629. else
  630. {
  631. SCU->CLKOCR &= (~BIT23);
  632. }
  633. }
  634. /*********************************************************************************
  635. * Function : System_USB_PHY_Config
  636. * Description : Configure USB PHY, such as clock select, pll...
  637. * Input : none
  638. * Output : 0: fail, 1:success
  639. * Author : xwl Date : 2021?¨º
  640. **********************************************************************************/
  641. uint32_t System_USB_PHY_Config(void)
  642. {
  643. volatile uint32_t delay_count;
  644. SCU->PHYCR &= (~BIT2); // exit power down, auto select clock source
  645. delay_count = SYSTEM_TIMEOUT;
  646. while(delay_count--)
  647. {
  648. if (SCU->PHYCR & (BIT19)) // clksel_end flag = 1
  649. {
  650. break;
  651. }
  652. }
  653. if (delay_count)
  654. {
  655. return HAL_OK;
  656. }
  657. else
  658. {
  659. return HAL_TIMEOUT;
  660. }
  661. }