Skip to content

Commit 10a91b1

Browse files
[nrf fromlist] drivers: i2c: i2c_nrfx_twim: use standard instantiation
Switched nrfx_twim API to standard instantiation. Upstream PR #: 98569 Signed-off-by: Adam Kondraciuk <[email protected]>
1 parent 284a3ad commit 10a91b1

File tree

5 files changed

+154
-299
lines changed

5 files changed

+154
-299
lines changed

drivers/i2c/i2c_nrfx_twi.c

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -131,49 +131,40 @@ static DEVICE_API(i2c, i2c_nrfx_twi_driver_api) = {
131131
.recover_bus = i2c_nrfx_twi_recover_bus,
132132
};
133133

134-
#define I2C_NRFX_TWI_DEVICE(idx) \
135-
NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(I2C(idx)); \
136-
BUILD_ASSERT(I2C_FREQUENCY(idx) != \
137-
I2C_NRFX_TWI_INVALID_FREQUENCY, \
138-
"Wrong I2C " #idx " frequency setting in dts"); \
139-
static int twi_##idx##_init(const struct device *dev) \
140-
{ \
141-
IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority), \
142-
nrfx_isr, nrfx_twi_##idx##_irq_handler, 0); \
143-
const struct i2c_nrfx_twi_config *config = dev->config; \
144-
int err = pinctrl_apply_state(config->pcfg, \
145-
PINCTRL_STATE_DEFAULT); \
146-
if (err < 0) { \
147-
return err; \
148-
} \
149-
return i2c_nrfx_twi_init(dev); \
150-
} \
151-
static struct i2c_nrfx_twi_data twi_##idx##_data = { \
152-
.transfer_sync = Z_SEM_INITIALIZER( \
153-
twi_##idx##_data.transfer_sync, 1, 1), \
154-
.completion_sync = Z_SEM_INITIALIZER( \
155-
twi_##idx##_data.completion_sync, 0, 1) \
156-
}; \
157-
PINCTRL_DT_DEFINE(I2C(idx)); \
158-
static const struct i2c_nrfx_twi_config twi_##idx##z_config = { \
159-
.twi = NRFX_TWI_INSTANCE(idx), \
160-
.config = { \
161-
.skip_gpio_cfg = true, \
162-
.skip_psel_cfg = true, \
163-
.frequency = I2C_FREQUENCY(idx), \
164-
}, \
165-
.event_handler = event_handler, \
166-
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(I2C(idx)), \
167-
}; \
168-
PM_DEVICE_DT_DEFINE(I2C(idx), twi_nrfx_pm_action); \
169-
I2C_DEVICE_DT_DEFINE(I2C(idx), \
170-
twi_##idx##_init, \
171-
PM_DEVICE_DT_GET(I2C(idx)), \
172-
&twi_##idx##_data, \
173-
&twi_##idx##z_config, \
174-
POST_KERNEL, \
175-
CONFIG_I2C_INIT_PRIORITY, \
176-
&i2c_nrfx_twi_driver_api)
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)
177168

178169
#ifdef CONFIG_HAS_HW_NRF_TWI0
179170
I2C_NRFX_TWI_DEVICE(0);

drivers/i2c/i2c_nrfx_twim.c

Lines changed: 51 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ LOG_MODULE_REGISTER(i2c_nrfx_twim, CONFIG_I2C_LOG_LEVEL);
2929
#endif
3030

3131
struct i2c_nrfx_twim_data {
32+
nrfx_twim_t twim;
3233
struct k_sem transfer_sync;
3334
struct k_sem completion_sync;
3435
volatile nrfx_err_t res;
@@ -81,7 +82,7 @@ static int i2c_nrfx_twim_transfer(const struct device *dev,
8182
break;
8283
}
8384

84-
bool dma_accessible = nrf_dma_accessible_check(&dev_config->twim, msgs[i].buf);
85+
bool dma_accessible = nrf_dma_accessible_check(&dev_data->twim, msgs[i].buf);
8586

8687
/* This fragment needs to be merged with the next one if:
8788
* - it is not the last fragment
@@ -191,7 +192,7 @@ static int i2c_nrfx_twim_transfer(const struct device *dev,
191192
return ret;
192193
}
193194

194-
static void event_handler(nrfx_twim_evt_t const *p_event, void *p_context)
195+
static void event_handler(nrfx_twim_event_t const *p_event, void *p_context)
195196
{
196197
const struct device *dev = p_context;
197198
struct i2c_nrfx_twim_data *dev_data = dev->data;
@@ -238,142 +239,73 @@ static DEVICE_API(i2c, i2c_nrfx_twim_driver_api) = {
238239
.recover_bus = i2c_nrfx_twim_recover_bus,
239240
};
240241

241-
#define CONCAT_BUF_SIZE(idx) \
242-
COND_CODE_1(DT_NODE_HAS_PROP(I2C(idx), zephyr_concat_buf_size), \
243-
(DT_PROP(I2C(idx), zephyr_concat_buf_size)), (0))
244-
#define FLASH_BUF_MAX_SIZE(idx) \
245-
COND_CODE_1(DT_NODE_HAS_PROP(I2C(idx), zephyr_flash_buf_max_size), \
246-
(DT_PROP(I2C(idx), zephyr_flash_buf_max_size)), (0))
242+
#if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_twim)
243+
#define DT_DRV_COMPAT nordic_nrf_twim
244+
#endif
245+
246+
#define CONCAT_BUF_SIZE(idx) \
247+
COND_CODE_1(DT_NODE_HAS_PROP(DT_DRV_INST(idx), zephyr_concat_buf_size), \
248+
(DT_INST_PROP(idx, zephyr_concat_buf_size)), (0))
249+
#define FLASH_BUF_MAX_SIZE(idx) \
250+
COND_CODE_1(DT_NODE_HAS_PROP(DT_DRV_INST(idx), zephyr_flash_buf_max_size), \
251+
(DT_INST_PROP(idx, zephyr_flash_buf_max_size)), (0))
247252

248-
#define USES_MSG_BUF(idx) \
249-
COND_CODE_0(CONCAT_BUF_SIZE(idx), \
250-
(COND_CODE_0(FLASH_BUF_MAX_SIZE(idx), (0), (1))), \
253+
#define USES_MSG_BUF(idx) \
254+
COND_CODE_0(CONCAT_BUF_SIZE(idx), \
255+
(COND_CODE_0(FLASH_BUF_MAX_SIZE(idx), (0), (1))), \
251256
(1))
252257
#define MSG_BUF_SIZE(idx) MAX(CONCAT_BUF_SIZE(idx), FLASH_BUF_MAX_SIZE(idx))
253258

254-
#define I2C_NRFX_TWIM_DEVICE(idx) \
255-
NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(I2C(idx)); \
256-
NRF_DT_CHECK_NODE_HAS_REQUIRED_MEMORY_REGIONS(I2C(idx)); \
257-
BUILD_ASSERT(I2C_FREQUENCY(idx) != \
258-
I2C_NRFX_TWIM_INVALID_FREQUENCY, \
259-
"Wrong I2C " #idx " frequency setting in dts"); \
260-
static void irq_connect##idx(void) \
261-
{ \
262-
IRQ_CONNECT(DT_IRQN(I2C(idx)), DT_IRQ(I2C(idx), priority), \
263-
nrfx_isr, nrfx_twim_##idx##_irq_handler, 0); \
264-
} \
259+
#define I2C_NRFX_TWIM_DEVICE(idx) \
260+
NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(DT_DRV_INST(idx)); \
261+
NRF_DT_CHECK_NODE_HAS_REQUIRED_MEMORY_REGIONS(DT_DRV_INST(idx)); \
262+
BUILD_ASSERT(I2C_FREQUENCY(DT_DRV_INST(idx)) != I2C_NRFX_TWIM_INVALID_FREQUENCY, \
263+
"Wrong I2C " #idx " frequency setting in dts"); \
264+
static struct i2c_nrfx_twim_data twim_##idx##_data; \
265+
static struct i2c_nrfx_twim_common_config twim_##idx##z_config; \
266+
static void pre_init##idx(void) \
267+
{ \
268+
twim_##idx##z_config.twim = &twim_##idx##_data.twim; \
269+
twim_##idx##_data.twim.p_twim = (NRF_TWIM_Type *)DT_INST_REG_ADDR(idx); \
270+
IRQ_CONNECT(DT_INST_IRQN(idx), DT_INST_IRQ(idx, priority), nrfx_twim_irq_handler, \
271+
&twim_##idx##_data.twim, 0); \
272+
} \
265273
IF_ENABLED(USES_MSG_BUF(idx), \
266274
(static uint8_t twim_##idx##_msg_buf[MSG_BUF_SIZE(idx)] \
267-
I2C_MEMORY_SECTION(idx);)) \
268-
static struct i2c_nrfx_twim_data twim_##idx##_data; \
269-
PINCTRL_DT_DEFINE(I2C(idx)); \
270-
static const \
271-
struct i2c_nrfx_twim_common_config twim_##idx##z_config = { \
272-
.twim = NRFX_TWIM_INSTANCE(idx), \
273-
.twim_config = { \
274-
.skip_gpio_cfg = true, \
275-
.skip_psel_cfg = true, \
276-
.frequency = I2C_FREQUENCY(idx), \
277-
}, \
278-
.event_handler = event_handler, \
279-
.msg_buf_size = MSG_BUF_SIZE(idx), \
280-
.irq_connect = irq_connect##idx, \
281-
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(I2C(idx)), \
275+
I2C_MEMORY_SECTION(idx);)) \
276+
PINCTRL_DT_INST_DEFINE(idx); \
277+
static struct i2c_nrfx_twim_common_config twim_##idx##z_config = { \
278+
.twim_config = \
279+
{ \
280+
.skip_gpio_cfg = true, \
281+
.skip_psel_cfg = true, \
282+
.frequency = I2C_FREQUENCY(DT_DRV_INST(idx)), \
283+
}, \
284+
.event_handler = event_handler, \
285+
.msg_buf_size = MSG_BUF_SIZE(idx), \
286+
.pre_init = pre_init##idx, \
287+
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(idx), \
282288
IF_ENABLED(USES_MSG_BUF(idx), \
283289
(.msg_buf = twim_##idx##_msg_buf,)) \
284290
.max_transfer_size = BIT_MASK( \
285-
DT_PROP(I2C(idx), easydma_maxcnt_bits)), \
291+
DT_INST_PROP(idx, easydma_maxcnt_bits)), \
286292
}; \
287-
PM_DEVICE_DT_DEFINE(I2C(idx), twim_nrfx_pm_action, \
293+
PM_DEVICE_DT_INST_DEFINE(idx, twim_nrfx_pm_action, \
288294
I2C_PM_ISR_SAFE(idx)); \
289-
I2C_DEVICE_DT_DEINIT_DEFINE(I2C(idx), \
295+
I2C_DEVICE_DT_INST_DEINIT_DEFINE(idx, \
290296
i2c_nrfx_twim_init, \
291297
i2c_nrfx_twim_deinit, \
292-
PM_DEVICE_DT_GET(I2C(idx)), \
298+
PM_DEVICE_DT_INST_GET(idx), \
293299
&twim_##idx##_data, \
294300
&twim_##idx##z_config, \
295301
POST_KERNEL, \
296302
CONFIG_I2C_INIT_PRIORITY, \
297303
&i2c_nrfx_twim_driver_api)
298304

299-
#define I2C_MEMORY_SECTION(idx) \
300-
COND_CODE_1(I2C_HAS_PROP(idx, memory_regions), \
305+
#define I2C_MEMORY_SECTION(idx) \
306+
COND_CODE_1(DT_NODE_HAS_PROP(DT_DRV_INST(idx), prop), \
301307
(__attribute__((__section__(LINKER_DT_NODE_REGION_NAME( \
302-
DT_PHANDLE(I2C(idx), memory_regions)))))), \
308+
DT_PHANDLE(DT_DRV_INST(idx), memory_regions)))))), \
303309
())
304310

305-
#ifdef CONFIG_HAS_HW_NRF_TWIM0
306-
I2C_NRFX_TWIM_DEVICE(0);
307-
#endif
308-
309-
#ifdef CONFIG_HAS_HW_NRF_TWIM1
310-
I2C_NRFX_TWIM_DEVICE(1);
311-
#endif
312-
313-
#ifdef CONFIG_HAS_HW_NRF_TWIM2
314-
I2C_NRFX_TWIM_DEVICE(2);
315-
#endif
316-
317-
#ifdef CONFIG_HAS_HW_NRF_TWIM3
318-
I2C_NRFX_TWIM_DEVICE(3);
319-
#endif
320-
321-
#ifdef CONFIG_HAS_HW_NRF_TWIM20
322-
I2C_NRFX_TWIM_DEVICE(20);
323-
#endif
324-
325-
#ifdef CONFIG_HAS_HW_NRF_TWIM21
326-
I2C_NRFX_TWIM_DEVICE(21);
327-
#endif
328-
329-
#ifdef CONFIG_HAS_HW_NRF_TWIM22
330-
I2C_NRFX_TWIM_DEVICE(22);
331-
#endif
332-
333-
#ifdef CONFIG_HAS_HW_NRF_TWIM23
334-
I2C_NRFX_TWIM_DEVICE(23);
335-
#endif
336-
337-
#ifdef CONFIG_HAS_HW_NRF_TWIM24
338-
I2C_NRFX_TWIM_DEVICE(24);
339-
#endif
340-
341-
#ifdef CONFIG_HAS_HW_NRF_TWIM30
342-
I2C_NRFX_TWIM_DEVICE(30);
343-
#endif
344-
345-
#ifdef CONFIG_HAS_HW_NRF_TWIM120
346-
I2C_NRFX_TWIM_DEVICE(120);
347-
#endif
348-
349-
#ifdef CONFIG_HAS_HW_NRF_TWIM130
350-
I2C_NRFX_TWIM_DEVICE(130);
351-
#endif
352-
353-
#ifdef CONFIG_HAS_HW_NRF_TWIM131
354-
I2C_NRFX_TWIM_DEVICE(131);
355-
#endif
356-
357-
#ifdef CONFIG_HAS_HW_NRF_TWIM132
358-
I2C_NRFX_TWIM_DEVICE(132);
359-
#endif
360-
361-
#ifdef CONFIG_HAS_HW_NRF_TWIM133
362-
I2C_NRFX_TWIM_DEVICE(133);
363-
#endif
364-
365-
#ifdef CONFIG_HAS_HW_NRF_TWIM134
366-
I2C_NRFX_TWIM_DEVICE(134);
367-
#endif
368-
369-
#ifdef CONFIG_HAS_HW_NRF_TWIM135
370-
I2C_NRFX_TWIM_DEVICE(135);
371-
#endif
372-
373-
#ifdef CONFIG_HAS_HW_NRF_TWIM136
374-
I2C_NRFX_TWIM_DEVICE(136);
375-
#endif
376-
377-
#ifdef CONFIG_HAS_HW_NRF_TWIM137
378-
I2C_NRFX_TWIM_DEVICE(137);
379-
#endif
311+
DT_INST_FOREACH_STATUS_OKAY(I2C_NRFX_TWIM_DEVICE)

drivers/i2c/i2c_nrfx_twim_common.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ int i2c_nrfx_twim_recover_bus(const struct device *dev)
2222
uint32_t sda_pin;
2323
nrfx_err_t err;
2424

25-
scl_pin = nrf_twim_scl_pin_get(config->twim.p_twim);
26-
sda_pin = nrf_twim_sda_pin_get(config->twim.p_twim);
25+
scl_pin = nrf_twim_scl_pin_get(config->twim->p_twim);
26+
sda_pin = nrf_twim_sda_pin_get(config->twim->p_twim);
2727

2828
/* disable peripheral if active (required to release SCL/SDA lines) */
2929
(void)pm_device_state_get(dev, &state);
3030
if (state == PM_DEVICE_STATE_ACTIVE) {
31-
nrfx_twim_disable(&config->twim);
31+
nrfx_twim_disable(config->twim);
3232
}
3333

3434
err = nrfx_twim_bus_recover(scl_pin, sda_pin);
3535

3636
/* restore peripheral if it was active before */
3737
if (state == PM_DEVICE_STATE_ACTIVE) {
3838
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
39-
nrfx_twim_enable(&config->twim);
39+
nrfx_twim_enable(config->twim);
4040
}
4141

4242
return (err == NRFX_SUCCESS ? 0 : -EBUSY);
@@ -52,14 +52,14 @@ int i2c_nrfx_twim_configure(const struct device *dev, uint32_t i2c_config)
5252

5353
switch (I2C_SPEED_GET(i2c_config)) {
5454
case I2C_SPEED_STANDARD:
55-
nrf_twim_frequency_set(config->twim.p_twim, NRF_TWIM_FREQ_100K);
55+
nrf_twim_frequency_set(config->twim->p_twim, NRF_TWIM_FREQ_100K);
5656
break;
5757
case I2C_SPEED_FAST:
58-
nrf_twim_frequency_set(config->twim.p_twim, NRF_TWIM_FREQ_400K);
58+
nrf_twim_frequency_set(config->twim->p_twim, NRF_TWIM_FREQ_400K);
5959
break;
6060
#if NRF_TWIM_HAS_1000_KHZ_FREQ
6161
case I2C_SPEED_FAST_PLUS:
62-
nrf_twim_frequency_set(config->twim.p_twim, NRF_TWIM_FREQ_1000K);
62+
nrf_twim_frequency_set(config->twim->p_twim, NRF_TWIM_FREQ_1000K);
6363
break;
6464
#endif
6565
default:
@@ -90,7 +90,7 @@ int i2c_nrfx_twim_msg_transfer(const struct device *dev, uint8_t flags, uint8_t
9090
return -ENOSPC;
9191
}
9292

93-
res = nrfx_twim_xfer(&config->twim, &cur_xfer,
93+
res = nrfx_twim_xfer(config->twim, &cur_xfer,
9494
(flags & I2C_MSG_STOP) ? 0 : NRFX_TWIM_FLAG_TX_NO_STOP);
9595
if (res != NRFX_SUCCESS) {
9696
if (res == NRFX_ERROR_BUSY) {
@@ -107,14 +107,14 @@ void twim_nrfx_pm_resume(const struct device *dev)
107107
const struct i2c_nrfx_twim_common_config *config = dev->config;
108108

109109
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
110-
nrfx_twim_enable(&config->twim);
110+
nrfx_twim_enable(config->twim);
111111
}
112112

113113
void twim_nrfx_pm_suspend(const struct device *dev)
114114
{
115115
const struct i2c_nrfx_twim_common_config *config = dev->config;
116116

117-
nrfx_twim_disable(&config->twim);
117+
nrfx_twim_disable(config->twim);
118118
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
119119
}
120120

@@ -138,11 +138,11 @@ int i2c_nrfx_twim_common_init(const struct device *dev)
138138
{
139139
const struct i2c_nrfx_twim_common_config *config = dev->config;
140140

141-
config->irq_connect();
141+
config->pre_init();
142142

143143
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
144144

145-
if (nrfx_twim_init(&config->twim, &config->twim_config, config->event_handler,
145+
if (nrfx_twim_init(config->twim, &config->twim_config, config->event_handler,
146146
(void *)dev) != NRFX_SUCCESS) {
147147
LOG_ERR("Failed to initialize device: %s", dev->name);
148148
return -EIO;
@@ -175,6 +175,6 @@ int i2c_nrfx_twim_common_deinit(const struct device *dev)
175175
#endif
176176

177177
/* Uninit device hardware */
178-
nrfx_twim_uninit(&config->twim);
178+
nrfx_twim_uninit(config->twim);
179179
return 0;
180180
}

0 commit comments

Comments
 (0)