stm32f4_clk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (C) 2012 by Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. #include <cfg/arch.h>
  35. #include <arch/cm3.h>
  36. #include <arch/cm3/timer.h>
  37. #include <arch/cm3/stm/stm32_clk.h>
  38. #include <cfg/clock.h>
  39. #include <arch/cm3/stm/stm32xxxx.h>
  40. #if !defined(HSI_VALUE)
  41. #define HSI_VALUE 160000000
  42. #endif /* HSI_VALUE */
  43. #if !defined(HSE_STARTUP_TIMEOUT)
  44. #define HSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSE start up, in ms */
  45. #endif
  46. static uint32_t SystemCoreClock = 0;
  47. /* Prepare some defaults if configuration is incomplete */
  48. #if !defined(SYSCLK_SOURCE)
  49. #define SYSCLK_SOURCE SYSCLK_HSI
  50. #endif
  51. static const uint8_t AHBPrescTable[16] = {
  52. 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
  53. static const uint8_t APBPrescTable[8] = {1, 1, 1, 1, 2, 4, 8, 16};
  54. /*---------------- Clock Setup Procedure ------------------------------
  55. *
  56. * Clock system ist arranged like this:
  57. *
  58. * /Q------------------------------ USB
  59. * | ,--------------- CPU
  60. * | +--------------- SDIO
  61. * 4-26 MHz HSE -+--/M*N/P--+-AHBPRES---+-- APB1PRESC--- APB1
  62. * | | +-- ABP2PRESC--- ABP2
  63. * 16MHz HSI ------+----------' '-- ADCPRESC---- ADC
  64. *
  65. *
  66. * ***** Setup of system clock configuration *****
  67. *
  68. * 1) Select system clock sources
  69. *
  70. * To setup system to use HSI call: SetSysClockSource( SYSCLK_HSI);
  71. * To setup system to use HSE call: SetSysClockSource( SYSCLK_HSE);
  72. *
  73. * To setup system to use the PLL output, first setup the PLL source:
  74. * SetPllClockSource( PLLCLK_HSI);
  75. * or
  76. * SetPllClockSource( PLLCLK_HSE);
  77. * Then call SetSysClockSource( SYSCLK_PLL);
  78. *
  79. * 2) Configure prescalers
  80. * After selecting the right clock sources, the prescalers need to
  81. * be configured:
  82. * Call SetSysClock(); to do this automatically.
  83. *
  84. */
  85. /*!
  86. * \brief Update SystemCoreClock according to Clock Register Values
  87. *
  88. * This function reads out the CPUs clock and PLL registers and assembles
  89. * the actual clock speed values into the SystemCoreClock local variable.
  90. */
  91. static void SystemCoreClockUpdate(void)
  92. {
  93. RCC_TypeDef *rcc = (RCC_TypeDef *)RCC_BASE;
  94. uint32_t tmp = 0;
  95. uint32_t cfgr;
  96. uint32_t hpre;
  97. /* Get SYSCLK source ---------------------------------------------------*/
  98. cfgr = RCC->CFGR & RCC_CFGR_SWS;
  99. switch (cfgr) {
  100. case RCC_CFGR_SWS_HSE:
  101. tmp = HSE_VALUE;
  102. break;
  103. case RCC_CFGR_SWS_PLL: {
  104. uint32_t pllcfgr = rcc->PLLCFGR;
  105. uint32_t n = (pllcfgr & RCC_PLLCFGR_PLLN) >> _BI32(RCC_PLLCFGR_PLLN_0);
  106. uint32_t m = (pllcfgr & RCC_PLLCFGR_PLLM) >> _BI32(RCC_PLLCFGR_PLLM_0);
  107. uint32_t p = (pllcfgr & RCC_PLLCFGR_PLLP) >> _BI32(RCC_PLLCFGR_PLLP_0);
  108. /* Pll Divisor is (p + 1) * 2. Move * 2 into the base clock sonstant*/
  109. if ((pllcfgr & RCC_PLLCFGR_PLLSRC_HSE) == RCC_PLLCFGR_PLLSRC_HSE)
  110. tmp = HSE_VALUE / 2;
  111. else
  112. tmp = HSI_VALUE / 2;
  113. tmp = ((tmp / m) * n)/(p + 1);
  114. break;
  115. }
  116. default:
  117. tmp = HSI_VALUE;
  118. }
  119. hpre = (cfgr & RCC_CFGR_HPRE) >> _BI32(RCC_CFGR_HPRE_0);
  120. SystemCoreClock = tmp >> AHBPrescTable[hpre];
  121. }
  122. /* Functional same as F1 */
  123. /*!
  124. * \brief Control HSE clock.
  125. *
  126. * \param ena 0 disable clock, any other value enable it.
  127. * \return 0 on success, -1 on HSE start failed.
  128. */
  129. int CtlHseClock( uint8_t ena)
  130. {
  131. int rc = 0;
  132. uint32_t tout = HSE_STARTUP_TIMEOUT;
  133. volatile uint32_t HSEStatus = 0;
  134. if( ena) {
  135. /* Enable HSE */
  136. RCC->CR |= RCC_CR_HSEON;
  137. /* Wait till HSE is ready or time out is reached */
  138. do {
  139. tout--;
  140. HSEStatus = RCC->CR & RCC_CR_HSERDY;
  141. } while((HSEStatus == 0) && (tout > 0));
  142. if ((RCC->CR & RCC_CR_HSERDY) == 0) {
  143. /* HSE failed to start */
  144. rc = -1;
  145. }
  146. }
  147. else {
  148. /* Disable HSE clock */
  149. RCC->CR &= ~RCC_CR_HSEON;
  150. }
  151. return rc;
  152. }
  153. /* Functional same as F1 */
  154. /*!
  155. * \brief Control HSI clock.
  156. *
  157. * \param ena 0 disable clock, any other value enable it.
  158. * \return 0 on success, -1 on HSI start failed.
  159. */
  160. int CtlHsiClock( uint8_t ena)
  161. {
  162. int rc = 0;
  163. uint32_t tout = HSE_STARTUP_TIMEOUT;
  164. volatile uint32_t HSIStatus = 0;
  165. if( ena) {
  166. /* Enable HSI */
  167. RCC->CR |= RCC_CR_HSION;
  168. /* Wait till HSI is ready or time out is reached */
  169. do {
  170. tout--;
  171. HSIStatus = RCC->CR & RCC_CR_HSIRDY;
  172. } while((HSIStatus == 0) && (tout > 0));
  173. if ((RCC->CR & RCC_CR_HSIRDY) == 0) {
  174. /* HSI failed to start */
  175. rc = -1;
  176. }
  177. }
  178. else {
  179. /* Disable HSE clock */
  180. RCC->CR &= ~RCC_CR_HSION;
  181. }
  182. return rc;
  183. }
  184. /* Functional same as F1 */
  185. /*!
  186. * \brief Control PLL clock.
  187. *
  188. * \param ena 0 disable clock, any other value enable it.
  189. * \return 0 on success, -1 on PLL start failed.
  190. */
  191. int CtlPllClock( uint8_t ena)
  192. {
  193. int rc = 0;
  194. uint32_t tout = HSE_STARTUP_TIMEOUT;
  195. volatile uint32_t PLLStatus = 0;
  196. if( ena) {
  197. /* Enable PLL */
  198. RCC->CR |= RCC_CR_PLLON;
  199. /* Wait till PLL is ready or time out is reached */
  200. do {
  201. tout--;
  202. PLLStatus = RCC->CR & RCC_CR_PLLRDY;
  203. } while((PLLStatus == 0) && (tout > 0));
  204. if ((RCC->CR & RCC_CR_PLLRDY) == 0) {
  205. /* PLL failed to start */
  206. rc = -1;
  207. }
  208. }
  209. else {
  210. /* Disable HSE clock */
  211. RCC->CR &= ~RCC_CR_PLLON;
  212. }
  213. return rc;
  214. }
  215. /*!
  216. * \brief Configures the System clock source: HSE or HSI.
  217. * \note This function should be used with PLL disables
  218. * \param src is one of PLLCLK_HSE, PLLCLK_HSI.
  219. * \return 0 if clock is running ale -1.
  220. */
  221. int SetPllClockSource( int src)
  222. {
  223. int rc = -1;
  224. if (src == PLLCLK_HSE) {
  225. rc = CtlHseClock(1);
  226. if (rc==0) {
  227. CM3BBSET(RCC_BASE, RCC_TypeDef, PLLCFGR, _BI32(RCC_PLLCFGR_PLLSRC));
  228. }
  229. }
  230. else if (src == PLLCLK_HSI) {
  231. rc = CtlHsiClock(1);
  232. /* Select HSI/2 as PLL clock source */
  233. if (rc==0) {
  234. CM3BBCLR(RCC_BASE, RCC_TypeDef, PLLCFGR, _BI32(RCC_PLLCFGR_PLLSRC));
  235. }
  236. }
  237. return rc;
  238. }
  239. /*!
  240. * \brief Configures the System clock source: HSI, HS or PLL.
  241. * \note This function should be used only after reset.
  242. * \param src is one of SYSCLK_HSE, SYSCLK_HSI or SYSCLK_PLL.
  243. * \return 0 if selected clock is running else -1.
  244. */
  245. int SetSysClockSource( int src)
  246. {
  247. int rc = -1;
  248. if (src == SYSCLK_HSE) {
  249. rc = CtlHseClock(1);
  250. if (rc == 0) {
  251. /* Select HSE as system clock source */
  252. RCC->CFGR &= ~RCC_CFGR_SW;
  253. RCC->CFGR |= RCC_CFGR_SW_HSE;
  254. /* Wait till HSE is used as system clock source */
  255. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSE);
  256. }
  257. }
  258. else if (src == SYSCLK_HSI) {
  259. rc = CtlHsiClock(1);
  260. if (rc == 0) {
  261. /* Select HSI as system clock source */
  262. RCC->CFGR &= ~RCC_CFGR_SW;
  263. RCC->CFGR |= RCC_CFGR_SW_HSI;
  264. /* Wait till HSI is used as system clock source */
  265. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI);
  266. }
  267. }
  268. else if (src == SYSCLK_PLL) {
  269. rc = CtlPllClock(1);
  270. if (rc == 0) {
  271. /* Select HSI as system clock source */
  272. RCC->CFGR &= ~RCC_CFGR_SW;
  273. RCC->CFGR |= RCC_CFGR_SW_PLL;
  274. /* Wait till PLL is used as system clock source */
  275. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);
  276. }
  277. }
  278. /* Update core clock information */
  279. SystemCoreClockUpdate();
  280. return rc;
  281. }
  282. #if (SYSCLK_SOURCE == SYSCLK_HSI) || (SYSCLK_SOURCE == SYSCLK_HSE)
  283. /*!
  284. * \brief Configures the System clock coming from HSE or HSI oscillator.
  285. *
  286. * Enable HSI/HSE clock and setup HCLK, PCLK2 and PCLK1 prescalers.
  287. *
  288. * \param None.
  289. * \return 0 on success, -1 on fault of HSE.
  290. */
  291. int SetSysClock(void)
  292. {
  293. int rc = 0;
  294. register uint32_t cfgr;
  295. /* Todo: Check Voltage range! Here 2.7-3.6 Volt is assumed */
  296. /* For 2.7-3.6 Volt up to 30 MHz no Wait state required */
  297. cfgr = RCC->CFGR;
  298. cfgr &= ~(RCC_CFGR_HPRE|RCC_CFGR_PPRE1|RCC_CFGR_PPRE2);
  299. /* HCLK = SYSCLK */
  300. cfgr |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  301. /* PCLK2 = HCLK */
  302. cfgr |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  303. /* PCLK1 = HCLK */
  304. cfgr |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  305. RCC->CFGR = cfgr;
  306. rc = SetSysClockSource(SYSCLK_SOURCE);
  307. return rc;
  308. }
  309. #else /* (SYSCLK_SOURCE == SYSCLK_HSI) || (SYSCLK_SOURCE == SYSCLK_HSE) */
  310. #if (PLLCLK_SOURCE==PLLCLK_HSE)
  311. #define PLLCLK_IN HSE_VALUE
  312. #else
  313. #define PLLCLK_IN HSI_VALUE
  314. #endif
  315. /**
  316. * @brief Sets System clock frequency to 120/168MHz and configure HCLK, PCLK2
  317. * and PCLK1 prescalers.
  318. * @note This function should be used only after reset.
  319. * @param None
  320. * @retval None
  321. */
  322. /*
  323. Ranges :
  324. M: 2..63
  325. N: 64.. 432
  326. P: 2, 4, 6, 8
  327. Q: 2..15
  328. 0.95 MHz < PLLCLK_IN/M < 2 MHz, Prefer 2 MHz for low jitter
  329. 192 MHz < PLLCLK_IN/M*N < 432 MHz
  330. PLLCLK_IN/M*N/P < 168 MHz
  331. PLLCLK_IN/M*N/Q < 48 MHz, Use 48 MHz for USB
  332. Easy Approach:
  333. Try to reach fvco = 336 Mhz with M/N.
  334. Require a clock >= 4 MHz in 2 MHz Steps
  335. */
  336. int SetSysClock(void)
  337. {
  338. int rc = 0;
  339. uint32_t rcc_reg;
  340. #if !defined(STM32_VRANGE) || STM32_VRANGE == 0
  341. #define FLASH_BASE_FREQ 30000000
  342. #elif STM32_VRANGE == 1
  343. #define FLASH_BASE_FREQ 24000000
  344. #elif STM32_VRANGE == 2
  345. #define FLASH_BASE_FREQ 22000000
  346. #elif STM32_VRANGE == 3
  347. #define FLASH_BASE_FREQ 20000000
  348. #endif
  349. #if PLLCLK_IN > 26000000
  350. #warning PLL Input frequency too high
  351. #endif
  352. #if (PLLCLK_IN > 3999999) && ((PLLCLK_IN % 2000000L) == 0)
  353. #define PLLM (PLLCLK_IN/2000000)
  354. #define PLLN ((SYSCLK_FREQ/1000000) << _BI32(RCC_PLLCFGR_PLLN_0))
  355. #elif (PLLCLK_IN > 1999999) && ((PLLCLK_IN % 1000000L) == 0)
  356. #define PLLM (PLLCLK_IN/1000000)
  357. #define PLLN ((SYSCLK_FREQ/500000 ) << _BI32(RCC_PLLCFGR_PLLN_0))
  358. #else
  359. #warning "PLL Source frequency isn't a multiple of 1 or 2 MHz"
  360. #endif
  361. #define PLLP ((2/2-1) << _BI32(RCC_PLLCFGR_PLLP_0))
  362. #define PLLQ (5 << _BI32(RCC_PLLCFGR_PLLQ_0))
  363. #define NUT_FLASH_LATENCY (SYSCLK_FREQ/FLASH_BASE_FREQ)
  364. /* Select System frequency up to 168 MHz */
  365. RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  366. rcc_reg = RCC->PLLCFGR;
  367. rcc_reg &= ~(RCC_PLLCFGR_PLLM | RCC_PLLCFGR_PLLN | RCC_PLLCFGR_PLLP | RCC_PLLCFGR_PLLQ);
  368. #if (PLLCLK_SOURCE==PLLCLK_HSE)
  369. if (CtlHseClock(1) != 0)
  370. return -1;
  371. rcc_reg = PLLM | PLLN | PLLP | PLLQ | RCC_PLLCFGR_PLLSRC_HSE;
  372. #else
  373. if (CtlHsiClock(1) != 0)
  374. return -1;
  375. rcc_reg = PLLM| PLLN | PLLP | PLLQ | RCC_PLLCFGR_PLLSRC_HSI;
  376. #endif
  377. RCC->PLLCFGR = rcc_reg;
  378. rcc_reg = FLASH->ACR;
  379. rcc_reg &= ~FLASH_ACR_LATENCY;
  380. #if STM32_VRANGE == 3
  381. /* Prefetch must be off*/
  382. rcc_reg |= NUT_FLASH_LATENCY;
  383. #else
  384. rcc_reg |= NUT_FLASH_LATENCY | FLASH_ACR_PRFTEN ;
  385. #endif
  386. /* Enable Instruction and Data cache */
  387. rcc_reg |= FLASH_ACR_ICEN | FLASH_ACR_DCEN;
  388. FLASH->ACR = rcc_reg;
  389. rcc_reg = RCC->CFGR;
  390. rcc_reg &= ~(RCC_CFGR_HPRE | RCC_CFGR_PPRE2| RCC_CFGR_PPRE1);
  391. /* HCLK = SYSCLK, PCLK2 = HCLK/2 , PCLK1 = HCLK/4 */
  392. rcc_reg |= (RCC_CFGR_HPRE_DIV1 | RCC_CFGR_PPRE2_DIV2| RCC_CFGR_PPRE1_DIV4);
  393. RCC->CFGR = rcc_reg;
  394. /* Start PLL, wait ready and switch to it as clock source */
  395. rc = SetSysClockSource(SYSCLK_SOURCE);
  396. if (rc) {
  397. /* Something went wrong with the PLL startup! */
  398. SetSysClockSource(SYSCLK_HSI);
  399. return rc;
  400. }
  401. return rc;
  402. }
  403. #endif /* (SYSCLK_SOURCE == SYSCLK_HSI) || (SYSCLK_SOURCE == SYSCLK_HSE) */
  404. /**
  405. * @brief requests System clock frequency
  406. *
  407. * @note This function should be used only after reset.
  408. * @param None
  409. * @retval None
  410. */
  411. uint32_t SysCtlClockGet(void)
  412. {
  413. SystemCoreClockUpdate();
  414. return SystemCoreClock;
  415. }
  416. /**
  417. * @brief requests frequency of the given clock
  418. *
  419. * @param idx NUT_HWCLK Index
  420. * @retval clock or 0 if idx points to an invalid clock
  421. */
  422. uint32_t STM_ClockGet(int idx)
  423. {
  424. SystemCoreClockUpdate();
  425. switch(idx) {
  426. case NUT_HWCLK_CPU:
  427. return SystemCoreClock;
  428. break;
  429. case NUT_HWCLK_PCLK1: {
  430. uint32_t tmp = (RCC->CFGR & RCC_CFGR_PPRE1) >> _BI32( RCC_CFGR_PPRE1_0);
  431. return SystemCoreClock/APBPrescTable[tmp];
  432. break;
  433. }
  434. case NUT_HWCLK_PCLK2: {
  435. uint32_t tmp = (RCC->CFGR & RCC_CFGR_PPRE2) >> _BI32( RCC_CFGR_PPRE2_0);
  436. return SystemCoreClock/APBPrescTable[tmp];
  437. break;
  438. }
  439. default:
  440. return 0;
  441. break;
  442. }
  443. }