Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Typedefs | Enumerations | Functions
I2C

I²C (Inter-Integrated Circuit) API for Soletta. More...

Typedefs

typedef struct sol_i2c sol_i2c
 I2C handle structure. More...
 
typedef struct sol_i2c_pending sol_i2c_pending
 I2C pending operation handle structure. More...
 

Enumerations

enum  sol_i2c_speed {
  SOL_I2C_SPEED_10KBIT = 0, SOL_I2C_SPEED_100KBIT, SOL_I2C_SPEED_400KBIT, SOL_I2C_SPEED_1MBIT,
  SOL_I2C_SPEED_3MBIT_400KBIT
}
 Enum for I2C bus speed. More...
 

Functions

void sol_i2c_close (struct sol_i2c *i2c)
 Close an I2C bus. More...
 
uint8_t sol_i2c_get_bus (const struct sol_i2c *i2c)
 Get the I2C bus ID. More...
 
uint8_t sol_i2c_get_slave_address (struct sol_i2c *i2c)
 Get the (slave) device address set on an I2C bus (to deliver I2C commands to) More...
 
struct sol_i2csol_i2c_open (uint8_t bus, enum sol_i2c_speed speed)
 Open an I2C bus. More...
 
struct sol_i2csol_i2c_open_raw (uint8_t bus, enum sol_i2c_speed speed)
 Open an I2C bus. More...
 
void sol_i2c_pending_cancel (struct sol_i2c *i2c, struct sol_i2c_pending *pending)
 Cancel a pending operation. More...
 
struct sol_i2c_pendingsol_i2c_read (struct sol_i2c *i2c, uint8_t *data, size_t count, void(*read_cb)(void *cb_data, struct sol_i2c *i2c, uint8_t *data, ssize_t status), const void *cb_data)
 Perform successive asynchronous I2C byte read operations, with no specified register. More...
 
struct sol_i2c_pendingsol_i2c_read_register (struct sol_i2c *i2c, uint8_t reg, uint8_t *data, size_t count, void(*read_reg_cb)(void *cb_data, struct sol_i2c *i2c, uint8_t reg, uint8_t *data, ssize_t status), const void *cb_data)
 Perform an asynchronous I2C read operation on a given device register. More...
 
struct sol_i2c_pendingsol_i2c_read_register_multiple (struct sol_i2c *i2c, uint8_t reg, uint8_t *values, size_t count, uint8_t times, void(*read_reg_multiple_cb)(void *cb_data, struct sol_i2c *i2c, uint8_t reg, uint8_t *data, ssize_t status), const void *cb_data)
 Asynchronous read of an arbitrary number of bytes from a register in repeated bursts of a given length (that start always on the provided register address) More...
 
int sol_i2c_set_slave_address (struct sol_i2c *i2c, uint8_t slave_address)
 Set a (slave) device address on a I2C bus to deliver commands to. More...
 
enum sol_i2c_speed sol_i2c_speed_from_str (const char *speed)
 Converts a string I2C speed to sol_i2c_speed. More...
 
const char * sol_i2c_speed_to_str (enum sol_i2c_speed speed)
 Converts sol_i2c_speed to a string name. More...
 
struct sol_i2c_pendingsol_i2c_write (struct sol_i2c *i2c, uint8_t *data, size_t count, void(*write_cb)(void *cb_data, struct sol_i2c *i2c, uint8_t *data, ssize_t status), const void *cb_data)
 Perform successive asynchronous I2C byte write operations, with no specified register. More...
 
struct sol_i2c_pendingsol_i2c_write_quick (struct sol_i2c *i2c, bool rw, void(*write_quick_cb)(void *cb_data, struct sol_i2c *i2c, ssize_t status), const void *cb_data)
 Perform a I2C write quick operation. More...
 
struct sol_i2c_pendingsol_i2c_write_register (struct sol_i2c *i2c, uint8_t reg, const uint8_t *data, size_t count, void(*write_reg_cb)(void *cb_data, struct sol_i2c *i2c, uint8_t reg, uint8_t *data, ssize_t status), const void *cb_data)
 Perform an asynchronous I2C write operation on a given device register. More...
 
enum  sol_i2c_op_type { SOL_I2C_READ, SOL_I2C_WRITE }
 I²C operation dispatcher. More...
 
typedef struct
sol_i2c_op_set_pending 
sol_i2c_op_set_pending
 I2C Dispatcher pending operation set handle structure. More...
 
struct sol_i2c_op_set_pendingsol_i2c_dispatcher_add_op_set (struct sol_i2c *i2c, uint8_t addr, struct sol_vector *set, void(*cb)(void *cb_data, ssize_t status), void *cb_data, uint32_t delay)
 Add an operation set in the dispatcher's queue of a given I2C bus. More...
 
void sol_i2c_dispatcher_remove_op_set (struct sol_i2c *i2c, struct sol_i2c_op_set_pending *pending)
 Cancel the execution of the pending operation set. More...
 

Detailed Description

I²C (Inter-Integrated Circuit) API for Soletta.

Typedef Documentation

I2C Dispatcher pending operation set handle structure.

Enumeration Type Documentation

I²C operation dispatcher.

These routines are used manipulate groups of I2C operations to slave devices under Soletta. Enum for the dispatcher operation type.

If a given operation in the set to either read or write a data.

Enumerator
SOL_I2C_READ 
SOL_I2C_WRITE 

Enum for I2C bus speed.

Must be chosen when opening a bus with sol_i2c_open() and sol_i2c_open_raw().

Enumerator
SOL_I2C_SPEED_10KBIT 

flag for low speed

SOL_I2C_SPEED_100KBIT 

flag for normal speed

SOL_I2C_SPEED_400KBIT 

flag for fast speed

SOL_I2C_SPEED_1MBIT 

flag for fast plus speed

SOL_I2C_SPEED_3MBIT_400KBIT 

flag for high speed

Function Documentation

void sol_i2c_close ( struct sol_i2c i2c)

Close an I2C bus.

Parameters
i2cThe I2C bus handle to close
struct sol_i2c_op_set_pending* sol_i2c_dispatcher_add_op_set ( struct sol_i2c i2c,
uint8_t  addr,
struct sol_vector set,
void(*)(void *cb_data, ssize_t status)  cb,
void *  cb_data,
uint32_t  delay 
)

Add an operation set in the dispatcher's queue of a given I2C bus.

It adds an operation set scheduling it for execution by the dispatcher handling i2c opeations at bus i2c.

Parameters
i2cThe i2c bus handle
addrThe slave device address
setOperation set to be added
cbCallback to be called after the operation set is executed
cb_dataCallback context data
delayTime, in milliseconds, to wait between two consecutive operations of this set
void sol_i2c_dispatcher_remove_op_set ( struct sol_i2c i2c,
struct sol_i2c_op_set_pending pending 
)

Cancel the execution of the pending operation set.

Parameters
i2cThe I2C bus handle
pendingThe operation set pending handle
uint8_t sol_i2c_get_bus ( const struct sol_i2c i2c)

Get the I2C bus ID.

Parameters
i2cThe I2C bus handle
Returns
the bus id
uint8_t sol_i2c_get_slave_address ( struct sol_i2c i2c)

Get the (slave) device address set on an I2C bus (to deliver I2C commands to)

Parameters
i2cThe I2C bus handle
Returns
The slave device address set on bus. 0x0 means bus was not set to any device yet
struct sol_i2c* sol_i2c_open ( uint8_t  bus,
enum sol_i2c_speed  speed 
)

Open an I2C bus.

Parameters
busThe I2C bus number to open
speedThe speed to open I2C bus bus at
Returns
A new I2C bus handle
Note
This call will attempt to make pin muxing operations underneath, for the given platform that the code is running in. Use sol_i2c_open_raw() if you want to skip any pin mux operation.
The same I2C bus is shared between every user, so only the first one opening a bus will be able to set its speed.
struct sol_i2c* sol_i2c_open_raw ( uint8_t  bus,
enum sol_i2c_speed  speed 
)

Open an I2C bus.

Parameters
busThe I2C bus number to open
speedThe speed to open I2C bus bus at
Returns
A new I2C bus handle
Note
This call won't attempt to make any pin muxing operations underneath. Use sol_i2c_open() for that. Also, this will never cache this I2C handle (or return any previously cached I2C handle).
void sol_i2c_pending_cancel ( struct sol_i2c i2c,
struct sol_i2c_pending pending 
)

Cancel a pending operation.

Parameters
i2cthe I2C bus handle
pendingthe operation handle
struct sol_i2c_pending* sol_i2c_read ( struct sol_i2c i2c,
uint8_t *  data,
size_t  count,
void(*)(void *cb_data, struct sol_i2c *i2c, uint8_t *data, ssize_t status)  read_cb,
const void *  cb_data 
)

Perform successive asynchronous I2C byte read operations, with no specified register.

This makes count read byte I2C operations on the device bus is set to operate on, at no specific register. Some devices are so simple that this interface is enough. For others, it is a shorthand if you want to read the same register as in the previous I2C command.

Parameters
i2cThe I2C bus handle
dataThe output buffer for the read operation
countThe bytes count for the read operation
read_cbThe callback to be called when operation finish, the status parameter should be equal to count in case of success (or a negative error code, on failure)
cb_dataThe first parameter of callback
Note
The caller should guarantee that data will not be freed until the callback is called. Also there is no transfer queue, calling this function when there is another I2C operation running will return false.
Returns
pending An I2C pending operation handle on success, otherwise NULL. It's only valid before read_cb is called. It may be used before that to cancel the read operation. If NULL is returned, the errno variable will be set with the correct error value. In case that the I2C device is in use, the errno variable is set to EBUSY.
struct sol_i2c_pending* sol_i2c_read_register ( struct sol_i2c i2c,
uint8_t  reg,
uint8_t *  data,
size_t  count,
void(*)(void *cb_data, struct sol_i2c *i2c, uint8_t reg, uint8_t *data, ssize_t status)  read_reg_cb,
const void *  cb_data 
)

Perform an asynchronous I2C read operation on a given device register.

Parameters
i2cThe I2C bus handle
regThe I2C register for the read operation
dataThe output buffer for the read operation
countThe bytes count for the read operation
read_reg_cbThe callback to be called when operation finish, the status parameter should be equal to count in case of success (or a negative error code, on failure)
cb_dataThe first parameter of callback
Note
The caller should guarantee that data will not be freed until the callback is called. Also there is no transfer queue, calling this function when there is another I2C operation running will return false.
Returns
pending An I2C pending operation handle on success, otherwise NULL. It's only valid before read_reg_cb is called. It may be used before that to cancel the read operation. If NULL is returned, the errno variable will be set with the correct error value. In case that the I2C device is in use, the errno variable is set to EBUSY.
struct sol_i2c_pending* sol_i2c_read_register_multiple ( struct sol_i2c i2c,
uint8_t  reg,
uint8_t *  values,
size_t  count,
uint8_t  times,
void(*)(void *cb_data, struct sol_i2c *i2c, uint8_t reg, uint8_t *data, ssize_t status)  read_reg_multiple_cb,
const void *  cb_data 
)

Asynchronous read of an arbitrary number of bytes from a register in repeated bursts of a given length (that start always on the provided register address)

This is so because a lot of devices will, after a read operation, update its register values with new data to be read on subsequent operations, until the total data length the user requested is read. If the device has the auto-increment feature, sol_i2c_read_register() might be a better call than this function.

This will issue multiple I2C read/write transactions with the first (write) message specifying the register to operate on and the second (read) message specifying the length (always len per read) and the destination of the read operation.

Parameters
i2cThe I2C bus handle
regThe register to start reading from
valuesWhere to store the read bytes
countThe size of a single read block
timesHow many reads of size len to perform (on success, len * times bytes will be read)
read_reg_multiple_cbThe callback to be called when the operation finishes. The status parameter should be equal to count in case of success (or a negative error code, on failure)
cb_dataThe first parameter of callback
Note
The caller should guarantee that data will not be freed until the callback is called. Also there is no transfer queue, calling this function when there is another I2C operation running will return false.
Returns
pending An I2C pending operation handle on success, otherwise NULL. It's only valid before read_reg_multiple_cb is called. It may be used before that to cancel the read operation. If NULL is returned, the errno variable will be set with the correct error value. In case that the I2C device is in use, the errno variable is set to EBUSY.
int sol_i2c_set_slave_address ( struct sol_i2c i2c,
uint8_t  slave_address 
)

Set a (slave) device address on a I2C bus to deliver commands to.

All other I2C functions, after this call, will act on the given slave_address device address. Since other I2C calls might happen in between your own ones, though, it's highly advisable that you issue this call before using any of the I2C read/write functions.

Parameters
i2cThe I2C bus handle
slave_addressThe slave device address to deliver commands to
Returns
0 on success, -EBUSY if the device is busy or -errno on error.
enum sol_i2c_speed sol_i2c_speed_from_str ( const char *  speed)

Converts a string I2C speed to sol_i2c_speed.

This function converts a string I2C speed to enumeration sol_i2c_speed.

See Also
sol_i2c_speed_to_str().
Parameters
speedValid values are "10kbps", "100kbps", "400kbps", "1000kbps", "3400kbps".
Returns
enumeration sol_i2c_speed.
const char* sol_i2c_speed_to_str ( enum sol_i2c_speed  speed)

Converts sol_i2c_speed to a string name.

This function converts sol_i2c_speed enumeration to a string I2C speed name.

See Also
sol_i2c_speed_from_str().
Parameters
speedsol_i2c_speed.
Returns
String representation of the sol_i2c_speed.
struct sol_i2c_pending* sol_i2c_write ( struct sol_i2c i2c,
uint8_t *  data,
size_t  count,
void(*)(void *cb_data, struct sol_i2c *i2c, uint8_t *data, ssize_t status)  write_cb,
const void *  cb_data 
)

Perform successive asynchronous I2C byte write operations, with no specified register.

This makes count write byte I2C operations on the device bus is set to operate on, at no specific register. Some devices are so simple that this interface is enough. For others, it is a shorthand if you want to write the same register as in the previous I2C command.

Parameters
i2cThe I2C bus handle
dataThe output buffer for the write operation
countThe bytes count for the write operation
write_cbThe callback to be called when operation finish, the status parameter should be equal to count in case of success (or a negative error code, on failure)
cb_dataThe first parameter of callback
Note
The caller should guarantee that data will not be freed until the callback is called. Also there is no transfer queue, calling this function when there is another I2C operation running will return false.
Returns
pending An I2C pending operation handle on success, otherwise NULL. It's only valid before write_cb is called. It may be used before that to cancel the read operation. If NULL is returned, the errno variable will be set with the correct error value. In case that the I2C device is in use, the errno variable is set to EBUSY.
struct sol_i2c_pending* sol_i2c_write_quick ( struct sol_i2c i2c,
bool  rw,
void(*)(void *cb_data, struct sol_i2c *i2c, ssize_t status)  write_quick_cb,
const void *  cb_data 
)

Perform a I2C write quick operation.

This sends a single bit to a device (command designed to turn on and off simple devices)

Parameters
i2cThe I2C bus handle
rwThe value to write
write_quick_cbThe callback to be issued when the operation finishes. The status parameter should be equal to one in case of success (or a negative error code, on failure)
cb_dataData to be passed to write_quick_cb
Returns
pending An I2C pending operation handle on success, otherwise NULL. It's only valid before write_quick_cb is called. It may be used before that to cancel the read operation. If NULL is returned, the errno variable will be set with the correct error value. In case that the I2C device is in use, the errno variable is set to EBUSY.
struct sol_i2c_pending* sol_i2c_write_register ( struct sol_i2c i2c,
uint8_t  reg,
const uint8_t *  data,
size_t  count,
void(*)(void *cb_data, struct sol_i2c *i2c, uint8_t reg, uint8_t *data, ssize_t status)  write_reg_cb,
const void *  cb_data 
)

Perform an asynchronous I2C write operation on a given device register.

Parameters
i2cThe I2C bus handle
regThe I2C register for the write operation
dataThe output buffer for the write operation
countThe bytes count for the write operation
write_reg_cbThe callback to be called when operation finish, the status parameter should be equal to count in case of success (or a negative error code, on failure)
cb_dataThe first parameter of callback
Note
The caller should guarantee that data will not be freed until the callback is called. Also there is no transfer queue, calling this function when there is another I2C operation running will return false.
Returns
pending An I2C pending operation handle on success, otherwise NULL. It's only valid before write_reg_cb is called. It may be used before that to cancel the read operation. If NULL is returned, the errno variable will be set with the correct error value. In case that the I2C device is in use, the errno variable is set to EBUSY.