30#include "freertos/FreeRTOS.h"
31#include "freertos/task.h"
36#pragma GCC optimize ("O2")
42#define I2C_COMMTASK_STACK 1000
43#define I2C_COMMTASK_PRIORITY 5
44#define I2C_DEFAULT_FREQUENCY 100000
47#define EVTGROUP_READY (1 << 0)
48#define EVTGROUP_WRITE (1 << 1)
49#define EVTGROUP_READ (1 << 2)
50#define EVTGROUP_DONE (1 << 3)
55 #if FABGL_ESP_IDF_VERSION < FABGL_ESP_IDF_VERSION_VAL(4, 4, 0)
58 m_i2cAvailable(false),
60 m_commTaskHandle(nullptr),
77 m_eventGroup = xEventGroupCreate();
81 xTaskCreatePinnedToCore(&commTaskFunc,
"", I2C_COMMTASK_STACK,
this, I2C_COMMTASK_PRIORITY, &m_commTaskHandle, 0);
84 xEventGroupWaitBits(m_eventGroup, EVTGROUP_DONE,
true,
false, portMAX_DELAY);
87 xEventGroupSetBits(m_eventGroup, EVTGROUP_READY);
89 return m_i2cAvailable;
96 vTaskDelete(m_commTaskHandle);
97 m_commTaskHandle =
nullptr;
99 #if FABGL_ESP_IDF_VERSION < FABGL_ESP_IDF_VERSION_VAL(4, 4, 0)
106 vEventGroupDelete(m_eventGroup);
107 m_eventGroup =
nullptr;
109 m_i2cAvailable =
false;
113bool I2C::write(
int address, uint8_t * buffer,
int size,
int frequency,
int timeOutMS)
116 xEventGroupWaitBits(m_eventGroup, EVTGROUP_READY,
true,
false, portMAX_DELAY);
118 m_jobInfo.frequency = frequency;
119 m_jobInfo.address = address;
120 m_jobInfo.buffer = buffer;
121 m_jobInfo.size = size;
122 m_jobInfo.timeout = timeOutMS;
126 xEventGroupSync(m_eventGroup, EVTGROUP_WRITE, EVTGROUP_DONE, portMAX_DELAY);
128 #if FABGL_ESP_IDF_VERSION < FABGL_ESP_IDF_VERSION_VAL(4, 4, 0)
129 bool ret = (m_jobInfo.lastError == I2C_ERROR_OK);
131 bool ret = (m_jobInfo.lastError == ESP_OK);
135 xEventGroupSetBits(m_eventGroup, EVTGROUP_READY);
141int I2C::read(
int address, uint8_t * buffer,
int size,
int frequency,
int timeOutMS)
144 xEventGroupWaitBits(m_eventGroup, EVTGROUP_READY,
true,
false, portMAX_DELAY);
146 m_jobInfo.frequency = frequency;
147 m_jobInfo.address = address;
148 m_jobInfo.buffer = buffer;
149 m_jobInfo.size = size;
150 m_jobInfo.timeout = timeOutMS;
154 xEventGroupSync(m_eventGroup, EVTGROUP_READ, EVTGROUP_DONE, portMAX_DELAY);
156 int ret = m_jobInfo.readCount;
159 xEventGroupSetBits(m_eventGroup, EVTGROUP_READY);
165#if FABGL_ESP_IDF_VERSION >= FABGL_ESP_IDF_VERSION_VAL(4, 4, 0)
166static int i2cGetFrequency(uint8_t i2c_num)
169 i2cGetClock(i2c_num, &r);
172static void i2cSetFrequency(uint8_t i2c_num,
int freq)
174 i2cSetClock(i2c_num, freq);
179void I2C::commTaskFunc(
void * pvParameters)
181 I2C * ths = (
I2C*) pvParameters;
183 auto initRes = i2cInit(ths->m_bus, ths->m_SDAGPIO, ths->m_SCLGPIO, I2C_DEFAULT_FREQUENCY);
185 #if FABGL_ESP_IDF_VERSION < FABGL_ESP_IDF_VERSION_VAL(4, 4, 0)
187 ESP_LOGE(
"FabGL",
"unable to init I2C");
194 if (initRes != ESP_OK) {
195 ESP_LOGE(
"FabGL",
"unable to init I2C");
198 auto i2c = ths->m_bus;
201 ths->m_i2cAvailable =
true;
204 int freq = i2cGetFrequency(i2c);
206 I2CJobInfo * job = &ths->m_jobInfo;
212 xEventGroupSetBits(ths->m_eventGroup, EVTGROUP_DONE);
215 auto bits = xEventGroupWaitBits(ths->m_eventGroup, EVTGROUP_WRITE | EVTGROUP_READ,
true,
false, portMAX_DELAY);
218 if (freq != job->frequency) {
219 freq = job->frequency;
220 i2cSetFrequency(i2c, freq);
223 #if FABGL_ESP_IDF_VERSION < FABGL_ESP_IDF_VERSION_VAL(4, 4, 0)
224 if (bits & EVTGROUP_WRITE)
225 job->lastError = i2cWrite(i2c, job->address, job->buffer, job->size,
true, job->timeout);
226 else if (bits & EVTGROUP_READ)
227 job->lastError = i2cRead(i2c, job->address, job->buffer, job->size,
true, job->timeout, &job->readCount);
229 if (bits & EVTGROUP_WRITE)
230 job->lastError = i2cWrite(i2c, job->address, job->buffer, job->size, job->timeout);
231 else if (bits & EVTGROUP_READ)
232 job->lastError = i2cRead(i2c, job->address, job->buffer, job->size, job->timeout, &job->readCount);
bool write(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Sends a buffer to I2C bus.
I2C(int bus=0)
I2C class constructor.
bool begin(gpio_num_t SDAGPIO, gpio_num_t SCLGPIO)
Initializes I2C instance associating GPIOs to I2C signals.
int read(int address, uint8_t *buffer, int size, int frequency=100000, int timeOutMS=50)
Receives a buffer from I2C bus.
This file contains fabgl::I2C definition.