Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I2C RTIO support #2330

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion drivers/i2c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ else()
)
endif()

zephyr_library_sources_ifdef(CONFIG_I2C_NRFX_TWIM i2c_nrfx_twim.c)
if(CONFIG_I2C_RTIO)
zephyr_library_sources_ifdef(CONFIG_I2C_NRFX_TWIM
i2c_nrfx_twim_rtio.c
i2c_nrfx_twim_common.c
)
else()
zephyr_library_sources_ifdef(CONFIG_I2C_NRFX_TWIM
i2c_nrfx_twim.c
i2c_nrfx_twim_common.c
)
endif()
zephyr_library_sources_ifdef(CONFIG_I2C_SAM_TWI i2c_sam_twi.c)

if(CONFIG_I2C_RTIO)
Expand Down
3 changes: 2 additions & 1 deletion drivers/i2c/i2c_nrfx_twi_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
: I2C_NRFX_TWI_INVALID_FREQUENCY)
#define I2C(idx) DT_NODELABEL(i2c##idx)
#define I2C_FREQUENCY(idx) \
I2C_NRFX_TWI_FREQUENCY(DT_PROP(I2C(idx), clock_frequency))
I2C_NRFX_TWI_FREQUENCY(DT_PROP_OR(I2C(idx), clock_frequency, \
I2C_BITRATE_STANDARD))

Check notice on line 27 in drivers/i2c/i2c_nrfx_twi_common.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/i2c/i2c_nrfx_twi_common.h:27 -#define I2C_FREQUENCY(idx) \ - I2C_NRFX_TWI_FREQUENCY(DT_PROP_OR(I2C(idx), clock_frequency, \ - I2C_BITRATE_STANDARD)) +#define I2C_FREQUENCY(idx) \ + I2C_NRFX_TWI_FREQUENCY(DT_PROP_OR(I2C(idx), clock_frequency, I2C_BITRATE_STANDARD))
struct i2c_nrfx_twi_common_data {
uint32_t dev_config;
};
Expand Down
215 changes: 25 additions & 190 deletions drivers/i2c/i2c_nrfx_twim.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* SPDX-License-Identifier: Apache-2.0
*/


#include <zephyr/drivers/i2c.h>
#include <zephyr/dt-bindings/i2c/i2c.h>
#include <zephyr/pm/device.h>
Expand All @@ -17,6 +16,9 @@

#include <zephyr/logging/log.h>
#include <zephyr/irq.h>

#include "i2c_nrfx_twim_common.h"

LOG_MODULE_REGISTER(i2c_nrfx_twim, CONFIG_I2C_LOG_LEVEL);

#if CONFIG_I2C_NRFX_TRANSFER_TIMEOUT
Expand All @@ -31,31 +33,18 @@
volatile nrfx_err_t res;
};

struct i2c_nrfx_twim_config {
nrfx_twim_t twim;
nrfx_twim_config_t twim_config;
uint16_t msg_buf_size;
void (*irq_connect)(void);
const struct pinctrl_dev_config *pcfg;
uint8_t *msg_buf;
uint16_t max_transfer_size;
};

static int i2c_nrfx_twim_recover_bus(const struct device *dev);

static int i2c_nrfx_twim_transfer(const struct device *dev,
struct i2c_msg *msgs,
uint8_t num_msgs, uint16_t addr)
{
struct i2c_nrfx_twim_data *dev_data = dev->data;
const struct i2c_nrfx_twim_config *dev_config = dev->config;
const struct i2c_nrfx_twim_common_config *dev_config = dev->config;
int ret = 0;
uint8_t *msg_buf = dev_config->msg_buf;
uint16_t msg_buf_used = 0;
uint16_t msg_buf_size = dev_config->msg_buf_size;
nrfx_twim_xfer_desc_t cur_xfer = {
.address = addr
};
uint8_t *buf;
uint16_t buf_len;

k_sem_take(&dev_data->transfer_sync, K_FOREVER);

Expand Down Expand Up @@ -116,35 +105,15 @@
}

if (msg_buf_used == 0) {
cur_xfer.p_primary_buf = msgs[i].buf;
cur_xfer.primary_length = msgs[i].len;
buf = msgs[i].buf;
buf_len = msgs[i].len;
} else {
cur_xfer.p_primary_buf = msg_buf;
cur_xfer.primary_length = msg_buf_used;
}
cur_xfer.type = (msgs[i].flags & I2C_MSG_READ) ?
NRFX_TWIM_XFER_RX : NRFX_TWIM_XFER_TX;

if (cur_xfer.primary_length > dev_config->max_transfer_size) {
LOG_ERR("Trying to transfer more than the maximum size "
"for this device: %d > %d",
cur_xfer.primary_length,
dev_config->max_transfer_size);
return -ENOSPC;
buf = msg_buf;
buf_len = msg_buf_used;
}

nrfx_err_t res = nrfx_twim_xfer(&dev_config->twim,
&cur_xfer,
(msgs[i].flags & I2C_MSG_STOP) ?
0 : NRFX_TWIM_FLAG_TX_NO_STOP);
if (res != NRFX_SUCCESS) {
if (res == NRFX_ERROR_BUSY) {
ret = -EBUSY;
break;
} else {
ret = -EIO;
break;
}
ret = i2c_nrfx_twim_msg_transfer(dev, msgs[i].flags, buf, buf_len, addr);
if (ret < 0) {
break;
}

ret = k_sem_take(&dev_data->completion_sync,
Expand All @@ -171,18 +140,15 @@
break;
}

res = dev_data->res;

if (res != NRFX_SUCCESS) {
if (dev_data->res != NRFX_SUCCESS) {
ret = -EIO;
break;
}

/* If concatenated messages were I2C_MSG_READ type, then
* content of concatenation buffer has to be copied back into
* buffers provided by user. */
if ((msgs[i].flags & I2C_MSG_READ)
&& cur_xfer.p_primary_buf == msg_buf) {
if ((msgs[i].flags & I2C_MSG_READ) && (buf == msg_buf)) {
int j = i;

while (msg_buf_used >= msgs[j].len) {
Expand All @@ -193,7 +159,7 @@
j--;
}

}

Check notice on line 162 in drivers/i2c/i2c_nrfx_twim.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/i2c/i2c_nrfx_twim.c:162 -

msg_buf_used = 0;
}
Expand All @@ -207,7 +173,8 @@

static void event_handler(nrfx_twim_evt_t const *p_event, void *p_context)
{
struct i2c_nrfx_twim_data *dev_data = p_context;
const struct device *dev = p_context;
struct i2c_nrfx_twim_data *dev_data = dev->data;

switch (p_event->type) {
case NRFX_TWIM_EVT_DONE:
Expand All @@ -227,65 +194,14 @@
k_sem_give(&dev_data->completion_sync);
}

static int i2c_nrfx_twim_configure(const struct device *dev,
uint32_t i2c_config)
{
const struct i2c_nrfx_twim_config *dev_config = dev->config;

if (I2C_ADDR_10_BITS & i2c_config) {
return -EINVAL;
}

switch (I2C_SPEED_GET(i2c_config)) {
case I2C_SPEED_STANDARD:
nrf_twim_frequency_set(dev_config->twim.p_twim,
NRF_TWIM_FREQ_100K);
break;
case I2C_SPEED_FAST:
nrf_twim_frequency_set(dev_config->twim.p_twim,
NRF_TWIM_FREQ_400K);
break;
#if NRF_TWIM_HAS_1000_KHZ_FREQ
case I2C_SPEED_FAST_PLUS:
nrf_twim_frequency_set(dev_config->twim.p_twim,
NRF_TWIM_FREQ_1000K);
break;
#endif
default:
LOG_ERR("unsupported speed");
return -EINVAL;
}

return 0;
}

static int i2c_nrfx_twim_recover_bus(const struct device *dev)
static int i2c_nrfx_twim_init(const struct device *dev)
{
const struct i2c_nrfx_twim_config *dev_config = dev->config;
enum pm_device_state state;
uint32_t scl_pin;
uint32_t sda_pin;
nrfx_err_t err;

scl_pin = nrf_twim_scl_pin_get(dev_config->twim.p_twim);
sda_pin = nrf_twim_sda_pin_get(dev_config->twim.p_twim);

/* disable peripheral if active (required to release SCL/SDA lines) */
(void)pm_device_state_get(dev, &state);
if (state == PM_DEVICE_STATE_ACTIVE) {
nrfx_twim_disable(&dev_config->twim);
}
struct i2c_nrfx_twim_data *data = dev->data;

err = nrfx_twim_bus_recover(scl_pin, sda_pin);
k_sem_init(&data->transfer_sync, 1, 1);
k_sem_init(&data->completion_sync, 0, 1);

/* restore peripheral if it was active before */
if (state == PM_DEVICE_STATE_ACTIVE) {
(void)pinctrl_apply_state(dev_config->pcfg,
PINCTRL_STATE_DEFAULT);
nrfx_twim_enable(&dev_config->twim);
}

return (err == NRFX_SUCCESS ? 0 : -EBUSY);
return i2c_nrfx_twim_common_init(dev);
}

static const struct i2c_driver_api i2c_nrfx_twim_driver_api = {
Expand All @@ -297,89 +213,6 @@
.recover_bus = i2c_nrfx_twim_recover_bus,
};

#ifdef CONFIG_PM_DEVICE
static int twim_nrfx_pm_action(const struct device *dev,
enum pm_device_action action)
{
const struct i2c_nrfx_twim_config *dev_config = dev->config;
int ret = 0;

switch (action) {
case PM_DEVICE_ACTION_RESUME:
ret = pinctrl_apply_state(dev_config->pcfg,
PINCTRL_STATE_DEFAULT);
if (ret < 0) {
return ret;
}
nrfx_twim_enable(&dev_config->twim);
break;

case PM_DEVICE_ACTION_SUSPEND:
nrfx_twim_disable(&dev_config->twim);

ret = pinctrl_apply_state(dev_config->pcfg,
PINCTRL_STATE_SLEEP);
if (ret < 0) {
return ret;
}
break;

default:
ret = -ENOTSUP;
}

return ret;
}
#endif /* CONFIG_PM_DEVICE */

static int i2c_nrfx_twim_init(const struct device *dev)
{
const struct i2c_nrfx_twim_config *dev_config = dev->config;
struct i2c_nrfx_twim_data *dev_data = dev->data;

dev_config->irq_connect();

k_sem_init(&dev_data->transfer_sync, 1, 1);
k_sem_init(&dev_data->completion_sync, 0, 1);

int err = pinctrl_apply_state(dev_config->pcfg,
COND_CODE_1(CONFIG_PM_DEVICE_RUNTIME,
(PINCTRL_STATE_SLEEP),
(PINCTRL_STATE_DEFAULT)));
if (err < 0) {
return err;
}

if (nrfx_twim_init(&dev_config->twim, &dev_config->twim_config,
event_handler, dev_data) != NRFX_SUCCESS) {
LOG_ERR("Failed to initialize device: %s", dev->name);
return -EIO;
}

#ifdef CONFIG_PM_DEVICE_RUNTIME
pm_device_init_suspended(dev);
pm_device_runtime_enable(dev);
#else
nrfx_twim_enable(&dev_config->twim);
#endif

return 0;
}

#define I2C_NRFX_TWIM_INVALID_FREQUENCY ((nrf_twim_frequency_t)-1)
#define I2C_NRFX_TWIM_FREQUENCY(bitrate) \
(bitrate == I2C_BITRATE_STANDARD ? NRF_TWIM_FREQ_100K : \
bitrate == 250000 ? NRF_TWIM_FREQ_250K : \
bitrate == I2C_BITRATE_FAST ? NRF_TWIM_FREQ_400K : \
IF_ENABLED(NRF_TWIM_HAS_1000_KHZ_FREQ, \
(bitrate == I2C_BITRATE_FAST_PLUS ? NRF_TWIM_FREQ_1000K :)) \
I2C_NRFX_TWIM_INVALID_FREQUENCY)

#define I2C(idx) DT_NODELABEL(i2c##idx)
#define I2C_HAS_PROP(idx, prop) DT_NODE_HAS_PROP(I2C(idx), prop)
#define I2C_FREQUENCY(idx) \
I2C_NRFX_TWIM_FREQUENCY(DT_PROP(I2C(idx), clock_frequency))

#define CONCAT_BUF_SIZE(idx) \
COND_CODE_1(DT_NODE_HAS_PROP(I2C(idx), zephyr_concat_buf_size), \
(DT_PROP(I2C(idx), zephyr_concat_buf_size)), (0))
Expand Down Expand Up @@ -408,13 +241,15 @@
I2C_MEMORY_SECTION(idx);)) \
static struct i2c_nrfx_twim_data twim_##idx##_data; \
PINCTRL_DT_DEFINE(I2C(idx)); \
static const struct i2c_nrfx_twim_config twim_##idx##z_config = { \
static const \
struct i2c_nrfx_twim_common_config twim_##idx##z_config = { \
.twim = NRFX_TWIM_INSTANCE(idx), \
.twim_config = { \
.skip_gpio_cfg = true, \
.skip_psel_cfg = true, \
.frequency = I2C_FREQUENCY(idx), \
}, \
.event_handler = event_handler, \
.msg_buf_size = MSG_BUF_SIZE(idx), \
.irq_connect = irq_connect##idx, \
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(I2C(idx)), \
Expand All @@ -432,7 +267,7 @@
POST_KERNEL, \
CONFIG_I2C_INIT_PRIORITY, \
&i2c_nrfx_twim_driver_api)

Check notice on line 270 in drivers/i2c/i2c_nrfx_twim.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/i2c/i2c_nrfx_twim.c:270 -#define I2C_NRFX_TWIM_DEVICE(idx) \ - NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(I2C(idx)); \ - BUILD_ASSERT(I2C_FREQUENCY(idx) != \ - I2C_NRFX_TWIM_INVALID_FREQUENCY, \ - "Wrong I2C " #idx " frequency setting in dts"); \ - static void irq_connect##idx(void) \ - { \ - IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority), \ - nrfx_isr, nrfx_twim_##idx##_irq_handler, 0); \ - } \ - IF_ENABLED(USES_MSG_BUF(idx), \ - (static uint8_t twim_##idx##_msg_buf[MSG_BUF_SIZE(idx)] \ - I2C_MEMORY_SECTION(idx);)) \ - static struct i2c_nrfx_twim_data twim_##idx##_data; \ - PINCTRL_DT_DEFINE(I2C(idx)); \ - static const \ - struct i2c_nrfx_twim_common_config twim_##idx##z_config = { \ - .twim = NRFX_TWIM_INSTANCE(idx), \ - .twim_config = { \ - .skip_gpio_cfg = true, \ - .skip_psel_cfg = true, \ - .frequency = I2C_FREQUENCY(idx), \ - }, \ - .event_handler = event_handler, \ - .msg_buf_size = MSG_BUF_SIZE(idx), \ - .irq_connect = irq_connect##idx, \ - .pcfg = PINCTRL_DT_DEV_CONFIG_GET(I2C(idx)), \ - IF_ENABLED(USES_MSG_BUF(idx), \ - (.msg_buf = twim_##idx##_msg_buf,)) \ - .max_transfer_size = BIT_MASK( \ - DT_PROP(I2C(idx), easydma_maxcnt_bits)), \ - }; \ - PM_DEVICE_DT_DEFINE(I2C(idx), twim_nrfx_pm_action); \ - I2C_DEVICE_DT_DEFINE(I2C(idx), \ - i2c_nrfx_twim_init, \ - PM_DEVICE_DT_GET(I2C(idx)), \ - &twim_##idx##_data, \ - &twim_##idx##z_config, \ - POST_KERNEL, \ - CONFIG_I2C_INIT_PRIORITY, \ - &i2c_nrfx_twim_driver_api) +#define I2C_NRFX_TWIM_DEVICE(idx) \ + NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(I2C(idx)); \ + BUILD_ASSERT(I2C_FREQUENCY(idx) != I2C_NRFX_TWIM_INVALID_FREQUENCY, \ + "Wrong I2C " #idx " frequency setting in dts"); \ + static void irq_connect##idx(void) \ + { \ + IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority), nrfx_isr, \ + nrfx_twim_##idx##_irq_handler, 0); \ + } \ + IF_ENABLED( \ + USES_MSG_BUF(idx), \ + (static uint8_t twim_##idx##_msg_buf[MSG_BUF_SIZE(idx)] I2C_MEMORY_SECTION(idx);)) \ + static struct i2c_nrfx_twim_data twim_##idx##_data; \ + PINCTRL_DT_DEFINE(I2C(idx)); \ + static const struct i2c_nrfx_twim_common_config twim_##idx##z_config = { \ + .twim = NRFX_TWIM_INSTANCE(idx), \ + .twim_config = \ + { \ + .skip_gpio_cfg = true, \ + .skip_psel_cfg = true, \ + .frequency = I2C_FREQUENCY(idx), \ + }, \ + .event_handler = event_handler, \ + .msg_buf_size = MSG_BUF_SIZE(idx), \ + .irq_connect = irq_connect##idx
#define I2C_MEMORY_SECTION(idx) \
COND_CODE_1(I2C_HAS_PROP(idx, memory_regions), \
(__attribute__((__section__(LINKER_DT_NODE_REGION_NAME( \
Expand Down
Loading
Loading