A handle for interacting with an I2C peripheral. This is a dumb gateway that internally points to one of the four I2C peripherals after it was initialised. It can then be copied and passed around. Use an I2CHandle like this:
// setup the configuration I2CHandle::Config i2c_conf; i2c_conf.periph = I2CHandle::Config::Peripheral::I2C_1; i2c_conf.speed = I2CHandle::Config::Speed::I2C_400KHZ; i2c_conf.mode = I2CHandle::Config::Mode::Master; i2c_conf.pin_config.scl = {DSY_GPIOB, 8}; i2c_conf.pin_config.sda = {DSY_GPIOB, 9}; // initialise the peripheral I2CHandle i2c; i2c.Init(i2c_conf); // now i2c points to the corresponding peripheral and can be used. i2c.TransmitBlocking( ... );
#include <i2c.h>
Classes | |
struct | Config |
Public Types | |
enum class | Result { OK , ERR } |
enum class | Direction { TRANSMIT , RECEIVE } |
typedef void(* | CallbackFunctionPtr) (void *context, Result result) |
Public Member Functions | |
I2CHandle () | |
I2CHandle (const I2CHandle &other)=default | |
I2CHandle & | operator= (const I2CHandle &other)=default |
Result | Init (const Config &config) |
const Config & | GetConfig () const |
Result | TransmitBlocking (uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout) |
Result | ReceiveBlocking (uint16_t address, uint8_t *data, uint16_t size, uint32_t timeout) |
Result | TransmitDma (uint16_t address, uint8_t *data, uint16_t size, CallbackFunctionPtr callback, void *callback_context) |
Result | ReceiveDma (uint16_t address, uint8_t *data, uint16_t size, CallbackFunctionPtr callback, void *callback_context) |
Result | ReadDataAtAddress (uint16_t address, uint16_t mem_address, uint16_t mem_address_size, uint8_t *data, uint16_t data_size, uint32_t timeout) |
Result | WriteDataAtAddress (uint16_t address, uint16_t mem_address, uint16_t mem_address_size, uint8_t *data, uint16_t data_size, uint32_t timeout) |
A callback to be executed when a dma transfer is complete.
|
strong |
|
strong |
|
inline |
Result daisy::I2CHandle::ReadDataAtAddress | ( | uint16_t | address, |
uint16_t | mem_address, | ||
uint16_t | mem_address_size, | ||
uint8_t * | data, | ||
uint16_t | data_size, | ||
uint32_t | timeout | ||
) |
Reads an amount of data from a specific memory address. This method will return an error if the I2C peripheral is in slave mode.
address | The slave device address. |
mem_address | Pointer to data containing the address to read from device. |
mem_address_size | Size of the memory address in bytes. |
data | Pointer to buffer that will be filled with contents at mem_address |
data_size | Size of the data to be read in bytes. |
timeout | The timeout in milliseconds before returning without communication |
Result daisy::I2CHandle::ReceiveBlocking | ( | uint16_t | address, |
uint8_t * | data, | ||
uint16_t | size, | ||
uint32_t | timeout | ||
) |
Receives data and blocks until the reception is complete. Use this for smaller transmissions of a few bytes.
address | The slave device address. Unused in slave mode. |
data | A pointer to the data to be received. |
size | The size of the data to be received, in bytes. |
timeout | A timeout. |
Result daisy::I2CHandle::ReceiveDma | ( | uint16_t | address, |
uint8_t * | data, | ||
uint16_t | size, | ||
CallbackFunctionPtr | callback, | ||
void * | callback_context | ||
) |
Receives data with a DMA and returns immediately. Use this for larger transmissions. The pointer to data must be located in the D2 memory domain by adding the DMA_BUFFER_MEM_SECTION
attribute like this: uint8_t DMA_BUFFER_MEM_SECTION my_buffer[100]; If that is not possible for some reason, you MUST clear the cachelines spanning the size of the buffer, before initiating the dma transfer by calling dsy_dma_clear_cache_for_buffer(buffer, size);
A single DMA is shared across I2C, I2C2 and I2C3. I2C4 has no DMA support (yet). If the DMA is busy with another transfer, the job will be queued and executed later. If there is a job waiting to be executed for this I2C peripheral, this function will block until the queue is free and the job can be queued.
address | The slave device address. Unused in slave mode. |
data | A pointer to the data buffer. |
size | The size of the data to be received, in bytes. |
callback | A callback to execute when the transfer finishes, or NULL. |
callback_context | A pointer that will be passed back to you in the callback. |
Result daisy::I2CHandle::TransmitBlocking | ( | uint16_t | address, |
uint8_t * | data, | ||
uint16_t | size, | ||
uint32_t | timeout | ||
) |
Transmits data and blocks until the transmission is complete. Use this for smaller transmissions of a few bytes.
address | The slave device address. Unused in slave mode. |
data | A pointer to the data to be sent. |
size | The size of the data to be sent, in bytes. |
timeout | A timeout. |
Result daisy::I2CHandle::TransmitDma | ( | uint16_t | address, |
uint8_t * | data, | ||
uint16_t | size, | ||
CallbackFunctionPtr | callback, | ||
void * | callback_context | ||
) |
Transmits data with a DMA and returns immediately. Use this for larger transmissions. The pointer to data must be located in the D2 memory domain by adding the DMA_BUFFER_MEM_SECTION
attribute like this: uint8_t DMA_BUFFER_MEM_SECTION my_buffer[100]; If that is not possible for some reason, you MUST clear the cachelines spanning the size of the buffer, before initiating the dma transfer by calling dsy_dma_clear_cache_for_buffer(buffer, size);
A single DMA is shared across I2C1, I2C2 and I2C3. I2C4 has no DMA support (yet). If the DMA is busy with another transfer, the job will be queued and executed later. If there is a job waiting to be executed for this I2C peripheral, this function will block until the queue is free and the job can be queued.
address | The slave device address. Unused in slave mode. |
data | A pointer to the data to be sent. |
size | The size of the data to be sent, in bytes. |
callback | A callback to execute when the transfer finishes, or NULL. |
callback_context | A pointer that will be passed back to you in the callback. |
Result daisy::I2CHandle::WriteDataAtAddress | ( | uint16_t | address, |
uint16_t | mem_address, | ||
uint16_t | mem_address_size, | ||
uint8_t * | data, | ||
uint16_t | data_size, | ||
uint32_t | timeout | ||
) |
Writes an amount of data from a specific memory address. This method will return an error if the I2C peripheral is in slave mode.
address | The slave device address. |
mem_address | Pointer to data containing the address to write to device. |
mem_address_size | Size of the memory address in bytes. |
data | Pointer to buffer that will be written to the mem_address |
data_size | Size of the data to be written in bytes. |
timeout | The timeout in milliseconds before returning without communication |