stm32l1_clk.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (C) 2010 by Ulrich Prinz (uprinz2@netscape.net)
  3. * (C) 2011, 2012 Uwe Bonnes (bon@elektron.ikp.physik.tu-darmstadt.de
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*
  36. * \verbatim
  37. * $Id: stm32_gpio.c 3182 2010-10-17 21:46:04Z Astralix $
  38. * \endverbatim
  39. */
  40. #include <cfg/arch.h>
  41. #include <arch/cm3.h>
  42. #include <arch/cm3/timer.h>
  43. #include <arch/cm3/stm/stm32_clk.h>
  44. #include <cfg/clock.h>
  45. #include <arch/cm3/stm/vendor/stm32l1xx.h>
  46. /* Prepare some defaults if configuration is incomplete */
  47. #if !defined(SYSCLK_SOURCE)
  48. #define SYSCLK_SOURCE SYSCLK_HSI
  49. #endif
  50. static uint32_t SystemCoreClock = 0;
  51. const uint32_t MSIFreqTable[8] = {65536, 131072, 262144, 524288, 1048000, 2097000, 4194000, 0};
  52. static const uint8_t APBPrescTable[8] = {1, 1, 1, 1, 2, 4, 8, 16};
  53. /*---------------- Clock Setup Procedure ------------------------------
  54. *
  55. * Clock system ist arranged like this:
  56. *
  57. * ,--------------------------- USB
  58. * | ,--------------- CPU
  59. * | +--------------- SDIO
  60. * 1-32MHz HSE ----+---PLLMUL-+-AHBPRES---+-- APB1PRESC--- APB1
  61. * | | +-- ABP2PRESC--- ABP2
  62. * 16MHz HSI ------+----------'-------------- ADCPRESC---- ADC
  63. * |
  64. * MSI -----------------'
  65. *
  66. *
  67. * ***** Setup of system clock configuration *****
  68. *
  69. * 1) Select system clock sources
  70. *
  71. * To setup system to use HSI call: SetSysClockSource( SYSCLK_HSI);
  72. * To setup system to use HSE call: SetSysClockSource( SYSCLK_HSE);
  73. *
  74. * To setup system to use the PLL output, first setup the PLL source:
  75. * SetPllClockSource( PLLCLK_HSI);
  76. * or
  77. * SetPllClockSource( PLLCLK_HSE);
  78. * Then call SetSysClockSource( SYSCLK_PLL);
  79. *
  80. * 2) Configure prescalers
  81. * After selecting the right clock sources, the prescalers need to
  82. * be configured:
  83. * Call SetSysClock(); to do this automatically.
  84. *
  85. */
  86. /*!
  87. * \brief Update SystemCoreClock according to Clock Register Values
  88. *
  89. * This function reads out the CPUs clock and PLL registers and assembles
  90. * the actual clock speed values into the SystemCoreClock local variable.
  91. */
  92. void SystemCoreClockUpdate(void)
  93. {
  94. uint32_t pllmull = 0, plldiv = 0, msirange;
  95. uint32_t rcc;
  96. rcc = RCC->CFGR;
  97. /* Get SYSCLK source -------------------------------------------------------*/
  98. switch (rcc & RCC_CFGR_SWS)
  99. {
  100. case RCC_CFGR_SWS_MSI: /* MSI used as system clock , value depends on RCC_ICSCR/MSIRANGE[2:0]: */
  101. msirange = RCC->ICSCR & RCC_ICSCR_MSIRANGE ;
  102. msirange = msirange>>_BI16(RCC_ICSCR_MSIRANGE_1);
  103. SystemCoreClock = MSIFreqTable[msirange];
  104. break;
  105. case RCC_CFGR_SWS_HSI: /* HSI used as system clock */
  106. SystemCoreClock = HSI_VALUE;
  107. break;
  108. case RCC_CFGR_SWS_HSE: /* HSE used as system clock */
  109. SystemCoreClock = HSE_VALUE;
  110. break;
  111. case RCC_CFGR_SWS_PLL:
  112. /* Assume that values not allowed don't occure*/
  113. if (rcc & RCC_CFGR_PLLMUL4) pllmull = 4;
  114. else if (rcc & RCC_CFGR_PLLMUL6) pllmull = 6;
  115. else if (rcc & RCC_CFGR_PLLMUL8) pllmull = 8;
  116. else if (rcc & RCC_CFGR_PLLMUL12) pllmull = 12;
  117. else if (rcc & RCC_CFGR_PLLMUL16) pllmull = 16;
  118. else if (rcc & RCC_CFGR_PLLMUL24) pllmull = 24;
  119. else if (rcc & RCC_CFGR_PLLMUL32) pllmull = 32;
  120. else if (rcc & RCC_CFGR_PLLMUL48) pllmull = 48;
  121. else pllmull = 3;
  122. if((rcc & RCC_CFGR_PLLDIV4) == RCC_CFGR_PLLDIV4) plldiv = 4;
  123. else if (rcc & RCC_CFGR_PLLDIV3) plldiv = 3;
  124. else if (rcc & RCC_CFGR_PLLDIV2) plldiv = 2;
  125. else plldiv = 1;
  126. if (rcc & RCC_CFGR_PLLSRC_HSE)
  127. SystemCoreClock = HSE_VALUE * pllmull / plldiv;
  128. else
  129. SystemCoreClock = HSI_VALUE * pllmull / plldiv;
  130. }
  131. /* Compute HCLK clock frequency ----------------*/
  132. if ((rcc & RCC_CFGR_HPRE_3))
  133. SystemCoreClock >>= ((rcc & (RCC_CFGR_HPRE_0 | RCC_CFGR_HPRE_1 |RCC_CFGR_HPRE_2)) +1);
  134. }
  135. /*!
  136. * \brief Re/Set RCC register bit and wait for same state of connected RDY bit or timeout
  137. *
  138. * \param bbreg Bitband address of the bit to set
  139. * \param tout timeout in delay units.
  140. * \return 0 on success, -1 on HSE start failed.
  141. */
  142. int rcc_set_and_wait_rdy(__IO uint32_t *bbreg, int value, uint32_t tout)
  143. {
  144. int state = (value)?1:0;
  145. *bbreg = state;
  146. do {
  147. tout--;
  148. } while ((bbreg[1] != state) && (tout > 0));
  149. return ( bbreg[1] == state)?0:-1;
  150. }
  151. /* Functional same as F1 */
  152. /*!
  153. * \brief Control HSE clock.
  154. *
  155. * \param ena 0 disable clock, any other value enable it.
  156. * \return 0 on success, -1 on HSE start failed.
  157. */
  158. int CtlHseClock( uint8_t ena)
  159. {
  160. int rc;
  161. /* switch HSE off to allow to set HSE_BYPASS */
  162. rc = rcc_set_and_wait_rdy(
  163. CM3BBADDR(RCC_BASE, RCC_TypeDef, CR, _BI32(RCC_CR_HSEON)), 0, HSE_STARTUP_TIMEOUT);
  164. if( ena) {
  165. #if defined(HSE_BYPASS)
  166. CM3BBSET(RCC_BASE, RCC_TypeDef, CR, _BI32(RCC_CR_HSEBYP));
  167. #else
  168. CM3BBCLR(RCC_BASE, RCC_TypeDef, CR, _BI32(RCC_CR_HSEBYP));
  169. #endif
  170. #if !defined(RTCPRE) || (RTCPRE <0) || (RTCPRE >3)
  171. #define RTCPRE 3
  172. #endif
  173. RCC->CR &= ~RCC_CR_RTCPRE;
  174. RCC->CR |= RTCPRE<< _BI32(RCC_CR_RTCPRE_0);
  175. /* Enable HSE */
  176. rc = rcc_set_and_wait_rdy(
  177. CM3BBADDR(RCC_BASE, RCC_TypeDef, CR, _BI32(RCC_CR_HSEON)), 1, HSE_STARTUP_TIMEOUT);
  178. }
  179. return rc;
  180. }
  181. /* Functional same as F1 */
  182. /*!
  183. * \brief Control HSI clock.
  184. *
  185. * \param ena 0 disable clock, any other value enable it.
  186. * \return 0 on success, -1 on HSI start failed.
  187. */
  188. int CtlHsiClock( uint8_t ena)
  189. {
  190. int rc = 0;
  191. if( ena) {
  192. /* Enable HSI */
  193. rc = rcc_set_and_wait_rdy(
  194. CM3BBADDR(RCC_BASE, RCC_TypeDef, CR, _BI32(RCC_CR_HSION)), 1, HSE_STARTUP_TIMEOUT);
  195. }
  196. else {
  197. /* Disable HSE clock */
  198. rc = rcc_set_and_wait_rdy(
  199. CM3BBADDR(RCC_BASE, RCC_TypeDef, CR, _BI32(RCC_CR_HSION)), 0, HSE_STARTUP_TIMEOUT);
  200. }
  201. return rc;
  202. }
  203. /* Functional same as F1 */
  204. /*!
  205. * \brief Control PLL clock.
  206. *
  207. * \param ena 0 disable clock, any other value enable it.
  208. * \return 0 on success, -1 on PLL start failed.
  209. */
  210. int CtlPllClock( uint8_t ena)
  211. {
  212. int rc = 0;
  213. if( ena) {
  214. /* Enable PLL */
  215. rc = rcc_set_and_wait_rdy(
  216. CM3BBADDR(RCC_BASE, RCC_TypeDef, CR, _BI32(RCC_CR_PLLON)), 1, HSE_STARTUP_TIMEOUT);
  217. }
  218. else {
  219. /* Disable HSE clock */
  220. rc = rcc_set_and_wait_rdy(
  221. CM3BBADDR(RCC_BASE, RCC_TypeDef, CR, _BI32(RCC_CR_PLLON)), 0, HSE_STARTUP_TIMEOUT);
  222. }
  223. return rc;
  224. }
  225. /*!
  226. * \brief Configures the System clock source: HSE or HSI.
  227. * \note This function should be used with PLL disables
  228. * \param src is one of PLLCLK_HSE, PLLCLK_HSI.
  229. * \return 0 if clock is running ale -1.
  230. */
  231. int SetPllClockSource( int src)
  232. {
  233. int rc = -1;
  234. if (src == PLLCLK_HSE) {
  235. rc = CtlHseClock(ENABLE);
  236. if (rc==0) {
  237. CM3BBSET(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PLLSRC));
  238. }
  239. }
  240. else if (src == PLLCLK_HSI) {
  241. rc = CtlHsiClock(ENABLE);
  242. /* Select HSI/2 as PLL clock source */
  243. if (rc==0) {
  244. CM3BBCLR(RCC_BASE, RCC_TypeDef, CFGR, _BI32(RCC_CFGR_PLLSRC));
  245. }
  246. }
  247. return rc;
  248. }
  249. /*!
  250. * \brief Configures the System clock source: HSI, HS or PLL.
  251. * \note This function should be used only after reset.
  252. * \param src is one of SYSCLK_HSE, SYSCLK_HSI or SYSCLK_PLL.
  253. * \return 0 if selected clock is running else -1.
  254. */
  255. int SetSysClockSource( int src)
  256. {
  257. int rc = -1;
  258. /* Fixme: Set MSI source with MSI frequency parameter */
  259. if (src == SYSCLK_HSE) {
  260. rc = CtlHseClock(ENABLE);
  261. if (rc == 0) {
  262. /* Select HSE as system clock source */
  263. RCC->CFGR &= ~RCC_CFGR_SW;
  264. RCC->CFGR |= RCC_CFGR_SW_HSE;
  265. /* Wait till HSE is used as system clock source */
  266. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSE);
  267. }
  268. }
  269. else if (src == SYSCLK_HSI) {
  270. rc = CtlHsiClock(ENABLE);
  271. if (rc == 0) {
  272. /* Select HSI as system clock source */
  273. RCC->CFGR &= ~RCC_CFGR_SW;
  274. RCC->CFGR |= RCC_CFGR_SW_HSI;
  275. /* Wait till HSI is used as system clock source */
  276. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI);
  277. }
  278. }
  279. else if (src == SYSCLK_PLL) {
  280. rc = CtlPllClock(ENABLE);
  281. if (rc == 0) {
  282. /* Select HSI as system clock source */
  283. RCC->CFGR &= ~RCC_CFGR_SW;
  284. RCC->CFGR |= RCC_CFGR_SW_PLL;
  285. /* Wait till HSI is used as system clock source */
  286. while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);
  287. }
  288. }
  289. /* Update core clock information */
  290. SystemCoreClockUpdate();
  291. return rc;
  292. }
  293. #if (SYSCLK_SOURCE == SYSCLK_HSI) || (SYSCLK_SOURCE == SYSCLK_HSE)
  294. /*!
  295. * \brief Configures the System clock coming from HSE or HSI oscillator.
  296. *
  297. * Enable HSI/HSE clock and setup HCLK, PCLK2 and PCLK1 prescalers.
  298. *
  299. * \param None.
  300. * \return 0 on success, -1 on fault of HSE.
  301. */
  302. int SetSysClock(void)
  303. {
  304. int rc = 0;
  305. register uint32_t cfgr;
  306. /* Fixme: Allow more flexible Flash Setting
  307. * For the moment, use 32-bit access with no prefetch . Latency has no meaning
  308. * for 32-bit access
  309. */
  310. cfgr = RCC->CFGR;
  311. cfgr &= ~(RCC_CFGR_HPRE|RCC_CFGR_PPRE1|RCC_CFGR_PPRE2);
  312. /* HCLK = SYSCLK */
  313. cfgr |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  314. /* PCLK2 = HCLK */
  315. cfgr |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
  316. /* PCLK1 = HCLK */
  317. cfgr |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
  318. RCC->CFGR = cfgr;
  319. rc = SetSysClockSource(SYSCLK_SOURCE);
  320. return rc;
  321. }
  322. #else /* (SYSCLK_SOURCE == SYSCLK_HSI) || (SYSCLK_SOURCE == SYSCLK_HSE) */
  323. #if (PLLCLK_SOURCE==PLLCLK_HSE)
  324. #define PLLCLK_IN HSE_VALUE
  325. #else
  326. #define PLLCLK_IN (HSI_VALUE)
  327. #endif
  328. /**
  329. * @brief Sets System clock frequency to 8MHz and configure HCLK, PCLK2
  330. * and PCLK1 prescalers.
  331. * @note This function should be used only after reset.
  332. * @param None
  333. * @retval None
  334. */
  335. int SetSysClock(void)
  336. {
  337. int rc = 0;
  338. /* FIXME*/
  339. return rc;
  340. }
  341. #endif /* (SYSCLK_SOURCE == SYSCLK_HSI) || (SYSCLK_SOURCE == SYSCLK_HSE) */
  342. /**
  343. * @brief Sets RTC clock to selected source.
  344. *
  345. * @param source Clock source LSI/LSE/HSE
  346. * @retval -1 on error, 0 on success
  347. */
  348. int SetRTCClock(int source)
  349. {
  350. int rc = -1;
  351. /* Enable PWR Controller and access to the RTC backup domain*/
  352. CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_PWREN));
  353. CM3BBSET(PWR_BASE, PWR_TypeDef, CR, _BI32(PWR_CR_DBP));
  354. /* Reset RTC to allow selection */
  355. CM3BBSET(RCC_BASE, RCC_TypeDef, CSR, _BI32(RCC_CSR_RTCRST));
  356. CM3BBCLR(RCC_BASE, RCC_TypeDef, CSR, _BI32(RCC_CSR_RTCRST));
  357. switch (source)
  358. {
  359. case RTCCLK_LSI:
  360. rc = rcc_set_and_wait_rdy(
  361. CM3BBADDR(RCC_BASE, RCC_TypeDef, CSR, _BI32(RCC_CSR_LSION)),
  362. 1, HSE_STARTUP_TIMEOUT*1000);
  363. if (rc == -1)
  364. return rc;
  365. RCC->CSR &= ~RCC_CSR_RTCSEL;
  366. RCC->CSR |= RCC_CSR_RTCSEL_LSI;
  367. break;
  368. case RTCCLK_LSE:
  369. /* LSE Bypass can only be written with LSE off*/
  370. rc = rcc_set_and_wait_rdy(
  371. CM3BBADDR(RCC_BASE, RCC_TypeDef, CSR, _BI32(RCC_CSR_LSEON)),
  372. 0, HSE_STARTUP_TIMEOUT*1000);
  373. if (rc == -1)
  374. return rc;
  375. #if defined(LSE_BYPASS)
  376. CM3BBSET(RCC_BASE, RCC_TypeDef, CSR, _BI32(RCC_CSR_LSEBYP));
  377. #else
  378. CM3BBCLR(RCC_BASE, RCC_TypeDef, CSR, _BI32(RCC_CSR_LSEBYP));
  379. #endif
  380. rc = rcc_set_and_wait_rdy(
  381. CM3BBADDR(RCC_BASE, RCC_TypeDef, CSR, _BI32(RCC_CSR_LSEON)),
  382. 1, HSE_STARTUP_TIMEOUT*1000);
  383. if (rc == -1)
  384. return rc;
  385. RCC->CSR &= ~RCC_CSR_RTCSEL;
  386. RCC->CSR |= RCC_CSR_RTCSEL_LSE;
  387. break;
  388. case RTCCLK_HSE:
  389. RCC->CSR &= ~RCC_CSR_RTCSEL;
  390. RCC->CSR |= RCC_CSR_RTCSEL_HSE;
  391. break;
  392. }
  393. return rc;
  394. }
  395. /**
  396. * @brief requests System clock frequency
  397. *
  398. * @note This function should be used only after reset.
  399. * @param None
  400. * @retval None
  401. */
  402. uint32_t SysCtlClockGet(void)
  403. {
  404. SystemCoreClockUpdate();
  405. return SystemCoreClock;
  406. }
  407. /**
  408. * @brief requests frequency of the given clock
  409. *
  410. * @param idx NUT_HWCLK Index
  411. * @retval clock or 0 if idx points to an invalid clock
  412. */
  413. uint32_t STM_ClockGet(int idx)
  414. {
  415. SystemCoreClockUpdate();
  416. switch(idx) {
  417. case NUT_HWCLK_CPU:
  418. return SystemCoreClock;
  419. break;
  420. case NUT_HWCLK_PCLK1: {
  421. uint32_t tmp = (RCC->CFGR & RCC_CFGR_PPRE1) >> _BI32( RCC_CFGR_PPRE1_0);
  422. return SystemCoreClock/APBPrescTable[tmp];
  423. break;
  424. }
  425. case NUT_HWCLK_PCLK2: {
  426. uint32_t tmp = (RCC->CFGR & RCC_CFGR_PPRE2) >> _BI32( RCC_CFGR_PPRE2_0);
  427. return SystemCoreClock/APBPrescTable[tmp];
  428. break;
  429. }
  430. default:
  431. return 0;
  432. break;
  433. }
  434. }