stm32f7.c 3.5 KB

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