forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c_gpio.c
220 lines (174 loc) · 5.32 KB
/
i2c_gpio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright (c) 2020 Vestas Wind Systems A/S
* Copyright (c) 2017 Linaro Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT gpio_i2c
/**
* @file
* @brief Driver for software driven I2C using GPIO lines
*
* This driver implements an I2C interface by driving two GPIO lines under
* software control.
*
* The GPIO pins used must be configured (through devicetree and pinmux) with
* suitable flags, i.e. the SDA pin as open-collector/open-drain with a pull-up
* resistor (possibly as an external component attached to the pin).
*
* When the SDA pin is read it must return the state of the physical hardware
* line, not just the last state written to it for output.
*
* The SCL pin should be configured in the same manner as SDA, or, if it is
* known that the hardware attached to pin doesn't attempt clock stretching,
* then the SCL pin may be a push/pull output.
*/
#include <zephyr/device.h>
#include <errno.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(i2c_gpio);
#include "i2c-priv.h"
#include "i2c_bitbang.h"
/* Driver config */
struct i2c_gpio_config {
struct gpio_dt_spec scl_gpio;
struct gpio_dt_spec sda_gpio;
uint32_t bitrate;
};
/* Driver instance data */
struct i2c_gpio_context {
struct i2c_bitbang bitbang; /* Bit-bang library data */
struct k_mutex mutex;
};
static void i2c_gpio_set_scl(void *io_context, int state)
{
const struct i2c_gpio_config *config = io_context;
gpio_pin_set_dt(&config->scl_gpio, state);
}
static void i2c_gpio_set_sda(void *io_context, int state)
{
const struct i2c_gpio_config *config = io_context;
gpio_pin_set_dt(&config->sda_gpio, state);
}
static int i2c_gpio_get_sda(void *io_context)
{
const struct i2c_gpio_config *config = io_context;
int rc = gpio_pin_get_dt(&config->sda_gpio);
/* Default high as that would be a NACK */
return rc != 0;
}
static const struct i2c_bitbang_io io_fns = {
.set_scl = &i2c_gpio_set_scl,
.set_sda = &i2c_gpio_set_sda,
.get_sda = &i2c_gpio_get_sda,
};
static int i2c_gpio_configure(const struct device *dev, uint32_t dev_config)
{
struct i2c_gpio_context *context = dev->data;
int rc;
k_mutex_lock(&context->mutex, K_FOREVER);
rc = i2c_bitbang_configure(&context->bitbang, dev_config);
k_mutex_unlock(&context->mutex);
return rc;
}
static int i2c_gpio_get_config(const struct device *dev, uint32_t *config)
{
struct i2c_gpio_context *context = dev->data;
int rc;
k_mutex_lock(&context->mutex, K_FOREVER);
rc = i2c_bitbang_get_config(&context->bitbang, config);
if (rc < 0) {
LOG_ERR("I2C controller not configured: %d", rc);
}
k_mutex_unlock(&context->mutex);
return rc;
}
static int i2c_gpio_transfer(const struct device *dev, struct i2c_msg *msgs,
uint8_t num_msgs, uint16_t slave_address)
{
struct i2c_gpio_context *context = dev->data;
int rc;
k_mutex_lock(&context->mutex, K_FOREVER);
rc = i2c_bitbang_transfer(&context->bitbang, msgs, num_msgs,
slave_address);
k_mutex_unlock(&context->mutex);
return rc;
}
static int i2c_gpio_recover_bus(const struct device *dev)
{
struct i2c_gpio_context *context = dev->data;
int rc;
k_mutex_lock(&context->mutex, K_FOREVER);
rc = i2c_bitbang_recover_bus(&context->bitbang);
k_mutex_unlock(&context->mutex);
return rc;
}
static const struct i2c_driver_api api = {
.configure = i2c_gpio_configure,
.get_config = i2c_gpio_get_config,
.transfer = i2c_gpio_transfer,
.recover_bus = i2c_gpio_recover_bus,
};
static int i2c_gpio_init(const struct device *dev)
{
struct i2c_gpio_context *context = dev->data;
const struct i2c_gpio_config *config = dev->config;
uint32_t bitrate_cfg;
int err;
if (!gpio_is_ready_dt(&config->scl_gpio)) {
LOG_ERR("SCL GPIO device not ready");
return -ENODEV;
}
err = gpio_pin_configure_dt(&config->scl_gpio, GPIO_OUTPUT_HIGH);
if (err) {
LOG_ERR("failed to configure SCL GPIO pin (err %d)", err);
return err;
}
if (!gpio_is_ready_dt(&config->sda_gpio)) {
LOG_ERR("SDA GPIO device not ready");
return -ENODEV;
}
err = gpio_pin_configure_dt(&config->sda_gpio,
GPIO_INPUT | GPIO_OUTPUT_HIGH);
if (err == -ENOTSUP) {
err = gpio_pin_configure_dt(&config->sda_gpio,
GPIO_OUTPUT_HIGH);
}
if (err) {
LOG_ERR("failed to configure SDA GPIO pin (err %d)", err);
return err;
}
i2c_bitbang_init(&context->bitbang, &io_fns, (void *)config);
bitrate_cfg = i2c_map_dt_bitrate(config->bitrate);
err = i2c_bitbang_configure(&context->bitbang,
I2C_MODE_CONTROLLER | bitrate_cfg);
if (err) {
LOG_ERR("failed to configure I2C bitbang (err %d)", err);
return err;
}
err = k_mutex_init(&context->mutex);
if (err) {
LOG_ERR("Failed to create the i2c lock mutex : %d", err);
return err;
}
return 0;
}
#define DEFINE_I2C_GPIO(_num) \
\
static struct i2c_gpio_context i2c_gpio_dev_data_##_num; \
\
static const struct i2c_gpio_config i2c_gpio_dev_cfg_##_num = { \
.scl_gpio = GPIO_DT_SPEC_INST_GET(_num, scl_gpios), \
.sda_gpio = GPIO_DT_SPEC_INST_GET(_num, sda_gpios), \
.bitrate = DT_INST_PROP(_num, clock_frequency), \
}; \
\
I2C_DEVICE_DT_INST_DEFINE(_num, \
i2c_gpio_init, \
NULL, \
&i2c_gpio_dev_data_##_num, \
&i2c_gpio_dev_cfg_##_num, \
POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, &api);
DT_INST_FOREACH_STATUS_OKAY(DEFINE_I2C_GPIO)