]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/infinity60/led_controller.c
5c177d26b0fb8086cafba3ae1f97245793f930b2
[qmk_firmware.git] / keyboards / infinity60 / led_controller.c
1 /*
2 Copyright 2016 flabbergast <s3+flabbergast@sdfeu.org>
3
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.
8
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.
13
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/>.
16 */
17
18 /*
19  * LED controller code
20  * IS31FL3731C matrix LED driver from ISSI
21  * datasheet: http://www.issi.com/WW/pdf/31FL3731C.pdf
22  */
23
24 #include "ch.h"
25 #include "hal.h"
26 #include "print.h"
27 #include "led.h"
28 #include "host.h"
29
30 #include "led_controller.h"
31
32 #include "suspend.h"
33
34 #include "usb_main.h"
35
36 /* Infinity60 LED MAP
37     - digits mean "row" and "col", i.e. 45 means C4-5 in the IS31 datasheet, matrix A
38
39   11 12 13 14 15 16 17 18 21 22 23 24 25  26 27*
40    28 31 32 33 34 35 36 37 38 41 42 43 44 45
41    46 47 48 51 52 53 54 55 56 57 58 61    62
42     63 64 65 66 67 68 71 72 73 74 75      76 77*
43   78  81  82       83         84  85  86  87
44
45 *Unused in Alphabet Layout
46 */
47
48 /*
49   each page has 0xB4 bytes
50   0 - 0x11: LED control (on/off):
51     order: CA1, CB1, CA2, CB2, .... (CA - matrix A, CB - matrix B)
52       CAn controls Cn-8 .. Cn-1 (LSbit)
53   0x12 - 0x23: blink control (like "LED control")
54   0x24 - 0xB3: PWM control: byte per LED, 0xFF max on
55     order same as above (CA 1st row (8bytes), CB 1st row (8bytes), ...)
56 */
57
58 // Which LED should be used for CAPS LOCK indicator
59 #if !defined(CAPS_LOCK_LED_ADDRESS)
60 #define CAPS_LOCK_LED_ADDRESS 46
61 #endif
62
63 #if !defined(NUM_LOCK_LED_ADDRESS)
64 #define NUM_LOCK_LED_ADDRESS 85
65 #endif
66
67 /* Which LED should breathe during sleep */
68 #if !defined(BREATHE_LED_ADDRESS)
69 #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
70 #endif
71
72 /* =================
73  * ChibiOS I2C setup
74  * ================= */
75 static const I2CConfig i2ccfg = {
76   400000 // clock speed (Hz); 400kHz max for IS31
77 };
78
79 /* ==============
80  *   variables
81  * ============== */
82 // internal communication buffers
83 uint8_t tx[2] __attribute__((aligned(2)));
84 uint8_t rx[1] __attribute__((aligned(2)));
85
86 // buffer for sending the whole page at once (used also as a temp buffer)
87 uint8_t full_page[0xB4+1] = {0};
88
89 // LED mask (which LEDs are present, selected by bits)
90 // IC60 pcb uses only CA matrix.
91 // Each byte is a control pin for 8 leds ordered 8-1
92 const uint8_t all_on_leds_mask[0x12] = {
93   0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
94   0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x00, 0x00
95 };
96
97 // array to hold brightness pwm steps
98 const uint8_t pwm_levels[5] = {
99     0x00, 0x16, 0x4E, 0xA1, 0xFF
100 };
101
102 // array to write to pwm register
103 uint8_t pwm_register_array[9] = {0};
104
105
106 /* ============================
107  *   communication functions
108  * ============================ */
109 msg_t is31_select_page(uint8_t page) {
110   tx[0] = IS31_COMMANDREGISTER;
111   tx[1] = page;
112   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
113 }
114
115 msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
116   is31_select_page(page);
117   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, US2ST(IS31_TIMEOUT));
118 }
119
120 msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
121   is31_select_page(page);
122   tx[0] = reg;
123   tx[1] = data;
124   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
125 }
126
127 msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) {
128   is31_select_page(page);
129
130   tx[0] = reg;
131   return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
132 }
133
134 /* ========================
135  * initialise the IS31 chip
136  * ======================== */
137 void is31_init(void) {
138       xprintf("_is31_init\n");
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);
147   // software shutdown
148   is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
149   chThdSleepMilliseconds(10);
150   // zero function page, all registers
151   is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
152   chThdSleepMilliseconds(10);
153   // software shutdown disable (i.e. turn stuff on)
154   is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
155   chThdSleepMilliseconds(10);
156   // zero all LED registers on all 8 pages
157   uint8_t i;
158   for(i=0; i<8; i++) {
159     is31_write_data(i, full_page, 0xB4 + 1);
160     chThdSleepMilliseconds(5);
161   }
162 }
163
164 /* ==================
165  * LED control thread
166  * ================== */
167 #define LED_MAILBOX_NUM_MSGS 5
168 static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
169 mailbox_t led_mailbox;
170 static THD_WORKING_AREA(waLEDthread, 256);
171 static THD_FUNCTION(LEDthread, arg) {
172   (void)arg;
173   chRegSetThreadName("LEDthread");
174
175   uint8_t i;
176   uint8_t control_register_word[2] = {0};//2 bytes: register address, byte to write
177   uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
178
179   //persistent status variables
180   uint8_t pwm_step_status, page_status, capslock_status, numlock_status;
181
182   //mailbox variables
183   uint8_t temp, msg_type;
184   uint8_t msg_args[3];
185   msg_t msg;
186
187   // initialize persistent variables
188   pwm_step_status = 4; //full brightness
189   page_status = 0; //start frame 0 (all off/on)
190   numlock_status = (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? 1 : 0;
191   capslock_status = (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) ? 1 : 0;
192
193   while(true) {
194     // wait for a message (asynchronous)
195     // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
196     //  be processed right away
197     chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
198     msg_type = msg & 0xFF; //first byte is action information
199     msg_args[0] = (msg >> 8) & 0xFF;
200     msg_args[1] = (msg >> 16) & 0XFF;
201     msg_args[2] = (msg >> 24) & 0xFF;
202
203       xprintf("msg_type: %d-%d-%d\n", msg_type, msg_args[0], msg_args[1]);
204
205     switch (msg_type){
206       case SET_FULL_ROW:
207       xprintf("FULL ROW: %d-%d\n", msg_args[0], msg_args[1]);
208       //write full byte to pin address, msg_args[1] = pin #, msg_args[0] = 8 bits to write
209       //writes only to currently displayed page
210         write_led_byte(page_status, msg_args[1], msg_args[0]);
211         break;
212
213       case OFF_LED:
214       xprintf("OFF: %d-%d\n", msg_args[0], msg_args[1]);
215       //on/off/toggle single led, msg_args[0] = row/col of led, msg_args[1] = page
216         set_led_bit(msg_args[1], control_register_word, msg_args[0], 0);
217         break;
218       case ON_LED:
219       xprintf("ON: %d-%d\n", msg_args[0], msg_args[1]);
220         set_led_bit(msg_args[1], control_register_word, msg_args[0], 1);
221         break;
222       case TOGGLE_LED:
223       xprintf("TOGGLE: %d-%d\n", msg_args[0], msg_args[1]);
224         set_led_bit(msg_args[1], control_register_word, msg_args[0], 2);
225         break;
226
227       case BLINK_OFF_LED:
228       xprintf("B_on: %d-%d\n", msg_args[0], msg_args[1]);
229       //on/off/toggle single led, msg_args[0] = row/col of led
230         set_led_bit(msg_args[1], control_register_word, msg_args[0], 4);
231         break;
232       case BLINK_ON_LED:
233       xprintf("B_off: %d-%d\n", msg_args[0], msg_args[1]);
234         set_led_bit(msg_args[1], control_register_word, msg_args[0], 5);
235         break;
236       case BLINK_TOGGLE_LED:
237       xprintf("B_togg: %d-%d\n", msg_args[0], msg_args[1]);
238         set_led_bit(msg_args[1], control_register_word, msg_args[0], 6);
239         break;
240
241       case TOGGLE_ALL:
242       //turn on/off all leds, msg_args = unused
243         is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
244         chThdSleepMilliseconds(5);
245         is31_read_register(0, 0x00, &temp);
246         is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
247       xprintf("TOGGLE_ALL: %d-%d\n", msg_args[0], msg_args[1]);
248       xprintf("temp: %d\n", temp);
249
250         led_control_reg[0] = 0;
251
252         //if first leds are already on, toggle frame 0 off
253         if (temp==0 || page_status > 0) {
254           __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
255         } else {
256           __builtin_memset(led_control_reg+1, 0, 0x12);
257         }
258         is31_write_data(0, led_control_reg, 0x13);
259
260         if (page_status > 0) {
261           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
262
263           page_status=0;
264
265           //maintain lock leds
266           led_set(host_keyboard_leds());
267         }
268         break;
269
270       case TOGGLE_BACKLIGHT:
271       xprintf("TOGGLE_BKLT: %d-%d\n", msg_args[0], msg_args[1]);
272         //msg_args[0] = on/off
273
274         //populate 9 byte rows to be written to each pin, first byte is register (pin) address
275         if (msg_args[0] == 1) {
276           __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
277         } else {
278           __builtin_memset(pwm_register_array+1, 0, 8);
279         }
280
281         for(i=0; i<8; i++) {
282         //first byte is register address, every 0x10 9 bytes is A-matrix pwm pins
283           pwm_register_array[0] = 0x24 + (i * 0x10);
284           is31_write_data(0,pwm_register_array,9);
285         }
286         break;
287
288       case DISPLAY_PAGE:
289       //msg_args[0] = page to toggle on
290       xprintf("DSPY_PG: %d-%d\n", msg_args[0], msg_args[1]);
291         if (page_status != msg_args[0]) {
292           is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_args[0]);
293           page_status = msg_args[0];
294
295           //maintain lock leds
296           led_set(host_keyboard_leds());
297         }
298         break;
299
300       case RESET_PAGE:
301       //led_args[0] = page to reset
302         led_control_reg[0] = 0;
303         __builtin_memset(led_control_reg+1, 0, 0x12);
304         is31_write_data(msg_args[0], led_control_reg, 0x13);
305
306         //repeat for blink register
307         led_control_reg[0] = 0x12;
308         is31_write_data(msg_args[0], led_control_reg, 0x13);
309         break;
310
311       case TOGGLE_NUM_LOCK:
312       xprintf("NMLK: %d-%d\n", msg_args[0], msg_args[1]);
313       //msg_args[0] = 0 or 1, off/on
314         if (numlock_status != msg_args[0]) {
315           set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_args[0], page_status);
316           numlock_status = msg_args[0];
317         }
318         break;
319       case TOGGLE_CAPS_LOCK:
320       xprintf("CPLK: %d-%d\n", msg_args[0], msg_args[1]);
321       //msg_args[0] = 0 or 1, off/on
322         if (capslock_status != msg_args[0]) {
323           set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_args[0], page_status);
324           capslock_status = msg_args[0];
325         }
326         break;
327
328       case STEP_BRIGHTNESS:
329       xprintf("Step: %d-%d\n", msg_args[0], msg_args[1]);
330       //led_args[0] = step up (1) or down (0)
331         switch (msg_args[0]) {
332           case 0:
333             if (pwm_step_status == 0) {
334               pwm_step_status = 4;
335             } else {
336               pwm_step_status--;
337             }
338             break;
339
340           case 1:
341             if (pwm_step_status == 4) {
342               pwm_step_status = 0;
343             } else {
344               pwm_step_status++;
345             }
346             break;
347         }
348
349         //populate 8 byte arrays to write on each pin
350         //first byte is register address, every 0x10 9 bytes are A-matrix pwm pins
351         __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
352
353         for(i=0; i<8; i++) {
354           pwm_register_array[0] = 0x24 + (i * 0x10);
355           is31_write_data(0,pwm_register_array,9);
356         }
357         break;
358     }
359   }
360 }
361
362 /* ==============================
363  *    led processing functions
364  * ============================== */
365
366 void set_led_bit (uint8_t page, uint8_t *led_control_word, uint8_t led_addr, uint8_t action) {
367   //returns 2 bytes: led control register address and byte to write
368   //action: 0 - off, 1 - on, 2 - toggle, 4 - blink on, 5 - blink off, 6 - toggle blink
369
370   uint8_t control_reg_addr, column_bit, column_byte, temp, blink_bit;
371
372   //check for valid led address
373   if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) {
374     return;
375   }
376       xprintf("_set action-led: %x-%d\n", action, led_addr);
377
378   blink_bit = action>>2;//check for blink bit
379   action &= ~(1<<2); //strip blink bit
380
381   //led_addr tens column is pin#, ones column is bit position in 8-bit mask
382   control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-matrix is every other byte
383   control_reg_addr += blink_bit == 1 ? 0x12 : 0x00;//if blink_bit, shift 12 bytes to blink register
384       xprintf("_set control address: %x\n", control_reg_addr);
385
386   is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
387   chThdSleepMilliseconds(5);
388   is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte
389   is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
390       xprintf("_set temp_byte_mask: %x\n", temp);
391
392   column_bit = 1<<(led_addr % 10 - 1);
393   column_byte = temp;
394       xprintf("_set col_byte_mask: %x\n", column_byte);
395
396   switch(action) {
397     case 0:
398       column_byte &= ~column_bit;
399       break;
400     case 1:
401       column_byte |= column_bit;
402       break;
403     case 2:
404       column_byte ^= column_bit;
405       break;
406   }
407
408   //return word to be written in register
409   led_control_word[0] = control_reg_addr;
410   led_control_word[1] = column_byte;
411   is31_write_data (page, led_control_word, 0x02);
412 }
413
414 void write_led_byte (uint8_t page, uint8_t row, uint8_t led_byte) {
415   uint8_t led_control_word[2] = {0};//register address and on/off byte
416
417   led_control_word[0] = (row - 1 ) * 0x02;// A-matrix is every other byte
418   led_control_word[1] = led_byte;
419   is31_write_data(page, led_control_word, 0x02);
420 }
421
422 void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
423   uint8_t i;
424   uint8_t pin, col;
425   uint8_t led_control_register[0x13] = {0};
426
427   __builtin_memset(led_control_register,0,13);
428
429   for(i=0;i<led_count;i++){
430     //shift pin by 1 for led register 0x00 address
431     pin = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;
432     col = user_led_array[i] % 10 - 1;
433     led_control_register[pin] |= 1<<(col);
434   }
435
436   is31_write_data(page, led_control_register, 0x13);
437 }
438
439 void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) {
440   uint8_t temp;
441   uint8_t led_control_word[2] = {0};
442
443   //blink if all leds are on
444   if (page == 0) {
445     is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
446     chThdSleepMilliseconds(5);
447     is31_read_register(0, 0x00, &temp);
448     is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
449
450     if (temp == 0xFF) {
451       led_action |= (1<<2); //set blink bit
452     }
453   }
454       xprintf("_lock action: %d\n", led_action);
455
456   set_led_bit(page,led_control_word,led_addr,led_action);
457 }
458
459 /* =====================
460  * hook into user keymap
461  * ===================== */
462 void led_controller_init(void) {
463   uint8_t i;
464
465       xprintf("led_init\n");
466   /* initialise I2C */
467   /* I2C pins */
468   palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
469   palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
470   /* start I2C */
471   i2cStart(&I2CD1, &i2ccfg);
472   // try high drive (from kiibohd)
473   I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
474   // try glitch fixing (from kiibohd)
475   I2CD1.i2c->FLT = 4;
476
477   chThdSleepMilliseconds(10);
478
479   /* initialise IS31 chip */
480   is31_init();
481
482   //set Display Option Register so all pwm intensity is controlled from page 0
483   //enable blink and set blink period to 0.27s x rate
484   is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME + IS31_REG_DISPLAYOPT_BLINK_ENABLE + 4);
485
486   /* set full pwm on page 1 */
487   pwm_register_array[0] = 0;
488   __builtin_memset(pwm_register_array+1, 0xFF, 8);
489   for(i=0; i<8; i++) {
490     pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
491     is31_write_data(0, pwm_register_array, 9);
492     chThdSleepMilliseconds(5);
493   }
494
495   /* enable breathing when the displayed page changes */
496   // Fade-in Fade-out, time = 26ms * 2^N, N=3
497   is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
498   is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
499
500   /* more time consuming LED processing should be offloaded into
501    * a thread, with asynchronous messaging. */
502   chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
503   chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
504 }