IZAR Flight Controller 1.0.0
IZAR Flight Controller running with an ESP32.
i2cdev.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2018 Ruslan V. Uss <unclerus@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
35#ifndef __I2CDEV_H__
36#define __I2CDEV_H__
37
38#define CONFIG_I2CDEV_TIMEOUT 1000
39
40#include <driver/i2c.h>
41#include <esp_err.h>
42#include <esp_idf_lib_helpers.h>
43#include <freertos/FreeRTOS.h>
44#include <freertos/semphr.h>
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50#if HELPER_TARGET_IS_ESP8266
51
52#define I2CDEV_MAX_STRETCH_TIME 0xffffffff
53
54#else
55
56#include <soc/i2c_reg.h>
57#if defined(I2C_TIME_OUT_VALUE_V)
58#define I2CDEV_MAX_STRETCH_TIME I2C_TIME_OUT_VALUE_V
59#elif defined(I2C_TIME_OUT_REG_V)
60#define I2CDEV_MAX_STRETCH_TIME I2C_TIME_OUT_REG_V
61#else
62#define I2CDEV_MAX_STRETCH_TIME 0x00ffffff
63#endif
64
65#endif /* HELPER_TARGET_IS_ESP8266 */
66
70typedef struct {
71 i2c_port_t port;
72 i2c_config_t cfg;
73 uint8_t addr;
74 SemaphoreHandle_t mutex;
75 uint32_t timeout_ticks;
79} i2c_dev_t;
80
88
97esp_err_t i2cdev_init();
98
106esp_err_t i2cdev_done();
107
116esp_err_t i2c_dev_create_mutex(i2c_dev_t *dev);
117
126esp_err_t i2c_dev_delete_mutex(i2c_dev_t *dev);
127
136esp_err_t i2c_dev_take_mutex(i2c_dev_t *dev);
137
146esp_err_t i2c_dev_give_mutex(i2c_dev_t *dev);
147
157esp_err_t i2c_dev_probe(const i2c_dev_t *dev, i2c_dev_type_t operation_type);
158
172esp_err_t i2c_dev_read(const i2c_dev_t *dev, const void *out_data,
173 size_t out_size, void *in_data, size_t in_size);
174
188esp_err_t i2c_dev_write(const i2c_dev_t *dev, const void *out_reg,
189 size_t out_reg_size, const void *out_data,
190 size_t out_size);
191
203esp_err_t i2c_dev_read_reg(const i2c_dev_t *dev, uint8_t reg, void *in_data,
204 size_t in_size);
205
217esp_err_t i2c_dev_write_reg(const i2c_dev_t *dev, uint8_t reg,
218 const void *out_data, size_t out_size);
219
220#define I2C_DEV_TAKE_MUTEX(dev) \
221 do { \
222 esp_err_t __ = i2c_dev_take_mutex(dev); \
223 if (__ != ESP_OK) \
224 return __; \
225 } while (0)
226
227#define I2C_DEV_GIVE_MUTEX(dev) \
228 do { \
229 esp_err_t __ = i2c_dev_give_mutex(dev); \
230 if (__ != ESP_OK) \
231 return __; \
232 } while (0)
233
234#define I2C_DEV_CHECK(dev, X) \
235 do { \
236 esp_err_t ___ = X; \
237 if (___ != ESP_OK) { \
238 I2C_DEV_GIVE_MUTEX(dev); \
239 return ___; \
240 } \
241 } while (0)
242
243#define I2C_DEV_CHECK_LOGE(dev, X, msg, ...) \
244 do { \
245 esp_err_t ___ = X; \
246 if (___ != ESP_OK) { \
247 I2C_DEV_GIVE_MUTEX(dev); \
248 ESP_LOGE(TAG, msg, ##__VA_ARGS__); \
249 return ___; \
250 } \
251 } while (0)
252
253#ifdef __cplusplus
254}
255#endif
256
258
259#endif /* __I2CDEV_H__ */
esp_err_t i2c_dev_create_mutex(i2c_dev_t *dev)
Create mutex for device descriptor.
Definition i2cdev.c:114
esp_err_t i2c_dev_delete_mutex(i2c_dev_t *dev)
Delete mutex for device descriptor.
Definition i2cdev.c:132
esp_err_t i2c_dev_write(const i2c_dev_t *dev, const void *out_reg, size_t out_reg_size, const void *out_data, size_t out_size)
Write to slave device.
Definition i2cdev.c:306
esp_err_t i2cdev_done()
Finish work with library.
Definition i2cdev.c:93
i2c_dev_type_t
Definition i2cdev.h:84
esp_err_t i2c_dev_give_mutex(i2c_dev_t *dev)
Give device mutex.
Definition i2cdev.c:160
esp_err_t i2cdev_init()
Init library.
Definition i2cdev.c:74
esp_err_t i2c_dev_read(const i2c_dev_t *dev, const void *out_data, size_t out_size, void *in_data, size_t in_size)
Read from slave device.
Definition i2cdev.c:274
esp_err_t i2c_dev_take_mutex(i2c_dev_t *dev)
Take device mutex.
Definition i2cdev.c:144
esp_err_t i2c_dev_write_reg(const i2c_dev_t *dev, uint8_t reg, const void *out_data, size_t out_size)
Write to register with an 8-bit address.
Definition i2cdev.c:337
esp_err_t i2c_dev_read_reg(const i2c_dev_t *dev, uint8_t reg, void *in_data, size_t in_size)
Read from register with an 8-bit address.
Definition i2cdev.c:332
esp_err_t i2c_dev_probe(const i2c_dev_t *dev, i2c_dev_type_t operation_type)
Check the availability of the device.
Definition i2cdev.c:250
@ I2C_DEV_READ
Definition i2cdev.h:86
@ I2C_DEV_WRITE
Definition i2cdev.h:85
Definition i2cdev.h:70
i2c_port_t port
I2C port number.
Definition i2cdev.h:71
uint8_t addr
Unshifted address.
Definition i2cdev.h:73
i2c_config_t cfg
I2C driver configuration.
Definition i2cdev.h:72
uint32_t timeout_ticks
Definition i2cdev.h:75
SemaphoreHandle_t mutex
Device mutex.
Definition i2cdev.h:74