Skip to content

Commit

Permalink
machine/stm32: add i2c Frequency and SetBaudRate() function for board…
Browse files Browse the repository at this point in the history
…s that were missing implementation

Signed-off-by: deadprogram <ron@hybridgroup.com>
  • Loading branch information
deadprogram committed May 8, 2024
1 parent 72f30ca commit 7d6b667
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 22 deletions.
34 changes: 28 additions & 6 deletions src/machine/machine_stm32_i2c_revb.go
Expand Up @@ -45,11 +45,21 @@ type I2C struct {

// I2CConfig is used to store config info for I2C.
type I2CConfig struct {
SCL Pin
SDA Pin
Frequency uint32
SCL Pin
SDA Pin
}

func (i2c *I2C) Configure(config I2CConfig) error {
// Frequency range
switch config.Frequency {
case 0:
config.Frequency = 100 * KHz
case 10 * KHz, 100 * KHz, 400 * KHz, 500 * KHz:
default:
return errI2CNotImplemented
}

// disable I2C interface before any configuration changes
i2c.Bus.CR1.ClearBits(stm32.I2C_CR1_PE)

Expand All @@ -63,8 +73,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
}
i2c.configurePins(config)

// Frequency range
i2c.Bus.TIMINGR.Set(i2c.getFreqRange())
i2c.Bus.TIMINGR.Set(i2c.getFreqRange(config.Frequency))

// Disable Own Address1 before set the Own Address1 configuration
i2c.Bus.OAR1.ClearBits(stm32.I2C_OAR1_OA1EN)
Expand All @@ -86,8 +95,21 @@ func (i2c *I2C) Configure(config I2CConfig) error {

// SetBaudRate sets the communication speed for I2C.
func (i2c *I2C) SetBaudRate(br uint32) error {
// TODO: implement
return errI2CNotImplemented
switch br {
case 10 * KHz, 100 * KHz, 400 * KHz, 500 * KHz:
default:
return errI2CNotImplemented
}

// disable I2C interface before any configuration changes
i2c.Bus.CR1.ClearBits(stm32.I2C_CR1_PE)

i2c.Bus.TIMINGR.Set(i2c.getFreqRange(br))

// Disable Generalcall and NoStretch, Enable peripheral
i2c.Bus.CR1.Set(stm32.I2C_CR1_PE)

return nil
}

func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
Expand Down
15 changes: 13 additions & 2 deletions src/machine/machine_stm32f7x2.go
Expand Up @@ -51,9 +51,20 @@ func (uart *UART) setRegisters() {
//---------- I2C related code

// Gets the value for TIMINGR register
func (i2c *I2C) getFreqRange() uint32 {
func (i2c *I2C) getFreqRange(br uint32) uint32 {
// This is a 'magic' value calculated by STM32CubeMX
// for 27MHz PCLK1 (216MHz CPU Freq / 8).
// TODO: Do calculations based on PCLK1
return 0x00606A9B
switch br {
case 10 * KHz:
return 0x5010C0FF
case 100 * KHz:
return 0x00606A9B
case 400 * KHz:
return 0x00201625
case 500 * KHz:
return 0x00100429
default:
return 0
}
}
17 changes: 14 additions & 3 deletions src/machine/machine_stm32l0.go
Expand Up @@ -298,9 +298,20 @@ func (spi SPI) configurePins(config SPIConfig) {
//---------- I2C related types and code

// Gets the value for TIMINGR register
func (i2c I2C) getFreqRange() uint32 {
func (i2c I2C) getFreqRange(br uint32) uint32 {
// This is a 'magic' value calculated by STM32CubeMX
// for 80MHz PCLK1.
// for 16MHz PCLK1.
// TODO: Do calculations based on PCLK1
return 0x00303D5B
switch br {
case 10 * KHz:
return 0x40003EFF
case 100 * KHz:
return 0x00303D5B
case 400 * KHz:
return 0x0010061A
case 500 * KHz:
return 0x00000117
default:
return 0
}
}
17 changes: 14 additions & 3 deletions src/machine/machine_stm32l4x2.go
Expand Up @@ -17,9 +17,20 @@ const APB2_TIM_FREQ = 80e6 // 80MHz
//---------- I2C related code

// Gets the value for TIMINGR register
func (i2c *I2C) getFreqRange() uint32 {
// This is a 'magic' value calculated by STM32CubeMX
func (i2c *I2C) getFreqRange(br uint32) uint32 {
// These are 'magic' values calculated by STM32CubeMX
// for 80MHz PCLK1.
// TODO: Do calculations based on PCLK1
return 0x10909CEC
switch br {
case 10 * KHz:
return 0xF010F3FE
case 100 * KHz:
return 0x10909CEC
case 400 * KHz:
return 0x00702991
case 500 * KHz:
return 0x00300E84
default:
return 0
}
}
15 changes: 13 additions & 2 deletions src/machine/machine_stm32l4x5.go
Expand Up @@ -17,9 +17,20 @@ const APB2_TIM_FREQ = 120e6 // 120MHz
//---------- I2C related code

// Gets the value for TIMINGR register
func (i2c *I2C) getFreqRange() uint32 {
func (i2c *I2C) getFreqRange(br uint32) uint32 {
// This is a 'magic' value calculated by STM32CubeMX
// for 120MHz PCLK1.
// TODO: Do calculations based on PCLK1
return 0x307075B1
switch br {
case 10 * KHz:
return 0x0 // does this even work? zero is weird here.
case 100 * KHz:
return 0x307075B1
case 400 * KHz:
return 0x00B03FDB
case 500 * KHz:
return 0x005017C7
default:
return 0
}
}
15 changes: 13 additions & 2 deletions src/machine/machine_stm32l4x6.go
Expand Up @@ -17,9 +17,20 @@ const APB2_TIM_FREQ = 80e6 // 80MHz
//---------- I2C related code

// Gets the value for TIMINGR register
func (i2c *I2C) getFreqRange() uint32 {
func (i2c *I2C) getFreqRange(br uint32) uint32 {
// This is a 'magic' value calculated by STM32CubeMX
// for 80MHz PCLK1.
// TODO: Do calculations based on PCLK1
return 0x10909CEC
switch br {
case 10 * KHz:
return 0xF010F3FE
case 100 * KHz:
return 0x10909CEC
case 400 * KHz:
return 0x00702991
case 500 * KHz:
return 0x00300E84
default:
return 0
}
}
15 changes: 13 additions & 2 deletions src/machine/machine_stm32l5x2.go
Expand Up @@ -49,9 +49,20 @@ func (uart *UART) setRegisters() {
//---------- I2C related code

// Gets the value for TIMINGR register
func (i2c *I2C) getFreqRange() uint32 {
func (i2c *I2C) getFreqRange(br uint32) uint32 {
// This is a 'magic' value calculated by STM32CubeMX
// for 110MHz PCLK1.
// TODO: Do calculations based on PCLK1
return 0x40505681
switch br {
case 10 * KHz:
return 0x0 // does this even work? zero is weird here.
case 100 * KHz:
return 0x40505681
case 400 * KHz:
return 0x00A03AC8
case 500 * KHz:
return 0x005015B6
default:
return 0
}
}
15 changes: 13 additions & 2 deletions src/machine/machine_stm32wlx.go
Expand Up @@ -289,11 +289,22 @@ func (spi SPI) getBaudRate(config SPIConfig) uint32 {
//---------- I2C related code

// Gets the value for TIMINGR register
func (i2c *I2C) getFreqRange() uint32 {
func (i2c *I2C) getFreqRange(br uint32) uint32 {
// This is a 'magic' value calculated by STM32CubeMX
// for 48Mhz PCLK1.
// TODO: Do calculations based on PCLK1
return 0x20303E5D
switch br {
case 10 * KHz:
return 0x9010DEFF
case 100 * KHz:
return 0x20303E5D
case 400 * KHz:
return 0x2010091A
case 500 * KHz:
return 0x00201441
default:
return 0
}
}

//---------- UART related code
Expand Down

0 comments on commit 7d6b667

Please sign in to comment.