diff options
Diffstat (limited to 'drivers/led')
31 files changed, 352 insertions, 391 deletions
diff --git a/drivers/led/apa102.c b/drivers/led/apa102.c index 527519eb8a..4d8f69cdcd 100644 --- a/drivers/led/apa102.c +++ b/drivers/led/apa102.c @@ -71,11 +71,6 @@ void apa102_setleds(rgb_led_t *start_led, uint16_t num_leds) { apa102_end_frame(num_leds); } -// Overwrite the default rgblight_call_driver to use apa102 driver -void rgblight_call_driver(rgb_led_t *start_led, uint8_t num_leds) { - apa102_setleds(start_led, num_leds); -} - void static apa102_init(void) { setPinOutput(APA102_DI_PIN); setPinOutput(APA102_CI_PIN); diff --git a/drivers/led/apa102.h b/drivers/led/apa102.h index cd0a19d445..027221f434 100644 --- a/drivers/led/apa102.h +++ b/drivers/led/apa102.h @@ -19,6 +19,12 @@ #include "color.h" +#if defined(RGBLED_NUM) +# define APA102_LED_COUNT RGBLED_NUM +#elif defined(RGB_MATRIX_LED_COUNT) +# define APA102_LED_COUNT RGB_MATRIX_LED_COUNT +#endif + #ifndef APA102_DEFAULT_BRIGHTNESS # define APA102_DEFAULT_BRIGHTNESS 31 #endif diff --git a/drivers/led/issi/is31fl3218-simple.c b/drivers/led/issi/is31fl3218-simple.c index ce28c51d18..e58b7d8bc0 100644 --- a/drivers/led/issi/is31fl3218-simple.c +++ b/drivers/led/issi/is31fl3218-simple.c @@ -28,8 +28,7 @@ # define IS31FL3218_I2C_PERSISTENCE 0 #endif -// Reusable buffer for transfers -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining. uint8_t g_pwm_buffer[IS31FL3218_PWM_REGISTER_COUNT]; @@ -39,27 +38,27 @@ uint8_t g_led_control_registers[IS31FL3218_LED_CONTROL_REGISTER_COUNT] = {0}; bool g_led_control_registers_update_required = false; void is31fl3218_write_register(uint8_t reg, uint8_t data) { - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if IS31FL3218_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) { - if (i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT); + i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT); #endif } void is31fl3218_write_pwm_buffer(uint8_t *pwm_buffer) { - g_twi_transfer_buffer[0] = IS31FL3218_REG_PWM; - memcpy(g_twi_transfer_buffer + 1, pwm_buffer, 18); + i2c_transfer_buffer[0] = IS31FL3218_REG_PWM; + memcpy(i2c_transfer_buffer + 1, pwm_buffer, 18); #if IS31FL3218_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) { - i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT); + i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT); } #else - i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT); + i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT); #endif } diff --git a/drivers/led/issi/is31fl3218-simple.h b/drivers/led/issi/is31fl3218-simple.h index 9492817809..e0a71f1387 100644 --- a/drivers/led/issi/is31fl3218-simple.h +++ b/drivers/led/issi/is31fl3218-simple.h @@ -43,6 +43,8 @@ extern const is31fl3218_led_t PROGMEM g_is31fl3218_leds[IS31FL3218_LED_COUNT]; void is31fl3218_init(void); +void is31fl3218_write_register(uint8_t reg, uint8_t data); + void is31fl3218_set_value(int index, uint8_t value); void is31fl3218_set_value_all(uint8_t value); diff --git a/drivers/led/issi/is31fl3218.c b/drivers/led/issi/is31fl3218.c index 39db09d518..9bfdc9c44d 100644 --- a/drivers/led/issi/is31fl3218.c +++ b/drivers/led/issi/is31fl3218.c @@ -28,8 +28,7 @@ # define IS31FL3218_I2C_PERSISTENCE 0 #endif -// Reusable buffer for transfers -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining. uint8_t g_pwm_buffer[IS31FL3218_PWM_REGISTER_COUNT]; @@ -39,27 +38,27 @@ uint8_t g_led_control_registers[IS31FL3218_LED_CONTROL_REGISTER_COUNT] = {0}; bool g_led_control_registers_update_required = false; void is31fl3218_write_register(uint8_t reg, uint8_t data) { - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if IS31FL3218_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) { - if (i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT); + i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT); #endif } void is31fl3218_write_pwm_buffer(uint8_t *pwm_buffer) { - g_twi_transfer_buffer[0] = IS31FL3218_REG_PWM; - memcpy(g_twi_transfer_buffer + 1, pwm_buffer, 18); + i2c_transfer_buffer[0] = IS31FL3218_REG_PWM; + memcpy(i2c_transfer_buffer + 1, pwm_buffer, 18); #if IS31FL3218_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) { - i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT); + i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT); } #else - i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT); + i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT); #endif } diff --git a/drivers/led/issi/is31fl3218.h b/drivers/led/issi/is31fl3218.h index ffa7f36d61..13916ffaf1 100644 --- a/drivers/led/issi/is31fl3218.h +++ b/drivers/led/issi/is31fl3218.h @@ -45,6 +45,8 @@ extern const is31fl3218_led_t PROGMEM g_is31fl3218_leds[IS31FL3218_LED_COUNT]; void is31fl3218_init(void); +void is31fl3218_write_register(uint8_t reg, uint8_t data); + void is31fl3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); void is31fl3218_set_color_all(uint8_t red, uint8_t green, uint8_t blue); diff --git a/drivers/led/issi/is31fl3731-simple.c b/drivers/led/issi/is31fl3731-simple.c index 8dbfc3cd31..14743717d4 100644 --- a/drivers/led/issi/is31fl3731-simple.c +++ b/drivers/led/issi/is31fl3731-simple.c @@ -33,8 +33,7 @@ # define IS31FL3731_I2C_PERSISTENCE 0 #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // These buffers match the IS31FL3731 PWM registers 0x24-0xB3. // Storing them like this is optimal for I2C transfers to the registers. @@ -48,41 +47,45 @@ uint8_t g_led_control_registers[IS31FL3731_DRIVER_COUNT][IS31FL3731_LED_CONTROL_ bool g_led_control_registers_update_required[IS31FL3731_DRIVER_COUNT] = {false}; void is31fl3731_write_register(uint8_t addr, uint8_t reg, uint8_t data) { - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if IS31FL3731_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3731_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT) == 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT) == 0) { break; } } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT); #endif } +void is31fl3731_select_page(uint8_t addr, uint8_t page) { + is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, page); +} + void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // assumes bank is already selected + // assumes page 0 is already selected // transmit PWM registers in 9 transfers of 16 bytes - // g_twi_transfer_buffer[] is 20 bytes + // i2c_transfer_buffer[] is 20 bytes // iterate over the pwm_buffer contents at 16 byte intervals for (int i = 0; i < IS31FL3731_PWM_REGISTER_COUNT; i += 16) { // set the first register, e.g. 0x24, 0x34, 0x44, etc. - g_twi_transfer_buffer[0] = 0x24 + i; + i2c_transfer_buffer[0] = 0x24 + i; // copy the data from i to i+15 // device will auto-increment register for data after the first byte // thus this sets registers 0x24-0x33, 0x34-0x43, etc. in one transfer - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); + memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16); #if IS31FL3731_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3731_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT); #endif } } @@ -123,8 +126,7 @@ void is31fl3731_init(uint8_t addr) { // then set up the mode and other settings, clear the PWM registers, // then disable software shutdown. - // select "function register" bank - is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, IS31FL3731_COMMAND_FUNCTION); + is31fl3731_select_page(addr, IS31FL3731_COMMAND_FUNCTION); // enable software shutdown is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x00); @@ -142,8 +144,7 @@ void is31fl3731_init(uint8_t addr) { // audio sync off is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_AUDIO_SYNC, 0x00); - // select bank 0 - is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, IS31FL3731_COMMAND_FRAME_1); + is31fl3731_select_page(addr, IS31FL3731_COMMAND_FRAME_1); // turn off all LEDs in the LED control register for (int i = 0; i < IS31FL3731_LED_CONTROL_REGISTER_COUNT; i++) { @@ -160,16 +161,15 @@ void is31fl3731_init(uint8_t addr) { is31fl3731_write_register(addr, i, 0x00); } - // select "function register" bank - is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, IS31FL3731_COMMAND_FUNCTION); + is31fl3731_select_page(addr, IS31FL3731_COMMAND_FUNCTION); // disable software shutdown is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x01); - // select bank 0 and leave it selected. - // most usage after initialization is just writing PWM buffers in bank 0 + // select page 0 and leave it selected. + // most usage after initialization is just writing PWM buffers in page 0 // as there's not much point in double-buffering - is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, IS31FL3731_COMMAND_FRAME_1); + is31fl3731_select_page(addr, IS31FL3731_COMMAND_FRAME_1); } void is31fl3731_set_value(int index, uint8_t value) { diff --git a/drivers/led/issi/is31fl3731-simple.h b/drivers/led/issi/is31fl3731-simple.h index 4d173847dd..ff970a3e5e 100644 --- a/drivers/led/issi/is31fl3731-simple.h +++ b/drivers/led/issi/is31fl3731-simple.h @@ -103,6 +103,7 @@ extern const is31fl3731_led_t PROGMEM g_is31fl3731_leds[IS31FL3731_LED_COUNT]; void is31fl3731_init_drivers(void); void is31fl3731_init(uint8_t addr); void is31fl3731_write_register(uint8_t addr, uint8_t reg, uint8_t data); +void is31fl3731_select_page(uint8_t addr, uint8_t page); void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); void is31fl3731_set_value(int index, uint8_t value); diff --git a/drivers/led/issi/is31fl3731.c b/drivers/led/issi/is31fl3731.c index 1ab8997731..9a7d0b5eca 100644 --- a/drivers/led/issi/is31fl3731.c +++ b/drivers/led/issi/is31fl3731.c @@ -32,8 +32,7 @@ # define IS31FL3731_I2C_PERSISTENCE 0 #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // These buffers match the IS31FL3731 PWM registers 0x24-0xB3. // Storing them like this is optimal for I2C transfers to the registers. @@ -47,39 +46,43 @@ uint8_t g_led_control_registers[IS31FL3731_DRIVER_COUNT][IS31FL3731_LED_CONTROL_ bool g_led_control_registers_update_required[IS31FL3731_DRIVER_COUNT] = {false}; void is31fl3731_write_register(uint8_t addr, uint8_t reg, uint8_t data) { - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if IS31FL3731_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3731_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT); #endif } +void is31fl3731_select_page(uint8_t addr, uint8_t page) { + is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, page); +} + void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // assumes bank is already selected + // assumes page 0 is already selected // transmit PWM registers in 9 transfers of 16 bytes - // g_twi_transfer_buffer[] is 20 bytes + // i2c_transfer_buffer[] is 20 bytes // iterate over the pwm_buffer contents at 16 byte intervals for (int i = 0; i < IS31FL3731_PWM_REGISTER_COUNT; i += 16) { // set the first register, e.g. 0x24, 0x34, 0x44, etc. - g_twi_transfer_buffer[0] = 0x24 + i; + i2c_transfer_buffer[0] = 0x24 + i; // copy the data from i to i+15 // device will auto-increment register for data after the first byte // thus this sets registers 0x24-0x33, 0x34-0x43, etc. in one transfer - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); + memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16); #if IS31FL3731_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3731_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT); #endif } } @@ -120,8 +123,7 @@ void is31fl3731_init(uint8_t addr) { // then set up the mode and other settings, clear the PWM registers, // then disable software shutdown. - // select "function register" bank - is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, IS31FL3731_COMMAND_FUNCTION); + is31fl3731_select_page(addr, IS31FL3731_COMMAND_FUNCTION); // enable software shutdown is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x00); @@ -139,8 +141,7 @@ void is31fl3731_init(uint8_t addr) { // audio sync off is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_AUDIO_SYNC, 0x00); - // select bank 0 - is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, IS31FL3731_COMMAND_FRAME_1); + is31fl3731_select_page(addr, IS31FL3731_COMMAND_FRAME_1); // turn off all LEDs in the LED control register for (int i = 0; i < IS31FL3731_LED_CONTROL_REGISTER_COUNT; i++) { @@ -157,16 +158,15 @@ void is31fl3731_init(uint8_t addr) { is31fl3731_write_register(addr, i, 0x00); } - // select "function register" bank - is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, IS31FL3731_COMMAND_FUNCTION); + is31fl3731_select_page(addr, IS31FL3731_COMMAND_FUNCTION); // disable software shutdown is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x01); - // select bank 0 and leave it selected. - // most usage after initialization is just writing PWM buffers in bank 0 + // select page 0 and leave it selected. + // most usage after initialization is just writing PWM buffers in page 0 // as there's not much point in double-buffering - is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, IS31FL3731_COMMAND_FRAME_1); + is31fl3731_select_page(addr, IS31FL3731_COMMAND_FRAME_1); } void is31fl3731_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { diff --git a/drivers/led/issi/is31fl3731.h b/drivers/led/issi/is31fl3731.h index b45cb2b07d..f125fbc64f 100644 --- a/drivers/led/issi/is31fl3731.h +++ b/drivers/led/issi/is31fl3731.h @@ -104,6 +104,7 @@ extern const is31fl3731_led_t PROGMEM g_is31fl3731_leds[IS31FL3731_LED_COUNT]; void is31fl3731_init_drivers(void); void is31fl3731_init(uint8_t addr); void is31fl3731_write_register(uint8_t addr, uint8_t reg, uint8_t data); +void is31fl3731_select_page(uint8_t addr, uint8_t page); void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); void is31fl3731_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); diff --git a/drivers/led/issi/is31fl3733-simple.c b/drivers/led/issi/is31fl3733-simple.c index 9f2444c253..805905ebc2 100644 --- a/drivers/led/issi/is31fl3733-simple.c +++ b/drivers/led/issi/is31fl3733-simple.c @@ -63,11 +63,10 @@ # define IS31FL3733_SYNC_4 IS31FL3733_SYNC_NONE #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // These buffers match the IS31FL3733 PWM registers. -// The control buffers match the PG0 LED On/Off registers. +// The control buffers match the page 0 LED On/Off registers. // Storing them like this is optimal for I2C transfers to the registers. // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3733_write_pwm_buffer() but it's @@ -80,45 +79,50 @@ bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] bool is31fl3733_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 IS31FL3733_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) != 0) { return false; } #endif return true; } +void is31fl3733_select_page(uint8_t addr, uint8_t page) { + is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); + is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, page); +} + bool is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // Assumes PG1 is already selected. + // Assumes page 1 is already selected. // If any of the transactions fails function returns false. // Transmit PWM registers in 12 transfers of 16 bytes. - // g_twi_transfer_buffer[] is 20 bytes + // i2c_transfer_buffer[] is 20 bytes // Iterate over the pwm_buffer contents at 16 byte intervals. for (int i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) { - g_twi_transfer_buffer[0] = i; + i2c_transfer_buffer[0] = i; // Copy the data from i to i+15. // Device will auto-increment register for data after the first byte // Thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer. - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); + memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16); #if IS31FL3733_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) != 0) { return false; } #endif @@ -163,32 +167,23 @@ void is31fl3733_init(uint8_t addr, uint8_t sync) { // then disable software shutdown. // Sync is passed so set it according to the datasheet. - // Unlock the command register. - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); + is31fl3733_select_page(addr, IS31FL3733_COMMAND_LED_CONTROL); - // Select PG0 - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, IS31FL3733_COMMAND_LED_CONTROL); // Turn off all LEDs. for (int i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3733_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); + is31fl3733_select_page(addr, IS31FL3733_COMMAND_PWM); - // Select PG1 - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, IS31FL3733_COMMAND_PWM); // Set PWM on all LEDs to 0 // No need to setup Breath registers to PWM as that is the default. for (int i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i++) { is31fl3733_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); + is31fl3733_select_page(addr, IS31FL3733_COMMAND_FUNCTION); - // Select PG3 - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, IS31FL3733_COMMAND_FUNCTION); // Set de-ghost pull-up resistors (SWx) is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_SW_PULLUP, IS31FL3733_SW_PULLUP); // Set de-ghost pull-down resistors (CSx) @@ -239,11 +234,9 @@ void is31fl3733_set_led_control_register(uint8_t index, bool value) { void is31fl3733_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - // Firstly we need to unlock the command register and select PG1. - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, IS31FL3733_COMMAND_PWM); + is31fl3733_select_page(addr, IS31FL3733_COMMAND_PWM); - // If any of the transactions fail we risk writing dirty PG0, + // If any of the transactions fail we risk writing dirty page 0, // refresh page 0 just in case. if (!is31fl3733_write_pwm_buffer(addr, g_pwm_buffer[index])) { g_led_control_registers_update_required[index] = true; @@ -254,9 +247,8 @@ void is31fl3733_update_pwm_buffers(uint8_t addr, uint8_t index) { void is31fl3733_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_led_control_registers_update_required[index]) { - // Firstly we need to unlock the command register and select PG0 - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, IS31FL3733_COMMAND_LED_CONTROL); + is31fl3733_select_page(addr, IS31FL3733_COMMAND_LED_CONTROL); + for (int i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3733_write_register(addr, i, g_led_control_registers[index][i]); } diff --git a/drivers/led/issi/is31fl3733-simple.h b/drivers/led/issi/is31fl3733-simple.h index c37b1fe5f2..dcb33e448f 100644 --- a/drivers/led/issi/is31fl3733-simple.h +++ b/drivers/led/issi/is31fl3733-simple.h @@ -117,6 +117,7 @@ extern const is31fl3733_led_t PROGMEM g_is31fl3733_leds[IS31FL3733_LED_COUNT]; void is31fl3733_init_drivers(void); void is31fl3733_init(uint8_t addr, uint8_t sync); bool is31fl3733_write_register(uint8_t addr, uint8_t reg, uint8_t data); +void is31fl3733_select_page(uint8_t addr, uint8_t page); bool is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); void is31fl3733_set_value(int index, uint8_t value); diff --git a/drivers/led/issi/is31fl3733.c b/drivers/led/issi/is31fl3733.c index 5857a800d7..f7162a0ce5 100644 --- a/drivers/led/issi/is31fl3733.c +++ b/drivers/led/issi/is31fl3733.c @@ -62,11 +62,10 @@ # define IS31FL3733_SYNC_4 IS31FL3733_SYNC_NONE #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // These buffers match the IS31FL3733 PWM registers. -// The control buffers match the PG0 LED On/Off registers. +// The control buffers match the page 0 LED On/Off registers. // Storing them like this is optimal for I2C transfers to the registers. // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3733_write_pwm_buffer() but it's @@ -79,45 +78,50 @@ bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] bool is31fl3733_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 IS31FL3733_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) != 0) { return false; } #endif return true; } +void is31fl3733_select_page(uint8_t addr, uint8_t page) { + is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); + is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, page); +} + bool is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // Assumes PG1 is already selected. + // Assumes page 1 is already selected. // If any of the transactions fails function returns false. // Transmit PWM registers in 12 transfers of 16 bytes. - // g_twi_transfer_buffer[] is 20 bytes + // i2c_transfer_buffer[] is 20 bytes // Iterate over the pwm_buffer contents at 16 byte intervals. for (int i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) { - g_twi_transfer_buffer[0] = i; + i2c_transfer_buffer[0] = i; // Copy the data from i to i+15. // Device will auto-increment register for data after the first byte // Thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer. - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); + memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16); #if IS31FL3733_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) != 0) { return false; } #endif @@ -162,32 +166,23 @@ void is31fl3733_init(uint8_t addr, uint8_t sync) { // then disable software shutdown. // Sync is passed so set it according to the datasheet. - // Unlock the command register. - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); + is31fl3733_select_page(addr, IS31FL3733_COMMAND_LED_CONTROL); - // Select PG0 - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, IS31FL3733_COMMAND_LED_CONTROL); // Turn off all LEDs. for (int i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3733_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); + is31fl3733_select_page(addr, IS31FL3733_COMMAND_PWM); - // Select PG1 - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, IS31FL3733_COMMAND_PWM); // Set PWM on all LEDs to 0 // No need to setup Breath registers to PWM as that is the default. for (int i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i++) { is31fl3733_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); + is31fl3733_select_page(addr, IS31FL3733_COMMAND_FUNCTION); - // Select PG3 - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, IS31FL3733_COMMAND_FUNCTION); // Set de-ghost pull-up resistors (SWx) is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_SW_PULLUP, IS31FL3733_SW_PULLUP); // Set de-ghost pull-down resistors (CSx) @@ -254,11 +249,9 @@ void is31fl3733_set_led_control_register(uint8_t index, bool red, bool green, bo void is31fl3733_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - // Firstly we need to unlock the command register and select PG1. - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, IS31FL3733_COMMAND_PWM); + is31fl3733_select_page(addr, IS31FL3733_COMMAND_PWM); - // If any of the transactions fail we risk writing dirty PG0, + // If any of the transactions fail we risk writing dirty page 0, // refresh page 0 just in case. if (!is31fl3733_write_pwm_buffer(addr, g_pwm_buffer[index])) { g_led_control_registers_update_required[index] = true; @@ -269,9 +262,8 @@ void is31fl3733_update_pwm_buffers(uint8_t addr, uint8_t index) { void is31fl3733_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_led_control_registers_update_required[index]) { - // Firstly we need to unlock the command register and select PG0 - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC); - is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, IS31FL3733_COMMAND_LED_CONTROL); + is31fl3733_select_page(addr, IS31FL3733_COMMAND_LED_CONTROL); + for (int i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3733_write_register(addr, i, g_led_control_registers[index][i]); } diff --git a/drivers/led/issi/is31fl3733.h b/drivers/led/issi/is31fl3733.h index 20804b016b..907b9669c6 100644 --- a/drivers/led/issi/is31fl3733.h +++ b/drivers/led/issi/is31fl3733.h @@ -142,6 +142,7 @@ extern const is31fl3733_led_t PROGMEM g_is31fl3733_leds[IS31FL3733_LED_COUNT]; void is31fl3733_init_drivers(void); void is31fl3733_init(uint8_t addr, uint8_t sync); bool is31fl3733_write_register(uint8_t addr, uint8_t reg, uint8_t data); +void is31fl3733_select_page(uint8_t addr, uint8_t page); bool is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); void is31fl3733_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); diff --git a/drivers/led/issi/is31fl3736-simple.c b/drivers/led/issi/is31fl3736-simple.c index e1cce3c48a..ac5e0fb453 100644 --- a/drivers/led/issi/is31fl3736-simple.c +++ b/drivers/led/issi/is31fl3736-simple.c @@ -47,11 +47,10 @@ # define IS31FL3736_GLOBAL_CURRENT 0xFF #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // These buffers match the IS31FL3736 PWM registers. -// The control buffers match the PG0 LED On/Off registers. +// The control buffers match the page 0 LED On/Off registers. // Storing them like this is optimal for I2C transfers to the registers. // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3736_write_pwm_buffer() but it's @@ -63,38 +62,43 @@ uint8_t g_led_control_registers[IS31FL3736_DRIVER_COUNT][IS31FL3736_LED_CONTROL_ bool g_led_control_registers_update_required[IS31FL3736_DRIVER_COUNT] = {false}; void is31fl3736_write_register(uint8_t addr, uint8_t reg, uint8_t data) { - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if IS31FL3736_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT); #endif } +void is31fl3736_select_page(uint8_t addr, uint8_t page) { + is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); + is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, page); +} + void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // assumes PG1 is already selected + // assumes page 1 is already selected // transmit PWM registers in 12 transfers of 16 bytes - // g_twi_transfer_buffer[] is 20 bytes + // i2c_transfer_buffer[] is 20 bytes // iterate over the pwm_buffer contents at 16 byte intervals for (int i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i += 16) { - g_twi_transfer_buffer[0] = i; + i2c_transfer_buffer[0] = i; // copy the data from i to i+15 // device will auto-increment register for data after the first byte // thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); + memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16); #if IS31FL3736_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT); #endif } } @@ -135,32 +139,23 @@ void is31fl3736_init(uint8_t addr) { // Set up the mode and other settings, clear the PWM registers, // then disable software shutdown. - // Unlock the command register. - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); + is31fl3736_select_page(addr, IS31FL3736_COMMAND_LED_CONTROL); - // Select PG0 - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, IS31FL3736_COMMAND_LED_CONTROL); // Turn off all LEDs. for (int i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3736_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); + is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM); - // Select PG1 - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, IS31FL3736_COMMAND_PWM); // Set PWM on all LEDs to 0 // No need to setup Breath registers to PWM as that is the default. for (int i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i++) { is31fl3736_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); + is31fl3736_select_page(addr, IS31FL3736_COMMAND_FUNCTION); - // Select PG3 - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, IS31FL3736_COMMAND_FUNCTION); // Set de-ghost pull-up resistors (SWx) is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_SW_PULLUP, IS31FL3736_SW_PULLUP); // Set de-ghost pull-down resistors (CSx) @@ -217,9 +212,7 @@ void is31fl3736_set_led_control_register(uint8_t index, bool value) { void is31fl3736_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - // Firstly we need to unlock the command register and select PG1 - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, IS31FL3736_COMMAND_PWM); + is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM); is31fl3736_write_pwm_buffer(addr, g_pwm_buffer[index]); g_pwm_buffer_update_required[index] = false; @@ -228,9 +221,8 @@ void is31fl3736_update_pwm_buffers(uint8_t addr, uint8_t index) { void is31fl3736_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_led_control_registers_update_required[index]) { - // Firstly we need to unlock the command register and select PG0 - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, IS31FL3736_COMMAND_LED_CONTROL); + is31fl3736_select_page(addr, IS31FL3736_COMMAND_LED_CONTROL); + for (int i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3736_write_register(addr, i, g_led_control_registers[index][i]); } diff --git a/drivers/led/issi/is31fl3736-simple.h b/drivers/led/issi/is31fl3736-simple.h index a73a872545..5e80eb646a 100644 --- a/drivers/led/issi/is31fl3736-simple.h +++ b/drivers/led/issi/is31fl3736-simple.h @@ -112,6 +112,7 @@ extern const is31fl3736_led_t PROGMEM g_is31fl3736_leds[IS31FL3736_LED_COUNT]; void is31fl3736_init_drivers(void); void is31fl3736_init(uint8_t addr); void is31fl3736_write_register(uint8_t addr, uint8_t reg, uint8_t data); +void is31fl3736_select_page(uint8_t addr, uint8_t page); void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); void is31fl3736_set_value(int index, uint8_t value); diff --git a/drivers/led/issi/is31fl3736.c b/drivers/led/issi/is31fl3736.c index 30ab796f3e..384b815850 100644 --- a/drivers/led/issi/is31fl3736.c +++ b/drivers/led/issi/is31fl3736.c @@ -47,11 +47,10 @@ # define IS31FL3736_GLOBAL_CURRENT 0xFF #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // These buffers match the IS31FL3736 PWM registers. -// The control buffers match the PG0 LED On/Off registers. +// The control buffers match the page 0 LED On/Off registers. // Storing them like this is optimal for I2C transfers to the registers. // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3736_write_pwm_buffer() but it's @@ -63,38 +62,43 @@ uint8_t g_led_control_registers[IS31FL3736_DRIVER_COUNT][IS31FL3736_LED_CONTROL_ bool g_led_control_registers_update_required[IS31FL3736_DRIVER_COUNT] = {false}; void is31fl3736_write_register(uint8_t addr, uint8_t reg, uint8_t data) { - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if IS31FL3736_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3736_I2C_TIMEOUT); #endif } +void is31fl3736_select_page(uint8_t addr, uint8_t page) { + is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); + is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, page); +} + void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // assumes PG1 is already selected + // assumes page 1 is already selected // transmit PWM registers in 12 transfers of 16 bytes - // g_twi_transfer_buffer[] is 20 bytes + // i2c_transfer_buffer[] is 20 bytes // iterate over the pwm_buffer contents at 16 byte intervals for (int i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i += 16) { - g_twi_transfer_buffer[0] = i; + i2c_transfer_buffer[0] = i; // copy the data from i to i+15 // device will auto-increment register for data after the first byte // thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); + memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16); #if IS31FL3736_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3736_I2C_TIMEOUT); #endif } } @@ -135,32 +139,23 @@ void is31fl3736_init(uint8_t addr) { // Set up the mode and other settings, clear the PWM registers, // then disable software shutdown. - // Unlock the command register. - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); + is31fl3736_select_page(addr, IS31FL3736_REG_LED_CONTROL); - // Select PG0 - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, IS31FL3736_REG_LED_CONTROL); // Turn off all LEDs. for (int i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3736_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITELOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); + is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM); - // Select PG1 - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, IS31FL3736_COMMAND_PWM); // Set PWM on all LEDs to 0 // No need to setup Breath registers to PWM as that is the default. for (int i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i++) { is31fl3736_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); + is31fl3736_select_page(addr, IS31FL3736_COMMAND_FUNCTION); - // Select PG3 - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, IS31FL3736_COMMAND_FUNCTION); // Set de-ghost pull-up resistors (SWx) is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_SW_PULLUP, IS31FL3736_SW_PULLUP); // Set de-ghost pull-down resistors (CSx) @@ -234,9 +229,7 @@ void is31fl3736_set_led_control_register(uint8_t index, bool red, bool green, bo void is31fl3736_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - // Firstly we need to unlock the command register and select PG1 - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, IS31FL3736_COMMAND_PWM); + is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM); is31fl3736_write_pwm_buffer(addr, g_pwm_buffer[index]); g_pwm_buffer_update_required[index] = false; @@ -245,9 +238,8 @@ void is31fl3736_update_pwm_buffers(uint8_t addr, uint8_t index) { void is31fl3736_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_led_control_registers_update_required[index]) { - // Firstly we need to unlock the command register and select PG0 - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC); - is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, IS31FL3736_COMMAND_LED_CONTROL); + is31fl3736_select_page(addr, IS31FL3736_COMMAND_LED_CONTROL); + for (int i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3736_write_register(addr, i, g_led_control_registers[index][i]); } diff --git a/drivers/led/issi/is31fl3736.h b/drivers/led/issi/is31fl3736.h index a5710d7ed4..5f0b11e46a 100644 --- a/drivers/led/issi/is31fl3736.h +++ b/drivers/led/issi/is31fl3736.h @@ -126,6 +126,7 @@ extern const is31fl3736_led_t PROGMEM g_is31fl3736_leds[IS31FL3736_LED_COUNT]; void is31fl3736_init_drivers(void); void is31fl3736_init(uint8_t addr); void is31fl3736_write_register(uint8_t addr, uint8_t reg, uint8_t data); +void is31fl3736_select_page(uint8_t addr, uint8_t page); void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); void is31fl3736_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); diff --git a/drivers/led/issi/is31fl3737-simple.c b/drivers/led/issi/is31fl3737-simple.c index 7f641f4ca5..967a102b48 100644 --- a/drivers/led/issi/is31fl3737-simple.c +++ b/drivers/led/issi/is31fl3737-simple.c @@ -49,11 +49,10 @@ # define IS31FL3737_GLOBAL_CURRENT 0xFF #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // These buffers match the IS31FL3737 PWM registers. -// The control buffers match the PG0 LED On/Off registers. +// The control buffers match the page 0 LED On/Off registers. // Storing them like this is optimal for I2C transfers to the registers. // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3737_write_pwm_buffer() but it's @@ -66,38 +65,43 @@ uint8_t g_led_control_registers[IS31FL3737_DRIVER_COUNT][IS31FL3737_LED_CONTROL_ bool g_led_control_registers_update_required[IS31FL3737_DRIVER_COUNT] = {false}; void is31fl3737_write_register(uint8_t addr, uint8_t reg, uint8_t data) { - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if IS31FL3737_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3737_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3737_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3737_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3737_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3737_I2C_TIMEOUT); #endif } +void is31fl3737_select_page(uint8_t addr, uint8_t page) { + is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); + is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, page); +} + void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // assumes PG1 is already selected + // assumes page 1 is already selected // transmit PWM registers in 12 transfers of 16 bytes - // g_twi_transfer_buffer[] is 20 bytes + // i2c_transfer_buffer[] is 20 bytes // iterate over the pwm_buffer contents at 16 byte intervals for (int i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i += 16) { - g_twi_transfer_buffer[0] = i; + i2c_transfer_buffer[0] = i; // copy the data from i to i+15 // device will auto-increment register for data after the first byte // thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); + memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16); #if IS31FL3737_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3737_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3737_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3737_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3737_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3737_I2C_TIMEOUT); #endif } } @@ -138,32 +142,23 @@ void is31fl3737_init(uint8_t addr) { // Set up the mode and other settings, clear the PWM registers, // then disable software shutdown. - // Unlock the command register. - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); + is31fl3737_select_page(addr, IS31FL3737_COMMAND_LED_CONTROL); - // Select PG0 - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, IS31FL3737_COMMAND_LED_CONTROL); // Turn off all LEDs. for (int i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3737_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); + is31fl3737_select_page(addr, IS31FL3737_COMMAND_PWM); - // Select PG1 - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, IS31FL3737_COMMAND_PWM); // Set PWM on all LEDs to 0 // No need to setup Breath registers to PWM as that is the default. for (int i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i++) { is31fl3737_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); + is31fl3737_select_page(addr, IS31FL3737_COMMAND_FUNCTION); - // Select PG3 - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, IS31FL3737_COMMAND_FUNCTION); // Set de-ghost pull-up resistors (SWx) is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_SW_PULLUP, IS31FL3737_SW_PULLUP); // Set de-ghost pull-down resistors (CSx) @@ -214,9 +209,7 @@ void is31fl3737_set_led_control_register(uint8_t index, bool value) { void is31fl3737_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - // Firstly we need to unlock the command register and select PG1 - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, IS31FL3737_COMMAND_PWM); + is31fl3737_select_page(addr, IS31FL3737_COMMAND_PWM); is31fl3737_write_pwm_buffer(addr, g_pwm_buffer[index]); g_pwm_buffer_update_required[index] = false; @@ -225,9 +218,8 @@ void is31fl3737_update_pwm_buffers(uint8_t addr, uint8_t index) { void is31fl3737_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_led_control_registers_update_required[index]) { - // Firstly we need to unlock the command register and select PG0 - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, IS31FL3737_COMMAND_LED_CONTROL); + is31fl3737_select_page(addr, IS31FL3737_COMMAND_LED_CONTROL); + for (int i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3737_write_register(addr, i, g_led_control_registers[index][i]); } diff --git a/drivers/led/issi/is31fl3737-simple.h b/drivers/led/issi/is31fl3737-simple.h index 2658702b1b..a11d2ef423 100644 --- a/drivers/led/issi/is31fl3737-simple.h +++ b/drivers/led/issi/is31fl3737-simple.h @@ -102,6 +102,7 @@ extern const is31fl3737_led_t PROGMEM g_is31fl3737_leds[IS31FL3737_LED_COUNT]; void is31fl3737_init_drivers(void); void is31fl3737_init(uint8_t addr); void is31fl3737_write_register(uint8_t addr, uint8_t reg, uint8_t data); +void is31fl3737_select_page(uint8_t addr, uint8_t page); void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); void is31fl3737_set_value(int index, uint8_t value); diff --git a/drivers/led/issi/is31fl3737.c b/drivers/led/issi/is31fl3737.c index a458431952..76d17c2b84 100644 --- a/drivers/led/issi/is31fl3737.c +++ b/drivers/led/issi/is31fl3737.c @@ -49,11 +49,10 @@ # define IS31FL3737_GLOBAL_CURRENT 0xFF #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // These buffers match the IS31FL3737 PWM registers. -// The control buffers match the PG0 LED On/Off registers. +// The control buffers match the page 0 LED On/Off registers. // Storing them like this is optimal for I2C transfers to the registers. // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3737_write_pwm_buffer() but it's @@ -66,38 +65,43 @@ uint8_t g_led_control_registers[IS31FL3737_DRIVER_COUNT][IS31FL3737_LED_CONTROL_ bool g_led_control_registers_update_required[IS31FL3737_DRIVER_COUNT] = {false}; void is31fl3737_write_register(uint8_t addr, uint8_t reg, uint8_t data) { - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if IS31FL3737_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3737_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3737_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3737_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3737_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3737_I2C_TIMEOUT); #endif } +void is31fl3737_select_page(uint8_t addr, uint8_t page) { + is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); + is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, page); +} + void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // assumes PG1 is already selected + // assumes page 1 is already selected // transmit PWM registers in 12 transfers of 16 bytes - // g_twi_transfer_buffer[] is 20 bytes + // i2c_transfer_buffer[] is 20 bytes // iterate over the pwm_buffer contents at 16 byte intervals for (int i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i += 16) { - g_twi_transfer_buffer[0] = i; + i2c_transfer_buffer[0] = i; // copy the data from i to i+15 // device will auto-increment register for data after the first byte // thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 16); + memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 16); #if IS31FL3737_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3737_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3737_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3737_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, IS31FL3737_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3737_I2C_TIMEOUT); #endif } } @@ -138,32 +142,23 @@ void is31fl3737_init(uint8_t addr) { // Set up the mode and other settings, clear the PWM registers, // then disable software shutdown. - // Unlock the command register. - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); + is31fl3737_select_page(addr, IS31FL3737_COMMAND_LED_CONTROL); - // Select PG0 - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, IS31FL3737_COMMAND_LED_CONTROL); // Turn off all LEDs. for (int i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3737_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); + is31fl3737_select_page(addr, IS31FL3737_COMMAND_PWM); - // Select PG1 - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, IS31FL3737_COMMAND_PWM); // Set PWM on all LEDs to 0 // No need to setup Breath registers to PWM as that is the default. for (int i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i++) { is31fl3737_write_register(addr, i, 0x00); } - // Unlock the command register. - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); + is31fl3737_select_page(addr, IS31FL3737_COMMAND_FUNCTION); - // Select PG3 - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, IS31FL3737_COMMAND_FUNCTION); // Set de-ghost pull-up resistors (SWx) is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_SW_PULLUP, IS31FL3737_SW_PULLUP); // Set de-ghost pull-down resistors (CSx) @@ -230,9 +225,7 @@ void is31fl3737_set_led_control_register(uint8_t index, bool red, bool green, bo void is31fl3737_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - // Firstly we need to unlock the command register and select PG1 - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, IS31FL3737_COMMAND_PWM); + is31fl3737_select_page(addr, IS31FL3737_COMMAND_PWM); is31fl3737_write_pwm_buffer(addr, g_pwm_buffer[index]); g_pwm_buffer_update_required[index] = false; @@ -241,9 +234,8 @@ void is31fl3737_update_pwm_buffers(uint8_t addr, uint8_t index) { void is31fl3737_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_led_control_registers_update_required[index]) { - // Firstly we need to unlock the command register and select PG0 - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC); - is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, IS31FL3737_COMMAND_LED_CONTROL); + is31fl3737_select_page(addr, IS31FL3737_COMMAND_LED_CONTROL); + for (int i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) { is31fl3737_write_register(addr, i, g_led_control_registers[index][i]); } diff --git a/drivers/led/issi/is31fl3737.h b/drivers/led/issi/is31fl3737.h index 8de3bf4ef5..a707808b51 100644 --- a/drivers/led/issi/is31fl3737.h +++ b/drivers/led/issi/is31fl3737.h @@ -119,6 +119,7 @@ extern const is31fl3737_led_t PROGMEM g_is31fl3737_leds[IS31FL3737_LED_COUNT]; void is31fl3737_init_drivers(void); void is31fl3737_init(uint8_t addr); void is31fl3737_write_register(uint8_t addr, uint8_t reg, uint8_t data); +void is31fl3737_select_page(uint8_t addr, uint8_t page); void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); void is31fl3737_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); diff --git a/drivers/led/issi/is31fl3741-simple.c b/drivers/led/issi/is31fl3741-simple.c index f7009853ba..db245af4da 100644 --- a/drivers/led/issi/is31fl3741-simple.c +++ b/drivers/led/issi/is31fl3741-simple.c @@ -52,11 +52,10 @@ # define IS31FL3741_GLOBAL_CURRENT 0xFF #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20] = {0xFF}; +uint8_t i2c_transfer_buffer[20] = {0xFF}; // These buffers match the IS31FL3741 and IS31FL3741A PWM registers. -// The scaling buffers match the PG2 and PG3 LED On/Off registers. +// The scaling buffers match the page 2 and 3 LED On/Off registers. // Storing them like this is optimal for I2C transfers to the registers. // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3741_write_pwm_buffer() but it's @@ -68,56 +67,59 @@ bool g_scaling_registers_update_required[IS31FL3741_DRIVER_COUNT] = {false}; uint8_t g_scaling_registers[IS31FL3741_DRIVER_COUNT][IS31FL3741_PWM_REGISTER_COUNT]; void is31fl3741_write_register(uint8_t addr, uint8_t reg, uint8_t data) { - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if IS31FL3741_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3741_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3741_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3741_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3741_I2C_TIMEOUT); #endif } +void is31fl3741_select_page(uint8_t addr, uint8_t page) { + is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); + is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, page); +} + bool is31fl3741_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // Assume PG0 is already selected + // Assume page 0 is already selected for (int i = 0; i < 342; i += 18) { if (i == 180) { - // unlock the command register and select PG1 - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, IS31FL3741_COMMAND_PWM_1); + is31fl3741_select_page(addr, IS31FL3741_COMMAND_PWM_1); } - g_twi_transfer_buffer[0] = i % 180; - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 18); + i2c_transfer_buffer[0] = i % 180; + memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 18); #if IS31FL3741_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 19, IS31FL3741_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 19, IS31FL3741_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 19, IS31FL3741_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 19, IS31FL3741_I2C_TIMEOUT) != 0) { return false; } #endif } // transfer the left cause the total number is 351 - g_twi_transfer_buffer[0] = 162; - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + 342, 9); + i2c_transfer_buffer[0] = 162; + memcpy(i2c_transfer_buffer + 1, pwm_buffer + 342, 9); #if IS31FL3741_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 10, IS31FL3741_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 10, IS31FL3741_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 10, IS31FL3741_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 10, IS31FL3741_I2C_TIMEOUT) != 0) { return false; } #endif @@ -162,11 +164,7 @@ void is31fl3741_init(uint8_t addr) { // then disable software shutdown. // Unlock the command register. - // Unlock the command register. - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); - - // Select PG4 - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, IS31FL3741_COMMAND_FUNCTION); + is31fl3741_select_page(addr, IS31FL3741_COMMAND_FUNCTION); // Set to Normal operation is31fl3741_write_register(addr, IS31FL3741_FUNCTION_REG_CONFIGURATION, IS31FL3741_CONFIGURATION); @@ -218,9 +216,7 @@ void is31fl3741_set_led_control_register(uint8_t index, bool value) { void is31fl3741_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - // unlock the command register and select PG2 - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, IS31FL3741_COMMAND_PWM_0); + is31fl3741_select_page(addr, IS31FL3741_COMMAND_PWM_0); is31fl3741_write_pwm_buffer(addr, g_pwm_buffer[index]); } @@ -236,20 +232,16 @@ void is31fl3741_set_pwm_buffer(const is31fl3741_led_t *pled, uint8_t value) { void is31fl3741_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_scaling_registers_update_required[index]) { - // unlock the command register and select PG2 - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, IS31FL3741_COMMAND_SCALING_0); + is31fl3741_select_page(addr, IS31FL3741_COMMAND_SCALING_0); - // CS1_SW1 to CS30_SW6 are on PG2 + // CS1_SW1 to CS30_SW6 are on page 2 for (int i = CS1_SW1; i <= CS30_SW6; ++i) { is31fl3741_write_register(addr, i, g_scaling_registers[index][i]); } - // unlock the command register and select PG3 - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, IS31FL3741_COMMAND_SCALING_1); + is31fl3741_select_page(addr, IS31FL3741_COMMAND_SCALING_1); - // CS1_SW7 to CS39_SW9 are on PG3 + // CS1_SW7 to CS39_SW9 are on page 3 for (int i = CS1_SW7; i <= CS39_SW9; ++i) { is31fl3741_write_register(addr, i - CS1_SW7, g_scaling_registers[index][i]); } diff --git a/drivers/led/issi/is31fl3741-simple.h b/drivers/led/issi/is31fl3741-simple.h index 34608a37e0..462543a5bb 100644 --- a/drivers/led/issi/is31fl3741-simple.h +++ b/drivers/led/issi/is31fl3741-simple.h @@ -104,6 +104,7 @@ extern const is31fl3741_led_t PROGMEM g_is31fl3741_leds[IS31FL3741_LED_COUNT]; void is31fl3741_init_drivers(void); void is31fl3741_init(uint8_t addr); void is31fl3741_write_register(uint8_t addr, uint8_t reg, uint8_t data); +void is31fl3741_select_page(uint8_t addr, uint8_t page); bool is31fl3741_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); void is31fl3741_set_value(int index, uint8_t value); diff --git a/drivers/led/issi/is31fl3741.c b/drivers/led/issi/is31fl3741.c index efcfa77b46..0575f31254 100644 --- a/drivers/led/issi/is31fl3741.c +++ b/drivers/led/issi/is31fl3741.c @@ -52,11 +52,10 @@ # define IS31FL3741_GLOBAL_CURRENT 0xFF #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20] = {0xFF}; +uint8_t i2c_transfer_buffer[20] = {0xFF}; // These buffers match the IS31FL3741 and IS31FL3741A PWM registers. -// The scaling buffers match the PG2 and PG3 LED On/Off registers. +// The scaling buffers match the page 2 and 3 LED On/Off registers. // Storing them like this is optimal for I2C transfers to the registers. // We could optimize this and take out the unused registers from these // buffers and the transfers in is31fl3741_write_pwm_buffer() but it's @@ -68,56 +67,59 @@ bool g_scaling_registers_update_required[IS31FL3741_DRIVER_COUNT] = {false}; uint8_t g_scaling_registers[IS31FL3741_DRIVER_COUNT][IS31FL3741_PWM_REGISTER_COUNT]; void is31fl3741_write_register(uint8_t addr, uint8_t reg, uint8_t data) { - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if IS31FL3741_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3741_I2C_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3741_I2C_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, IS31FL3741_I2C_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3741_I2C_TIMEOUT); #endif } +void is31fl3741_select_page(uint8_t addr, uint8_t page) { + is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); + is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, page); +} + bool is31fl3741_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { - // Assume PG0 is already selected + // Assume page 0 is already selected for (int i = 0; i < 342; i += 18) { if (i == 180) { - // unlock the command register and select PG1 - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, IS31FL3741_COMMAND_PWM_1); + is31fl3741_select_page(addr, IS31FL3741_COMMAND_PWM_1); } - g_twi_transfer_buffer[0] = i % 180; - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + i, 18); + i2c_transfer_buffer[0] = i % 180; + memcpy(i2c_transfer_buffer + 1, pwm_buffer + i, 18); #if IS31FL3741_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 19, IS31FL3741_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 19, IS31FL3741_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 19, IS31FL3741_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 19, IS31FL3741_I2C_TIMEOUT) != 0) { return false; } #endif } // transfer the left cause the total number is 351 - g_twi_transfer_buffer[0] = 162; - memcpy(g_twi_transfer_buffer + 1, pwm_buffer + 342, 9); + i2c_transfer_buffer[0] = 162; + memcpy(i2c_transfer_buffer + 1, pwm_buffer + 342, 9); #if IS31FL3741_I2C_PERSISTENCE > 0 for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 10, IS31FL3741_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 10, IS31FL3741_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 10, IS31FL3741_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 10, IS31FL3741_I2C_TIMEOUT) != 0) { return false; } #endif @@ -162,11 +164,7 @@ void is31fl3741_init(uint8_t addr) { // then disable software shutdown. // Unlock the command register. - // Unlock the command register. - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); - - // Select PG4 - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, IS31FL3741_COMMAND_FUNCTION); + is31fl3741_select_page(addr, IS31FL3741_COMMAND_FUNCTION); // Set to Normal operation is31fl3741_write_register(addr, IS31FL3741_FUNCTION_REG_CONFIGURATION, IS31FL3741_CONFIGURATION); @@ -232,9 +230,7 @@ void is31fl3741_set_led_control_register(uint8_t index, bool red, bool green, bo void is31fl3741_update_pwm_buffers(uint8_t addr, uint8_t index) { if (g_pwm_buffer_update_required[index]) { - // unlock the command register and select PG2 - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, IS31FL3741_COMMAND_PWM_0); + is31fl3741_select_page(addr, IS31FL3741_COMMAND_PWM_0); is31fl3741_write_pwm_buffer(addr, g_pwm_buffer[index]); } @@ -252,20 +248,16 @@ void is31fl3741_set_pwm_buffer(const is31fl3741_led_t *pled, uint8_t red, uint8_ void is31fl3741_update_led_control_registers(uint8_t addr, uint8_t index) { if (g_scaling_registers_update_required[index]) { - // unlock the command register and select PG2 - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, IS31FL3741_COMMAND_SCALING_0); + is31fl3741_select_page(addr, IS31FL3741_COMMAND_SCALING_0); - // CS1_SW1 to CS30_SW6 are on PG2 + // CS1_SW1 to CS30_SW6 are on page 2 for (int i = CS1_SW1; i <= CS30_SW6; ++i) { is31fl3741_write_register(addr, i, g_scaling_registers[index][i]); } - // unlock the command register and select PG3 - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC); - is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, IS31FL3741_COMMAND_SCALING_1); + is31fl3741_select_page(addr, IS31FL3741_COMMAND_SCALING_1); - // CS1_SW7 to CS39_SW9 are on PG3 + // CS1_SW7 to CS39_SW9 are on page 3 for (int i = CS1_SW7; i <= CS39_SW9; ++i) { is31fl3741_write_register(addr, i - CS1_SW7, g_scaling_registers[index][i]); } diff --git a/drivers/led/issi/is31fl3741.h b/drivers/led/issi/is31fl3741.h index 6466696b60..5151d81c37 100644 --- a/drivers/led/issi/is31fl3741.h +++ b/drivers/led/issi/is31fl3741.h @@ -121,6 +121,7 @@ extern const is31fl3741_led_t PROGMEM g_is31fl3741_leds[IS31FL3741_LED_COUNT]; void is31fl3741_init_drivers(void); void is31fl3741_init(uint8_t addr); void is31fl3741_write_register(uint8_t addr, uint8_t reg, uint8_t data); +void is31fl3741_select_page(uint8_t addr, uint8_t page); bool is31fl3741_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); void is31fl3741_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); diff --git a/drivers/led/issi/is31flcommon.c b/drivers/led/issi/is31flcommon.c index d6b9bce93d..048db286b6 100644 --- a/drivers/led/issi/is31flcommon.c +++ b/drivers/led/issi/is31flcommon.c @@ -31,8 +31,7 @@ # define ISSI_PERSISTENCE 0 #endif -// Transfer buffer for TWITransmitData() -uint8_t g_twi_transfer_buffer[20]; +uint8_t i2c_transfer_buffer[20]; // These buffers match the PWM & scaling registers. // Storing them like this is optimal for I2C transfers to the registers. @@ -45,15 +44,15 @@ bool g_scaling_buffer_update_required[DRIVER_COUNT] = {false}; // For writing of single register entry void IS31FL_write_single_register(uint8_t addr, uint8_t reg, uint8_t data) { // Set register address and register data ready to write - g_twi_transfer_buffer[0] = reg; - g_twi_transfer_buffer[1] = data; + i2c_transfer_buffer[0] = reg; + i2c_transfer_buffer[1] = data; #if ISSI_PERSISTENCE > 0 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, ISSI_TIMEOUT) == 0) break; + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 2, ISSI_TIMEOUT) == 0) break; } #else - i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, ISSI_TIMEOUT); + i2c_transmit(addr << 1, i2c_transfer_buffer, 2, ISSI_TIMEOUT); #endif } @@ -64,18 +63,18 @@ bool IS31FL_write_multi_registers(uint8_t addr, uint8_t *source_buffer, uint8_t // Split the buffer into chunks to transfer for (int i = 0; i < buffer_size; i += transfer_size) { // Set the first entry of transfer buffer to the first register we want to write - g_twi_transfer_buffer[0] = i + start_reg_addr; + i2c_transfer_buffer[0] = i + start_reg_addr; // Copy the section of our source buffer into the transfer buffer after first register address - memcpy(g_twi_transfer_buffer + 1, source_buffer + i, transfer_size); + memcpy(i2c_transfer_buffer + 1, source_buffer + i, transfer_size); #if ISSI_PERSISTENCE > 0 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) { - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, transfer_size + 1, ISSI_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, transfer_size + 1, ISSI_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, transfer_size + 1, ISSI_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, transfer_size + 1, ISSI_TIMEOUT) != 0) { return false; } #endif diff --git a/drivers/led/snled27351-simple.c b/drivers/led/snled27351-simple.c index b2054c96d5..7b7a82f90f 100644 --- a/drivers/led/snled27351-simple.c +++ b/drivers/led/snled27351-simple.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[20]; +uint8_t i2c_transfer_buffer[20]; // These buffers match the SNLED27351 PWM registers. // The control buffers match the PG0 LED On/Off registers. @@ -54,47 +53,51 @@ 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. // Transmit PWM registers in 12 transfers of 16 bytes. - // g_twi_transfer_buffer[] is 20 bytes + // i2c_transfer_buffer[] is 20 bytes // Iterate over the pwm_buffer contents at 16 byte intervals. for (int i = 0; i < SNLED27351_PWM_REGISTER_COUNT; i += 16) { - g_twi_transfer_buffer[0] = i; + i2c_transfer_buffer[0] = i; // Copy the data from i to i+15. // Device will auto-increment register for data after the first byte // Thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer. for (int j = 0; j < 16; 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, 17, SNLED27351_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, SNLED27351_I2C_TIMEOUT) != 0) { return false; } } #else - if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, SNLED27351_I2C_TIMEOUT) != 0) { + if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, SNLED27351_I2C_TIMEOUT) != 0) { return false; } #endif @@ -133,8 +136,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 @@ -147,33 +150,35 @@ 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]); } + snled27351_select_page(addr, SNLED27351_COMMAND_LED_CONTROL); + // Enable LEDs ON/OFF - snled27351_write_register(addr, SNLED27351_REG_COMMAND, 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); } @@ -215,7 +220,7 @@ void snled27351_set_led_control_register(uint8_t index, bool value) { 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. @@ -228,7 +233,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]); } @@ -250,15 +256,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 diff --git a/drivers/led/snled27351-simple.h b/drivers/led/snled27351-simple.h index 2fc62a6f0a..0a4d2469f0 100644 --- a/drivers/led/snled27351-simple.h +++ b/drivers/led/snled27351-simple.h @@ -155,6 +155,7 @@ extern const snled27351_led_t PROGMEM g_snled27351_leds[SNLED27351_LED_COUNT]; void snled27351_init_drivers(void); void snled27351_init(uint8_t addr); +void snled27351_select_page(uint8_t addr, uint8_t page); bool snled27351_write_register(uint8_t addr, uint8_t reg, uint8_t data); bool snled27351_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); 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 diff --git a/drivers/led/snled27351.h b/drivers/led/snled27351.h index 77337f177b..8260df1ce1 100644 --- a/drivers/led/snled27351.h +++ b/drivers/led/snled27351.h @@ -169,6 +169,7 @@ extern const snled27351_led_t PROGMEM g_snled27351_leds[SNLED27351_LED_COUNT]; void snled27351_init_drivers(void); void snled27351_init(uint8_t addr); +void snled27351_select_page(uint8_t addr, uint8_t page); bool snled27351_write_register(uint8_t addr, uint8_t reg, uint8_t data); bool snled27351_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); |