|
libeasymcp2221 2.0.3
|
High-level helpers for communicating with an I2C target device. More...
Go to the source code of this file.
Classes | |
| struct | mcp2221_i2c_slave |
| Caller-owned I2C target context. More... | |
Enumerations | |
| enum | mcp2221_i2c_byte_order_t { MCP2221_I2C_BYTE_ORDER_DEFAULT = -1 , MCP2221_I2C_BYTE_ORDER_BIG = 0 , MCP2221_I2C_BYTE_ORDER_LITTLE = 1 } |
| Register-address byte order used by I2C slave register helpers. More... | |
Functions | |
| mcp2221_error_code_t | mcp2221_i2c_slave_init (mcp2221_i2c_slave_t *slave, mcp2221_t *mcp, uint8_t addr, int force, uint32_t i2c_speed_hz, int reg_bytes, mcp2221_i2c_byte_order_t reg_byteorder) |
| Initialize a caller-owned I2C target context. More... | |
| mcp2221_error_code_t | mcp2221_i2c_slave_check_present (mcp2221_i2c_slave_t *slave, int *is_present) |
| Check whether the configured I2C target acknowledges its address. More... | |
| int | mcp2221_i2c_slave_is_present (mcp2221_i2c_slave_t *slave) |
| Boolean-style convenience presence check. More... | |
| mcp2221_error_code_t | mcp2221_i2c_slave_read_register (mcp2221_i2c_slave_t *slave, uint32_t reg, uint8_t *buffer, size_t length, int reg_bytes, mcp2221_i2c_byte_order_t reg_byteorder) |
| Read bytes starting at a target register. More... | |
| mcp2221_error_code_t | mcp2221_i2c_slave_read (mcp2221_i2c_slave_t *slave, uint8_t *buffer, size_t length) |
| Read bytes directly from the configured I2C target. More... | |
| mcp2221_error_code_t | mcp2221_i2c_slave_write_register (mcp2221_i2c_slave_t *slave, uint32_t reg, const uint8_t *data, size_t length, int reg_bytes, mcp2221_i2c_byte_order_t reg_byteorder) |
| Write a register address followed by optional data. More... | |
| mcp2221_error_code_t | mcp2221_i2c_slave_write (mcp2221_i2c_slave_t *slave, const uint8_t *data, size_t length) |
| Write bytes directly to the configured I2C target. More... | |
High-level helpers for communicating with an I2C target device.
Definition in file mcp2221_i2c_slave.h.
Register-address byte order used by I2C slave register helpers.
| Enumerator | |
|---|---|
| MCP2221_I2C_BYTE_ORDER_DEFAULT | Use the default byte order. During mcp2221_i2c_slave_init(), this resolves to big endian. For per-operation register helpers it resolves to the byte order stored in the slave context. |
| MCP2221_I2C_BYTE_ORDER_BIG | Encode register addresses most-significant byte first. |
| MCP2221_I2C_BYTE_ORDER_LITTLE | Encode register addresses least-significant byte first. |
Definition at line 19 of file mcp2221_i2c_slave.h.
| mcp2221_error_code_t mcp2221_i2c_slave_check_present | ( | mcp2221_i2c_slave_t * | slave, |
| int * | is_present | ||
| ) |
Check whether the configured I2C target acknowledges its address.
An address NACK is treated as a successful presence check with *is_present == 0. Transport, timeout, and other I2C failures are returned to the caller.
| [in] | slave | Initialized I2C target context. |
| [out] | is_present | Set to 1 when the target acknowledges and 0 when it NACKs its address. Must not be NULL. |
| mcp2221_error_code_t mcp2221_i2c_slave_init | ( | mcp2221_i2c_slave_t * | slave, |
| mcp2221_t * | mcp, | ||
| uint8_t | addr, | ||
| int | force, | ||
| uint32_t | i2c_speed_hz, | ||
| int | reg_bytes, | ||
| mcp2221_i2c_byte_order_t | reg_byteorder | ||
| ) |
Initialize a caller-owned I2C target context.
Configures the MCP2221 I2C clock and, unless force is nonzero, verifies that the target acknowledges its address. The context is committed only after all validation and setup steps succeed.
On failure, slave is left invalid with slave->mcp == NULL.
| [out] | slave | Caller-owned context to initialize. Must not be NULL. |
| [in] | mcp | Open MCP2221 device handle. |
| [in] | addr | 7-bit I2C target address. |
| [in] | force | Nonzero to skip the initial presence check. |
| [in] | i2c_speed_hz | Requested I2C clock frequency in hertz. |
| [in] | reg_bytes | Default register-address width. Values less than or equal to 0 select 1 byte; valid explicit widths are 1 through 4 bytes. |
| [in] | reg_byteorder | Default register-address byte order. MCP2221_I2C_BYTE_ORDER_DEFAULT selects big endian. |
| int mcp2221_i2c_slave_is_present | ( | mcp2221_i2c_slave_t * | slave | ) |
Boolean-style convenience presence check.
| [in] | slave | Initialized I2C target context. |
| mcp2221_error_code_t mcp2221_i2c_slave_read | ( | mcp2221_i2c_slave_t * | slave, |
| uint8_t * | buffer, | ||
| size_t | length | ||
| ) |
Read bytes directly from the configured I2C target.
| [in] | slave | Initialized I2C target context. |
| [out] | buffer | Buffer receiving the data. |
| [in] | length | Number of bytes to read. Must be from 1 to 256. |
| mcp2221_error_code_t mcp2221_i2c_slave_read_register | ( | mcp2221_i2c_slave_t * | slave, |
| uint32_t | reg, | ||
| uint8_t * | buffer, | ||
| size_t | length, | ||
| int | reg_bytes, | ||
| mcp2221_i2c_byte_order_t | reg_byteorder | ||
| ) |
Read bytes starting at a target register.
Encodes reg using the selected register width and byte order, writes the register address without a STOP condition, then reads the requested data using a repeated START.
| [in] | slave | Initialized I2C target context. |
| [in] | reg | Register address to read from. The value must fit in the effective register-address width. |
| [out] | buffer | Buffer receiving the data. |
| [in] | length | Number of data bytes to read. Must be from 1 to 256. |
| [in] | reg_bytes | Register-address width for this operation. Values less than or equal to 0 use the context default; valid explicit widths are 1 through 4 bytes. |
| [in] | reg_byteorder | Byte order for this operation. MCP2221_I2C_BYTE_ORDER_DEFAULT uses the context default. |
| mcp2221_error_code_t mcp2221_i2c_slave_write | ( | mcp2221_i2c_slave_t * | slave, |
| const uint8_t * | data, | ||
| size_t | length | ||
| ) |
Write bytes directly to the configured I2C target.
| [in] | slave | Initialized I2C target context. |
| [in] | data | Data to write. |
| [in] | length | Number of bytes to write. Must be from 1 to 256. |
| mcp2221_error_code_t mcp2221_i2c_slave_write_register | ( | mcp2221_i2c_slave_t * | slave, |
| uint32_t | reg, | ||
| const uint8_t * | data, | ||
| size_t | length, | ||
| int | reg_bytes, | ||
| mcp2221_i2c_byte_order_t | reg_byteorder | ||
| ) |
Write a register address followed by optional data.
Encodes reg using the selected register width and byte order and sends the encoded register address followed by data in one normal I2C write.
| [in] | slave | Initialized I2C target context. |
| [in] | reg | Register address to write. The value must fit in the effective register-address width. |
| [in] | data | Data bytes to append after the register address. May be NULL when length is 0. |
| [in] | length | Number of data bytes to write. May be 0 and must not exceed 256. |
| [in] | reg_bytes | Register-address width for this operation. Values less than or equal to 0 use the context default; valid explicit widths are 1 through 4 bytes. |
| [in] | reg_byteorder | Byte order for this operation. MCP2221_I2C_BYTE_ORDER_DEFAULT uses the context default. |