unleashed-firmware/lib/drivers/lp5562.c
あく 68a3f6b4b7
[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>
2021-02-18 15:49:32 +03:00

90 lines
2.4 KiB
C

#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*)&reg);
}
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*)&reg);
}
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);
}