rp2040: i2c baud rate handling improvements

Этот коммит содержится в:
Yurii Soldak 2021-10-05 22:40:01 +02:00 коммит произвёл Ron Evans
родитель d21ffc63b9
коммит d1f1f267a3

Просмотреть файл

@ -96,6 +96,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
// SDA: 2, 6, 10, 14, 18, 26 // SDA: 2, 6, 10, 14, 18, 26
// SCL: 3, 7, 11, 15, 19, 27 // SCL: 3, 7, 11, 15, 19, 27
func (i2c *I2C) Configure(config I2CConfig) error { func (i2c *I2C) Configure(config I2CConfig) error {
const defaultBaud uint32 = 100_000 // 100kHz standard mode
if config.SCL == 0 { if config.SCL == 0 {
// If config pins are zero valued or clock pin is invalid then we set default values. // If config pins are zero valued or clock pin is invalid then we set default values.
switch i2c.Bus { switch i2c.Bus {
@ -107,6 +108,9 @@ func (i2c *I2C) Configure(config I2CConfig) error {
config.SDA = I2C1_SDA_PIN config.SDA = I2C1_SDA_PIN
} }
} }
if config.Frequency == 0 {
config.Frequency = defaultBaud
}
config.SDA.Configure(PinConfig{PinI2C}) config.SDA.Configure(PinConfig{PinI2C})
config.SCL.Configure(PinConfig{PinI2C}) config.SCL.Configure(PinConfig{PinI2C})
return i2c.init(config) return i2c.init(config)
@ -116,8 +120,13 @@ func (i2c *I2C) Configure(config I2CConfig) error {
// enabling the I2C hardware if disabled beforehand. // enabling the I2C hardware if disabled beforehand.
//go:inline //go:inline
func (i2c *I2C) SetBaudrate(br uint32) error { func (i2c *I2C) SetBaudrate(br uint32) error {
const freqin uint32 = 125 * MHz
// Find smallest prescale value which puts o if br == 0 {
return ErrInvalidI2CBaudrate
}
// I2C is synchronous design that runs from clk_sys
freqin := CPUFrequency()
// TODO there are some subtleties to I2C timing which we are completely ignoring here // TODO there are some subtleties to I2C timing which we are completely ignoring here
period := (freqin + br/2) / br period := (freqin + br/2) / br