Skip to content

Commit 48c8a20

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 9df4cf1 commit 48c8a20

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

drivers/i2c/i2c_nrfx_twim.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ LOG_MODULE_REGISTER(i2c_nrfx_twim, CONFIG_I2C_LOG_LEVEL);
2828
#define I2C_TRANSFER_TIMEOUT_MSEC K_FOREVER
2929
#endif
3030

31-
struct i2c_nrfx_twim_data {
32-
struct k_sem transfer_sync;
33-
struct k_sem completion_sync;
34-
volatile nrfx_err_t res;
35-
};
36-
3731
int i2c_nrfx_twim_exclusive_access_acquire(const struct device *dev, k_timeout_t timeout)
3832
{
3933
struct i2c_nrfx_twim_data *dev_data = dev->data;
@@ -198,6 +192,23 @@ static void event_handler(nrfx_twim_evt_t const *p_event, void *p_context)
198192

199193
switch (p_event->type) {
200194
case NRFX_TWIM_EVT_DONE:
195+
const struct i2c_nrfx_twim_common_config *config = dev->config;
196+
int ret = 0;
197+
198+
if (p_event->xfer_desc.type == NRFX_TWIM_XFER_TX) {
199+
ret = dmm_buffer_out_release(config->mem_reg,
200+
(void **)&p_event->xfer_desc.p_primary_buf);
201+
} else {
202+
ret = dmm_buffer_in_release(config->mem_reg, dev_data->usr_buf,
203+
p_event->xfer_desc.primary_length,
204+
p_event->xfer_desc.p_primary_buf);
205+
}
206+
207+
if (ret < 0) {
208+
dev_data->res = NRFX_ERROR_INTERNAL;
209+
break;
210+
}
211+
201212
dev_data->res = NRFX_SUCCESS;
202213
break;
203214
case NRFX_TWIM_EVT_ADDRESS_NACK:
@@ -282,6 +293,7 @@ static DEVICE_API(i2c, i2c_nrfx_twim_driver_api) = {
282293
(.msg_buf = twim_##idx##_msg_buf,)) \
283294
.max_transfer_size = BIT_MASK( \
284295
DT_PROP(I2C(idx), easydma_maxcnt_bits)), \
296+
.mem_reg = DMM_DEV_TO_REG(I2C(idx)), \
285297
}; \
286298
PM_DEVICE_DT_DEFINE(I2C(idx), twim_nrfx_pm_action, \
287299
PM_DEVICE_ISR_SAFE); \

drivers/i2c/i2c_nrfx_twim_common.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,31 @@ int i2c_nrfx_twim_msg_transfer(const struct device *dev, uint8_t flags, uint8_t
7474
size_t buf_len, uint16_t i2c_addr)
7575
{
7676
const struct i2c_nrfx_twim_common_config *config = dev->config;
77+
struct i2c_nrfx_twim_data *dev_data = dev->data;
78+
nrfx_err_t res;
79+
int ret = 0;
80+
uint8_t *dma_buf;
81+
82+
if (flags & I2C_MSG_READ) {
83+
ret = dmm_buffer_in_prepare(config->mem_reg, buf, buf_len, (void **)&dma_buf);
84+
} else {
85+
ret = dmm_buffer_out_prepare(config->mem_reg, buf, buf_len, (void **)&dma_buf);
86+
}
87+
88+
if (ret < 0) {
89+
LOG_ERR("Failed to prepare buffer: %d", ret);
90+
return ret;
91+
}
92+
93+
dev_data->usr_buf = buf;
94+
buf = dma_buf;
95+
7796
nrfx_twim_xfer_desc_t cur_xfer = {
7897
.address = i2c_addr,
7998
.type = (flags & I2C_MSG_READ) ? NRFX_TWIM_XFER_RX : NRFX_TWIM_XFER_TX,
8099
.p_primary_buf = buf,
81100
.primary_length = buf_len,
82101
};
83-
nrfx_err_t res;
84-
int ret = 0;
85102

86103
if (buf_len > config->max_transfer_size) {
87104
LOG_ERR("Trying to transfer more than the maximum size "

drivers/i2c/i2c_nrfx_twim_common.h

Lines changed: 9 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,14 @@ 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;
45+
};
46+
47+
struct i2c_nrfx_twim_data {
48+
struct k_sem transfer_sync;
49+
struct k_sem completion_sync;
50+
volatile nrfx_err_t res;
51+
uint8_t *usr_buf;
4352
};
4453

4554
int i2c_nrfx_twim_common_init(const struct device *dev);

0 commit comments

Comments
 (0)