Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Esp32HardwareI2C.hxx
Go to the documentation of this file.
1
37#ifndef _FREERTOS_DRIVERS_ESP32_ESP32HARDWAREI2C_HXX_
38#define _FREERTOS_DRIVERS_ESP32_ESP32HARDWAREI2C_HXX_
39
40#include "i2c.h"
41#include "i2c-dev.h"
42#include <unistd.h>
43#include <driver/i2c.h>
44#include <hal/gpio_types.h>
45#include <utils/Atomic.hxx>
46#include <vector>
47
48namespace openmrn_arduino
49{
50
82class Esp32HardwareI2C : private Atomic
83{
84public:
88 Esp32HardwareI2C(const char * const path = "/dev/i2c");
89
92
105 void hw_init(const gpio_num_t sda, const gpio_num_t scl,
106 const uint32_t bus_speed, const i2c_port_t port = I2C_NUM_0);
107
114 void scan(const i2c_port_t port);
115
128 ssize_t write(int fd, const void *buf, size_t size);
129
137 ssize_t read(int fd, void *buf, size_t size);
138
149 int open(const char *path, int flags, int mode);
150
159 int close(int fd);
160
168 int ioctl(int fd, int cmd, va_list args);
169
170private:
172 const char * const path_;
173
175 static constexpr TickType_t I2C_OP_TIMEOUT = pdMS_TO_TICKS(1000);
176
178 static constexpr TickType_t I2C_SCAN_TIMEOUT = pdMS_TO_TICKS(50);
179
182 static constexpr int I2C_ISR_FLAGS = ESP_INTR_FLAG_SHARED;
183
186 static constexpr size_t I2C_SLAVE_RX_BUF_SIZE = 0;
187
190 static constexpr size_t I2C_SLAVE_TX_BUF_SIZE = 0;
191
195 {
197 i2c_port_t port;
198
201
203 int fd;
204 };
205
207 std::vector<i2c_device_t> devices_;
208
210 bool i2cInitialized_[SOC_I2C_NUM];
211
213 bool vfsInitialized_{false};
214
223 int transfer_messages(const i2c_port_t port, struct i2c_msg *msgs, int num);
224};
225
226}; // namespace openmrn_arduino
227
229
230#endif // _FREERTOS_DRIVERS_ESP32_ESP32HARDWAREI2C_HXX_
Lightweight locking class for protecting small critical sections.
Definition Atomic.hxx:130
The ESP32 has one or two built-in I2C controller interfaces, this code provides a VFS interface that ...
bool vfsInitialized_
Internal tracking for the VFS adapter layer.
int transfer_messages(const i2c_port_t port, struct i2c_msg *msgs, int num)
Transfers multiple payloads to I2C devices.
static constexpr size_t I2C_SLAVE_RX_BUF_SIZE
I2C "Slave" RX buffer size, we do not use/support this feature so it is set to zero.
static constexpr int I2C_ISR_FLAGS
ISR flags to use for I2C, this defaults to allowing usage of a shared interupt.
const char *const path_
VFS Mount point.
static constexpr TickType_t I2C_SCAN_TIMEOUT
Timeout to use for I2C device scanning, default is 50 milliseconds.
void hw_init(const gpio_num_t sda, const gpio_num_t scl, const uint32_t bus_speed, const i2c_port_t port=I2C_NUM_0)
Initializes the underlying I2C controller hardware and VFS interface.
ssize_t read(int fd, void *buf, size_t size)
VFS implementation of read(fd, buf, size)
int ioctl(int fd, int cmd, va_list args)
VFS implementation of ioctl.
int open(const char *path, int flags, int mode)
VFS implementation of open(path, flags, mode).
bool i2cInitialized_[SOC_I2C_NUM]
Internal tracking for initialization of the underlying I2C hardware.
int close(int fd)
VFS implementation of close(fd).
void scan(const i2c_port_t port)
Utility method to perform a scan for all devices on the I2C bus.
Esp32HardwareI2C(const char *const path="/dev/i2c")
Constructor.
std::vector< i2c_device_t > devices_
Collection of I2C devices that have been opened.
ssize_t write(int fd, const void *buf, size_t size)
VFS implementation of write(fd, buf, size)
static constexpr size_t I2C_SLAVE_TX_BUF_SIZE
I2C "Slave" TX buffer size, we do not use/support this feature so it is set to zero.
static constexpr TickType_t I2C_OP_TIMEOUT
Timeout to use for I2C operations, default is one second.
Tracking structure used to map file handles to an I2C controller and address.
i2c_port_t port
I2C Controller that this file handle will use.
int fd
Assigned file handle for this entry.
int address
I2C address to use when greater than zero.