8.2. hardware_i2c Library Functions#
To 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.
8.2.1. uint i2c_init(i2c_inst_t* i2c, uint baudRate)#
Initialize a specific instance of I2C on the μC.
Function input
i2cis a pointer to the I2C instance that should be initialized. The library already defines variables for both the instances. So, you only have to specifyi2c0ori2c1.Function input
baudRateis 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.Function returns an unsigned integer representing the baud rate that was actually set.
8.2.2. 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.
Function input
i2cis a pointer to the I2C instance that should be used for communication.Function input
addris the address of the target that the controller wants to communicate with. This must be the 7-bit address of the target.Function input
srcis the name of the array containing the data that the controller wants to write to the target. The datatype of the array must beuint8_t.Function input
lenis 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.Function input
nostopindicates whether the controller should send a stop condition after writinglenbytes of data. If this value istruethen stop condition is not sent otherwise it is sent. This is helpful in conveying a repeated start situation.Function returns a positive integer indicating the number of bytes that were written successfully. The return value might be -2 if the target with the
addrwasn’t present.This function doesn’t exit until all the data is sent or an error occurred.
8.2.3. 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.
Function input
i2cis a pointer to the I2C instance that should be used for communication.Function input
addris the address of the target that the controller wants to communicate with. This must be the 7-bit address of the target.Function input
dstis the name of the array where the data being received by the controller will be stored. The datatype of the array must beuint8_t.Function input
lenis 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.Function input
nostopindicates whether the controller should send a stop condition after receivinglenbytes of data. If this value istruethen stop condition is not sent otherwise it is sent. This is helpful in conveying a repeated start situation.Function returns a positive integer indicating the number of bytes received successfully. The return value might be -2 if the target with the
addrwasn’t present.This function doesn’t exit until all the data is received or an error occurred.