led_controller.c 17 KB

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