1 /*
2 * Copyright (c) 2024, Croxel Inc
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #ifndef ZEPHYR_DRIVERS_I2C_I2C_NRFX_TWI_COMMON_H_
8 #define ZEPHYR_DRIVERS_I2C_I2C_NRFX_TWI_COMMON_H_
9
10 #include <zephyr/pm/device.h>
11 #include <nrfx_twi.h>
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 #define I2C_NRFX_TWI_INVALID_FREQUENCY ((nrf_twi_frequency_t)-1)
18 #define I2C_NRFX_TWI_FREQUENCY(bitrate) \
19 (bitrate == I2C_BITRATE_STANDARD ? NRF_TWI_FREQ_100K \
20 : bitrate == 250000 ? NRF_TWI_FREQ_250K \
21 : bitrate == I2C_BITRATE_FAST ? NRF_TWI_FREQ_400K \
22 : I2C_NRFX_TWI_INVALID_FREQUENCY)
23 #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))
27
28 struct i2c_nrfx_twi_common_data {
29 uint32_t dev_config;
30 };
31
32 struct i2c_nrfx_twi_config {
33 nrfx_twi_t twi;
34 nrfx_twi_config_t config;
35 nrfx_twi_evt_handler_t event_handler;
36 const struct pinctrl_dev_config *pcfg;
37 };
38
i2c_nrfx_twi_get_evt_result(nrfx_twi_evt_t const * p_event)39 static inline nrfx_err_t i2c_nrfx_twi_get_evt_result(nrfx_twi_evt_t const *p_event)
40 {
41 switch (p_event->type) {
42 case NRFX_TWI_EVT_DONE:
43 return NRFX_SUCCESS;
44 case NRFX_TWI_EVT_ADDRESS_NACK:
45 return NRFX_ERROR_DRV_TWI_ERR_ANACK;
46 case NRFX_TWI_EVT_DATA_NACK:
47 return NRFX_ERROR_DRV_TWI_ERR_DNACK;
48 default:
49 return NRFX_ERROR_INTERNAL;
50 }
51 }
52
53 int i2c_nrfx_twi_init(const struct device *dev);
54 int i2c_nrfx_twi_configure(const struct device *dev, uint32_t dev_config);
55 int i2c_nrfx_twi_recover_bus(const struct device *dev);
56 int i2c_nrfx_twi_msg_transfer(const struct device *dev, uint8_t flags,
57 uint8_t *buf, size_t buf_len,
58 uint16_t i2c_addr, bool more_msgs);
59
60 #ifdef CONFIG_PM_DEVICE
61 int twi_nrfx_pm_action(const struct device *dev, enum pm_device_action action);
62 #endif
63
64 #ifdef __cplusplus
65 }
66 #endif
67
68 #endif /* ZEPHYR_DRIVERS_I2C_I2C_NRFX_TWI_COMMON_H_ */
69