github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/machine/machine_stm32f7x2.go (about) 1 //go:build stm32f7x2 2 3 package machine 4 5 // Peripheral abstraction layer for the stm32f407 6 7 import ( 8 "device/stm32" 9 ) 10 11 func CPUFrequency() uint32 { 12 return 216000000 13 } 14 15 // Internal use: configured speed of the APB1 and APB2 timers, this should be kept 16 // in sync with any changes to runtime package which configures the oscillators 17 // and clock frequencies 18 const APB1_TIM_FREQ = 54e6 // 54MHz 19 const APB2_TIM_FREQ = 216e6 // 216MHz 20 21 //---------- UART related code 22 23 // Configure the UART. 24 func (uart *UART) configurePins(config UARTConfig) { 25 // enable the alternate functions on the TX and RX pins 26 config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector) 27 config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector) 28 } 29 30 // UART baudrate calc based on the bus and clockspeed 31 // NOTE: keep this in sync with the runtime/runtime_stm32f7x2.go clock init code 32 func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 { 33 var clock uint32 34 switch uart.Bus { 35 case stm32.USART1, stm32.USART6: 36 clock = CPUFrequency() / 2 // APB2 Frequency 37 case stm32.USART2, stm32.USART3, stm32.UART4, stm32.UART5: 38 clock = CPUFrequency() / 8 // APB1 Frequency 39 } 40 return clock / baudRate 41 } 42 43 // Register names vary by ST processor, these are for STM F7x2 44 func (uart *UART) setRegisters() { 45 uart.rxReg = &uart.Bus.RDR 46 uart.txReg = &uart.Bus.TDR 47 uart.statusReg = &uart.Bus.ISR 48 uart.txEmptyFlag = stm32.USART_ISR_TXE 49 } 50 51 //---------- I2C related code 52 53 // Gets the value for TIMINGR register 54 func (i2c *I2C) getFreqRange() uint32 { 55 // This is a 'magic' value calculated by STM32CubeMX 56 // for 27MHz PCLK1 (216MHz CPU Freq / 8). 57 // TODO: Do calculations based on PCLK1 58 return 0x00606A9B 59 }