github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/machine/machine_stm32l5x2.go (about)

     1  //go:build stm32l5x2
     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 110000000
    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 = 110e6 // 110MHz
    19  const APB2_TIM_FREQ = 110e6 // 110MHz
    20  
    21  //---------- UART related code
    22  
    23  // Configure the UART.
    24  func (uart *UART) configurePins(config UARTConfig) {
    25  	if config.RX.getPort() == stm32.GPIOG || config.TX.getPort() == stm32.GPIOG {
    26  		// Enable VDDIO2 power supply, which is an independant power supply for the PGx pins
    27  		stm32.PWR.CR2.SetBits(stm32.PWR_CR2_IOSV)
    28  	}
    29  
    30  	// enable the alternate functions on the TX and RX pins
    31  	config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
    32  	config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
    33  }
    34  
    35  // UART baudrate calc based on the bus and clockspeed
    36  // NOTE: keep this in sync with the runtime/runtime_stm32l5x2.go clock init code
    37  func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 {
    38  	return 256 * (CPUFrequency() / baudRate)
    39  }
    40  
    41  // Register names vary by ST processor, these are for STM L5
    42  func (uart *UART) setRegisters() {
    43  	uart.rxReg = &uart.Bus.RDR
    44  	uart.txReg = &uart.Bus.TDR
    45  	uart.statusReg = &uart.Bus.ISR
    46  	uart.txEmptyFlag = stm32.USART_ISR_TXE
    47  }
    48  
    49  //---------- I2C related code
    50  
    51  // Gets the value for TIMINGR register
    52  func (i2c *I2C) getFreqRange() uint32 {
    53  	// This is a 'magic' value calculated by STM32CubeMX
    54  	// for 110MHz PCLK1.
    55  	// TODO: Do calculations based on PCLK1
    56  	return 0x40505681
    57  }