//go:build stm32f405 package runtime import ( "device/stm32" "machine" ) const ( // +---------------------------------------------+ // | Clock Settings | // +-------------+-------------------------------+ // | HSE | selectable (xtal_8/12/16_mhz) | // | SYSCLK | 168mhz | // | HCLK | 168mhz | // | APB1(PCLK1) | 42mhz | // | APB2(PCLK2) | 84mhz | // +-------------+-------------------------------+ HCLK_FREQ_HZ = 168000000 PCLK1_FREQ_HZ = HCLK_FREQ_HZ / 4 PCLK2_FREQ_HZ = HCLK_FREQ_HZ / 2 ) const ( PWR_SCALE1 = 1 << stm32.PWR_CSR_VOSRDY_Pos // max value of HCLK = 168 MHz PWR_SCALE2 = 0 // max value of HCLK = 144 MHz PLL_SRC_HSE = 1 << stm32.RCC_PLLCFGR_PLLSRC_Pos // use HSE for PLL and PLLI2S PLL_SRC_HSI = 0 // use HSI for PLL and PLLI2S SYSCLK_SRC_PLL = stm32.RCC_CFGR_SW_PLL << stm32.RCC_CFGR_SW_Pos SYSCLK_STAT_PLL = stm32.RCC_CFGR_SWS_PLL << stm32.RCC_CFGR_SWS_Pos RCC_DIV_PCLK1 = stm32.RCC_CFGR_PPRE1_Div4 << stm32.RCC_CFGR_PPRE1_Pos // HCLK / 4 RCC_DIV_PCLK2 = stm32.RCC_CFGR_PPRE2_Div2 << stm32.RCC_CFGR_PPRE2_Pos // HCLK / 2 RCC_DIV_HCLK = stm32.RCC_CFGR_HPRE_Div1 << stm32.RCC_CFGR_HPRE_Pos // SYSCLK / 1 CLK_CCM_RAM = 1 << 20 ) const ( // +-----------------------------------+ // | Voltage range = 2.7V - 3.6V | // +----------------+------------------+ // | Wait states | System Bus | // | (WS, LATENCY) | HCLK (MHz) | // +----------------+------------------+ // | 0 WS, 1 cycle | 0 < HCLK ≤ 30 | // | 1 WS, 2 cycles | 30 < HCLK ≤ 60 | // | 2 WS, 3 cycles | 60 < HCLK ≤ 90 | // | 3 WS, 4 cycles | 90 < HCLK ≤ 120 | // | 4 WS, 5 cycles | 120 < HCLK ≤ 150 | // | 5 WS, 6 cycles | 150 < HCLK ≤ 168 | // +----------------+------------------+ FLASH_LATENCY = 5 << stm32.FLASH_ACR_LATENCY_Pos // 5 WS (6 CPU cycles) // instruction cache, data cache, and prefetch FLASH_OPTIONS = stm32.FLASH_ACR_ICEN | stm32.FLASH_ACR_DCEN | stm32.FLASH_ACR_PRFTEN ) func init() { initOSC() // configure oscillators initCLK() initCOM() initTickTimer(&machine.TIM3) } func initOSC() { // enable voltage regulator stm32.RCC.APB1ENR.SetBits(stm32.RCC_APB1ENR_PWREN) stm32.PWR.CR.SetBits(PWR_SCALE1) // enable HSE stm32.RCC.CR.Set(stm32.RCC_CR_HSEON) for !stm32.RCC.CR.HasBits(stm32.RCC_CR_HSERDY) { } // Since the main-PLL configuration parameters cannot be changed once PLL is // enabled, it is recommended to configure PLL before enabling it (selection // of the HSI or HSE oscillator as PLL clock source, and configuration of // division factors M, N, P, and Q). // disable PLL and wait for it to reset stm32.RCC.CR.ClearBits(stm32.RCC_CR_PLLON) for stm32.RCC.CR.HasBits(stm32.RCC_CR_PLLRDY) { } // set HSE as PLL source and configure clock divisors pll := machine.PLLParams168MHz() stm32.RCC.PLLCFGR.Set(PLL_SRC_HSE | pll.M<>1)-1)< 0 { machine.InitSerial() } } func putchar(c byte) { machine.Serial.WriteByte(c) } func getchar() byte { for machine.Serial.Buffered() == 0 { Gosched() } v, _ := machine.Serial.ReadByte() return v } func buffered() int { return machine.Serial.Buffered() }