aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xdrivers/avr/i2c_master.c213
-rwxr-xr-xdrivers/avr/i2c_master.h27
-rw-r--r--drivers/avr/is31fl3731.c36
-rw-r--r--keyboards/ergodox_ez/config.h14
-rw-r--r--keyboards/ergodox_ez/ergodox_ez.c169
-rw-r--r--keyboards/ergodox_ez/ergodox_ez.h5
-rw-r--r--keyboards/ergodox_ez/matrix.c24
-rw-r--r--keyboards/ergodox_ez/rules.mk7
-rw-r--r--quantum/quantum.c2
-rw-r--r--quantum/rgb_matrix.c78
-rw-r--r--quantum/rgb_matrix.h3
11 files changed, 408 insertions, 170 deletions
diff --git a/drivers/avr/i2c_master.c b/drivers/avr/i2c_master.c
index f4a4bb7b0..4e76e2e7c 100755
--- a/drivers/avr/i2c_master.c
+++ b/drivers/avr/i2c_master.c
@@ -6,6 +6,7 @@
6#include <util/twi.h> 6#include <util/twi.h>
7 7
8#include "i2c_master.h" 8#include "i2c_master.h"
9#include "timer.h"
9 10
10#define F_SCL 400000UL // SCL frequency 11#define F_SCL 400000UL // SCL frequency
11#define Prescaler 1 12#define Prescaler 1
@@ -13,137 +14,205 @@
13 14
14void i2c_init(void) 15void i2c_init(void)
15{ 16{
17 TWSR = 0; /* no prescaler */
16 TWBR = (uint8_t)TWBR_val; 18 TWBR = (uint8_t)TWBR_val;
17} 19}
18 20
19uint8_t i2c_start(uint8_t address) 21i2c_status_t i2c_start(uint8_t address, uint16_t timeout)
20{ 22{
21 // reset TWI control register 23 // reset TWI control register
22 TWCR = 0; 24 TWCR = 0;
23 // transmit START condition 25 // transmit START condition
24 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); 26 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
25 // wait for end of transmission 27
26 while( !(TWCR & (1<<TWINT)) ); 28 uint16_t timeout_timer = timer_read();
27 29 while( !(TWCR & (1<<TWINT)) ) {
30 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
31 return I2C_STATUS_TIMEOUT;
32 }
33 }
34
28 // check if the start condition was successfully transmitted 35 // check if the start condition was successfully transmitted
29 if((TWSR & 0xF8) != TW_START){ return 1; } 36 if(((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)){ return I2C_STATUS_ERROR; }
30 37
31 // load slave address into data register 38 // load slave address into data register
32 TWDR = address; 39 TWDR = address;
33 // start transmission of address 40 // start transmission of address
34 TWCR = (1<<TWINT) | (1<<TWEN); 41 TWCR = (1<<TWINT) | (1<<TWEN);
35 // wait for end of transmission 42
36 while( !(TWCR & (1<<TWINT)) ); 43 timeout_timer = timer_read();
37 44 while( !(TWCR & (1<<TWINT)) ) {
45 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
46 return I2C_STATUS_TIMEOUT;
47 }
48 }
49
38 // check if the device has acknowledged the READ / WRITE mode 50 // check if the device has acknowledged the READ / WRITE mode
39 uint8_t twst = TW_STATUS & 0xF8; 51 uint8_t twst = TW_STATUS & 0xF8;
40 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1; 52 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return I2C_STATUS_ERROR;
41 53
42 return 0; 54 return I2C_STATUS_SUCCESS;
43} 55}
44 56
45uint8_t i2c_write(uint8_t data) 57i2c_status_t i2c_write(uint8_t data, uint16_t timeout)
46{ 58{
47 // load data into data register 59 // load data into data register
48 TWDR = data; 60 TWDR = data;
49 // start transmission of data 61 // start transmission of data
50 TWCR = (1<<TWINT) | (1<<TWEN); 62 TWCR = (1<<TWINT) | (1<<TWEN);
51 // wait for end of transmission 63
52 while( !(TWCR & (1<<TWINT)) ); 64 uint16_t timeout_timer = timer_read();
53 65 while( !(TWCR & (1<<TWINT)) ) {
54 if( (TWSR & 0xF8) != TW_MT_DATA_ACK ){ return 1; } 66 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
55 67 return I2C_STATUS_TIMEOUT;
56 return 0; 68 }
69 }
70
71 if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return I2C_STATUS_ERROR; }
72
73 return I2C_STATUS_SUCCESS;
57} 74}
58 75
59uint8_t i2c_read_ack(void) 76int16_t i2c_read_ack(uint16_t timeout)
60{ 77{
61 78
62 // start TWI module and acknowledge data after reception 79 // start TWI module and acknowledge data after reception
63 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); 80 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
64 // wait for end of transmission 81
65 while( !(TWCR & (1<<TWINT)) ); 82 uint16_t timeout_timer = timer_read();
83 while( !(TWCR & (1<<TWINT)) ) {
84 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
85 return I2C_STATUS_TIMEOUT;
86 }
87 }
88
66 // return received data from TWDR 89 // return received data from TWDR
67 return TWDR; 90 return TWDR;
68} 91}
69 92
70uint8_t i2c_read_nack(void) 93int16_t i2c_read_nack(uint16_t timeout)
71{ 94{
72 95
73 // start receiving without acknowledging reception 96 // start receiving without acknowledging reception
74 TWCR = (1<<TWINT) | (1<<TWEN); 97 TWCR = (1<<TWINT) | (1<<TWEN);
75 // wait for end of transmission 98
76 while( !(TWCR & (1<<TWINT)) ); 99 uint16_t timeout_timer = timer_read();
100 while( !(TWCR & (1<<TWINT)) ) {
101 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
102 return I2C_STATUS_TIMEOUT;
103 }
104 }
105
77 // return received data from TWDR 106 // return received data from TWDR
78 return TWDR; 107 return TWDR;
79} 108}
80 109
81uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length) 110i2c_status_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)
82{ 111{
83 if (i2c_start(address | I2C_WRITE)) return 1; 112 i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
84 113 if (status) return status;
85 for (uint16_t i = 0; i < length; i++) 114
86 { 115 for (uint16_t i = 0; i < length; i++) {
87 if (i2c_write(data[i])) return 1; 116 status = i2c_write(data[i], timeout);
117 if (status) return status;
88 } 118 }
89 119
90 i2c_stop(); 120 status = i2c_stop(timeout);
91 121 if (status) return status;
92 return 0; 122
123 return I2C_STATUS_SUCCESS;
93} 124}
94 125
95uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length) 126i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout)
96{ 127{
97 if (i2c_start(address | I2C_READ)) return 1; 128 i2c_status_t status = i2c_start(address | I2C_READ, timeout);
98 129 if (status) return status;
99 for (uint16_t i = 0; i < (length-1); i++) 130
100 { 131 for (uint16_t i = 0; i < (length-1); i++) {
101 data[i] = i2c_read_ack(); 132 status = i2c_read_ack(timeout);
133 if (status >= 0) {
134 data[i] = status;
135 } else {
136 return status;
137 }
102 } 138 }
103 data[(length-1)] = i2c_read_nack(); 139
104 140 status = i2c_read_nack(timeout);
105 i2c_stop(); 141 if (status >= 0 ) {
106 142 data[(length-1)] = status;
107 return 0; 143 } else {
144 return status;
145 }
146
147 status = i2c_stop(timeout);
148 if (status) return status;
149
150 return I2C_STATUS_SUCCESS;
108} 151}
109 152
110uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length) 153i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)
111{ 154{
112 if (i2c_start(devaddr | 0x00)) return 1; 155 i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
156 if (status) return status;
113 157
114 i2c_write(regaddr); 158 status = i2c_write(regaddr, timeout);
159 if (status) return status;
115 160
116 for (uint16_t i = 0; i < length; i++) 161 for (uint16_t i = 0; i < length; i++) {
117 { 162 status = i2c_write(data[i], timeout);
118 if (i2c_write(data[i])) return 1; 163 if (status) return status;
119 } 164 }
120 165
121 i2c_stop(); 166 status = i2c_stop(timeout);
167 if (status) return status;
122 168
123 return 0; 169 return I2C_STATUS_SUCCESS;
124} 170}
125 171
126uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length) 172i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout)
127{ 173{
128 if (i2c_start(devaddr)) return 1; 174 i2c_status_t status = i2c_start(devaddr, timeout);
129 175 if (status) return status;
130 i2c_write(regaddr); 176
131 177 status = i2c_write(regaddr, timeout);
132 if (i2c_start(devaddr | 0x01)) return 1; 178 if (status) return status;
133 179
134 for (uint16_t i = 0; i < (length-1); i++) 180 status = i2c_start(devaddr | 0x01, timeout);
135 { 181 if (status) return status;
136 data[i] = i2c_read_ack(); 182
183 for (uint16_t i = 0; i < (length-1); i++) {
184 status = i2c_read_ack(timeout);
185 if (status >= 0) {
186 data[i] = status;
187 } else {
188 return status;
189 }
137 } 190 }
138 data[(length-1)] = i2c_read_nack();
139 191
140 i2c_stop(); 192 status = i2c_read_nack(timeout);
193 if (status >= 0 ) {
194 data[(length-1)] = status;
195 } else {
196 return status;
197 }
141 198
142 return 0; 199 status = i2c_stop(timeout);
200 if (status) return status;
201
202 return I2C_STATUS_SUCCESS;
143} 203}
144 204
145void i2c_stop(void) 205i2c_status_t i2c_stop(uint16_t timeout)
146{ 206{
147 // transmit STOP condition 207 // transmit STOP condition
148 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); 208 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
209
210 uint16_t timeout_timer = timer_read();
211 while(TWCR & (1<<TWSTO)) {
212 if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
213 return I2C_STATUS_TIMEOUT;
214 }
215 }
216
217 return I2C_STATUS_SUCCESS;
149} 218}
diff --git a/drivers/avr/i2c_master.h b/drivers/avr/i2c_master.h
index 2479d28d5..cf93680be 100755
--- a/drivers/avr/i2c_master.h
+++ b/drivers/avr/i2c_master.h
@@ -8,15 +8,24 @@
8#define I2C_READ 0x01 8#define I2C_READ 0x01
9#define I2C_WRITE 0x00 9#define I2C_WRITE 0x00
10 10
11typedef int16_t i2c_status_t;
12
13#define I2C_STATUS_SUCCESS (0)
14#define I2C_STATUS_ERROR (-1)
15#define I2C_STATUS_TIMEOUT (-2)
16
17#define I2C_TIMEOUT_IMMEDIATE (0)
18#define I2C_TIMEOUT_INFINITE (0xFFFF)
19
11void i2c_init(void); 20void i2c_init(void);
12uint8_t i2c_start(uint8_t address); 21i2c_status_t i2c_start(uint8_t address, uint16_t timeout);
13uint8_t i2c_write(uint8_t data); 22i2c_status_t i2c_write(uint8_t data, uint16_t timeout);
14uint8_t i2c_read_ack(void); 23int16_t i2c_read_ack(uint16_t timeout);
15uint8_t i2c_read_nack(void); 24int16_t i2c_read_nack(uint16_t timeout);
16uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length); 25i2c_status_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout);
17uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length); 26i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout);
18uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length); 27i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout);
19uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length); 28i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout);
20void i2c_stop(void); 29i2c_status_t i2c_stop(uint16_t timeout);
21 30
22#endif // I2C_MASTER_H 31#endif // I2C_MASTER_H
diff --git a/drivers/avr/is31fl3731.c b/drivers/avr/is31fl3731.c
index c7a99e3a3..70813464b 100644
--- a/drivers/avr/is31fl3731.c
+++ b/drivers/avr/is31fl3731.c
@@ -49,6 +49,14 @@
49#define ISSI_COMMANDREGISTER 0xFD 49#define ISSI_COMMANDREGISTER 0xFD
50#define ISSI_BANK_FUNCTIONREG 0x0B // helpfully called 'page nine' 50#define ISSI_BANK_FUNCTIONREG 0x0B // helpfully called 'page nine'
51 51
52#ifndef ISSI_TIMEOUT
53 #define ISSI_TIMEOUT 100
54#endif
55
56#ifndef ISSI_PERSISTENCE
57 #define ISSI_PERSISTENCE 0
58#endif
59
52// Transfer buffer for TWITransmitData() 60// Transfer buffer for TWITransmitData()
53uint8_t g_twi_transfer_buffer[20]; 61uint8_t g_twi_transfer_buffer[20];
54 62
@@ -83,8 +91,14 @@ void IS31FL3731_write_register( uint8_t addr, uint8_t reg, uint8_t data )
83 g_twi_transfer_buffer[0] = reg; 91 g_twi_transfer_buffer[0] = reg;
84 g_twi_transfer_buffer[1] = data; 92 g_twi_transfer_buffer[1] = data;
85 93
86 //Transmit data until succesful 94 #if ISSI_PERSISTENCE > 0
87 while(i2c_transmit(addr << 1, g_twi_transfer_buffer,2) != 0); 95 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) {
96 if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, ISSI_TIMEOUT) == 0)
97 break;
98 }
99 #else
100 i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, ISSI_TIMEOUT);
101 #endif
88} 102}
89 103
90void IS31FL3731_write_pwm_buffer( uint8_t addr, uint8_t *pwm_buffer ) 104void IS31FL3731_write_pwm_buffer( uint8_t addr, uint8_t *pwm_buffer )
@@ -95,20 +109,24 @@ void IS31FL3731_write_pwm_buffer( uint8_t addr, uint8_t *pwm_buffer )
95 // g_twi_transfer_buffer[] is 20 bytes 109 // g_twi_transfer_buffer[] is 20 bytes
96 110
97 // iterate over the pwm_buffer contents at 16 byte intervals 111 // iterate over the pwm_buffer contents at 16 byte intervals
98 for ( int i = 0; i < 144; i += 16 ) 112 for ( int i = 0; i < 144; i += 16 ) {
99 {
100 // set the first register, e.g. 0x24, 0x34, 0x44, etc. 113 // set the first register, e.g. 0x24, 0x34, 0x44, etc.
101 g_twi_transfer_buffer[0] = 0x24 + i; 114 g_twi_transfer_buffer[0] = 0x24 + i;
102 // copy the data from i to i+15 115 // copy the data from i to i+15
103 // device will auto-increment register for data after the first byte 116 // device will auto-increment register for data after the first byte
104 // thus this sets registers 0x24-0x33, 0x34-0x43, etc. in one transfer 117 // thus this sets registers 0x24-0x33, 0x34-0x43, etc. in one transfer
105 for ( int j = 0; j < 16; j++ ) 118 for ( int j = 0; j < 16; j++ ) {
106 {
107 g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j]; 119 g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j];
108 } 120 }
109 121
110 //Transmit buffer until succesful 122 #if ISSI_PERSISTENCE > 0
111 while(i2c_transmit(addr << 1, g_twi_transfer_buffer,17) != 0); 123 for (uint8_t i = 0; i < ISSI_PERSISTENCE; i++) {
124 if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, ISSI_TIMEOUT) == 0)
125 break;
126 }
127 #else
128 i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, ISSI_TIMEOUT);
129 #endif
112 } 130 }
113} 131}
114 132
@@ -165,6 +183,7 @@ void IS31FL3731_init( uint8_t addr )
165 // most usage after initialization is just writing PWM buffers in bank 0 183 // most usage after initialization is just writing PWM buffers in bank 0
166 // as there's not much point in double-buffering 184 // as there's not much point in double-buffering
167 IS31FL3731_write_register( addr, ISSI_COMMANDREGISTER, 0 ); 185 IS31FL3731_write_register( addr, ISSI_COMMANDREGISTER, 0 );
186
168} 187}
169 188
170void IS31FL3731_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) 189void IS31FL3731_set_color( int index, uint8_t red, uint8_t green, uint8_t blue )
@@ -217,7 +236,6 @@ void IS31FL3731_set_led_control_register( uint8_t index, bool red, bool green, b
217 236
218 g_led_control_registers_update_required = true; 237 g_led_control_registers_update_required = true;
219 238
220
221} 239}
222 240
223void IS31FL3731_update_pwm_buffers( uint8_t addr1, uint8_t addr2 ) 241void IS31FL3731_update_pwm_buffers( uint8_t addr1, uint8_t addr2 )
diff --git a/keyboards/ergodox_ez/config.h b/keyboards/ergodox_ez/config.h
index ae70c4f2e..1285cbe1c 100644
--- a/keyboards/ergodox_ez/config.h
+++ b/keyboards/ergodox_ez/config.h
@@ -81,10 +81,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
81/* fix space cadet rollover issue */ 81/* fix space cadet rollover issue */
82#define DISABLE_SPACE_CADET_ROLLOVER 82#define DISABLE_SPACE_CADET_ROLLOVER
83 83
84// #define RGB_MIDI 84// #define RGBW_BB_TWI
85#define RGBW_BB_TWI
86 85
87#define RGBW 1 86// #define RGBW 1
88 87
89/* "debounce" is measured in keyboard scans. Some users reported 88/* "debounce" is measured in keyboard scans. Some users reported
90 * needing values as high as 15, which was at the time around 50ms. 89 * needing values as high as 15, which was at the time around 50ms.
@@ -102,6 +101,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
102 101
103#define USB_MAX_POWER_CONSUMPTION 500 102#define USB_MAX_POWER_CONSUMPTION 500
104 103
104// RGB backlight
105#define DRIVER_ADDR_1 0b1110100
106#define DRIVER_ADDR_2 0b1110111
107#define DRIVER_COUNT 2
108#define DRIVER_1_LED_TOTAL 24
109#define DRIVER_2_LED_TOTAL 24
110#define DRIVER_LED_TOTAL DRIVER_1_LED_TOTAL + DRIVER_2_LED_TOTAL
111#define RGB_MATRIX_SKIP_FRAMES 10
112
105// #define RGBLIGHT_COLOR_LAYER_0 0x00, 0x00, 0xFF 113// #define RGBLIGHT_COLOR_LAYER_0 0x00, 0x00, 0xFF
106/* #define RGBLIGHT_COLOR_LAYER_1 0x00, 0x00, 0xFF */ 114/* #define RGBLIGHT_COLOR_LAYER_1 0x00, 0x00, 0xFF */
107/* #define RGBLIGHT_COLOR_LAYER_2 0xFF, 0x00, 0x00 */ 115/* #define RGBLIGHT_COLOR_LAYER_2 0xFF, 0x00, 0x00 */
diff --git a/keyboards/ergodox_ez/ergodox_ez.c b/keyboards/ergodox_ez/ergodox_ez.c
index 437411856..61f910711 100644
--- a/keyboards/ergodox_ez/ergodox_ez.c
+++ b/keyboards/ergodox_ez/ergodox_ez.c
@@ -1,6 +1,4 @@
1#include QMK_KEYBOARD_H 1#include QMK_KEYBOARD_H
2#include "i2cmaster.h"
3
4 2
5extern inline void ergodox_board_led_on(void); 3extern inline void ergodox_board_led_on(void);
6extern inline void ergodox_right_led_1_on(void); 4extern inline void ergodox_right_led_1_on(void);
@@ -24,9 +22,8 @@ extern inline void ergodox_right_led_set(uint8_t led, uint8_t n);
24 22
25extern inline void ergodox_led_all_set(uint8_t n); 23extern inline void ergodox_led_all_set(uint8_t n);
26 24
27
28bool i2c_initialized = 0; 25bool i2c_initialized = 0;
29uint8_t mcp23018_status = 0x20; 26i2c_status_t mcp23018_status = 0x20;
30 27
31void matrix_init_kb(void) { 28void matrix_init_kb(void) {
32 // keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md") 29 // keyboard LEDs (see "PWM on ports OC1(A|B|C)" in "teensy-2-0.md")
@@ -114,33 +111,36 @@ uint8_t init_mcp23018(void) {
114 // uint8_t sreg_prev; 111 // uint8_t sreg_prev;
115 // sreg_prev=SREG; 112 // sreg_prev=SREG;
116 // cli(); 113 // cli();
114
117 if (i2c_initialized == 0) { 115 if (i2c_initialized == 0) {
118 i2c_init(); // on pins D(1,0) 116 i2c_init(); // on pins D(1,0)
119 i2c_initialized = true; 117 i2c_initialized = true;
120 _delay_ms(1000); 118 _delay_ms(1000);
121 } 119 }
120 // i2c_init(); // on pins D(1,0)
121 // _delay_ms(1000);
122 122
123 // set pin direction 123 // set pin direction
124 // - unused : input : 1 124 // - unused : input : 1
125 // - input : input : 1 125 // - input : input : 1
126 // - driving : output : 0 126 // - driving : output : 0
127 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; 127 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
128 mcp23018_status = i2c_write(IODIRA); if (mcp23018_status) goto out; 128 mcp23018_status = i2c_write(IODIRA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
129 mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out; 129 mcp23018_status = i2c_write(0b00000000, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
130 mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out; 130 mcp23018_status = i2c_write(0b00111111, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
131 i2c_stop(); 131 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
132 132
133 // set pull-up 133 // set pull-up
134 // - unused : on : 1 134 // - unused : on : 1
135 // - input : on : 1 135 // - input : on : 1
136 // - driving : off : 0 136 // - driving : off : 0
137 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; 137 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
138 mcp23018_status = i2c_write(GPPUA); if (mcp23018_status) goto out; 138 mcp23018_status = i2c_write(GPPUA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
139 mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out; 139 mcp23018_status = i2c_write(0b00000000, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
140 mcp23018_status = i2c_write(0b00111111); if (mcp23018_status) goto out; 140 mcp23018_status = i2c_write(0b00111111, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
141 141
142out: 142out:
143 i2c_stop(); 143 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
144 144
145#ifdef LEFT_LEDS 145#ifdef LEFT_LEDS
146 if (!mcp23018_status) mcp23018_status = ergodox_left_leds_update(); 146 if (!mcp23018_status) mcp23018_status = ergodox_left_leds_update();
@@ -164,22 +164,22 @@ uint8_t ergodox_left_leds_update(void) {
164 // - unused : hi-Z : 1 164 // - unused : hi-Z : 1
165 // - input : hi-Z : 1 165 // - input : hi-Z : 1
166 // - driving : hi-Z : 1 166 // - driving : hi-Z : 1
167 mcp23018_status = i2c_start(I2C_ADDR_WRITE); 167 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
168 if (mcp23018_status) goto out; 168 if (mcp23018_status) goto out;
169 mcp23018_status = i2c_write(OLATA); 169 mcp23018_status = i2c_write(OLATA, ERGODOX_EZ_I2C_TIMEOUT);
170 if (mcp23018_status) goto out; 170 if (mcp23018_status) goto out;
171 mcp23018_status = i2c_write(0b11111111 171 mcp23018_status = i2c_write(0b11111111
172 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT) 172 & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT),
173 ); 173 ERGODOX_EZ_I2C_TIMEOUT);
174 if (mcp23018_status) goto out; 174 if (mcp23018_status) goto out;
175 mcp23018_status = i2c_write(0b11111111 175 mcp23018_status = i2c_write(0b11111111
176 & ~(ergodox_left_led_2<<LEFT_LED_2_SHIFT) 176 & ~(ergodox_left_led_2<<LEFT_LED_2_SHIFT)
177 & ~(ergodox_left_led_1<<LEFT_LED_1_SHIFT) 177 & ~(ergodox_left_led_1<<LEFT_LED_1_SHIFT),
178 ); 178 ERGODOX_EZ_I2C_TIMEOUT);
179 if (mcp23018_status) goto out; 179 if (mcp23018_status) goto out;
180 180
181 out: 181 out:
182 i2c_stop(); 182 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
183 return mcp23018_status; 183 return mcp23018_status;
184} 184}
185#endif 185#endif
@@ -207,3 +207,130 @@ const keypos_t hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
207 {{0,0}, {1,0}, {2,0}, {3,0}, {4,0}, {5,0}}, 207 {{0,0}, {1,0}, {2,0}, {3,0}, {4,0}, {5,0}},
208}; 208};
209#endif 209#endif
210
211#ifdef RGB_MATRIX_ENABLE
212const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
213/* driver
214 * | R location
215 * | | G location
216 * | | | B location
217 * | | | | */
218 {0, C3_1, C2_1, C4_1}, // LED1 on right
219 {0, C6_1, C5_1, C7_1}, // LED2
220 {0, C4_2, C3_2, C5_2}, // LED3
221 {0, C7_2, C6_2, C8_2}, // LED4
222 {0, C2_3, C1_3, C3_3}, // LED5
223 {0, C5_3, C4_3, C6_3}, // LED6
224 {0, C8_3, C7_3, C9_3}, // LED7
225 {0, C2_4, C1_4, C3_4}, // LED8
226 {0, C6_4, C5_4, C7_4}, // LED9
227 {0, C2_5, C1_5, C3_5}, // LED10
228 {0, C7_5, C6_5, C8_5}, // LED11
229 {0, C2_6, C1_6, C3_6}, // LED12
230 {0, C5_6, C4_6, C6_6}, // LED13
231 {0, C8_6, C7_6, C9_6}, // LED14
232 {0, C2_7, C1_7, C3_7}, // LED15
233 {0, C5_7, C4_7, C6_7}, // LED16
234 {0, C2_8, C1_8, C3_8}, // LED17
235 {0, C5_8, C4_8, C6_8}, // LED18
236
237 {0, C3_9, C2_9, C4_9}, // LED19
238 {0, C6_9, C5_9, C7_9}, // LED20
239 {0, C4_10, C3_10, C5_10}, // LED21
240 {0, C7_10, C6_10, C8_10}, // LED22
241 {0, C2_11, C1_11, C3_11}, // LED23
242 {0, C5_11, C4_11, C6_11}, // LED24
243
244 {1, C3_1, C2_1, C4_1}, // LED1 on left
245 {1, C6_1, C5_1, C7_1}, // LED2
246 {1, C4_2, C3_2, C5_2}, // LED3
247 {1, C7_2, C6_2, C8_2}, // LED4
248 {1, C2_3, C1_3, C3_3}, // LED5
249 {1, C5_3, C4_3, C6_3}, // LED6
250 {1, C8_3, C7_3, C9_3}, // LED7
251 {1, C2_4, C1_4, C3_4}, // LED8
252 {1, C6_4, C5_4, C7_4}, // LED9
253 {1, C2_5, C1_5, C3_5}, // LED10
254 {1, C7_5, C6_5, C8_5}, // LED11
255 {1, C2_6, C1_6, C3_6}, // LED12
256 {1, C5_6, C4_6, C6_6}, // LED13
257 {1, C8_6, C7_6, C9_6}, // LED14
258 {1, C2_7, C1_7, C3_7}, // LED15
259 {1, C5_7, C4_7, C6_7}, // LED16
260 {1, C2_8, C1_8, C3_8}, // LED17
261 {1, C5_8, C4_8, C6_8}, // LED18
262
263 {1, C3_9, C2_9, C4_9}, // LED19
264 {1, C6_9, C5_9, C7_9}, // LED20
265 {1, C4_10, C3_10, C5_10}, // LED21
266 {1, C7_10, C6_10, C8_10}, // LED22
267 {1, C2_11, C1_11, C3_11}, // LED23
268 {1, C5_11, C4_11, C6_11} // LED24
269};
270
271
272const rgb_led g_rgb_leds[DRIVER_LED_TOTAL] = {
273
274 /*{row | col << 4}
275 | {x=0..224, y=0..64}
276 | | modifier
277 | | | */
278 {{0|(0<<4)}, {24.9*5, 16*0}, 0}, // LED 1 on right
279 {{0|(1<<4)}, {24.9*6, 16*0}, 0}, // LED 2
280 {{0|(2<<4)}, {24.9*7, 16*0}, 0}, // LED 3
281 {{0|(3<<4)}, {24.9*8, 16*0}, 0}, // LED 4
282 {{0|(4<<4)}, {24.9*9, 16*0}, 0}, // LED 5
283
284 {{1|(5<<4)}, {24.9*5, 16*1}, 0}, // LED 6
285 {{1|(6<<4)}, {24.9*6, 16*1}, 0}, // LED 7
286 {{1|(7<<4)}, {24.9*7, 16*1}, 0}, // LED 8
287 {{1|(8<<4)}, {24.9*8, 16*1}, 0}, // LED 9
288 {{1|(9<<4)}, {24.9*9, 16*1}, 0}, // LED 10
289
290 {{2|(5<<4)}, {24.9*5, 16*2}, 0}, // LED 11
291 {{2|(6<<4)}, {24.9*6, 16*2}, 0}, // LED 12
292 {{2|(7<<4)}, {24.9*7, 16*2}, 0}, // LED 13
293 {{2|(8<<4)}, {24.9*8, 16*2}, 0}, // LED 14
294 {{2|(9<<4)}, {24.9*9, 16*2}, 0}, // LED 15
295
296 {{3|(5<<4)}, {24.9*5, 16*2}, 0}, // LED 16
297 {{3|(6<<4)}, {24.9*6, 16*2}, 0}, // LED 17
298 {{3|(7<<4)}, {24.9*7, 16*2}, 0}, // LED 18
299 {{3|(8<<4)}, {24.9*8, 16*2}, 0}, // LED 19
300 {{3|(9<<4)}, {24.9*9, 16*2}, 0}, // LED 20
301
302 {{4|(6<<4)}, {24.9*6, 16*2}, 0}, // LED 21
303 {{4|(7<<4)}, {24.9*7, 16*2}, 0}, // LED 22
304 {{4|(8<<4)}, {24.9*8, 16*2}, 0}, // LED 23
305 {{4|(9<<4)}, {24.9*9, 16*2}, 0}, // LED 24
306
307 {{0|(0<<4)}, {24.9*4, 16*0}, 0}, // LED 1 on left
308 {{0|(1<<4)}, {24.9*3, 16*0}, 0}, // LED 2
309 {{0|(2<<4)}, {24.9*2, 16*0}, 0}, // LED 3
310 {{0|(3<<4)}, {24.9*1, 16*0}, 0}, // LED 4
311 {{0|(4<<4)}, {24.9*0, 16*0}, 0}, // LED 5
312
313 {{1|(5<<4)}, {24.9*4, 16*1}, 0}, // LED 6
314 {{1|(6<<4)}, {24.9*3, 16*1}, 0}, // LED 7
315 {{1|(7<<4)}, {24.9*2, 16*1}, 0}, // LED 8
316 {{1|(8<<4)}, {24.9*1, 16*1}, 0}, // LED 9
317 {{1|(9<<4)}, {24.9*0, 16*1}, 0}, // LED 10
318
319 {{2|(5<<4)}, {24.9*4, 16*2}, 0}, // LED 11
320 {{2|(6<<4)}, {24.9*3, 16*2}, 0}, // LED 12
321 {{2|(7<<4)}, {24.9*2, 16*2}, 0}, // LED 13
322 {{2|(8<<4)}, {24.9*1, 16*2}, 0}, // LED 14
323 {{2|(9<<4)}, {24.9*0, 16*2}, 0}, // LED 15
324
325 {{3|(5<<4)}, {24.9*4, 16*2}, 0}, // LED 16
326 {{3|(6<<4)}, {24.9*3, 16*2}, 0}, // LED 17
327 {{3|(7<<4)}, {24.9*2, 16*2}, 0}, // LED 18
328 {{3|(8<<4)}, {24.9*1, 16*2}, 0}, // LED 19
329 {{3|(9<<4)}, {24.9*0, 16*2}, 0}, // LED 20
330
331 {{4|(6<<4)}, {24.9*3, 16*2}, 0}, // LED 21
332 {{4|(7<<4)}, {24.9*2, 16*2}, 0}, // LED 22
333 {{4|(8<<4)}, {24.9*1, 16*2}, 0}, // LED 23
334 {{4|(9<<4)}, {24.9*0, 16*2}, 0}, // LED 24
335};
336#endif
diff --git a/keyboards/ergodox_ez/ergodox_ez.h b/keyboards/ergodox_ez/ergodox_ez.h
index 985dcfae5..383702b95 100644
--- a/keyboards/ergodox_ez/ergodox_ez.h
+++ b/keyboards/ergodox_ez/ergodox_ez.h
@@ -4,7 +4,7 @@
4#include "quantum.h" 4#include "quantum.h"
5#include <stdint.h> 5#include <stdint.h>
6#include <stdbool.h> 6#include <stdbool.h>
7#include "i2cmaster.h" 7#include "i2c_master.h"
8#include <util/delay.h> 8#include <util/delay.h>
9 9
10#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) 10#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
@@ -23,7 +23,8 @@
23#define OLATA 0x14 // output latch register 23#define OLATA 0x14 // output latch register
24#define OLATB 0x15 24#define OLATB 0x15
25 25
26extern uint8_t mcp23018_status; 26extern i2c_status_t mcp23018_status;
27#define ERGODOX_EZ_I2C_TIMEOUT 100
27 28
28void init_ergodox(void); 29void init_ergodox(void);
29void ergodox_blink_all_leds(void); 30void ergodox_blink_all_leds(void);
diff --git a/keyboards/ergodox_ez/matrix.c b/keyboards/ergodox_ez/matrix.c
index e10171133..2e95c83b6 100644
--- a/keyboards/ergodox_ez/matrix.c
+++ b/keyboards/ergodox_ez/matrix.c
@@ -34,7 +34,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
34#include "util.h" 34#include "util.h"
35#include "matrix.h" 35#include "matrix.h"
36#include QMK_KEYBOARD_H 36#include QMK_KEYBOARD_H
37#include "i2cmaster.h"
38#ifdef DEBUG_MATRIX_SCAN_RATE 37#ifdef DEBUG_MATRIX_SCAN_RATE
39#include "timer.h" 38#include "timer.h"
40#endif 39#endif
@@ -70,6 +69,7 @@ static void unselect_rows(void);
70static void select_row(uint8_t row); 69static void select_row(uint8_t row);
71 70
72static uint8_t mcp23018_reset_loop; 71static uint8_t mcp23018_reset_loop;
72// static uint16_t mcp23018_reset_loop;
73 73
74#ifdef DEBUG_MATRIX_SCAN_RATE 74#ifdef DEBUG_MATRIX_SCAN_RATE
75uint32_t matrix_timer; 75uint32_t matrix_timer;
@@ -177,6 +177,7 @@ uint8_t matrix_scan(void)
177{ 177{
178 if (mcp23018_status) { // if there was an error 178 if (mcp23018_status) { // if there was an error
179 if (++mcp23018_reset_loop == 0) { 179 if (++mcp23018_reset_loop == 0) {
180 // if (++mcp23018_reset_loop >= 1300) {
180 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans 181 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
181 // this will be approx bit more frequent than once per second 182 // this will be approx bit more frequent than once per second
182 print("trying to reset mcp23018\n"); 183 print("trying to reset mcp23018\n");
@@ -294,13 +295,14 @@ static matrix_row_t read_cols(uint8_t row)
294 return 0; 295 return 0;
295 } else { 296 } else {
296 uint8_t data = 0; 297 uint8_t data = 0;
297 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; 298 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
298 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out; 299 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
299 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out; 300 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
300 data = i2c_readNak(); 301 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
301 data = ~data; 302 data = ~((uint8_t)mcp23018_status);
303 mcp23018_status = I2C_STATUS_SUCCESS;
302 out: 304 out:
303 i2c_stop(); 305 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
304 return data; 306 return data;
305 } 307 }
306 } else { 308 } else {
@@ -349,11 +351,11 @@ static void select_row(uint8_t row)
349 } else { 351 } else {
350 // set active row low : 0 352 // set active row low : 0
351 // set other rows hi-Z : 1 353 // set other rows hi-Z : 1
352 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; 354 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
353 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out; 355 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
354 mcp23018_status = i2c_write(0xFF & ~(1<<row)); if (mcp23018_status) goto out; 356 mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
355 out: 357 out:
356 i2c_stop(); 358 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
357 } 359 }
358 } else { 360 } else {
359 // select on teensy 361 // select on teensy
diff --git a/keyboards/ergodox_ez/rules.mk b/keyboards/ergodox_ez/rules.mk
index 5ee9d5cb8..0e0b3cdef 100644
--- a/keyboards/ergodox_ez/rules.mk
+++ b/keyboards/ergodox_ez/rules.mk
@@ -15,8 +15,8 @@
15#---------------------------------------------------------------------------- 15#----------------------------------------------------------------------------
16 16
17# # project specific files 17# # project specific files
18SRC = twimaster.c \ 18SRC = matrix.c \
19 matrix.c 19 i2c_master.c
20 20
21# MCU name 21# MCU name
22MCU = atmega32u4 22MCU = atmega32u4
@@ -82,6 +82,7 @@ UNICODE_ENABLE = yes # Unicode
82SWAP_HANDS_ENABLE= yes # Allow swapping hands of keyboard 82SWAP_HANDS_ENABLE= yes # Allow swapping hands of keyboard
83SLEEP_LED_ENABLE = no 83SLEEP_LED_ENABLE = no
84API_SYSEX_ENABLE = no 84API_SYSEX_ENABLE = no
85RGBLIGHT_ENABLE = yes 85RGBLIGHT_ENABLE = no
86RGB_MATRIX_ENABLE = yes
86 87
87LAYOUTS = ergodox 88LAYOUTS = ergodox
diff --git a/quantum/quantum.c b/quantum/quantum.c
index cfa3df741..5abd222d1 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -854,7 +854,7 @@ void matrix_init_quantum() {
854 audio_init(); 854 audio_init();
855 #endif 855 #endif
856 #ifdef RGB_MATRIX_ENABLE 856 #ifdef RGB_MATRIX_ENABLE
857 rgb_matrix_init_drivers(); 857 rgb_matrix_init();
858 #endif 858 #endif
859 matrix_init_kb(); 859 matrix_init_kb();
860} 860}
diff --git a/quantum/rgb_matrix.c b/quantum/rgb_matrix.c
index b7424d637..b4bbc3dc0 100644
--- a/quantum/rgb_matrix.c
+++ b/quantum/rgb_matrix.c
@@ -105,7 +105,6 @@ void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t
105 } 105 }
106} 106}
107 107
108
109void rgb_matrix_update_pwm_buffers(void) { 108void rgb_matrix_update_pwm_buffers(void) {
110 IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); 109 IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
111 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); 110 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
@@ -119,7 +118,6 @@ void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
119 IS31FL3731_set_color_all( red, green, blue ); 118 IS31FL3731_set_color_all( red, green, blue );
120} 119}
121 120
122
123bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) { 121bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
124 if ( record->event.pressed ) { 122 if ( record->event.pressed ) {
125 uint8_t led[8], led_count; 123 uint8_t led[8], led_count;
@@ -222,7 +220,7 @@ void rgb_matrix_single_LED_test(void) {
222} 220}
223 221
224// All LEDs off 222// All LEDs off
225void rgb_matrix_all_off(void) { 223void rgb_matrix_all_off(void) {
226 rgb_matrix_set_color_all( 0, 0, 0 ); 224 rgb_matrix_set_color_all( 0, 0, 0 );
227} 225}
228 226
@@ -248,7 +246,7 @@ void rgb_matrix_solid_reactive(void) {
248 246
249// alphas = color1, mods = color2 247// alphas = color1, mods = color2
250void rgb_matrix_alphas_mods(void) { 248void rgb_matrix_alphas_mods(void) {
251 249
252 RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } ); 250 RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
253 RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } ); 251 RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
254 252
@@ -726,40 +724,44 @@ void rgb_matrix_indicators_user(void) {}
726// } 724// }
727// } 725// }
728 726
729void rgb_matrix_init_drivers(void) { 727void rgb_matrix_init(void) {
730 // Initialize TWI 728 rgb_matrix_setup_drivers();
731 i2c_init(); 729
732 IS31FL3731_init( DRIVER_ADDR_1 ); 730 // TODO: put the 1 second startup delay here?
733 IS31FL3731_init( DRIVER_ADDR_2 ); 731
734 732 // clear the key hits
735 for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) { 733 for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
736 bool enabled = true; 734 g_key_hit[led] = 255;
737 // This only caches it for later 735 }
738 IS31FL3731_set_led_control_register( index, enabled, enabled, enabled ); 736
739 } 737
740 // This actually updates the LED drivers 738 if (!eeconfig_is_enabled()) {
741 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); 739 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
742 740 eeconfig_init();
743 // TODO: put the 1 second startup delay here? 741 eeconfig_update_rgb_matrix_default();
744 742 }
745 // clear the key hits 743 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
746 for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) { 744 if (!rgb_matrix_config.mode) {
747 g_key_hit[led] = 255; 745 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
748 } 746 eeconfig_update_rgb_matrix_default();
749 747 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
750 748 }
751 if (!eeconfig_is_enabled()) { 749 eeconfig_debug_rgb_matrix(); // display current eeprom values
752 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n"); 750}
753 eeconfig_init(); 751
754 eeconfig_update_rgb_matrix_default(); 752void rgb_matrix_setup_drivers(void) {
755 } 753 // Initialize TWI
756 rgb_matrix_config.raw = eeconfig_read_rgb_matrix(); 754 i2c_init();
757 if (!rgb_matrix_config.mode) { 755 IS31FL3731_init( DRIVER_ADDR_1 );
758 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n"); 756 IS31FL3731_init( DRIVER_ADDR_2 );
759 eeconfig_update_rgb_matrix_default(); 757
760 rgb_matrix_config.raw = eeconfig_read_rgb_matrix(); 758 for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
761 } 759 bool enabled = true;
762 eeconfig_debug_rgb_matrix(); // display current eeprom values 760 // This only caches it for later
761 IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
762 }
763 // This actually updates the LED drivers
764 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
763} 765}
764 766
765// Deals with the messy details of incrementing an integer 767// Deals with the messy details of incrementing an integer
diff --git a/quantum/rgb_matrix.h b/quantum/rgb_matrix.h
index aaa85d5f5..576931400 100644
--- a/quantum/rgb_matrix.h
+++ b/quantum/rgb_matrix.h
@@ -95,7 +95,8 @@ void rgb_matrix_indicators_user(void);
95 95
96void rgb_matrix_single_LED_test(void); 96void rgb_matrix_single_LED_test(void);
97 97
98void rgb_matrix_init_drivers(void); 98void rgb_matrix_init(void);
99void rgb_matrix_setup_drivers(void);
99 100
100void rgb_matrix_set_suspend_state(bool state); 101void rgb_matrix_set_suspend_state(bool state);
101void rgb_matrix_set_indicator_state(uint8_t state); 102void rgb_matrix_set_indicator_state(uint8_t state);