1
0

cdc_startup.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* This file is the part of the Lightweight USB device Stack for STM32 microcontrollers
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "stm32.h"
  16. static void cdc_init_rcc (void) {
  17. #if defined(STM32L0)
  18. _BST(RCC->APB1ENR, RCC_APB1ENR_PWREN);
  19. _BMD(PWR->CR, PWR_CR_VOS, PWR_CR_VOS_0);
  20. _WBC(PWR->CSR, PWR_CSR_VOSF);
  21. /* set FLASH latency to 1 */
  22. _BST(FLASH->ACR, FLASH_ACR_LATENCY);
  23. /* set clock at 32MHz PLL 6/3 HSI */
  24. _BMD(RCC->CFGR, RCC_CFGR_PLLDIV | RCC_CFGR_PLLMUL | RCC_CFGR_PLLSRC, RCC_CFGR_PLLDIV3 | RCC_CFGR_PLLMUL6);
  25. _BST(RCC->CR, RCC_CR_HSION);
  26. _WBS(RCC->CR, RCC_CR_HSIRDY);
  27. _BST(RCC->CR, RCC_CR_PLLON);
  28. _WBS(RCC->CR, RCC_CR_PLLRDY);
  29. /* switch clock to PLL */
  30. _BMD(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);
  31. _WVL(RCC->CFGR, RCC_CFGR_SWS, RCC_CFGR_SWS_PLL);
  32. #elif defined(STM32L1)
  33. _BST(RCC->APB1ENR, RCC_APB1ENR_PWREN);
  34. _BMD(PWR->CR, PWR_CR_VOS, PWR_CR_VOS_0);
  35. _WBC(PWR->CSR, PWR_CSR_VOSF);
  36. /* set FLASH latency to 1 */
  37. _BST(FLASH->ACR, FLASH_ACR_ACC64);
  38. _BST(FLASH->ACR, FLASH_ACR_LATENCY);
  39. /* set clock at 32 MHz PLL 6/3 HSI */
  40. _BMD(RCC->CFGR, RCC_CFGR_PLLDIV | RCC_CFGR_PLLMUL | RCC_CFGR_PLLSRC, RCC_CFGR_PLLDIV3 | RCC_CFGR_PLLMUL6);
  41. _BST(RCC->CR, RCC_CR_HSION);
  42. _WBS(RCC->CR, RCC_CR_HSIRDY);
  43. _BST(RCC->CR, RCC_CR_PLLON);
  44. _WBS(RCC->CR, RCC_CR_PLLRDY);
  45. /* switch clock to PLL */
  46. _BMD(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);
  47. _WVL(RCC->CFGR, RCC_CFGR_SWS, RCC_CFGR_SWS_PLL);
  48. #elif defined(STM32L4)
  49. _BST(RCC->APB1ENR1, RCC_APB1ENR1_PWREN);
  50. /* Set power Range 1 */
  51. _BMD(PWR->CR1, PWR_CR1_VOS, PWR_CR1_VOS_0);
  52. _WBC(PWR->SR2, PWR_SR2_VOSF);
  53. /* Adjust Flash latency */
  54. _BMD(FLASH->ACR, FLASH_ACR_LATENCY, FLASH_ACR_LATENCY_2WS);
  55. /* set clock 48Mhz MSI */
  56. _BMD(RCC->CR, RCC_CR_MSIRANGE, RCC_CR_MSIRANGE_11 | RCC_CR_MSIRGSEL);
  57. /* set MSI as 48MHz USB */
  58. _BMD(RCC->CCIPR, RCC_CCIPR_CLK48SEL, RCC_CCIPR_CLK48SEL_0 | RCC_CCIPR_CLK48SEL_1);
  59. /* enable GPIOA clock */
  60. _BST(RCC->AHB2ENR, RCC_AHB2ENR_GPIOAEN);
  61. /* set GP11 and GP12 as USB data pins AF10 */
  62. _BST(GPIOA->AFR[1], (0x0A << 12) | (0x0A << 16));
  63. _BMD(GPIOA->MODER, (0x03 << 22) | (0x03 << 24), (0x02 << 22) | (0x02 << 24));
  64. #elif defined(STM32F103x6)
  65. /* set flash latency 1WS */
  66. _BMD(FLASH->ACR, FLASH_ACR_LATENCY, FLASH_ACR_LATENCY_1);
  67. /* use PLL 48MHz clock from 8Mhz HSI */
  68. _BMD(RCC->CFGR,
  69. RCC_CFGR_PLLMULL | RCC_CFGR_PLLSRC | RCC_CFGR_USBPRE,
  70. RCC_CFGR_PLLMULL12 | RCC_CFGR_USBPRE);
  71. _BST(RCC->CR, RCC_CR_PLLON);
  72. _WBS(RCC->CR, RCC_CR_PLLRDY);
  73. /* switch to PLL */
  74. _BMD(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);
  75. _WVL(RCC->CFGR, RCC_CFGR_SWS, RCC_CFGR_SWS_PLL);
  76. #elif defined(STM32F303xE)
  77. /* set flash latency 1WS */
  78. _BMD(FLASH->ACR, FLASH_ACR_LATENCY, FLASH_ACR_LATENCY_1);
  79. /* use PLL 48MHz clock from 8Mhz HSI */
  80. _BMD(RCC->CFGR,
  81. RCC_CFGR_PLLMUL | RCC_CFGR_PLLSRC | RCC_CFGR_USBPRE ,
  82. RCC_CFGR_PLLMUL12 | RCC_CFGR_USBPRE);
  83. _BST(RCC->CR, RCC_CR_PLLON);
  84. _WBS(RCC->CR, RCC_CR_PLLRDY);
  85. /* switch to PLL */
  86. _BMD(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);
  87. _WVL(RCC->CFGR, RCC_CFGR_SWS, RCC_CFGR_SWS_PLL);
  88. #else
  89. #error Not supported
  90. #endif
  91. }
  92. void __libc_init_array(void) {
  93. }
  94. void SystemInit(void) {
  95. cdc_init_rcc();
  96. }