diff options
Diffstat (limited to 'drivers/led/snled27351.c')
-rw-r--r-- | drivers/led/snled27351.c | 61 |
1 files changed, 33 insertions, 28 deletions
diff --git a/drivers/led/snled27351.c b/drivers/led/snled27351.c index 71992b7322..d985e4c5f1 100644 --- a/drivers/led/snled27351.c +++ b/drivers/led/snled27351.c @@ -37,8 +37,7 @@ { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[65]; +uint8_t i2c_transfer_buffer[65]; // These buffers match the SNLED27351 PWM registers. // The control buffers match the PG0 LED On/Off registers. @@ -54,23 +53,27 @@ bool g_led_control_registers_update_required[SNLED27351_DRIVER_COUNT] bool snled27351_write_register(uint8_t addr, uint8_t reg, uint8_t data) { // If the transaction fails function returns false. - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if SNLED27351_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < SNLED27351_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, SNLED27351_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, SNLED27351_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, SNLED27351_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, SNLED27351_I2C_TIMEOUT) != 0) { return false; } #endif return true; } +void snled27351_select_page(uint8_t addr, uint8_t page) { + snled27351_write_register(addr, SNLED27351_REG_COMMAND, page); +} + bool snled27351_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { // Assumes PG1 is already selected. // If any of the transactions fails function returns false. @@ -78,22 +81,22 @@ bool snled27351_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { // Iterate over the pwm_buffer contents at 64 byte intervals. for (uint8_t i = 0; i < SNLED27351_PWM_REGISTER_COUNT; i += 64) { - g_twi_transfer_buffer[0] = i; + i2c_transfer_buffer[0] = i; // Copy the data from i to i+63. // Device will auto-increment register for data after the first byte // Thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer. for (uint8_t j = 0; j < 64; j++) { - g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j]; + i2c_transfer_buffer[1 + j] = pwm_buffer[i + j]; } #if SNLED27351_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < SNLED27351_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 65, SNLED27351_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 65, SNLED27351_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 65, SNLED27351_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 65, SNLED27351_I2C_TIMEOUT) != 0) { return false; } #endif @@ -132,8 +135,8 @@ void snled27351_init_drivers(void) { } void snled27351_init(uint8_t addr) { - // Select to function page - snled27351_write_register(addr, SNLED27351_REG_COMMAND, SNLED27351_COMMAND_FUNCTION); + snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION); + // Setting LED driver to shutdown mode snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN); // Setting internal channel pulldown/pullup @@ -146,33 +149,34 @@ void snled27351_init(uint8_t addr) { snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_2, SNLED27351_SLEW_RATE_CONTROL_MODE_2_DSL_ENABLE | SNLED27351_SLEW_RATE_CONTROL_MODE_2_SSL_ENABLE); // Setting Iref snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SLEEP, 0); - // Set LED CONTROL PAGE (Page 0) - snled27351_write_register(addr, SNLED27351_REG_COMMAND, SNLED27351_COMMAND_LED_CONTROL); + + snled27351_select_page(addr, SNLED27351_COMMAND_LED_CONTROL); + for (int i = 0; i < SNLED27351_LED_CONTROL_ON_OFF_LENGTH; i++) { snled27351_write_register(addr, i, 0x00); } - // Set PWM PAGE (Page 1) - snled27351_write_register(addr, SNLED27351_REG_COMMAND, SNLED27351_COMMAND_PWM); + snled27351_select_page(addr, SNLED27351_COMMAND_PWM); + for (int i = 0; i < SNLED27351_LED_CURRENT_TUNE_LENGTH; i++) { snled27351_write_register(addr, i, 0x00); } - // Set CURRENT PAGE (Page 4) + snled27351_select_page(addr, SNLED27351_COMMAND_CURRENT_TUNE); + uint8_t current_tune_reg_list[SNLED27351_LED_CURRENT_TUNE_LENGTH] = SNLED27351_CURRENT_TUNE; - snled27351_write_register(addr, SNLED27351_REG_COMMAND, SNLED27351_COMMAND_CURRENT_TUNE); for (int i = 0; i < SNLED27351_LED_CURRENT_TUNE_LENGTH; i++) { snled27351_write_register(addr, i, current_tune_reg_list[i]); } - // Enable LEDs ON/OFF - snled27351_write_register(addr, SNLED27351_REG_COMMAND, SNLED27351_COMMAND_LED_CONTROL); + snled27351_select_page(addr, SNLED27351_COMMAND_LED_CONTROL); + for (int i = 0; i < SNLED27351_LED_CONTROL_ON_OFF_LENGTH; i++) { snled27351_write_register(addr, i, 0xFF); } - // Select to function page - snled27351_write_register(addr, SNLED27351_REG_COMMAND, SNLED27351_COMMAND_FUNCTION); + snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION); + // Setting LED driver to normal mode snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL); } @@ -230,7 +234,7 @@ void snled27351_set_led_control_register(uint8_t index, bool red, bool green, bo void snled27351_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - snled27351_write_register(addr, SNLED27351_REG_COMMAND, SNLED27351_COMMAND_PWM); + snled27351_select_page(addr, SNLED27351_COMMAND_PWM); // If any of the transactions fail we risk writing dirty PG0, // refresh page 0 just in case. @@ -243,7 +247,8 @@ void snled27351_update_pwm_buffers(uint8_t addr, uint8_t index) { void snled27351_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_led_control_registers_update_required[index]) { - snled27351_write_register(addr, SNLED27351_REG_COMMAND, SNLED27351_COMMAND_LED_CONTROL); + snled27351_select_page(addr, SNLED27351_COMMAND_LED_CONTROL); + for (int i = 0; i < SNLED27351_LED_CONTROL_REGISTER_COUNT; i++) { snled27351_write_register(addr, i, g_led_control_registers[index][i]); } @@ -265,15 +270,15 @@ void snled27351_flush(void) { } void snled27351_sw_return_normal(uint8_t addr) { - // Select to function page - snled27351_write_register(addr, SNLED27351_REG_COMMAND, SNLED27351_COMMAND_FUNCTION); + snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION); + // Setting LED driver to normal mode snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL); } void snled27351_sw_shutdown(uint8_t addr) { - // Select to function page - snled27351_write_register(addr, SNLED27351_REG_COMMAND, SNLED27351_COMMAND_FUNCTION); + snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION); + // Setting LED driver to shutdown mode snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN); // Write SW Sleep Register |