system_n32g4fr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file system_n32g4fr.c
  29. * @author Nations
  30. * @version v1.0.2
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g4fr.h"
  35. /* Uncomment the line corresponding to the desired System clock (SYSCLK)
  36. frequency (after reset the HSI is used as SYSCLK source)
  37. IMPORTANT NOTE:
  38. ==============
  39. 1. After each device reset the HSI is used as System clock source.
  40. 2. Please make sure that the selected System clock doesn't exceed your
  41. device's maximum frequency.
  42. 3. If none of the define below is enabled, the HSI is used as System clock
  43. source.
  44. 4. The System clock configuration functions provided within this file assume
  45. that:
  46. - For Low, Medium and High density Value line devices an external 8MHz
  47. crystal is used to drive the System clock.
  48. - For Low, Medium and High density devices an external 8MHz crystal is
  49. used to drive the System clock.
  50. - For Connectivity line devices an external 25MHz crystal is used to
  51. drive the System clock. If you are using different crystal you have to adapt
  52. those functions accordingly.
  53. */
  54. #define SYSCLK_USE_HSI 0
  55. #define SYSCLK_USE_HSE 1
  56. #define SYSCLK_USE_HSI_PLL 2
  57. #define SYSCLK_USE_HSE_PLL 3
  58. #ifndef SYSCLK_FREQ
  59. #define SYSCLK_FREQ 144000000
  60. #endif
  61. #ifndef SYSCLK_SRC
  62. #define SYSCLK_SRC SYSCLK_USE_HSE_PLL
  63. #endif
  64. #if SYSCLK_SRC == SYSCLK_USE_HSI
  65. #if SYSCLK_FREQ != HSI_VALUE
  66. #error SYSCL_FREQ must be set to HSI_VALUE
  67. #endif
  68. #elif SYSCLK_SRC == SYSCLK_USE_HSE
  69. #ifndef HSE_VALUE
  70. #error HSE_VALUE must be defined!
  71. #endif
  72. #if SYSCLK_FREQ != HSE_VALUE
  73. #error SYSCL_FREQ must be set to HSE_VALUE
  74. #endif
  75. #elif SYSCLK_SRC == SYSCLK_USE_HSI_PLL
  76. #if (SYSCLK_FREQ % (HSI_VALUE / 2) == 0) && (SYSCLK_FREQ / (HSI_VALUE / 2) >= 2) \
  77. && (SYSCLK_FREQ / (HSI_VALUE / 2) <= 32)
  78. #define PLLSRC_DIV 2
  79. #define PLL_MUL (SYSCLK_FREQ / (HSI_VALUE / 2))
  80. #else
  81. #error Cannot make a PLL multiply factor to SYSCLK_FREQ.
  82. #endif
  83. #elif SYSCLK_SRC == SYSCLK_USE_HSE_PLL
  84. #ifndef HSE_VALUE
  85. #error HSE_VALUE must be defined!
  86. #endif
  87. #if ((SYSCLK_FREQ % (HSE_VALUE / 2)) == 0) && (SYSCLK_FREQ / (HSE_VALUE / 2) >= 2) \
  88. && (SYSCLK_FREQ / (HSE_VALUE / 2) <= 32)
  89. #define PLLSRC_DIV 2
  90. #define PLL_MUL (SYSCLK_FREQ / (HSE_VALUE / 2))
  91. #elif (SYSCLK_FREQ % HSE_VALUE == 0) && (SYSCLK_FREQ / HSE_VALUE >= 2) && (SYSCLK_FREQ / HSE_VALUE <= 32)
  92. #define PLLSRC_DIV 1
  93. #define PLL_MUL (SYSCLK_FREQ / HSE_VALUE)
  94. #else
  95. #error Cannot make a PLL multiply factor to SYSCLK_FREQ.
  96. #endif
  97. #else
  98. #error wrong value for SYSCLK_SRC
  99. #endif
  100. /* #define VECT_TAB_SRAM */
  101. #define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field. This value must be a multiple of 0x200. */
  102. /*******************************************************************************
  103. * Clock Definitions
  104. *******************************************************************************/
  105. uint32_t SystemCoreClock = SYSCLK_FREQ; /*!< System Clock Frequency (Core Clock) */
  106. const uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  107. static void SetSysClock(void);
  108. #ifdef DATA_IN_ExtSRAM
  109. static void SystemInit_ExtMemCtl(void);
  110. #endif /* DATA_IN_ExtSRAM */
  111. /**
  112. * @brief Setup the microcontroller system
  113. * Initialize the Embedded Flash Interface, the PLL and update the
  114. * SystemCoreClock variable.
  115. * @note This function should be used only after reset.
  116. */
  117. void SystemInit(void)
  118. {
  119. /* FPU settings
  120. * ------------------------------------------------------------*/
  121. #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  122. SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2)); /* set CP10 and CP11 Full Access */
  123. #endif
  124. /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  125. /* Set HSIEN bit */
  126. RCC->CTRL |= (uint32_t)0x00000001;
  127. /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
  128. RCC->CFG &= (uint32_t)0xF8FFC000;
  129. /* Reset HSEON, CLKSSEN and PLLEN bits */
  130. RCC->CTRL &= (uint32_t)0xFEF6FFFF;
  131. /* Reset HSEBYP bit */
  132. RCC->CTRL &= (uint32_t)0xFFFBFFFF;
  133. /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRES/OTGFSPRE bits */
  134. RCC->CFG &= (uint32_t)0xF700FFFF;
  135. /* Reset CFG2 register */
  136. RCC->CFG2 = 0x00003800;
  137. /* Reset CFG3 register */
  138. RCC->CFG3 = 0x00003840;
  139. /* Disable all interrupts and clear pending bits */
  140. RCC->CLKINT = 0x009F0000;
  141. /* Enable ex mode */
  142. RCC->APB1PCLKEN |= RCC_APB1PCLKEN_PWREN;
  143. PWR->CTRL3 |= 0x00000001;
  144. RCC->APB1PCLKEN &= (uint32_t)(~RCC_APB1PCLKEN_PWREN);
  145. /* Enable ICACHE and Prefetch Buffer */
  146. FLASH->AC |= (uint32_t)(FLASH_AC_ICAHEN | FLASH_AC_PRFTBFEN);
  147. #ifdef DATA_IN_ExtSRAM
  148. SystemInit_ExtMemCtl();
  149. #endif /* DATA_IN_ExtSRAM */
  150. /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  151. /* Configure the Flash Latency cycles and enable prefetch buffer */
  152. SetSysClock();
  153. #ifdef VECT_TAB_SRAM
  154. SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
  155. #else
  156. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
  157. #endif
  158. }
  159. /**
  160. * @brief Update SystemCoreClock variable according to Clock Register Values.
  161. * The SystemCoreClock variable contains the core clock (HCLK), it can
  162. * be used by the user application to setup the SysTick timer or
  163. * configure other parameters.
  164. *
  165. * @note Each time the core clock (HCLK) changes, this function must be called
  166. * to update SystemCoreClock variable value. Otherwise, any
  167. * configuration based on this variable will be incorrect.
  168. *
  169. * @note - The system frequency computed by this function is not the real
  170. * frequency in the chip. It is calculated based on the predefined
  171. * constant and the selected clock source:
  172. *
  173. * - If SYSCLK source is HSI, SystemCoreClock will contain the
  174. * HSI_VALUE(*)
  175. *
  176. * - If SYSCLK source is HSE, SystemCoreClock will contain the
  177. * HSE_VALUE(**)
  178. *
  179. * - If SYSCLK source is PLL, SystemCoreClock will contain the
  180. * HSE_VALUE(**) or HSI_VALUE(*) multiplied by the PLL factors.
  181. *
  182. * (*) HSI_VALUE is a constant defined in n32g4fr.h file (default value
  183. * 8 MHz) but the real value may vary depending on the variations
  184. * in voltage and temperature.
  185. *
  186. * (**) HSE_VALUE is a constant defined in N32G4FR.h file (default value
  187. * 8 MHz or 25 MHz, depedning on the product used), user has to
  188. * ensure that HSE_VALUE is same as the real frequency of the crystal used.
  189. * Otherwise, this function may have wrong result.
  190. *
  191. * - The result of this function could be not correct when using
  192. * fractional value for HSE crystal.
  193. */
  194. void SystemCoreClockUpdate(void)
  195. {
  196. uint32_t tmp = 0, pllmull = 0, pllsource = 0;
  197. /* Get SYSCLK source
  198. * -------------------------------------------------------*/
  199. tmp = RCC->CFG & RCC_CFG_SCLKSTS;
  200. switch (tmp)
  201. {
  202. case 0x00: /* HSI used as system clock */
  203. SystemCoreClock = HSI_VALUE;
  204. break;
  205. case 0x04: /* HSE used as system clock */
  206. SystemCoreClock = HSE_VALUE;
  207. break;
  208. case 0x08: /* PLL used as system clock */
  209. /* Get PLL clock source and multiplication factor
  210. * ----------------------*/
  211. pllmull = RCC->CFG & RCC_CFG_PLLMULFCT;
  212. pllsource = RCC->CFG & RCC_CFG_PLLSRC;
  213. if ((pllmull & RCC_CFG_PLLMULFCT_4) == 0)
  214. {
  215. pllmull = (pllmull >> 18) + 2; // PLLMUL[4]=0
  216. }
  217. else
  218. {
  219. pllmull = ((pllmull >> 18) - 496) + 1; // PLLMUL[4]=1
  220. }
  221. if (pllsource == 0x00)
  222. {
  223. /* HSI oscillator clock divided by 2 selected as PLL clock entry */
  224. SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
  225. }
  226. else
  227. {
  228. /* HSE selected as PLL clock entry */
  229. if ((RCC->CFG & RCC_CFG_PLLHSEPRES) != (uint32_t)RESET)
  230. { /* HSE oscillator clock divided by 2 */
  231. SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
  232. }
  233. else
  234. {
  235. SystemCoreClock = HSE_VALUE * pllmull;
  236. }
  237. }
  238. break;
  239. default:
  240. SystemCoreClock = HSI_VALUE;
  241. break;
  242. }
  243. /* Compute HCLK clock frequency ----------------*/
  244. /* Get HCLK prescaler */
  245. tmp = AHBPrescTable[((RCC->CFG & RCC_CFG_AHBPRES) >> 4)];
  246. /* HCLK clock frequency */
  247. SystemCoreClock >>= tmp;
  248. }
  249. /**
  250. * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1
  251. * prescalers.
  252. */
  253. static void SetSysClock(void)
  254. {
  255. uint32_t rcc_cfgr = 0;
  256. bool HSEStatus = 0;
  257. uint32_t StartUpCounter = 0;
  258. #if SYSCLK_SRC == SYSCLK_USE_HSE || SYSCLK_SRC == SYSCLK_USE_HSE_PLL
  259. /* Enable HSE */
  260. RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
  261. /* Wait till HSE is ready and if Time out is reached exit */
  262. do
  263. {
  264. HSEStatus = RCC->CTRL & RCC_CTRL_HSERDF;
  265. StartUpCounter++;
  266. } while ((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
  267. HSEStatus = ((RCC->CTRL & RCC_CTRL_HSERDF) != RESET);
  268. if (!HSEStatus)
  269. {
  270. /* If HSE fails to start-up, the application will have wrong clock
  271. * configuration. User can add here some code to deal with this error */
  272. SystemCoreClock = HSI_VALUE;
  273. return;
  274. }
  275. #endif
  276. /* Flash wait state
  277. 0: HCLK <= 32M
  278. 1: HCLK <= 64M
  279. 2: HCLK <= 96M
  280. 3: HCLK <= 128M
  281. 4: HCLK <= 144M
  282. */
  283. FLASH->AC &= (uint32_t)((uint32_t)~FLASH_AC_LATENCY);
  284. FLASH->AC |= (uint32_t)((SYSCLK_FREQ - 1) / 32000000);
  285. /* HCLK = SYSCLK */
  286. RCC->CFG |= (uint32_t)RCC_CFG_AHBPRES_DIV1;
  287. /* PCLK2 max 72M */
  288. if (SYSCLK_FREQ > 72000000)
  289. {
  290. RCC->CFG |= (uint32_t)RCC_CFG_APB2PRES_DIV2;
  291. }
  292. else
  293. {
  294. RCC->CFG |= (uint32_t)RCC_CFG_APB2PRES_DIV1;
  295. }
  296. /* PCLK1 max 36M */
  297. if (SYSCLK_FREQ > 72000000)
  298. {
  299. RCC->CFG |= (uint32_t)RCC_CFG_APB1PRES_DIV4;
  300. }
  301. else if (SYSCLK_FREQ > 36000000)
  302. {
  303. RCC->CFG |= (uint32_t)RCC_CFG_APB1PRES_DIV2;
  304. }
  305. else
  306. {
  307. RCC->CFG |= (uint32_t)RCC_CFG_APB1PRES_DIV1;
  308. }
  309. #if SYSCLK_SRC == SYSCLK_USE_HSE
  310. /* Select HSE as system clock source */
  311. RCC->CFG &= (uint32_t)((uint32_t) ~(RCC_CFG_SCLKSW));
  312. RCC->CFG |= (uint32_t)RCC_CFG_SCLKSW_HSE;
  313. /* Wait till HSE is used as system clock source */
  314. while ((RCC->CFG & (uint32_t)RCC_CFG_SCLKSTS) != (uint32_t)0x04)
  315. {
  316. }
  317. #elif SYSCLK_SRC == SYSCLK_USE_HSI_PLL || SYSCLK_SRC == SYSCLK_USE_HSE_PLL
  318. /* clear bits */
  319. RCC->CFG &= (uint32_t)((uint32_t) ~(RCC_CFG_PLLSRC | RCC_CFG_PLLHSEPRES | RCC_CFG_PLLMULFCT));
  320. /* set PLL source */
  321. rcc_cfgr = RCC->CFG;
  322. rcc_cfgr |= (SYSCLK_SRC == SYSCLK_USE_HSI_PLL ? RCC_CFG_PLLSRC_HSI_DIV2 : RCC_CFG_PLLSRC_HSE);
  323. #if SYSCLK_SRC == SYSCLK_USE_HSE_PLL
  324. rcc_cfgr |= (PLLSRC_DIV == 1 ? RCC_CFG_PLLHSEPRES_HSE : RCC_CFG_PLLHSEPRES_HSE_DIV2);
  325. #endif
  326. /* set PLL multiply factor */
  327. #if PLL_MUL <= 16
  328. rcc_cfgr |= (PLL_MUL - 2) << 18;
  329. #else
  330. rcc_cfgr |= ((PLL_MUL - 17) << 18) | (1 << 27);
  331. #endif
  332. RCC->CFG = rcc_cfgr;
  333. /* Enable PLL */
  334. RCC->CTRL |= RCC_CTRL_PLLEN;
  335. /* Wait till PLL is ready */
  336. while ((RCC->CTRL & RCC_CTRL_PLLRDF) == 0)
  337. {
  338. }
  339. /* Select PLL as system clock source */
  340. RCC->CFG &= (uint32_t)((uint32_t) ~(RCC_CFG_SCLKSW));
  341. RCC->CFG |= (uint32_t)RCC_CFG_SCLKSW_PLL;
  342. /* Wait till PLL is used as system clock source */
  343. while ((RCC->CFG & (uint32_t)RCC_CFG_SCLKSTS) != (uint32_t)0x08)
  344. {
  345. }
  346. #endif
  347. }