stm32f7.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * stm32f7.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 7 wait states at 216MHz. */
  14. flash->acr = FLASH_ACR_ARTEN | FLASH_ACR_PRFTEN | FLASH_ACR_LATENCY(7);
  15. /* Bus divisors. */
  16. rcc->cfgr = (RCC_CFGR_PPRE2(4) | /* APB2 = 216MHz/2 = 108MHz */
  17. RCC_CFGR_PPRE1(5) | /* APB1 = 216MHz/4 = 54MHz */
  18. RCC_CFGR_HPRE(0)); /* AHB = 216MHz/1 = 216MHz */
  19. /* Timers run from Host Clock (216MHz). */
  20. rcc->dckcfgr1 = RCC_DCKCFGR1_TIMPRE;
  21. /* Start up the external oscillator. */
  22. rcc->cr |= RCC_CR_HSEON;
  23. while (!(rcc->cr & RCC_CR_HSERDY))
  24. cpu_relax();
  25. /* Main PLL. */
  26. rcc->pllcfgr = (RCC_PLLCFGR_PLLSRC_HSE | /* PLLSrc = HSE = 8MHz */
  27. RCC_PLLCFGR_PLLM(4) | /* PLL In = HSE/4 = 2MHz */
  28. RCC_PLLCFGR_PLLN(216) | /* PLLVCO = 2MHz*216 = 432MHz */
  29. RCC_PLLCFGR_PLLP(0) | /* SYSCLK = 432MHz/2 = 216MHz */
  30. RCC_PLLCFGR_PLLQ(9)); /* USB = 432MHz/9 = 48MHz */
  31. /* Enable and stabilise the PLL. */
  32. rcc->cr |= RCC_CR_PLLON;
  33. while (!(rcc->cr & RCC_CR_PLLRDY))
  34. cpu_relax();
  35. /* Switch to the externally-driven PLL for system clock. */
  36. rcc->cfgr |= RCC_CFGR_SW(2);
  37. while ((rcc->cfgr & RCC_CFGR_SWS(3)) != RCC_CFGR_SWS(2))
  38. cpu_relax();
  39. /* Internal oscillator no longer needed. */
  40. rcc->cr &= ~RCC_CR_HSION;
  41. /* Enable SysTick counter at 216MHz/8 = 27MHz. */
  42. stk->load = STK_MASK;
  43. stk->ctrl = STK_CTRL_ENABLE;
  44. }
  45. void peripheral_clock_delay(void)
  46. {
  47. delay_ticks(2);
  48. }
  49. static void peripheral_init(void)
  50. {
  51. /* Enable basic GPIO clocks, DTCM RAM, and DMA. */
  52. rcc->ahb1enr = (RCC_AHB1ENR_DMA2EN |
  53. RCC_AHB1ENR_DMA1EN |
  54. RCC_AHB1ENR_DTCMRAMEN |
  55. RCC_AHB1ENR_GPIOCEN |
  56. RCC_AHB1ENR_GPIOBEN |
  57. RCC_AHB1ENR_GPIOAEN);
  58. rcc->apb2enr = (RCC_APB2ENR_SYSCFGEN);
  59. peripheral_clock_delay();
  60. /* Release JTAG pins. */
  61. gpio_configure_pin(gpioa, 15, GPI_floating);
  62. gpio_configure_pin(gpiob, 3, GPI_floating);
  63. gpio_configure_pin(gpiob, 4, GPI_floating);
  64. }
  65. void stm32_init(void)
  66. {
  67. cortex_init();
  68. clock_init();
  69. peripheral_init();
  70. cpu_sync();
  71. }
  72. void gpio_configure_pin(GPIO gpio, unsigned int pin, unsigned int mode)
  73. {
  74. gpio_write_pin(gpio, pin, mode >> 7);
  75. gpio->moder = (gpio->moder & ~(3<<(pin<<1))) | ((mode&3)<<(pin<<1));
  76. mode >>= 2;
  77. gpio->otyper = (gpio->otyper & ~(1<<pin)) | ((mode&1)<<pin);
  78. mode >>= 1;
  79. gpio->ospeedr = (gpio->ospeedr & ~(3<<(pin<<1))) | ((mode&3)<<(pin<<1));
  80. mode >>= 2;
  81. gpio->pupdr = (gpio->pupdr & ~(3<<(pin<<1))) | ((mode&3)<<(pin<<1));
  82. }
  83. void gpio_set_af(GPIO gpio, unsigned int pin, unsigned int af)
  84. {
  85. if (pin < 8) {
  86. gpio->afrl = (gpio->afrl & ~(15<<(pin<<2))) | (af<<(pin<<2));
  87. } else {
  88. pin -= 8;
  89. gpio->afrh = (gpio->afrh & ~(15<<(pin<<2))) | (af<<(pin<<2));
  90. }
  91. }
  92. /*
  93. * Local variables:
  94. * mode: C
  95. * c-file-style: "Linux"
  96. * c-basic-offset: 4
  97. * tab-width: 4
  98. * indent-tabs-mode: nil
  99. * End:
  100. */