Skip to content

Commit 9d3ae41

Browse files
committed
ports: analog: Improve portability for BUSIO.I2C
Use register wrapper macros to improve portability for I2C. This includes macros for the CTRL, STATUS, INTFL0, and FIFO registers. Additionally, some field values for MST_MODE, BUSY, ACK, and NACK are used. Signed-off-by: Brandon-Hurst <brandon.hurst97@gmail.com>
1 parent def19d2 commit 9d3ae41

4 files changed

Lines changed: 41 additions & 8 deletions

File tree

ports/analog/common-hal/busio/I2C.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
124124
bool ret = 0;
125125

126126
// If not in Master mode, error out (can happen in some error conditions)
127-
if (!(self->i2c_regs->ctrl & MXC_F_I2C_CTRL_MST_MODE)) {
127+
if (!(I2C_CTRL_REG(self->i2c_regs) & I2C_F_CTRL_MST_MODE)) {
128128
return false;
129129
}
130130

@@ -135,29 +135,29 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
135135

136136
// Pre-load target address into transmit FIFO
137137
addr = (addr << 1);
138-
self->i2c_regs->fifo = addr;
138+
I2C_FIFO_REG(self->i2c_regs) = addr;
139139

140140
// Set start bit & wait for it to clear
141141
MXC_I2C_Start(self->i2c_regs);
142142

143143
// wait for ACK/NACK
144-
while (!(self->i2c_regs->intfl0 & MXC_F_I2C_INTFL0_ADDR_ACK) &&
145-
!(self->i2c_regs->intfl0 & MXC_F_I2C_INTFL0_ADDR_NACK_ERR)) {
144+
while (!(I2C_INTFL0_REG(self->i2c_regs) & I2C_F_INTFL0_ADDR_ACK) &&
145+
!(I2C_INTFL0_REG(self->i2c_regs) & I2C_F_INTFL0_ADDR_NACK_ERR)) {
146146
}
147147

148148
// Save interrupt flags for ACK/NACK checking
149-
int_fl0 = self->i2c_regs->intfl0;
149+
int_fl0 = I2C_INTFL0_REG(self->i2c_regs);
150150

151151
// Set / Wait for stop
152152
MXC_I2C_Stop(self->i2c_regs);
153153

154154
// Wait for controller not busy, then clear flags
155-
while (self->i2c_regs->status & MXC_F_I2C_STATUS_BUSY) {
155+
while (I2C_STATUS_REG(self->i2c_regs) & I2C_F_STATUS_BUSY) {
156156
;
157157
}
158158
MXC_I2C_ClearFlags(self->i2c_regs, 0xFFFFFF, 0xFFFFFF);
159159

160-
if (int_fl0 & MXC_F_I2C_INTFL0_ADDR_ACK) {
160+
if (int_fl0 & I2C_F_INTFL0_ADDR_ACK) {
161161
ret = true;
162162
} else {
163163
ret = false;
@@ -168,7 +168,7 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
168168
// Lock I2C bus
169169
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
170170

171-
if (self->i2c_regs->status & MXC_F_I2C_STATUS_BUSY) {
171+
if (I2C_STATUS_REG(self->i2c_regs) & I2C_F_STATUS_BUSY) {
172172
return false;
173173
} else {
174174
self->has_lock = true;

ports/analog/peripherals/max32650/max32_i2c.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313

1414
#define NUM_I2C 2
1515

16+
// I2C register compatibility macros for MAX32650
17+
#define I2C_CTRL_REG(i2c) ((i2c)->ctrl0)
18+
#define I2C_STATUS_REG(i2c) ((i2c)->stat)
19+
#define I2C_INTFL0_REG(i2c) ((i2c)->int_fl0)
20+
#define I2C_FIFO_REG(i2c) ((i2c)->fifo)
21+
22+
#define I2C_F_CTRL_MST_MODE MXC_F_I2C_CTRL0_MST
23+
#define I2C_F_STATUS_BUSY MXC_F_I2C_STAT_BUSY
24+
#define I2C_F_INTFL0_ADDR_ACK MXC_F_I2C_INT_FL0_ADRACKI
25+
#define I2C_F_INTFL0_ADDR_NACK_ERR MXC_F_I2C_INT_FL0_ADRERI
26+
1627
int pinsToI2c(const mcu_pin_obj_t *sda, const mcu_pin_obj_t *scl);

ports/analog/peripherals/max32665/max32_i2c.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313

1414
#define NUM_I2C 3
1515

16+
// I2C register compatibility macros for MAX32665
17+
#define I2C_CTRL_REG(i2c) ((i2c)->ctrl)
18+
#define I2C_STATUS_REG(i2c) ((i2c)->status)
19+
#define I2C_INTFL0_REG(i2c) ((i2c)->int_fl0)
20+
#define I2C_FIFO_REG(i2c) ((i2c)->fifo)
21+
22+
#define I2C_F_CTRL_MST_MODE MXC_F_I2C_CTRL_MST
23+
#define I2C_F_STATUS_BUSY MXC_F_I2C_STATUS_BUS
24+
#define I2C_F_INTFL0_ADDR_ACK MXC_F_I2C_INT_FL0_ADDR_ACK
25+
#define I2C_F_INTFL0_ADDR_NACK_ERR MXC_F_I2C_INT_FL0_ADDR_NACK_ER
26+
1627
int pinsToI2c(const mcu_pin_obj_t *sda, const mcu_pin_obj_t *scl);

ports/analog/peripherals/max32690/max32_i2c.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313

1414
#define NUM_I2C 3
1515

16+
// I2C register compatibility macros for MAX32690
17+
#define I2C_CTRL_REG(i2c) ((i2c)->ctrl)
18+
#define I2C_STATUS_REG(i2c) ((i2c)->status)
19+
#define I2C_INTFL0_REG(i2c) ((i2c)->intfl0)
20+
#define I2C_FIFO_REG(i2c) ((i2c)->fifo)
21+
22+
#define I2C_F_CTRL_MST_MODE MXC_F_I2C_CTRL_MST_MODE
23+
#define I2C_F_STATUS_BUSY MXC_F_I2C_STATUS_BUSY
24+
#define I2C_F_INTFL0_ADDR_ACK MXC_F_I2C_INTFL0_ADDR_ACK
25+
#define I2C_F_INTFL0_ADDR_NACK_ERR MXC_F_I2C_INTFL0_ADDR_NACK_ERR
26+
1627
int pinsToI2c(const mcu_pin_obj_t *sda, const mcu_pin_obj_t *scl);

0 commit comments

Comments
 (0)