[FL-1506, FL-2197] Power, USB, LED driver improvements (#966)
* Power, USB, LED driver improvements * u2f hid descriptor fix * variable_item_list: value alignment fix * InputTypeRepeat handling in menu/submenu/var_item_list * lp5562: fix bugs on 400khz i2c * Scripts: lint in parallel. * FuriHal: rename some USB structure to match naming convention. Drivers: update magic values in LP5562. Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -1,21 +1,17 @@
|
||||
#include "lp5562.h"
|
||||
#include "lp5562_reg.h"
|
||||
#include <furi_hal.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static bool lp5562_write_reg(FuriHalI2cBusHandle* handle, uint8_t address, uint8_t* data) {
|
||||
uint8_t buffer[2] = {address, *data};
|
||||
return furi_hal_i2c_tx(handle, LP5562_ADDRESS, buffer, 2, LP5562_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
void lp5562_reset(FuriHalI2cBusHandle* handle) {
|
||||
Reg0D_Reset reg = {.value = 0xFF};
|
||||
lp5562_write_reg(handle, 0x0D, (uint8_t*)®);
|
||||
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x0D, *(uint8_t*)®, LP5562_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
void lp5562_configure(FuriHalI2cBusHandle* handle) {
|
||||
Reg08_Config config = {.INT_CLK_EN = true, .PS_EN = true, .PWM_HF = true};
|
||||
lp5562_write_reg(handle, 0x08, (uint8_t*)&config);
|
||||
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x08, *(uint8_t*)&config, LP5562_I2C_TIMEOUT);
|
||||
|
||||
Reg70_LedMap map = {
|
||||
.red = EngSelectI2C,
|
||||
@@ -23,42 +19,178 @@ void lp5562_configure(FuriHalI2cBusHandle* handle) {
|
||||
.blue = EngSelectI2C,
|
||||
.white = EngSelectI2C,
|
||||
};
|
||||
lp5562_write_reg(handle, 0x70, (uint8_t*)&map);
|
||||
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x70, *(uint8_t*)&map, LP5562_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
void lp5562_enable(FuriHalI2cBusHandle* handle) {
|
||||
Reg00_Enable reg = {.CHIP_EN = true, .LOG_EN = true};
|
||||
lp5562_write_reg(handle, 0x00, (uint8_t*)®);
|
||||
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x00, *(uint8_t*)®, LP5562_I2C_TIMEOUT);
|
||||
//>488μs delay is required after writing to 0x00 register, otherwise program engine will not work
|
||||
delay_us(500);
|
||||
}
|
||||
|
||||
void lp5562_set_channel_current(FuriHalI2cBusHandle* handle, LP5562Channel channel, uint8_t value) {
|
||||
uint8_t reg_no;
|
||||
if(channel == LP5562ChannelRed) {
|
||||
reg_no = 0x07;
|
||||
reg_no = LP5562_CHANNEL_RED_CURRENT_REGISTER;
|
||||
} else if(channel == LP5562ChannelGreen) {
|
||||
reg_no = 0x06;
|
||||
reg_no = LP5562_CHANNEL_GREEN_CURRENT_REGISTER;
|
||||
} else if(channel == LP5562ChannelBlue) {
|
||||
reg_no = 0x05;
|
||||
reg_no = LP5562_CHANNEL_BLUE_CURRENT_REGISTER;
|
||||
} else if(channel == LP5562ChannelWhite) {
|
||||
reg_no = 0x0F;
|
||||
reg_no = LP5562_CHANNEL_WHITE_CURRENT_REGISTER;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
lp5562_write_reg(handle, reg_no, &value);
|
||||
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, reg_no, value, LP5562_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
void lp5562_set_channel_value(FuriHalI2cBusHandle* handle, LP5562Channel channel, uint8_t value) {
|
||||
uint8_t reg_no;
|
||||
if(channel == LP5562ChannelRed) {
|
||||
reg_no = 0x04;
|
||||
reg_no = LP5562_CHANNEL_RED_VALUE_REGISTER;
|
||||
} else if(channel == LP5562ChannelGreen) {
|
||||
reg_no = 0x03;
|
||||
reg_no = LP5562_CHANNEL_GREEN_VALUE_REGISTER;
|
||||
} else if(channel == LP5562ChannelBlue) {
|
||||
reg_no = 0x02;
|
||||
reg_no = LP5562_CHANNEL_BLUE_VALUE_REGISTER;
|
||||
} else if(channel == LP5562ChannelWhite) {
|
||||
reg_no = 0x0E;
|
||||
reg_no = LP5562_CHANNEL_WHITE_VALUE_REGISTER;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
lp5562_write_reg(handle, reg_no, &value);
|
||||
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, reg_no, value, LP5562_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
uint8_t lp5562_get_channel_value(FuriHalI2cBusHandle* handle, LP5562Channel channel) {
|
||||
uint8_t reg_no;
|
||||
uint8_t value;
|
||||
if(channel == LP5562ChannelRed) {
|
||||
reg_no = LP5562_CHANNEL_RED_VALUE_REGISTER;
|
||||
} else if(channel == LP5562ChannelGreen) {
|
||||
reg_no = LP5562_CHANNEL_GREEN_VALUE_REGISTER;
|
||||
} else if(channel == LP5562ChannelBlue) {
|
||||
reg_no = LP5562_CHANNEL_BLUE_VALUE_REGISTER;
|
||||
} else if(channel == LP5562ChannelWhite) {
|
||||
reg_no = LP5562_CHANNEL_WHITE_VALUE_REGISTER;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, reg_no, &value, LP5562_I2C_TIMEOUT);
|
||||
return value;
|
||||
}
|
||||
|
||||
static void
|
||||
lp5562_set_channel_src(FuriHalI2cBusHandle* handle, LP5562Channel channel, LP5562Engine src) {
|
||||
uint8_t reg_val = 0;
|
||||
uint8_t bit_offset = 0;
|
||||
|
||||
if(channel == LP5562ChannelRed) {
|
||||
bit_offset = 4;
|
||||
} else if(channel == LP5562ChannelGreen) {
|
||||
bit_offset = 2;
|
||||
} else if(channel == LP5562ChannelBlue) {
|
||||
bit_offset = 0;
|
||||
} else if(channel == LP5562ChannelWhite) {
|
||||
bit_offset = 6;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, 0x70, ®_val, LP5562_I2C_TIMEOUT);
|
||||
reg_val &= ~(0x3 << bit_offset);
|
||||
reg_val |= ((src & 0x03) << bit_offset);
|
||||
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x70, reg_val, LP5562_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
void lp5562_execute_program(
|
||||
FuriHalI2cBusHandle* handle,
|
||||
LP5562Engine eng,
|
||||
LP5562Channel ch,
|
||||
uint16_t* program) {
|
||||
if((eng < LP5562Engine1) || (eng > LP5562Engine3)) return;
|
||||
uint8_t reg_val = 0;
|
||||
uint8_t bit_offset = 0;
|
||||
uint8_t enable_reg = 0;
|
||||
|
||||
// Read old value of enable register
|
||||
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, 0x00, &enable_reg, LP5562_I2C_TIMEOUT);
|
||||
|
||||
// Engine configuration
|
||||
bit_offset = (3 - eng) * 2;
|
||||
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, 0x01, ®_val, LP5562_I2C_TIMEOUT);
|
||||
reg_val &= ~(0x3 << bit_offset);
|
||||
reg_val |= (0x01 << bit_offset); // load
|
||||
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x01, reg_val, LP5562_I2C_TIMEOUT);
|
||||
delay_us(100);
|
||||
|
||||
// Program load
|
||||
for(uint8_t i = 0; i < 16; i++) {
|
||||
// Program words are big-endian, so reverse byte order before loading
|
||||
program[i] = __REV16(program[i]);
|
||||
}
|
||||
furi_hal_i2c_write_mem(
|
||||
handle,
|
||||
LP5562_ADDRESS,
|
||||
0x10 + (0x20 * (eng - 1)),
|
||||
(uint8_t*)program,
|
||||
16 * 2,
|
||||
LP5562_I2C_TIMEOUT);
|
||||
|
||||
// Program start
|
||||
bit_offset = (3 - eng) * 2;
|
||||
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, 0x01, ®_val, LP5562_I2C_TIMEOUT);
|
||||
reg_val &= ~(0x3 << bit_offset);
|
||||
reg_val |= (0x02 << bit_offset); // run
|
||||
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x01, reg_val, LP5562_I2C_TIMEOUT);
|
||||
|
||||
// Switch output to Execution Engine
|
||||
lp5562_set_channel_src(handle, ch, eng);
|
||||
|
||||
enable_reg &= ~(0x3 << bit_offset);
|
||||
enable_reg |= (0x02 << bit_offset); // run
|
||||
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x00, enable_reg, LP5562_I2C_TIMEOUT);
|
||||
}
|
||||
|
||||
void lp5562_execute_ramp(
|
||||
FuriHalI2cBusHandle* handle,
|
||||
LP5562Engine eng,
|
||||
LP5562Channel ch,
|
||||
uint8_t val_start,
|
||||
uint8_t val_end,
|
||||
uint16_t time) {
|
||||
if(val_start == val_end) return;
|
||||
|
||||
// Temporary switch to constant value from register
|
||||
lp5562_set_channel_src(handle, ch, LP5562Direct);
|
||||
|
||||
// Prepare command sequence
|
||||
uint16_t program[16];
|
||||
uint8_t diff = (val_end > val_start) ? (val_end - val_start) : (val_start - val_end);
|
||||
uint16_t time_step = time * 2 / diff;
|
||||
uint8_t prescaller = 0;
|
||||
if(time_step > 0x3F) {
|
||||
time_step /= 32;
|
||||
prescaller = 1;
|
||||
}
|
||||
|
||||
if(time_step == 0) {
|
||||
time_step = 1;
|
||||
} else if(time_step > 0x3F)
|
||||
time_step = 0x3F;
|
||||
|
||||
program[0] = 0x4000 | val_start; // Set PWM
|
||||
if(val_end > val_start) {
|
||||
program[1] = (prescaller << 14) | (time_step << 8) | ((diff / 2) & 0x7F); // Ramp Up
|
||||
} else {
|
||||
program[1] = (prescaller << 14) | (time_step << 8) | 0x80 |
|
||||
((diff / 2) & 0x7F); // Ramp Down
|
||||
}
|
||||
program[2] = 0xA001 | ((2 - 1) << 7); // Loop to step 1, repeat twice to get full 8-bit scale
|
||||
program[3] = 0xC000; // End
|
||||
|
||||
// Execute program
|
||||
lp5562_execute_program(handle, eng, LP5562ChannelWhite, program);
|
||||
|
||||
// Write end value to register
|
||||
lp5562_set_channel_value(handle, ch, val_end);
|
||||
}
|
||||
|
Reference in New Issue
Block a user