libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
daisy::I2CHandle Class Reference

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:

// 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
 
I2CHandleoperator= (const I2CHandle &other)=default
 
Result Init (const Config &config)
 
const ConfigGetConfig () 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)
 

Member Typedef Documentation

◆ CallbackFunctionPtr

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

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

Member Enumeration Documentation

◆ Direction

Enumerator
TRANSMIT 

&

RECEIVE 

&

◆ Result

Return values for I2C functions.

Enumerator
OK 

&

ERR 

&

Constructor & Destructor Documentation

◆ I2CHandle() [1/2]

daisy::I2CHandle::I2CHandle ( )
inline

◆ I2CHandle() [2/2]

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

Member Function Documentation

◆ GetConfig()

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

Returns the current config.

◆ Init()

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

Initializes an I2C peripheral.

◆ operator=()

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

◆ 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. This method will return an error if the I2C peripheral is in slave mode.

Parameters
addressThe slave device address.
mem_addressPointer to data containing the address to read from device.
mem_address_sizeSize of the memory address in bytes.
dataPointer to buffer that will be filled with contents at mem_address
data_sizeSize of the data to be read in bytes.
timeoutThe timeout in milliseconds before returning without communication

◆ 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
addressThe slave device address. Unused in slave mode.
dataA pointer to the data to be received.
sizeThe size of the data to be received, in bytes.
timeoutA timeout.

◆ 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
addressThe slave device address. Unused in slave mode.
dataA pointer to the data buffer.
sizeThe size of the data to be received, in bytes.
callbackA callback to execute when the transfer finishes, or NULL.
callback_contextA pointer that will be passed back to you in the callback.

◆ 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
addressThe slave device address. Unused in slave mode.
dataA pointer to the data to be sent.
sizeThe size of the data to be sent, in bytes.
timeoutA timeout.

◆ 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
addressThe slave device address. Unused in slave mode.
dataA pointer to the data to be sent.
sizeThe size of the data to be sent, in bytes.
callbackA callback to execute when the transfer finishes, or NULL.
callback_contextA pointer that will be passed back to you in the callback.

◆ 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 from a specific memory address. This method will return an error if the I2C peripheral is in slave mode.

Parameters
addressThe slave device address.
mem_addressPointer to data containing the address to write to device.
mem_address_sizeSize of the memory address in bytes.
dataPointer to buffer that will be written to the mem_address
data_sizeSize of the data to be written in bytes.
timeoutThe timeout in milliseconds before returning without communication

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