Skip to content

Commit ee63332

Browse files
feat(PeriphDrivers,CMSIS): Add support for multiple I2C target addresses (#725)
1 parent 9edb3e3 commit ee63332

File tree

22 files changed

+1932
-357
lines changed

22 files changed

+1932
-357
lines changed

Libraries/CMSIS/Device/Maxim/MAX32660/Include/max32660.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ typedef enum {
261261
/* I2C */
262262
#define MXC_I2C_INSTANCES (2)
263263
#define MXC_I2C_FIFO_DEPTH (8)
264+
#define MXC_I2C_NUM_TARGET_ADDR (4)
264265

265266
#define MXC_BASE_I2C0 ((uint32_t)0x4001D000UL)
266267
#define MXC_I2C0 ((mxc_i2c_regs_t *)MXC_BASE_I2C0)

Libraries/CMSIS/Device/Maxim/MAX32670/Include/max32670.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ typedef enum {
371371
/* I2C */
372372
#define MXC_I2C_INSTANCES (3)
373373
#define MXC_I2C_FIFO_DEPTH (8)
374+
#define MXC_I2C_NUM_TARGET_ADDR (4)
374375

375376
#define MXC_BASE_I2C0 ((uint32_t)0x4001D000UL)
376377
#define MXC_I2C0 ((mxc_i2c_regs_t *)MXC_BASE_I2C0)

Libraries/CMSIS/Device/Maxim/MAX32675/Include/max32675.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ typedef enum {
370370
/* I2C */
371371
#define MXC_I2C_INSTANCES (3)
372372
#define MXC_I2C_FIFO_DEPTH (8)
373+
#define MXC_I2C_NUM_TARGET_ADDR (4)
373374

374375
#define MXC_BASE_I2C0 ((uint32_t)0x4001D000UL)
375376
#define MXC_I2C0 ((mxc_i2c_regs_t *)MXC_BASE_I2C0)

Libraries/PeriphDrivers/Include/MAX32672/i2c.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ typedef int (*mxc_i2c_slave_handler_t)(mxc_i2c_regs_t *i2c, mxc_i2c_slave_event_
189189
* @param masterMode Whether to put the device in master or slave mode. Use
190190
* non-zero
191191
* for master mode, and zero for slave mode.
192-
* @param slaveAddr 7-bit or 10-bit address to use when in slave mode.
192+
* @param slaveAddr 7-bit or 10-bit address to use when in slave mode. By
193+
* default this value will be set to slave index 0. Use
194+
* MXC_I2C_SetSlaveAddr() to set additional slave addresses
195+
* if needed.
193196
* This parameter is ignored when masterMode is non-zero.
194197
*
195198
* @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
@@ -198,8 +201,6 @@ int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr);
198201

199202
/**
200203
* @brief Initialize and enable I2C peripheral.
201-
* @note Set idx to 0, multiple I2C instances acting as slaves is not yet
202-
* supported.
203204
*
204205
* @param i2c Pointer to I2C registers (selects the I2C block used.)
205206
* @param slaveAddr 7-bit or 10-bit address to use when in slave mode.

Libraries/PeriphDrivers/Include/MAX32672/mxc_sys.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ static inline int MXC_SYS_In_Crit_Section(void)
284284
*/
285285
int MXC_SYS_GetUSN(uint8_t *usn, uint8_t *checksum);
286286

287+
/**
288+
* @brief Gets design revision of the chip.
289+
*
290+
* @return Design revision.
291+
*/
292+
int MXC_SYS_GetRevision(void);
293+
287294
/**
288295
* @brief Determines if the selected peripheral clock is enabled.
289296
* @param clock Enumeration for desired clock.

Libraries/PeriphDrivers/Source/I2C/i2c_ai87.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr)
7878

7979
int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
8080
{
81+
if (idx != 0) {
82+
// MAX78002 does not support multiple slave addresses
83+
return E_NOT_SUPPORTED;
84+
}
85+
8186
return MXC_I2C_RevA_SetSlaveAddr((mxc_i2c_reva_regs_t *)i2c, slaveAddr, idx);
8287
}
8388

Libraries/PeriphDrivers/Source/I2C/i2c_es17.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
#define MXC_I2C_FASTPLUS_SPEED 1000000
4949

5050
/* **** Variable Declaration **** */
51-
uint32_t interruptCheck = MXC_F_I2C_INT0_RDAMI | MXC_F_I2C_INT0_WRAMI | MXC_F_I2C_INT0_DNRERI;
51+
uint32_t interruptCheck = MXC_F_I2C_INT0_RDAMI | MXC_F_I2C_INT0_WRAMI | MXC_F_I2C_INT0_AMI |
52+
MXC_F_I2C_INT0_DNRERI;
5253

5354
/* **** Function Prototypes **** */
5455

Libraries/PeriphDrivers/Source/I2C/i2c_me10.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#define MXC_I2C_MAX_ADDR_WIDTH 0x7F
5454
#define MXC_I2C_STD_MODE 100000
5555
#define MXC_I2C_FAST_SPEED 400000
56+
#define MXC_I2C_FASTPLUS_SPEED 1000000
5657

5758
/* **** Variable Declaration **** */
5859

@@ -125,7 +126,7 @@ int MXC_I2C_Shutdown(mxc_i2c_regs_t *i2c)
125126
/* ************************************************************************** */
126127
int MXC_I2C_SetFrequency(mxc_i2c_regs_t *i2c, unsigned int hz)
127128
{
128-
if (hz > MXC_I2C_FAST_SPEED) {
129+
if (hz > MXC_I2C_FASTPLUS_SPEED) {
129130
return E_NOT_SUPPORTED;
130131
}
131132

Libraries/PeriphDrivers/Source/I2C/i2c_me11.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
#include "i2c.h"
4646
#include "i2c_reva.h"
4747

48+
/* **** Definitions **** */
49+
#define MXC_I2C_MAX_ADDR_WIDTH 0x7F
50+
4851
/* **** Variable Declaration **** */
4952
uint32_t interruptCheck = MXC_F_I2C_INTFL0_AMI | MXC_F_I2C_INTFL0_DNRERI;
5053

@@ -80,8 +83,7 @@ int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
8083
return E_NULL_PTR;
8184
}
8285

83-
if (idx != 0) {
84-
// Multiple slaves are not supported yet
86+
if (idx >= MXC_I2C_NUM_TARGET_ADDR) {
8587
return E_NOT_SUPPORTED;
8688
}
8789

@@ -90,14 +92,22 @@ int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
9092
return E_BAD_PARAM;
9193
}
9294

93-
i2c->sladdr = 0;
95+
// Set the slave address to operate on
96+
MXC_SETFIELD(i2c->sladdr, MXC_F_I2C_SLADDR_SLAIDX, (idx << MXC_F_I2C_SLADDR_SLAIDX_POS));
9497

95-
if (slaveAddr > MXC_I2C_REVA_MAX_ADDR_WIDTH) {
98+
if (slaveAddr > MXC_I2C_MAX_ADDR_WIDTH) {
9699
// Set for 10bit addressing mode
97-
i2c->sladdr = MXC_F_I2C_SLADDR_EA;
100+
i2c->sladdr |= MXC_F_I2C_SLADDR_EA;
101+
} else {
102+
// Clear for 7bit addressing mode
103+
i2c->sladdr &= ~MXC_F_I2C_SLADDR_EA;
98104
}
99105

100-
i2c->sladdr |= slaveAddr;
106+
// Set the slave address
107+
MXC_SETFIELD(i2c->sladdr, MXC_F_I2C_SLADDR_SLA, (slaveAddr << MXC_F_I2C_SLADDR_SLA_POS));
108+
109+
// Enable the slave address
110+
i2c->sladdr &= ~MXC_F_I2C_SLADDR_SLADIS;
101111

102112
return E_NO_ERROR;
103113
}

Libraries/PeriphDrivers/Source/I2C/i2c_me12.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,6 @@ int MXC_I2C_Reset(mxc_i2c_regs_t *i2c)
117117

118118
int MXC_I2C_SetFrequency(mxc_i2c_regs_t *i2c, unsigned int hz)
119119
{
120-
// ME17 doesn't support high speed more
121-
if (hz > MXC_I2C_FASTPLUS_SPEED) {
122-
return E_NOT_SUPPORTED;
123-
}
124-
125120
return MXC_I2C_RevA_SetFrequency((mxc_i2c_reva_regs_t *)i2c, hz);
126121
}
127122

Libraries/PeriphDrivers/Source/I2C/i2c_me13.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
/* **** Variable Declaration **** */
5151
uint32_t interruptCheck = MXC_F_I2C_INT_FL0_RD_ADDR_MATCH | MXC_F_I2C_INT_FL0_WR_ADDR_MATCH |
52-
MXC_F_I2C_INT_FL0_DO_NOT_RESP_ER;
52+
MXC_F_I2C_INT_FL0_ADDR_MATCH | MXC_F_I2C_INT_FL0_DO_NOT_RESP_ER;
5353

5454
/* **** Function Prototypes **** */
5555

@@ -82,6 +82,11 @@ int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr)
8282

8383
int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
8484
{
85+
if ((MXC_SYS_GetRev() & 0xF0) == 0xA0 && idx != 0) {
86+
// MAX32570 Rev. A only supports one slave address
87+
return E_NOT_SUPPORTED;
88+
}
89+
8590
return MXC_I2C_RevA_SetSlaveAddr((mxc_i2c_reva_regs_t *)i2c, slaveAddr, idx);
8691
}
8792

Libraries/PeriphDrivers/Source/I2C/i2c_me14.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
/* **** Variable Declaration **** */
4949
uint32_t interruptCheck = MXC_F_I2C_INT_FL0_RD_ADDR_MATCH | MXC_F_I2C_INT_FL0_WR_ADDR_MATCH |
50-
MXC_F_I2C_INT_FL0_DO_NOT_RESP_ER;
50+
MXC_F_I2C_INT_FL0_ADDR_MATCH | MXC_F_I2C_INT_FL0_DO_NOT_RESP_ER;
5151

5252
/* **** Function Prototypes **** */
5353

@@ -80,6 +80,11 @@ int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr)
8080

8181
int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
8282
{
83+
if (idx != 0) {
84+
// MAX32665 only supports one slave address
85+
return E_NOT_SUPPORTED;
86+
}
87+
8388
return MXC_I2C_RevA_SetSlaveAddr((mxc_i2c_reva_regs_t *)i2c, slaveAddr, idx);
8489
}
8590

Libraries/PeriphDrivers/Source/I2C/i2c_me15.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
#include "i2c.h"
4646
#include "i2c_reva.h"
4747

48+
/* **** Definitions **** */
49+
#define MXC_I2C_MAX_ADDR_WIDTH 0x7F
50+
4851
/* **** Variable Declaration **** */
4952
uint32_t interruptCheck = MXC_F_I2C_INTFL0_ADDR_MATCH | MXC_F_I2C_INTFL0_DNR_ERR;
5053

@@ -85,8 +88,7 @@ int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
8588
return E_NULL_PTR;
8689
}
8790

88-
if (idx != 0) {
89-
// Multiple slaves are not supported yet
91+
if (idx >= MXC_I2C_NUM_TARGET_ADDR) {
9092
return E_NOT_SUPPORTED;
9193
}
9294

@@ -95,14 +97,22 @@ int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
9597
return E_BAD_PARAM;
9698
}
9799

98-
i2c->slave = 0;
100+
// Set the slave address to operate on
101+
MXC_SETFIELD(i2c->slave, MXC_F_I2C_SLAVE_IDX, (idx << MXC_F_I2C_SLAVE_IDX_POS));
99102

100-
if (slaveAddr > MXC_I2C_REVA_MAX_ADDR_WIDTH) {
103+
if (slaveAddr > MXC_I2C_MAX_ADDR_WIDTH) {
101104
// Set for 10bit addressing mode
102-
i2c->slave = MXC_F_I2C_SLAVE_EXT_ADDR_EN;
105+
i2c->slave |= MXC_F_I2C_SLAVE_EXT_ADDR_EN;
106+
} else {
107+
// Clear for 7bit addressing mode
108+
i2c->slave &= ~MXC_F_I2C_SLAVE_EXT_ADDR_EN;
103109
}
104110

105-
i2c->slave |= slaveAddr;
111+
// Set the slave address
112+
MXC_SETFIELD(i2c->slave, MXC_F_I2C_SLAVE_ADDR, (slaveAddr << MXC_F_I2C_SLAVE_ADDR_POS));
113+
114+
// Enable the slave address
115+
i2c->slave &= ~MXC_F_I2C_SLAVE_DIS;
106116

107117
return E_NO_ERROR;
108118
}

Libraries/PeriphDrivers/Source/I2C/i2c_me16.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,37 @@ int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr)
7878

7979
int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
8080
{
81-
return MXC_I2C_RevA_SetSlaveAddr(i2c, slaveAddr, idx);
81+
if (i2c == NULL) {
82+
return E_NULL_PTR;
83+
}
84+
85+
if (idx >= MXC_I2C_NUM_TARGET_ADDR) {
86+
return E_NOT_SUPPORTED;
87+
}
88+
89+
if (slaveAddr > MXC_F_I2C_SLAVE_ADDR) {
90+
// Only support addresses up to 10 bits
91+
return E_BAD_PARAM;
92+
}
93+
94+
// Set the slave address to operate on
95+
MXC_SETFIELD(i2c->slave, MXC_F_I2C_SLAVE_IDX, (idx << MXC_F_I2C_SLAVE_IDX_POS));
96+
97+
if (slaveAddr > MXC_I2C_MAX_ADDR_WIDTH) {
98+
// Set for 10bit addressing mode
99+
i2c->slave |= MXC_F_I2C_SLAVE_EXT_ADDR_EN;
100+
} else {
101+
// Clear for 7bit addressing mode
102+
i2c->slave &= ~MXC_F_I2C_SLAVE_EXT_ADDR_EN;
103+
}
104+
105+
// Set the slave address
106+
MXC_SETFIELD(i2c->slave, MXC_F_I2C_SLAVE_ADDR, (slaveAddr << MXC_F_I2C_SLAVE_ADDR_POS));
107+
108+
// Enable the slave address
109+
i2c->slave &= ~MXC_F_I2C_SLAVE_DIS;
110+
111+
return E_NO_ERROR;
82112
}
83113

84114
int MXC_I2C_Shutdown(mxc_i2c_regs_t *i2c)

Libraries/PeriphDrivers/Source/I2C/i2c_me17.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr)
7878

7979
int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
8080
{
81+
if (idx != 0) {
82+
// MAX32655 only supports one slave device
83+
return E_NOT_SUPPORTED;
84+
}
85+
8186
return MXC_I2C_RevA_SetSlaveAddr((mxc_i2c_reva_regs_t *)i2c, slaveAddr, idx);
8287
}
8388

Libraries/PeriphDrivers/Source/I2C/i2c_me18.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr)
7878

7979
int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
8080
{
81+
if (idx != 0) {
82+
// MAX32690 does not support multiple slave addresses
83+
return E_NOT_SUPPORTED;
84+
}
85+
8186
return MXC_I2C_RevA_SetSlaveAddr((mxc_i2c_reva_regs_t *)i2c, slaveAddr, idx);
8287
}
8388

0 commit comments

Comments
 (0)