Skip to content

Commit f171d9e

Browse files
committed
address remaining review comments
1 parent 4d64ee6 commit f171d9e

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

src/machine/board_teensy40.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,35 +352,41 @@ var (
352352
I2C1 = &_I2C1
353353
_I2C1 = I2C{
354354
Bus: nxp.LPI2C1,
355-
muxSDA: muxSelect{ // D18 (PA17 [AD_B1_01])
355+
sda: I2C1_SDA_PIN, // D18 (PA17 [AD_B1_01])
356+
scl: I2C1_SCL_PIN, // D19 (PA16 [AD_B1_00])
357+
muxSDA: muxSelect{
356358
mux: nxp.IOMUXC_LPI2C1_SDA_SELECT_INPUT_DAISY_GPIO_AD_B1_01_ALT3,
357359
sel: &nxp.IOMUXC.LPI2C1_SDA_SELECT_INPUT,
358360
},
359-
muxSCL: muxSelect{ // D19 (PA16 [AD_B1_00])
361+
muxSCL: muxSelect{
360362
mux: nxp.IOMUXC_LPI2C1_SCL_SELECT_INPUT_DAISY_GPIO_AD_B1_00_ALT3,
361363
sel: &nxp.IOMUXC.LPI2C1_SCL_SELECT_INPUT,
362364
},
363365
}
364366
I2C2 = &_I2C2
365367
_I2C2 = I2C{
366368
Bus: nxp.LPI2C3,
367-
muxSDA: muxSelect{ // D17 (PA22 [AD_B1_06])
369+
sda: I2C2_SDA_PIN, // D17 (PA22 [AD_B1_06])
370+
scl: I2C2_SCL_PIN, // D16 (PA23 [AD_B1_07])
371+
muxSDA: muxSelect{
368372
mux: nxp.IOMUXC_LPI2C3_SDA_SELECT_INPUT_DAISY_GPIO_AD_B1_06_ALT1,
369373
sel: &nxp.IOMUXC.LPI2C3_SDA_SELECT_INPUT,
370374
},
371-
muxSCL: muxSelect{ // D16 (PA23 [AD_B1_07])
375+
muxSCL: muxSelect{
372376
mux: nxp.IOMUXC_LPI2C3_SCL_SELECT_INPUT_DAISY_GPIO_AD_B1_07_ALT1,
373377
sel: &nxp.IOMUXC.LPI2C3_SCL_SELECT_INPUT,
374378
},
375379
}
376380
I2C3 = &_I2C3
377381
_I2C3 = I2C{
378382
Bus: nxp.LPI2C4,
379-
muxSDA: muxSelect{ // D25 (PA13 [AD_B0_13])
383+
sda: I2C3_SDA_PIN, // D25 (PA13 [AD_B0_13])
384+
scl: I2C3_SCL_PIN, // D24 (PA12 [AD_B0_12])
385+
muxSDA: muxSelect{
380386
mux: nxp.IOMUXC_LPI2C4_SDA_SELECT_INPUT_DAISY_GPIO_AD_B0_13_ALT0,
381387
sel: &nxp.IOMUXC.LPI2C4_SDA_SELECT_INPUT,
382388
},
383-
muxSCL: muxSelect{ // D24 (PA12 [AD_B0_12])
389+
muxSCL: muxSelect{
384390
mux: nxp.IOMUXC_LPI2C4_SCL_SELECT_INPUT_DAISY_GPIO_AD_B0_12_ALT0,
385391
sel: &nxp.IOMUXC.LPI2C4_SCL_SELECT_INPUT,
386392
},

src/machine/machine_mimxrt1062_i2c.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build mimxrt1062
12
// +build mimxrt1062
23

34
package machine
@@ -37,17 +38,15 @@ type I2CConfig struct {
3738
SCL Pin
3839
}
3940

40-
func (c I2CConfig) getPins() (sda, scl Pin) {
41-
if 0 == c.SDA && 0 == c.SCL {
42-
// default pins if none specified
43-
return I2C_SDA_PIN, I2C_SCL_PIN
44-
}
45-
return c.SDA, c.SCL
46-
}
47-
4841
type I2C struct {
4942
Bus *nxp.LPI2C_Type
5043

44+
// these pins are initialized by each global I2C variable declared in the
45+
// board_teensy4x.go file according to the board manufacturer's default pin
46+
// mapping. they can be overridden with the I2CConfig argument given to
47+
// (*I2C) Configure(I2CConfig).
48+
sda, scl Pin
49+
5150
// these hold the input selector ("daisy chain") values that select which pins
5251
// are connected to the LPI2C device, and should be defined where the I2C
5352
// instance is declared (e.g., in the board definition). see the godoc
@@ -150,11 +149,20 @@ const (
150149
stateWaitForCompletion stateFlag = 0x5
151150
)
152151

152+
func (i2c *I2C) setPins(c I2CConfig) (sda, scl Pin) {
153+
// if both given pins are defined, or either receiver pin is undefined.
154+
if 0 != c.SDA && 0 != c.SCL || 0 == i2c.sda || 0 == i2c.scl {
155+
// override the receiver's pins.
156+
i2c.sda, i2c.scl = c.SDA, c.SCL
157+
}
158+
// return the selected pins.
159+
return i2c.sda, i2c.scl
160+
}
161+
153162
// Configure is intended to setup an I2C interface for transmit/receive.
154163
func (i2c *I2C) Configure(config I2CConfig) {
155-
156164
// init pins
157-
sda, scl := config.getPins()
165+
sda, scl := i2c.setPins(config)
158166

159167
// configure the mux and pad control registers
160168
sda.Configure(PinConfig{Mode: PinModeI2CSDA})
@@ -174,7 +182,6 @@ func (i2c *I2C) Configure(config I2CConfig) {
174182
}
175183

176184
func (i2c I2C) Tx(addr uint16, w, r []byte) error {
177-
178185
// perform transmit transfer
179186
if nil != w {
180187
// generate start condition on bus
@@ -281,6 +288,10 @@ func (i2c *I2C) reset(freq uint32) {
281288

282289
// clear reset, and enable the interface
283290
i2c.Bus.MCR.Set(nxp.LPI2C_MCR_MEN)
291+
292+
// wait for the I2C bus to idle
293+
for i2c.Bus.MSR.Get()&nxp.LPI2C_MSR_BBF != 0 {
294+
}
284295
}
285296

286297
func (i2c *I2C) setFrequency(freq uint32) {

src/runtime/runtime_mimxrt1062_clock.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func initClocks() {
166166
nxp.ClockIpLpi2c1.Enable(false) // disable LPI2C
167167
nxp.ClockIpLpi2c2.Enable(false) //
168168
nxp.ClockIpLpi2c3.Enable(false) //
169+
nxp.ClockIpLpi2c4.Enable(false) //
169170
nxp.DivIpLpi2c.Div(0) // divide LPI2C_CLK_PODF (DIV1)
170171
nxp.MuxIpLpi2c.Mux(1) // LPI2C select OSC
171172

@@ -266,7 +267,6 @@ func initClocks() {
266267
}
267268

268269
func enableTimerClocks() {
269-
270270
nxp.ClockIpGpt1.Enable(true) // enable GPT/PIT
271271
nxp.ClockIpGpt1S.Enable(true) //
272272
nxp.ClockIpGpt2.Enable(true) //
@@ -275,7 +275,6 @@ func enableTimerClocks() {
275275
}
276276

277277
func enablePinClocks() {
278-
279278
nxp.ClockIpIomuxcGpr.Enable(true) // enable IOMUXC
280279
nxp.ClockIpIomuxc.Enable(true) //
281280

@@ -286,7 +285,6 @@ func enablePinClocks() {
286285
}
287286

288287
func enablePeripheralClocks() {
289-
290288
nxp.ClockIpAdc1.Enable(true) // enable ADC
291289
nxp.ClockIpAdc2.Enable(true) //
292290

@@ -309,6 +307,7 @@ func enablePeripheralClocks() {
309307
nxp.ClockIpLpi2c1.Enable(true) // enable LPI2C
310308
nxp.ClockIpLpi2c2.Enable(true) //
311309
nxp.ClockIpLpi2c3.Enable(true) //
310+
nxp.ClockIpLpi2c4.Enable(true) //
312311

313312
nxp.ClockIpCan1.Enable(true) // enable CAN
314313
nxp.ClockIpCan2.Enable(true) //

0 commit comments

Comments
 (0)