Skip to content
Open
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
3 changes: 1 addition & 2 deletions drivers/i2c/Kconfig.nrfx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ if I2C_NRFX
config I2C_NRFX_TWI
def_bool y
depends on DT_HAS_NORDIC_NRF_TWI_ENABLED
select NRFX_TWI0 if HAS_HW_NRF_TWI0
select NRFX_TWI1 if HAS_HW_NRF_TWI1
select NRFX_TWI

config I2C_NRFX_TWIM
def_bool y
Expand Down
93 changes: 45 additions & 48 deletions drivers/i2c/i2c_nrfx_twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
#include <zephyr/irq.h>
LOG_MODULE_REGISTER(i2c_nrfx_twi, CONFIG_I2C_LOG_LEVEL);

#define DT_DRV_COMPAT nordic_nrf_twi

#if CONFIG_I2C_NRFX_TRANSFER_TIMEOUT
#define I2C_TRANSFER_TIMEOUT_MSEC K_MSEC(CONFIG_I2C_NRFX_TRANSFER_TIMEOUT)
#else
#define I2C_TRANSFER_TIMEOUT_MSEC K_FOREVER
#endif

struct i2c_nrfx_twi_data {
nrfx_twi_t twi;
uint32_t dev_config;
struct k_sem transfer_sync;
struct k_sem completion_sync;
Expand All @@ -42,7 +45,6 @@
struct i2c_msg *msgs,
uint8_t num_msgs, uint16_t addr)
{
const struct i2c_nrfx_twi_config *config = dev->config;
struct i2c_nrfx_twi_data *data = dev->data;
int ret = 0;

Expand All @@ -51,12 +53,12 @@
/* Dummy take on completion_sync sem to be sure that it is empty */
k_sem_take(&data->completion_sync, K_NO_WAIT);

nrfx_twi_enable(&config->twi);
nrfx_twi_enable(&data->twi);

for (size_t i = 0; i < num_msgs; i++) {
bool more_msgs = ((i < (num_msgs - 1)) &&
!(msgs[i + 1].flags & I2C_MSG_RESTART));
!(msgs[i + 1].flags & I2C_MSG_RESTART));

Check warning on line 60 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

LEADING_SPACE

drivers/i2c/i2c_nrfx_twi.c:60 please, no spaces at the start of a line

Check failure on line 60 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

CODE_INDENT

drivers/i2c/i2c_nrfx_twi.c:60 code indent should use tabs where possible

Check notice on line 61 in drivers/i2c/i2c_nrfx_twi.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_twi.c:61 - bool more_msgs = ((i < (num_msgs - 1)) && - !(msgs[i + 1].flags & I2C_MSG_RESTART)); + bool more_msgs = ((i < (num_msgs - 1)) && !(msgs[i + 1].flags & I2C_MSG_RESTART));
ret = i2c_nrfx_twi_msg_transfer(dev, msgs[i].flags,
msgs[i].buf,
msgs[i].len, addr,
Expand Down Expand Up @@ -84,7 +86,7 @@
* to make sure everything has been done to restore the
* bus from this error.
*/
nrfx_twi_disable(&config->twi);
nrfx_twi_disable(&data->twi);
(void)i2c_nrfx_twi_recover_bus(dev);
ret = -EIO;
break;
Expand All @@ -96,13 +98,13 @@
}
}

nrfx_twi_disable(&config->twi);
nrfx_twi_disable(&data->twi);
k_sem_give(&data->transfer_sync);

return ret;
}

static void event_handler(nrfx_twi_evt_t const *p_event, void *p_context)
static void event_handler(nrfx_twi_event_t const *p_event, void *p_context)
{
const struct device *dev = p_context;
struct i2c_nrfx_twi_data *dev_data = (struct i2c_nrfx_twi_data *)dev->data;
Expand Down Expand Up @@ -131,45 +133,40 @@
.recover_bus = i2c_nrfx_twi_recover_bus,
};

#define I2C_NRFX_TWI_DEVICE(idx) \
NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(I2C(idx)); \
BUILD_ASSERT(I2C_FREQUENCY(I2C(idx)) != I2C_NRFX_TWI_INVALID_FREQUENCY, \
"Wrong I2C " #idx " frequency setting in dts"); \
static int twi_##idx##_init(const struct device *dev) \
{ \
IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority), nrfx_isr, \
nrfx_twi_##idx##_irq_handler, 0); \
const struct i2c_nrfx_twi_config *config = dev->config; \
int err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); \
if (err < 0) { \
return err; \
} \
return i2c_nrfx_twi_init(dev); \
} \
static struct i2c_nrfx_twi_data twi_##idx##_data = { \
.transfer_sync = Z_SEM_INITIALIZER(twi_##idx##_data.transfer_sync, 1, 1), \
.completion_sync = Z_SEM_INITIALIZER(twi_##idx##_data.completion_sync, 0, 1)}; \
PINCTRL_DT_DEFINE(I2C(idx)); \
static const struct i2c_nrfx_twi_config twi_##idx##z_config = { \
.twi = NRFX_TWI_INSTANCE(idx), \
.config = \
{ \
.skip_gpio_cfg = true, \
.skip_psel_cfg = true, \
.frequency = I2C_FREQUENCY(I2C(idx)), \
}, \
.event_handler = event_handler, \
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(I2C(idx)), \
}; \
PM_DEVICE_DT_DEFINE(I2C(idx), twi_nrfx_pm_action); \
I2C_DEVICE_DT_DEFINE(I2C(idx), twi_##idx##_init, PM_DEVICE_DT_GET(I2C(idx)), \
&twi_##idx##_data, &twi_##idx##z_config, POST_KERNEL, \
CONFIG_I2C_INIT_PRIORITY, &i2c_nrfx_twi_driver_api)

#ifdef CONFIG_HAS_HW_NRF_TWI0
I2C_NRFX_TWI_DEVICE(0);
#endif

#ifdef CONFIG_HAS_HW_NRF_TWI1
I2C_NRFX_TWI_DEVICE(1);
#endif
#define I2C_NRFX_TWI_DEVICE(idx) \
NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(DT_DRV_INST(idx)); \

Check warning on line 137 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

LONG_LINE

drivers/i2c/i2c_nrfx_twi.c:137 line length of 103 exceeds 100 columns
BUILD_ASSERT(I2C_FREQUENCY(DT_DRV_INST(idx)) != I2C_NRFX_TWI_INVALID_FREQUENCY, \

Check warning on line 138 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

LONG_LINE

drivers/i2c/i2c_nrfx_twi.c:138 line length of 103 exceeds 100 columns
"Wrong I2C " #idx " frequency setting in dts"); \

Check warning on line 139 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

LONG_LINE

drivers/i2c/i2c_nrfx_twi.c:139 line length of 111 exceeds 100 columns
static struct i2c_nrfx_twi_data twi_##idx##_data = { \

Check warning on line 140 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

LONG_LINE

drivers/i2c/i2c_nrfx_twi.c:140 line length of 103 exceeds 100 columns
.twi = NRFX_TWI_INSTANCE(DT_INST_REG_ADDR(idx)), \ \

Check warning on line 141 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

LONG_LINE

drivers/i2c/i2c_nrfx_twi.c:141 line length of 164 exceeds 100 columns
.transfer_sync = Z_SEM_INITIALIZER(twi_##idx##_data.transfer_sync, 1, 1), \

Check warning on line 142 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

LONG_LINE

drivers/i2c/i2c_nrfx_twi.c:142 line length of 107 exceeds 100 columns
.completion_sync = Z_SEM_INITIALIZER(twi_##idx##_data.completion_sync, 0, 1) \

Check warning on line 143 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

LONG_LINE

drivers/i2c/i2c_nrfx_twi.c:143 line length of 107 exceeds 100 columns
}; \

Check warning on line 144 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

LONG_LINE

drivers/i2c/i2c_nrfx_twi.c:144 line length of 103 exceeds 100 columns
static int twi_##idx##_init(const struct device *dev) \

Check warning on line 145 in drivers/i2c/i2c_nrfx_twi.c

View workflow job for this annotation

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

LONG_LINE

drivers/i2c/i2c_nrfx_twi.c:145 line length of 103 exceeds 100 columns
{ \
IRQ_CONNECT(DT_INST_IRQN(idx), DT_INST_IRQ(idx, priority), nrfx_twi_irq_handler, \
&twi_##idx##_data.twi, 0); \
const struct i2c_nrfx_twi_config *config = dev->config; \
int err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); \
if (err < 0) { \
return err; \
} \
return i2c_nrfx_twi_init(dev); \
} \
PINCTRL_DT_INST_DEFINE(idx); \
static const struct i2c_nrfx_twi_config twi_##idx##z_config = { \
.config = \
{ \
.skip_gpio_cfg = true, \
.skip_psel_cfg = true, \
.frequency = I2C_FREQUENCY(DT_DRV_INST(idx)), \
}, \
.event_handler = event_handler, \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(idx), \
}; \
PM_DEVICE_DT_INST_DEFINE(idx, twi_nrfx_pm_action); \
I2C_DEVICE_DT_INST_DEFINE(idx, twi_##idx##_init, PM_DEVICE_DT_INST_GET(idx), \
&twi_##idx##_data, &twi_##idx##z_config, POST_KERNEL, \
CONFIG_I2C_INIT_PRIORITY, &i2c_nrfx_twi_driver_api)

Check notice on line 171 in drivers/i2c/i2c_nrfx_twi.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_twi.c:171 -#define I2C_NRFX_TWI_DEVICE(idx) \ - NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(DT_DRV_INST(idx)); \ - BUILD_ASSERT(I2C_FREQUENCY(DT_DRV_INST(idx)) != I2C_NRFX_TWI_INVALID_FREQUENCY, \ - "Wrong I2C " #idx " frequency setting in dts"); \ - static struct i2c_nrfx_twi_data twi_##idx##_data = { \ - .twi = NRFX_TWI_INSTANCE(DT_INST_REG_ADDR(idx)), \ \ - .transfer_sync = Z_SEM_INITIALIZER(twi_##idx##_data.transfer_sync, 1, 1), \ - .completion_sync = Z_SEM_INITIALIZER(twi_##idx##_data.completion_sync, 0, 1) \ - }; \ - static int twi_##idx##_init(const struct device *dev) \ - { \ - IRQ_CONNECT(DT_INST_IRQN(idx), DT_INST_IRQ(idx, priority), nrfx_twi_irq_handler, \ - &twi_##idx##_data.twi, 0); \ - const struct i2c_nrfx_twi_config *config = dev->config; \ - int err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); \ - if (err < 0) { \ +#define I2C_NRFX_TWI_DEVICE(idx) \ + NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(DT_DRV_INST(idx)); \ + BUILD_ASSERT(I2C_FREQUENCY(DT_DRV_INST(idx)) != I2C_NRFX_TWI_INVALID_FREQUENCY, \ + "Wrong I2C " #idx " frequency setting in dts"); \ + static struct i2c_nrfx_twi_data twi_##idx##_data = { \ + .twi = NRFX_TWI_INSTANCE(DT_INST_REG_ADDR(idx)), \ .transfer_sync = Z_SEM_INITIALIZER( \ + twi_##idx##_data \ + .transfer_sync, \ + 1, 1), \ + .completion_sync = Z_SEM_INITIALIZER(twi_##idx##_data.completion_sync, 0, 1)}; \ + static int twi_##idx##_init(const struct device *dev) \ + { \ + IRQ_CONNECT(DT_INST_IRQN(idx), DT_INST_IRQ(idx, priority), nrfx_twi_irq_handler, \ + &twi_##idx##_data.twi, 0); \ + const struct i2c_nrfx_twi_config *config = dev->config; \ + int err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); \ + if (err < 0) { \ return err; \ - } \ - return i2c_nrfx_twi_init(dev); \ - } \ - PINCTRL_DT_INST_DEFINE(idx); \ - static const struct i2c_nrfx_twi_config twi_##idx##z_config = { \ - .config = \ + } \ + return i2c_nrfx_twi_init(dev); \ + }
DT_INST_FOREACH_STATUS_OKAY(I2C_NRFX_TWI_DEVICE)
18 changes: 9 additions & 9 deletions drivers/i2c/i2c_nrfx_twi_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
int i2c_nrfx_twi_init(const struct device *dev)
{
const struct i2c_nrfx_twi_config *config = dev->config;
int result = nrfx_twi_init(&config->twi, &config->config,
struct i2c_nrfx_twi_common_data *data = dev->data;
int result = nrfx_twi_init(&data->twi, &config->config,
config->event_handler, (void *)dev);
if (result != 0) {

Check notice on line 22 in drivers/i2c/i2c_nrfx_twi_common.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_twi_common.c:22 - int result = nrfx_twi_init(&data->twi, &config->config, - config->event_handler, (void *)dev); + int result = nrfx_twi_init(&data->twi, &config->config, config->event_handler, (void *)dev);
LOG_ERR("Failed to initialize device: %s",
dev->name);
return result;
Expand All @@ -29,9 +30,8 @@

int i2c_nrfx_twi_configure(const struct device *dev, uint32_t dev_config)
{
const struct i2c_nrfx_twi_config *config = dev->config;
struct i2c_nrfx_twi_common_data *data = dev->data;
nrfx_twi_t const *inst = &config->twi;
nrfx_twi_t const *inst = &data->twi;

if (I2C_ADDR_10_BITS & dev_config) {
return -EINVAL;
Expand All @@ -55,12 +55,12 @@

int i2c_nrfx_twi_recover_bus(const struct device *dev)
{
const struct i2c_nrfx_twi_config *config = dev->config;
struct i2c_nrfx_twi_common_data *data = dev->data;
uint32_t scl_pin;
uint32_t sda_pin;

scl_pin = nrf_twi_scl_pin_get(config->twi.p_twi);
sda_pin = nrf_twi_sda_pin_get(config->twi.p_twi);
scl_pin = nrf_twi_scl_pin_get(data->twi.p_twi);
sda_pin = nrf_twi_sda_pin_get(data->twi.p_twi);

return nrfx_twi_bus_recover(scl_pin, sda_pin);
}
Expand All @@ -69,7 +69,7 @@
uint8_t *buf, size_t buf_len,
uint16_t i2c_addr, bool more_msgs)
{
const struct i2c_nrfx_twi_config *config = dev->config;
struct i2c_nrfx_twi_common_data *data = dev->data;
int ret = 0;
uint32_t xfer_flags = 0;
nrfx_twi_xfer_desc_t cur_xfer = {
Expand Down Expand Up @@ -107,7 +107,7 @@
}

if (!ret) {
ret = nrfx_twi_xfer(&config->twi, &cur_xfer, xfer_flags);
ret = nrfx_twi_xfer(&data->twi, &cur_xfer, xfer_flags);
}

return ret;
Expand All @@ -133,7 +133,7 @@
break;

case PM_DEVICE_ACTION_SUSPEND:
nrfx_twi_uninit(&config->twi);
nrfx_twi_uninit(&data->twi);

ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
if (ret < 0) {
Expand Down
13 changes: 6 additions & 7 deletions drivers/i2c/i2c_nrfx_twi_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@
: bitrate == 250000 ? NRF_TWI_FREQ_250K \
: bitrate == I2C_BITRATE_FAST ? NRF_TWI_FREQ_400K \
: I2C_NRFX_TWI_INVALID_FREQUENCY)
#define I2C(idx) DT_NODELABEL(i2c##idx)
#define I2C_FREQUENCY(idx) \
I2C_NRFX_TWI_FREQUENCY(DT_PROP_OR(I2C(idx), clock_frequency, \
I2C_BITRATE_STANDARD))

#define I2C_FREQUENCY(node) \
I2C_NRFX_TWI_FREQUENCY(DT_PROP_OR(node, clock_frequency, I2C_BITRATE_STANDARD))

Check notice on line 25 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:25 -#define I2C_FREQUENCY(node) \ +#define I2C_FREQUENCY(node) \

struct i2c_nrfx_twi_common_data {
nrfx_twi_t twi;
uint32_t dev_config;
};

struct i2c_nrfx_twi_config {
nrfx_twi_t twi;
nrfx_twi_config_t config;
nrfx_twi_evt_handler_t event_handler;
nrfx_twi_event_handler_t event_handler;
const struct pinctrl_dev_config *pcfg;
};

static inline nrfx_err_t i2c_nrfx_twi_get_evt_result(nrfx_twi_evt_t const *p_event)
static inline nrfx_err_t i2c_nrfx_twi_get_evt_result(nrfx_twi_event_t const *p_event)
{
switch (p_event->type) {
case NRFX_TWI_EVT_DONE:
Expand Down
Loading
Loading