system_fm33lc0xx.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**************************************************************************//**
  2. * @file system_fm33lc0xx.c
  3. * @brief CMSIS Cortex-M0 Device Peripheral Access Layer Source File for
  4. * Device FM33LC0XX
  5. * @version V2.00
  6. * @date 15. March 2021
  7. *
  8. * @note
  9. *
  10. ******************************************************************************/
  11. /* Copyright (c) 2012 ARM LIMITED
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions are met:
  15. - Redistributions of source code must retain the above copyright
  16. notice, this list of conditions and the following disclaimer.
  17. - Redistributions in binary form must reproduce the above copyright
  18. notice, this list of conditions and the following disclaimer in the
  19. documentation and/or other materials provided with the distribution.
  20. - Neither the name of ARM nor the names of its contributors may be used
  21. to endorse or promote products derived from this software without
  22. specific prior written permission.
  23. *
  24. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  28. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. POSSIBILITY OF SUCH DAMAGE.
  35. ---------------------------------------------------------------------------*/
  36. #include "system_fm33lc0xx.h"
  37. /*----------------------------------------------------------------------------
  38. DEFINES
  39. *----------------------------------------------------------------------------*/
  40. /*----------------------------------------------------------------------------
  41. Define clocks
  42. *----------------------------------------------------------------------------*/
  43. /* ToDo: add here your necessary defines for device initialization
  44. following is an example for different system frequencies */
  45. /*----------------------------------------------------------------------------
  46. Clock Variable definitions
  47. *----------------------------------------------------------------------------*/
  48. /* ToDo: initialize SystemCoreClock with the system core clock frequency value
  49. achieved after system intitialization.
  50. This means system core clock frequency after call to SystemInit() */
  51. uint32_t SystemCoreClock = __SYSTEM_CLOCK; /*!< System Clock Frequency (Core Clock)*/
  52. /*----------------------------------------------------------------------------
  53. Clock functions
  54. *----------------------------------------------------------------------------*/
  55. static uint32_t SystemPLLClockUpdate(void)
  56. {
  57. uint32_t clock = 0;
  58. // 时钟源
  59. switch ((RCC->PLLCR >> 1) & 0x1)
  60. {
  61. case 0:
  62. switch ((RCC->RCHFCR >> 16) & 0xf)
  63. {
  64. case 1: // 16M
  65. clock = 16000000;
  66. break;
  67. case 2: // 24M
  68. clock = 24000000;
  69. break;
  70. case 0: // 8M
  71. default:
  72. clock = 8000000;
  73. break;
  74. }
  75. break;
  76. case 1:
  77. clock = XTHF_VALUE;
  78. break;
  79. }
  80. // 分频
  81. switch ((RCC->PLLCR >> 0x4) & 0x7)
  82. {
  83. case 0: // 不分频
  84. clock /= 1;
  85. break;
  86. case 1: // 2分频
  87. clock /= 2;
  88. break;
  89. case 2: // 4分频
  90. clock /= 4;
  91. break;
  92. case 3: // 8分频
  93. clock /= 8;
  94. break;
  95. case 4: // 12分频
  96. clock /= 12;
  97. break;
  98. case 5: // 16分频
  99. clock /= 16;
  100. break;
  101. case 6: // 24分频
  102. clock /= 24;
  103. break;
  104. case 7: // 32分频
  105. clock /= 32;
  106. break;
  107. }
  108. // 倍频比
  109. clock = clock * (((RCC->PLLCR >> 16) & 0x7f) + 1);
  110. // 输出选择
  111. if ((RCC->PLLCR >> 3) & 0x1)
  112. {
  113. clock *= 2;
  114. }
  115. return clock;
  116. }
  117. void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
  118. {
  119. switch ((RCC->SYSCLKCR >> 0) & 0x7)
  120. {
  121. case 1: // XTHF
  122. SystemCoreClock = XTHF_VALUE;
  123. break;
  124. case 2: // PLL
  125. SystemCoreClock = SystemPLLClockUpdate();
  126. break;
  127. case 4: // RCMF
  128. switch ((RCC->RCMFCR >> 16) & 0x3)
  129. {
  130. case 0: // 不分频
  131. SystemCoreClock = 4000000;
  132. break;
  133. case 1: // 4分频
  134. SystemCoreClock = 1000000;
  135. break;
  136. case 2: // 8分频
  137. SystemCoreClock = 500000;
  138. break;
  139. case 3: // 16分频
  140. SystemCoreClock = 250000;
  141. break;
  142. }
  143. break;
  144. case 5: // LSCLK
  145. case 6: // LPOSC
  146. SystemCoreClock = 32768;
  147. break;
  148. case 7: // USBBCK
  149. switch ((RCC->SYSCLKCR >> 3) & 0x1)
  150. {
  151. case 0: // USBBCK 48M
  152. SystemCoreClock = 48000000;
  153. break;
  154. case 1: // USBBCK 120M 2分频
  155. SystemCoreClock = 60000000;
  156. break;
  157. }
  158. break;
  159. default:
  160. switch ((RCC->RCHFCR >> 16) & 0xf)
  161. {
  162. case 1: // 16M
  163. SystemCoreClock = 16000000;
  164. break;
  165. case 2: // 24M
  166. SystemCoreClock = 24000000;
  167. break;
  168. case 0: // 8M
  169. default:
  170. SystemCoreClock = 8000000;
  171. break;
  172. }
  173. break;
  174. }
  175. }
  176. /**
  177. * @brief NVIC_Init config NVIC
  178. *
  179. * @param NVIC_configStruct configParams
  180. *
  181. * @param IRQn Interrupt number
  182. *
  183. * @retval None
  184. */
  185. void NVIC_Init(NVIC_ConfigTypeDef *NVIC_configStruct,IRQn_Type IRQn)
  186. {
  187. /* Params Check */
  188. if(NVIC_configStruct->preemptPriority>3)
  189. {
  190. NVIC_configStruct->preemptPriority = 3;
  191. }
  192. NVIC_DisableIRQ(IRQn);
  193. NVIC_SetPriority(IRQn,NVIC_configStruct->preemptPriority);
  194. NVIC_EnableIRQ(IRQn);
  195. }
  196. /**
  197. * Initialize the system
  198. *
  199. * @param none
  200. * @return none
  201. *
  202. * @brief Setup the microcontroller system.
  203. * Initialize the System.
  204. */
  205. void SystemInit (void)
  206. {
  207. uint32_t temp;
  208. /* */
  209. RCC->PLLCR = (uint32_t)0x00000000U;
  210. RCC->SYSCLKCR = (uint32_t)0x0A000000U;
  211. /* PAD RCC*/
  212. RCC->PCLKCR1 |= (0x1U << 7U);
  213. #ifdef USE_LSCLK_CLOCK_SRC_XTLF
  214. GPIOD->FCR |= 0x3C0000;
  215. /* XTLF*/
  216. RCC->XTLFCR = (uint32_t)(0x00000000U);
  217. /* XTLF*/
  218. RCC->XTLFCR |= (uint32_t)(0x00000005U<<8);
  219. for(temp = 2000;temp>0;temp--);
  220. /* LSCLKXTLF*/
  221. RCC->LSCLKSEL = 0xAA;
  222. /* LSCXTLF*/
  223. RCC->SYSCLKCR |= 0x8000000U;
  224. #else
  225. RCC->SYSCLKCR &= 0x7FFFFFFU;
  226. RCC->LSCLKSEL = 0x55;
  227. #endif
  228. /*PDR*/
  229. RMU->PDRCR |=0x01;
  230. /*BOR*/
  231. RMU->BORCR &=0xFE;
  232. /* DEBUG IWDT WWDT */
  233. DBG->CR =0x03;
  234. RCC->RCHFTR = RCHF24M_TRIM;
  235. RCC->RCMFTR = RCMF4M_TRIM;
  236. RCC->LPOSCTR = LPOSC_TRIM;
  237. GPIOD->PUEN |= 0x3 << 7;
  238. /* DMA Flash Channel: Flash->RAM */
  239. RCC->PCLKCR2 |= 0x1 << 4;
  240. DMA->CH7CR |= 0x1 << 10;
  241. RCC->PCLKCR2 &= ~(0x1 << 4);
  242. }