board.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "board.h"
  2. #include <stm32f407xx.h>
  3. #define RCC_PLL_N 0x150
  4. #define RCC_PLL_Q 0x7
  5. void Board::init_start() {
  6. RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  7. while (!(RCC->CR & RCC_CR_HSERDY))
  8. ;
  9. FLASH->ACR |= FLASH_ACR_PRFTEN;
  10. FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
  11. FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_5WS; // 5 WS (6 CPU cycles)
  12. FLASH->ACR |= FLASH_ACR_ICEN | FLASH_ACR_DCEN;
  13. RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
  14. RCC->CFGR |= RCC_CFGR_PPRE1_DIV4;
  15. RCC->PLLCFGR = RCC_PLLCFGR_PLLM_3 | (RCC_PLL_N << RCC_PLLCFGR_PLLN_Pos) | RCC_PLLCFGR_PLLSRC_HSE |
  16. (RCC_PLL_Q << RCC_PLLCFGR_PLLQ_Pos);
  17. RCC->CFGR |= (RCC_CFGR_PPRE1_0 | RCC_CFGR_PPRE1_2);
  18. RCC->CR |= RCC_CR_PLLON;
  19. while ((RCC->CR & RCC_CR_PLLRDY) == 0) {
  20. };
  21. RCC->CFGR &= (uint32_t)((uint32_t) ~(RCC_CFGR_SW));
  22. RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
  23. while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
  24. ;
  25. SystemCoreClockUpdate();
  26. RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;
  27. GPIOD->MODER |= GPIO_MODER_MODER12_0;
  28. }
  29. void Board::enable_timer() {
  30. SysTick_Config(SystemCoreClock / 1000);
  31. }
  32. volatile uint64_t current_time = 0;
  33. rl::TimePoint Board::get_now() {
  34. ROTOR_LIGHT_DISABLE_INTERRUPTS();
  35. auto copy = current_time;
  36. ROTOR_LIGHT_ENABLE_INTERRUPTS();
  37. return copy;
  38. }
  39. void Board::toggle_led() {
  40. GPIOD->ODR ^= GPIO_ODR_OD12;
  41. }
  42. void Board::delay() {
  43. auto end = Board::get_now() + 1000;
  44. while (Board::get_now() < end);
  45. }
  46. extern "C" {
  47. void SysTick_Handler(void) {
  48. ++current_time;
  49. }
  50. }