API HAL I2C: add timeout support. Update I2C API usage by drivers. (#374)

* API HAL I2C: add timeout support. Update I2C API usage by drivers.
* F4: Add missing API HAL Vibro implementation.
This commit is contained in:
あく 2021-03-10 16:23:54 +03:00 committed by GitHub
parent 468ee09785
commit 2f14f6dac5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 502 additions and 334 deletions

View File

@ -4,6 +4,7 @@
#include <stm32wbxx_ll_i2c.h>
#include <stm32wbxx_ll_rcc.h>
#include <stm32wbxx_ll_gpio.h>
#include <stm32wbxx_ll_cortex.h>
void api_hal_i2c_init() {
LL_I2C_InitTypeDef I2C_InitStruct = {0};
@ -37,7 +38,15 @@ void api_hal_i2c_init() {
LL_I2C_EnableClockStretching(I2C1);
}
void api_hal_i2c_tx(I2C_TypeDef* instance, uint8_t address, const uint8_t* data, uint8_t size) {
bool api_hal_i2c_tx(
I2C_TypeDef* instance,
uint8_t address,
const uint8_t* data,
uint8_t size,
uint32_t timeout) {
uint32_t time_left = timeout;
bool ret = true;
LL_I2C_HandleTransfer(
instance,
address,
@ -49,13 +58,30 @@ void api_hal_i2c_tx(I2C_TypeDef* instance, uint8_t address, const uint8_t* data,
while(!LL_I2C_IsActiveFlag_STOP(instance)) {
if(LL_I2C_IsActiveFlag_TXIS(instance)) {
LL_I2C_TransmitData8(instance, (*data++));
time_left = timeout;
}
if(LL_SYSTICK_IsActiveCounterFlag()) {
if(--time_left == 0) {
ret = false;
break;
}
}
}
LL_I2C_ClearFlag_STOP(instance);
return ret;
}
void api_hal_i2c_rx(I2C_TypeDef* instance, uint8_t address, uint8_t* data, uint8_t size) {
bool api_hal_i2c_rx(
I2C_TypeDef* instance,
uint8_t address,
uint8_t* data,
uint8_t size,
uint32_t timeout) {
uint32_t time_left = timeout;
bool ret = true;
LL_I2C_HandleTransfer(
instance,
address,
@ -67,19 +93,33 @@ void api_hal_i2c_rx(I2C_TypeDef* instance, uint8_t address, uint8_t* data, uint8
while(!LL_I2C_IsActiveFlag_STOP(instance)) {
if(LL_I2C_IsActiveFlag_RXNE(instance)) {
*data++ = LL_I2C_ReceiveData8(instance);
time_left = timeout;
}
if(LL_SYSTICK_IsActiveCounterFlag()) {
if(--time_left == 0) {
ret = false;
break;
}
}
}
LL_I2C_ClearFlag_STOP(instance);
return ret;
}
void api_hal_i2c_trx(
bool api_hal_i2c_trx(
I2C_TypeDef* instance,
uint8_t address,
const uint8_t* tx_data,
uint8_t tx_size,
uint8_t* rx_data,
uint8_t rx_size) {
api_hal_i2c_tx(instance, address, tx_data, tx_size);
api_hal_i2c_rx(instance, address, rx_data, rx_size);
uint8_t rx_size,
uint32_t timeout) {
if(api_hal_i2c_tx(instance, address, tx_data, tx_size, timeout) &&
api_hal_i2c_rx(instance, address, rx_data, rx_size, timeout)) {
return true;
} else {
return false;
}
}

View File

@ -10,25 +10,28 @@ extern "C" {
void api_hal_i2c_init();
void api_hal_i2c_tx(
bool api_hal_i2c_tx(
I2C_TypeDef* instance,
const uint8_t address,
const uint8_t* data,
const uint8_t size);
const uint8_t size,
uint32_t timeout);
void api_hal_i2c_rx(
bool api_hal_i2c_rx(
I2C_TypeDef* instance,
const uint8_t address,
uint8_t* data,
const uint8_t size);
const uint8_t size,
uint32_t timeout);
void api_hal_i2c_trx(
bool api_hal_i2c_trx(
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);
const uint8_t rx_size,
uint32_t timeout);
#define with_api_hal_i2c(type, pointer, function_body) \
{ \

View File

@ -10,15 +10,28 @@ extern "C" {
void api_hal_i2c_init();
void api_hal_i2c_tx(I2C_TypeDef* instance, const uint8_t address, const uint8_t *data, const uint8_t size);
bool api_hal_i2c_tx(
I2C_TypeDef* instance,
const uint8_t address,
const uint8_t* data,
const uint8_t size,
uint32_t timeout);
void api_hal_i2c_rx(I2C_TypeDef* instance, const uint8_t address, uint8_t *data, const uint8_t size);
bool api_hal_i2c_rx(
I2C_TypeDef* instance,
const uint8_t address,
uint8_t* data,
const uint8_t size,
uint32_t timeout);
void api_hal_i2c_trx(
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
);
bool api_hal_i2c_trx(
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);
void api_hal_i2c_lock();

View File

@ -1,6 +1,7 @@
#include <api-hal-i2c.h>
#include <stm32wbxx_ll_i2c.h>
#include <stm32wbxx_ll_gpio.h>
#include <stm32wbxx_ll_cortex.h>
#include <furi.h>
osMutexId_t api_hal_i2c_mutex = NULL;
@ -40,33 +41,90 @@ void api_hal_i2c_init() {
LL_I2C_EnableClockStretching(I2C1);
}
void api_hal_i2c_tx(I2C_TypeDef* instance, uint8_t address, const uint8_t *data, uint8_t size) {
LL_I2C_HandleTransfer(instance, address, LL_I2C_ADDRSLAVE_7BIT, size, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
bool api_hal_i2c_tx(
I2C_TypeDef* instance,
uint8_t address,
const uint8_t* data,
uint8_t size,
uint32_t timeout) {
uint32_t time_left = timeout;
bool ret = true;
LL_I2C_HandleTransfer(
instance,
address,
LL_I2C_ADDRSLAVE_7BIT,
size,
LL_I2C_MODE_AUTOEND,
LL_I2C_GENERATE_START_WRITE);
while(!LL_I2C_IsActiveFlag_STOP(instance)) {
if(LL_I2C_IsActiveFlag_TXIS(instance)) {
LL_I2C_TransmitData8(instance, (*data++));
time_left = timeout;
}
if(LL_SYSTICK_IsActiveCounterFlag()) {
if(--time_left == 0) {
ret = false;
break;
}
}
}
LL_I2C_ClearFlag_STOP(instance);
return ret;
}
void api_hal_i2c_rx(I2C_TypeDef* instance, uint8_t address, uint8_t *data, uint8_t size) {
LL_I2C_HandleTransfer(instance, address, LL_I2C_ADDRSLAVE_7BIT, size, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ);
bool api_hal_i2c_rx(
I2C_TypeDef* instance,
uint8_t address,
uint8_t* data,
uint8_t size,
uint32_t timeout) {
uint32_t time_left = timeout;
bool ret = true;
LL_I2C_HandleTransfer(
instance,
address,
LL_I2C_ADDRSLAVE_7BIT,
size,
LL_I2C_MODE_AUTOEND,
LL_I2C_GENERATE_START_READ);
while(!LL_I2C_IsActiveFlag_STOP(instance)) {
if(LL_I2C_IsActiveFlag_RXNE(instance)) {
*data++ = LL_I2C_ReceiveData8(instance);
time_left = timeout;
}
if(LL_SYSTICK_IsActiveCounterFlag()) {
if(--time_left == 0) {
ret = false;
break;
}
}
}
LL_I2C_ClearFlag_STOP(instance);
return ret;
}
void api_hal_i2c_trx(I2C_TypeDef* instance, uint8_t address, const uint8_t *tx_data, uint8_t tx_size, uint8_t *rx_data, uint8_t rx_size) {
api_hal_i2c_tx(instance, address, tx_data, tx_size);
api_hal_i2c_rx(instance, address, rx_data, rx_size);
bool api_hal_i2c_trx(
I2C_TypeDef* instance,
uint8_t address,
const uint8_t* tx_data,
uint8_t tx_size,
uint8_t* rx_data,
uint8_t rx_size,
uint32_t timeout) {
if(api_hal_i2c_tx(instance, address, tx_data, tx_size, timeout) &&
api_hal_i2c_rx(instance, address, rx_data, rx_size, timeout)) {
return true;
} else {
return false;
}
}
void api_hal_i2c_lock() {

View File

@ -0,0 +1,11 @@
#include <api-hal-vibro.h>
#include <api-hal-gpio.h>
void api_hal_vibro_init() {
hal_gpio_init(&vibro_gpio, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
hal_gpio_write(&vibro_gpio, false);
}
void api_hal_vibro_on(bool value) {
hal_gpio_write(&vibro_gpio, value);
}

View File

@ -7,4 +7,5 @@ void api_hal_init() {
api_hal_i2c_init();
api_hal_power_init();
api_hal_light_init();
api_hal_vibro_init();
}

View File

@ -1,6 +1,7 @@
#include <api-hal-i2c.h>
#include <stm32wbxx_ll_i2c.h>
#include <stm32wbxx_ll_gpio.h>
#include <stm32wbxx_ll_cortex.h>
#include <furi.h>
osMutexId_t api_hal_i2c_mutex = NULL;
@ -40,33 +41,90 @@ void api_hal_i2c_init() {
LL_I2C_EnableClockStretching(I2C1);
}
void api_hal_i2c_tx(I2C_TypeDef* instance, uint8_t address, const uint8_t *data, uint8_t size) {
LL_I2C_HandleTransfer(instance, address, LL_I2C_ADDRSLAVE_7BIT, size, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
bool api_hal_i2c_tx(
I2C_TypeDef* instance,
uint8_t address,
const uint8_t* data,
uint8_t size,
uint32_t timeout) {
uint32_t time_left = timeout;
bool ret = true;
LL_I2C_HandleTransfer(
instance,
address,
LL_I2C_ADDRSLAVE_7BIT,
size,
LL_I2C_MODE_AUTOEND,
LL_I2C_GENERATE_START_WRITE);
while(!LL_I2C_IsActiveFlag_STOP(instance)) {
if(LL_I2C_IsActiveFlag_TXIS(instance)) {
LL_I2C_TransmitData8(instance, (*data++));
time_left = timeout;
}
if(LL_SYSTICK_IsActiveCounterFlag()) {
if(--time_left == 0) {
ret = false;
break;
}
}
}
LL_I2C_ClearFlag_STOP(instance);
return ret;
}
void api_hal_i2c_rx(I2C_TypeDef* instance, uint8_t address, uint8_t *data, uint8_t size) {
LL_I2C_HandleTransfer(instance, address, LL_I2C_ADDRSLAVE_7BIT, size, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ);
bool api_hal_i2c_rx(
I2C_TypeDef* instance,
uint8_t address,
uint8_t* data,
uint8_t size,
uint32_t timeout) {
uint32_t time_left = timeout;
bool ret = true;
LL_I2C_HandleTransfer(
instance,
address,
LL_I2C_ADDRSLAVE_7BIT,
size,
LL_I2C_MODE_AUTOEND,
LL_I2C_GENERATE_START_READ);
while(!LL_I2C_IsActiveFlag_STOP(instance)) {
if(LL_I2C_IsActiveFlag_RXNE(instance)) {
*data++ = LL_I2C_ReceiveData8(instance);
time_left = timeout;
}
if(LL_SYSTICK_IsActiveCounterFlag()) {
if(--time_left == 0) {
ret = false;
break;
}
}
}
LL_I2C_ClearFlag_STOP(instance);
return ret;
}
void api_hal_i2c_trx(I2C_TypeDef* instance, uint8_t address, const uint8_t *tx_data, uint8_t tx_size, uint8_t *rx_data, uint8_t rx_size) {
api_hal_i2c_tx(instance, address, tx_data, tx_size);
api_hal_i2c_rx(instance, address, rx_data, rx_size);
bool api_hal_i2c_trx(
I2C_TypeDef* instance,
uint8_t address,
const uint8_t* tx_data,
uint8_t tx_size,
uint8_t* rx_data,
uint8_t rx_size,
uint32_t timeout) {
if(api_hal_i2c_tx(instance, address, tx_data, tx_size, timeout) &&
api_hal_i2c_rx(instance, address, rx_data, rx_size, timeout)) {
return true;
} else {
return false;
}
}
void api_hal_i2c_lock() {

View File

@ -13,12 +13,10 @@ uint8_t bit_reverse(uint8_t b) {
bool bq25896_read(uint8_t address, uint8_t* data, size_t size) {
bool ret;
with_api_hal_i2c(bool, &ret, (){
api_hal_i2c_trx(
POWER_I2C, BQ25896_ADDRESS,
&address, 1, data, size
);
return true;
with_api_hal_i2c(
bool, &ret, () {
return api_hal_i2c_trx(
POWER_I2C, BQ25896_ADDRESS, &address, 1, data, size, BQ25896_I2C_TIMEOUT);
});
return ret;
}
@ -31,9 +29,9 @@ bool bq25896_read_reg(uint8_t address, uint8_t* data) {
bool bq25896_write_reg(uint8_t address, uint8_t* data) {
uint8_t buffer[2] = {address, *data};
bool ret;
with_api_hal_i2c(bool, &ret, (){
api_hal_i2c_tx(POWER_I2C, BQ25896_ADDRESS, buffer, 2);
return true;
with_api_hal_i2c(
bool, &ret, () {
return api_hal_i2c_tx(POWER_I2C, BQ25896_ADDRESS, buffer, 2, BQ25896_I2C_TIMEOUT);
});
return ret;
}

View File

@ -8,6 +8,7 @@
#endif
#define BQ25896_ADDRESS 0xD6
#define BQ25896_I2C_TIMEOUT 50
#define IILIM_1600 (1 << 5)
#define IILIM_800 (1 << 4)
@ -22,7 +23,6 @@ typedef struct {
bool EN_HIZ : 1; // Enable HIZ Mode
} REG00;
#define VINDPM_OS_1600 (1 << 4)
#define VINDPM_OS_800 (1 << 3)
#define VINDPM_OS_400 (1 << 2)
@ -42,7 +42,6 @@ typedef struct {
Bhot BHOT : 2; // Boost Mode Hot Temperature Monitor Threshold
} REG01;
typedef struct {
bool AUTO_DPDM_EN : 1; // Automatic Input Detection Enable
bool FORCE_DPDM : 1; // Force Input Detection
@ -53,7 +52,6 @@ typedef struct {
bool CONV_START : 1; // ADC Conversion Start Control
} REG02;
#define SYS_MIN_400 (1 << 2)
#define SYS_MIN_200 (1 << 1)
#define SYS_MIN_100 (1 << 0)
@ -67,7 +65,6 @@ typedef struct {
bool BAT_LOADEN : 1; // Battery Load (IBATLOAD) Enable
} REG03;
#define ICHG_4096 (1 << 6)
#define ICHG_2048 (1 << 5)
#define ICHG_1024 (1 << 4)
@ -81,7 +78,6 @@ typedef struct {
bool EN_PUMPX : 1; // Current pulse control Enable
} REG04;
#define IPRETERM_512 (1 << 3)
#define IPRETERM_256 (1 << 2)
#define IPRETERM_128 (1 << 1)
@ -92,7 +88,6 @@ typedef struct {
uint8_t IPRECHG : 4; // Precharge Current Limit, offset: +64mA
} REG05;
#define VREG_512 (1 << 5)
#define VREG_256 (1 << 4)
#define VREG_128 (1 << 3)
@ -106,7 +101,6 @@ typedef struct {
uint8_t VREG : 6; // Charge Voltage Limit, offset: +3840mV
} REG06;
typedef enum {
WatchdogDisable = 0b00,
Watchdog40 = 0b01,
@ -130,7 +124,6 @@ typedef struct {
bool EN_TERM : 1; // Charging Termination Enable
} REG07;
#define BAT_COMP_80 (1 << 2)
#define BAT_COMP_40 (1 << 1)
#define BAT_COMP_20 (1 << 0)
@ -150,7 +143,6 @@ typedef struct {
uint8_t BAT_COMP : 3; // IR Compensation Resistor Setting
} REG08;
typedef struct {
bool PUMPX_DN : 1; // Current pulse control voltage down enable
bool PUMPX_UP : 1; // Current pulse control voltage up enable
@ -162,7 +154,6 @@ typedef struct {
bool FORCE_ICO : 1; // Force Start Input Current Optimizer
} REG09;
#define BOOSTV_512 (1 << 3)
#define BOOSTV_256 (1 << 2)
#define BOOSTV_128 (1 << 1)
@ -183,7 +174,6 @@ typedef struct {
uint8_t BOOSTV : 4; // Boost Mode Voltage Regulation, offset: +4550mV
} REG0A;
typedef enum {
VBusStatNo = 0b000,
VBusStatUSB = 0b001,
@ -206,7 +196,6 @@ typedef struct {
VBusStat VBUS_STAT : 3; // VBUS Status register
} REG0B;
typedef enum {
ChrgFaultNO = 0b00,
ChrgFaultIN = 0b01,
@ -230,7 +219,6 @@ typedef struct {
bool WATCHDOG_FAULT : 1; // Watchdog Fault Status
} REG0C;
#define VINDPM_6400 (1 << 6)
#define VINDPM_3200 (1 << 5)
#define VINDPM_1600 (1 << 4)
@ -244,44 +232,38 @@ typedef struct {
bool FORCE_VINDPM : 1; // VINDPM Threshold Setting Method
} REG0D;
typedef struct {
uint8_t BATV : 7; // ADC conversion of Battery Voltage (VBAT), offset: +2304mV
bool THERM_STAT : 1; // Thermal Regulation Status
} REG0E;
typedef struct {
uint8_t SYSV : 7; // ADDC conversion of System Voltage (VSYS), offset: +2304mV
uint8_t RES : 1; // Reserved: Always reads 0
} REG0F;
typedef struct {
uint8_t TSPCT : 7; // ADC conversion of TS Voltage (TS) as percentage of REGN, offset: +21%
uint8_t RES : 1; // Reserved: Always reads 0
} REG10;
typedef struct {
uint8_t VBUSV : 7; // ADC conversion of VBUS voltage (VBUS), offset: +2600mV
bool VBUS_GD : 1; // VBUS Good Status
} REG11;
typedef struct {
uint8_t ICHGR : 7; // ADC conversion of Charge Current (IBAT) when VBAT > VBATSHORT
uint8_t RES : 1; // Reserved: Always reads 0
} REG12;
typedef struct {
uint8_t IDPM_LIM:6; // Input Current Limit in effect while Input Current Optimizer (ICO) is enabled, offset: 100mA (default)
uint8_t
IDPM_LIM : 6; // Input Current Limit in effect while Input Current Optimizer (ICO) is enabled, offset: 100mA (default)
bool IDPM_STAT : 1; // IINDPM Status
bool VDPM_STAT : 1; // VINDPM Status
} REG13;
typedef struct {
uint8_t DEV_REV : 2; // Device Revision
bool TS_PROFILE : 1; // Temperature Profile

View File

@ -7,25 +7,27 @@
uint16_t bq27220_read_word(uint8_t address) {
uint8_t buffer[2] = {address};
uint16_t ret;
with_api_hal_i2c(uint16_t, &ret, (){
api_hal_i2c_trx(
POWER_I2C, BQ27220_ADDRESS,
buffer, 1, buffer, 2
);
with_api_hal_i2c(
uint16_t, &ret, () {
if(api_hal_i2c_trx(
POWER_I2C, BQ27220_ADDRESS, buffer, 1, buffer, 2, BQ27220_I2C_TIMEOUT)) {
return *(uint16_t*)buffer;
} else {
return 0;
}
});
return ret;
}
bool bq27220_control(uint16_t control) {
bool ret;
with_api_hal_i2c(bool, &ret, (){
with_api_hal_i2c(
bool, &ret, () {
uint8_t buffer[3];
buffer[0] = CommandControl;
buffer[1] = (control >> 8) & 0xFF;
buffer[2] = control & 0xFF;
api_hal_i2c_tx(POWER_I2C, BQ27220_ADDRESS, buffer, 3);
return true;
return api_hal_i2c_tx(POWER_I2C, BQ27220_ADDRESS, buffer, 3, BQ27220_I2C_TIMEOUT);
});
return ret;
}

View File

@ -1,6 +1,7 @@
#pragma once
#define BQ27220_ADDRESS 0xAA
#define BQ27220_I2C_TIMEOUT 50
#define CommandControl 0x00
#define CommandAtRate 0x02

View File

@ -7,9 +7,9 @@
bool lp5562_write_reg(uint8_t address, uint8_t* data) {
uint8_t buffer[2] = {address, *data};
bool ret;
with_api_hal_i2c(bool, &ret, (){
api_hal_i2c_tx(POWER_I2C, LP5562_ADDRESS, buffer, 2);
return true;
with_api_hal_i2c(
bool, &ret, () {
return api_hal_i2c_tx(POWER_I2C, LP5562_ADDRESS, buffer, 2, LP5562_I2C_TIMEOUT);
});
return ret;
}

View File

@ -5,6 +5,7 @@
#endif
#define LP5562_ADDRESS 0x60
#define LP5562_I2C_TIMEOUT 50
typedef enum {
EngExecHold = 0b00,