Skip to content

Commit

Permalink
drivers: eeprom: at2x: simplify cmd[] assignment in at25_read, `w…
Browse files Browse the repository at this point in the history
…rite`

Simplify offset assignment in `eeprom_at25_read` and `eeprom_at25_write`
by directly setting values in `cmd[]` instead of using an intermediary
`paddr` pointer.
This makes the code more compact and improves readability.

Signed-off-by: Pisit Sawangvonganan <[email protected]>
  • Loading branch information
ndrs-pst committed Oct 31, 2024
1 parent 4c68fd3 commit 35ca62b
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions drivers/eeprom/eeprom_at2x.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ static int eeprom_at25_read(const struct device *dev, off_t offset, void *buf,
const struct eeprom_at2x_config *config = dev->config;
size_t cmd_len = 1 + config->addr_width / 8;
uint8_t cmd[4] = { EEPROM_AT25_READ, 0, 0, 0 };
uint8_t *paddr;
int err;
const struct spi_buf tx_buf = {
.buf = cmd,
Expand Down Expand Up @@ -455,16 +454,15 @@ static int eeprom_at25_read(const struct device *dev, off_t offset, void *buf,
return -EINVAL;
}

paddr = &cmd[1];
switch (config->addr_width) {
case 24:
*paddr++ = offset >> 16;
cmd[1] = offset >> 16;
__fallthrough;
case 16:
*paddr++ = offset >> 8;
cmd[2] = offset >> 8;
__fallthrough;
case 8:
*paddr++ = offset;
cmd[3] = offset;
break;
default:
__ASSERT(0, "invalid address width");
Expand Down Expand Up @@ -507,7 +505,6 @@ static int eeprom_at25_write(const struct device *dev, off_t offset,
int count = eeprom_at2x_limit_write_count(dev, offset, len);
uint8_t cmd[4] = { EEPROM_AT25_WRITE, 0, 0, 0 };
size_t cmd_len = 1 + config->addr_width / 8;
uint8_t *paddr;
int err;
const struct spi_buf tx_bufs[2] = {
{
Expand All @@ -524,16 +521,15 @@ static int eeprom_at25_write(const struct device *dev, off_t offset,
.count = ARRAY_SIZE(tx_bufs),
};

paddr = &cmd[1];
switch (config->addr_width) {
case 24:
*paddr++ = offset >> 16;
cmd[1] = offset >> 16;
__fallthrough;
case 16:
*paddr++ = offset >> 8;
cmd[2] = offset >> 8;
__fallthrough;
case 8:
*paddr++ = offset;
cmd[3] = offset;
break;
default:
__ASSERT(0, "invalid address width");
Expand Down

0 comments on commit 35ca62b

Please sign in to comment.