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-2021 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 "fabglconf.h"
43 
44 
45 
53 namespace fabgl {
54 
55 
56 struct I2CJobInfo {
57  int32_t frequency;
58  uint8_t * buffer;
59  uint8_t address;
60  uint16_t size;
61  uint16_t timeout;
62  uint32_t readCount;
63  i2c_err_t lastError;
64 };
65 
66 
80 class I2C {
81 
82 public:
83 
89  I2C(int bus = 0);
90 
91  ~I2C();
92 
104  bool begin(gpio_num_t SDAGPIO, gpio_num_t SCLGPIO);
105 
106  void end();
107 
121  bool write(int address, uint8_t * buffer, int size, int frequency = 100000, int timeOutMS = 50);
122 
136  int read(int address, uint8_t * buffer, int size, int frequency = 100000, int timeOutMS = 50);
137 
143  int getMaxBufferLength() { return 128; }
144 
145 
146 private:
147 
148  static void commTaskFunc(void * pvParameters);
149 
150 
151  i2c_t * m_i2c;
152 
153  uint8_t m_bus;
154  gpio_num_t m_SDAGPIO;
155  gpio_num_t m_SCLGPIO;
156 
157  TaskHandle_t m_commTaskHandle;
158 
159  EventGroupHandle_t m_eventGroup;
160 
161  I2CJobInfo m_jobInfo;
162 };
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 } // end of namespace
174 
175 
176 
177 #endif // #ifdef ARDUINO
178 
int getMaxBufferLength()
Returns maximum size of read and write buffers.
Definition: tsi2c.h:143
bool begin(gpio_num_t SDAGPIO, gpio_num_t SCLGPIO)
Initializes I2C instance associating GPIOs to I2C signals.
Definition: tsi2c.cpp:68
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs...
Definition: tsi2c.h:80
I2C(int bus=0)
I2C class constructor.
Definition: tsi2c.cpp:53
int read(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Receives a buffer from I2C bus.
Definition: tsi2c.cpp:130
Definition: canvas.cpp:36
This file contains FabGL library configuration settings, like number of supported colors...
bool write(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Sends a buffer to I2C bus.
Definition: tsi2c.cpp:105