FabGL
ESP32 Display Controller and Graphics Library
tsi2c.h
Go to the documentation of this file.
1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27#pragma once
28
29
30#ifdef ARDUINO
31
32
33#include <stdint.h>
34#include <stddef.h>
35
36#include "freertos/FreeRTOS.h"
37#include "freertos/queue.h"
38#include "freertos/semphr.h"
39
40#include "esp32-hal.h"
41
42#include "fabutils.h"
43#include "fabglconf.h"
44
45
46
54namespace fabgl {
55
56
57struct I2CJobInfo {
58 int32_t frequency;
59 uint8_t * buffer;
60 uint8_t address;
61 uint16_t size;
62 uint16_t timeout;
63 uint32_t readCount;
64 #if FABGL_ESP_IDF_VERSION < FABGL_ESP_IDF_VERSION_VAL(4, 4, 0)
65 i2c_err_t lastError;
66 #else
67 esp_err_t lastError;
68 #endif
69};
70
71
85class I2C {
86
87public:
88
94 I2C(int bus = 0);
95
96 ~I2C();
97
109 bool begin(gpio_num_t SDAGPIO, gpio_num_t SCLGPIO);
110
111 void end();
112
126 bool write(int address, uint8_t * buffer, int size, int frequency = 100000, int timeOutMS = 50);
127
141 int read(int address, uint8_t * buffer, int size, int frequency = 100000, int timeOutMS = 50);
142
148 int getMaxBufferLength() { return 128; }
149
150
151private:
152
153 static void commTaskFunc(void * pvParameters);
154
155 #if FABGL_ESP_IDF_VERSION < FABGL_ESP_IDF_VERSION_VAL(4, 4, 0)
156 i2c_t * m_i2c;
157 #endif
158
159 bool m_i2cAvailable;
160
161 uint8_t m_bus;
162 gpio_num_t m_SDAGPIO;
163 gpio_num_t m_SCLGPIO;
164
165 TaskHandle_t m_commTaskHandle;
166
167 EventGroupHandle_t m_eventGroup;
168
169 I2CJobInfo m_jobInfo;
170};
171
172
173
174
175
176
177
178
179
180
181} // end of namespace
182
183
184
185#endif // #ifdef ARDUINO
186
bool write(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Sends a buffer to I2C bus.
Definition: tsi2c.cpp:113
int getMaxBufferLength()
Returns maximum size of read and write buffers.
Definition: tsi2c.h:148
I2C(int bus=0)
I2C class constructor.
Definition: tsi2c.cpp:53
bool begin(gpio_num_t SDAGPIO, gpio_num_t SCLGPIO)
Initializes I2C instance associating GPIOs to I2C signals.
Definition: tsi2c.cpp:72
int read(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Receives a buffer from I2C bus.
Definition: tsi2c.cpp:141
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs.
Definition: tsi2c.h:85
This file contains FabGL library configuration settings, like number of supported colors,...
This file contains some utility classes and functions.