aboutsummaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2019-04-08 19:57:43 -0400
committerDrashna Jaelre <drashna@live.com>2019-04-08 16:57:43 -0700
commitfa4052c26e33b42b65521fd5ba9f94a3a778b275 (patch)
treeb16d69187656b109d98710359c9044edc716aaa2 /drivers
parentbc536b9b6d98e5428a28f6e6ba69675bd77b79cc (diff)
downloadqmk_firmware-fa4052c26e33b42b65521fd5ba9f94a3a778b275.tar.gz
qmk_firmware-fa4052c26e33b42b65521fd5ba9f94a3a778b275.zip
Adds the Planck EZ, 3737 RGB, fixes out-of-tune notes (#5532)
* RGB Matrix overhaul Breakout of animations to separate files Integration of optimized int based math lib Overhaul of rgb_matrix.c and animations for performance * Updating effect function api for future extensions * Combined the keypresses || keyreleases define checks into a single define so I stop forgetting it where necessary * Moving define RGB_MATRIX_KEYREACTIVE_ENABLED earlier in the include chain * Adds the Planck EZ, 3737 RGB, fixes out-of-tune notes * fix bug in quantum/rgb_matrix_drivers.c Co-Authored-By: jackhumbert <jack.humb@gmail.com> * update command setting to the correct default * correct rgb config * remove commented-out lines * update docs for the 3737 * Update docs/feature_rgb_matrix.md Co-Authored-By: jackhumbert <jack.humb@gmail.com>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/issi/is31fl3737.c252
-rw-r--r--drivers/issi/is31fl3737.h207
2 files changed, 459 insertions, 0 deletions
diff --git a/drivers/issi/is31fl3737.c b/drivers/issi/is31fl3737.c
new file mode 100644
index 000000000..649104927
--- /dev/null
+++ b/drivers/issi/is31fl3737.c
@@ -0,0 +1,252 @@
1/* Copyright 2017 Jason Williams
2 * Copyright 2018 Jack Humbert
3 * Copyright 2018 Yiancar
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifdef __AVR__
20#include <avr/interrupt.h>
21#include <avr/io.h>
22#include <util/delay.h>
23#else
24#include "wait.h"
25#endif
26
27#include <string.h>
28#include "i2c_master.h"
29#include "progmem.h"
30#include "rgb_matrix.h"
31
32// This is a 7-bit address, that gets left-shifted and bit 0
33// set to 0 for write, 1 for read (as per I2C protocol)
34// The address will vary depending on your wiring:
35// 00 <-> GND
36// 01 <-> SCL
37// 10 <-> SDA
38// 11 <-> VCC
39// ADDR1 represents A1:A0 of the 7-bit address.
40// ADDR2 represents A3:A2 of the 7-bit address.
41// The result is: 0b101(ADDR2)(ADDR1)
42#define ISSI_ADDR_DEFAULT 0x50
43
44#define ISSI_COMMANDREGISTER 0xFD
45#define ISSI_COMMANDREGISTER_WRITELOCK 0xFE
46#define ISSI_INTERRUPTMASKREGISTER 0xF0
47#define ISSI_INTERRUPTSTATUSREGISTER 0xF1
48
49#define ISSI_PAGE_LEDCONTROL 0x00 //PG0
50#define ISSI_PAGE_PWM 0x01 //PG1
51#define ISSI_PAGE_AUTOBREATH 0x02 //PG2
52#define ISSI_PAGE_FUNCTION 0x03 //PG3
53
54#define ISSI_REG_CONFIGURATION 0x00 //PG3
55#define ISSI_REG_GLOBALCURRENT 0x01 //PG3
56#define ISSI_REG_RESET 0x11// PG3
57#define ISSI_REG_SWPULLUP 0x0F //PG3
58#define ISSI_REG_CSPULLUP 0x10 //PG3
59
60#ifndef ISSI_TIMEOUT
61 #define ISSI_TIMEOUT 100
62#endif
63
64#ifndef ISSI_PERSISTENCE
65 #define ISSI_PERSISTENCE 0
66#endif
67
68// Transfer buffer for TWITransmitData()
69uint8_t g_twi_transfer_buffer[20];
70
71// These buffers match the IS31FL3737 PWM registers.
72// The control buffers match the PG0 LED On/Off registers.
73// Storing them like this is optimal for I2C transfers to the registers.
74// We could optimize this and take out the unused registers from these
75// buffers and the transfers in IS31FL3737_write_pwm_buffer() but it's
76// probably not worth the extra complexity.
77uint8_t g_pwm_buffer[DRIVER_COUNT][192];
78bool g_pwm_buffer_update_required = false;
79
80uint8_t g_led_control_registers[DRIVER_COUNT][24] = { { 0 } };
81bool g_led_control_registers_update_required = false;
82
83void IS31FL3737_write_register( uint8_t addr, uint8_t reg, uint8_t data )
84{
85 g_twi_transfer_buffer[0] = reg;
86 g_twi_transfer_buffer[1] = data;
87
88 #if ISSI_PERSISTENCE > 0
89 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) {
90 if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, ISSI_TIMEOUT) == 0)
91 break;
92 }
93 #else
94 i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, ISSI_TIMEOUT);
95 #endif
96}
97
98void IS31FL3737_write_pwm_buffer( uint8_t addr, uint8_t *pwm_buffer )
99{
100 // assumes PG1 is already selected
101
102 // transmit PWM registers in 12 transfers of 16 bytes
103 // g_twi_transfer_buffer[] is 20 bytes
104
105 // iterate over the pwm_buffer contents at 16 byte intervals
106 for ( int i = 0; i < 192; i += 16 ) {
107 g_twi_transfer_buffer[0] = i;
108 // copy the data from i to i+15
109 // device will auto-increment register for data after the first byte
110 // thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer
111 for ( int j = 0; j < 16; j++ ) {
112 g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j];
113 }
114
115 #if ISSI_PERSISTENCE > 0
116 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) {
117 if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, ISSI_TIMEOUT) == 0)
118 break;
119 }
120 #else
121 i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, ISSI_TIMEOUT);
122 #endif
123 }
124}
125
126void IS31FL3737_init( uint8_t addr )
127{
128 // In order to avoid the LEDs being driven with garbage data
129 // in the LED driver's PWM registers, shutdown is enabled last.
130 // Set up the mode and other settings, clear the PWM registers,
131 // then disable software shutdown.
132
133 // Unlock the command register.
134 IS31FL3737_write_register( addr, ISSI_COMMANDREGISTER_WRITELOCK, 0xC5 );
135
136 // Select PG0
137 IS31FL3737_write_register( addr, ISSI_COMMANDREGISTER, ISSI_PAGE_LEDCONTROL );
138 // Turn off all LEDs.
139 for ( int i = 0x00; i <= 0x17; i++ )
140 {
141 IS31FL3737_write_register( addr, i, 0x00 );
142 }
143
144 // Unlock the command register.
145 IS31FL3737_write_register( addr, ISSI_COMMANDREGISTER_WRITELOCK, 0xC5 );
146
147 // Select PG1
148 IS31FL3737_write_register( addr, ISSI_COMMANDREGISTER, ISSI_PAGE_PWM );
149 // Set PWM on all LEDs to 0
150 // No need to setup Breath registers to PWM as that is the default.
151 for ( int i = 0x00; i <= 0xBF; i++ )
152 {
153 IS31FL3737_write_register( addr, i, 0x00 );
154 }
155
156 // Unlock the command register.
157 IS31FL3737_write_register( addr, ISSI_COMMANDREGISTER_WRITELOCK, 0xC5 );
158
159 // Select PG3
160 IS31FL3737_write_register( addr, ISSI_COMMANDREGISTER, ISSI_PAGE_FUNCTION );
161 // Set global current to maximum.
162 IS31FL3737_write_register( addr, ISSI_REG_GLOBALCURRENT, 0xFF );
163 // Disable software shutdown.
164 IS31FL3737_write_register( addr, ISSI_REG_CONFIGURATION, 0x01 );
165
166 // Wait 10ms to ensure the device has woken up.
167 #ifdef __AVR__
168 _delay_ms( 10 );
169 #else
170 wait_ms(10);
171 #endif
172}
173
174void IS31FL3737_set_color( int index, uint8_t red, uint8_t green, uint8_t blue )
175{
176 if ( index >= 0 && index < DRIVER_LED_TOTAL ) {
177 is31_led led = g_is31_leds[index];
178
179 g_pwm_buffer[led.driver][led.r] = red;
180 g_pwm_buffer[led.driver][led.g] = green;
181 g_pwm_buffer[led.driver][led.b] = blue;
182 g_pwm_buffer_update_required = true;
183 }
184}
185
186void IS31FL3737_set_color_all( uint8_t red, uint8_t green, uint8_t blue )
187{
188 for ( int i = 0; i < DRIVER_LED_TOTAL; i++ )
189 {
190 IS31FL3737_set_color( i, red, green, blue );
191 }
192}
193
194void IS31FL3737_set_led_control_register( uint8_t index, bool red, bool green, bool blue )
195{
196 is31_led led = g_is31_leds[index];
197
198 uint8_t control_register_r = led.r / 8;
199 uint8_t control_register_g = led.g / 8;
200 uint8_t control_register_b = led.b / 8;
201 uint8_t bit_r = led.r % 8;
202 uint8_t bit_g = led.g % 8;
203 uint8_t bit_b = led.b % 8;
204
205 if ( red ) {
206 g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r);
207 } else {
208 g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r);
209 }
210 if ( green ) {
211 g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g);
212 } else {
213 g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g);
214 }
215 if ( blue ) {
216 g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b);
217 } else {
218 g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b);
219 }
220
221 g_led_control_registers_update_required = true;
222
223}
224
225void IS31FL3737_update_pwm_buffers( uint8_t addr1, uint8_t addr2 )
226{
227 if ( g_pwm_buffer_update_required )
228 {
229 // Firstly we need to unlock the command register and select PG1
230 IS31FL3737_write_register( addr1, ISSI_COMMANDREGISTER_WRITELOCK, 0xC5 );
231 IS31FL3737_write_register( addr1, ISSI_COMMANDREGISTER, ISSI_PAGE_PWM );
232
233 IS31FL3737_write_pwm_buffer( addr1, g_pwm_buffer[0] );
234 //IS31FL3737_write_pwm_buffer( addr2, g_pwm_buffer[1] );
235 }
236 g_pwm_buffer_update_required = false;
237}
238
239void IS31FL3737_update_led_control_registers( uint8_t addr1, uint8_t addr2 )
240{
241 if ( g_led_control_registers_update_required )
242 {
243 // Firstly we need to unlock the command register and select PG0
244 IS31FL3737_write_register( addr1, ISSI_COMMANDREGISTER_WRITELOCK, 0xC5 );
245 IS31FL3737_write_register( addr1, ISSI_COMMANDREGISTER, ISSI_PAGE_LEDCONTROL );
246 for ( int i=0; i<24; i++ )
247 {
248 IS31FL3737_write_register(addr1, i, g_led_control_registers[0][i] );
249 //IS31FL3737_write_register(addr2, i, g_led_control_registers[1][i] );
250 }
251 }
252}
diff --git a/drivers/issi/is31fl3737.h b/drivers/issi/is31fl3737.h
new file mode 100644
index 000000000..69c4b9b53
--- /dev/null
+++ b/drivers/issi/is31fl3737.h
@@ -0,0 +1,207 @@
1/* Copyright 2017 Jason Williams
2 * Copyright 2018 Jack Humbert
3 * Copyright 2018 Yiancar
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20#ifndef IS31FL3737_DRIVER_H
21#define IS31FL3737_DRIVER_H
22
23#include <stdint.h>
24#include <stdbool.h>
25
26typedef struct is31_led {
27 uint8_t driver:2;
28 uint8_t r;
29 uint8_t g;
30 uint8_t b;
31} __attribute__((packed)) is31_led;
32
33extern const is31_led g_is31_leds[DRIVER_LED_TOTAL];
34
35void IS31FL3737_init( uint8_t addr );
36void IS31FL3737_write_register( uint8_t addr, uint8_t reg, uint8_t data );
37void IS31FL3737_write_pwm_buffer( uint8_t addr, uint8_t *pwm_buffer );
38
39void IS31FL3737_set_color( int index, uint8_t red, uint8_t green, uint8_t blue );
40void IS31FL3737_set_color_all( uint8_t red, uint8_t green, uint8_t blue );
41
42void IS31FL3737_set_led_control_register( uint8_t index, bool red, bool green, bool blue );
43
44// This should not be called from an interrupt
45// (eg. from a timer interrupt).
46// Call this while idle (in between matrix scans).
47// If the buffer is dirty, it will update the driver with the buffer.
48void IS31FL3737_update_pwm_buffers( uint8_t addr1, uint8_t addr2 );
49void IS31FL3737_update_led_control_registers( uint8_t addr1, uint8_t addr2 );
50
51#define A_1 0x00
52#define A_2 0x01
53#define A_3 0x02
54#define A_4 0x03
55#define A_5 0x04
56#define A_6 0x05
57#define A_7 0x08
58#define A_8 0x09
59#define A_9 0x0A
60#define A_10 0x0B
61#define A_11 0x0C
62#define A_12 0x0D
63
64#define B_1 0x10
65#define B_2 0x11
66#define B_3 0x12
67#define B_4 0x13
68#define B_5 0x14
69#define B_6 0x15
70#define B_7 0x18
71#define B_8 0x19
72#define B_9 0x1A
73#define B_10 0x1B
74#define B_11 0x1C
75#define B_12 0x1D
76
77#define C_1 0x20
78#define C_2 0x21
79#define C_3 0x22
80#define C_4 0x23
81#define C_5 0x24
82#define C_6 0x25
83#define C_7 0x28
84#define C_8 0x29
85#define C_9 0x2A
86#define C_10 0x2B
87#define C_11 0x2C
88#define C_12 0x2D
89
90#define D_1 0x30
91#define D_2 0x31
92#define D_3 0x32
93#define D_4 0x33
94#define D_5 0x34
95#define D_6 0x35
96#define D_7 0x38
97#define D_8 0x39
98#define D_9 0x3A
99#define D_10 0x3B
100#define D_11 0x3C
101#define D_12 0x3D
102
103#define E_1 0x40
104#define E_2 0x41
105#define E_3 0x42
106#define E_4 0x43
107#define E_5 0x44
108#define E_6 0x45
109#define E_7 0x48
110#define E_8 0x49
111#define E_9 0x4A
112#define E_10 0x4B
113#define E_11 0x4C
114#define E_12 0x4D
115
116#define F_1 0x50
117#define F_2 0x51
118#define F_3 0x52
119#define F_4 0x53
120#define F_5 0x54
121#define F_6 0x55
122#define F_7 0x58
123#define F_8 0x59
124#define F_9 0x5A
125#define F_10 0x5B
126#define F_11 0x5C
127#define F_12 0x5D
128
129#define G_1 0x60
130#define G_2 0x61
131#define G_3 0x62
132#define G_4 0x63
133#define G_5 0x64
134#define G_6 0x65
135#define G_7 0x68
136#define G_8 0x69
137#define G_9 0x6A
138#define G_10 0x6B
139#define G_11 0x6C
140#define G_12 0x6D
141
142#define H_1 0x70
143#define H_2 0x71
144#define H_3 0x72
145#define H_4 0x73
146#define H_5 0x74
147#define H_6 0x75
148#define H_7 0x78
149#define H_8 0x79
150#define H_9 0x7A
151#define H_10 0x7B
152#define H_11 0x7C
153#define H_12 0x7D
154
155#define I_1 0x80
156#define I_2 0x81
157#define I_3 0x82
158#define I_4 0x83
159#define I_5 0x84
160#define I_6 0x85
161#define I_7 0x88
162#define I_8 0x89
163#define I_9 0x8A
164#define I_10 0x8B
165#define I_11 0x8C
166#define I_12 0x8D
167
168#define J_1 0x90
169#define J_2 0x91
170#define J_3 0x92
171#define J_4 0x93
172#define J_5 0x94
173#define J_6 0x95
174#define J_7 0x98
175#define J_8 0x99
176#define J_9 0x9A
177#define J_10 0x9B
178#define J_11 0x9C
179#define J_12 0x9D
180
181#define K_1 0xA0
182#define K_2 0xA1
183#define K_3 0xA2
184#define K_4 0xA3
185#define K_5 0xA4
186#define K_6 0xA5
187#define K_7 0xA8
188#define K_8 0xA9
189#define K_9 0xAA
190#define K_10 0xAB
191#define K_11 0xAC
192#define K_12 0xAD
193
194#define L_1 0xB0
195#define L_2 0xB1
196#define L_3 0xB2
197#define L_4 0xB3
198#define L_5 0xB4
199#define L_6 0xB5
200#define L_7 0xB8
201#define L_8 0xB9
202#define L_9 0xBA
203#define L_10 0xBB
204#define L_11 0xBC
205#define L_12 0xBD
206
207#endif // IS31FL3737_DRIVER_H