2 Copyright 2016 flabbergast <s3+flabbergast@sdfeu.org>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * WF uses IS31FL3731C matrix LED driver from ISSI
21 * datasheet: http://www.issi.com/WW/pdf/31FL3731C.pdf
28 #include "led_controller.h"
35 - digits mean "row" and "col", i.e. 45 means C4-5 in the IS31 datasheet, matrix A
37 11 12 13 14 15 16 17 18 21 22 23 24 25 26 27*
38 28 31 32 33 34 35 36 37 38 41 42 43 44 45
39 46 47 48 51 52 53 54 55 56 57 58 61 62
40 63 64 65 66 67 68 71 72 73 74 75 76 77*
41 78 81 82 83 84 85 86 87
43 *Unused in Alphabet Layout
47 each page has 0xB4 bytes
48 0 - 0x11: LED control (on/off):
49 order: CA1, CB1, CA2, CB2, .... (CA - matrix A, CB - matrix B)
50 CAn controls Cn-8 .. Cn-1 (LSbit)
51 0x12 - 0x23: blink control (like "LED control")
52 0x24 - 0xB3: PWM control: byte per LED, 0xFF max on
53 order same as above (CA 1st row (8bytes), CB 1st row (8bytes), ...)
56 /* Which LED should be used for CAPS LOCK indicator
57 * The usual Caps Lock position is C4-6, so the address is
58 * 0x24 + (4-1)*0x10 + (8-1) = 0x59 */
59 #if !defined(CAPS_LOCK_LED_ADDRESS)
60 #define CAPS_LOCK_LED_ADDRESS 0x46
63 #if !defined(NUM_LOCK_LED_ADDRESS)
64 #define NUM_LOCK_LED_ADDRESS 0x85
67 /* Which LED should breathe during sleep */
68 #if !defined(BREATHE_LED_ADDRESS)
69 #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
74 * ================= */
75 static const I2CConfig i2ccfg = {
76 400000 // clock speed (Hz); 400kHz max for IS31
82 // internal communication buffers
83 uint8_t tx[2] __attribute__((aligned(2)));
84 uint8_t rx[1] __attribute__((aligned(2)));
86 // buffer for sending the whole page at once (used also as a temp buffer)
87 uint8_t full_page[0xB4+1] = {0};
89 // LED mask (which LEDs are present, selected by bits)
90 // See page comment above, control alternates CA matrix/CB matrix
91 // IC60 pcb uses only CA matrix.
92 // Each byte is a control pin for 8 leds ordered 8-1
93 const uint8_t is31_ic60_leds_mask[0x12] = {
94 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
95 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x00, 0x00
98 // array to hold brightness pwm steps
99 const uint8_t pwm_levels[5] = {
100 0x00, 0x16, 0x4E, 0xA1, 0xFF
103 // array to write to pwm register
104 uint8_t pwm_reg_array[9] = {0};
107 /* ============================
108 * communication functions
109 * ============================ */
110 msg_t is31_select_page(uint8_t page) {
111 tx[0] = IS31_COMMANDREGISTER;
113 return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
116 msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
117 is31_select_page(page);
118 return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, US2ST(IS31_TIMEOUT));
121 msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
122 is31_select_page(page);
125 return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
128 msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) {
129 is31_select_page(page);
132 return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
135 /* ========================
136 * initialise the IS31 chip
137 * ======================== */
138 void is31_init(void) {
139 // just to be sure that it's all zeroes
140 __builtin_memset(full_page,0,0xB4+1);
141 // zero function page, all registers (assuming full_page is all zeroes)
142 is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
143 // disable hardware shutdown
144 palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
145 palSetPad(GPIOB, 16);
146 chThdSleepMilliseconds(10);
148 is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
149 chThdSleepMilliseconds(10);
150 // TODO: This already done above, remove?
151 // zero function page, all registers
152 is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
153 chThdSleepMilliseconds(10);
154 // software shutdown disable (i.e. turn stuff on)
155 is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
156 chThdSleepMilliseconds(10);
157 // zero all LED registers on all 8 pages
160 is31_write_data(i, full_page, 0xB4 + 1);
161 chThdSleepMilliseconds(1);
165 /* ==================
167 * ================== */
168 #define LED_MAILBOX_NUM_MSGS 5
169 static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
170 mailbox_t led_mailbox;
171 static THD_WORKING_AREA(waLEDthread, 256);
172 static THD_FUNCTION(LEDthread, arg) {
174 chRegSetThreadName("LEDthread");
178 //persistent status variables
179 uint8_t backlight_status, lock_status, led_step, active_layer;
180 uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
183 uint8_t temp, msg_type, msg_led;
186 /* //control register variables
187 uint8_t page, save_page, save_breath1, save_breath2;
191 // initialize persistent variables
192 backlight_status = 0;
193 lock_status = 0;//TODO: does keyboard remember locks?
194 led_step = 4; //full brightness
198 // wait for a message (asynchronous)
199 // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
200 // be processed right away)
201 chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
202 msg_type = (msg >> 8) & 0xFF; //first byte is msg type
203 msg_led = (msg) & 0xFF; //second byte is action information
205 xprintf("--------------------\n");
206 xprintf("mailbox fetch\nmsg: %X\n", msg);
207 xprintf("type: %X - led: %X\n", msg_type, msg_led); //test if msg_type is 1 or 2 bytes after mask
210 //TODO: lighting key led on keypress
214 //TODO: toggle existing indicator off, or let user do this, but write frame 7 for every led change
215 //turn on single led, msg_led = row/col of led
216 set_led_bit(led_control_reg, msg_led, 1);
218 is31_write_data (7, led_control_reg, 0x12+1);
219 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 7);
221 is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
222 xprintf("page display: %X\n", temp);
226 xprintf("TOGGLE_ALL\n");
227 //msg_led = unused, TODO: consider using msg_led to toggle layer display
228 is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
230 xprintf("temp: %X\n", temp);
231 //if LED_ALL is on then toggle off, any other layer, turn on LED_ALL
233 xprintf("page display true: %X\n", temp);
234 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
236 xprintf("page display false: %X\n", temp);
237 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 1);
239 is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
240 xprintf("page display: %X\n", temp);
243 case TOGGLE_BACKLIGHT:
245 backlight_status ^= 1;
246 is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
249 page = backlight_status == 0 ? 0 : active_layer;
250 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, page);
253 case TOGGLE_LAYER_LEDS://show layer indicator or full map of layer keys.
254 //TODO: change so user can flag which they want, indiv or full map in fn_actions
255 //msg_led = layer to toggle on
256 is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
258 if(temp == msg_led) {
259 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 7);
262 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_led);
263 active_layer = msg_led;
267 case TOGGLE_LOCK_LED:
268 //msg_led = 0-3 for lock flags
269 lock_status ^= msg_led; //TODO: confirm toggling works and doesn't get out of sync
270 set_lock_leds(led_control_reg, lock_status);
275 case STEP_BRIGHTNESS:
276 //pwm_levels[] bounds checking, loop through array
277 //TODO: find a cleaner way to walk through this logic
292 //TODO: this seems a messy way to populate the pwm register
293 //populate the 9 byte rows to be written to each pin, first byte is register (pin) address
295 pwm_reg_array[i]=pwm_levels[led_step];
298 pwm_reg_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
299 is31_write_data(0, pwm_reg_array, 9);
300 chThdSleepMilliseconds(5);
304 /* case LED_MSG_SLEEP_LED_ON:
305 // save current settings
306 is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &save_page);
307 is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, &save_breath1);
308 is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, &save_breath2);
309 // use pages 7 and 8 for (hardware) breathing (assuming they're empty)
310 is31_write_register(6, BREATHE_LED_ADDRESS, 0xFF);
311 is31_write_register(7, BREATHE_LED_ADDRESS, 0x00);
312 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (6<<4)|6);
313 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
314 retval = MSG_TIMEOUT;
316 while(retval == MSG_TIMEOUT) {
317 // switch to the other page
318 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, temp);
319 temp = (temp == 6 ? 7 : 6);
320 // the times should be sufficiently long for IS31 to finish switching pages
321 retval = chMBFetch(&led_mailbox, &msg, MS2ST(temp == 6 ? 4000 : 6000));
323 // received a message (should be a wakeup), so restore previous state
324 chThdSleepMilliseconds(3000); // need to wait until the page change finishes
325 // note: any other messages are queued
326 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, save_breath1);
327 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, save_breath2);
328 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, save_page);
330 case LED_MSG_SLEEP_LED_OFF:
331 // should not get here; wakeup should be received in the branch above break;
334 //TODO: individual led state unchanged if page arrays are selected in code above
335 //avoidable if full pages are written on the fly
336 //or use pg8 for individual leds, have pointer to currently on led address for toggling
337 if (msg == 0x59 || msg == 0x84) {
338 //toggle lock keys on all layers
340 is31_read_register(0, msg, &temp);
341 pwm = (temp > 0x00 ? 0x00 : 0xFF);
342 is31_write_register(i,msg,pwm);
345 } else if(msg >= 0x24) {
346 xprintf("Power pre-read\ntemp: %X - msg: %X - pwm: %X\n", temp, msg, pwm);
347 is31_read_register(7, msg, &temp);
348 xprintf("Post-read\ntemp: %X - msg: %X - pwm: %X\n", temp, msg, pwm);
349 if (msg == active_led) {
351 pwm = (temp > 0x00 ? 0x00 : 0xFF);
353 //Use 8th led page for individual led indicators
354 is31_write_register(7, msg, pwm);
356 is31_write_register(7, active_led, 0x00);
357 is31_write_register(7, msg, 0xFF);
359 xprintf("Power post-change\ntemp: %X - msg: %X - pwm: %X\n", temp, msg, pwm);
360 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 7);
365 xprintf("--------------------\n");
370 /* ========================
372 * ======================== */
373 void set_led_bit (uint8_t *led_control_reg, uint8_t msg_led, uint8_t toggle_on) {
374 uint8_t row_byte, column_bit;
375 //msg_led tens column is pin#, A-control register is every other 8 bits
376 //ones column is bit position in 8-bit mask
377 //control register will be one bit shifted into position along register's full 0x12 bytes
378 ////first byte is register address 0x00
379 row_byte = ((msg_led / 10) % 10 - 1 ) * 2 + 1;
380 column_bit = 1<<(msg_led % 10 - 1);
381 xprintf("row %X\n", row_byte);
382 xprintf("col %X\n", column_bit);
385 led_control_reg[row_byte] |= 1<<(column_bit);
387 led_control_reg[row_byte] &= ~1<<(column_bit);
391 void set_lock_leds(uint8_t *led_control_reg, uint8_t lock_status) {
394 switch (lock_status) {
396 set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 1);//TODO: define lock addresses by matrix#, and loop for all frames
397 set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 0);
400 set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 0);
401 set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 1);
404 set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 1);
405 set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 1);
409 for(i=1; i<8; i++) { //keep LED_OFF layer all off, including locks
410 is31_write_data (i, led_control_reg, 0x12+1);
411 chThdSleepMilliseconds(5);
415 void write_led_page (uint8_t page, const uint8_t *led_array, uint8_t led_count) {
416 //TODO: init function that accepts array of led addresses and sets them by row
419 uint8_t temp_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
420 xprintf("-------------\n");
421 xprintf("write page %X\n", page);
423 for(i=0;i<led_count;i++){
424 row = ((led_array[i] / 10) % 10 - 1 ) * 2 + 1;//includes 1 byte shift for 0x00 address
425 col = 1<<(led_array[i] % 10 - 1);
427 temp_control_reg[row] |= 1<<(col);
430 is31_write_data(page, temp_control_reg, 0x13);
431 xprintf("-------------\n");
433 /* =====================
434 * hook into user keymap
435 * ===================== */
436 void led_controller_init(void) {
441 palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
442 palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
444 i2cStart(&I2CD1, &i2ccfg);
445 // try high drive (from kiibohd)
446 I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
447 // try glitch fixing (from kiibohd)
450 chThdSleepMilliseconds(10);
452 /* initialise IS31 chip */
455 //set Display Option Register so all pwm intensity is controlled from Frame 1
456 is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME);
458 /* set full pwm on Frame 1 */
460 pwm_reg_array[i]=0xFF;
463 pwm_reg_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
464 is31_write_data(0, pwm_reg_array, 9);
465 chThdSleepMilliseconds(5);
468 //set all led bits on for Frame 2 LEDS_ALL
470 __builtin_memcpy(full_page+1, is31_ic60_leds_mask, 0x12);
471 is31_write_data(1, full_page, 1+0x12);
473 /* enable breathing when the displayed page changes */
474 // Fade-in Fade-out, time = 26ms * 2^N, N=3
475 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
476 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
478 // clean up the lock LEDs
479 //TODO: adjust for new addressing and additional frames
480 //is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0);
481 //is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0);
483 /* more time consuming LED processing should be offloaded into
484 * a thread, with asynchronous messaging. */
485 chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
486 chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
489 //TODO: Don't know equivalent QMK hooks for these
491 //void hook_usb_suspend_entry(void) {
492 //#ifdef SLEEP_LED_ENABLE
493 // chSysLockFromISR();
494 // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_ON);
495 // chSysUnlockFromISR();
496 //#endif /* SLEEP_LED_ENABLE */
499 //void hook_usb_suspend_loop(void) {
500 // chThdSleepMilliseconds(100);
501 // /* Remote wakeup */
502 // if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
503 // send_remote_wakeup(&USB_DRIVER);
507 //void hook_usb_wakeup(void) {
508 //#ifdef SLEEP_LED_ENABLE
509 // chSysLockFromISR();
510 // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_OFF);
511 // chSysUnlockFromISR();
512 //#endif /* SLEEP_LED_ENABLE */