arduino-emulator
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
arduino::HardwareI2C_FTDI Class Reference

Implementation of I2C communication for FTDI FT2232HL using MPSSE mode. More...

#include <HardwareI2C_FTDI.h>

Inheritance diagram for arduino::HardwareI2C_FTDI:
arduino::HardwareI2C arduino::Stream arduino::Print

Public Member Functions

 HardwareI2C_FTDI (int channel=0)
 Constructor for FTDI I2C interface.
 
 ~HardwareI2C_FTDI ()
 Destructor - closes FTDI connection.
 
int available () override
 Get the number of bytes available for reading.
 
void begin () override
 Initialize I2C as master (same as begin()).
 
bool begin (int vendor_id=0x0403, int product_id=0x6010, const char *description=nullptr, const char *serial=nullptr)
 Initialize the I2C interface.
 
void begin (uint8_t address) override
 Initialize I2C as slave (not supported by FTDI).
 
void beginTransmission (uint8_t address) override
 Begin transmission to an I2C slave device.
 
void end () override
 Close the I2C interface and cleanup resources.
 
uint8_t endTransmission (bool stopBit=true) override
 End transmission and send data to slave device.
 
uint8_t endTransmission (void) override
 End transmission and send data to slave device (with stop condition).
 
void flush () override
 Flush the output buffer (no-op for I2C).
 
void onReceive (void(*function)(int)) override
 Register a function to be called when data is received as a slave.
 
void onRequest (void(*function)(void)) override
 Register a function to be called when master requests data from slave.
 
 operator bool ()
 Boolean conversion operator.
 
int peek () override
 Peek at the next byte without removing it from buffer.
 
int read () override
 Read a byte from the receive buffer.
 
size_t requestFrom (uint8_t address, size_t quantity) override
 
size_t requestFrom (uint8_t address, size_t quantity, bool stopBit=true) override
 Request data from an I2C slave device.
 
void setClock (uint32_t freq) override
 Set the I2C clock frequency.
 
size_t write (const uint8_t *data, size_t len) override
 Write multiple bytes to the I2C bus.
 
size_t write (uint8_t data) override
 Write a single byte to the I2C bus.
 
- Public Member Functions inherited from arduino::Stream
bool find (char target)
 
bool find (const char *target)
 
bool find (const char *target, size_t length)
 
bool find (const uint8_t *target)
 
bool find (const uint8_t *target, size_t length)
 
bool findUntil (const char *target, const char *terminator)
 
bool findUntil (const char *target, size_t targetLen, const char *terminate, size_t termLen)
 
bool findUntil (const uint8_t *target, const char *terminator)
 
bool findUntil (const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen)
 
unsigned long getTimeout (void)
 
float parseFloat (LookaheadMode lookahead=SKIP_ALL, char ignore=NO_IGNORE_CHAR)
 
long parseInt (LookaheadMode lookahead=SKIP_ALL, char ignore=NO_IGNORE_CHAR)
 
size_t readBytes (char *buffer, size_t length)
 
size_t readBytes (uint8_t *buffer, size_t length)
 
size_t readBytesUntil (char terminator, char *buffer, size_t length)
 
size_t readBytesUntil (char terminator, uint8_t *buffer, size_t length)
 
String readString ()
 
String readStringUntil (char terminator)
 
void setTimeout (unsigned long timeout)
 
- Public Member Functions inherited from arduino::Print
virtual int availableForWrite ()
 
void clearWriteError ()
 
int getWriteError ()
 
size_t print (char)
 
size_t print (const __FlashStringHelper *)
 
size_t print (const char[])
 
size_t print (const Printable &)
 
size_t print (const String &)
 
size_t print (double, int=2)
 
size_t print (int, int=DEC)
 
size_t print (long long, int=DEC)
 
size_t print (long, int=DEC)
 
size_t print (unsigned char, int=DEC)
 
size_t print (unsigned int, int=DEC)
 
size_t print (unsigned long long, int=DEC)
 
size_t print (unsigned long, int=DEC)
 
size_t println (char)
 
size_t println (const __FlashStringHelper *)
 
size_t println (const char[])
 
size_t println (const Printable &)
 
size_t println (const String &s)
 
size_t println (double, int=2)
 
size_t println (int, int=DEC)
 
size_t println (long long, int=DEC)
 
size_t println (long, int=DEC)
 
size_t println (unsigned char, int=DEC)
 
size_t println (unsigned int, int=DEC)
 
size_t println (unsigned long long, int=DEC)
 
size_t println (unsigned long, int=DEC)
 
size_t println (void)
 
size_t write (const char *buffer, size_t size)
 
size_t write (const char *str)
 

Protected Member Functions

bool configureMPSSE ()
 Configure FTDI device for I2C MPSSE mode.
 
uint8_t receiveByteSendAck (bool send_ack)
 Receive a byte over I2C and send ACK/NACK.
 
int receiveData (uint8_t *data, size_t length)
 Receive raw data from FTDI device.
 
bool sendByteCheckAck (uint8_t data)
 Send a byte over I2C and check for ACK.
 
int sendData (const uint8_t *data, size_t length)
 Send raw data to FTDI device.
 
bool sendRepeatedStart ()
 Send I2C repeated start condition.
 
bool sendStart ()
 Send I2C start condition.
 
bool sendStop ()
 Send I2C stop condition.
 
bool setClockFrequency (uint32_t frequency)
 Set the I2C clock frequency.
 
bool setLineStates (bool sda_state, bool scl_state)
 Set the state of SDA and SCL lines.
 
- Protected Member Functions inherited from arduino::Stream
int findMulti (struct MultiTarget *targets, int tCount)
 
float parseFloat (char ignore)
 
long parseInt (char ignore)
 
int peekNextDigit (LookaheadMode lookahead, bool detectDecimal)
 
int timedPeek ()
 
int timedRead ()
 
- Protected Member Functions inherited from arduino::Print
void setWriteError (int err=1)
 

Protected Attributes

uint8_t current_slave_address = 0
 
int ftdi_channel = 0
 
struct ftdi_context * ftdi_context = nullptr
 
uint32_t i2c_clock_frequency = 100000
 
bool is_open = false
 
uint8_t rx_buffer [32]
 
size_t rx_buffer_index = 0
 
size_t rx_buffer_length = 0
 
uint8_t tx_buffer [32]
 
size_t tx_buffer_length = 0
 
- Protected Attributes inherited from arduino::Stream
unsigned long _startMillis
 
unsigned long _timeout
 

Static Protected Attributes

static const uint8_t I2C_DATA_SHIFT_IN = 0x24
 
static const uint8_t I2C_DATA_SHIFT_OUT = 0x11
 
static const uint8_t I2C_DATA_SHIFT_OUT_IN = 0x31
 
static const uint8_t I2C_GET_DATA_BITS_HIGH_BYTE = 0x83
 
static const uint8_t I2C_GET_DATA_BITS_LOW_BYTE = 0x81
 
static const uint8_t I2C_SET_DATA_BITS_HIGH_BYTE = 0x82
 
static const uint8_t I2C_SET_DATA_BITS_LOW_BYTE = 0x80
 
static const uint8_t SCL_BIT = 0
 
static const uint8_t SDA_IN_BIT = 2
 
static const uint8_t SDA_OUT_BIT = 1
 

Detailed Description

Implementation of I2C communication for FTDI FT2232HL using MPSSE mode.

This class provides an interface to the I2C bus on FTDI FT2232HL devices using the Multi-Protocol Synchronous Serial Engine (MPSSE) mode. The FT2232HL can operate in MPSSE mode to provide I2C master functionality.

Pin mapping for I2C on FT2232HL (Channel A):

The class inherits from HardwareI2C and implements all required methods for I2C communication, including device addressing, data transfer, and bus management.

Note
This class is only available when USE_FTDI is defined.
Requires libftdi1 development library to be installed.
External pull-up resistors (typically 4.7kΩ) are required on SCL and SDA lines.

Constructor & Destructor Documentation

◆ HardwareI2C_FTDI()

arduino::HardwareI2C_FTDI::HardwareI2C_FTDI ( int  channel = 0)

Constructor for FTDI I2C interface.

Parameters
channelFTDI channel to use (0 for Channel A, 1 for Channel B)

Member Function Documentation

◆ available()

int arduino::HardwareI2C_FTDI::available ( void  )
overridevirtual

Get the number of bytes available for reading.

Returns
Number of bytes available

Implements arduino::Stream.

◆ begin() [1/3]

void arduino::HardwareI2C_FTDI::begin ( )
inlineoverridevirtual

Initialize I2C as master (same as begin()).

Implements arduino::HardwareI2C.

◆ begin() [2/3]

bool arduino::HardwareI2C_FTDI::begin ( int  vendor_id = 0x0403,
int  product_id = 0x6010,
const char description = nullptr,
const char serial = nullptr 
)

Initialize the I2C interface.

Parameters
vendor_idUSB vendor ID (default: 0x0403)
product_idUSB product ID (default: 0x6010 for FT2232HL)
descriptionDevice description string (optional)
serialDevice serial number (optional)
Returns
true if initialization successful, false otherwise

◆ begin() [3/3]

void arduino::HardwareI2C_FTDI::begin ( uint8_t  address)
overridevirtual

Initialize I2C as slave (not supported by FTDI).

Parameters
addressSlave address (ignored)

Implements arduino::HardwareI2C.

◆ beginTransmission()

void arduino::HardwareI2C_FTDI::beginTransmission ( uint8_t  address)
overridevirtual

Begin transmission to an I2C slave device.

Parameters
address7-bit slave address

Implements arduino::HardwareI2C.

◆ configureMPSSE()

bool arduino::HardwareI2C_FTDI::configureMPSSE ( )
protected

Configure FTDI device for I2C MPSSE mode.

Returns
true if successful, false on error

◆ end()

void arduino::HardwareI2C_FTDI::end ( )
overridevirtual

Close the I2C interface and cleanup resources.

Implements arduino::HardwareI2C.

◆ endTransmission() [1/2]

uint8_t arduino::HardwareI2C_FTDI::endTransmission ( bool  stopBit = true)
overridevirtual

End transmission and send data to slave device.

Parameters
stopBitWhether to send stop condition (default: true)
Returns
0 on success, error code on failure

Implements arduino::HardwareI2C.

◆ endTransmission() [2/2]

uint8_t arduino::HardwareI2C_FTDI::endTransmission ( void  )
overridevirtual

End transmission and send data to slave device (with stop condition).

Returns
0 on success, error code on failure

Implements arduino::HardwareI2C.

◆ flush()

void arduino::HardwareI2C_FTDI::flush ( void  )
overridevirtual

Flush the output buffer (no-op for I2C).

Reimplemented from arduino::Print.

◆ onReceive()

void arduino::HardwareI2C_FTDI::onReceive ( void(*)(int function)
overridevirtual

Register a function to be called when data is received as a slave.

Parameters
functionCallback function (not supported by FTDI master-only mode)
Note
FTDI operates in master mode only - slave callbacks not supported

Implements arduino::HardwareI2C.

◆ onRequest()

void arduino::HardwareI2C_FTDI::onRequest ( void(*)(void function)
overridevirtual

Register a function to be called when master requests data from slave.

Parameters
functionCallback function (not supported by FTDI master-only mode)
Note
FTDI operates in master mode only - slave callbacks not supported

Implements arduino::HardwareI2C.

◆ operator bool()

arduino::HardwareI2C_FTDI::operator bool ( )
inline

Boolean conversion operator.

Returns
true if the FTDI I2C interface is open and initialized, false otherwise.

◆ peek()

int arduino::HardwareI2C_FTDI::peek ( void  )
overridevirtual

Peek at the next byte without removing it from buffer.

Returns
Next byte, or -1 if no data available

Implements arduino::Stream.

◆ read()

int arduino::HardwareI2C_FTDI::read ( void  )
overridevirtual

Read a byte from the receive buffer.

Returns
Received byte, or -1 if no data available

Implements arduino::Stream.

◆ receiveByteSendAck()

uint8_t arduino::HardwareI2C_FTDI::receiveByteSendAck ( bool  send_ack)
protected

Receive a byte over I2C and send ACK/NACK.

Parameters
send_ackWhether to send ACK (true) or NACK (false)
Returns
Received byte

◆ receiveData()

int arduino::HardwareI2C_FTDI::receiveData ( uint8_t data,
size_t  length 
)
protected

Receive raw data from FTDI device.

Parameters
dataPointer to receive buffer
lengthNumber of bytes to receive
Returns
Number of bytes actually received, or negative on error

◆ requestFrom() [1/2]

size_t arduino::HardwareI2C_FTDI::requestFrom ( uint8_t  address,
size_t  quantity 
)
overridevirtual

Implements arduino::HardwareI2C.

◆ requestFrom() [2/2]

size_t arduino::HardwareI2C_FTDI::requestFrom ( uint8_t  address,
size_t  quantity,
bool  stopBit = true 
)
overridevirtual

Request data from an I2C slave device.

Parameters
address7-bit slave address
quantityNumber of bytes to request
stopBitWhether to send stop condition (default: true)
Returns
Number of bytes received

Implements arduino::HardwareI2C.

◆ sendByteCheckAck()

bool arduino::HardwareI2C_FTDI::sendByteCheckAck ( uint8_t  data)
protected

Send a byte over I2C and check for ACK.

Parameters
dataByte to send
Returns
true if ACK received, false if NACK or error

◆ sendData()

int arduino::HardwareI2C_FTDI::sendData ( const uint8_t data,
size_t  length 
)
protected

Send raw data to FTDI device.

Parameters
dataPointer to data buffer
lengthNumber of bytes to send
Returns
Number of bytes actually sent, or negative on error

◆ sendRepeatedStart()

bool arduino::HardwareI2C_FTDI::sendRepeatedStart ( )
protected

Send I2C repeated start condition.

Returns
true if successful, false on error

◆ sendStart()

bool arduino::HardwareI2C_FTDI::sendStart ( )
protected

Send I2C start condition.

Returns
true if successful, false on error

◆ sendStop()

bool arduino::HardwareI2C_FTDI::sendStop ( )
protected

Send I2C stop condition.

Returns
true if successful, false on error

◆ setClock()

void arduino::HardwareI2C_FTDI::setClock ( uint32_t  freq)
overridevirtual

Set the I2C clock frequency.

Parameters
freqFrequency in Hz (default: 100000 for 100kHz)

Implements arduino::HardwareI2C.

◆ setClockFrequency()

bool arduino::HardwareI2C_FTDI::setClockFrequency ( uint32_t  frequency)
protected

Set the I2C clock frequency.

Parameters
frequencyClock frequency in Hz
Returns
true if successful, false on error

◆ setLineStates()

bool arduino::HardwareI2C_FTDI::setLineStates ( bool  sda_state,
bool  scl_state 
)
protected

Set the state of SDA and SCL lines.

Parameters
sda_stateState of SDA line (true = high, false = low)
scl_stateState of SCL line (true = high, false = low)
Returns
true if successful, false on error

◆ write() [1/2]

size_t arduino::HardwareI2C_FTDI::write ( const uint8_t data,
size_t  len 
)
overridevirtual

Write multiple bytes to the I2C bus.

Parameters
dataPointer to data buffer
lenNumber of bytes to write
Returns
Number of bytes written

Reimplemented from arduino::Print.

◆ write() [2/2]

size_t arduino::HardwareI2C_FTDI::write ( uint8_t  data)
overridevirtual

Write a single byte to the I2C bus.

Parameters
dataByte to write
Returns
Number of bytes written (1 on success, 0 on error)

Implements arduino::Print.


The documentation for this class was generated from the following files: