1717#include <zephyr/irq.h>
1818LOG_MODULE_REGISTER (i2c_nrfx_twi , CONFIG_I2C_LOG_LEVEL );
1919
20+ #define DT_DRV_COMPAT nordic_nrf_twi
21+
2022#if CONFIG_I2C_NRFX_TRANSFER_TIMEOUT
2123#define I2C_TRANSFER_TIMEOUT_MSEC K_MSEC(CONFIG_I2C_NRFX_TRANSFER_TIMEOUT)
2224#else
2325#define I2C_TRANSFER_TIMEOUT_MSEC K_FOREVER
2426#endif
2527
2628struct i2c_nrfx_twi_data {
29+ nrfx_twi_t twi ;
2730 uint32_t dev_config ;
2831 struct k_sem transfer_sync ;
2932 struct k_sem completion_sync ;
@@ -42,7 +45,6 @@ static int i2c_nrfx_twi_transfer(const struct device *dev,
4245 struct i2c_msg * msgs ,
4346 uint8_t num_msgs , uint16_t addr )
4447{
45- const struct i2c_nrfx_twi_config * config = dev -> config ;
4648 struct i2c_nrfx_twi_data * data = dev -> data ;
4749 int ret = 0 ;
4850
@@ -51,11 +53,11 @@ static int i2c_nrfx_twi_transfer(const struct device *dev,
5153 /* Dummy take on completion_sync sem to be sure that it is empty */
5254 k_sem_take (& data -> completion_sync , K_NO_WAIT );
5355
54- nrfx_twi_enable (& config -> twi );
56+ nrfx_twi_enable (& data -> twi );
5557
5658 for (size_t i = 0 ; i < num_msgs ; i ++ ) {
5759 bool more_msgs = ((i < (num_msgs - 1 )) &&
58- !(msgs [i + 1 ].flags & I2C_MSG_RESTART ));
60+ !(msgs [i + 1 ].flags & I2C_MSG_RESTART ));
5961
6062 ret = i2c_nrfx_twi_msg_transfer (dev , msgs [i ].flags ,
6163 msgs [i ].buf ,
@@ -84,7 +86,7 @@ static int i2c_nrfx_twi_transfer(const struct device *dev,
8486 * to make sure everything has been done to restore the
8587 * bus from this error.
8688 */
87- nrfx_twi_disable (& config -> twi );
89+ nrfx_twi_disable (& data -> twi );
8890 (void )i2c_nrfx_twi_recover_bus (dev );
8991 ret = - EIO ;
9092 break ;
@@ -96,13 +98,13 @@ static int i2c_nrfx_twi_transfer(const struct device *dev,
9698 }
9799 }
98100
99- nrfx_twi_disable (& config -> twi );
101+ nrfx_twi_disable (& data -> twi );
100102 k_sem_give (& data -> transfer_sync );
101103
102104 return ret ;
103105}
104106
105- static void event_handler (nrfx_twi_evt_t const * p_event , void * p_context )
107+ static void event_handler (nrfx_twi_event_t const * p_event , void * p_context )
106108{
107109 const struct device * dev = p_context ;
108110 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) = {
131133 .recover_bus = i2c_nrfx_twi_recover_bus ,
132134};
133135
134- #define I2C_NRFX_TWI_DEVICE (idx ) \
135- NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(I2C(idx)); \
136- BUILD_ASSERT(I2C_FREQUENCY(I2C(idx)) != I2C_NRFX_TWI_INVALID_FREQUENCY, \
137- "Wrong I2C " #idx " frequency setting in dts"); \
138- static int twi_##idx##_init(const struct device *dev) \
139- { \
140- IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority), nrfx_isr, \
141- nrfx_twi_##idx##_irq_handler, 0); \
142- const struct i2c_nrfx_twi_config *config = dev->config; \
143- int err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); \
144- if (err < 0) { \
145- return err; \
146- } \
147- return i2c_nrfx_twi_init(dev); \
148- } \
149- static struct i2c_nrfx_twi_data twi_##idx##_data = { \
150- .transfer_sync = Z_SEM_INITIALIZER(twi_##idx##_data.transfer_sync, 1, 1), \
151- .completion_sync = Z_SEM_INITIALIZER(twi_##idx##_data.completion_sync, 0, 1)}; \
152- PINCTRL_DT_DEFINE(I2C(idx)); \
153- static const struct i2c_nrfx_twi_config twi_##idx##z_config = { \
154- .twi = NRFX_TWI_INSTANCE(idx), \
155- .config = \
156- { \
157- .skip_gpio_cfg = true, \
158- .skip_psel_cfg = true, \
159- .frequency = I2C_FREQUENCY(I2C(idx)), \
160- }, \
161- .event_handler = event_handler, \
162- .pcfg = PINCTRL_DT_DEV_CONFIG_GET(I2C(idx)), \
163- }; \
164- PM_DEVICE_DT_DEFINE(I2C(idx), twi_nrfx_pm_action); \
165- I2C_DEVICE_DT_DEFINE(I2C(idx), twi_##idx##_init, PM_DEVICE_DT_GET(I2C(idx)), \
166- &twi_##idx##_data, &twi_##idx##z_config, POST_KERNEL, \
167- CONFIG_I2C_INIT_PRIORITY, &i2c_nrfx_twi_driver_api)
168-
169- #ifdef CONFIG_HAS_HW_NRF_TWI0
170- I2C_NRFX_TWI_DEVICE (0 );
171- #endif
172-
173- #ifdef CONFIG_HAS_HW_NRF_TWI1
174- I2C_NRFX_TWI_DEVICE (1 );
175- #endif
136+ #define I2C_NRFX_TWI_DEVICE (idx ) \
137+ NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(DT_DRV_INST(idx)); \
138+ BUILD_ASSERT(I2C_FREQUENCY(DT_DRV_INST(idx)) != I2C_NRFX_TWI_INVALID_FREQUENCY, \
139+ "Wrong I2C " #idx " frequency setting in dts"); \
140+ static struct i2c_nrfx_twi_data twi_##idx##_data = { \
141+ .twi = NRFX_TWI_INSTANCE(DT_INST_REG_ADDR(idx)), \
142+ .transfer_sync = Z_SEM_INITIALIZER(twi_##idx##_data.transfer_sync, 1, 1), \
143+ .completion_sync = Z_SEM_INITIALIZER(twi_##idx##_data.completion_sync, 0, 1) \
144+ }; \
145+ static int twi_##idx##_init(const struct device *dev) \
146+ { \
147+ IRQ_CONNECT(DT_INST_IRQN(idx), DT_INST_IRQ(idx, priority), nrfx_twi_irq_handler, \
148+ &twi_##idx##_data.twi, 0); \
149+ const struct i2c_nrfx_twi_config *config = dev->config; \
150+ int err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); \
151+ if (err < 0) { \
152+ return err; \
153+ } \
154+ return i2c_nrfx_twi_init(dev); \
155+ } \
156+ PINCTRL_DT_INST_DEFINE(idx); \
157+ static const struct i2c_nrfx_twi_config twi_##idx##z_config = { \
158+ .config = \
159+ { \
160+ .skip_gpio_cfg = true, \
161+ .skip_psel_cfg = true, \
162+ .frequency = I2C_FREQUENCY(DT_DRV_INST(idx)), \
163+ }, \
164+ .event_handler = event_handler, \
165+ .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(idx), \
166+ }; \
167+ PM_DEVICE_DT_INST_DEFINE(idx, twi_nrfx_pm_action); \
168+ I2C_DEVICE_DT_INST_DEFINE(idx, twi_##idx##_init, PM_DEVICE_DT_INST_GET(idx), \
169+ &twi_##idx##_data, &twi_##idx##z_config, POST_KERNEL, \
170+ CONFIG_I2C_INIT_PRIORITY, &i2c_nrfx_twi_driver_api)
171+
172+ DT_INST_FOREACH_STATUS_OKAY (I2C_NRFX_TWI_DEVICE )
0 commit comments