Skip to content

Commit

Permalink
Merge pull request RIOT-OS#20298 from chrysn-pull-requests/nrf52-spi-…
Browse files Browse the repository at this point in the history
…bugs

cpu/nrf52 i2c: Always buffer writes
  • Loading branch information
chrysn authored Jan 25, 2024
2 parents 2d45915 + 52a976e commit 1073df2
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion cpu/nrf5x_common/periph/i2c_nrf52_nrf9160.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ static inline NRF_TWIM_Type *bus(i2c_t dev)
return i2c_config[dev].dev;
}

/**
* @brief Like i2c_write_bytes, but with the constraint (created by the
* hardware) that data is in RAM
*/
static int direct_i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data,
size_t len,
uint8_t flags);

/**
* Block until the interrupt described by inten_success_flag or
* TWIM_INTEN_ERROR_Msk fires.
Expand Down Expand Up @@ -262,7 +270,7 @@ int i2c_write_regs(i2c_t dev, uint16_t addr, uint16_t reg,
}

memcpy(&tx_buf[reg_addr_len], data, len);
int ret = i2c_write_bytes(dev, addr, tx_buf, reg_addr_len + len, flags);
int ret = direct_i2c_write_bytes(dev, addr, tx_buf, reg_addr_len + len, flags);

/* Release tx_buf */
mutex_unlock(&buffer_lock);
Expand Down Expand Up @@ -331,6 +339,31 @@ int i2c_read_regs(i2c_t dev, uint16_t addr, uint16_t reg,

int i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data, size_t len,
uint8_t flags)
{
if ((unsigned int)data >= CPU_RAM_BASE && (unsigned int)data < CPU_RAM_BASE + CPU_RAM_SIZE) {
return direct_i2c_write_bytes(dev, addr, data, len, flags);
}

/* These are critical for the memcpy; direct_i2c_write_bytes makes some
* more */
assert((len > 0) && (len < 256));

/* Lock tx_buf */
mutex_lock(&buffer_lock);

memcpy(tx_buf, data, len);

int result = direct_i2c_write_bytes(dev, addr, tx_buf, len, flags);

/* Release tx_buf */
mutex_unlock(&buffer_lock);

return result;
}

int direct_i2c_write_bytes(i2c_t dev, uint16_t addr, const void *data,
size_t len,
uint8_t flags)
{
assert((dev < I2C_NUMOF) && data && (len > 0) && (len < 256));

Expand Down

0 comments on commit 1073df2

Please sign in to comment.