Skip to content

daisy::I2CHandle

More...

#include <i2c.h>

Public Classes

Name
struct Config

Public Types

Name
enum class Result
enum class Direction
typedef void(*)(void *context, Result result) CallbackFunctionPtr

Public Functions

Name
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)

Detailed Description

class daisy::I2CHandle;

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( ... );

Public Types Documentation

enum Result

Enumerator Value Description
OK &
ERR &

Return values for I2C functions.

enum Direction

Enumerator Value Description
TRANSMIT &
RECEIVE &

typedef CallbackFunctionPtr

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

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

Public Functions Documentation

function I2CHandle

inline I2CHandle()

function I2CHandle

I2CHandle(
    const I2CHandle & other
) =default

function operator=

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

function Init

Result Init(
    const Config & config
)

Initializes an I2C peripheral.


function GetConfig

const Config & GetConfig() const

Returns the current config.


function TransmitBlocking

Result TransmitBlocking(
    uint16_t address,
    uint8_t * data,
    uint16_t size,
    uint32_t timeout
)

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.

Transmits data and blocks until the transmission is complete. Use this for smaller transmissions of a few bytes.


function ReceiveBlocking

Result ReceiveBlocking(
    uint16_t address,
    uint8_t * data,
    uint16_t size,
    uint32_t timeout
)

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.

Receives data and blocks until the reception is complete. Use this for smaller transmissions of a few bytes.


function TransmitDma

Result TransmitDma(
    uint16_t address,
    uint8_t * data,
    uint16_t size,
    CallbackFunctionPtr callback,
    void * callback_context
)

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.

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.


function ReceiveDma

Result ReceiveDma(
    uint16_t address,
    uint8_t * data,
    uint16_t size,
    CallbackFunctionPtr callback,
    void * callback_context
)

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.

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.


function ReadDataAtAddress

Result ReadDataAtAddress(
    uint16_t address,
    uint16_t mem_address,
    uint16_t mem_address_size,
    uint8_t * data,
    uint16_t data_size,
    uint32_t timeout
)

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

Reads an amount of data from a specific memory address. This method will return an error if the I2C peripheral is in slave mode.


function WriteDataAtAddress

Result WriteDataAtAddress(
    uint16_t address,
    uint16_t mem_address,
    uint16_t mem_address_size,
    uint8_t * data,
    uint16_t data_size,
    uint32_t timeout
)

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

Writes an amount of data from a specific memory address. This method will return an error if the I2C peripheral is in slave mode.

---

Updated on 2024-01-03 at 19:41:00 +0000