2021-11-28 18:28:19 +00:00
|
|
|
/**
|
2023-09-21 07:54:25 +00:00
|
|
|
* @file furi_hal_i2c.h I2C HAL API
|
2021-11-28 18:28:19 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-18 09:23:14 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
2023-09-21 07:54:25 +00:00
|
|
|
#include <stddef.h>
|
2021-05-18 09:23:14 +00:00
|
|
|
#include <stdint.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_i2c_config.h>
|
2021-05-18 09:23:14 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2023-09-21 07:54:25 +00:00
|
|
|
/** Transaction beginning signal */
|
|
|
|
typedef enum {
|
|
|
|
/*!Begin the transaction by sending a START condition followed by the
|
|
|
|
* address */
|
|
|
|
FuriHalI2cBeginStart,
|
|
|
|
/*!Begin the transaction by sending a RESTART condition followed by the
|
|
|
|
* address
|
|
|
|
* @note Must follow a transaction ended with
|
|
|
|
* FuriHalI2cEndAwaitRestart */
|
|
|
|
FuriHalI2cBeginRestart,
|
|
|
|
/*!Continue the previous transaction with new data
|
|
|
|
* @note Must follow a transaction ended with FuriHalI2cEndPause and
|
|
|
|
* be of the same type (RX/TX) */
|
|
|
|
FuriHalI2cBeginResume,
|
|
|
|
} FuriHalI2cBegin;
|
|
|
|
|
|
|
|
/** Transaction end signal */
|
|
|
|
typedef enum {
|
|
|
|
/*!End the transaction by sending a STOP condition */
|
|
|
|
FuriHalI2cEndStop,
|
|
|
|
/*!End the transaction by clock stretching
|
|
|
|
* @note Must be followed by a transaction using
|
|
|
|
* FuriHalI2cBeginRestart */
|
|
|
|
FuriHalI2cEndAwaitRestart,
|
|
|
|
/*!Pauses the transaction by clock stretching
|
|
|
|
* @note Must be followed by a transaction using FuriHalI2cBeginResume */
|
|
|
|
FuriHalI2cEndPause,
|
|
|
|
} FuriHalI2cEnd;
|
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
/** Early Init I2C */
|
|
|
|
void furi_hal_i2c_init_early();
|
|
|
|
|
|
|
|
/** Early DeInit I2C */
|
|
|
|
void furi_hal_i2c_deinit_early();
|
|
|
|
|
|
|
|
/** Init I2C */
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_i2c_init();
|
2021-05-18 09:23:14 +00:00
|
|
|
|
2023-09-21 07:54:25 +00:00
|
|
|
/** Acquire I2C bus handle
|
2021-11-28 18:28:19 +00:00
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
2021-11-28 18:28:19 +00:00
|
|
|
*/
|
|
|
|
void furi_hal_i2c_acquire(FuriHalI2cBusHandle* handle);
|
|
|
|
|
2023-09-21 07:54:25 +00:00
|
|
|
/** Release I2C bus handle
|
|
|
|
*
|
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance acquired in
|
|
|
|
* `furi_hal_i2c_acquire`
|
2021-11-28 18:28:19 +00:00
|
|
|
*/
|
|
|
|
void furi_hal_i2c_release(FuriHalI2cBusHandle* handle);
|
|
|
|
|
2023-09-21 07:54:25 +00:00
|
|
|
/** Perform I2C TX transfer
|
2021-11-28 18:28:19 +00:00
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
2021-11-30 22:07:17 +00:00
|
|
|
* @param address I2C slave address
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param data Pointer to data buffer
|
|
|
|
* @param size Size of data buffer
|
|
|
|
* @param timeout Timeout in milliseconds
|
2021-11-28 18:28:19 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
bool furi_hal_i2c_tx(
|
2021-11-28 18:28:19 +00:00
|
|
|
FuriHalI2cBusHandle* handle,
|
2023-09-21 07:54:25 +00:00
|
|
|
uint8_t address,
|
|
|
|
const uint8_t* data,
|
|
|
|
size_t size,
|
|
|
|
uint32_t timeout);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform I2C TX transfer, with additional settings.
|
|
|
|
*
|
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
|
|
|
* @param address I2C slave address
|
|
|
|
* @param ten_bit Whether the address is 10 bits wide
|
|
|
|
* @param data Pointer to data buffer
|
|
|
|
* @param size Size of data buffer
|
|
|
|
* @param begin How to begin the transaction
|
|
|
|
* @param end How to end the transaction
|
2024-03-06 06:25:21 +00:00
|
|
|
* @param timeout Timeout in milliseconds
|
2023-09-21 07:54:25 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
|
|
|
bool furi_hal_i2c_tx_ext(
|
|
|
|
FuriHalI2cBusHandle* handle,
|
|
|
|
uint16_t address,
|
|
|
|
bool ten_bit,
|
2021-05-18 09:23:14 +00:00
|
|
|
const uint8_t* data,
|
2023-09-21 07:54:25 +00:00
|
|
|
size_t size,
|
|
|
|
FuriHalI2cBegin begin,
|
|
|
|
FuriHalI2cEnd end,
|
2021-05-18 09:23:14 +00:00
|
|
|
uint32_t timeout);
|
|
|
|
|
2023-09-21 07:54:25 +00:00
|
|
|
/** Perform I2C RX transfer
|
2021-11-28 18:28:19 +00:00
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
2021-11-30 22:07:17 +00:00
|
|
|
* @param address I2C slave address
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param data Pointer to data buffer
|
|
|
|
* @param size Size of data buffer
|
|
|
|
* @param timeout Timeout in milliseconds
|
2021-11-28 18:28:19 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
bool furi_hal_i2c_rx(
|
2021-11-28 18:28:19 +00:00
|
|
|
FuriHalI2cBusHandle* handle,
|
2023-09-21 07:54:25 +00:00
|
|
|
uint8_t address,
|
|
|
|
uint8_t* data,
|
|
|
|
size_t size,
|
|
|
|
uint32_t timeout);
|
|
|
|
|
|
|
|
/** Perform I2C RX transfer, with additional settings.
|
|
|
|
*
|
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
|
|
|
* @param address I2C slave address
|
|
|
|
* @param ten_bit Whether the address is 10 bits wide
|
|
|
|
* @param data Pointer to data buffer
|
|
|
|
* @param size Size of data buffer
|
|
|
|
* @param begin How to begin the transaction
|
|
|
|
* @param end How to end the transaction
|
2024-03-06 06:25:21 +00:00
|
|
|
* @param timeout Timeout in milliseconds
|
2023-09-21 07:54:25 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
|
|
|
bool furi_hal_i2c_rx_ext(
|
|
|
|
FuriHalI2cBusHandle* handle,
|
|
|
|
uint16_t address,
|
|
|
|
bool ten_bit,
|
2021-05-18 09:23:14 +00:00
|
|
|
uint8_t* data,
|
2023-09-21 07:54:25 +00:00
|
|
|
size_t size,
|
|
|
|
FuriHalI2cBegin begin,
|
|
|
|
FuriHalI2cEnd end,
|
2021-05-18 09:23:14 +00:00
|
|
|
uint32_t timeout);
|
|
|
|
|
2023-09-21 07:54:25 +00:00
|
|
|
/** Perform I2C TX and RX transfers
|
2021-11-28 18:28:19 +00:00
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
2021-11-30 22:07:17 +00:00
|
|
|
* @param address I2C slave address
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param tx_data Pointer to TX data buffer
|
|
|
|
* @param tx_size Size of TX data buffer
|
|
|
|
* @param rx_data Pointer to RX data buffer
|
|
|
|
* @param rx_size Size of RX data buffer
|
|
|
|
* @param timeout Timeout in milliseconds
|
2021-11-28 18:28:19 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
bool furi_hal_i2c_trx(
|
2021-11-28 18:28:19 +00:00
|
|
|
FuriHalI2cBusHandle* handle,
|
2023-09-21 07:54:25 +00:00
|
|
|
uint8_t address,
|
2021-05-18 09:23:14 +00:00
|
|
|
const uint8_t* tx_data,
|
2023-09-21 07:54:25 +00:00
|
|
|
size_t tx_size,
|
2021-05-18 09:23:14 +00:00
|
|
|
uint8_t* rx_data,
|
2023-09-21 07:54:25 +00:00
|
|
|
size_t rx_size,
|
2021-05-18 09:23:14 +00:00
|
|
|
uint32_t timeout);
|
|
|
|
|
2022-01-12 15:29:28 +00:00
|
|
|
/** Check if I2C device presents on bus
|
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
|
|
|
* @param i2c_addr I2C slave address
|
|
|
|
* @param timeout Timeout in milliseconds
|
2022-01-12 15:29:28 +00:00
|
|
|
*
|
|
|
|
* @return true if device present and is ready, false otherwise
|
|
|
|
*/
|
|
|
|
bool furi_hal_i2c_is_device_ready(FuriHalI2cBusHandle* handle, uint8_t i2c_addr, uint32_t timeout);
|
|
|
|
|
|
|
|
/** Perform I2C device register read (8-bit)
|
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
|
|
|
* @param i2c_addr I2C slave address
|
|
|
|
* @param reg_addr Register address
|
|
|
|
* @param data Pointer to register value
|
|
|
|
* @param timeout Timeout in milliseconds
|
2022-01-12 15:29:28 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
|
|
|
bool furi_hal_i2c_read_reg_8(
|
|
|
|
FuriHalI2cBusHandle* handle,
|
|
|
|
uint8_t i2c_addr,
|
|
|
|
uint8_t reg_addr,
|
|
|
|
uint8_t* data,
|
|
|
|
uint32_t timeout);
|
|
|
|
|
|
|
|
/** Perform I2C device register read (16-bit)
|
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
|
|
|
* @param i2c_addr I2C slave address
|
|
|
|
* @param reg_addr Register address
|
|
|
|
* @param data Pointer to register value
|
|
|
|
* @param timeout Timeout in milliseconds
|
2022-01-12 15:29:28 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
|
|
|
bool furi_hal_i2c_read_reg_16(
|
|
|
|
FuriHalI2cBusHandle* handle,
|
|
|
|
uint8_t i2c_addr,
|
|
|
|
uint8_t reg_addr,
|
|
|
|
uint16_t* data,
|
|
|
|
uint32_t timeout);
|
|
|
|
|
|
|
|
/** Perform I2C device memory read
|
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
|
|
|
* @param i2c_addr I2C slave address
|
|
|
|
* @param mem_addr Memory start address
|
|
|
|
* @param data Pointer to data buffer
|
|
|
|
* @param len Size of data buffer
|
|
|
|
* @param timeout Timeout in milliseconds
|
2022-01-12 15:29:28 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
|
|
|
bool furi_hal_i2c_read_mem(
|
|
|
|
FuriHalI2cBusHandle* handle,
|
|
|
|
uint8_t i2c_addr,
|
|
|
|
uint8_t mem_addr,
|
|
|
|
uint8_t* data,
|
2023-09-21 07:54:25 +00:00
|
|
|
size_t len,
|
2022-01-12 15:29:28 +00:00
|
|
|
uint32_t timeout);
|
|
|
|
|
|
|
|
/** Perform I2C device register write (8-bit)
|
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
|
|
|
* @param i2c_addr I2C slave address
|
|
|
|
* @param reg_addr Register address
|
|
|
|
* @param data Register value
|
|
|
|
* @param timeout Timeout in milliseconds
|
2022-01-12 15:29:28 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
|
|
|
bool furi_hal_i2c_write_reg_8(
|
|
|
|
FuriHalI2cBusHandle* handle,
|
|
|
|
uint8_t i2c_addr,
|
|
|
|
uint8_t reg_addr,
|
|
|
|
uint8_t data,
|
|
|
|
uint32_t timeout);
|
|
|
|
|
|
|
|
/** Perform I2C device register write (16-bit)
|
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
|
|
|
* @param i2c_addr I2C slave address
|
|
|
|
* @param reg_addr Register address
|
|
|
|
* @param data Register value
|
|
|
|
* @param timeout Timeout in milliseconds
|
2022-01-12 15:29:28 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
|
|
|
bool furi_hal_i2c_write_reg_16(
|
|
|
|
FuriHalI2cBusHandle* handle,
|
|
|
|
uint8_t i2c_addr,
|
|
|
|
uint8_t reg_addr,
|
|
|
|
uint16_t data,
|
|
|
|
uint32_t timeout);
|
|
|
|
|
|
|
|
/** Perform I2C device memory
|
|
|
|
*
|
2023-09-21 07:54:25 +00:00
|
|
|
* @param handle Pointer to FuriHalI2cBusHandle instance
|
|
|
|
* @param i2c_addr I2C slave address
|
|
|
|
* @param mem_addr Memory start address
|
|
|
|
* @param data Pointer to data buffer
|
|
|
|
* @param len Size of data buffer
|
|
|
|
* @param timeout Timeout in milliseconds
|
2022-01-12 15:29:28 +00:00
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
|
|
|
*/
|
|
|
|
bool furi_hal_i2c_write_mem(
|
|
|
|
FuriHalI2cBusHandle* handle,
|
|
|
|
uint8_t i2c_addr,
|
|
|
|
uint8_t mem_addr,
|
2023-09-21 07:54:25 +00:00
|
|
|
const uint8_t* data,
|
|
|
|
size_t len,
|
2022-01-12 15:29:28 +00:00
|
|
|
uint32_t timeout);
|
|
|
|
|
2021-05-18 09:23:14 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|