Skip to content

Commit 9f205a1

Browse files
[nrf fromlist] drivers: i2c: i2c_nrfx_twim: add DMM usage in driver
Added usage of DMM API in i2c controller driver. Upstream PR #: 93083 Signed-off-by: Michał Stasiak <[email protected]>
1 parent 7c4deb9 commit 9f205a1

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

drivers/i2c/i2c_nrfx_twim.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct i2c_nrfx_twim_data {
3232
struct k_sem transfer_sync;
3333
struct k_sem completion_sync;
3434
volatile nrfx_err_t res;
35+
uint8_t *buf_ptr;
3536
};
3637

3738
int i2c_nrfx_twim_exclusive_access_acquire(const struct device *dev, k_timeout_t timeout)
@@ -69,6 +70,7 @@ static int i2c_nrfx_twim_transfer(const struct device *dev,
6970
uint16_t msg_buf_size = dev_config->msg_buf_size;
7071
uint8_t *buf;
7172
uint16_t buf_len;
73+
uint8_t *dma_buf;
7274

7375
(void)i2c_nrfx_twim_exclusive_access_acquire(dev, K_FOREVER);
7476

@@ -133,6 +135,23 @@ static int i2c_nrfx_twim_transfer(const struct device *dev,
133135
buf = msg_buf;
134136
buf_len = msg_buf_used;
135137
}
138+
139+
if (msgs[i].flags & I2C_MSG_READ) {
140+
ret = dmm_buffer_in_prepare(dev_config->mem_reg, buf, buf_len,
141+
(void **)&dma_buf);
142+
} else {
143+
ret = dmm_buffer_out_prepare(dev_config->mem_reg, buf, buf_len,
144+
(void **)&dma_buf);
145+
}
146+
147+
if (ret < 0) {
148+
LOG_ERR("Failed to prepare buffer: %d", ret);
149+
return ret;
150+
}
151+
152+
dev_data->buf_ptr = buf;
153+
buf = dma_buf;
154+
136155
ret = i2c_nrfx_twim_msg_transfer(dev, msgs[i].flags, buf, buf_len, addr);
137156
if (ret < 0) {
138157
break;
@@ -198,6 +217,23 @@ static void event_handler(nrfx_twim_evt_t const *p_event, void *p_context)
198217

199218
switch (p_event->type) {
200219
case NRFX_TWIM_EVT_DONE:
220+
const struct i2c_nrfx_twim_common_config *config = dev->config;
221+
int ret = 0;
222+
223+
if (p_event->xfer_desc.type == NRFX_TWIM_XFER_TX) {
224+
ret = dmm_buffer_out_release(config->mem_reg,
225+
(void **)&p_event->xfer_desc.p_primary_buf);
226+
} else {
227+
ret = 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+
232+
if (ret < 0) {
233+
dev_data->res = NRFX_ERROR_INTERNAL;
234+
break;
235+
}
236+
201237
dev_data->res = NRFX_SUCCESS;
202238
break;
203239
case NRFX_TWIM_EVT_ADDRESS_NACK:
@@ -283,6 +319,7 @@ static DEVICE_API(i2c, i2c_nrfx_twim_driver_api) = {
283319
(.msg_buf = twim_##idx##_msg_buf,)) \
284320
.max_transfer_size = BIT_MASK( \
285321
DT_PROP(I2C(idx), easydma_maxcnt_bits)), \
322+
.mem_reg = DMM_DEV_TO_REG(I2C(idx)), \
286323
}; \
287324
PM_DEVICE_DT_DEFINE(I2C(idx), twim_nrfx_pm_action, \
288325
PM_DEVICE_ISR_SAFE); \

drivers/i2c/i2c_nrfx_twim_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <zephyr/device.h>
1212
#include <zephyr/pm/device.h>
1313
#include <nrfx_twim.h>
14+
#include <dmm.h>
1415

1516
#ifdef __cplusplus
1617
extern "C" {
@@ -40,6 +41,7 @@ struct i2c_nrfx_twim_common_config {
4041
const struct pinctrl_dev_config *pcfg;
4142
uint8_t *msg_buf;
4243
uint16_t max_transfer_size;
44+
void *mem_reg;
4345
};
4446

4547
int i2c_nrfx_twim_common_init(const struct device *dev);

0 commit comments

Comments
 (0)