Skip to content

Commit a8e60dd

Browse files
drivers: i2c: i2c_nrfx_twi: use standard instantiation
Switched nrfx_twi API to standard instantiation. Signed-off-by: Adam Kondraciuk <adam.kondraciuk@nordicsemi.no>
1 parent 906af45 commit a8e60dd

File tree

4 files changed

+118
-121
lines changed

4 files changed

+118
-121
lines changed

drivers/i2c/i2c_nrfx_twi.c

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <zephyr/irq.h>
1818
LOG_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)

drivers/i2c/i2c_nrfx_twi_common.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ LOG_MODULE_DECLARE(i2c_nrfx_twi);
1616
int i2c_nrfx_twi_init(const struct device *dev)
1717
{
1818
const struct i2c_nrfx_twi_config *config = dev->config;
19-
int result = nrfx_twi_init(&config->twi, &config->config,
19+
struct i2c_nrfx_twi_common_data *data = dev->data;
20+
int result = nrfx_twi_init(&data->twi, &config->config,
2021
config->event_handler, (void *)dev);
2122
if (result != 0) {
2223
LOG_ERR("Failed to initialize device: %s",
@@ -29,9 +30,8 @@ int i2c_nrfx_twi_init(const struct device *dev)
2930

3031
int i2c_nrfx_twi_configure(const struct device *dev, uint32_t dev_config)
3132
{
32-
const struct i2c_nrfx_twi_config *config = dev->config;
3333
struct i2c_nrfx_twi_common_data *data = dev->data;
34-
nrfx_twi_t const *inst = &config->twi;
34+
nrfx_twi_t const *inst = &data->twi;
3535

3636
if (I2C_ADDR_10_BITS & dev_config) {
3737
return -EINVAL;
@@ -55,12 +55,12 @@ int i2c_nrfx_twi_configure(const struct device *dev, uint32_t dev_config)
5555

5656
int i2c_nrfx_twi_recover_bus(const struct device *dev)
5757
{
58-
const struct i2c_nrfx_twi_config *config = dev->config;
58+
struct i2c_nrfx_twi_common_data *data = dev->data;
5959
uint32_t scl_pin;
6060
uint32_t sda_pin;
6161

62-
scl_pin = nrf_twi_scl_pin_get(config->twi.p_twi);
63-
sda_pin = nrf_twi_sda_pin_get(config->twi.p_twi);
62+
scl_pin = nrf_twi_scl_pin_get(data->twi.p_twi);
63+
sda_pin = nrf_twi_sda_pin_get(data->twi.p_twi);
6464

6565
return nrfx_twi_bus_recover(scl_pin, sda_pin);
6666
}
@@ -69,7 +69,7 @@ int i2c_nrfx_twi_msg_transfer(const struct device *dev, uint8_t flags,
6969
uint8_t *buf, size_t buf_len,
7070
uint16_t i2c_addr, bool more_msgs)
7171
{
72-
const struct i2c_nrfx_twi_config *config = dev->config;
72+
struct i2c_nrfx_twi_common_data *data = dev->data;
7373
int ret = 0;
7474
uint32_t xfer_flags = 0;
7575
nrfx_twi_xfer_desc_t cur_xfer = {
@@ -107,7 +107,7 @@ int i2c_nrfx_twi_msg_transfer(const struct device *dev, uint8_t flags,
107107
}
108108

109109
if (!ret) {
110-
ret = nrfx_twi_xfer(&config->twi, &cur_xfer, xfer_flags);
110+
ret = nrfx_twi_xfer(&data->twi, &cur_xfer, xfer_flags);
111111
}
112112

113113
return ret;
@@ -133,7 +133,7 @@ int twi_nrfx_pm_action(const struct device *dev, enum pm_device_action action)
133133
break;
134134

135135
case PM_DEVICE_ACTION_SUSPEND:
136-
nrfx_twi_uninit(&config->twi);
136+
nrfx_twi_uninit(&data->twi);
137137

138138
ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
139139
if (ret < 0) {

drivers/i2c/i2c_nrfx_twi_common.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,21 @@ extern "C" {
2121
: bitrate == I2C_BITRATE_FAST ? NRF_TWI_FREQ_400K \
2222
: I2C_NRFX_TWI_INVALID_FREQUENCY)
2323
#define I2C(idx) DT_NODELABEL(i2c##idx)
24-
#define I2C_FREQUENCY(idx) \
25-
I2C_NRFX_TWI_FREQUENCY(DT_PROP_OR(I2C(idx), clock_frequency, \
26-
I2C_BITRATE_STANDARD))
24+
#define I2C_FREQUENCY(node) \
25+
I2C_NRFX_TWI_FREQUENCY(DT_PROP_OR(node, clock_frequency, I2C_BITRATE_STANDARD))
2726

2827
struct i2c_nrfx_twi_common_data {
28+
nrfx_twi_t twi;
2929
uint32_t dev_config;
3030
};
3131

3232
struct i2c_nrfx_twi_config {
33-
nrfx_twi_t twi;
3433
nrfx_twi_config_t config;
3534
nrfx_twi_evt_handler_t event_handler;
3635
const struct pinctrl_dev_config *pcfg;
3736
};
3837

39-
static inline nrfx_err_t i2c_nrfx_twi_get_evt_result(nrfx_twi_evt_t const *p_event)
38+
static inline nrfx_err_t i2c_nrfx_twi_get_evt_result(nrfx_twi_event_t const *p_event)
4039
{
4140
switch (p_event->type) {
4241
case NRFX_TWI_EVT_DONE:

0 commit comments

Comments
 (0)