[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:
あく
2021-11-28 21:28:19 +03:00
committed by GitHub
parent 7c0943e736
commit cf591ef7eb
45 changed files with 1685 additions and 781 deletions

View File

@@ -1,27 +1,21 @@
#include "lp5562.h"
#include "lp5562_reg.h"
#include <furi-hal-i2c.h>
#include <stdio.h>
bool lp5562_write_reg(uint8_t address, uint8_t* data) {
static bool lp5562_write_reg(FuriHalI2cBusHandle* handle, uint8_t address, uint8_t* data) {
uint8_t buffer[2] = {address, *data};
bool ret;
with_furi_hal_i2c(
bool, &ret, () {
return furi_hal_i2c_tx(POWER_I2C, LP5562_ADDRESS, buffer, 2, LP5562_I2C_TIMEOUT);
});
return ret;
return furi_hal_i2c_tx(handle, LP5562_ADDRESS, buffer, 2, LP5562_I2C_TIMEOUT);
}
void lp5562_reset() {
void lp5562_reset(FuriHalI2cBusHandle* handle) {
Reg0D_Reset reg = {.value = 0xFF};
lp5562_write_reg(0x0D, (uint8_t*)&reg);
lp5562_write_reg(handle, 0x0D, (uint8_t*)&reg);
}
void lp5562_configure() {
void lp5562_configure(FuriHalI2cBusHandle* handle) {
Reg08_Config config = {.INT_CLK_EN = true, .PS_EN = true, .PWM_HF = true};
lp5562_write_reg(0x08, (uint8_t*)&config);
lp5562_write_reg(handle, 0x08, (uint8_t*)&config);
Reg70_LedMap map = {
.red = EngSelectI2C,
@@ -29,15 +23,15 @@ void lp5562_configure() {
.blue = EngSelectI2C,
.white = EngSelectI2C,
};
lp5562_write_reg(0x70, (uint8_t*)&map);
lp5562_write_reg(handle, 0x70, (uint8_t*)&map);
}
void lp5562_enable() {
void lp5562_enable(FuriHalI2cBusHandle* handle) {
Reg00_Enable reg = {.CHIP_EN = true, .LOG_EN = true};
lp5562_write_reg(0x00, (uint8_t*)&reg);
lp5562_write_reg(handle, 0x00, (uint8_t*)&reg);
}
void lp5562_set_channel_current(LP5562Channel channel, uint8_t value) {
void lp5562_set_channel_current(FuriHalI2cBusHandle* handle, LP5562Channel channel, uint8_t value) {
uint8_t reg_no;
if(channel == LP5562ChannelRed) {
reg_no = 0x07;
@@ -50,10 +44,10 @@ void lp5562_set_channel_current(LP5562Channel channel, uint8_t value) {
} else {
return;
}
lp5562_write_reg(reg_no, &value);
lp5562_write_reg(handle, reg_no, &value);
}
void lp5562_set_channel_value(LP5562Channel channel, uint8_t value) {
void lp5562_set_channel_value(FuriHalI2cBusHandle* handle, LP5562Channel channel, uint8_t value) {
uint8_t reg_no;
if(channel == LP5562ChannelRed) {
reg_no = 0x04;
@@ -66,5 +60,5 @@ void lp5562_set_channel_value(LP5562Channel channel, uint8_t value) {
} else {
return;
}
lp5562_write_reg(reg_no, &value);
lp5562_write_reg(handle, reg_no, &value);
}