Skip to content

Commit 1ea54a2

Browse files
authored
Complete RGB Matrix support for IS31FL3218 (qmk#22004)
1 parent 32126dc commit 1ea54a2

File tree

9 files changed

+178
-33
lines changed

9 files changed

+178
-33
lines changed

builddefs/common_features.mk

+8-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ endif
416416

417417
RGB_MATRIX_ENABLE ?= no
418418

419-
VALID_RGB_MATRIX_TYPES := aw20216s is31fl3731 is31fl3733 is31fl3736 is31fl3737 is31fl3741 is31fl3742a is31fl3743a is31fl3745 is31fl3746a ckled2001 ws2812 custom
419+
VALID_RGB_MATRIX_TYPES := aw20216s is31fl3218 is31fl3731 is31fl3733 is31fl3736 is31fl3737 is31fl3741 is31fl3742a is31fl3743a is31fl3745 is31fl3746a ckled2001 ws2812 custom
420420
ifeq ($(strip $(RGB_MATRIX_ENABLE)), yes)
421421
ifeq ($(filter $(RGB_MATRIX_DRIVER),$(VALID_RGB_MATRIX_TYPES)),)
422422
$(call CATASTROPHIC_ERROR,Invalid RGB_MATRIX_DRIVER,RGB_MATRIX_DRIVER="$(RGB_MATRIX_DRIVER)" is not a valid matrix type)
@@ -444,6 +444,13 @@ endif
444444
QUANTUM_LIB_SRC += spi_master.c
445445
endif
446446

447+
ifeq ($(strip $(RGB_MATRIX_DRIVER)), is31fl3218)
448+
OPT_DEFS += -DIS31FL3218 -DSTM32_I2C -DHAL_USE_I2C=TRUE
449+
COMMON_VPATH += $(DRIVER_PATH)/led/issi
450+
SRC += is31fl3218.c
451+
QUANTUM_LIB_SRC += i2c_master.c
452+
endif
453+
447454
ifeq ($(strip $(RGB_MATRIX_DRIVER)), is31fl3731)
448455
OPT_DEFS += -DIS31FL3731 -DSTM32_I2C -DHAL_USE_I2C=TRUE
449456
COMMON_VPATH += $(DRIVER_PATH)/led/issi

docs/reference_info_json.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ Configures the [RGB Matrix](feature_rgb_matrix.md) feature.
530530
* The centroid (geometric center) of the LEDs. Used for certain effects.
531531
* Default: `[112, 32]`
532532
* `driver` (Required)
533-
* The driver to use. Must be one of `aw20216s`, `ckled2001`, `custom`, `is31fl3731`, `is31fl3733`, `is31fl3736`, `is31fl3737`, `is31fl3741`, `is31fl3742a`, `is31fl3743a`, `is31fl3745`, `is31fl3746a`, `ws2812`.
533+
* The driver to use. Must be one of `aw20216s`, `ckled2001`, `custom`, `is31fl3218`, `is31fl3731`, `is31fl3733`, `is31fl3736`, `is31fl3737`, `is31fl3741`, `is31fl3742a`, `is31fl3743a`, `is31fl3745`, `is31fl3746a`, `ws2812`.
534534
* `hue_steps`
535535
* The number of hue adjustment steps.
536536
* Default: `8`

drivers/led/issi/is31fl3218.c

+79-17
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,57 @@
1414
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1515
*/
1616
#include "is31fl3218.h"
17+
#include <string.h>
1718
#include "i2c_master.h"
1819

19-
// This is the full 8-bit address
20-
#define IS31FL3218_I2C_ADDRESS 0xA8
21-
2220
// These are the register addresses
2321
#define IS31FL3218_REG_SHUTDOWN 0x00
2422
#define IS31FL3218_REG_PWM 0x01
2523
#define IS31FL3218_REG_CONTROL 0x13
2624
#define IS31FL3218_REG_UPDATE 0x16
2725
#define IS31FL3218_REG_RESET 0x17
2826

29-
// Default timeout if no I2C response
30-
#define IS31FL3218_I2C_TIMEOUT 100
27+
#ifndef IS31FL3218_I2C_TIMEOUT
28+
# define IS31FL3218_I2C_TIMEOUT 100
29+
#endif
30+
31+
#ifndef IS31FL3218_I2C_PERSISTENCE
32+
# define IS31FL3218_I2C_PERSISTENCE 0
33+
#endif
3134

3235
// Reusable buffer for transfers
3336
uint8_t g_twi_transfer_buffer[20];
3437

3538
// IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining.
36-
// If used as RGB LED driver, LEDs are assigned RGB,RGB,RGB,RGB,RGB,RGB
3739
uint8_t g_pwm_buffer[18];
3840
bool g_pwm_buffer_update_required = false;
3941

42+
uint8_t g_led_control_registers[3] = {0};
43+
bool g_led_control_registers_update_required = false;
44+
4045
void is31fl3218_write_register(uint8_t reg, uint8_t data) {
4146
g_twi_transfer_buffer[0] = reg;
4247
g_twi_transfer_buffer[1] = data;
43-
i2c_transmit(IS31FL3218_I2C_ADDRESS, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT);
48+
#if IS31FL3218_I2C_PERSISTENCE > 0
49+
for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) {
50+
if (i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT) == 0) break;
51+
}
52+
#else
53+
i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 2, IS31FL3218_I2C_TIMEOUT);
54+
#endif
4455
}
4556

4657
void is31fl3218_write_pwm_buffer(uint8_t *pwm_buffer) {
4758
g_twi_transfer_buffer[0] = IS31FL3218_REG_PWM;
4859
memcpy(g_twi_transfer_buffer + 1, pwm_buffer, 18);
4960

50-
i2c_transmit(IS31FL3218_I2C_ADDRESS, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
61+
#if IS31FL3218_I2C_PERSISTENCE > 0
62+
for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) {
63+
i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
64+
}
65+
#else
66+
i2c_transmit(IS31FL3218_I2C_ADDRESS << 1, g_twi_transfer_buffer, 19, IS31FL3218_I2C_TIMEOUT);
67+
#endif
5168
}
5269

5370
void is31fl3218_init(void) {
@@ -62,36 +79,81 @@ void is31fl3218_init(void) {
6279
is31fl3218_write_register(IS31FL3218_REG_PWM + i, 0x00);
6380
}
6481

65-
// Enable all channels
82+
// turn off all LEDs in the LED control register
6683
for (uint8_t i = 0; i < 3; i++) {
67-
is31fl3218_write_register(IS31FL3218_REG_CONTROL + i, 0b00111111);
84+
is31fl3218_write_register(IS31FL3218_REG_CONTROL + i, 0x00);
6885
}
6986

7087
// Load PWM registers and LED Control register data
7188
is31fl3218_write_register(IS31FL3218_REG_UPDATE, 0x01);
7289
}
7390

7491
void is31fl3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
75-
if (g_pwm_buffer[index * 3 + 0] == red && g_pwm_buffer[index * 3 + 1] == green && g_pwm_buffer[index * 3 + 2] == blue) {
92+
is31fl3218_led_t led;
93+
if (index >= 0 && index < RGB_MATRIX_LED_COUNT) {
94+
memcpy_P(&led, (&g_is31fl3218_leds[index]), sizeof(led));
95+
}
96+
if (g_pwm_buffer[led.r - IS31FL3218_REG_PWM] == red && g_pwm_buffer[led.g - IS31FL3218_REG_PWM] == green && g_pwm_buffer[led.b - IS31FL3218_REG_PWM] == blue) {
7697
return;
7798
}
78-
g_pwm_buffer[index * 3 + 0] = red;
79-
g_pwm_buffer[index * 3 + 1] = green;
80-
g_pwm_buffer[index * 3 + 2] = blue;
81-
g_pwm_buffer_update_required = true;
99+
g_pwm_buffer[led.r - IS31FL3218_REG_PWM] = red;
100+
g_pwm_buffer[led.g - IS31FL3218_REG_PWM] = green;
101+
g_pwm_buffer[led.b - IS31FL3218_REG_PWM] = blue;
102+
g_pwm_buffer_update_required = true;
82103
}
83104

84105
void is31fl3218_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
85-
for (int i = 0; i < 6; i++) {
106+
for (int i = 0; i < RGB_MATRIX_LED_COUNT; i++) {
86107
is31fl3218_set_color(i, red, green, blue);
87108
}
88109
}
89110

111+
void is31fl3218_set_led_control_register(uint8_t index, bool red, bool green, bool blue) {
112+
is31fl3218_led_t led;
113+
memcpy_P(&led, (&g_is31fl3218_leds[index]), sizeof(led));
114+
115+
uint8_t control_register_r = (led.r - IS31FL3218_REG_PWM) / 6;
116+
uint8_t control_register_g = (led.g - IS31FL3218_REG_PWM) / 6;
117+
uint8_t control_register_b = (led.b - IS31FL3218_REG_PWM) / 6;
118+
uint8_t bit_r = (led.r - IS31FL3218_REG_PWM) % 6;
119+
uint8_t bit_g = (led.g - IS31FL3218_REG_PWM) % 6;
120+
uint8_t bit_b = (led.b - IS31FL3218_REG_PWM) % 6;
121+
122+
if (red) {
123+
g_led_control_registers[control_register_r] |= (1 << bit_r);
124+
} else {
125+
g_led_control_registers[control_register_r] &= ~(1 << bit_r);
126+
}
127+
if (green) {
128+
g_led_control_registers[control_register_g] |= (1 << bit_g);
129+
} else {
130+
g_led_control_registers[control_register_g] &= ~(1 << bit_g);
131+
}
132+
if (blue) {
133+
g_led_control_registers[control_register_b] |= (1 << bit_b);
134+
} else {
135+
g_led_control_registers[control_register_b] &= ~(1 << bit_b);
136+
}
137+
138+
g_led_control_registers_update_required = true;
139+
}
140+
90141
void is31fl3218_update_pwm_buffers(void) {
91142
if (g_pwm_buffer_update_required) {
92143
is31fl3218_write_pwm_buffer(g_pwm_buffer);
93144
// Load PWM registers and LED Control register data
94145
is31fl3218_write_register(IS31FL3218_REG_UPDATE, 0x01);
146+
147+
g_pwm_buffer_update_required = false;
148+
}
149+
}
150+
151+
void is31fl3218_update_led_control_registers(void) {
152+
if (g_led_control_registers_update_required) {
153+
for (int i = 0; i < 3; i++) {
154+
is31fl3218_write_register(IS31FL3218_REG_CONTROL + i, g_led_control_registers[i]);
155+
}
156+
157+
g_led_control_registers_update_required = false;
95158
}
96-
g_pwm_buffer_update_required = false;
97159
}

drivers/led/issi/is31fl3218.h

+37-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,45 @@
1818

1919
#include <stdint.h>
2020
#include <stdbool.h>
21-
#include <string.h>
21+
#include "progmem.h"
22+
23+
#define IS31FL3218_I2C_ADDRESS 0x54
24+
25+
typedef struct is31fl3218_led_t {
26+
uint8_t r;
27+
uint8_t g;
28+
uint8_t b;
29+
} __attribute__((packed)) is31fl3218_led_t;
30+
31+
extern const is31fl3218_led_t PROGMEM g_is31fl3218_leds[RGB_MATRIX_LED_COUNT];
2232

2333
void is31fl3218_init(void);
34+
2435
void is31fl3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
36+
2537
void is31fl3218_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
38+
39+
void is31fl3218_set_led_control_register(uint8_t index, bool red, bool green, bool blue);
40+
2641
void is31fl3218_update_pwm_buffers(void);
42+
43+
void is31fl3218_update_led_control_registers(void);
44+
45+
#define OUT1 0x01
46+
#define OUT2 0x02
47+
#define OUT3 0x03
48+
#define OUT4 0x04
49+
#define OUT5 0x05
50+
#define OUT6 0x06
51+
#define OUT7 0x07
52+
#define OUT8 0x08
53+
#define OUT9 0x09
54+
#define OUT10 0x0A
55+
#define OUT11 0x0B
56+
#define OUT12 0x0C
57+
#define OUT13 0x0D
58+
#define OUT14 0x0E
59+
#define OUT15 0x0F
60+
#define OUT16 0x10
61+
#define OUT17 0x11
62+
#define OUT18 0x12

keyboards/wilba_tech/rama_works_m60_a/keymaps/zyber/config.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33

44
#pragma once
55
#undef PRODUCT
6-
#define PRODUCT RAMA WORKS M60-A Seq2
7-
#undef DESCRIPTION
8-
#define DESCRIPTION RAMA WORKS M60-A Seq2
6+
#define PRODUCT "RAMA WORKS M60-A Seq2"

keyboards/wilba_tech/rama_works_m6_b/config.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
/* Locking resynchronize hack */
2121
#define LOCKING_RESYNC_ENABLE
2222

23+
#define RGB_MATRIX_LED_COUNT 6
24+
2325
#define RGB_BACKLIGHT_ENABLED 1
2426

2527
// This conditionally compiles the backlight code for M6-B specifics

keyboards/wilba_tech/wt_rgb_backlight.c

+19-1
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,14 @@ const is31_led PROGMEM g_is31_leds[RGB_MATRIX_LED_COUNT] = {
834834
};
835835

836836
#elif defined(RGB_BACKLIGHT_M6_B)
837-
// Driver has fixed mapping of index to the red, green and blue LEDs
837+
const is31fl3218_led_t PROGMEM g_is31fl3218_leds[RGB_MATRIX_LED_COUNT] = {
838+
{OUT1, OUT2, OUT3},
839+
{OUT4, OUT5, OUT6},
840+
{OUT7, OUT8, OUT9},
841+
{OUT10, OUT11, OUT12},
842+
{OUT13, OUT14, OUT15},
843+
{OUT16, OUT17, OUT18}
844+
};
838845
#elif defined(RGB_BACKLIGHT_M10_C)
839846
// This is a 7-bit address, that gets left-shifted and bit 0
840847
// set to 0 for write, 1 for read (as per I2C protocol)
@@ -2915,6 +2922,17 @@ void backlight_init_drivers(void)
29152922

29162923
#if defined(RGB_BACKLIGHT_M6_B)
29172924
is31fl3218_init();
2925+
2926+
for ( int index = 0; index < RGB_MATRIX_LED_COUNT; index++ )
2927+
{
2928+
bool enabled = true;
2929+
2930+
// This only caches it for later
2931+
is31fl3218_set_led_control_register( index, enabled, enabled, enabled );
2932+
}
2933+
2934+
// This actually updates the LED drivers
2935+
is31fl3218_update_led_control_registers();
29182936
#elif defined(RGB_BACKLIGHT_HS60)
29192937
is31fl3733_init( ISSI_ADDR_1, 0 );
29202938

quantum/rgb_matrix/rgb_matrix.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#include "color.h"
2525
#include "keyboard.h"
2626

27-
#ifdef IS31FL3731
27+
#if defined(IS31FL3218)
28+
# include "is31fl3218.h"
29+
#elif defined(IS31FL3731)
2830
# include "is31fl3731.h"
2931
#elif defined(IS31FL3733)
3032
# include "is31fl3733.h"

quantum/rgb_matrix/rgb_matrix_drivers.c

+28-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* be here if shared between boards.
2525
*/
2626

27-
#if defined(IS31FL3731) || defined(IS31FL3733) || defined(IS31FL3736) || defined(IS31FL3737) || defined(IS31FL3741) || defined(IS31FLCOMMON) || defined(CKLED2001)
27+
#if defined(IS31FL3218) || defined(IS31FL3731) || defined(IS31FL3733) || defined(IS31FL3736) || defined(IS31FL3737) || defined(IS31FL3741) || defined(IS31FLCOMMON) || defined(CKLED2001)
2828
# include "i2c_master.h"
2929

3030
// TODO: Remove this at some later date
@@ -37,7 +37,10 @@
3737
static void init(void) {
3838
i2c_init();
3939

40-
# if defined(IS31FL3731)
40+
# if defined(IS31FL3218)
41+
is31fl3218_init();
42+
43+
# elif defined(IS31FL3731)
4144
is31fl3731_init(DRIVER_ADDR_1);
4245
# if defined(DRIVER_ADDR_2)
4346
is31fl3731_init(DRIVER_ADDR_2);
@@ -138,7 +141,9 @@ static void init(void) {
138141
bool enabled = true;
139142

140143
// This only caches it for later
141-
# if defined(IS31FL3731)
144+
# if defined(IS31FL3218)
145+
is31fl3218_set_led_control_register(index, enabled, enabled, enabled);
146+
# elif defined(IS31FL3731)
142147
is31fl3731_set_led_control_register(index, enabled, enabled, enabled);
143148
# elif defined(IS31FL3733)
144149
is31fl3733_set_led_control_register(index, enabled, enabled, enabled);
@@ -156,7 +161,10 @@ static void init(void) {
156161
}
157162

158163
// This actually updates the LED drivers
159-
# if defined(IS31FL3731)
164+
# if defined(IS31FL3218)
165+
is31fl3218_update_led_control_registers();
166+
167+
# elif defined(IS31FL3731)
160168
is31fl3731_update_led_control_registers(DRIVER_ADDR_1, 0);
161169
# if defined(DRIVER_ADDR_2)
162170
is31fl3731_update_led_control_registers(DRIVER_ADDR_2, 1);
@@ -245,7 +253,19 @@ static void init(void) {
245253
# endif
246254
}
247255

248-
# if defined(IS31FL3731)
256+
# if defined(IS31FL3218)
257+
static void flush(void) {
258+
is31fl3218_update_pwm_buffers();
259+
}
260+
261+
const rgb_matrix_driver_t rgb_matrix_driver = {
262+
.init = init,
263+
.flush = flush,
264+
.set_color = is31fl3218_set_color,
265+
.set_color_all = is31fl3218_set_color_all,
266+
};
267+
268+
# elif defined(IS31FL3731)
249269
static void flush(void) {
250270
is31fl3731_update_pwm_buffers(DRIVER_ADDR_1, 0);
251271
# if defined(DRIVER_ADDR_2)
@@ -260,9 +280,9 @@ static void flush(void) {
260280
}
261281

262282
const rgb_matrix_driver_t rgb_matrix_driver = {
263-
.init = init,
264-
.flush = flush,
265-
.set_color = is31fl3731_set_color,
283+
.init = init,
284+
.flush = flush,
285+
.set_color = is31fl3731_set_color,
266286
.set_color_all = is31fl3731_set_color_all,
267287
};
268288

0 commit comments

Comments
 (0)