github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/runtime_stm32f103.go (about) 1 //go:build stm32 && stm32f103 2 3 package runtime 4 5 import ( 6 "device/stm32" 7 "machine" 8 ) 9 10 func init() { 11 initCLK() 12 13 machine.InitSerial() 14 15 initTickTimer(&machine.TIM4) 16 } 17 18 func putchar(c byte) { 19 machine.Serial.WriteByte(c) 20 } 21 22 func getchar() byte { 23 for machine.Serial.Buffered() == 0 { 24 Gosched() 25 } 26 v, _ := machine.Serial.ReadByte() 27 return v 28 } 29 30 func buffered() int { 31 return machine.Serial.Buffered() 32 } 33 34 // initCLK sets clock to 72MHz using HSE 8MHz crystal w/ PLL X 9 (8MHz x 9 = 72MHz). 35 func initCLK() { 36 stm32.FLASH.ACR.SetBits(stm32.FLASH_ACR_LATENCY_WS2) // Two wait states, per datasheet 37 stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_PPRE1_Div2 << stm32.RCC_CFGR_PPRE1_Pos) // prescale PCLK1 = HCLK/2 38 stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_PPRE2_Div1 << stm32.RCC_CFGR_PPRE2_Pos) // prescale PCLK2 = HCLK/1 39 stm32.RCC.CR.SetBits(stm32.RCC_CR_HSEON) // enable HSE clock 40 41 // wait for the HSEREADY flag 42 for !stm32.RCC.CR.HasBits(stm32.RCC_CR_HSERDY) { 43 } 44 45 stm32.RCC.CR.SetBits(stm32.RCC_CR_HSION) // enable HSI clock 46 47 // wait for the HSIREADY flag 48 for !stm32.RCC.CR.HasBits(stm32.RCC_CR_HSIRDY) { 49 } 50 51 stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_PLLSRC) // set PLL source to HSE 52 stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_PLLMUL_Mul9 << stm32.RCC_CFGR_PLLMUL_Pos) // multiply by 9 53 stm32.RCC.CR.SetBits(stm32.RCC_CR_PLLON) // enable the PLL 54 55 // wait for the PLLRDY flag 56 for !stm32.RCC.CR.HasBits(stm32.RCC_CR_PLLRDY) { 57 } 58 59 stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_SW_PLL) // set clock source to pll 60 61 // wait for PLL to be CLK 62 for !stm32.RCC.CFGR.HasBits(stm32.RCC_CFGR_SWS_PLL << stm32.RCC_CFGR_SWS_Pos) { 63 } 64 }