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 "action_layer.h"
31 #include "led_controller.h"
38 - digits mean "row" and "col", i.e. 45 means C4-5 in the IS31 datasheet, matrix A
40 11 12 13 14 15 16 17 18 21 22 23 24 25 26 27*
41 28 31 32 33 34 35 36 37 38 41 42 43 44 45
42 46 47 48 51 52 53 54 55 56 57 58 61 62
43 63 64 65 66 67 68 71 72 73 74 75 76 77*
44 78 81 82 83 84 85 86 87
46 *Unused in Alphabet Layout
50 each page has 0xB4 bytes
51 0 - 0x11: LED control (on/off):
52 order: CA1, CB1, CA2, CB2, .... (CA - matrix A, CB - matrix B)
53 CAn controls Cn-8 .. Cn-1 (LSbit)
54 0x12 - 0x23: blink control (like "LED control")
55 0x24 - 0xB3: PWM control: byte per LED, 0xFF max on
56 order same as above (CA 1st row (8bytes), CB 1st row (8bytes), ...)
59 /* Which LED should be used for CAPS LOCK indicator
60 * The usual Caps Lock position is C4-6, so the address is
61 * 0x24 + (4-1)*0x10 + (8-1) = 0x59 */
62 #if !defined(CAPS_LOCK_LED_ADDRESS)
63 #define CAPS_LOCK_LED_ADDRESS 46
66 #if !defined(NUM_LOCK_LED_ADDRESS)
67 #define NUM_LOCK_LED_ADDRESS 85
70 /* Which LED should breathe during sleep */
71 #if !defined(BREATHE_LED_ADDRESS)
72 #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
75 #define DEBUG_ENABLED 0
79 * ================= */
80 static const I2CConfig i2ccfg = {
81 400000 // clock speed (Hz); 400kHz max for IS31
87 // internal communication buffers
88 uint8_t tx[2] __attribute__((aligned(2)));
89 uint8_t rx[1] __attribute__((aligned(2)));
91 // buffer for sending the whole page at once (used also as a temp buffer)
92 uint8_t full_page[0xB4+1] = {0};
94 // LED mask (which LEDs are present, selected by bits)
95 // See page comment above, control alternates CA matrix/CB matrix
96 // IC60 pcb uses only CA matrix.
97 // Each byte is a control pin for 8 leds ordered 8-1
98 const uint8_t all_on_leds_mask[0x12] = {
99 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
100 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x00, 0x00
103 // array to hold brightness pwm steps
104 const uint8_t pwm_levels[5] = {
105 0x00, 0x16, 0x4E, 0xA1, 0xFF
108 // array to write to pwm register
109 uint8_t pwm_register_array[9] = {0};
112 /* ============================
113 * communication functions
114 * ============================ */
115 msg_t is31_select_page(uint8_t page) {
116 tx[0] = IS31_COMMANDREGISTER;
118 return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
121 msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
122 is31_select_page(page);
123 return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, US2ST(IS31_TIMEOUT));
126 msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
127 is31_select_page(page);
130 return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
133 msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) {
134 is31_select_page(page);
137 return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
140 /* ========================
141 * initialise the IS31 chip
142 * ======================== */
143 void is31_init(void) {
144 // just to be sure that it's all zeroes
145 __builtin_memset(full_page,0,0xB4+1);
146 // zero function page, all registers (assuming full_page is all zeroes)
147 is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
148 // disable hardware shutdown
149 palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
150 palSetPad(GPIOB, 16);
151 chThdSleepMilliseconds(10);
153 is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
154 chThdSleepMilliseconds(10);
155 // software shutdown disable (i.e. turn stuff on)
156 is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
157 chThdSleepMilliseconds(10);
158 // zero all LED registers on all 8 pages
161 is31_write_data(i, full_page, 0xB4 + 1);
162 chThdSleepMilliseconds(1);
166 /* ==================
168 * ================== */
169 #define LED_MAILBOX_NUM_MSGS 5
170 static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
171 mailbox_t led_mailbox;
172 static THD_WORKING_AREA(waLEDthread, 256);
173 static THD_FUNCTION(LEDthread, arg) {
175 chRegSetThreadName("LEDthread");
178 uint8_t control_register_word[2] = {0};//2 bytes: register address, byte to write
179 uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
181 //persistent status variables
182 uint8_t pwm_step_status, page_status;
185 uint8_t temp, msg_type, msg_led;
188 /* //control register variables
189 uint8_t page, save_page, save_breath1, save_breath2;
193 // initialize persistent variables
194 pwm_step_status = 4; //full brightness
195 page_status = 0; //start frame 0 (all off/on)
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 chThdSleepMilliseconds(10);
207 xprintf("mailbox fetch\nmsg: %X\n", msg);
208 chThdSleepMilliseconds(10);
209 xprintf("type: %X - led: %X\n", msg_type, msg_led);
210 chThdSleepMilliseconds(10);
214 //TODO: lighting key led on keypress
217 //TODO: custom page that is written using keypresses
218 //TODO: BLINK_ON/OFF_LED
221 //on/off/toggle single led, msg_led = row/col of led
222 xprintf("OFF_LED\n");
223 chThdSleepMilliseconds(10);
224 set_led_bit(7, control_register_word, msg_led, 0);
225 is31_write_data (7, control_register_word, 0x02);
230 chThdSleepMilliseconds(10);
231 set_led_bit(7, control_register_word, msg_led, 1);
232 is31_write_data (7, control_register_word, 0x02);
236 xprintf("TOGGLE_LED\n");
237 chThdSleepMilliseconds(10);
238 set_led_bit(7, control_register_word, msg_led, 2);
239 is31_write_data (7, control_register_word, 0x02);
243 xprintf("TOGGLE_ALL\n");
244 chThdSleepMilliseconds(10);
246 is31_read_register(0, 0x00, &temp);//if first byte is on, then toggle frame 0 off
247 led_control_reg[0] = 0;
248 if (temp==0 || page_status > 0) {
249 xprintf("all leds on");
250 chThdSleepMilliseconds(10);
251 __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
253 xprintf("all leds off");
254 chThdSleepMilliseconds(10);
255 __builtin_memset(led_control_reg+1, 0, 0x12);
257 is31_write_data(0, led_control_reg, 0x13);
259 if (page_status > 0) {
260 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
264 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) {
265 set_lock_leds(USB_LED_NUM_LOCK, 1);
267 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
268 set_lock_leds(USB_LED_CAPS_LOCK, 1);
274 case TOGGLE_BACKLIGHT:
276 xprintf("TOGGLE_BACKLIGHT\n");
277 chThdSleepMilliseconds(10);
279 //populate the 9 byte rows to be written to each pin, first byte is register (pin) address
281 __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
283 __builtin_memset(pwm_register_array+1, 0, 8);
287 //first byte is register address, every 0x10 9 bytes is A-register pwm pins
288 pwm_register_array[0] = 0x24 + (i * 0x10);
289 is31_write_data(0,pwm_register_array,9);
293 case DISPLAY_PAGE://show single layer indicator or full map of layer
294 //msg_led = page to toggle on
295 xprintf("DISPLAY_PAGE\n");
296 chThdSleepMilliseconds(10);
297 if (page_status != msg_led) {
298 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_led);
300 page_status = msg_led;
304 //led_msg = page to reset
305 led_control_reg[0] = 0;
306 __builtin_memset(led_control_reg+1, 0, 0x12);
307 is31_write_data(msg_led, led_control_reg, 0x13);
310 if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) {
311 set_lock_leds(USB_LED_NUM_LOCK, 1);
313 if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
314 set_lock_leds(USB_LED_CAPS_LOCK, 1);
318 case TOGGLE_NUM_LOCK:
319 //msg_led = 0 or 1, off/on
320 set_lock_leds(USB_LED_NUM_LOCK, msg_led);
323 case TOGGLE_CAPS_LOCK:
324 //msg_led = 0 or 1, off/on
325 set_lock_leds(USB_LED_CAPS_LOCK, msg_led);
331 case STEP_BRIGHTNESS:
332 xprintf("TOGGLE_BACKLIGHT\n");
333 chThdSleepMilliseconds(10);
334 //led_msg = step pwm up or down
337 if (pwm_step_status == 0) {
345 if (pwm_step_status == 4) {
353 //populate 8 byte rows to write on each pin
354 //first byte is register address, every 0x10 9 bytes are A-register pwm pins
355 __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
358 pwm_register_array[0] = 0x24 + (i * 0x10);
359 is31_write_data(0,pwm_register_array,9);
363 /* case LED_MSG_SLEEP_LED_ON:
364 // save current settings
365 is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &save_page);
366 is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, &save_breath1);
367 is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, &save_breath2);
368 // use pages 7 and 8 for (hardware) breathing (assuming they're empty)
369 is31_write_register(6, BREATHE_LED_ADDRESS, 0xFF);
370 is31_write_register(7, BREATHE_LED_ADDRESS, 0x00);
371 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (6<<4)|6);
372 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
373 retval = MSG_TIMEOUT;
375 while(retval == MSG_TIMEOUT) {
376 // switch to the other page
377 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, temp);
378 temp = (temp == 6 ? 7 : 6);
379 // the times should be sufficiently long for IS31 to finish switching pages
380 retval = chMBFetch(&led_mailbox, &msg, MS2ST(temp == 6 ? 4000 : 6000));
382 // received a message (should be a wakeup), so restore previous state
383 chThdSleepMilliseconds(3000); // need to wait until the page change finishes
384 // note: any other messages are queued
385 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, save_breath1);
386 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, save_breath2);
387 is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, save_page);
389 case LED_MSG_SLEEP_LED_OFF:
390 // should not get here; wakeup should be received in the branch above break;
393 xprintf("--------------------\n");
394 chThdSleepMilliseconds(10);
398 //debugging code - print full led/blink/pwm registers on each frame
399 xprintf("----layer state----: %X\n", layer_state);
401 xprintf("page: %d", i);
402 chThdSleepMilliseconds(2);
404 is31_read_register(i,j,&temp);
405 chThdSleepMilliseconds(2);
406 xprintf("%02X, ", temp);
414 chThdSleepMilliseconds(1);
422 /* ==============================
423 * led processing functions
424 * ============================== */
426 void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint8_t action) {
427 //returns 2 bytes led control register address and byte to write
429 uint8_t control_reg_addr, column_bit, column_byte, temp;
430 //check for valid led address
431 if (led_addr < 0 || led_addr > 90 || led_addr % 10 > 8) {
432 xprintf("Invalid address: %d\n", led_addr);
436 //first byte is led control register address 0x00
437 //msg_led tens column is pin#, ones column is bit position in 8-bit mask
438 control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-register is every other byte
439 column_bit = 1<<(led_addr % 10 - 1);
441 is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte
446 column_byte &= ~column_bit;
449 column_byte |= column_bit;
452 column_byte ^= column_bit;
456 //return word to be written in register
457 led_control_reg[0] = control_reg_addr;
458 led_control_reg[1] = column_byte;
461 void set_lock_leds(uint8_t lock_type, uint8_t led_on) {
462 uint8_t page, led_addr, start, temp;
463 uint8_t led_control_word[2] = {0};
464 //TODO: this function call could send led address vs lock_type.
465 //however, the switch/case allows for additional steps, like audio, depending on type
469 case USB_LED_NUM_LOCK:
470 led_addr = NUM_LOCK_LED_ADDRESS;
472 case USB_LED_CAPS_LOCK:
473 led_addr = CAPS_LOCK_LED_ADDRESS;
475 #ifdef SCROLL_LOCK_LED_ADDRESS
476 case USB_LED_SCROLL_LOCK:
477 led_addr = SCROLL_LOCK_LED_ADDRESS;
480 #ifdef COMPOSE_LED_ADDRESS
481 case USB_LED_COMPOSE:
482 led_addr = COMPOSE_LED_ADDRESS;
485 #ifdef SCROLL_LOCK_LED_ADDRESS
487 led_addr = KANA_LED_ADDRESS;
492 //ignore frame0 if all leds are on or if option set in led_controller.h
493 //TODO: blink of all leds are on, clear blink register if not
494 is31_read_register(0, 0x00, &temp);
495 led_addr += temp == 0 ? 0 : 0x12;//send bit to blink register instead
496 start = BACKLIGHT_OFF_LOCK_LED_OFF ? 1 : 0;
498 for(page=start; page<8; page++) {
499 set_led_bit(page,led_control_word,led_addr,led_on);
500 is31_write_data(page, led_control_word, 0x02);
504 void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
507 uint8_t led_control_register[0x13] = {0};//led control register start address + 0x12 bytes
509 __builtin_memset(led_control_register,0,13);
511 for(i=0;i<led_count;i++){
512 row = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;// 1 byte shift for led register 0x00 address
513 col = user_led_array[i] % 10 - 1;
515 led_control_register[row] |= 1<<(col);
518 is31_write_data(page, led_control_register, 0x13);
521 /* =====================
522 * hook into user keymap
523 * ===================== */
524 void led_controller_init(void) {
529 palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
530 palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
532 i2cStart(&I2CD1, &i2ccfg);
533 // try high drive (from kiibohd)
534 I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
535 // try glitch fixing (from kiibohd)
538 chThdSleepMilliseconds(10);
540 /* initialise IS31 chip */
543 //set Display Option Register so all pwm intensity is controlled from Frame 0
544 is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME);
546 /* set full pwm on Frame 1 */
547 pwm_register_array[0] = 0;
548 __builtin_memset(pwm_register_array+1, 0xFF, 8);
550 pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
551 is31_write_data(0, pwm_register_array, 9);
552 chThdSleepMilliseconds(5);
555 /* enable breathing when the displayed page changes */
556 // Fade-in Fade-out, time = 26ms * 2^N, N=3
557 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
558 is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
560 // clean up the lock LEDs
561 set_lock_leds(USB_LED_NUM_LOCK, 0);
562 set_lock_leds(USB_LED_CAPS_LOCK, 0);
564 /* more time consuming LED processing should be offloaded into
565 * a thread, with asynchronous messaging. */
566 chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
567 chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
570 //TODO: Don't know equivalent QMK hooks for these
572 //void hook_usb_suspend_entry(void) {
573 //#ifdef SLEEP_LED_ENABLE
574 // chSysLockFromISR();
575 // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_ON);
576 // chSysUnlockFromISR();
577 //#endif /* SLEEP_LED_ENABLE */
580 //void hook_usb_suspend_loop(void) {
581 // chThdSleepMilliseconds(100);
582 // /* Remote wakeup */
583 // if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
584 // send_remote_wakeup(&USB_DRIVER);
588 //void hook_usb_wakeup(void) {
589 //#ifdef SLEEP_LED_ENABLE
590 // chSysLockFromISR();
591 // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_OFF);
592 // chSysUnlockFromISR();
593 //#endif /* SLEEP_LED_ENABLE */