Skip to content

Commit 7513fa2

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent bc946f3 commit 7513fa2

11 files changed

+24
-29
lines changed

src/machine/i2c.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
)
99

1010
// TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.
11+
//
12+
// Deprecated: use 100 * machine.KHz or 400 * machine.KHz instead.
1113
const (
1214
TWI_FREQ_100KHZ = 100000
1315
TWI_FREQ_400KHZ = 400000

src/machine/machine.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ var (
1818
// particular chip but instead runs in WebAssembly for example.
1919
const Device = deviceName
2020

21+
// Generic constants.
22+
const (
23+
KHz = 1000
24+
MHz = 1000_000
25+
GHz = 1000_000_000
26+
)
27+
2128
// PinMode sets the direction and pull mode of the pin. For example, PinOutput
2229
// sets the pin as an output and PinInputPullup sets the pin as an input with a
2330
// pull-up.

src/machine/machine_atmega.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type I2CConfig struct {
2626
func (i2c *I2C) Configure(config I2CConfig) error {
2727
// Default I2C bus speed is 100 kHz.
2828
if config.Frequency == 0 {
29-
config.Frequency = TWI_FREQ_100KHZ
29+
config.Frequency = 100 * KHz
3030
}
3131

3232
// Activate internal pullups for twi.

src/machine/machine_atsamd21.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ const i2cTimeout = 1000
677677
func (i2c *I2C) Configure(config I2CConfig) error {
678678
// Default I2C bus speed is 100 kHz.
679679
if config.Frequency == 0 {
680-
config.Frequency = TWI_FREQ_100KHZ
680+
config.Frequency = 100 * KHz
681681
}
682682
if config.SDA == 0 && config.SCL == 0 {
683683
config.SDA = SDA_PIN

src/machine/machine_atsamd51.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ const i2cTimeout = 1000
11511151
func (i2c *I2C) Configure(config I2CConfig) error {
11521152
// Default I2C bus speed is 100 kHz.
11531153
if config.Frequency == 0 {
1154-
config.Frequency = TWI_FREQ_100KHZ
1154+
config.Frequency = 100 * KHz
11551155
}
11561156

11571157
// Use default I2C pins if not set.

src/machine/machine_fe310.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ type I2CConfig struct {
231231
func (i2c *I2C) Configure(config I2CConfig) error {
232232
var i2cClockFrequency uint32 = 32000000
233233
if config.Frequency == 0 {
234-
config.Frequency = TWI_FREQ_100KHZ
234+
config.Frequency = 100 * KHz
235235
}
236236

237237
if config.SDA == 0 && config.SCL == 0 {

src/machine/machine_k210.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ type I2CConfig struct {
523523
func (i2c *I2C) Configure(config I2CConfig) error {
524524

525525
if config.Frequency == 0 {
526-
config.Frequency = TWI_FREQ_100KHZ
526+
config.Frequency = 100 * KHz
527527
}
528528

529529
if config.SDA == 0 && config.SCL == 0 {

src/machine/machine_mimxrt1062_i2c.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@ import (
1010
"errors"
1111
)
1212

13-
const (
14-
TWI_FREQ_BUS = 24000000 // LPI2C root clock is on 24 MHz OSC
15-
TWI_FREQ_100KHZ = 100000 // StandardMode (100 kHz)
16-
TWI_FREQ_400KHZ = 400000 // FastMode (400 kHz)
17-
TWI_FREQ_1MHZ = 1000000 // FastModePlus (1 MHz)
18-
TWI_FREQ_5MHZ = 5000000 // UltraFastMode (5 MHz)
19-
TWI_FREQ_DEFAULT = TWI_FREQ_100KHZ // default to StandardMode (100 kHz)
20-
)
21-
2213
var (
2314
errI2CWriteTimeout = errors.New("I2C timeout during write")
2415
errI2CReadTimeout = errors.New("I2C timeout during read")
@@ -174,7 +165,7 @@ func (i2c *I2C) Configure(config I2CConfig) {
174165

175166
freq := config.Frequency
176167
if 0 == freq {
177-
freq = TWI_FREQ_DEFAULT
168+
freq = 100 * KHz
178169
}
179170

180171
// reset clock and registers, and enable LPI2C module interface
@@ -305,7 +296,7 @@ func (i2c *I2C) setFrequency(freq uint32) {
305296
wasEnabled := i2c.Bus.MCR.HasBits(nxp.LPI2C_MCR_MEN)
306297
i2c.Bus.MCR.ClearBits(nxp.LPI2C_MCR_MEN)
307298

308-
// baud rate = (TWI_FREQ_BUS/(2^pre))/(CLKLO+1 + CLKHI+1 + FLOOR((2+FILTSCL)/(2^pre)))
299+
// baud rate = (24MHz/(2^pre))/(CLKLO+1 + CLKHI+1 + FLOOR((2+FILTSCL)/(2^pre)))
309300
// assume: CLKLO=2*CLKHI, SETHOLD=CLKHI, DATAVD=CLKHI/2
310301
for pre := uint32(1); pre <= 128; pre *= 2 {
311302
if bestError == 0 {
@@ -314,9 +305,9 @@ func (i2c *I2C) setFrequency(freq uint32) {
314305
for clkHi := uint32(1); clkHi < 32; clkHi++ {
315306
var absError, rate uint32
316307
if clkHi == 1 {
317-
rate = (TWI_FREQ_BUS / pre) / (1 + 3 + 2 + 2/pre)
308+
rate = (24 * MHz / pre) / (1 + 3 + 2 + 2/pre)
318309
} else {
319-
rate = (TWI_FREQ_BUS / pre) / (3*clkHi + 2 + 2/pre)
310+
rate = (24 * MHz / pre) / (3*clkHi + 2 + 2/pre)
320311
}
321312
if freq > rate {
322313
absError = freq - rate
@@ -370,15 +361,15 @@ func (i2c *I2C) setFrequency(freq uint32) {
370361
mcfgr2, mcfgr3 uint32
371362
)
372363
const i2cClockStretchTimeout = 15000 // microseconds
373-
if freq >= TWI_FREQ_5MHZ {
364+
if freq >= 5*MHz {
374365
// I2C UltraFastMode 5 MHz
375366
mcfgr2 = 0 // disable glitch filters and timeout for UltraFastMode
376367
mcfgr3 = 0 //
377-
} else if freq >= TWI_FREQ_1MHZ {
368+
} else if freq >= 1*MHz {
378369
// I2C FastModePlus 1 MHz
379370
mcfgr2 = filtsda(1) | filtscl(1) | busidle(2400) // 100us timeout
380371
mcfgr3 = pinlow(i2cClockStretchTimeout*24/256 + 1)
381-
} else if freq >= TWI_FREQ_400KHZ {
372+
} else if freq >= 400*KHz {
382373
// I2C FastMode 400 kHz
383374
mcfgr2 = filtsda(2) | filtscl(2) | busidle(3600) // 150us timeout
384375
mcfgr3 = pinlow(i2cClockStretchTimeout*24/256 + 1)

src/machine/machine_nrf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
230230

231231
// Default I2C bus speed is 100 kHz.
232232
if config.Frequency == 0 {
233-
config.Frequency = TWI_FREQ_100KHZ
233+
config.Frequency = 100 * KHz
234234
}
235235
// Default I2C pins if not set.
236236
if config.SDA == 0 && config.SCL == 0 {
@@ -253,7 +253,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
253253
(nrf.GPIO_PIN_CNF_DRIVE_S0D1 << nrf.GPIO_PIN_CNF_DRIVE_Pos) |
254254
(nrf.GPIO_PIN_CNF_SENSE_Disabled << nrf.GPIO_PIN_CNF_SENSE_Pos))
255255

256-
if config.Frequency == TWI_FREQ_400KHZ {
256+
if config.Frequency >= 400*KHz {
257257
i2c.Bus.FREQUENCY.Set(nrf.TWI_FREQUENCY_FREQUENCY_K400)
258258
} else {
259259
i2c.Bus.FREQUENCY.Set(nrf.TWI_FREQUENCY_FREQUENCY_K100)

src/machine/machine_rp2040_clocks.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ import (
1010
"unsafe"
1111
)
1212

13-
const (
14-
KHz = 1000
15-
MHz = 1000000
16-
)
17-
1813
func CPUFrequency() uint32 {
1914
return 125 * MHz
2015
}

0 commit comments

Comments
 (0)