mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-21 12:59:04 +00:00
machine/stm32: add i2c Frequency and SetBaudRate() function for boards that were missing implementation
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -45,11 +45,21 @@ type I2C struct {
|
|||||||
|
|
||||||
// I2CConfig is used to store config info for I2C.
|
// I2CConfig is used to store config info for I2C.
|
||||||
type I2CConfig struct {
|
type I2CConfig struct {
|
||||||
|
Frequency uint32
|
||||||
SCL Pin
|
SCL Pin
|
||||||
SDA Pin
|
SDA Pin
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i2c *I2C) Configure(config I2CConfig) error {
|
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
|
// disable I2C interface before any configuration changes
|
||||||
i2c.Bus.CR1.ClearBits(stm32.I2C_CR1_PE)
|
i2c.Bus.CR1.ClearBits(stm32.I2C_CR1_PE)
|
||||||
|
|
||||||
@@ -63,8 +73,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
|||||||
}
|
}
|
||||||
i2c.configurePins(config)
|
i2c.configurePins(config)
|
||||||
|
|
||||||
// Frequency range
|
i2c.Bus.TIMINGR.Set(i2c.getFreqRange(config.Frequency))
|
||||||
i2c.Bus.TIMINGR.Set(i2c.getFreqRange())
|
|
||||||
|
|
||||||
// Disable Own Address1 before set the Own Address1 configuration
|
// Disable Own Address1 before set the Own Address1 configuration
|
||||||
i2c.Bus.OAR1.ClearBits(stm32.I2C_OAR1_OA1EN)
|
i2c.Bus.OAR1.ClearBits(stm32.I2C_OAR1_OA1EN)
|
||||||
@@ -86,10 +95,23 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
|||||||
|
|
||||||
// SetBaudRate sets the communication speed for I2C.
|
// SetBaudRate sets the communication speed for I2C.
|
||||||
func (i2c *I2C) SetBaudRate(br uint32) error {
|
func (i2c *I2C) SetBaudRate(br uint32) error {
|
||||||
// TODO: implement
|
switch br {
|
||||||
|
case 10 * KHz, 100 * KHz, 400 * KHz, 500 * KHz:
|
||||||
|
default:
|
||||||
return errI2CNotImplemented
|
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 {
|
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
if len(w) > 0 {
|
if len(w) > 0 {
|
||||||
if err := i2c.controllerTransmit(addr, w); nil != err {
|
if err := i2c.controllerTransmit(addr, w); nil != err {
|
||||||
|
|||||||
@@ -51,9 +51,20 @@ func (uart *UART) setRegisters() {
|
|||||||
//---------- I2C related code
|
//---------- I2C related code
|
||||||
|
|
||||||
// Gets the value for TIMINGR register
|
// 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
|
// This is a 'magic' value calculated by STM32CubeMX
|
||||||
// for 27MHz PCLK1 (216MHz CPU Freq / 8).
|
// for 27MHz PCLK1 (216MHz CPU Freq / 8).
|
||||||
// TODO: Do calculations based on PCLK1
|
// TODO: Do calculations based on PCLK1
|
||||||
|
switch br {
|
||||||
|
case 10 * KHz:
|
||||||
|
return 0x5010C0FF
|
||||||
|
case 100 * KHz:
|
||||||
return 0x00606A9B
|
return 0x00606A9B
|
||||||
|
case 400 * KHz:
|
||||||
|
return 0x00201625
|
||||||
|
case 500 * KHz:
|
||||||
|
return 0x00100429
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -298,9 +298,20 @@ func (spi SPI) configurePins(config SPIConfig) {
|
|||||||
//---------- I2C related types and code
|
//---------- I2C related types and code
|
||||||
|
|
||||||
// Gets the value for TIMINGR register
|
// 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
|
// This is a 'magic' value calculated by STM32CubeMX
|
||||||
// for 80MHz PCLK1.
|
// for 16MHz PCLK1.
|
||||||
// TODO: Do calculations based on PCLK1
|
// TODO: Do calculations based on PCLK1
|
||||||
|
switch br {
|
||||||
|
case 10 * KHz:
|
||||||
|
return 0x40003EFF
|
||||||
|
case 100 * KHz:
|
||||||
return 0x00303D5B
|
return 0x00303D5B
|
||||||
|
case 400 * KHz:
|
||||||
|
return 0x0010061A
|
||||||
|
case 500 * KHz:
|
||||||
|
return 0x00000117
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,20 @@ const APB2_TIM_FREQ = 80e6 // 80MHz
|
|||||||
//---------- I2C related code
|
//---------- I2C related code
|
||||||
|
|
||||||
// Gets the value for TIMINGR register
|
// 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
|
// These are 'magic' values calculated by STM32CubeMX
|
||||||
// for 80MHz PCLK1.
|
// for 80MHz PCLK1.
|
||||||
// TODO: Do calculations based on PCLK1
|
// TODO: Do calculations based on PCLK1
|
||||||
|
switch br {
|
||||||
|
case 10 * KHz:
|
||||||
|
return 0xF010F3FE
|
||||||
|
case 100 * KHz:
|
||||||
return 0x10909CEC
|
return 0x10909CEC
|
||||||
|
case 400 * KHz:
|
||||||
|
return 0x00702991
|
||||||
|
case 500 * KHz:
|
||||||
|
return 0x00300E84
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,20 @@ const APB2_TIM_FREQ = 120e6 // 120MHz
|
|||||||
//---------- I2C related code
|
//---------- I2C related code
|
||||||
|
|
||||||
// Gets the value for TIMINGR register
|
// 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
|
// This is a 'magic' value calculated by STM32CubeMX
|
||||||
// for 120MHz PCLK1.
|
// for 120MHz PCLK1.
|
||||||
// TODO: Do calculations based on PCLK1
|
// TODO: Do calculations based on PCLK1
|
||||||
|
switch br {
|
||||||
|
case 10 * KHz:
|
||||||
|
return 0x0 // does this even work? zero is weird here.
|
||||||
|
case 100 * KHz:
|
||||||
return 0x307075B1
|
return 0x307075B1
|
||||||
|
case 400 * KHz:
|
||||||
|
return 0x00B03FDB
|
||||||
|
case 500 * KHz:
|
||||||
|
return 0x005017C7
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,20 @@ const APB2_TIM_FREQ = 80e6 // 80MHz
|
|||||||
//---------- I2C related code
|
//---------- I2C related code
|
||||||
|
|
||||||
// Gets the value for TIMINGR register
|
// 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
|
// This is a 'magic' value calculated by STM32CubeMX
|
||||||
// for 80MHz PCLK1.
|
// for 80MHz PCLK1.
|
||||||
// TODO: Do calculations based on PCLK1
|
// TODO: Do calculations based on PCLK1
|
||||||
|
switch br {
|
||||||
|
case 10 * KHz:
|
||||||
|
return 0xF010F3FE
|
||||||
|
case 100 * KHz:
|
||||||
return 0x10909CEC
|
return 0x10909CEC
|
||||||
|
case 400 * KHz:
|
||||||
|
return 0x00702991
|
||||||
|
case 500 * KHz:
|
||||||
|
return 0x00300E84
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,9 +49,20 @@ func (uart *UART) setRegisters() {
|
|||||||
//---------- I2C related code
|
//---------- I2C related code
|
||||||
|
|
||||||
// Gets the value for TIMINGR register
|
// 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
|
// This is a 'magic' value calculated by STM32CubeMX
|
||||||
// for 110MHz PCLK1.
|
// for 110MHz PCLK1.
|
||||||
// TODO: Do calculations based on PCLK1
|
// TODO: Do calculations based on PCLK1
|
||||||
|
switch br {
|
||||||
|
case 10 * KHz:
|
||||||
|
return 0x0 // does this even work? zero is weird here.
|
||||||
|
case 100 * KHz:
|
||||||
return 0x40505681
|
return 0x40505681
|
||||||
|
case 400 * KHz:
|
||||||
|
return 0x00A03AC8
|
||||||
|
case 500 * KHz:
|
||||||
|
return 0x005015B6
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -289,11 +289,22 @@ func (spi SPI) getBaudRate(config SPIConfig) uint32 {
|
|||||||
//---------- I2C related code
|
//---------- I2C related code
|
||||||
|
|
||||||
// Gets the value for TIMINGR register
|
// 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
|
// This is a 'magic' value calculated by STM32CubeMX
|
||||||
// for 48Mhz PCLK1.
|
// for 48Mhz PCLK1.
|
||||||
// TODO: Do calculations based on PCLK1
|
// TODO: Do calculations based on PCLK1
|
||||||
|
switch br {
|
||||||
|
case 10 * KHz:
|
||||||
|
return 0x9010DEFF
|
||||||
|
case 100 * KHz:
|
||||||
return 0x20303E5D
|
return 0x20303E5D
|
||||||
|
case 400 * KHz:
|
||||||
|
return 0x2010091A
|
||||||
|
case 500 * KHz:
|
||||||
|
return 0x00201441
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------- UART related code
|
//---------- UART related code
|
||||||
|
|||||||
Reference in New Issue
Block a user