led_controller.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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_reg_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. xprintf("page display: %X\n", page);
  101. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  102. }
  103. msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) {
  104. is31_select_page(page);
  105. tx[0] = reg;
  106. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
  107. }
  108. /* ========================
  109. * initialise the IS31 chip
  110. * ======================== */
  111. void is31_init(void) {
  112. // just to be sure that it's all zeroes
  113. __builtin_memset(full_page,0,0xB4+1);
  114. // zero function page, all registers (assuming full_page is all zeroes)
  115. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  116. // disable hardware shutdown
  117. palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
  118. palSetPad(GPIOB, 16);
  119. chThdSleepMilliseconds(10);
  120. // software shutdown
  121. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
  122. chThdSleepMilliseconds(10);
  123. // TODO: This already done above, remove?
  124. // zero function page, all registers
  125. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  126. chThdSleepMilliseconds(10);
  127. // software shutdown disable (i.e. turn stuff on)
  128. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  129. chThdSleepMilliseconds(10);
  130. // zero all LED registers on all 8 pages
  131. uint8_t i;
  132. for(i=0; i<8; i++) {
  133. is31_write_data(i, full_page, 0xB4 + 1);
  134. chThdSleepMilliseconds(1);
  135. }
  136. }
  137. /* ==================
  138. * LED control thread
  139. * ================== */
  140. #define LED_MAILBOX_NUM_MSGS 5
  141. static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
  142. mailbox_t led_mailbox;
  143. static THD_WORKING_AREA(waLEDthread, 256);
  144. static THD_FUNCTION(LEDthread, arg) {
  145. (void)arg;
  146. chRegSetThreadName("LEDthread");
  147. uint8_t i, page;
  148. //persistent status variables
  149. uint8_t backlight_status, lock_status, led_step, active_layer;
  150. uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
  151. //mailbox variables
  152. uint8_t temp, msg_type, msg_led;
  153. msg_t msg;
  154. /* //control register variables
  155. uint8_t page, save_page, save_breath1, save_breath2;
  156. msg_t msg, retval;
  157. */
  158. // initialize persistent variables
  159. backlight_status = 0;
  160. lock_status = 0;//TODO: does keyboard remember locks?
  161. led_step = 4; //full brightness
  162. active_layer = 0;
  163. while(true) {
  164. // wait for a message (asynchronous)
  165. // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
  166. // be processed right away)
  167. chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
  168. msg_type = (msg >> 8) & 0xFF; //first byte is msg type
  169. msg_led = (msg) & 0xFF; //second byte is action information
  170. xprintf("--------------------\n");
  171. xprintf("mailbox fetch\nmsg: %X\n", msg);
  172. xprintf("type: %X - led: %X\n", msg_type, msg_led); //test if msg_type is 1 or 2 bytes after mask
  173. switch (msg_type){
  174. case KEY_LIGHT:
  175. //TODO: lighting key led on keypress
  176. break;
  177. case TOGGLE_LED:
  178. //TODO: toggle existing indicator off, or let user do this, but write frame 7 for every led change
  179. //turn on single led, msg_led = row/col of led
  180. set_led_bit(led_control_reg, msg_led, 1);
  181. is31_write_data (7, led_control_reg, 0x12+1);
  182. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 7);
  183. active_layer = 7;
  184. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  185. xprintf("page display: %X\n", temp);
  186. break;
  187. case TOGGLE_ALL:
  188. xprintf("TOGGLE_ALL\n");
  189. //msg_led = unused, TODO: consider using msg_led to toggle layer display
  190. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  191. xprintf("temp: %X\n", temp);
  192. //if LED_ALL is on then toggle off, any other layer, turn on LED_ALL
  193. if(temp == 1) {
  194. xprintf("page display true: %X\n", temp);
  195. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  196. } else {
  197. xprintf("page display false: %X\n", temp);
  198. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 1);
  199. }
  200. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  201. xprintf("page display: %X\n", temp);
  202. break;
  203. case TOGGLE_BACKLIGHT:
  204. //msg_led = unused
  205. backlight_status ^= 1;
  206. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  207. active_layer = temp;
  208. page = backlight_status == 0 ? 0 : active_layer;
  209. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, page);
  210. break;
  211. case TOGGLE_LAYER_LEDS://show layer indicator or full map of layer keys.
  212. //TODO: change so user can flag which they want, indiv or full map in fn_actions
  213. //msg_led = layer to toggle on
  214. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  215. if(temp == msg_led) {
  216. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 7);
  217. active_layer = 7;
  218. } else {
  219. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_led);
  220. active_layer = msg_led;
  221. }
  222. break;
  223. case TOGGLE_LOCK_LED:
  224. //msg_led = 0-3 for lock flags
  225. lock_status ^= msg_led; //TODO: confirm toggling works and doesn't get out of sync
  226. set_lock_leds(led_control_reg, lock_status);
  227. break;
  228. case MODE_BREATH:
  229. break;
  230. case STEP_BRIGHTNESS:
  231. //pwm_levels[] bounds checking, loop through array
  232. //TODO: find a cleaner way to walk through this logic
  233. if (msg_led == 0) {
  234. if (led_step == 0) {
  235. led_step = 4;
  236. } else {
  237. led_step--;
  238. }
  239. } else {
  240. if (led_step == 4) {
  241. led_step = 0;
  242. } else {
  243. led_step++;
  244. }
  245. }
  246. //TODO: this seems a messy way to populate the pwm register
  247. //populate the 9 byte rows to be written to each pin, first byte is register (pin) address
  248. for(i=1; i<9; i++) {
  249. pwm_reg_array[i]=pwm_levels[led_step];
  250. }
  251. for(i=0; i<8; i++) {
  252. pwm_reg_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
  253. is31_write_data(0, pwm_reg_array, 9);
  254. chThdSleepMilliseconds(5);
  255. }
  256. break;
  257. /* case LED_MSG_SLEEP_LED_ON:
  258. // save current settings
  259. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &save_page);
  260. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, &save_breath1);
  261. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, &save_breath2);
  262. // use pages 7 and 8 for (hardware) breathing (assuming they're empty)
  263. is31_write_register(6, BREATHE_LED_ADDRESS, 0xFF);
  264. is31_write_register(7, BREATHE_LED_ADDRESS, 0x00);
  265. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (6<<4)|6);
  266. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  267. retval = MSG_TIMEOUT;
  268. temp = 6;
  269. while(retval == MSG_TIMEOUT) {
  270. // switch to the other page
  271. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, temp);
  272. temp = (temp == 6 ? 7 : 6);
  273. // the times should be sufficiently long for IS31 to finish switching pages
  274. retval = chMBFetch(&led_mailbox, &msg, MS2ST(temp == 6 ? 4000 : 6000));
  275. }
  276. // received a message (should be a wakeup), so restore previous state
  277. chThdSleepMilliseconds(3000); // need to wait until the page change finishes
  278. // note: any other messages are queued
  279. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, save_breath1);
  280. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, save_breath2);
  281. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, save_page);
  282. break;
  283. case LED_MSG_SLEEP_LED_OFF:
  284. // should not get here; wakeup should be received in the branch above break;
  285. break;
  286. default:
  287. //TODO: individual led state unchanged if page arrays are selected in code above
  288. //avoidable if full pages are written on the fly
  289. //or use pg8 for individual leds, have pointer to currently on led address for toggling
  290. if (msg == 0x59 || msg == 0x84) {
  291. //toggle lock keys on all layers
  292. for (i=0,i<8,i++) {
  293. is31_read_register(0, msg, &temp);
  294. pwm = (temp > 0x00 ? 0x00 : 0xFF);
  295. is31_write_register(i,msg,pwm);
  296. }
  297. } else if(msg >= 0x24) {
  298. xprintf("Power pre-read\ntemp: %X - msg: %X - pwm: %X\n", temp, msg, pwm);
  299. is31_read_register(7, msg, &temp);
  300. xprintf("Post-read\ntemp: %X - msg: %X - pwm: %X\n", temp, msg, pwm);
  301. if (msg == active_led) {
  302. //toggle led power
  303. pwm = (temp > 0x00 ? 0x00 : 0xFF);
  304. //Use 8th led page for individual led indicators
  305. is31_write_register(7, msg, pwm);
  306. } else {
  307. is31_write_register(7, active_led, 0x00);
  308. is31_write_register(7, msg, 0xFF);
  309. }
  310. xprintf("Power post-change\ntemp: %X - msg: %X - pwm: %X\n", temp, msg, pwm);
  311. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 7);
  312. }
  313. break;
  314. */
  315. }
  316. xprintf("--------------------\n");
  317. }
  318. }
  319. /* ========================
  320. * led bit processing
  321. * ======================== */
  322. void set_led_bit (uint8_t *led_control_reg, uint8_t msg_led, uint8_t toggle_on) {
  323. uint8_t row_byte, column_bit;
  324. //msg_led tens column is pin#, A-control register is every other 8 bits
  325. //ones column is bit position in 8-bit mask
  326. //control register will be one bit shifted into position along register's full 0x12 bytes
  327. ////first byte is register address 0x00
  328. row_byte = ((msg_led / 10) % 10 - 1 ) * 2 + 1;
  329. column_bit = 1<<(msg_led % 10 - 1);
  330. if (toggle_on) {
  331. led_control_reg[row_byte] |= 1<<(column_bit);
  332. } else {
  333. led_control_reg[row_byte] &= ~1<<(column_bit);
  334. }
  335. }
  336. void set_lock_leds(uint8_t *led_control_reg, uint8_t lock_status) {
  337. uint8_t i;
  338. switch (lock_status) {
  339. case 1:
  340. set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 1);//TODO: define lock addresses by matrix#, and loop for all frames
  341. set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 0);
  342. break;
  343. case 2:
  344. set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 0);
  345. set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 1);
  346. break;
  347. case 3:
  348. set_led_bit(led_control_reg, NUM_LOCK_LED_ADDRESS, 1);
  349. set_led_bit(led_control_reg, CAPS_LOCK_LED_ADDRESS, 1);
  350. break;
  351. }
  352. for(i=1; i<8; i++) { //keep LED_OFF layer all off, including locks
  353. is31_write_data (i, led_control_reg, 0x12+1);
  354. chThdSleepMilliseconds(5);
  355. }
  356. }
  357. void write_led_page (uint8_t page, const uint8_t *led_array, uint8_t led_count) {
  358. //TODO: init function that accepts array of led addresses and sets them by row
  359. uint8_t i;
  360. uint8_t row, col;
  361. uint8_t temp_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
  362. xprintf("-------------\n");
  363. xprintf("write page %X\n", page);
  364. for(i=0;i<led_count;i++){
  365. row = ((led_array[i] / 10) % 10 - 1 ) * 2 + 1;//includes 1 byte shift for 0x00 address
  366. col = 1<<(led_array[i] % 10 - 1);
  367. temp_control_reg[row] |= 1<<(col);
  368. }
  369. is31_write_data(page, temp_control_reg, 0x13);
  370. xprintf("-------------\n");
  371. }
  372. /* =====================
  373. * hook into user keymap
  374. * ===================== */
  375. void led_controller_init(void) {
  376. uint8_t i;
  377. /* initialise I2C */
  378. /* I2C pins */
  379. palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
  380. palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
  381. /* start I2C */
  382. i2cStart(&I2CD1, &i2ccfg);
  383. // try high drive (from kiibohd)
  384. I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
  385. // try glitch fixing (from kiibohd)
  386. I2CD1.i2c->FLT = 4;
  387. chThdSleepMilliseconds(10);
  388. /* initialise IS31 chip */
  389. is31_init();
  390. /* enable LEDs on all pages */
  391. full_page[0] = 0;
  392. __builtin_memcpy(full_page+1, is31_ic60_leds_mask, 0x12);
  393. for(i=0; i<8; i++) {
  394. is31_write_data(i, full_page, 1+0x12);
  395. }
  396. //set Display Option Register so all pwm intensity is controlled from Frame 1
  397. is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME);
  398. /* enable breathing when the displayed page changes */
  399. // Fade-in Fade-out, time = 26ms * 2^N, N=3
  400. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
  401. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  402. // clean up the lock LEDs
  403. //TODO: adjust for new addressing and additional frames
  404. //is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0);
  405. //is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0);
  406. /* more time consuming LED processing should be offloaded into
  407. * a thread, with asynchronous messaging. */
  408. chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
  409. chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
  410. }
  411. //TODO: Don't know equivalent QMK hooks for these
  412. //
  413. //void hook_usb_suspend_entry(void) {
  414. //#ifdef SLEEP_LED_ENABLE
  415. // chSysLockFromISR();
  416. // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_ON);
  417. // chSysUnlockFromISR();
  418. //#endif /* SLEEP_LED_ENABLE */
  419. //}
  420. //
  421. //void hook_usb_suspend_loop(void) {
  422. // chThdSleepMilliseconds(100);
  423. // /* Remote wakeup */
  424. // if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
  425. // send_remote_wakeup(&USB_DRIVER);
  426. // }
  427. //}
  428. //
  429. //void hook_usb_wakeup(void) {
  430. //#ifdef SLEEP_LED_ENABLE
  431. // chSysLockFromISR();
  432. // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_OFF);
  433. // chSysUnlockFromISR();
  434. //#endif /* SLEEP_LED_ENABLE */
  435. //}
  436. //*/