cdc_startup.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #else
  65. #error Not supported
  66. #endif
  67. }
  68. void __libc_init_array(void) {
  69. }
  70. void SystemInit(void) {
  71. cdc_init_rcc();
  72. }