Skip to content

Commit

Permalink
LED drivers: switch to i2c_writeReg() (#22878)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored Jan 10, 2024
1 parent 7a3183b commit 58696a3
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 477 deletions.
16 changes: 4 additions & 12 deletions drivers/led/issi/is31fl3218-mono.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

#include "is31fl3218-mono.h"
#include <string.h>
#include "i2c_master.h"

#define IS31FL3218_PWM_REGISTER_COUNT 18
Expand All @@ -29,8 +28,6 @@
# define IS31FL3218_I2C_PERSISTENCE 0
#endif

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];
bool g_pwm_buffer_update_required = false;
Expand All @@ -39,27 +36,22 @@ 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) {
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, i2c_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT) == 0) break;
if (i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, reg, &data, 1, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT);
i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, reg, &data, 1, IS31FL3218_I2C_TIMEOUT);
#endif
}

void is31fl3218_write_pwm_buffer(uint8_t *pwm_buffer) {
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, i2c_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
if (i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT);
#endif
}

Expand Down
16 changes: 4 additions & 12 deletions drivers/led/issi/is31fl3218.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

#include "is31fl3218.h"
#include <string.h>
#include "i2c_master.h"

#define IS31FL3218_PWM_REGISTER_COUNT 18
Expand All @@ -29,8 +28,6 @@
# define IS31FL3218_I2C_PERSISTENCE 0
#endif

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];
bool g_pwm_buffer_update_required = false;
Expand All @@ -39,27 +36,22 @@ 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) {
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, i2c_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT) == 0) break;
if (i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, reg, &data, 1, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT);
i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, reg, &data, 1, IS31FL3218_I2C_TIMEOUT);
#endif
}

void is31fl3218_write_pwm_buffer(uint8_t *pwm_buffer) {
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, i2c_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
if (i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, i2c_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
i2c_writeReg(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT);
#endif
}

Expand Down
33 changes: 9 additions & 24 deletions drivers/led/issi/is31fl3731-mono.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

#include "is31fl3731-mono.h"
#include <string.h>
#include "i2c_master.h"
#include "wait.h"

Expand All @@ -33,8 +32,6 @@
# define IS31FL3731_I2C_PERSISTENCE 0
#endif

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.
// We could optimize this and take out the unused registers from these
Expand All @@ -47,15 +44,12 @@ 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) {
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, i2c_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT) == 0) break;
if (i2c_writeReg(addr << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT);
i2c_writeReg(addr << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT);
#endif
}

Expand All @@ -64,26 +58,17 @@ void is31fl3731_select_page(uint8_t addr, uint8_t page) {
}

void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) {
// assumes page 0 is already selected

// transmit PWM registers in 9 transfers of 16 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.
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(i2c_transfer_buffer + 1, pwm_buffer + i, 16);
// Assumes page 0 is already selected.
// Transmit PWM registers in 9 transfers of 16 bytes.

// Iterate over the pwm_buffer contents at 16 byte intervals.
for (uint8_t i = 0; i < IS31FL3731_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3731_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3731_I2C_PERSISTENCE; i++) {
if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT) == 0) break;
for (uint8_t j = 0; j < IS31FL3731_I2C_PERSISTENCE; j++) {
if (i2c_writeReg(addr << 1, 0x24 + i, pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT);
i2c_writeReg(addr << 1, 0x24 + i, pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT);
#endif
}
}
Expand Down
33 changes: 9 additions & 24 deletions drivers/led/issi/is31fl3731.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

#include "is31fl3731.h"
#include <string.h>
#include "i2c_master.h"
#include "wait.h"

Expand All @@ -32,8 +31,6 @@
# define IS31FL3731_I2C_PERSISTENCE 0
#endif

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.
// We could optimize this and take out the unused registers from these
Expand All @@ -46,15 +43,12 @@ 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) {
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, i2c_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT) == 0) break;
if (i2c_writeReg(addr << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3731_I2C_TIMEOUT);
i2c_writeReg(addr << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT);
#endif
}

Expand All @@ -63,26 +57,17 @@ void is31fl3731_select_page(uint8_t addr, uint8_t page) {
}

void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) {
// assumes page 0 is already selected

// transmit PWM registers in 9 transfers of 16 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.
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(i2c_transfer_buffer + 1, pwm_buffer + i, 16);
// Assumes page 0 is already selected.
// Transmit PWM registers in 9 transfers of 16 bytes.

// Iterate over the pwm_buffer contents at 16 byte intervals.
for (uint8_t i = 0; i < IS31FL3731_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3731_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3731_I2C_PERSISTENCE; i++) {
if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT) == 0) break;
for (uint8_t j = 0; j < IS31FL3731_I2C_PERSISTENCE; j++) {
if (i2c_writeReg(addr << 1, 0x24 + i, pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3731_I2C_TIMEOUT);
i2c_writeReg(addr << 1, 0x24 + i, pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT);
#endif
}
}
Expand Down
25 changes: 6 additions & 19 deletions drivers/led/issi/is31fl3733-mono.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

#include "is31fl3733-mono.h"
#include <string.h>
#include "i2c_master.h"
#include "wait.h"

Expand Down Expand Up @@ -63,8 +62,6 @@
# define IS31FL3733_SYNC_4 IS31FL3733_SYNC_NONE
#endif

uint8_t i2c_transfer_buffer[20];

// These buffers match the IS31FL3733 PWM registers.
// The control buffers match the page 0 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
Expand All @@ -78,15 +75,12 @@ uint8_t g_led_control_registers[IS31FL3733_DRIVER_COUNT][IS31FL3733_LED_CONTROL_
bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] = {false};

void is31fl3733_write_register(uint8_t addr, uint8_t reg, uint8_t 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, i2c_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) == 0) break;
if (i2c_writeReg(addr << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT);
i2c_writeReg(addr << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT);
#endif
}

Expand All @@ -98,22 +92,15 @@ void is31fl3733_select_page(uint8_t addr, uint8_t page) {
void is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) {
// Assumes page 1 is already selected.
// Transmit PWM registers in 12 transfers of 16 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) {
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(i2c_transfer_buffer + 1, pwm_buffer + i, 16);

for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3733_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) {
if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) == 0) break;
for (uint8_t j = 0; j < IS31FL3733_I2C_PERSISTENCE; j++) {
if (i2c_writeReg(addr << 1, i, pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT);
i2c_writeReg(addr << 1, i, pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT);
#endif
}
}
Expand Down
25 changes: 6 additions & 19 deletions drivers/led/issi/is31fl3733.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

#include "is31fl3733.h"
#include <string.h>
#include "i2c_master.h"
#include "wait.h"

Expand Down Expand Up @@ -62,8 +61,6 @@
# define IS31FL3733_SYNC_4 IS31FL3733_SYNC_NONE
#endif

uint8_t i2c_transfer_buffer[20];

// These buffers match the IS31FL3733 PWM registers.
// The control buffers match the page 0 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
Expand All @@ -77,15 +74,12 @@ uint8_t g_led_control_registers[IS31FL3733_DRIVER_COUNT][IS31FL3733_LED_CONTROL_
bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] = {false};

void is31fl3733_write_register(uint8_t addr, uint8_t reg, uint8_t 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, i2c_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT) == 0) break;
if (i2c_writeReg(addr << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(addr << 1, i2c_transfer_buffer, 2, IS31FL3733_I2C_TIMEOUT);
i2c_writeReg(addr << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT);
#endif
}

Expand All @@ -97,22 +91,15 @@ void is31fl3733_select_page(uint8_t addr, uint8_t page) {
void is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) {
// Assumes page 1 is already selected.
// Transmit PWM registers in 12 transfers of 16 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) {
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(i2c_transfer_buffer + 1, pwm_buffer + i, 16);

for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3733_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) {
if (i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT) == 0) break;
for (uint8_t j = 0; j < IS31FL3733_I2C_PERSISTENCE; j++) {
if (i2c_writeReg(addr << 1, i, pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_transmit(addr << 1, i2c_transfer_buffer, 17, IS31FL3733_I2C_TIMEOUT);
i2c_writeReg(addr << 1, i, pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT);
#endif
}
}
Expand Down
Loading

0 comments on commit 58696a3

Please sign in to comment.