stm32f1.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * stm32f1.c
  3. *
  4. * Core and peripheral registers.
  5. *
  6. * Written & released by Keir Fraser <keir.xen@gmail.com>
  7. *
  8. * This is free and unencumbered software released into the public domain.
  9. * See the file COPYING for more details, or visit <http://unlicense.org>.
  10. */
  11. static void clock_init(void)
  12. {
  13. /* Flash controller: reads require 2 wait states at 72MHz. */
  14. flash->acr = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY(2);
  15. /* Start up the external oscillator. */
  16. rcc->cr |= RCC_CR_HSEON;
  17. while (!(rcc->cr & RCC_CR_HSERDY))
  18. cpu_relax();
  19. /* PLLs, scalers, muxes. */
  20. rcc->cfgr = (RCC_CFGR_PLLMUL(9) | /* PLL = 9*8MHz = 72MHz */
  21. RCC_CFGR_PLLSRC_PREDIV1 |
  22. RCC_CFGR_ADCPRE_DIV8 |
  23. RCC_CFGR_PPRE1_DIV2);
  24. /* Enable and stabilise the PLL. */
  25. rcc->cr |= RCC_CR_PLLON;
  26. while (!(rcc->cr & RCC_CR_PLLRDY))
  27. cpu_relax();
  28. /* Switch to the externally-driven PLL for system clock. */
  29. rcc->cfgr |= RCC_CFGR_SW_PLL;
  30. while ((rcc->cfgr & RCC_CFGR_SWS_MASK) != RCC_CFGR_SWS_PLL)
  31. cpu_relax();
  32. /* Internal oscillator no longer needed. */
  33. rcc->cr &= ~RCC_CR_HSION;
  34. /* Enable SysTick counter at 72/8=9MHz. */
  35. stk->load = STK_MASK;
  36. stk->ctrl = STK_CTRL_ENABLE;
  37. }
  38. static void gpio_init(GPIO gpio)
  39. {
  40. /* Floating Input. Reference Manual states that JTAG pins are in PU/PD
  41. * mode at reset, so ensure all PU/PD are disabled. */
  42. gpio->crl = gpio->crh = 0x44444444u;
  43. }
  44. static void peripheral_init(void)
  45. {
  46. /* Enable basic GPIO and AFIO clocks, all timers, and DMA. */
  47. rcc->apb1enr = (RCC_APB1ENR_TIM2EN |
  48. RCC_APB1ENR_TIM3EN |
  49. RCC_APB1ENR_TIM4EN);
  50. rcc->apb2enr = (RCC_APB2ENR_IOPAEN |
  51. RCC_APB2ENR_IOPBEN |
  52. RCC_APB2ENR_IOPCEN |
  53. RCC_APB2ENR_AFIOEN |
  54. RCC_APB2ENR_TIM1EN);
  55. rcc->ahbenr = RCC_AHBENR_DMA1EN;
  56. /* Turn off serial-wire JTAG and reclaim the GPIOs. */
  57. afio->mapr = AFIO_MAPR_SWJ_CFG_DISABLED;
  58. /* All pins in a stable state. */
  59. gpio_init(gpioa);
  60. gpio_init(gpiob);
  61. gpio_init(gpioc);
  62. }
  63. void stm32_init(void)
  64. {
  65. cortex_init();
  66. clock_init();
  67. peripheral_init();
  68. cpu_sync();
  69. }
  70. void gpio_configure_pin(GPIO gpio, unsigned int pin, unsigned int mode)
  71. {
  72. gpio_write_pin(gpio, pin, mode >> 4);
  73. mode &= 0xfu;
  74. if (pin >= 8) {
  75. pin -= 8;
  76. gpio->crh = (gpio->crh & ~(0xfu<<(pin<<2))) | (mode<<(pin<<2));
  77. } else {
  78. gpio->crl = (gpio->crl & ~(0xfu<<(pin<<2))) | (mode<<(pin<<2));
  79. }
  80. }
  81. /*
  82. * Local variables:
  83. * mode: C
  84. * c-file-style: "Linux"
  85. * c-basic-offset: 4
  86. * tab-width: 4
  87. * indent-tabs-mode: nil
  88. * End:
  89. */