hardware_i2c
Library FunctionsTo use this library, add following to the C/C++ file,
#include <hardware/i2c.h>
and following to the CMakeLists.txt
file.
target_link_libraries(projectName pico_stdlib hardware_gpio hardware_i2c)
Following are some of the most commonly used functions for configuring and using the I2C peripheral.
uint i2c_init(i2c_inst_t* i2c, uint baudRate)
Initialize a specific instance of I2C on the μC.
i2c
is a pointer to the I2C instance that should be initialized. The library already defines variables for both the instances. So, you only have to specify i2c0
or i2c1
.baudRate
is the baud rate, in bits/s, that the I2C instance should use for communication. It must be an unsigned integer number. For example, setting this value to 100000 means 100 kb/s baud rate, which is the standard mode.int i2c_write_blocking(i2c_inst_t* i2c, uint8_t addr, const uint8_t* src, size_t len, bool nostop)
Initiate an I2C message to write data to a target.
i2c
is a pointer to the I2C instance that should be used for communication.addr
is the address of the target that the controller wants to communicate with. This must be the 7-bit address of the target.src
is the name of the array containing the data that the controller wants to write to the target. The datatype of the array must be uint8_t
.len
is the size of the data in bytes that the controller is supposed to write to the target. Make sure that the size of array defined to hold the data is equal to or greater than this value.nostop
indicates whether the controller should send a stop condition after writing len
bytes of data. If this value is true
then stop condition is not sent otherwise it is sent. This is helpful in conveying a repeated start situation.addr
wasn’t present.int i2c_read_blocking(i2c_inst_t* i2c, uint8_t addr, const uint8_t* dst, size_t len, bool nostop)
Initiate an I2C message to read data from a target.
i2c
is a pointer to the I2C instance that should be used for communication.addr
is the address of the target that the controller wants to communicate with. This must be the 7-bit address of the target.dst
is the name of the array where the data being received by the controller will be stored. The datatype of the array must be uint8_t
.len
is the size of the data in bytes that the controller is supposed receive from the target. Make sure that the size of array defined to hold the data is equal to or greater than this value.nostop
indicates whether the controller should send a stop condition after receiving len
bytes of data. If this value is true
then stop condition is not sent otherwise it is sent. This is helpful in conveying a repeated start situation.addr
wasn’t present.