Rishka
RISC-V virtual runtime in C/C++ made for ESP32-WROVER
I2C Class Referencefinal

Class for handling I2C operations in Rishka applications. More...

#include <i2c.h>

Static Public Member Functions

static bool begin (u8 address)
 Initialize the I2C communication with the specified address. More...
 
static bool end ()
 End the I2C communication. More...
 
static bool pins (u8 sda, u8 scl)
 Set the SDA and SCL pins for the I2C communication. More...
 
static void flush ()
 Flush the I2C buffer. More...
 
static void begin_transmission (u8 address)
 Begin an I2C transmission to the specified device address. More...
 
static u8 end_transmission (bool stop_bit)
 End an I2C transmission with an optional stop bit. More...
 
static usize write (u8 *data, usize size)
 Write data to the I2C bus. More...
 
static usize slave_write (u8 *data, usize size)
 Write data to the I2C bus as a slave device. More...
 
static usize set_buffersize (usize size)
 Set the buffer size for I2C transactions. More...
 
static i32 read ()
 Read data from the I2C bus. More...
 
static i32 peek ()
 Peek at the next byte on the I2C bus. More...
 
static i32 available ()
 Get the number of bytes available for reading on the I2C bus. More...
 
static usize request (u8 address, usize size, bool stop_bit)
 Request data from a remote I2C device. More...
 
static void on_receive (void(*callback)(int))
 Register a callback function for I2C receive events. More...
 
static void on_request (void(*callback)(void))
 Register a callback function for I2C request events. More...
 
static void set_timeout (u16 timeout)
 Set the timeout for I2C operations. More...
 
static u16 get_timeout ()
 Get the current timeout for I2C operations. More...
 
static bool set_clock (u32 clock)
 Set the clock frequency for I2C communication. More...
 
static u32 get_clock ()
 Get the current clock frequency for I2C communication. More...
 

Detailed Description

Class for handling I2C operations in Rishka applications.

The I2C class provides static methods for initializing, configuring, and communicating with I2C devices on ESP32-WROVER microcontrollers. It includes functionalities for setting up the I2C bus, beginning and ending I2C transactions, reading and writing data, registering callbacks for I2C events, and setting the clock frequency and timeout.

Member Function Documentation

◆ available()

i32 I2C::available ( )
static

Get the number of bytes available for reading on the I2C bus.

This method returns the number of bytes available for reading on the I2C bus. It can be used to determine the amount of data available for reading.

Returns
The number of bytes available for reading.

◆ begin()

bool I2C::begin ( u8  address)
static

Initialize the I2C communication with the specified address.

This method initializes the I2C communication with the specified 7-bit address. It should be called before any other I2C communication functions are used.

Parameters
addressThe 7-bit address of the I2C device.
Returns
True if initialization was successful, false otherwise.

◆ begin_transmission()

void I2C::begin_transmission ( u8  address)
static

Begin an I2C transmission to the specified device address.

This method begins an I2C transmission to the specified device address. Subsequent write operations will send data to the specified device.

Parameters
addressThe 7-bit address of the I2C device.

◆ end()

bool I2C::end ( )
static

End the I2C communication.

This method ends the I2C communication and releases the associated resources. It should be called to clean up the I2C interface after use.

Returns
True if ending the communication was successful, false otherwise.

◆ end_transmission()

u8 I2C::end_transmission ( bool  stop_bit)
static

End an I2C transmission with an optional stop bit.

This method ends an I2C transmission with an optional stop bit. If the stop_bit parameter is true, a stop condition will be generated on the bus after the transmission is complete.

Parameters
stop_bitTrue to generate a stop condition, false otherwise.
Returns
The status of the transmission (0 for success, non-zero for error).

◆ flush()

void I2C::flush ( )
static

Flush the I2C buffer.

This method flushes the I2C buffer, discarding any unread data. It can be called to clear the buffer before initiating a new transaction.

◆ get_clock()

u32 I2C::get_clock ( )
static

Get the current clock frequency for I2C communication.

This method returns the current clock frequency for I2C communication in Hertz (Hz).

Returns
The current clock frequency in Hertz (Hz).

◆ get_timeout()

u16 I2C::get_timeout ( )
static

Get the current timeout for I2C operations.

This method returns the current timeout for I2C operations in microseconds.

Returns
The current timeout value in microseconds.

◆ on_receive()

void I2C::on_receive ( void(*)(int)  callback)
static

Register a callback function for I2C receive events.

This method registers a callback function to be called when data is received on the I2C bus as a slave device. The callback function should take an integer parameter representing the number of bytes received.

Parameters
callbackPointer to the callback function.

◆ on_request()

void I2C::on_request ( void(*)(void)  callback)
static

Register a callback function for I2C request events.

This method registers a callback function to be called when a master device requests data from the I2C slave device. The callback function should not take any parameters.

Parameters
callbackPointer to the callback function.

◆ peek()

i32 I2C::peek ( )
static

Peek at the next byte on the I2C bus.

This method peeks at the next byte on the I2C bus without removing it from the buffer. It is useful for checking if data is available for reading.

Returns
The next byte on the I2C bus, or -1 if no data is available.

◆ pins()

bool I2C::pins ( u8  sda,
u8  scl 
)
static

Set the SDA and SCL pins for the I2C communication.

This method sets the SDA (data) and SCL (clock) pins for the I2C communication. It should be called to configure the GPIO pins used for I2C communication.

Parameters
sdaThe pin number for the SDA (data) line.
sclThe pin number for the SCL (clock) line.
Returns
True if setting the pins was successful, false otherwise.

◆ read()

i32 I2C::read ( )
static

Read data from the I2C bus.

This method reads data from the I2C bus received from the device specified in the begin_transmission() method.

Returns
The received data byte.

◆ request()

usize I2C::request ( u8  address,
usize  size,
bool  stop_bit 
)
static

Request data from a remote I2C device.

This method requests data from a remote I2C device with the specified address and size, optionally generating a stop condition after the request.

Parameters
addressThe 7-bit address of the remote I2C device.
sizeThe number of bytes to request.
stop_bitTrue to generate a stop condition after the request, false otherwise.
Returns
The number of bytes requested.

◆ set_buffersize()

usize I2C::set_buffersize ( usize  size)
static

Set the buffer size for I2C transactions.

This method sets the buffer size for I2C transactions. It should be called before beginning an I2C transmission.

Parameters
sizeThe buffer size in bytes.
Returns
True if setting the buffer size was successful, false otherwise.

◆ set_clock()

bool I2C::set_clock ( u32  clock)
static

Set the clock frequency for I2C communication.

This method sets the clock frequency for I2C communication in Hertz (Hz).

Parameters
clockThe clock frequency in Hertz (Hz).
Returns
True if setting the clock frequency was successful, false otherwise.

◆ set_timeout()

void I2C::set_timeout ( u16  timeout)
static

Set the timeout for I2C operations.

This method sets the timeout for I2C operations in microseconds. It specifies the maximum time allowed for an I2C operation to complete.

Parameters
timeoutThe timeout value in microseconds.

◆ slave_write()

usize I2C::slave_write ( u8 data,
usize  size 
)
static

Write data to the I2C bus as a slave device.

This method writes data to the I2C bus as a slave device. It is typically used in response to a master device's request.

Parameters
dataPointer to the data buffer.
sizeThe number of bytes to write.
Returns
The number of bytes written.

◆ write()

usize I2C::write ( u8 data,
usize  size 
)
static

Write data to the I2C bus.

This method writes data to the I2C bus for transmission to the device specified in the begin_transmission() method.

Parameters
dataPointer to the data buffer.
sizeThe number of bytes to write.
Returns
The number of bytes written.

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