machine: add KHz, MHz, GHz constants, deprecate TWI_FREQ_* constants
There are two main issues with these constants: * They don't follow the Go naming convention. * They call themselves "TWI", while it makes a lot more sense to refer to the actual name which is I2C (or I²C). I have not removed them but just deprecated them. Perhaps we can remove them when we move towards version 1.0.
Этот коммит содержится в:
родитель
bc946f346d
коммит
7513fa2c96
11 изменённых файлов: 24 добавлений и 29 удалений
|
@ -8,6 +8,8 @@ import (
|
|||
)
|
||||
|
||||
// TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.
|
||||
//
|
||||
// Deprecated: use 100 * machine.KHz or 400 * machine.KHz instead.
|
||||
const (
|
||||
TWI_FREQ_100KHZ = 100000
|
||||
TWI_FREQ_400KHZ = 400000
|
||||
|
|
|
@ -18,6 +18,13 @@ var (
|
|||
// particular chip but instead runs in WebAssembly for example.
|
||||
const Device = deviceName
|
||||
|
||||
// Generic constants.
|
||||
const (
|
||||
KHz = 1000
|
||||
MHz = 1000_000
|
||||
GHz = 1000_000_000
|
||||
)
|
||||
|
||||
// PinMode sets the direction and pull mode of the pin. For example, PinOutput
|
||||
// sets the pin as an output and PinInputPullup sets the pin as an input with a
|
||||
// pull-up.
|
||||
|
|
|
@ -26,7 +26,7 @@ type I2CConfig struct {
|
|||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
// Default I2C bus speed is 100 kHz.
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
config.Frequency = 100 * KHz
|
||||
}
|
||||
|
||||
// Activate internal pullups for twi.
|
||||
|
|
|
@ -677,7 +677,7 @@ const i2cTimeout = 1000
|
|||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
// Default I2C bus speed is 100 kHz.
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
config.Frequency = 100 * KHz
|
||||
}
|
||||
if config.SDA == 0 && config.SCL == 0 {
|
||||
config.SDA = SDA_PIN
|
||||
|
|
|
@ -1151,7 +1151,7 @@ const i2cTimeout = 1000
|
|||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
// Default I2C bus speed is 100 kHz.
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
config.Frequency = 100 * KHz
|
||||
}
|
||||
|
||||
// Use default I2C pins if not set.
|
||||
|
|
|
@ -231,7 +231,7 @@ type I2CConfig struct {
|
|||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
var i2cClockFrequency uint32 = 32000000
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
config.Frequency = 100 * KHz
|
||||
}
|
||||
|
||||
if config.SDA == 0 && config.SCL == 0 {
|
||||
|
|
|
@ -523,7 +523,7 @@ type I2CConfig struct {
|
|||
func (i2c *I2C) Configure(config I2CConfig) error {
|
||||
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
config.Frequency = 100 * KHz
|
||||
}
|
||||
|
||||
if config.SDA == 0 && config.SCL == 0 {
|
||||
|
|
|
@ -10,15 +10,6 @@ import (
|
|||
"errors"
|
||||
)
|
||||
|
||||
const (
|
||||
TWI_FREQ_BUS = 24000000 // LPI2C root clock is on 24 MHz OSC
|
||||
TWI_FREQ_100KHZ = 100000 // StandardMode (100 kHz)
|
||||
TWI_FREQ_400KHZ = 400000 // FastMode (400 kHz)
|
||||
TWI_FREQ_1MHZ = 1000000 // FastModePlus (1 MHz)
|
||||
TWI_FREQ_5MHZ = 5000000 // UltraFastMode (5 MHz)
|
||||
TWI_FREQ_DEFAULT = TWI_FREQ_100KHZ // default to StandardMode (100 kHz)
|
||||
)
|
||||
|
||||
var (
|
||||
errI2CWriteTimeout = errors.New("I2C timeout during write")
|
||||
errI2CReadTimeout = errors.New("I2C timeout during read")
|
||||
|
@ -174,7 +165,7 @@ func (i2c *I2C) Configure(config I2CConfig) {
|
|||
|
||||
freq := config.Frequency
|
||||
if 0 == freq {
|
||||
freq = TWI_FREQ_DEFAULT
|
||||
freq = 100 * KHz
|
||||
}
|
||||
|
||||
// reset clock and registers, and enable LPI2C module interface
|
||||
|
@ -305,7 +296,7 @@ func (i2c *I2C) setFrequency(freq uint32) {
|
|||
wasEnabled := i2c.Bus.MCR.HasBits(nxp.LPI2C_MCR_MEN)
|
||||
i2c.Bus.MCR.ClearBits(nxp.LPI2C_MCR_MEN)
|
||||
|
||||
// baud rate = (TWI_FREQ_BUS/(2^pre))/(CLKLO+1 + CLKHI+1 + FLOOR((2+FILTSCL)/(2^pre)))
|
||||
// baud rate = (24MHz/(2^pre))/(CLKLO+1 + CLKHI+1 + FLOOR((2+FILTSCL)/(2^pre)))
|
||||
// assume: CLKLO=2*CLKHI, SETHOLD=CLKHI, DATAVD=CLKHI/2
|
||||
for pre := uint32(1); pre <= 128; pre *= 2 {
|
||||
if bestError == 0 {
|
||||
|
@ -314,9 +305,9 @@ func (i2c *I2C) setFrequency(freq uint32) {
|
|||
for clkHi := uint32(1); clkHi < 32; clkHi++ {
|
||||
var absError, rate uint32
|
||||
if clkHi == 1 {
|
||||
rate = (TWI_FREQ_BUS / pre) / (1 + 3 + 2 + 2/pre)
|
||||
rate = (24 * MHz / pre) / (1 + 3 + 2 + 2/pre)
|
||||
} else {
|
||||
rate = (TWI_FREQ_BUS / pre) / (3*clkHi + 2 + 2/pre)
|
||||
rate = (24 * MHz / pre) / (3*clkHi + 2 + 2/pre)
|
||||
}
|
||||
if freq > rate {
|
||||
absError = freq - rate
|
||||
|
@ -370,15 +361,15 @@ func (i2c *I2C) setFrequency(freq uint32) {
|
|||
mcfgr2, mcfgr3 uint32
|
||||
)
|
||||
const i2cClockStretchTimeout = 15000 // microseconds
|
||||
if freq >= TWI_FREQ_5MHZ {
|
||||
if freq >= 5*MHz {
|
||||
// I2C UltraFastMode 5 MHz
|
||||
mcfgr2 = 0 // disable glitch filters and timeout for UltraFastMode
|
||||
mcfgr3 = 0 //
|
||||
} else if freq >= TWI_FREQ_1MHZ {
|
||||
} else if freq >= 1*MHz {
|
||||
// I2C FastModePlus 1 MHz
|
||||
mcfgr2 = filtsda(1) | filtscl(1) | busidle(2400) // 100us timeout
|
||||
mcfgr3 = pinlow(i2cClockStretchTimeout*24/256 + 1)
|
||||
} else if freq >= TWI_FREQ_400KHZ {
|
||||
} else if freq >= 400*KHz {
|
||||
// I2C FastMode 400 kHz
|
||||
mcfgr2 = filtsda(2) | filtscl(2) | busidle(3600) // 150us timeout
|
||||
mcfgr3 = pinlow(i2cClockStretchTimeout*24/256 + 1)
|
||||
|
|
|
@ -230,7 +230,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
|||
|
||||
// Default I2C bus speed is 100 kHz.
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
config.Frequency = 100 * KHz
|
||||
}
|
||||
// Default I2C pins if not set.
|
||||
if config.SDA == 0 && config.SCL == 0 {
|
||||
|
@ -253,7 +253,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
|||
(nrf.GPIO_PIN_CNF_DRIVE_S0D1 << nrf.GPIO_PIN_CNF_DRIVE_Pos) |
|
||||
(nrf.GPIO_PIN_CNF_SENSE_Disabled << nrf.GPIO_PIN_CNF_SENSE_Pos))
|
||||
|
||||
if config.Frequency == TWI_FREQ_400KHZ {
|
||||
if config.Frequency >= 400*KHz {
|
||||
i2c.Bus.FREQUENCY.Set(nrf.TWI_FREQUENCY_FREQUENCY_K400)
|
||||
} else {
|
||||
i2c.Bus.FREQUENCY.Set(nrf.TWI_FREQUENCY_FREQUENCY_K100)
|
||||
|
|
|
@ -10,11 +10,6 @@ import (
|
|||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
KHz = 1000
|
||||
MHz = 1000000
|
||||
)
|
||||
|
||||
func CPUFrequency() uint32 {
|
||||
return 125 * MHz
|
||||
}
|
||||
|
|
|
@ -137,7 +137,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
|
|||
|
||||
// default to 100 kHz (Sm, standard mode) if no frequency is set
|
||||
if config.Frequency == 0 {
|
||||
config.Frequency = TWI_FREQ_100KHZ
|
||||
config.Frequency = 100 * KHz
|
||||
}
|
||||
|
||||
// configure I2C input clock
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче