Skip to content

Commit 8e62d56

Browse files
[nrf fromlist] drivers: i2c: i2c_nrfx_twis: use standard instantiation
Switched nrfx_twis API to standard instantiation. Upstream PR #: 98569 Signed-off-by: Adam Kondraciuk <[email protected]>
1 parent d530095 commit 8e62d56

File tree

1 file changed

+53
-151
lines changed

1 file changed

+53
-151
lines changed

drivers/i2c/i2c_nrfx_twis.c

Lines changed: 53 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,10 @@
1616

1717
#define DT_DRV_COMPAT nordic_nrf_twis
1818

19-
#define SHIM_NRF_TWIS_NODE(id) \
20-
DT_NODELABEL(_CONCAT(i2c, id))
19+
#define SHIM_NRF_TWIS_HAS_MEMORY_REGIONS(id) DT_NODE_HAS_PROP(DT_DRV_INST(id), memory_regions)
2120

22-
#define SHIM_NRF_TWIS_DEVICE_GET(id) \
23-
DEVICE_DT_GET(SHIM_NRF_TWIS_NODE(id))
24-
25-
#define SHIM_NRF_TWIS_IRQ_HANDLER(id) \
26-
_CONCAT_3(nrfx_twis_, id, _irq_handler)
27-
28-
#define SHIM_NRF_TWIS_IRQN(id) \
29-
DT_IRQN(SHIM_NRF_TWIS_NODE(id))
30-
31-
#define SHIM_NRF_TWIS_IRQ_PRIO(id) \
32-
DT_IRQ(SHIM_NRF_TWIS_NODE(id), priority)
33-
34-
#define SHIM_NRF_TWIS_HAS_MEMORY_REGIONS(id) \
35-
DT_NODE_HAS_PROP(SHIM_NRF_TWIS_NODE(id), memory_regions)
36-
37-
#define SHIM_NRF_TWIS_LINKER_REGION_NAME(id) \
38-
LINKER_DT_NODE_REGION_NAME(DT_PHANDLE(SHIM_NRF_TWIS_NODE(id), memory_regions))
21+
#define SHIM_NRF_TWIS_LINKER_REGION_NAME(id) \
22+
LINKER_DT_NODE_REGION_NAME(DT_PHANDLE(DT_DRV_INST(id), memory_regions))
3923

4024
#define SHIM_NRF_TWIS_BUF_ATTR_SECTION(id) \
4125
__attribute__((__section__(SHIM_NRF_TWIS_LINKER_REGION_NAME(id))))
@@ -53,14 +37,14 @@
5337
LOG_MODULE_REGISTER(i2c_nrfx_twis, CONFIG_I2C_LOG_LEVEL);
5438

5539
struct shim_nrf_twis_config {
56-
nrfx_twis_t twis;
57-
void (*irq_connect)(void);
58-
void (*event_handler)(nrfx_twis_evt_t const *event);
40+
void (*pre_init)(void);
41+
void (*event_handler)(nrfx_twis_event_t const *event);
5942
const struct pinctrl_dev_config *pcfg;
6043
uint8_t *buf;
6144
};
6245

6346
struct shim_nrf_twis_data {
47+
nrfx_twis_t twis;
6448
struct i2c_target_config *target_config;
6549
bool enabled;
6650
};
@@ -105,7 +89,7 @@ static void shim_nrf_twis_enable(const struct device *dev)
10589
}
10690

10791
(void)pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_DEFAULT);
108-
nrfx_twis_enable(&dev_config->twis);
92+
nrfx_twis_enable(&dev_data->twis);
10993
dev_data->enabled = true;
11094
}
11195

@@ -119,7 +103,7 @@ static void shim_nrf_twis_disable(const struct device *dev)
119103
}
120104

121105
dev_data->enabled = false;
122-
nrfx_twis_disable(&dev_config->twis);
106+
nrfx_twis_disable(&dev_data->twis);
123107
(void)pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_SLEEP);
124108
}
125109

@@ -129,7 +113,7 @@ static void shim_nrf_twis_handle_read_req(const struct device *dev)
129113
const struct shim_nrf_twis_config *dev_config = dev->config;
130114
struct i2c_target_config *target_config = dev_data->target_config;
131115
const struct i2c_target_callbacks *callbacks = target_config->callbacks;
132-
const nrfx_twis_t *twis = &dev_config->twis;
116+
nrfx_twis_t *twis = &dev_data->twis;
133117
uint8_t *buf;
134118
uint32_t buf_size;
135119
nrfx_err_t err;
@@ -155,8 +139,9 @@ static void shim_nrf_twis_handle_read_req(const struct device *dev)
155139

156140
static void shim_nrf_twis_handle_write_req(const struct device *dev)
157141
{
142+
struct shim_nrf_twis_data *dev_data = dev->data;
158143
const struct shim_nrf_twis_config *dev_config = dev->config;
159-
const nrfx_twis_t *twis = &dev_config->twis;
144+
nrfx_twis_t *twis = &dev_data->twis;
160145
nrfx_err_t err;
161146

162147
err = nrfx_twis_rx_prepare(twis, dev_config->buf, SHIM_NRF_TWIS_BUF_SIZE);
@@ -172,13 +157,13 @@ static void shim_nrf_twis_handle_write_done(const struct device *dev)
172157
const struct shim_nrf_twis_config *dev_config = dev->config;
173158
struct i2c_target_config *target_config = dev_data->target_config;
174159
const struct i2c_target_callbacks *callbacks = target_config->callbacks;
175-
const nrfx_twis_t *twis = &dev_config->twis;
160+
nrfx_twis_t *twis = &dev_data->twis;
176161

177162
callbacks->buf_write_received(target_config, dev_config->buf, nrfx_twis_rx_amount(twis));
178163
}
179164

180165
static void shim_nrf_twis_event_handler(const struct device *dev,
181-
nrfx_twis_evt_t const *event)
166+
nrfx_twis_event_t const *event)
182167
{
183168
switch (event->type) {
184169
case NRFX_TWIS_EVT_READ_REQ:
@@ -223,8 +208,7 @@ static int shim_nrf_twis_target_register(const struct device *dev,
223208
struct i2c_target_config *target_config)
224209
{
225210
struct shim_nrf_twis_data *dev_data = dev->data;
226-
const struct shim_nrf_twis_config *dev_config = dev->config;
227-
const nrfx_twis_t *twis = &dev_config->twis;
211+
nrfx_twis_t *twis = &dev_data->twis;
228212
nrfx_err_t err;
229213
const nrfx_twis_config_t config = {
230214
.addr = {
@@ -276,25 +260,25 @@ const struct i2c_driver_api shim_nrf_twis_api = {
276260

277261
static int shim_nrf_twis_init(const struct device *dev)
278262
{
263+
struct shim_nrf_twis_data *dev_data = dev->data;
279264
const struct shim_nrf_twis_config *dev_config = dev->config;
280265
nrfx_err_t err;
281266
const nrfx_twis_config_t config = {
282267
.skip_gpio_cfg = true,
283268
.skip_psel_cfg = true,
284269
};
285270

286-
err = nrfx_twis_init(&dev_config->twis, &config, dev_config->event_handler);
271+
dev_config->pre_init();
272+
err = nrfx_twis_init(&dev_data->twis, &config, dev_config->event_handler);
287273
if (err != NRFX_SUCCESS) {
288274
return -ENODEV;
289275
}
290276

291-
dev_config->irq_connect();
292277
return pm_device_driver_init(dev, shim_nrf_twis_pm_action_cb);
293278
}
294279

295280
static int shim_nrf_twis_deinit(const struct device *dev)
296281
{
297-
const struct shim_nrf_twis_config *dev_config = dev->config;
298282
struct shim_nrf_twis_data *dev_data = dev->data;
299283

300284
if (dev_data->target_config != NULL) {
@@ -317,127 +301,45 @@ static int shim_nrf_twis_deinit(const struct device *dev)
317301
#endif
318302

319303
/* Uninit device hardware */
320-
nrfx_twis_uninit(&dev_config->twis);
304+
nrfx_twis_uninit(&dev_data->twis);
321305
return 0;
322306
}
323307

324308
#define SHIM_NRF_TWIS_NAME(id, name) \
325309
_CONCAT_4(shim_nrf_twis_, name, _, id)
326310

327-
#define SHIM_NRF_TWIS_DEVICE_DEFINE(id) \
328-
NRF_DT_CHECK_NODE_HAS_REQUIRED_MEMORY_REGIONS(SHIM_NRF_TWIS_NODE(id)); \
329-
static void SHIM_NRF_TWIS_NAME(id, irq_connect)(void) \
330-
{ \
331-
IRQ_CONNECT( \
332-
SHIM_NRF_TWIS_IRQN(id), \
333-
SHIM_NRF_TWIS_IRQ_PRIO(id), \
334-
nrfx_isr, \
335-
SHIM_NRF_TWIS_IRQ_HANDLER(id), \
336-
0 \
337-
); \
338-
} \
339-
\
340-
static void SHIM_NRF_TWIS_NAME(id, event_handler)(nrfx_twis_evt_t const *event) \
341-
{ \
342-
shim_nrf_twis_event_handler(SHIM_NRF_TWIS_DEVICE_GET(id), event); \
343-
} \
344-
\
345-
static struct shim_nrf_twis_data SHIM_NRF_TWIS_NAME(id, data); \
346-
\
347-
PINCTRL_DT_DEFINE(SHIM_NRF_TWIS_NODE(id)); \
348-
\
349-
static uint8_t SHIM_NRF_TWIS_NAME(id, buf) \
350-
[SHIM_NRF_TWIS_BUF_SIZE] SHIM_NRF_TWIS_BUF_ATTR(id); \
351-
\
352-
static const struct shim_nrf_twis_config SHIM_NRF_TWIS_NAME(id, config) = { \
353-
.twis = NRFX_TWIS_INSTANCE(id), \
354-
.irq_connect = SHIM_NRF_TWIS_NAME(id, irq_connect), \
355-
.event_handler = SHIM_NRF_TWIS_NAME(id, event_handler), \
356-
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(SHIM_NRF_TWIS_NODE(id)), \
357-
.buf = SHIM_NRF_TWIS_NAME(id, buf), \
358-
}; \
359-
\
360-
PM_DEVICE_DT_DEFINE( \
361-
SHIM_NRF_TWIS_NODE(id), \
362-
shim_nrf_twis_pm_action_cb, \
363-
); \
364-
\
365-
DEVICE_DT_DEINIT_DEFINE( \
366-
SHIM_NRF_TWIS_NODE(id), \
367-
shim_nrf_twis_init, \
368-
shim_nrf_twis_deinit, \
369-
PM_DEVICE_DT_GET(SHIM_NRF_TWIS_NODE(id)), \
370-
&SHIM_NRF_TWIS_NAME(id, data), \
371-
&SHIM_NRF_TWIS_NAME(id, config), \
372-
POST_KERNEL, \
373-
CONFIG_I2C_INIT_PRIORITY, \
374-
&shim_nrf_twis_api \
375-
);
376-
377-
#ifdef CONFIG_HAS_HW_NRF_TWIS0
378-
SHIM_NRF_TWIS_DEVICE_DEFINE(0);
379-
#endif
380-
381-
#ifdef CONFIG_HAS_HW_NRF_TWIS1
382-
SHIM_NRF_TWIS_DEVICE_DEFINE(1);
383-
#endif
384-
385-
#ifdef CONFIG_HAS_HW_NRF_TWIS2
386-
SHIM_NRF_TWIS_DEVICE_DEFINE(2);
387-
#endif
388-
389-
#ifdef CONFIG_HAS_HW_NRF_TWIS3
390-
SHIM_NRF_TWIS_DEVICE_DEFINE(3);
391-
#endif
392-
393-
#ifdef CONFIG_HAS_HW_NRF_TWIS20
394-
SHIM_NRF_TWIS_DEVICE_DEFINE(20);
395-
#endif
396-
397-
#ifdef CONFIG_HAS_HW_NRF_TWIS21
398-
SHIM_NRF_TWIS_DEVICE_DEFINE(21);
399-
#endif
400-
401-
#ifdef CONFIG_HAS_HW_NRF_TWIS22
402-
SHIM_NRF_TWIS_DEVICE_DEFINE(22);
403-
#endif
404-
405-
#ifdef CONFIG_HAS_HW_NRF_TWIS23
406-
SHIM_NRF_TWIS_DEVICE_DEFINE(23);
407-
#endif
408-
409-
#ifdef CONFIG_HAS_HW_NRF_TWIS24
410-
SHIM_NRF_TWIS_DEVICE_DEFINE(24);
411-
#endif
412-
413-
#ifdef CONFIG_HAS_HW_NRF_TWIS30
414-
SHIM_NRF_TWIS_DEVICE_DEFINE(30);
415-
#endif
416-
417-
#ifdef CONFIG_HAS_HW_NRF_TWIS130
418-
SHIM_NRF_TWIS_DEVICE_DEFINE(130);
419-
#endif
420-
421-
#ifdef CONFIG_HAS_HW_NRF_TWIS131
422-
SHIM_NRF_TWIS_DEVICE_DEFINE(131);
423-
#endif
424-
425-
#ifdef CONFIG_HAS_HW_NRF_TWIS133
426-
SHIM_NRF_TWIS_DEVICE_DEFINE(133);
427-
#endif
428-
429-
#ifdef CONFIG_HAS_HW_NRF_TWIS134
430-
SHIM_NRF_TWIS_DEVICE_DEFINE(134);
431-
#endif
432-
433-
#ifdef CONFIG_HAS_HW_NRF_TWIS135
434-
SHIM_NRF_TWIS_DEVICE_DEFINE(135);
435-
#endif
436-
437-
#ifdef CONFIG_HAS_HW_NRF_TWIS136
438-
SHIM_NRF_TWIS_DEVICE_DEFINE(136);
439-
#endif
440-
441-
#ifdef CONFIG_HAS_HW_NRF_TWIS137
442-
SHIM_NRF_TWIS_DEVICE_DEFINE(137);
443-
#endif
311+
#define SHIM_NRF_TWIS_DEVICE_DEFINE(id) \
312+
static struct shim_nrf_twis_data SHIM_NRF_TWIS_NAME(id, data); \
313+
NRF_DT_CHECK_NODE_HAS_REQUIRED_MEMORY_REGIONS(DT_DRV_INST(id)); \
314+
static void SHIM_NRF_TWIS_NAME(id, pre_init)(void) \
315+
{ \
316+
SHIM_NRF_TWIS_NAME(id, data).twis.p_reg = (NRF_TWIS_Type *)DT_INST_REG_ADDR(id); \
317+
IRQ_CONNECT(DT_INST_IRQN(id), DT_INST_IRQ(id, priority), nrfx_twis_irq_handler, \
318+
&SHIM_NRF_TWIS_NAME(id, data).twis, 0); \
319+
} \
320+
\
321+
static void SHIM_NRF_TWIS_NAME(id, event_handler)(nrfx_twis_event_t const *event) \
322+
{ \
323+
shim_nrf_twis_event_handler(DEVICE_DT_INST_GET(id), event); \
324+
} \
325+
\
326+
PINCTRL_DT_INST_DEFINE(id); \
327+
\
328+
static uint8_t SHIM_NRF_TWIS_NAME(id, \
329+
buf)[SHIM_NRF_TWIS_BUF_SIZE] SHIM_NRF_TWIS_BUF_ATTR(id); \
330+
\
331+
static const struct shim_nrf_twis_config SHIM_NRF_TWIS_NAME(id, config) = { \
332+
.pre_init = SHIM_NRF_TWIS_NAME(id, pre_init), \
333+
.event_handler = SHIM_NRF_TWIS_NAME(id, event_handler), \
334+
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(id), \
335+
.buf = SHIM_NRF_TWIS_NAME(id, buf), \
336+
}; \
337+
\
338+
PM_DEVICE_DT_INST_DEFINE(id, shim_nrf_twis_pm_action_cb, ); \
339+
\
340+
DEVICE_DT_INST_DEINIT_DEFINE(id, shim_nrf_twis_init, shim_nrf_twis_deinit, \
341+
PM_DEVICE_DT_INST_GET(id), &SHIM_NRF_TWIS_NAME(id, data), \
342+
&SHIM_NRF_TWIS_NAME(id, config), POST_KERNEL, \
343+
CONFIG_I2C_INIT_PRIORITY, &shim_nrf_twis_api);
344+
345+
DT_INST_FOREACH_STATUS_OKAY(SHIM_NRF_TWIS_DEVICE_DEFINE)

0 commit comments

Comments
 (0)