1
0

cdc_startup.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 "macro.h"
  16. #include "stm32.h"
  17. static void cdc_init_rcc (void) {
  18. #if defined(STM32L0)
  19. _BST(RCC->APB1ENR, RCC_APB1ENR_PWREN);
  20. _BMD(PWR->CR, PWR_CR_VOS, PWR_CR_VOS_0);
  21. _WBC(PWR->CSR, PWR_CSR_VOSF);
  22. /* set FLASH latency to 1 */
  23. _BST(FLASH->ACR, FLASH_ACR_LATENCY);
  24. /* set clock at 32MHz PLL 6/3 HSI */
  25. _BMD(RCC->CFGR, RCC_CFGR_PLLDIV | RCC_CFGR_PLLMUL | RCC_CFGR_PLLSRC, RCC_CFGR_PLLDIV3 | RCC_CFGR_PLLMUL6);
  26. _BST(RCC->CR, RCC_CR_HSION);
  27. _WBS(RCC->CR, RCC_CR_HSIRDY);
  28. _BST(RCC->CR, RCC_CR_PLLON);
  29. _WBS(RCC->CR, RCC_CR_PLLRDY);
  30. /* switch clock to PLL */
  31. _BMD(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);
  32. _WVL(RCC->CFGR, RCC_CFGR_SWS, RCC_CFGR_SWS_PLL);
  33. #elif defined(STM32L1)
  34. _BST(RCC->APB1ENR, RCC_APB1ENR_PWREN);
  35. _BMD(PWR->CR, PWR_CR_VOS, PWR_CR_VOS_0);
  36. _WBC(PWR->CSR, PWR_CSR_VOSF);
  37. /* set FLASH latency to 1 */
  38. _BST(FLASH->ACR, FLASH_ACR_ACC64);
  39. _BST(FLASH->ACR, FLASH_ACR_LATENCY);
  40. /* set clock at 32 MHz PLL 6/3 HSI */
  41. _BMD(RCC->CFGR, RCC_CFGR_PLLDIV | RCC_CFGR_PLLMUL | RCC_CFGR_PLLSRC, RCC_CFGR_PLLDIV3 | RCC_CFGR_PLLMUL6);
  42. _BST(RCC->CR, RCC_CR_HSION);
  43. _WBS(RCC->CR, RCC_CR_HSIRDY);
  44. _BST(RCC->CR, RCC_CR_PLLON);
  45. _WBS(RCC->CR, RCC_CR_PLLRDY);
  46. /* switch clock to PLL */
  47. _BMD(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);
  48. _WVL(RCC->CFGR, RCC_CFGR_SWS, RCC_CFGR_SWS_PLL);
  49. #elif defined(STM32L4)
  50. _BST(RCC->APB1ENR1, RCC_APB1ENR1_PWREN);
  51. /* Set power Range 1 */
  52. _BMD(PWR->CR1, PWR_CR1_VOS, PWR_CR1_VOS_0);
  53. _WBC(PWR->SR2, PWR_SR2_VOSF);
  54. /* Adjust Flash latency */
  55. _BMD(FLASH->ACR, FLASH_ACR_LATENCY, FLASH_ACR_LATENCY_2WS);
  56. /* set clock 48Mhz MSI */
  57. _BMD(RCC->CR, RCC_CR_MSIRANGE, RCC_CR_MSIRANGE_11 | RCC_CR_MSIRGSEL);
  58. /* set MSI as 48MHz USB */
  59. _BMD(RCC->CCIPR, RCC_CCIPR_CLK48SEL, RCC_CCIPR_CLK48SEL_0 | RCC_CCIPR_CLK48SEL_1);
  60. /* enable GPIOA clock */
  61. _BST(RCC->AHB2ENR, RCC_AHB2ENR_GPIOAEN);
  62. /* set GP11 and GP12 as USB data pins AF10 */
  63. _BST(GPIOA->AFR[1], (0x0A << 12) | (0x0A << 16));
  64. _BMD(GPIOA->MODER, (0x03 << 22) | (0x03 << 24), (0x02 << 22) | (0x02 << 24));
  65. #else
  66. #error Not supported
  67. #endif
  68. }
  69. void __libc_init_array(void) {
  70. }
  71. void SystemInit(void) {
  72. cdc_init_rcc();
  73. }