|
esp32-smbus
ESP32-compatible C library for I2C SMBus Protocol.
|
#include <stddef.h>#include <string.h>#include <inttypes.h>#include "freertos/FreeRTOS.h"#include "freertos/task.h"#include "esp_system.h"#include "esp_log.h"#include "smbus.h"Macros | |
| #define | WRITE_BIT I2C_MASTER_WRITE |
| #define | READ_BIT I2C_MASTER_READ |
| #define | ACK_CHECK true |
| #define | NO_ACK_CHECK false |
| #define | ACK_VALUE 0x0 |
| #define | NACK_VALUE 0x1 |
| #define | MAX_BLOCK_LEN 255 |
Functions | |
| esp_err_t | _write_bytes (const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, size_t len) |
| esp_err_t | _read_bytes (const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, size_t len) |
| smbus_info_t * | smbus_malloc (void) |
| Construct a new SMBus info instance. New instance should be initialised before calling other functions. More... | |
| void | smbus_free (smbus_info_t **smbus_info) |
| Delete an existing SMBus info instance. More... | |
| esp_err_t | smbus_init (smbus_info_t *smbus_info, i2c_port_t i2c_port, i2c_address_t address) |
| Initialise a SMBus info instance with the specified I2C information. The I2C timeout defaults to approximately 1 second. More... | |
| esp_err_t | smbus_set_timeout (smbus_info_t *smbus_info, portBASE_TYPE timeout) |
| Set the I2C timeout. I2C transactions that do not complete within this period are considered an error. More... | |
| esp_err_t | smbus_quick (const smbus_info_t *smbus_info, bool bit) |
| Send a single bit to a slave device in the place of the read/write bit. May be used to simply turn a device function on or off, or enable or disable a low-power standby mode. There is no data sent or received. More... | |
| esp_err_t | smbus_send_byte (const smbus_info_t *smbus_info, uint8_t data) |
| Send a single byte to a slave device. More... | |
| esp_err_t | smbus_receive_byte (const smbus_info_t *smbus_info, uint8_t *data) |
| Receive a single byte from a slave device. More... | |
| esp_err_t | smbus_write_byte (const smbus_info_t *smbus_info, uint8_t command, uint8_t data) |
| Write a single byte to a slave device with a command code. More... | |
| esp_err_t | smbus_write_word (const smbus_info_t *smbus_info, uint8_t command, uint16_t data) |
| Write a single word (two bytes) to a slave device with a command code. The least significant byte is transmitted first. More... | |
| esp_err_t | smbus_read_byte (const smbus_info_t *smbus_info, uint8_t command, uint8_t *data) |
| Read a single byte from a slave device with a command code. More... | |
| esp_err_t | smbus_read_word (const smbus_info_t *smbus_info, uint8_t command, uint16_t *data) |
| Read a single word (two bytes) from a slave device with a command code. The first byte received is the least significant byte. More... | |
| esp_err_t | smbus_write_block (const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, uint8_t len) |
| Write up to 255 bytes to a slave device with a command code. This uses a byte count to negotiate the length of the transaction. The first byte in the data array is transmitted first. More... | |
| esp_err_t | smbus_read_block (const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, uint8_t *len) |
| Read up to 255 bytes from a slave device with a command code. This uses a byte count to negotiate the length of the transaction. The first byte received is placed in the first array location. More... | |
| esp_err_t | smbus_i2c_write_block (const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, size_t len) |
| Write bytes to a slave device with a command code. No byte count is used - the transaction lasts as long as the master requires. The first byte in the data array is transmitted first. This operation is not defined by the SMBus specification. More... | |
| esp_err_t | smbus_i2c_read_block (const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, size_t len) |
| Read bytes from a slave device with a command code (combined format). No byte count is used - the transaction lasts as long as the master requires. The first byte received is placed in the first array location. This operation is not defined by the SMBus specification. More... | |
SMBus diagrams are represented as a chain of "piped" symbols, using the following symbols:
| smbus_info_t* smbus_malloc | ( | void | ) |
Construct a new SMBus info instance. New instance should be initialised before calling other functions.
| void smbus_free | ( | smbus_info_t ** | smbus_info | ) |
Delete an existing SMBus info instance.
| [in,out] | smbus_info | Pointer to SMBus info instance that will be freed and set to NULL. |
| esp_err_t smbus_init | ( | smbus_info_t * | smbus_info, |
| i2c_port_t | i2c_port, | ||
| i2c_address_t | address | ||
| ) |
Initialise a SMBus info instance with the specified I2C information. The I2C timeout defaults to approximately 1 second.
| [in] | smbus_info | Pointer to SMBus info instance. |
| [in] | i2c_port | I2C port to associate with this SMBus instance. |
| [in] | address | Address of I2C slave device. |
| esp_err_t smbus_set_timeout | ( | smbus_info_t * | smbus_info, |
| portBASE_TYPE | timeout | ||
| ) |
Set the I2C timeout. I2C transactions that do not complete within this period are considered an error.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | timeout | Number of ticks to wait until the transaction is considered in error. |
| esp_err_t smbus_quick | ( | const smbus_info_t * | smbus_info, |
| bool | bit | ||
| ) |
Send a single bit to a slave device in the place of the read/write bit. May be used to simply turn a device function on or off, or enable or disable a low-power standby mode. There is no data sent or received.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | bit | Data bit to send. |
| esp_err_t smbus_send_byte | ( | const smbus_info_t * | smbus_info, |
| uint8_t | data | ||
| ) |
Send a single byte to a slave device.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | data | Data byte to send to slave. |
| esp_err_t smbus_receive_byte | ( | const smbus_info_t * | smbus_info, |
| uint8_t * | data | ||
| ) |
Receive a single byte from a slave device.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [out] | data | Data byte received from slave. |
| esp_err_t smbus_write_byte | ( | const smbus_info_t * | smbus_info, |
| uint8_t | command, | ||
| uint8_t | data | ||
| ) |
Write a single byte to a slave device with a command code.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | command | Device-specific command byte. |
| [in] | data | Data byte to send to slave. |
| esp_err_t smbus_write_word | ( | const smbus_info_t * | smbus_info, |
| uint8_t | command, | ||
| uint16_t | data | ||
| ) |
Write a single word (two bytes) to a slave device with a command code. The least significant byte is transmitted first.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | command | Device-specific command byte. |
| [in] | data | Data word to send to slave. |
| esp_err_t smbus_read_byte | ( | const smbus_info_t * | smbus_info, |
| uint8_t | command, | ||
| uint8_t * | data | ||
| ) |
Read a single byte from a slave device with a command code.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | command | Device-specific command byte. |
| [out] | data | Data byte received from slave. |
| esp_err_t smbus_read_word | ( | const smbus_info_t * | smbus_info, |
| uint8_t | command, | ||
| uint16_t * | data | ||
| ) |
Read a single word (two bytes) from a slave device with a command code. The first byte received is the least significant byte.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | command | Device-specific command byte. |
| [out] | data | Data byte received from slave. |
| esp_err_t smbus_write_block | ( | const smbus_info_t * | smbus_info, |
| uint8_t | command, | ||
| uint8_t * | data, | ||
| uint8_t | len | ||
| ) |
Write up to 255 bytes to a slave device with a command code. This uses a byte count to negotiate the length of the transaction. The first byte in the data array is transmitted first.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | command | Device-specific command byte. |
| [in] | data | Data bytes to send to slave. |
| [in] | len | Number of bytes to send to slave. |
| esp_err_t smbus_read_block | ( | const smbus_info_t * | smbus_info, |
| uint8_t | command, | ||
| uint8_t * | data, | ||
| uint8_t * | len | ||
| ) |
Read up to 255 bytes from a slave device with a command code. This uses a byte count to negotiate the length of the transaction. The first byte received is placed in the first array location.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | command | Device-specific command byte. |
| [out] | data | Data bytes received from slave. |
| in/out] | len Size of data array, and number of bytes actually received. |
| esp_err_t smbus_i2c_write_block | ( | const smbus_info_t * | smbus_info, |
| uint8_t | command, | ||
| uint8_t * | data, | ||
| size_t | len | ||
| ) |
Write bytes to a slave device with a command code. No byte count is used - the transaction lasts as long as the master requires. The first byte in the data array is transmitted first. This operation is not defined by the SMBus specification.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | command | Device-specific command byte. |
| [in] | data | Data bytes to send to slave. |
| [in] | len | Number of bytes to send to slave. |
| esp_err_t smbus_i2c_read_block | ( | const smbus_info_t * | smbus_info, |
| uint8_t | command, | ||
| uint8_t * | data, | ||
| size_t | len | ||
| ) |
Read bytes from a slave device with a command code (combined format). No byte count is used - the transaction lasts as long as the master requires. The first byte received is placed in the first array location. This operation is not defined by the SMBus specification.
| [in] | smbus_info | Pointer to initialised SMBus info instance. |
| [in] | command | Device-specific command byte. |
| [out] | data | Data bytes received from slave. |
| in/out] | len Size of data array. If the slave fails to provide sufficient bytes, ESP_ERR_TIMEOUT will be returned. |
1.8.6