arduino-emulator
Loading...
Searching...
No Matches
HardwareI2C_FTDI.h
1#pragma once
2/*
3 HardwareI2C_FTDI.h
4 Copyright (c) 2025 Phil Schatzmann. All right reserved.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20#ifdef USE_FTDI
21#include <inttypes.h>
22#include <ftdi.h>
23// Undefine DEPRECATED macro from libftdi1 to avoid conflict with Arduino API
24#ifdef DEPRECATED
25#undef DEPRECATED
26#endif
27
28#include "api/Common.h"
29#include "api/HardwareI2C.h"
30#include "api/Stream.h"
31
32namespace arduino {
33
56 public:
62
67
76 bool begin(int vendor_id = 0x0403, int product_id = 0x6010,
77 const char* description = nullptr, const char* serial = nullptr);
78
82 void begin() override { begin(0x0403, 0x6010); }
83
88 void begin(uint8_t address) override;
89
93 void end() override;
94
99 void setClock(uint32_t freq) override;
100
105 void beginTransmission(uint8_t address) override;
106
112 size_t write(uint8_t data) override;
113
120 size_t write(const uint8_t* data, size_t len) override;
121
127 uint8_t endTransmission(bool stopBit = true) override;
128
133 uint8_t endTransmission(void) override;
134
142 size_t requestFrom(uint8_t address, size_t quantity, bool stopBit = true) override;
143 size_t requestFrom(uint8_t address, size_t quantity) override;
144
149 int available() override;
150
155 int peek() override;
156
161 int read() override;
162
166 void flush() override;
167
173 void onReceive(void(*function)(int)) override;
174
180 void onRequest(void(*function)(void)) override;
181
186 operator bool() { return is_open && ftdi_context != nullptr; }
187
188 protected:
189 struct ftdi_context* ftdi_context = nullptr;
190 int ftdi_channel = 0; // 0 for Channel A, 1 for Channel B
191 bool is_open = false;
192
193 // I2C state
194 uint8_t current_slave_address = 0;
195 uint32_t i2c_clock_frequency = 100000; // Default 100kHz
196
197 // Transmission buffer
198 uint8_t tx_buffer[32]; // Buffer for outgoing data
199 size_t tx_buffer_length = 0;
200
201 // Receive buffer
202 uint8_t rx_buffer[32]; // Buffer for incoming data
203 size_t rx_buffer_length = 0;
204 size_t rx_buffer_index = 0;
205
206 // MPSSE I2C command constants
207 static const uint8_t I2C_DATA_SHIFT_OUT = 0x11;
208 static const uint8_t I2C_DATA_SHIFT_IN = 0x24;
209 static const uint8_t I2C_DATA_SHIFT_OUT_IN = 0x31;
210 static const uint8_t I2C_SET_DATA_BITS_LOW_BYTE = 0x80;
211 static const uint8_t I2C_SET_DATA_BITS_HIGH_BYTE = 0x82;
212 static const uint8_t I2C_GET_DATA_BITS_LOW_BYTE = 0x81;
213 static const uint8_t I2C_GET_DATA_BITS_HIGH_BYTE = 0x83;
214
215 // Pin assignments for I2C on ADBUS
216 static const uint8_t SCL_BIT = 0; // ADBUS0
217 static const uint8_t SDA_OUT_BIT = 1; // ADBUS1
218 static const uint8_t SDA_IN_BIT = 2; // ADBUS2
219
224 bool configureMPSSE();
225
231 bool setClockFrequency(uint32_t frequency);
232
237 bool sendStart();
238
243 bool sendStop();
244
249 bool sendRepeatedStart();
250
256 bool sendByteCheckAck(uint8_t data);
257
264
271 int sendData(const uint8_t* data, size_t length);
272
279 int receiveData(uint8_t* data, size_t length);
280
287 bool setLineStates(bool sda_state, bool scl_state);
288};
289
290} // namespace arduino
291
292#endif // USE_FTDI
Definition DMAPool.h:103
Implementation of I2C communication for FTDI FT2232HL using MPSSE mode.
Definition HardwareI2C_FTDI.h:55
bool setLineStates(bool sda_state, bool scl_state)
Set the state of SDA and SCL lines.
Definition HardwareI2C_FTDI.cpp:399
int receiveData(uint8_t *data, size_t length)
Receive raw data from FTDI device.
Definition HardwareI2C_FTDI.cpp:392
uint8_t endTransmission(void) override
End transmission and send data to slave device (with stop condition).
Definition HardwareI2C_FTDI.cpp:425
uint8_t receiveByteSendAck(bool send_ack)
Receive a byte over I2C and send ACK/NACK.
Definition HardwareI2C_FTDI.cpp:356
void end() override
Close the I2C interface and cleanup resources.
Definition HardwareI2C_FTDI.cpp:97
bool setClockFrequency(uint32_t frequency)
Set the I2C clock frequency.
Definition HardwareI2C_FTDI.cpp:280
bool configureMPSSE()
Configure FTDI device for I2C MPSSE mode.
Definition HardwareI2C_FTDI.cpp:244
void setClock(uint32_t freq) override
Set the I2C clock frequency.
Definition HardwareI2C_FTDI.cpp:109
int peek() override
Peek at the next byte without removing it from buffer.
Definition HardwareI2C_FTDI.cpp:226
bool sendStart()
Send I2C start condition.
Definition HardwareI2C_FTDI.cpp:302
bool sendByteCheckAck(uint8_t data)
Send a byte over I2C and check for ACK.
Definition HardwareI2C_FTDI.cpp:326
~HardwareI2C_FTDI()
Destructor - closes FTDI connection.
Definition HardwareI2C_FTDI.cpp:33
void onReceive(void(*function)(int)) override
Register a function to be called when data is received as a slave.
Definition HardwareI2C_FTDI.cpp:429
int read() override
Read a byte from the receive buffer.
Definition HardwareI2C_FTDI.cpp:233
size_t write(uint8_t data) override
Write a single byte to the I2C bus.
Definition HardwareI2C_FTDI.cpp:121
bool sendRepeatedStart()
Send I2C repeated start condition.
Definition HardwareI2C_FTDI.cpp:322
void flush() override
Flush the output buffer (no-op for I2C).
Definition HardwareI2C_FTDI.cpp:240
bool sendStop()
Send I2C stop condition.
Definition HardwareI2C_FTDI.cpp:312
size_t requestFrom(uint8_t address, size_t quantity, bool stopBit=true) override
Request data from an I2C slave device.
Definition HardwareI2C_FTDI.cpp:180
void onRequest(void(*function)(void)) override
Register a function to be called when master requests data from slave.
Definition HardwareI2C_FTDI.cpp:434
void beginTransmission(uint8_t address) override
Begin transmission to an I2C slave device.
Definition HardwareI2C_FTDI.cpp:116
int available() override
Get the number of bytes available for reading.
Definition HardwareI2C_FTDI.cpp:222
int sendData(const uint8_t *data, size_t length)
Send raw data to FTDI device.
Definition HardwareI2C_FTDI.cpp:385
void begin() override
Initialize I2C as master (same as begin()).
Definition HardwareI2C_FTDI.h:82
Definition HardwareI2C.h:28
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31