[FL-873] Add F5 target, lp5562 driver and api-hal-light (#347)
* Add F5 target, lp5562 driver and api-hal-light. Update api-usage, switch to F5 by default. * API HAL: add i2c and hardware version api. Dolphin: show hardware version. * OTP version generator and flashing utility. * Assets script: fix code formatting * Backport F5 changes to F4 * F4: disable insomnia, prevent damage to BLE RX path * F5 HAL API Light: remove magic delay to fix magic BLE * Dolphin: HW target validation on start * invert RSSI indication in sub-1 * API HAL: rename board to body in version api * Gpio tester: detach and release viewport on exit Co-authored-by: aanper <mail@s3f.ru>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
|
||||
#include <bq25896.h>
|
||||
#include <bq25896_reg.h>
|
||||
#include <i2c.h>
|
||||
|
||||
#include <api-hal.h>
|
||||
|
||||
uint8_t bit_reverse(uint8_t b) {
|
||||
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
|
||||
@@ -11,13 +11,17 @@ uint8_t bit_reverse(uint8_t b) {
|
||||
}
|
||||
|
||||
bool bq25896_read(uint8_t address, uint8_t* data, size_t size) {
|
||||
if (HAL_I2C_Master_Transmit(&POWER_I2C, BQ25896_ADDRESS, &address, 1, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
if (HAL_I2C_Master_Receive(&POWER_I2C, BQ25896_ADDRESS, data, size, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
bool ret;
|
||||
with_api_hal_i2c(bool, &ret, (){
|
||||
if (HAL_I2C_Master_Transmit(&POWER_I2C, BQ25896_ADDRESS, &address, 1, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
if (HAL_I2C_Master_Receive(&POWER_I2C, BQ25896_ADDRESS, data, size, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool bq25896_read_reg(uint8_t address, uint8_t* data) {
|
||||
@@ -27,11 +31,14 @@ 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 };
|
||||
|
||||
if (HAL_I2C_Master_Transmit(&POWER_I2C, BQ25896_ADDRESS, buffer, 2, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
bool ret;
|
||||
with_api_hal_i2c(bool, &ret, (){
|
||||
if (HAL_I2C_Master_Transmit(&POWER_I2C, BQ25896_ADDRESS, buffer, 2, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
|
@@ -1,30 +1,38 @@
|
||||
#include <bq27220.h>
|
||||
#include <bq27220_reg.h>
|
||||
#include <i2c.h>
|
||||
|
||||
#include <api-hal.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
uint16_t bq27220_read_word(uint8_t address) {
|
||||
uint8_t data[2] = { address };
|
||||
if (HAL_I2C_Master_Transmit(&POWER_I2C, BQ27220_ADDRESS, data, 1, 2000) != HAL_OK) {
|
||||
return BQ27220_ERROR;
|
||||
}
|
||||
uint16_t ret;
|
||||
with_api_hal_i2c(uint16_t, &ret, (){
|
||||
if (HAL_I2C_Master_Transmit(&POWER_I2C, BQ27220_ADDRESS, data, 1, 2000) != HAL_OK) {
|
||||
return BQ27220_ERROR;
|
||||
}
|
||||
|
||||
if (HAL_I2C_Master_Receive(&POWER_I2C, BQ27220_ADDRESS, data, 2, 2000) != HAL_OK) {
|
||||
return BQ27220_ERROR;
|
||||
}
|
||||
return *(uint16_t*)data;
|
||||
if (HAL_I2C_Master_Receive(&POWER_I2C, BQ27220_ADDRESS, data, 2, 2000) != HAL_OK) {
|
||||
return BQ27220_ERROR;
|
||||
}
|
||||
return *(uint16_t*)data;
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool bq27220_control(uint16_t control) {
|
||||
uint8_t data[3];
|
||||
data[0] = CommandControl;
|
||||
data[1] = (control>>8) & 0xFF;
|
||||
data[2] = control & 0xFF;
|
||||
if (HAL_I2C_Master_Transmit(&POWER_I2C, BQ27220_ADDRESS, data, 3, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
bool ret;
|
||||
with_api_hal_i2c(bool, &ret, (){
|
||||
uint8_t data[3];
|
||||
data[0] = CommandControl;
|
||||
data[1] = (control>>8) & 0xFF;
|
||||
data[2] = control & 0xFF;
|
||||
if (HAL_I2C_Master_Transmit(&POWER_I2C, BQ27220_ADDRESS, data, 3, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
void bq27220_init() {
|
||||
|
90
lib/drivers/lp5562.c
Normal file
90
lib/drivers/lp5562.c
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "lp5562.h"
|
||||
#include "lp5562_reg.h"
|
||||
|
||||
#include <api-hal.h>
|
||||
#include <stdio.h>
|
||||
|
||||
bool lp5562_read(uint8_t address, uint8_t* data, size_t size) {
|
||||
bool ret;
|
||||
with_api_hal_i2c(bool, &ret, (){
|
||||
if (HAL_I2C_Master_Transmit(&POWER_I2C, LP5562_ADDRESS, &address, 1, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
if (HAL_I2C_Master_Receive(&POWER_I2C, LP5562_ADDRESS, data, size, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool lp5562_read_reg(uint8_t address, uint8_t* data) {
|
||||
return lp5562_read(address, data, 1);
|
||||
}
|
||||
|
||||
bool lp5562_write_reg(uint8_t address, uint8_t *data) {
|
||||
uint8_t buffer[2] = { address, *data };
|
||||
bool ret;
|
||||
with_api_hal_i2c(bool, &ret, (){
|
||||
if (HAL_I2C_Master_Transmit(&POWER_I2C, LP5562_ADDRESS, buffer, 2, 2000) != HAL_OK) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
void lp5562_reset() {
|
||||
Reg0D_Reset reg = {.value = 0xFF };
|
||||
lp5562_write_reg(0x0D, (uint8_t*)®);
|
||||
}
|
||||
|
||||
void lp5562_configure() {
|
||||
Reg08_Config config = { .INT_CLK_EN = true, .PS_EN = true, .PWM_HF = true };
|
||||
lp5562_write_reg(0x08, (uint8_t*)&config);
|
||||
|
||||
Reg70_LedMap map = {
|
||||
.red = EngSelectI2C,
|
||||
.green = EngSelectI2C,
|
||||
.blue = EngSelectI2C,
|
||||
.white = EngSelectI2C,
|
||||
};
|
||||
lp5562_write_reg(0x70, (uint8_t*)&map);
|
||||
}
|
||||
|
||||
void lp5562_enable() {
|
||||
Reg00_Enable reg = { .CHIP_EN = true, .LOG_EN = true };
|
||||
lp5562_write_reg(0x00, (uint8_t*)®);
|
||||
}
|
||||
|
||||
void lp5562_set_channel_current(LP5562Channel channel, uint8_t value) {
|
||||
uint8_t reg_no;
|
||||
if (channel == LP5562ChannelRed) {
|
||||
reg_no = 0x07;
|
||||
} else if (channel == LP5562ChannelGreen) {
|
||||
reg_no = 0x06;
|
||||
} else if (channel == LP5562ChannelBlue) {
|
||||
reg_no = 0x05;
|
||||
} else if (channel == LP5562ChannelWhite) {
|
||||
reg_no = 0x0F;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
lp5562_write_reg(reg_no, &value);
|
||||
}
|
||||
|
||||
void lp5562_set_channel_value(LP5562Channel channel, uint8_t value) {
|
||||
uint8_t reg_no;
|
||||
if (channel == LP5562ChannelRed) {
|
||||
reg_no = 0x04;
|
||||
} else if (channel == LP5562ChannelGreen) {
|
||||
reg_no = 0x03;
|
||||
} else if (channel == LP5562ChannelBlue) {
|
||||
reg_no = 0x02;
|
||||
} else if (channel == LP5562ChannelWhite) {
|
||||
reg_no = 0x0E;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
lp5562_write_reg(reg_no, &value);
|
||||
}
|
22
lib/drivers/lp5562.h
Normal file
22
lib/drivers/lp5562.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum {
|
||||
LP5562ChannelRed,
|
||||
LP5562ChannelGreen,
|
||||
LP5562ChannelBlue,
|
||||
LP5562ChannelWhite,
|
||||
} LP5562Channel;
|
||||
|
||||
/* Initialize Driver */
|
||||
void lp5562_reset();
|
||||
|
||||
void lp5562_configure();
|
||||
|
||||
void lp5562_enable();
|
||||
|
||||
void lp5562_set_channel_current(LP5562Channel channel, uint8_t value);
|
||||
|
||||
void lp5562_set_channel_value(LP5562Channel channel, uint8_t value);
|
86
lib/drivers/lp5562_reg.h
Normal file
86
lib/drivers/lp5562_reg.h
Normal file
@@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
|
||||
#if BITS_BIG_ENDIAN == 1
|
||||
#error Bit structures defined in this file is not portable to BE
|
||||
#endif
|
||||
|
||||
#define LP5562_ADDRESS 0x60
|
||||
|
||||
typedef enum {
|
||||
EngExecHold=0b00,
|
||||
EngExecStep=0b01,
|
||||
EngExecRun=0b10,
|
||||
EngExecPC=0b11,
|
||||
} EngExec;
|
||||
|
||||
typedef struct {
|
||||
EngExec ENG3_EXEC:2;
|
||||
EngExec ENG2_EXEC:2;
|
||||
EngExec ENG1_EXEC:2;
|
||||
bool CHIP_EN:1;
|
||||
bool LOG_EN:1;
|
||||
} Reg00_Enable;
|
||||
|
||||
typedef enum {
|
||||
EngModeDisable=0b00,
|
||||
EngModeLoad=0b01,
|
||||
EngModeRun=0b10,
|
||||
EngModeDirect=0b11,
|
||||
} EngMode;
|
||||
|
||||
typedef struct {
|
||||
EngMode ENG3_MODE:2;
|
||||
EngMode ENG2_MODE:2;
|
||||
EngMode ENG1_MODE:2;
|
||||
uint8_t reserved:2;
|
||||
} Reg01_OpMode;
|
||||
|
||||
typedef struct {
|
||||
bool INT_CLK_EN:1;
|
||||
bool CLK_DET_EN:1;
|
||||
uint8_t reserved0:3;
|
||||
bool PS_EN:1;
|
||||
bool PWM_HF:1;
|
||||
uint8_t reserved1:1;
|
||||
} Reg08_Config;
|
||||
|
||||
typedef struct {
|
||||
uint8_t pc:3;
|
||||
uint8_t reserved:5;
|
||||
} Reg09_Engine1PC;
|
||||
|
||||
typedef struct {
|
||||
uint8_t pc:3;
|
||||
uint8_t reserved:5;
|
||||
} Reg0A_Engine2PC;
|
||||
|
||||
typedef struct {
|
||||
uint8_t pc:3;
|
||||
uint8_t reserved:5;
|
||||
} Reg0B_Engine3PC;
|
||||
|
||||
typedef struct {
|
||||
bool ENG3_INT:1;
|
||||
bool ENG2_INT:1;
|
||||
bool ENG1_INT:1;
|
||||
bool EXT_CLK_USED:1;
|
||||
uint8_t reserved:5;
|
||||
} Reg0C_Status;
|
||||
|
||||
typedef struct {
|
||||
uint8_t value;
|
||||
} Reg0D_Reset;
|
||||
|
||||
typedef enum {
|
||||
EngSelectI2C=0b00,
|
||||
EngSelectEngine1=0b01,
|
||||
EngSelectEngine2=0b10,
|
||||
EngSelectEngine3=0b11,
|
||||
} EngSelect;
|
||||
|
||||
typedef struct {
|
||||
EngSelect blue:2;
|
||||
EngSelect green:2;
|
||||
EngSelect red:2;
|
||||
EngSelect white:2;
|
||||
} Reg70_LedMap;
|
Reference in New Issue
Block a user