Skip to content

Class daisy::I2CHandle

ClassList > daisy > I2CHandle

More...

  • #include <i2c.h>

Classes

Type Name
struct Config

Public Types

Type Name
typedef void(* CallbackFunctionPtr
enum Direction
enum Result

Public Functions

Type Name
const Config & GetConfig () const
I2CHandle ()
I2CHandle (const I2CHandle & other) = default
Result Init (const Config & config)
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 ReceiveBlocking (uint16_t address, uint8_t * data, uint16_t size, uint32_t timeout)
Result ReceiveDma (uint16_t address, uint8_t * data, uint16_t size, CallbackFunctionPtr callback, void * callback_context)
Result TransmitBlocking (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 WriteDataAtAddress (uint16_t address, uint16_t mem_address, uint16_t mem_address_size, uint8_t * data, uint16_t data_size, uint32_t timeout)
I2CHandle & operator= (const I2CHandle & other) = default

Detailed Description

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:

Public Types Documentation

typedef CallbackFunctionPtr

typedef void(* daisy::I2CHandle::CallbackFunctionPtr) (void *context, Result result);

A callback to be executed when a dma transfer is complete.


enum Direction

enum daisy::I2CHandle::Direction {
    TRANSMIT,
    RECEIVE
};

enum Result

enum daisy::I2CHandle::Result {
    OK,
    ERR
};

Return values for I2C functions.


Public Functions Documentation

function GetConfig

const  Config & daisy::I2CHandle::GetConfig () const

Returns the current config.


function I2CHandle [½]

inline daisy::I2CHandle::I2CHandle () 

function I2CHandle [2/2]

daisy::I2CHandle::I2CHandle (
    const  I2CHandle & other
) = default

function Init

Result daisy::I2CHandle::Init (
    const  Config & config
) 

Initializes an I2C peripheral.


function ReadDataAtAddress

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 / register. This method will return an error if the I2C peripheral is in slave mode. This method is equivalent to calling TransmitBlocking to send mem_address, followed immediately by a call to ReceiveBlocking.

Warning:

Breaking change: In libDaisy versions v7.1.0 and below, ReadDataAtAddress required that the address parameter be left-shifted by one bit. This has been changed in subsequent versions, so now ReadDataAtAddress, ReceiveBlocking, and ReceiveDma all use the same address convention. When updating libDaisy, you may need to remove any address left-shifting that you have applied.

Parameters:

  • 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 (either 1 or 2).
  • 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

function ReceiveBlocking

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.

Parameters:

  • 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.

function ReceiveDma

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.

Parameters:

  • 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.

function TransmitBlocking

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.

Parameters:

  • 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.

function TransmitDma

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.

Parameters:

  • 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.

function WriteDataAtAddress

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 to a specific memory address / register. This method will return an error if the I2C peripheral is in slave mode. This method is equivalent to calling TransmitBlocking to send mem_address, followed immediately by another call to TransmitBlocking to send data.

Warning:

Breaking change: In libDaisy versions v7.1.0 and below, WriteDataAtAddress required that the address parameter be left-shifted by one bit. This has been changed in subsequent versions, so now WriteDataAtAddress, TransmitBlocking, and TransmitDma all use the same address convention. When updating libDaisy, you may need to remove any address left-shifting that you have applied.

Parameters:

  • 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 (either 1 or 2).
  • 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

function operator=

I2CHandle & daisy::I2CHandle::operator= (
    const  I2CHandle & other
) = default


The documentation for this class was generated from the following file external-docs/libDaisy/src/per/i2c.h