Skip to content

Commit 5be6e2e

Browse files
drivers: i2s: nrf_tdm: Add guards for buffers size
TDM peripheral requires TXD.MAXCNT and RXD.MAXCNT registers to be: - multiple of 4 bytes - larger than 8 bytes Signed-off-by: Adam Kondraciuk <[email protected]>
1 parent 53eaf0f commit 5be6e2e

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

drivers/i2s/i2s_nrf_tdm.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ LOG_MODULE_REGISTER(tdm_nrf, CONFIG_I2S_LOG_LEVEL);
3232
*/
3333
#define NRFX_TDM_STATUS_TRANSFER_STOPPED BIT(1)
3434

35+
/* Due to hardware limitations, the TDM peripheral requires the rx/tx size
36+
* to be greater than 8 bytes.
37+
*/
38+
#define NRFX_TDM_MIN_TRANSFER_SIZE_ALLOWED 8
39+
3540
/* Maximum clock divider value. Corresponds to CKDIV2. */
3641
#define NRFX_TDM_MAX_SCK_DIV_VALUE TDM_CONFIG_SCK_DIV_SCKDIV_Max
3742
#define NRFX_TDM_MAX_MCK_DIV_VALUE TDM_CONFIG_MCK_DIV_DIV_Max
@@ -475,8 +480,10 @@ static int tdm_nrf_configure(const struct device *dev, enum i2s_dir dir,
475480

476481
__ASSERT_NO_MSG(tdm_cfg->mem_slab != NULL && tdm_cfg->block_size != 0);
477482

478-
if ((tdm_cfg->block_size % sizeof(uint32_t)) != 0) {
479-
LOG_ERR("This device can transfer only full 32-bit words");
483+
if ((tdm_cfg->block_size % sizeof(uint32_t)) != 0 ||
484+
tdm_cfg->block_size <= NRFX_TDM_MIN_TRANSFER_SIZE_ALLOWED) {
485+
LOG_ERR("This device can only transmit full 32-bit words greater than %u bytes.",
486+
NRFX_TDM_MIN_TRANSFER_SIZE_ALLOWED);
480487
return -EINVAL;
481488
}
482489

@@ -673,11 +680,18 @@ static int tdm_nrf_write(const struct device *dev, void *mem_block, size_t size)
673680
return -EIO;
674681
}
675682

676-
if (size > drv_data->tx.cfg.block_size || size < sizeof(uint32_t)) {
683+
if (size > drv_data->tx.cfg.block_size) {
677684
LOG_ERR("This device can only write blocks up to %u bytes",
678685
drv_data->tx.cfg.block_size);
679686
return -EIO;
680687
}
688+
689+
if ((size % sizeof(uint32_t)) != 0 || size <= NRFX_TDM_MIN_TRANSFER_SIZE_ALLOWED) {
690+
LOG_ERR("This device can only write full 32-bit words greater than %u bytes.",
691+
NRFX_TDM_MIN_TRANSFER_SIZE_ALLOWED);
692+
return -EIO;
693+
}
694+
681695
ret = dmm_buffer_out_prepare(drv_cfg->mem_reg, buf.mem_block, buf.size,
682696
(void **)&buf.dmm_buf);
683697
ret = k_msgq_put(&drv_data->tx_queue, &buf, SYS_TIMEOUT_MS(drv_data->tx.cfg.timeout));

0 commit comments

Comments
 (0)