Skip to content

Commit

Permalink
Change lpc17 i2c driver to accept any size of command in MasterCmdRead
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavosr8 committed Feb 15, 2024
1 parent 3f659b9 commit 70b234f
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 25 deletions.
4 changes: 3 additions & 1 deletion modules/adn4604.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,15 @@ adn_connect_map_t adn4604_out_status( void )
{
uint8_t i2c_addr, i2c_interf;
uint8_t i;
uint8_t cmd;
adn_connect_map_t stat_map = {0};

if ( i2c_take_by_chipid( CHIP_ID_ADN, &i2c_addr, &i2c_interf, (TickType_t)10 ) ) {

/* Read all outputs status */
for ( i = 0; i < 8; i++ ) {
xI2CMasterWriteRead( i2c_interf, i2c_addr, ADN_XPT_STATUS_REG+i, (uint8_t *)(&stat_map)+i, 1 );
cmd = ADN_XPT_STATUS_REG+i;
xI2CMasterWriteRead( i2c_interf, i2c_addr, &cmd, 1, (uint8_t *)(&stat_map)+i, 1 );
}
i2c_give( i2c_interf );
}
Expand Down
12 changes: 5 additions & 7 deletions modules/at24mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ size_t at24mac_read( uint8_t id, uint16_t address, uint8_t *rx_data, size_t buf_
uint8_t i2c_interface;
uint8_t rx_len = 0;

if (rx_data == NULL) {
return 0;
}

if (i2c_take_by_chipid( id, &i2c_addr, &i2c_interface, timeout ) && ( rx_data != NULL ) ) {
rx_len = xI2CMasterWriteRead( i2c_interface, i2c_addr, address, rx_data, buf_len );
rx_len = xI2CMasterWriteRead( i2c_interface, i2c_addr, (uint8_t *)&address, 1, rx_data, buf_len );
i2c_give( i2c_interface );
}

Expand All @@ -60,9 +56,10 @@ size_t at24mac_read_serial_num( uint8_t id, uint8_t *rx_data, size_t buf_len, ui
uint8_t i2c_addr;
uint8_t i2c_interface;
uint8_t rx_len = 0;
const uint8_t cmd = AT24MAC_ID_ADDR;

if (i2c_take_by_chipid( id, &i2c_addr, &i2c_interface, timeout ) && ( rx_data != NULL ) ) {
rx_len = xI2CMasterWriteRead( i2c_interface, i2c_addr+8, AT24MAC_ID_ADDR, rx_data, buf_len);
rx_len = xI2CMasterWriteRead( i2c_interface, i2c_addr+8, &cmd, 1, rx_data, buf_len);

i2c_give( i2c_interface );
}
Expand All @@ -75,10 +72,11 @@ size_t at24mac_read_eui( uint8_t id, uint8_t *rx_data, size_t buf_len, uint32_t
uint8_t i2c_addr;
uint8_t i2c_interface;
uint8_t rx_len = 0;
const uint8_t cmd = AT24MAC_EUI_ADDR;

if (i2c_take_by_chipid( id, &i2c_addr, &i2c_interface, timeout ) && ( rx_data != NULL ) ) {

rx_len = xI2CMasterWriteRead( i2c_interface, i2c_addr+8, AT24MAC_EUI_ADDR, rx_data, buf_len);
rx_len = xI2CMasterWriteRead( i2c_interface, i2c_addr+8, &cmd, 1, rx_data, buf_len);

i2c_give( i2c_interface );
}
Expand Down
2 changes: 1 addition & 1 deletion modules/cdce906.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ int cdce906_read_cfg(uint8_t chip_id, cdce906_cfg* cfg)
int ret = 0;

if (i2c_take_by_chipid(chip_id, &i2c_addr, &i2c_id, (TickType_t)10)) {
i2c_trans = xI2CMasterWriteRead(i2c_id, i2c_addr, 0x00, data, sizeof(data));
i2c_trans = xI2CMasterWriteRead(i2c_id, i2c_addr, 0x00, 1, data, sizeof(data));
i2c_give(i2c_id);
}

Expand Down
2 changes: 1 addition & 1 deletion modules/eeprom_24xx02.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ size_t eeprom_24xx02_read( uint8_t id, uint16_t address, uint8_t *rx_data, size_
}

if (i2c_take_by_chipid( id, &i2c_addr, &i2c_interface, timeout ) ) {
rx_len = xI2CMasterWriteRead( i2c_interface, i2c_addr, address, rx_data, buf_len );
rx_len = xI2CMasterWriteRead( i2c_interface, i2c_addr, (uint8_t *)&address, 1, rx_data, buf_len );
i2c_give( i2c_interface );
}

Expand Down
2 changes: 1 addition & 1 deletion modules/eeprom_24xx64.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ size_t eeprom_24xx64_read( uint8_t id, uint16_t address, uint8_t *rx_data, size_
}

if (i2c_take_by_chipid( id, &i2c_addr, &i2c_interface, timeout ) ) {
rx_len = xI2CMasterWriteRead (i2c_interface, i2c_addr, addr8, rx_data, buf_len);
rx_len = xI2CMasterWriteRead (i2c_interface, i2c_addr, addr8, 2, rx_data, buf_len);
i2c_give( i2c_interface );
}

Expand Down
4 changes: 2 additions & 2 deletions modules/mcp23016.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static mmc_err mcp23016_read_reg ( uint8_t reg, uint8_t *readout )
}

if( i2c_take_by_chipid( CHIP_ID_MCP23016, &i2c_addr, &i2c_id, (TickType_t) 10) ) {
rx_len = xI2CMasterWriteRead(i2c_id, i2c_addr, reg, data, sizeof(data));
rx_len = xI2CMasterWriteRead(i2c_id, i2c_addr, &reg, 1, data, sizeof(data));
i2c_give(i2c_id);
} else {
return MMC_TIMEOUT_ERR;
Expand Down Expand Up @@ -177,7 +177,7 @@ mmc_err mcp23016_read_reg_pair ( uint8_t reg, uint16_t *readout ) {
uint8_t data[2] = {0};

if( i2c_take_by_chipid( CHIP_ID_MCP23016, &i2c_addr, &i2c_id, (TickType_t) 10) ) {
rx_len = xI2CMasterWriteRead(i2c_id, i2c_addr, reg, data, sizeof(data));
rx_len = xI2CMasterWriteRead(i2c_id, i2c_addr, &reg, 1, data, sizeof(data));
i2c_give(i2c_id);
} else {
return MMC_TIMEOUT_ERR;
Expand Down
2 changes: 1 addition & 1 deletion modules/pca9554.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static uint8_t pca9554_read_reg ( uint8_t chip_id, uint8_t reg, uint8_t *readout
}

if( i2c_take_by_chipid( chip_id, &i2c_addr, &i2c_id, (TickType_t) 10) ) {
rx_len = xI2CMasterWriteRead(i2c_id, i2c_addr, reg, readout, 1);
rx_len = xI2CMasterWriteRead(i2c_id, i2c_addr, &reg, 1, readout, 1);
i2c_give(i2c_id);
}
return rx_len;
Expand Down
2 changes: 1 addition & 1 deletion modules/sensors/ina220.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Bool ina220_readvalue( ina220_data_t * data, uint8_t reg, uint16_t *read )

if( i2c_take_by_chipid( data->sensor->chipid, &i2c_addr, &i2c_interf, portMAX_DELAY) == pdTRUE ) {

xI2CMasterWriteRead( i2c_interf, i2c_addr, reg, &val[0], sizeof(val)/sizeof(val[0]) );
xI2CMasterWriteRead( i2c_interf, i2c_addr, &reg, 1, &val[0], sizeof(val)/sizeof(val[0]) );

i2c_give( i2c_interf );

Expand Down
2 changes: 1 addition & 1 deletion modules/sensors/ina3221.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ uint8_t ina3221_read_reg( ina3221_data_t * data, uint8_t reg, uint16_t *read )

if( i2c_take_by_chipid( data->chipid, &i2c_addr, &i2c_interf, portMAX_DELAY) == pdTRUE ) {

rx_len = xI2CMasterWriteRead( i2c_interf, i2c_addr, reg, &val[0], sizeof(val)/sizeof(val[0]) );
rx_len = xI2CMasterWriteRead( i2c_interf, i2c_addr, reg, 1, &val[0], sizeof(val)/sizeof(val[0]) );

i2c_give( i2c_interf );

Expand Down
19 changes: 12 additions & 7 deletions modules/sensors/max6642.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ Bool max6642_read_local( sensor_t *sensor, uint8_t *temp )
{
uint8_t i2c_interf, i2c_addr;
uint8_t read = 0;

const uint8_t cmd = MAX6642_CMD_READ_LOCAL;
if ( i2c_take_by_chipid( sensor->chipid, &i2c_addr, &i2c_interf, portMAX_DELAY ) == pdTRUE ) {

xI2CMasterWriteRead( i2c_interf, i2c_addr, MAX6642_CMD_READ_LOCAL, &read, 1 );
xI2CMasterWriteRead( i2c_interf, i2c_addr, &cmd, 1, &read, 1 );
i2c_give( i2c_interf );

*temp = read;
Expand All @@ -100,10 +100,11 @@ Bool max6642_read_remote( sensor_t *sensor, uint8_t *temp )
{
uint8_t i2c_interf, i2c_addr;
uint8_t read = 0;
const uint8_t cmd = MAX6642_CMD_READ_REMOTE;

if ( i2c_take_by_chipid( sensor->chipid, &i2c_addr, &i2c_interf, portMAX_DELAY ) == pdTRUE ) {

xI2CMasterWriteRead( i2c_interf, i2c_addr, MAX6642_CMD_READ_REMOTE, &read, 1 );
xI2CMasterWriteRead( i2c_interf, i2c_addr, &cmd, 1, &read, 1 );
i2c_give( i2c_interf );

*temp = read;
Expand All @@ -117,10 +118,11 @@ Bool max6642_read_local_extd( sensor_t *sensor, uint8_t *temp )
{
uint8_t i2c_interf, i2c_addr;
uint8_t read = 0;
const uint8_t cmd = MAX6642_CMD_READ_LOCAL_EXTD;

if ( i2c_take_by_chipid( sensor->chipid, &i2c_addr, &i2c_interf, portMAX_DELAY ) == pdTRUE ) {

xI2CMasterWriteRead( i2c_interf, i2c_addr, MAX6642_CMD_READ_LOCAL_EXTD, &read, 1 );
xI2CMasterWriteRead( i2c_interf, i2c_addr, &cmd, 1, &read, 1 );
i2c_give( i2c_interf );

*temp = read;
Expand All @@ -134,10 +136,11 @@ Bool max6642_read_remote_extd( sensor_t *sensor, uint8_t *temp )
{
uint8_t i2c_interf, i2c_addr;
uint8_t read = 0;
const uint8_t cmd = MAX6642_CMD_READ_REMOTE_EXTD;

if ( i2c_take_by_chipid( sensor->chipid, &i2c_addr, &i2c_interf, portMAX_DELAY ) == pdTRUE ) {

xI2CMasterWriteRead( i2c_interf, i2c_addr, MAX6642_CMD_READ_REMOTE_EXTD, &read, 1 );
xI2CMasterWriteRead( i2c_interf, i2c_addr, &cmd, 1, &read, 1 );
i2c_give( i2c_interf );

*temp = read;
Expand All @@ -151,10 +154,11 @@ uint8_t max6642_read_status( sensor_t *sensor )
{
uint8_t i2c_interf, i2c_addr;
uint8_t stat = 0;
const uint8_t cmd = MAX6642_CMD_READ_STATUS;

if ( i2c_take_by_chipid( sensor->chipid, &i2c_addr, &i2c_interf, portMAX_DELAY ) == pdTRUE ) {

xI2CMasterWriteRead( i2c_interf, i2c_addr, MAX6642_CMD_READ_STATUS, &stat, 1 );
xI2CMasterWriteRead( i2c_interf, i2c_addr, &cmd, 1, &stat, 1 );
i2c_give( i2c_interf );
}

Expand All @@ -165,10 +169,11 @@ uint8_t max6642_read_cfg( sensor_t *sensor )
{
uint8_t i2c_interf, i2c_addr;
uint8_t cfg = 0;
const uint8_t cmd = MAX6642_CMD_READ_CONFIG;

if ( i2c_take_by_chipid( sensor->chipid, &i2c_addr, &i2c_interf, portMAX_DELAY) == pdTRUE ) {

xI2CMasterWriteRead( i2c_interf, i2c_addr, MAX6642_CMD_READ_CONFIG, &cfg, 1 );
xI2CMasterWriteRead( i2c_interf, i2c_addr, &cmd, 1, &cfg, 1 );
i2c_give( i2c_interf );
}

Expand Down
12 changes: 12 additions & 0 deletions port/ucontroller/nxp/lpc17xx/lpc17_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,15 @@ void vI2CSlaveSetup ( I2C_ID_T id, uint8_t slave_addr )
slave_cfg.rxSz = (sizeof(recv_msg)/sizeof(recv_msg[0]));
Chip_I2C_SlaveSetup( id, I2C_SLAVE_0, &slave_cfg, I2C_Slave_Event, SLAVE_MASK);
}

int xI2CMasterWriteRead(I2C_ID_T id, uint8_t addr, const uint8_t *tx_buff, int tx_len, uint8_t *rx_buff, int rx_len)
{
I2C_XFER_T xfer = {0};
xfer.slaveAddr = addr;
xfer.txBuff = tx_buff;
xfer.txSz = tx_len;
xfer.rxBuff = rx_buff;
xfer.rxSz = rx_len;
while (Chip_I2C_MasterTransfer(id, &xfer) == I2C_STATUS_ARBLOST) {}
return rx_len - xfer.rxSz;
}
3 changes: 1 addition & 2 deletions port/ucontroller/nxp/lpc17xx/lpc17_i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@

#define xI2CMasterWrite(id, addr, tx_buff, tx_len) Chip_I2C_MasterSend(id, addr, tx_buff, tx_len)
#define xI2CMasterRead(id, addr, rx_buff, rx_len) Chip_I2C_MasterRead(id, addr, rx_buff, rx_len)
#define xI2CMasterWriteRead(id, addr, cmd, rx_buff, rx_len) Chip_I2C_MasterCmdRead(id, addr, cmd, rx_buff, rx_len)

uint8_t xI2CSlaveReceive( I2C_ID_T id, uint8_t * rx_buff, uint8_t buff_len, uint32_t timeout );
void vI2CSlaveSetup ( I2C_ID_T id, uint8_t slave_addr );
void vI2CConfig( I2C_ID_T id, uint32_t speed );

int xI2CMasterWriteRead(I2C_ID_T id, uint8_t addr, const uint8_t *tx_buff, int tx_len, uint8_t *rx_buff, int rx_len);

0 comments on commit 70b234f

Please sign in to comment.