Skip to content

Commit 85d1b72

Browse files
[nrf fromlist] drivers: i2c: add DMM to i2c_nrfx_twim
Added DMM buffer preparation to I2C TWIM driver. Adds little impact as buffer content is already copied into message buffer which is places in proper memory for optimized transfer. Upstream PR #: 93083 Signed-off-by: Michał Stasiak <[email protected]>
1 parent bb9c9df commit 85d1b72

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

drivers/i2c/i2c_nrfx_twim.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <nrfx_twim.h>
1515
#include <zephyr/sys/util.h>
1616
#include <zephyr/linker/devicetree_regions.h>
17+
#include <dmm.h>
1718

1819
#include <zephyr/logging/log.h>
1920
#include <zephyr/irq.h>
@@ -33,6 +34,7 @@ struct i2c_nrfx_twim_data {
3334
struct k_sem transfer_sync;
3435
struct k_sem completion_sync;
3536
volatile int res;
37+
uint8_t *buf_ptr;
3638
};
3739

3840
int i2c_nrfx_twim_exclusive_access_acquire(const struct device *dev, k_timeout_t timeout)
@@ -70,6 +72,7 @@ static int i2c_nrfx_twim_transfer(const struct device *dev,
7072
uint16_t msg_buf_size = dev_config->msg_buf_size;
7173
uint8_t *buf;
7274
uint16_t buf_len;
75+
uint8_t *dma_buf;
7376

7477
(void)i2c_nrfx_twim_exclusive_access_acquire(dev, K_FOREVER);
7578

@@ -134,7 +137,23 @@ static int i2c_nrfx_twim_transfer(const struct device *dev,
134137
buf = msg_buf;
135138
buf_len = msg_buf_used;
136139
}
137-
ret = i2c_nrfx_twim_msg_transfer(dev, msgs[i].flags, buf, buf_len, addr);
140+
141+
if (msgs[i].flags & I2C_MSG_READ) {
142+
ret = dmm_buffer_in_prepare(dev_config->mem_reg, buf, buf_len,
143+
(void **)&dma_buf);
144+
} else {
145+
ret = dmm_buffer_out_prepare(dev_config->mem_reg, buf, buf_len,
146+
(void **)&dma_buf);
147+
}
148+
149+
if (ret < 0) {
150+
LOG_ERR("Failed to prepare buffer: %d", ret);
151+
return ret;
152+
}
153+
154+
dev_data->buf_ptr = buf;
155+
156+
ret = i2c_nrfx_twim_msg_transfer(dev, msgs[i].flags, dma_buf, buf_len, addr);
138157
if (ret < 0) {
139158
break;
140159
}
@@ -199,7 +218,17 @@ static void event_handler(nrfx_twim_event_t const *p_event, void *p_context)
199218

200219
switch (p_event->type) {
201220
case NRFX_TWIM_EVT_DONE:
202-
dev_data->res = 0;
221+
const struct i2c_nrfx_twim_common_config *config = dev->config;
222+
223+
if (p_event->xfer_desc.type == NRFX_TWIM_XFER_TX) {
224+
dev_data->res = dmm_buffer_out_release(config->mem_reg,
225+
(void **)&p_event->xfer_desc.p_primary_buf);
226+
} else {
227+
dev_data->res = dmm_buffer_in_release(config->mem_reg, dev_data->buf_ptr,
228+
p_event->xfer_desc.primary_length,
229+
p_event->xfer_desc.p_primary_buf);
230+
}
231+
203232
break;
204233
case NRFX_TWIM_EVT_ADDRESS_NACK:
205234
dev_data->res = -EFAULT;
@@ -275,6 +304,7 @@ static DEVICE_API(i2c, i2c_nrfx_twim_driver_api) = {
275304
IF_ENABLED(USES_MSG_BUF(inst), \
276305
(.msg_buf = twim_##inst##_msg_buf,)) \
277306
.max_transfer_size = MAX_TRANSFER_SIZE(inst), \
307+
.mem_reg = DMM_DEV_TO_REG(DT_DRV_INST(inst)), \
278308
}; \
279309
PM_DEVICE_DT_INST_DEFINE(inst, twim_nrfx_pm_action, I2C_PM_ISR_SAFE(inst)); \
280310
I2C_DEVICE_DT_INST_DEINIT_DEFINE(inst, i2c_nrfx_twim_init, i2c_nrfx_twim_deinit, \

drivers/i2c/i2c_nrfx_twim_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct i2c_nrfx_twim_common_config {
7575
uint8_t *msg_buf;
7676
uint16_t max_transfer_size;
7777
nrfx_twim_t *twim;
78+
void *mem_reg;
7879
};
7980

8081
int i2c_nrfx_twim_common_init(const struct device *dev);

0 commit comments

Comments
 (0)