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
@@ -42,7 +44,6 @@ static int i2c_nrfx_twi_transfer(const struct device *dev,
4244 struct i2c_msg * msgs ,
4345 uint8_t num_msgs , uint16_t addr )
4446{
45- const struct i2c_nrfx_twi_config * config = dev -> config ;
4647 struct i2c_nrfx_twi_data * data = dev -> data ;
4748 int ret = 0 ;
4849
@@ -51,11 +52,11 @@ static int i2c_nrfx_twi_transfer(const struct device *dev,
5152 /* Dummy take on completion_sync sem to be sure that it is empty */
5253 k_sem_take (& data -> completion_sync , K_NO_WAIT );
5354
54- nrfx_twi_enable (& config -> twi );
55+ nrfx_twi_enable (& data -> twi );
5556
5657 for (size_t i = 0 ; i < num_msgs ; i ++ ) {
5758 bool more_msgs = ((i < (num_msgs - 1 )) &&
58- !(msgs [i + 1 ].flags & I2C_MSG_RESTART ));
59+ !(msgs [i + 1 ].flags & I2C_MSG_RESTART ));
5960
6061 ret = i2c_nrfx_twi_msg_transfer (dev , msgs [i ].flags ,
6162 msgs [i ].buf ,
@@ -84,7 +85,7 @@ static int i2c_nrfx_twi_transfer(const struct device *dev,
8485 * to make sure everything has been done to restore the
8586 * bus from this error.
8687 */
87- nrfx_twi_disable (& config -> twi );
88+ nrfx_twi_disable (& data -> twi );
8889 (void )i2c_nrfx_twi_recover_bus (dev );
8990 ret = - EIO ;
9091 break ;
@@ -96,13 +97,13 @@ static int i2c_nrfx_twi_transfer(const struct device *dev,
9697 }
9798 }
9899
99- nrfx_twi_disable (& config -> twi );
100+ nrfx_twi_disable (& data -> twi );
100101 k_sem_give (& data -> transfer_sync );
101102
102103 return ret ;
103104}
104105
105- static void event_handler (nrfx_twi_evt_t const * p_event , void * p_context )
106+ static void event_handler (nrfx_twi_event_t const * p_event , void * p_context )
106107{
107108 const struct device * dev = p_context ;
108109 struct i2c_nrfx_twi_data * dev_data = (struct i2c_nrfx_twi_data * )dev -> data ;
@@ -131,45 +132,43 @@ static DEVICE_API(i2c, i2c_nrfx_twi_driver_api) = {
131132 .recover_bus = i2c_nrfx_twi_recover_bus ,
132133};
133134
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
135+ #define I2C_NRFX_TWI_DEVICE (idx ) \
136+ NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(DT_DRV_INST(idx)); \
137+ BUILD_ASSERT(I2C_FREQUENCY(DT_DRV_INST(idx)) != I2C_NRFX_TWI_INVALID_FREQUENCY, \
138+ "Wrong I2C " #idx " frequency setting in dts"); \
139+ static struct i2c_nrfx_twi_data twi_##idx##_data = { \
140+ .twi = \
141+ { \
142+ .p_twi = (NRF_TWI_Type *)DT_INST_REG_ADDR(idx); \
143+ }, \
144+ .transfer_sync = Z_SEM_INITIALIZER(twi_##idx##_data.transfer_sync, 1, 1), \
145+ .completion_sync = Z_SEM_INITIALIZER(twi_##idx##_data.completion_sync, 0, 1) \
146+ }; \
147+ static int twi_##idx##_init(const struct device *dev) \
148+ { \
149+ IRQ_CONNECT(DT_INST_IRQN(idx), DT_INST_IRQ(idx, priority), nrfx_twi_irq_handler, \
150+ &twi_##idx##_data.twi, 0); \
151+ const struct i2c_nrfx_twi_config *config = dev->config; \
152+ int err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); \
153+ if (err < 0) { \
154+ return err; \
155+ } \
156+ return i2c_nrfx_twi_init(dev); \
157+ } \
158+ PINCTRL_DT_INST_DEFINE(idx); \
159+ static const struct i2c_nrfx_twi_config twi_##idx##z_config = { \
160+ .config = \
161+ { \
162+ .skip_gpio_cfg = true, \
163+ .skip_psel_cfg = true, \
164+ .frequency = I2C_FREQUENCY(DT_DRV_INST(idx)), \
165+ }, \
166+ .event_handler = event_handler, \
167+ .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(idx), \
168+ }; \
169+ PM_DEVICE_DT_INST_DEFINE(idx, twi_nrfx_pm_action); \
170+ I2C_DEVICE_DT_INST_DEFINE(idx, twi_##idx##_init, PM_DEVICE_DT_INST_GET(idx), \
171+ &twi_##idx##_data, &twi_##idx##z_config, POST_KERNEL, \
172+ CONFIG_I2C_INIT_PRIORITY, &i2c_nrfx_twi_driver_api)
173+
174+ DT_INST_FOREACH_STATUS_OKAY (I2C_NRFX_TWI_DEVICE )
0 commit comments