system.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. *
  3. * MIT License
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include <stdint.h>
  23. #include "system.h"
  24. volatile uint32_t cpu_freq = 168000000;
  25. static void flash_set_waitstates(int waitstates)
  26. {
  27. FLASH_ACR |= waitstates | FLASH_ACR_ENABLE_DATA_CACHE | FLASH_ACR_ENABLE_INST_CACHE;
  28. }
  29. void clock_pll_off(void)
  30. {
  31. uint32_t reg32;
  32. /* Enable internal high-speed oscillator. */
  33. RCC_CR |= RCC_CR_HSION;
  34. DMB();
  35. while ((RCC_CR & RCC_CR_HSIRDY) == 0) {};
  36. /* Select HSI as SYSCLK source. */
  37. reg32 = RCC_CFGR;
  38. reg32 &= ~((1 << 1) | (1 << 0));
  39. RCC_CFGR = (reg32 | RCC_CFGR_SW_HSI);
  40. DMB();
  41. /* Turn off PLL */
  42. RCC_CR &= ~RCC_CR_PLLON;
  43. DMB();
  44. }
  45. void clock_pll_on(int powersave)
  46. {
  47. uint32_t reg32;
  48. uint32_t plln, pllm, pllq, pllp, pllr, hpre, ppre1, ppre2, flash_waitstates;
  49. /* Enable Power controller */
  50. APB1_CLOCK_ER |= PWR_APB1_CLOCK_ER_VAL;
  51. /* Select clock parameters */
  52. if (powersave) { /* 48 MHz */
  53. cpu_freq = 48000000;
  54. pllm = 8;
  55. plln = 96;
  56. pllp = 2;
  57. pllq = 2;
  58. pllr = 0;
  59. hpre = RCC_PRESCALER_DIV_NONE;
  60. ppre1 = RCC_PRESCALER_DIV_4;
  61. ppre2 = RCC_PRESCALER_DIV_2;
  62. flash_waitstates = 5;
  63. } else { /* 168 MHz */
  64. cpu_freq = 168000000;
  65. pllm = 8;
  66. plln = 336;
  67. pllp = 2;
  68. pllq = 7;
  69. pllr = 0;
  70. hpre = RCC_PRESCALER_DIV_NONE;
  71. ppre1 = RCC_PRESCALER_DIV_4;
  72. ppre2 = RCC_PRESCALER_DIV_2;
  73. flash_waitstates = 3;
  74. }
  75. flash_set_waitstates(flash_waitstates);
  76. (void)pllr;
  77. /* Enable internal high-speed oscillator. */
  78. RCC_CR |= RCC_CR_HSION;
  79. DMB();
  80. while ((RCC_CR & RCC_CR_HSIRDY) == 0) {};
  81. /* Select HSI as SYSCLK source. */
  82. reg32 = RCC_CFGR;
  83. reg32 &= ~((1 << 1) | (1 << 0));
  84. RCC_CFGR = (reg32 | RCC_CFGR_SW_HSI);
  85. DMB();
  86. /* Enable external high-speed oscillator 8MHz. */
  87. RCC_CR |= RCC_CR_HSEON;
  88. DMB();
  89. while ((RCC_CR & RCC_CR_HSERDY) == 0) {};
  90. /*
  91. * Set prescalers for AHB, ADC, ABP1, ABP2.
  92. */
  93. reg32 = RCC_CFGR;
  94. reg32 &= ~(0xF0);
  95. RCC_CFGR = (reg32 | (hpre << 4));
  96. DMB();
  97. reg32 = RCC_CFGR;
  98. reg32 &= ~(0x1C00);
  99. RCC_CFGR = (reg32 | (ppre1 << 10));
  100. DMB();
  101. reg32 = RCC_CFGR;
  102. reg32 &= ~(0x07 << 13);
  103. RCC_CFGR = (reg32 | (ppre2 << 13));
  104. DMB();
  105. /* Set PLL config */
  106. reg32 = RCC_PLLCFGR;
  107. reg32 &= ~(PLL_FULL_MASK);
  108. RCC_PLLCFGR = reg32 | RCC_PLLCFGR_PLLSRC | pllm |
  109. (plln << 6) | (((pllp >> 1) - 1) << 16) |
  110. (pllq << 24);
  111. DMB();
  112. /* Enable power-save mode if selected */
  113. if (powersave) {
  114. POW_CR |= (POW_CR_VOS);
  115. }
  116. /* Enable PLL oscillator and wait for it to stabilize. */
  117. RCC_CR |= RCC_CR_PLLON;
  118. DMB();
  119. while ((RCC_CR & RCC_CR_PLLRDY) == 0) {};
  120. /* Select PLL as SYSCLK source. */
  121. reg32 = RCC_CFGR;
  122. reg32 &= ~((1 << 1) | (1 << 0));
  123. RCC_CFGR = (reg32 | RCC_CFGR_SW_PLL);
  124. DMB();
  125. /* Wait for PLL clock to be selected. */
  126. while ((RCC_CFGR & ((1 << 1) | (1 << 0))) != RCC_CFGR_SW_PLL) {};
  127. /* Disable internal high-speed oscillator. */
  128. RCC_CR &= ~RCC_CR_HSION;
  129. }
  130. void systick_on( void )
  131. {
  132. /* Initialize the Systick timer (no interrupts!) */
  133. SYSTICK_CSR = SYSTICK_CSR_ON;
  134. SYSTICK_CVR = SYSTICK_MAX;
  135. }
  136. void waitus( uint16_t const us )
  137. {
  138. uint32_t cmpTicks = 0u;
  139. SYSTICK_CVR = SYSTICK_MAX;
  140. cmpTicks = SYSTICK_MAX - ((uint32_t) us * SYSTICK_TICKS_PER_US);
  141. while (SYSTICK_CVR >= cmpTicks)
  142. {
  143. ; /* Wait */
  144. }
  145. return ;
  146. }
  147. void waitms( uint16_t const ms )
  148. {
  149. uint16_t timeMs = ms;
  150. while (timeMs)
  151. {
  152. waitus(1000u);
  153. --timeMs;
  154. }
  155. return ;
  156. }