2021-10-03 10:36:05 +00:00
|
|
|
/**
|
|
|
|
* @file furi-hal-i2c.h
|
|
|
|
* I2C HAL API
|
|
|
|
*/
|
|
|
|
|
2021-02-18 12:49:32 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2021-08-08 18:03:25 +00:00
|
|
|
#include <furi-hal-resources.h>
|
2021-02-18 12:49:32 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Init I2C
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_i2c_init();
|
2021-02-18 12:49:32 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Perform I2C tx transfer
|
|
|
|
*
|
|
|
|
* @param instance I2C_TypeDef instance
|
|
|
|
* @param address I2C slave address
|
|
|
|
* @param data pointer to data buffer
|
|
|
|
* @param size size of data buffer
|
|
|
|
* @param timeout timeout in CPU ticks
|
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
2021-03-24 09:35:33 +00:00
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
bool furi_hal_i2c_tx(
|
2021-03-10 13:23:54 +00:00
|
|
|
I2C_TypeDef* instance,
|
|
|
|
const uint8_t address,
|
|
|
|
const uint8_t* data,
|
|
|
|
const uint8_t size,
|
|
|
|
uint32_t timeout);
|
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Perform I2C rx transfer
|
|
|
|
*
|
|
|
|
* @param instance I2C_TypeDef instance
|
|
|
|
* @param address I2C slave address
|
|
|
|
* @param data pointer to data buffer
|
|
|
|
* @param size size of data buffer
|
|
|
|
* @param timeout timeout in CPU ticks
|
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
2021-03-24 09:35:33 +00:00
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
bool furi_hal_i2c_rx(
|
2021-03-10 13:23:54 +00:00
|
|
|
I2C_TypeDef* instance,
|
|
|
|
const uint8_t address,
|
|
|
|
uint8_t* data,
|
|
|
|
const uint8_t size,
|
|
|
|
uint32_t timeout);
|
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Perform I2C tx and rx transfers
|
|
|
|
*
|
|
|
|
* @param instance I2C_TypeDef instance
|
|
|
|
* @param address I2C slave address
|
|
|
|
* @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 CPU ticks
|
|
|
|
*
|
|
|
|
* @return true on successful transfer, false otherwise
|
2021-03-24 09:35:33 +00:00
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
bool furi_hal_i2c_trx(
|
2021-03-10 13:23:54 +00:00
|
|
|
I2C_TypeDef* instance,
|
|
|
|
const uint8_t address,
|
|
|
|
const uint8_t* tx_data,
|
|
|
|
const uint8_t tx_size,
|
|
|
|
uint8_t* rx_data,
|
|
|
|
const uint8_t rx_size,
|
|
|
|
uint32_t timeout);
|
2021-03-02 11:45:47 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Acquire I2C mutex
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_i2c_lock();
|
2021-02-18 12:49:32 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Release I2C mutex
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_i2c_unlock();
|
2021-02-18 12:49:32 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** With clause for I2C peripheral
|
|
|
|
*
|
|
|
|
* @param type type of function_body
|
|
|
|
* @param pointer pointer to return of function_body
|
|
|
|
* @param function_body a (){} lambda declaration, executed with I2C mutex
|
|
|
|
* acquired
|
|
|
|
*
|
|
|
|
* @return Nothing
|
2021-03-24 09:35:33 +00:00
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
#define with_furi_hal_i2c(type, pointer, function_body) \
|
2021-03-10 13:23:54 +00:00
|
|
|
{ \
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_i2c_lock(); \
|
2021-03-10 13:23:54 +00:00
|
|
|
*pointer = ({ type __fn__ function_body __fn__; })(); \
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_i2c_unlock(); \
|
2021-02-18 12:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|