Skip to content

Commit

Permalink
i2c: i2c_nrfx_twim: extract common code
Browse files Browse the repository at this point in the history
Extract code useful for an RTIO implementation to a separate file.

Signed-off-by: Jordan Yates <[email protected]>
  • Loading branch information
JordanYates authored and aescolar committed Oct 21, 2024
1 parent 6773f33 commit 0218b36
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 163 deletions.
5 changes: 4 additions & 1 deletion drivers/i2c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ else()
)
endif()

zephyr_library_sources_ifdef(CONFIG_I2C_NRFX_TWIM i2c_nrfx_twim.c)
zephyr_library_sources_ifdef(CONFIG_I2C_NRFX_TWIM
i2c_nrfx_twim.c
i2c_nrfx_twim_common.c
)
zephyr_library_sources_ifdef(CONFIG_I2C_SAM_TWI i2c_sam_twi.c)

if(CONFIG_I2C_RTIO)
Expand Down
185 changes: 23 additions & 162 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 @@ struct i2c_nrfx_twim_data {
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 @@ static int i2c_nrfx_twim_transfer(const struct device *dev,
}

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 @@ static int i2c_nrfx_twim_transfer(const struct device *dev,
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 Down Expand Up @@ -227,65 +193,14 @@ static void event_handler(nrfx_twim_evt_t const *p_event, void *p_context)
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);

/* 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);
}
k_sem_init(&data->transfer_sync, 1, 1);
k_sem_init(&data->completion_sync, 0, 1);

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,62 +212,6 @@ static const struct i2c_driver_api i2c_nrfx_twim_driver_api = {
.recover_bus = i2c_nrfx_twim_recover_bus,
};

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;

switch (action) {
case PM_DEVICE_ACTION_RESUME:
(void)pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_DEFAULT);
nrfx_twim_enable(&dev_config->twim);
break;
case PM_DEVICE_ACTION_SUSPEND:
nrfx_twim_disable(&dev_config->twim);
(void)pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_SLEEP);
break;
default:
return -ENOTSUP;
}

return 0;
}

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);

(void)pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_SLEEP);

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;
}

return pm_device_driver_init(dev, twim_nrfx_pm_action);
}

#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 @@ -381,13 +240,15 @@ static int i2c_nrfx_twim_init(const struct device *dev)
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 Down
Loading

0 comments on commit 0218b36

Please sign in to comment.