SRA Board Components
ESP-IDF component for SRA Board
i2cdev.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2018 Ruslan V. Uss <[email protected]>
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 all
13 * 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#include <driver/i2c.h>
39#include <freertos/FreeRTOS.h>
40#include <freertos/semphr.h>
41#include <esp_err.h>
42#include <esp_idf_lib_helpers.h>
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48#if HELPER_TARGET_IS_ESP8266
49#define I2CDEV_MAX_STRETCH_TIME 0xffffffff
50#else
51#include <soc/i2c_reg.h>
52#ifdef I2C_TIME_OUT_REG_V
53#define I2CDEV_MAX_STRETCH_TIME I2C_TIME_OUT_REG_V
54#else
55#define I2CDEV_MAX_STRETCH_TIME 0x00ffffff
56#endif
57#endif
58
59extern SemaphoreHandle_t mutex;
60
64typedef struct
65{
66 i2c_port_t port;
67 i2c_config_t cfg;
68 uint8_t addr;
69 SemaphoreHandle_t mutex;
70 uint32_t timeout_ticks;
73} i2c_dev_t;
74
83esp_err_t i2cdev_init();
84
92esp_err_t i2cdev_done();
93
102esp_err_t i2c_dev_create_mutex(i2c_dev_t *dev);
103
112esp_err_t i2c_dev_delete_mutex(i2c_dev_t *dev);
113
122esp_err_t i2c_dev_take_mutex(i2c_dev_t *dev);
123
132esp_err_t i2c_dev_give_mutex(i2c_dev_t *dev);
133
148esp_err_t i2c_dev_read(const i2c_dev_t *dev, const void *out_data,
149 size_t out_size, void *in_data, size_t in_size);
150
164esp_err_t i2c_dev_write(const i2c_dev_t *dev, const void *out_reg,
165 size_t out_reg_size, const void *out_data, size_t out_size);
166
178esp_err_t i2c_dev_read_reg(const i2c_dev_t *dev, uint8_t reg,
179 void *in_data, size_t in_size);
180
192esp_err_t i2c_dev_write_reg(const i2c_dev_t *dev, uint8_t reg,
193 const void *out_data, size_t out_size);
194
195#define I2C_DEV_TAKE_MUTEX(dev) do { \
196 esp_err_t __ = i2c_dev_take_mutex(dev); \
197 if (__ != ESP_OK) return __;\
198 } while (0)
199
200#define I2C_DEV_GIVE_MUTEX(dev) do { \
201 esp_err_t __ = i2c_dev_give_mutex(dev); \
202 if (__ != ESP_OK) return __;\
203 } while (0)
204
205#define I2C_DEV_CHECK(dev, X) do { \
206 esp_err_t ___ = X; \
207 if (___ != ESP_OK) { \
208 I2C_DEV_GIVE_MUTEX(dev); \
209 return ___; \
210 } \
211 } while (0)
212
213#define I2C_DEV_CHECK_LOGE(dev, X, msg, ...) do { \
214 esp_err_t ___ = X; \
215 if (___ != ESP_OK) { \
216 I2C_DEV_GIVE_MUTEX(dev); \
217 ESP_LOGE(TAG, msg, ## __VA_ARGS__); \
218 return ___; \
219 } \
220 } while (0)
221
222#ifdef __cplusplus
223}
224#endif
225
228#endif /* __I2CDEV_H__ */
esp_err_t i2c_dev_create_mutex(i2c_dev_t *dev)
Create mutex for device descriptor.
Definition: i2cdev.c:118
esp_err_t i2c_dev_delete_mutex(i2c_dev_t *dev)
Delete mutex for device descriptor.
Definition: i2cdev.c:136
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:273
esp_err_t i2cdev_done()
Finish work with library.
Definition: i2cdev.c:97
esp_err_t i2c_dev_give_mutex(i2c_dev_t *dev)
Give device mutex.
Definition: i2cdev.c:164
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:241
esp_err_t i2c_dev_take_mutex(i2c_dev_t *dev)
Take device mutex.
Definition: i2cdev.c:148
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:305
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:299
SemaphoreHandle_t mutex
Definition: i2cdev.h:65
i2c_port_t port
I2C port number.
Definition: i2cdev.h:66
uint8_t addr
Unshifted address.
Definition: i2cdev.h:68
i2c_config_t cfg
I2C driver configuration.
Definition: i2cdev.h:67
uint32_t timeout_ticks
Definition: i2cdev.h:70
SemaphoreHandle_t mutex
Device mutex.
Definition: i2cdev.h:69