diff options
author | deadprogram <[email protected]> | 2024-05-06 19:29:11 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-05-08 10:57:46 +0200 |
commit | 7d6b667bba801205e32c5ff3b460100b0409fda2 (patch) | |
tree | 244a3fa1238490c055212cffd9d055e96c0a66f0 | |
parent | 72f30ca6ec13b8c5eaa74e4881839fd7360f37ed (diff) | |
download | tinygo-7d6b667bba801205e32c5ff3b460100b0409fda2.tar.gz tinygo-7d6b667bba801205e32c5ff3b460100b0409fda2.zip |
machine/stm32: add i2c Frequency and SetBaudRate() function for boards that were missing implementation
Signed-off-by: deadprogram <[email protected]>
-rw-r--r-- | src/machine/machine_stm32_i2c_revb.go | 34 | ||||
-rw-r--r-- | src/machine/machine_stm32f7x2.go | 15 | ||||
-rw-r--r-- | src/machine/machine_stm32l0.go | 17 | ||||
-rw-r--r-- | src/machine/machine_stm32l4x2.go | 17 | ||||
-rw-r--r-- | src/machine/machine_stm32l4x5.go | 15 | ||||
-rw-r--r-- | src/machine/machine_stm32l4x6.go | 15 | ||||
-rw-r--r-- | src/machine/machine_stm32l5x2.go | 15 | ||||
-rw-r--r-- | src/machine/machine_stm32wlx.go | 15 |
8 files changed, 121 insertions, 22 deletions
diff --git a/src/machine/machine_stm32_i2c_revb.go b/src/machine/machine_stm32_i2c_revb.go index 925390294..006661f9a 100644 --- a/src/machine/machine_stm32_i2c_revb.go +++ b/src/machine/machine_stm32_i2c_revb.go @@ -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) @@ -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) @@ -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 { diff --git a/src/machine/machine_stm32f7x2.go b/src/machine/machine_stm32f7x2.go index 8755bc8ff..7da407071 100644 --- a/src/machine/machine_stm32f7x2.go +++ b/src/machine/machine_stm32f7x2.go @@ -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 + } } diff --git a/src/machine/machine_stm32l0.go b/src/machine/machine_stm32l0.go index 844cfccb4..e3fcefb57 100644 --- a/src/machine/machine_stm32l0.go +++ b/src/machine/machine_stm32l0.go @@ -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 + } } diff --git a/src/machine/machine_stm32l4x2.go b/src/machine/machine_stm32l4x2.go index 497cf3e1d..142a8f5f3 100644 --- a/src/machine/machine_stm32l4x2.go +++ b/src/machine/machine_stm32l4x2.go @@ -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 + } } diff --git a/src/machine/machine_stm32l4x5.go b/src/machine/machine_stm32l4x5.go index ec06544ba..c8c550c3d 100644 --- a/src/machine/machine_stm32l4x5.go +++ b/src/machine/machine_stm32l4x5.go @@ -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 + } } diff --git a/src/machine/machine_stm32l4x6.go b/src/machine/machine_stm32l4x6.go index cf546303a..de878ebe3 100644 --- a/src/machine/machine_stm32l4x6.go +++ b/src/machine/machine_stm32l4x6.go @@ -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 + } } diff --git a/src/machine/machine_stm32l5x2.go b/src/machine/machine_stm32l5x2.go index d439b8fd9..82ca1ecf6 100644 --- a/src/machine/machine_stm32l5x2.go +++ b/src/machine/machine_stm32l5x2.go @@ -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 + } } diff --git a/src/machine/machine_stm32wlx.go b/src/machine/machine_stm32wlx.go index d42ef2e38..b27c779fe 100644 --- a/src/machine/machine_stm32wlx.go +++ b/src/machine/machine_stm32wlx.go @@ -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 |