led_controller.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. Copyright 2016 flabbergast <s3+flabbergast@sdfeu.org>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. * LED controller code
  16. * WF uses IS31FL3731C matrix LED driver from ISSI
  17. * datasheet: http://www.issi.com/WW/pdf/31FL3731C.pdf
  18. */
  19. #include "ch.h"
  20. #include "hal.h"
  21. #include "print.h"
  22. #include "led_controller.h"
  23. #include "suspend.h"
  24. #include "usb_main.h"
  25. /* Infinity60 LED MAP
  26. - digits mean "row" and "col", i.e. 45 means C4-5 in the IS31 datasheet, matrix A
  27. 11 12 13 14 15 16 17 18 21 22 23 24 25 26 27*
  28. 28 31 32 33 34 35 36 37 38 41 42 43 44 45
  29. 46 47 48 51 52 53 54 55 56 57 58 61 62
  30. 63 64 65 66 67 68 71 72 73 74 75 76 77*
  31. 78 81 82 83 84 85 86 87
  32. *Unused in Alphabet Layout
  33. */
  34. /*
  35. each page has 0xB4 bytes
  36. 0 - 0x11: LED control (on/off):
  37. order: CA1, CB1, CA2, CB2, .... (CA - matrix A, CB - matrix B)
  38. CAn controls Cn-8 .. Cn-1 (LSbit)
  39. 0x12 - 0x23: blink control (like "LED control")
  40. 0x24 - 0xB3: PWM control: byte per LED, 0xFF max on
  41. order same as above (CA 1st row (8bytes), CB 1st row (8bytes), ...)
  42. */
  43. /* Which LED should be used for CAPS LOCK indicator
  44. * The usual Caps Lock position is C4-6, so the address is
  45. * 0x24 + (4-1)*0x10 + (8-1) = 0x59 */
  46. #if !defined(CAPS_LOCK_LED_ADDRESS)
  47. #define CAPS_LOCK_LED_ADDRESS 0x46
  48. #endif
  49. #if !defined(NUM_LOCK_LED_ADDRESS)
  50. #define NUM_LOCK_LED_ADDRESS 0x85
  51. #endif
  52. /* Which LED should breathe during sleep */
  53. #if !defined(BREATHE_LED_ADDRESS)
  54. #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
  55. #endif
  56. /* =================
  57. * ChibiOS I2C setup
  58. * ================= */
  59. static const I2CConfig i2ccfg = {
  60. 400000 // clock speed (Hz); 400kHz max for IS31
  61. };
  62. /* ==============
  63. * variables
  64. * ============== */
  65. // internal communication buffers
  66. uint8_t tx[2] __attribute__((aligned(2)));
  67. uint8_t rx[1] __attribute__((aligned(2)));
  68. // buffer for sending the whole page at once (used also as a temp buffer)
  69. uint8_t full_page[0xB4+1] = {0};
  70. // LED mask (which LEDs are present, selected by bits)
  71. // See page comment above, control alternates CA matrix/CB matrix
  72. // IC60 pcb uses only CA matrix.
  73. // Each byte is a control pin for 8 leds ordered 8-1
  74. const uint8_t is31_ic60_leds_mask[0x12] = {
  75. 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
  76. 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x00, 0x00
  77. };
  78. // array to hold brightness pwm steps
  79. const uint8_t pwm_levels[5] = {
  80. 0x00, 0x16, 0x4E, 0xA1, 0xFF
  81. };
  82. // array to write to pwm register
  83. uint8_t pwm_register_array[9] = {0};
  84. /* ============================
  85. * communication functions
  86. * ============================ */
  87. msg_t is31_select_page(uint8_t page) {
  88. tx[0] = IS31_COMMANDREGISTER;
  89. tx[1] = page;
  90. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  91. }
  92. msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
  93. is31_select_page(page);
  94. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, US2ST(IS31_TIMEOUT));
  95. }
  96. msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
  97. is31_select_page(page);
  98. tx[0] = reg;
  99. tx[1] = data;
  100. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  101. }
  102. msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) {
  103. is31_select_page(page);
  104. tx[0] = reg;
  105. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
  106. }
  107. /* ========================
  108. * initialise the IS31 chip
  109. * ======================== */
  110. void is31_init(void) {
  111. // just to be sure that it's all zeroes
  112. __builtin_memset(full_page,0,0xB4+1);
  113. // zero function page, all registers (assuming full_page is all zeroes)
  114. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  115. // disable hardware shutdown
  116. palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
  117. palSetPad(GPIOB, 16);
  118. chThdSleepMilliseconds(10);
  119. // software shutdown
  120. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
  121. chThdSleepMilliseconds(10);
  122. // software shutdown disable (i.e. turn stuff on)
  123. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  124. chThdSleepMilliseconds(10);
  125. // zero all LED registers on all 8 pages
  126. uint8_t i;
  127. for(i=0; i<8; i++) {
  128. is31_write_data(i, full_page, 0xB4 + 1);
  129. chThdSleepMilliseconds(1);
  130. }
  131. }
  132. /* ==================
  133. * LED control thread
  134. * ================== */
  135. #define LED_MAILBOX_NUM_MSGS 5
  136. static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
  137. mailbox_t led_mailbox;
  138. static THD_WORKING_AREA(waLEDthread, 256);
  139. static THD_FUNCTION(LEDthread, arg) {
  140. (void)arg;
  141. chRegSetThreadName("LEDthread");
  142. uint8_t i, page;
  143. //persistent status variables
  144. uint8_t backlight_status, lock_status, led_step_status, layer_status;
  145. uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
  146. //mailbox variables
  147. uint8_t temp, msg_type, msg_led;
  148. msg_t msg;
  149. /* //control register variables
  150. uint8_t page, save_page, save_breath1, save_breath2;
  151. msg_t msg, retval;
  152. */
  153. // initialize persistent variables
  154. backlight_status = 0;
  155. lock_status = 0;//TODO: does keyboard remember locks?
  156. led_step_status = 4; //full brightness
  157. layer_status = 0;
  158. while(true) {
  159. // wait for a message (asynchronous)
  160. // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
  161. // be processed right away)
  162. chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
  163. msg_type = (msg >> 8) & 0xFF; //first byte is msg type
  164. msg_led = (msg) & 0xFF; //second byte is action information
  165. xprintf("--------------------\n");
  166. xprintf("mailbox fetch\nmsg: %X\n", msg);
  167. xprintf("type: %X - led: %X\n", msg_type, msg_led); //test if msg_type is 1 or 2 bytes after mask
  168. switch (msg_type){
  169. case KEY_LIGHT:
  170. //TODO: lighting key led on keypress
  171. break;
  172. case TOGGLE_LED:
  173. //TODO: toggle existing indicator off, or let user do this, but write frame 7 for every led change
  174. //turn on single led, msg_led = row/col of led
  175. xprintf("TOGGLE_LED\n");
  176. set_led_bit(led_control_reg, msg_led, 1);
  177. is31_write_data (7, led_control_reg, 0x12+1);
  178. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 7);
  179. layer_status = 7;
  180. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  181. break;
  182. case TOGGLE_ALL:
  183. xprintf("TOGGLE_ALL\n");
  184. //msg_led = unused, TODO: consider using msg_led to toggle layer display
  185. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  186. //if LED_ALL is on then toggle off, any other layer, turn on LED_ALL
  187. if(temp == 1) {
  188. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  189. } else {
  190. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 1);
  191. }
  192. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  193. break;
  194. case TOGGLE_BACKLIGHT:
  195. //msg_led = unused
  196. //TODO: consider Frame 0 as on/off layer and toggle led control register here
  197. xprintf("TOGGLE_BACKLIGHT\n");
  198. backlight_status ^= 1;
  199. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  200. layer_status = temp;
  201. page = backlight_status == 0 ? 0 : layer_status;
  202. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, page);
  203. break;
  204. case TOGGLE_LAYER_LEDS://show layer indicator or full map of layer keys.
  205. //TODO: change so user can flag which they want, indiv or full map in fn_actions
  206. //msg_led = layer to toggle on
  207. xprintf("TOGGLE_LAYER_LEDS\n");
  208. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  209. if(temp == msg_led) {
  210. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 7);
  211. layer_status = 7;
  212. } else {
  213. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_led);
  214. layer_status = msg_led;
  215. }
  216. break;
  217. case TOGGLE_LOCK_LED:
  218. //msg_led = 0-3 for lock flags
  219. lock_status ^= msg_led; //TODO: confirm toggling works and doesn't get out of sync
  220. set_lock_leds(led_control_reg, lock_status);
  221. break;
  222. case MODE_BREATH:
  223. break;
  224. case STEP_BRIGHTNESS:
  225. //pwm_levels[] bounds checking, loop through array
  226. //TODO: find a cleaner way to walk through this logic
  227. if (msg_led == 0 && led_step_status == 0) {
  228. led_step_status = 4;
  229. } else {
  230. led_step_status--;
  231. }
  232. if (msg_led == 1 && led_step_status == 4) {
  233. led_step_status = 0;
  234. } else {
  235. led_step_status++;
  236. }
  237. //TODO: this seems a messy way to populate the pwm register
  238. //mimic whitefox init which uses memcpy
  239. //populate the 9 byte rows to be written to each pin, first byte is register (pin) address
  240. for(i=1; i<9; i++) {
  241. pwm_register_array[i]=pwm_levels[led_step_status];
  242. }
  243. for(i=0; i<8; i++) {
  244. pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
  245. is31_write_data(0, pwm_register_array, 9);//first page controls pwm in all pages (init Display Option register)
  246. }
  247. break;
  248. /* case LED_MSG_SLEEP_LED_ON:
  249. // save current settings
  250. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &save_page);
  251. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, &save_breath1);
  252. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, &save_breath2);
  253. // use pages 7 and 8 for (hardware) breathing (assuming they're empty)
  254. is31_write_register(6, BREATHE_LED_ADDRESS, 0xFF);
  255. is31_write_register(7, BREATHE_LED_ADDRESS, 0x00);
  256. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (6<<4)|6);
  257. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  258. retval = MSG_TIMEOUT;
  259. temp = 6;
  260. while(retval == MSG_TIMEOUT) {
  261. // switch to the other page
  262. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, temp);
  263. temp = (temp == 6 ? 7 : 6);
  264. // the times should be sufficiently long for IS31 to finish switching pages
  265. retval = chMBFetch(&led_mailbox, &msg, MS2ST(temp == 6 ? 4000 : 6000));
  266. }
  267. // received a message (should be a wakeup), so restore previous state
  268. chThdSleepMilliseconds(3000); // need to wait until the page change finishes
  269. // note: any other messages are queued
  270. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, save_breath1);
  271. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, save_breath2);
  272. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, save_page);
  273. break;
  274. case LED_MSG_SLEEP_LED_OFF:
  275. // should not get here; wakeup should be received in the branch above break;
  276. break;
  277. */
  278. }
  279. xprintf("--------------------\n");
  280. }
  281. }
  282. /* ========================
  283. * led bit processing
  284. * ======================== */
  285. void set_led_bit (uint8_t *led_control_reg, uint8_t msg_led, uint8_t toggle_on) {
  286. uint8_t row_byte, column_bit;
  287. //msg_led tens column is pin#, A-control register is every other 8 bits
  288. //ones column is bit position in 8-bit mask
  289. //control register will be one bit shifted into position along register's full 0x12 bytes
  290. ////first byte is register address 0x00
  291. row_byte = ((msg_led / 10) % 10 - 1 ) * 2 + 1;
  292. column_bit = 1<<(msg_led % 10 - 1);
  293. if (toggle_on) {
  294. led_control_reg[row_byte] |= 1<<(column_bit);
  295. } else {
  296. led_control_reg[row_byte] &= ~1<<(column_bit);
  297. }
  298. }
  299. void set_lock_leds(uint8_t *led_control_reg, uint8_t lock_status) {
  300. uint8_t i;
  301. switch (lock_status) {
  302. case 1:
  303. set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 1);
  304. set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 0);
  305. break;
  306. case 2:
  307. set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 0);
  308. set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 1);
  309. break;
  310. case 3:
  311. set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 1);
  312. set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 1);
  313. break;
  314. }
  315. for(i=BACKLIGHT_OFF_LOCK_LED_OFF; i<8; i++) { //set in led_controller.h
  316. is31_write_data (i, led_control_reg, 0x12+1);
  317. }
  318. }
  319. void write_led_page (uint8_t page, const uint8_t *led_array, uint8_t led_count) {
  320. //TODO: init function that accepts array of led addresses and sets them by row
  321. uint8_t i;
  322. uint8_t row, col;
  323. uint8_t temp_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
  324. for(i=0;i<led_count;i++){
  325. row = ((led_array[i] / 10) % 10 - 1 ) * 2 + 1;//includes 1 byte shift for led register 0x00 address
  326. col = led_array[i] % 10 - 1;
  327. temp_control_reg[row] |= 1<<(col);
  328. }
  329. is31_write_data(page, temp_control_reg, 0x13);
  330. }
  331. /* =====================
  332. * hook into user keymap
  333. * ===================== */
  334. void led_controller_init(void) {
  335. uint8_t i;
  336. /* initialise I2C */
  337. /* I2C pins */
  338. palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
  339. palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
  340. /* start I2C */
  341. i2cStart(&I2CD1, &i2ccfg);
  342. // try high drive (from kiibohd)
  343. I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
  344. // try glitch fixing (from kiibohd)
  345. I2CD1.i2c->FLT = 4;
  346. chThdSleepMilliseconds(10);
  347. /* initialise IS31 chip */
  348. is31_init();
  349. //set Display Option Register so all pwm intensity is controlled from Frame 1
  350. is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME);
  351. /* set full pwm on Frame 1 */
  352. for(i=1; i<9; i++) {
  353. pwm_register_array[i]=0xFF;
  354. }
  355. for(i=0; i<8; i++) {
  356. pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
  357. is31_write_data(0, pwm_register_array, 9);
  358. chThdSleepMilliseconds(5);
  359. }
  360. //set all led bits on for Frame 2 LEDS_ALL
  361. full_page[0] = 0;
  362. __builtin_memcpy(full_page+1, is31_ic60_leds_mask, 0x12);
  363. is31_write_data(1, full_page, 1+0x12);
  364. /* enable breathing when the displayed page changes */
  365. // Fade-in Fade-out, time = 26ms * 2^N, N=3
  366. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
  367. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  368. // clean up the lock LEDs
  369. //TODO: adjust for new addressing and additional frames
  370. //is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0);
  371. //is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0);
  372. /* more time consuming LED processing should be offloaded into
  373. * a thread, with asynchronous messaging. */
  374. chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
  375. chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
  376. }
  377. //TODO: Don't know equivalent QMK hooks for these
  378. //
  379. //void hook_usb_suspend_entry(void) {
  380. //#ifdef SLEEP_LED_ENABLE
  381. // chSysLockFromISR();
  382. // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_ON);
  383. // chSysUnlockFromISR();
  384. //#endif /* SLEEP_LED_ENABLE */
  385. //}
  386. //
  387. //void hook_usb_suspend_loop(void) {
  388. // chThdSleepMilliseconds(100);
  389. // /* Remote wakeup */
  390. // if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
  391. // send_remote_wakeup(&USB_DRIVER);
  392. // }
  393. //}
  394. //
  395. //void hook_usb_wakeup(void) {
  396. //#ifdef SLEEP_LED_ENABLE
  397. // chSysLockFromISR();
  398. // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_OFF);
  399. // chSysUnlockFromISR();
  400. //#endif /* SLEEP_LED_ENABLE */
  401. //}
  402. //*/