[FL-1911] FuriHal: i2c refactoring (#847)
* Project: fix release build, replace asserts with checks. * FuriHal: i2c refactoring, new bus access model, flexible bus gpio configuration. * FuriHal: set i2c pins to high on detach. * FuriHal: more i2c bus events, put bus under reset when not used, move bus enable/disable routine to bus handler. * FuriHal: fix i2c deadlock in power api, add external i2c handle.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <furi-hal-resources.h>
|
||||
#include <furi-hal-i2c-config.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -17,18 +17,30 @@ extern "C" {
|
||||
*/
|
||||
void furi_hal_i2c_init();
|
||||
|
||||
/** Acquire i2c bus handle
|
||||
*
|
||||
* @return Instance of FuriHalI2cBus
|
||||
*/
|
||||
void furi_hal_i2c_acquire(FuriHalI2cBusHandle* handle);
|
||||
|
||||
/** Release i2c bus handle
|
||||
*
|
||||
* @param bus instance of FuriHalI2cBus aquired in `furi_hal_i2c_acquire`
|
||||
*/
|
||||
void furi_hal_i2c_release(FuriHalI2cBusHandle* handle);
|
||||
|
||||
/** 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
|
||||
* @param timeout timeout in ticks
|
||||
*
|
||||
* @return true on successful transfer, false otherwise
|
||||
*/
|
||||
bool furi_hal_i2c_tx(
|
||||
I2C_TypeDef* instance,
|
||||
FuriHalI2cBusHandle* handle,
|
||||
const uint8_t address,
|
||||
const uint8_t* data,
|
||||
const uint8_t size,
|
||||
@@ -40,12 +52,12 @@ bool furi_hal_i2c_tx(
|
||||
* @param address I2C slave address
|
||||
* @param data pointer to data buffer
|
||||
* @param size size of data buffer
|
||||
* @param timeout timeout in CPU ticks
|
||||
* @param timeout timeout in ticks
|
||||
*
|
||||
* @return true on successful transfer, false otherwise
|
||||
*/
|
||||
bool furi_hal_i2c_rx(
|
||||
I2C_TypeDef* instance,
|
||||
FuriHalI2cBusHandle* handle,
|
||||
const uint8_t address,
|
||||
uint8_t* data,
|
||||
const uint8_t size,
|
||||
@@ -59,12 +71,12 @@ bool furi_hal_i2c_rx(
|
||||
* @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
|
||||
* @param timeout timeout in ticks
|
||||
*
|
||||
* @return true on successful transfer, false otherwise
|
||||
*/
|
||||
bool furi_hal_i2c_trx(
|
||||
I2C_TypeDef* instance,
|
||||
FuriHalI2cBusHandle* handle,
|
||||
const uint8_t address,
|
||||
const uint8_t* tx_data,
|
||||
const uint8_t tx_size,
|
||||
@@ -72,30 +84,6 @@ bool furi_hal_i2c_trx(
|
||||
const uint8_t rx_size,
|
||||
uint32_t timeout);
|
||||
|
||||
/** Acquire I2C mutex
|
||||
*/
|
||||
void furi_hal_i2c_lock();
|
||||
|
||||
/** Release I2C mutex
|
||||
*/
|
||||
void furi_hal_i2c_unlock();
|
||||
|
||||
/** 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
|
||||
*/
|
||||
#define with_furi_hal_i2c(type, pointer, function_body) \
|
||||
{ \
|
||||
furi_hal_i2c_lock(); \
|
||||
*pointer = ({ type __fn__ function_body __fn__; })(); \
|
||||
furi_hal_i2c_unlock(); \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
107
firmware/targets/furi-hal-include/furi-hal-spi.h
Normal file
107
firmware/targets/furi-hal-include/furi-hal-spi.h
Normal file
@@ -0,0 +1,107 @@
|
||||
#pragma once
|
||||
#include "main.h"
|
||||
#include "furi-hal-spi-config.h"
|
||||
#include <furi-hal-gpio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Init SPI API
|
||||
*/
|
||||
void furi_hal_spi_init();
|
||||
|
||||
/* Bus Level API */
|
||||
|
||||
/** Lock SPI bus
|
||||
* Takes bus mutex, if used
|
||||
*/
|
||||
void furi_hal_spi_bus_lock(const FuriHalSpiBus* bus);
|
||||
|
||||
/** Unlock SPI bus
|
||||
* Releases BUS mutex, if used
|
||||
*/
|
||||
void furi_hal_spi_bus_unlock(const FuriHalSpiBus* bus);
|
||||
|
||||
/** Configure SPI bus
|
||||
* @param bus - spi bus handler
|
||||
* @param config - spi configuration structure
|
||||
*/
|
||||
void furi_hal_spi_bus_configure(const FuriHalSpiBus* bus, const LL_SPI_InitTypeDef* config);
|
||||
|
||||
/** SPI Receive
|
||||
* @param bus - spi bus handler
|
||||
* @param buffer - receive buffer
|
||||
* @param size - transaction size
|
||||
* @param timeout - bus operation timeout in ms
|
||||
*/
|
||||
bool furi_hal_spi_bus_rx(const FuriHalSpiBus* bus, uint8_t* buffer, size_t size, uint32_t timeout);
|
||||
|
||||
/** SPI Transmit
|
||||
* @param bus - spi bus handler
|
||||
* @param buffer - transmit buffer
|
||||
* @param size - transaction size
|
||||
* @param timeout - bus operation timeout in ms
|
||||
*/
|
||||
bool furi_hal_spi_bus_tx(const FuriHalSpiBus* bus, uint8_t* buffer, size_t size, uint32_t timeout);
|
||||
|
||||
/** SPI Transmit and Receive
|
||||
* @param bus - spi bus handlere
|
||||
* @param tx_buffer - device handle
|
||||
* @param rx_buffer - device handle
|
||||
* @param size - transaction size
|
||||
* @param timeout - bus operation timeout in ms
|
||||
*/
|
||||
bool furi_hal_spi_bus_trx(const FuriHalSpiBus* bus, uint8_t* tx_buffer, uint8_t* rx_buffer, size_t size, uint32_t timeout);
|
||||
|
||||
/* Device Level API */
|
||||
|
||||
/** Reconfigure SPI bus for device
|
||||
* @param device - device description
|
||||
*/
|
||||
void furi_hal_spi_device_configure(const FuriHalSpiDevice* device);
|
||||
|
||||
/** Get Device handle
|
||||
* And lock access to the corresponding SPI BUS
|
||||
* @param device_id - device identifier
|
||||
* @return device handle
|
||||
*/
|
||||
const FuriHalSpiDevice* furi_hal_spi_device_get(FuriHalSpiDeviceId device_id);
|
||||
|
||||
/** Return Device handle
|
||||
* And unlock access to the corresponding SPI BUS
|
||||
* @param device - device handle
|
||||
*/
|
||||
void furi_hal_spi_device_return(const FuriHalSpiDevice* device);
|
||||
|
||||
/** SPI Recieve
|
||||
* @param device - device handle
|
||||
* @param buffer - receive buffer
|
||||
* @param size - transaction size
|
||||
* @param timeout - bus operation timeout in ms
|
||||
*/
|
||||
bool furi_hal_spi_device_rx(const FuriHalSpiDevice* device, uint8_t* buffer, size_t size, uint32_t timeout);
|
||||
|
||||
/** SPI Transmit
|
||||
* @param device - device handle
|
||||
* @param buffer - transmit buffer
|
||||
* @param size - transaction size
|
||||
* @param timeout - bus operation timeout in ms
|
||||
*/
|
||||
bool furi_hal_spi_device_tx(const FuriHalSpiDevice* device, uint8_t* buffer, size_t size, uint32_t timeout);
|
||||
|
||||
/** SPI Transmit and Receive
|
||||
* @param device - device handle
|
||||
* @param tx_buffer - device handle
|
||||
* @param rx_buffer - device handle
|
||||
* @param size - transaction size
|
||||
* @param timeout - bus operation timeout in ms
|
||||
*/
|
||||
bool furi_hal_spi_device_trx(const FuriHalSpiDevice* device, uint8_t* tx_buffer, uint8_t* rx_buffer, size_t size, uint32_t timeout);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user