From 801c3b3fea49944311a54b39e5ddde9f65e3d038 Mon Sep 17 00:00:00 2001 From: Adam Kondraciuk Date: Mon, 17 Nov 2025 17:04:10 +0100 Subject: [PATCH 1/2] drivers: i2c: i2c_nrfx_twi: use standard instantiation Switched nrfx_twi API to standard instantiation. Signed-off-by: Adam Kondraciuk --- drivers/i2c/Kconfig.nrfx | 3 +- drivers/i2c/i2c_nrfx_twi.c | 93 ++++++++++---------- drivers/i2c/i2c_nrfx_twi_common.c | 18 ++-- drivers/i2c/i2c_nrfx_twi_common.h | 13 ++- drivers/i2c/i2c_nrfx_twi_rtio.c | 114 ++++++++++++------------- modules/hal_nordic/nrfx/Kconfig | 12 +-- modules/hal_nordic/nrfx/nrfx_kconfig.h | 6 -- soc/nordic/common/Kconfig.peripherals | 6 -- 8 files changed, 117 insertions(+), 148 deletions(-) diff --git a/drivers/i2c/Kconfig.nrfx b/drivers/i2c/Kconfig.nrfx index c6a7dbeadcf7..c967cc239cf5 100644 --- a/drivers/i2c/Kconfig.nrfx +++ b/drivers/i2c/Kconfig.nrfx @@ -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 diff --git a/drivers/i2c/i2c_nrfx_twi.c b/drivers/i2c/i2c_nrfx_twi.c index a28ec6f08a05..255ead716fbc 100644 --- a/drivers/i2c/i2c_nrfx_twi.c +++ b/drivers/i2c/i2c_nrfx_twi.c @@ -17,6 +17,8 @@ #include 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 @@ -24,6 +26,7 @@ LOG_MODULE_REGISTER(i2c_nrfx_twi, CONFIG_I2C_LOG_LEVEL); #endif struct i2c_nrfx_twi_data { + nrfx_twi_t twi; uint32_t dev_config; struct k_sem transfer_sync; struct k_sem completion_sync; @@ -42,7 +45,6 @@ static int i2c_nrfx_twi_transfer(const struct device *dev, 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; @@ -51,11 +53,11 @@ static int i2c_nrfx_twi_transfer(const struct device *dev, /* 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)); ret = i2c_nrfx_twi_msg_transfer(dev, msgs[i].flags, msgs[i].buf, @@ -84,7 +86,7 @@ static int i2c_nrfx_twi_transfer(const struct device *dev, * 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; @@ -96,13 +98,13 @@ static int i2c_nrfx_twi_transfer(const struct device *dev, } } - 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; @@ -131,45 +133,40 @@ static DEVICE_API(i2c, i2c_nrfx_twi_driver_api) = { .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)); \ + 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 = \ + { \ + .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) + +DT_INST_FOREACH_STATUS_OKAY(I2C_NRFX_TWI_DEVICE) diff --git a/drivers/i2c/i2c_nrfx_twi_common.c b/drivers/i2c/i2c_nrfx_twi_common.c index 2d07863e3d73..3953e95aa827 100644 --- a/drivers/i2c/i2c_nrfx_twi_common.c +++ b/drivers/i2c/i2c_nrfx_twi_common.c @@ -16,7 +16,8 @@ LOG_MODULE_DECLARE(i2c_nrfx_twi); 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) { LOG_ERR("Failed to initialize device: %s", @@ -29,9 +30,8 @@ int i2c_nrfx_twi_init(const struct device *dev) 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; @@ -55,12 +55,12 @@ int i2c_nrfx_twi_configure(const struct device *dev, uint32_t dev_config) 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); } @@ -69,7 +69,7 @@ int i2c_nrfx_twi_msg_transfer(const struct device *dev, uint8_t flags, 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 = { @@ -107,7 +107,7 @@ int i2c_nrfx_twi_msg_transfer(const struct device *dev, uint8_t flags, } if (!ret) { - ret = nrfx_twi_xfer(&config->twi, &cur_xfer, xfer_flags); + ret = nrfx_twi_xfer(&data->twi, &cur_xfer, xfer_flags); } return ret; @@ -133,7 +133,7 @@ int twi_nrfx_pm_action(const struct device *dev, enum pm_device_action action) 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) { diff --git a/drivers/i2c/i2c_nrfx_twi_common.h b/drivers/i2c/i2c_nrfx_twi_common.h index a3e9847bab5b..6409a1c7f172 100644 --- a/drivers/i2c/i2c_nrfx_twi_common.h +++ b/drivers/i2c/i2c_nrfx_twi_common.h @@ -20,23 +20,22 @@ extern "C" { : 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)) 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: diff --git a/drivers/i2c/i2c_nrfx_twi_rtio.c b/drivers/i2c/i2c_nrfx_twi_rtio.c index e8374f317f47..44bdb3617d2d 100644 --- a/drivers/i2c/i2c_nrfx_twi_rtio.c +++ b/drivers/i2c/i2c_nrfx_twi_rtio.c @@ -18,7 +18,10 @@ #include LOG_MODULE_REGISTER(i2c_nrfx_twi, CONFIG_I2C_LOG_LEVEL); +#define DT_DRV_COMPAT nordic_nrf_twi + struct i2c_nrfx_twi_rtio_data { + nrfx_twi_t twi; uint32_t dev_config; bool twi_enabled; struct i2c_rtio *ctx; @@ -37,7 +40,7 @@ static void i2c_nrfx_twi_rtio_complete(const struct device *dev, int status); static bool i2c_nrfx_twi_rtio_msg_start(const struct device *dev, uint8_t flags, uint8_t *buf, size_t buf_len, uint16_t i2c_addr) { - const struct i2c_nrfx_twi_config *config = dev->config; + struct i2c_nrfx_twi_common_data *data = dev->data; struct i2c_nrfx_twi_rtio_data *const dev_data = dev->data; struct i2c_rtio *ctx = dev_data->ctx; int ret = 0; @@ -46,13 +49,13 @@ static bool i2c_nrfx_twi_rtio_msg_start(const struct device *dev, uint8_t flags, /** Enabling while already enabled ends up in a failed assertion: skip it. */ if (!dev_data->twi_enabled) { - nrfx_twi_enable(&config->twi); + nrfx_twi_enable(&data->twi); dev_data->twi_enabled = true; } ret = i2c_nrfx_twi_msg_transfer(dev, flags, buf, buf_len, i2c_addr, more_msgs); if (ret != 0) { - nrfx_twi_disable(&config->twi); + nrfx_twi_disable(&data->twi); dev_data->twi_enabled = false; return i2c_rtio_complete(ctx, ret); @@ -112,14 +115,13 @@ static bool i2c_nrfx_twi_rtio_start(const struct device *dev) static void i2c_nrfx_twi_rtio_complete(const struct device *dev, int status) { /** Finalize if there are no more pending xfers */ - const struct i2c_nrfx_twi_config *config = dev->config; struct i2c_nrfx_twi_rtio_data *data = dev->data; struct i2c_rtio *const ctx = data->ctx; if (i2c_rtio_complete(ctx, status)) { (void)i2c_nrfx_twi_rtio_start(dev); } else { - nrfx_twi_disable(&config->twi); + nrfx_twi_disable(&data->twi); data->twi_enabled = false; } } @@ -149,7 +151,7 @@ static int i2c_nrfx_twi_rtio_recover_bus(const struct device *dev) return i2c_rtio_recover(ctx); } -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; int status = 0; @@ -178,56 +180,50 @@ static DEVICE_API(i2c, i2c_nrfx_twi_rtio_driver_api) = { .iodev_submit = i2c_nrfx_twi_rtio_submit, }; -#define I2C_NRFX_TWI_RTIO_DEVICE(idx) \ - NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(I2C(idx)); \ - BUILD_ASSERT(I2C_FREQUENCY(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; \ - const struct i2c_nrfx_twi_rtio_data *dev_data = dev->data; \ - int err = pinctrl_apply_state(config->pcfg, \ - PINCTRL_STATE_DEFAULT); \ - if (err < 0) { \ - return err; \ - } \ - i2c_rtio_init(dev_data->ctx, dev); \ - return i2c_nrfx_twi_init(dev); \ - } \ - I2C_RTIO_DEFINE(_i2c##idx##_twi_rtio, \ - DT_INST_PROP_OR(n, sq_size, CONFIG_I2C_RTIO_SQ_SIZE), \ - DT_INST_PROP_OR(n, cq_size, CONFIG_I2C_RTIO_CQ_SIZE)); \ - static struct i2c_nrfx_twi_rtio_data twi_##idx##_data = { \ - .ctx = &_i2c##idx##_twi_rtio, \ - }; \ - 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(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_rtio_driver_api) - -#ifdef CONFIG_HAS_HW_NRF_TWI0 -I2C_NRFX_TWI_RTIO_DEVICE(0); -#endif - -#ifdef CONFIG_HAS_HW_NRF_TWI1 -I2C_NRFX_TWI_RTIO_DEVICE(1); -#endif +#define I2C_NRFX_TWI_RTIO_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_rtio_data twi_##idx##_data = { \ + .twi = NRFX_TWI_INSTANCE(DT_INST_REG_ADDR(idx)), \ + .ctx = &_i2c##idx##_twi_rtio, \ + }; \ + 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; \ + const struct i2c_nrfx_twi_rtio_data *dev_data = dev->data; \ + int err = pinctrl_apply_state(config->pcfg, \ + PINCTRL_STATE_DEFAULT); \ + if (err < 0) { \ + return err; \ + } \ + i2c_rtio_init(dev_data->ctx, dev); \ + return i2c_nrfx_twi_init(dev); \ + } \ + I2C_RTIO_DEFINE(_i2c##idx##_twi_rtio, \ + DT_INST_PROP_OR(n, sq_size, CONFIG_I2C_RTIO_SQ_SIZE), \ + DT_INST_PROP_OR(n, cq_size, CONFIG_I2C_RTIO_CQ_SIZE)); \ + 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_rtio_driver_api) + +DT_INST_FOREACH_STATUS_OKAY(I2C_NRFX_TWI_RTIO_DEVICE) diff --git a/modules/hal_nordic/nrfx/Kconfig b/modules/hal_nordic/nrfx/Kconfig index c5a5a5f95279..c89834ca1c2f 100644 --- a/modules/hal_nordic/nrfx/Kconfig +++ b/modules/hal_nordic/nrfx/Kconfig @@ -301,17 +301,7 @@ config NRFX_TIMER bool "TIMER driver" config NRFX_TWI - bool - -config NRFX_TWI0 - bool "TWI0 driver instance" - depends on $(dt_nodelabel_exists,i2c0) && (SOC_SERIES_NRF51X || SOC_SERIES_NRF52X) - select NRFX_TWI - -config NRFX_TWI1 - bool "TWI1 driver instance" - depends on $(dt_nodelabel_exists,i2c1) && (SOC_SERIES_NRF51X || SOC_SERIES_NRF52X) - select NRFX_TWI + bool "NRFX driver for TWI peripheral" config NRFX_TWIM bool "NRFX driver for TWIM peripheral" diff --git a/modules/hal_nordic/nrfx/nrfx_kconfig.h b/modules/hal_nordic/nrfx/nrfx_kconfig.h index 20a5fe671d34..5e624416463c 100644 --- a/modules/hal_nordic/nrfx/nrfx_kconfig.h +++ b/modules/hal_nordic/nrfx/nrfx_kconfig.h @@ -380,12 +380,6 @@ #ifdef CONFIG_NRFX_TWI_LOG #define NRFX_TWI_CONFIG_LOG_ENABLED 1 #endif -#ifdef CONFIG_NRFX_TWI0 -#define NRFX_TWI0_ENABLED 1 -#endif -#ifdef CONFIG_NRFX_TWI1 -#define NRFX_TWI1_ENABLED 1 -#endif #ifdef CONFIG_NRFX_TWIM #define NRFX_TWIM_ENABLED 1 diff --git a/soc/nordic/common/Kconfig.peripherals b/soc/nordic/common/Kconfig.peripherals index f15702fa291b..284de4bb4935 100644 --- a/soc/nordic/common/Kconfig.peripherals +++ b/soc/nordic/common/Kconfig.peripherals @@ -294,12 +294,6 @@ config HAS_HW_NRF_TIMER136 config HAS_HW_NRF_TIMER137 def_bool $(dt_nodelabel_enabled_with_compat,timer137,$(DT_COMPAT_NORDIC_NRF_TIMER)) -config HAS_HW_NRF_TWI0 - def_bool $(dt_nodelabel_enabled_with_compat,i2c0,$(DT_COMPAT_NORDIC_NRF_TWI)) - -config HAS_HW_NRF_TWI1 - def_bool $(dt_nodelabel_enabled_with_compat,i2c1,$(DT_COMPAT_NORDIC_NRF_TWI)) - config HAS_HW_NRF_UART0 def_bool $(dt_nodelabel_enabled_with_compat,uart0,$(DT_COMPAT_NORDIC_NRF_UART)) From c1881f5a7af439113b2ab877d3c82398fcea13b0 Mon Sep 17 00:00:00 2001 From: Adam Kondraciuk Date: Fri, 21 Nov 2025 17:01:36 +0100 Subject: [PATCH 2/2] drivers: spi: spi_nrfx_spi: use standard instantiation Switched nrfx_spi API to standard instantiation. Signed-off-by: Adam Kondraciuk --- drivers/spi/Kconfig.nrfx | 4 +- drivers/spi/spi_nrfx_spi.c | 74 +++++++++++--------------- modules/hal_nordic/nrfx/Kconfig | 17 +----- modules/hal_nordic/nrfx/nrfx_kconfig.h | 9 ---- 4 files changed, 33 insertions(+), 71 deletions(-) diff --git a/drivers/spi/Kconfig.nrfx b/drivers/spi/Kconfig.nrfx index 990f044574ba..b391b919beff 100644 --- a/drivers/spi/Kconfig.nrfx +++ b/drivers/spi/Kconfig.nrfx @@ -15,9 +15,7 @@ if SPI_NRFX config SPI_NRFX_SPI def_bool y depends on DT_HAS_NORDIC_NRF_SPI_ENABLED - select NRFX_SPI0 if HAS_HW_NRF_SPI0 - select NRFX_SPI1 if HAS_HW_NRF_SPI1 - select NRFX_SPI2 if HAS_HW_NRF_SPI2 + select NRFX_SPI config SPI_NRFX_SPIM def_bool y diff --git a/drivers/spi/spi_nrfx_spi.c b/drivers/spi/spi_nrfx_spi.c index 10badb033d97..42c23f68c506 100644 --- a/drivers/spi/spi_nrfx_spi.c +++ b/drivers/spi/spi_nrfx_spi.c @@ -15,10 +15,13 @@ #include LOG_MODULE_REGISTER(spi_nrfx_spi, CONFIG_SPI_LOG_LEVEL); +#define DT_DRV_COMPAT nordic_nrf_spi + #include "spi_context.h" #include "spi_nrfx_common.h" struct spi_nrfx_data { + nrfx_spi_t spi; struct spi_context ctx; const struct device *dev; size_t chunk_len; @@ -27,7 +30,6 @@ struct spi_nrfx_data { }; struct spi_nrfx_config { - nrfx_spi_t spi; nrfx_spi_config_t def_config; void (*irq_connect)(void); const struct pinctrl_dev_config *pcfg; @@ -35,7 +37,7 @@ struct spi_nrfx_config { uint32_t wake_pin; }; -static void event_handler(const nrfx_spi_evt_t *p_event, void *p_context); +static void event_handler(const nrfx_spi_event_t *p_event, void *p_context); static inline nrf_spi_frequency_t get_nrf_spi_frequency(uint32_t frequency) { @@ -143,11 +145,11 @@ static int configure(const struct device *dev, } if (dev_data->initialized) { - nrfx_spi_uninit(&dev_config->spi); + nrfx_spi_uninit(&dev_data->spi); dev_data->initialized = false; } - result = nrfx_spi_init(&dev_config->spi, &config, + result = nrfx_spi_init(&dev_data->spi, &config, event_handler, dev_data); if (result != 0) { LOG_ERR("Failed to initialize nrfx driver: %d", result); @@ -174,7 +176,6 @@ static void finish_transaction(const struct device *dev, int error) static void transfer_next_chunk(const struct device *dev) { - const struct spi_nrfx_config *dev_config = dev->config; struct spi_nrfx_data *dev_data = dev->data; struct spi_context *ctx = &dev_data->ctx; int error = 0; @@ -190,7 +191,7 @@ static void transfer_next_chunk(const struct device *dev) xfer.tx_length = spi_context_tx_buf_on(ctx) ? chunk_len : 0; xfer.p_rx_buffer = ctx->rx_buf; xfer.rx_length = spi_context_rx_buf_on(ctx) ? chunk_len : 0; - error = nrfx_spi_xfer(&dev_config->spi, &xfer, 0); + error = nrfx_spi_xfer(&dev_data->spi, &xfer, 0); if (error == 0) { return; } @@ -201,7 +202,7 @@ static void transfer_next_chunk(const struct device *dev) finish_transaction(dev, error); } -static void event_handler(const nrfx_spi_evt_t *p_event, void *p_context) +static void event_handler(const nrfx_spi_event_t *p_event, void *p_context) { struct spi_nrfx_data *dev_data = p_context; @@ -267,7 +268,7 @@ static int transceive(const struct device *dev, /* Abort the current transfer by deinitializing * the nrfx driver. */ - nrfx_spi_uninit(&dev_config->spi); + nrfx_spi_uninit(&dev_data->spi); dev_data->initialized = false; /* Make sure the transaction is finished (it may be @@ -363,7 +364,7 @@ static int spi_nrfx_pm_action(const struct device *dev, case PM_DEVICE_ACTION_SUSPEND: if (dev_data->initialized) { - nrfx_spi_uninit(&dev_config->spi); + nrfx_spi_uninit(&dev_data->spi); dev_data->initialized = false; } @@ -424,31 +425,27 @@ static int spi_nrfx_init(const struct device *dev) * - Name-based HAL IRQ handlers, e.g. nrfx_spi_0_irq_handler */ -#define SPI(idx) DT_NODELABEL(spi##idx) -#define SPI_PROP(idx, prop) DT_PROP(SPI(idx), prop) +#define SPI_PROP(idx, prop) DT_INST_PROP(idx, prop) #define SPI_NRFX_SPI_DEFINE(idx) \ - NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(SPI(idx)); \ - static void irq_connect##idx(void) \ - { \ - IRQ_CONNECT(DT_IRQN(SPI(idx)), DT_IRQ(SPI(idx), priority), \ - nrfx_isr, nrfx_spi_##idx##_irq_handler, 0); \ - } \ + NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(DT_DRV_INST(idx)); \ static struct spi_nrfx_data spi_##idx##_data = { \ IF_ENABLED(CONFIG_MULTITHREADING, \ (SPI_CONTEXT_INIT_LOCK(spi_##idx##_data, ctx),)) \ IF_ENABLED(CONFIG_MULTITHREADING, \ (SPI_CONTEXT_INIT_SYNC(spi_##idx##_data, ctx),)) \ - SPI_CONTEXT_CS_GPIOS_INITIALIZE(SPI(idx), ctx) \ - .dev = DEVICE_DT_GET(SPI(idx)), \ + SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(idx), ctx) \ + .spi = NRFX_SPI_INSTANCE(DT_INST_REG_ADDR(idx)), \ + .dev = DEVICE_DT_INST_GET(idx), \ .busy = false, \ }; \ - PINCTRL_DT_DEFINE(SPI(idx)); \ + static void irq_connect##idx(void) \ + { \ + IRQ_CONNECT(DT_INST_IRQN(idx), DT_INST_IRQ(idx, priority), \ + nrfx_spi_irq_handler, &spi_##idx##_data.spi, 0); \ + } \ + PINCTRL_DT_INST_DEFINE(idx); \ static const struct spi_nrfx_config spi_##idx##z_config = { \ - .spi = { \ - .p_reg = (NRF_SPI_Type *)DT_REG_ADDR(SPI(idx)), \ - .drv_inst_idx = NRFX_SPI##idx##_INST_IDX, \ - }, \ .def_config = { \ .skip_gpio_cfg = true, \ .skip_psel_cfg = true, \ @@ -456,31 +453,22 @@ static int spi_nrfx_init(const struct device *dev) .orc = SPI_PROP(idx, overrun_character), \ }, \ .irq_connect = irq_connect##idx, \ - .pcfg = PINCTRL_DT_DEV_CONFIG_GET(SPI(idx)), \ - .wake_gpiote = WAKE_GPIOTE_NODE(SPI(idx)), \ - .wake_pin = NRF_DT_GPIOS_TO_PSEL_OR(SPI(idx), wake_gpios, \ - WAKE_PIN_NOT_USED), \ + .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(idx), \ + .wake_gpiote = WAKE_GPIOTE_NODE(DT_DRV_INST(idx)), \ + .wake_pin = NRF_DT_GPIOS_TO_PSEL_OR(DT_DRV_INST(idx), \ + wake_gpios, WAKE_PIN_NOT_USED),\ }; \ - BUILD_ASSERT(!DT_NODE_HAS_PROP(SPI(idx), wake_gpios) || \ - !(DT_GPIO_FLAGS(SPI(idx), wake_gpios) & GPIO_ACTIVE_LOW), \ + BUILD_ASSERT(!DT_NODE_HAS_PROP(DT_DRV_INST(idx), wake_gpios) || \ + !(DT_INST_GPIO_FLAGS(idx, wake_gpios) & GPIO_ACTIVE_LOW), \ "WAKE line must be configured as active high"); \ - PM_DEVICE_DT_DEFINE(SPI(idx), spi_nrfx_pm_action); \ - SPI_DEVICE_DT_DEFINE(SPI(idx), \ + PM_DEVICE_DT_INST_DEFINE(idx, spi_nrfx_pm_action); \ + SPI_DEVICE_DT_INST_DEFINE(idx, \ spi_nrfx_init, \ - PM_DEVICE_DT_GET(SPI(idx)), \ + PM_DEVICE_DT_INST_GET(idx), \ &spi_##idx##_data, \ &spi_##idx##z_config, \ POST_KERNEL, CONFIG_SPI_INIT_PRIORITY, \ &spi_nrfx_driver_api) -#ifdef CONFIG_HAS_HW_NRF_SPI0 -SPI_NRFX_SPI_DEFINE(0); -#endif - -#ifdef CONFIG_HAS_HW_NRF_SPI1 -SPI_NRFX_SPI_DEFINE(1); -#endif +DT_INST_FOREACH_STATUS_OKAY(SPI_NRFX_SPI_DEFINE) -#ifdef CONFIG_HAS_HW_NRF_SPI2 -SPI_NRFX_SPI_DEFINE(2); -#endif diff --git a/modules/hal_nordic/nrfx/Kconfig b/modules/hal_nordic/nrfx/Kconfig index c89834ca1c2f..205394de49e9 100644 --- a/modules/hal_nordic/nrfx/Kconfig +++ b/modules/hal_nordic/nrfx/Kconfig @@ -256,22 +256,7 @@ config NRFX_SAADC depends on $(dt_nodelabel_exists,adc) && !SOC_SERIES_NRF51X config NRFX_SPI - bool - -config NRFX_SPI0 - bool "SPI0 driver instance" - depends on $(dt_nodelabel_exists,spi0) && (SOC_SERIES_NRF51X || SOC_SERIES_NRF52X) - select NRFX_SPI - -config NRFX_SPI1 - bool "SPI1 driver instance" - depends on $(dt_nodelabel_exists,spi1) && (SOC_SERIES_NRF51X || SOC_SERIES_NRF52X) - select NRFX_SPI - -config NRFX_SPI2 - bool "SPI2 driver instance" - depends on $(dt_nodelabel_exists,spi2) && SOC_SERIES_NRF52X - select NRFX_SPI + bool "SPI driver" DT_COMPAT_NORDIC_NRF_SPIM := nordic,nrf-spim diff --git a/modules/hal_nordic/nrfx/nrfx_kconfig.h b/modules/hal_nordic/nrfx/nrfx_kconfig.h index 5e624416463c..c34956ce30fc 100644 --- a/modules/hal_nordic/nrfx/nrfx_kconfig.h +++ b/modules/hal_nordic/nrfx/nrfx_kconfig.h @@ -314,15 +314,6 @@ #ifdef CONFIG_NRFX_SPI_LOG #define NRFX_SPI_CONFIG_LOG_ENABLED 1 #endif -#ifdef CONFIG_NRFX_SPI0 -#define NRFX_SPI0_ENABLED 1 -#endif -#ifdef CONFIG_NRFX_SPI1 -#define NRFX_SPI1_ENABLED 1 -#endif -#ifdef CONFIG_NRFX_SPI2 -#define NRFX_SPI2_ENABLED 1 -#endif #ifdef CONFIG_NRFX_SPIM #define NRFX_SPIM_ENABLED 1