led_controller.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 0x59
  48. #endif
  49. /* Which LED should breathe during sleep */
  50. #if !defined(BREATHE_LED_ADDRESS)
  51. #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
  52. #endif
  53. /* =================
  54. * ChibiOS I2C setup
  55. * ================= */
  56. static const I2CConfig i2ccfg = {
  57. 400000 // clock speed (Hz); 400kHz max for IS31
  58. };
  59. /* ==============
  60. * variables
  61. * ============== */
  62. // internal communication buffers
  63. uint8_t tx[2] __attribute__((aligned(2)));
  64. uint8_t rx[1] __attribute__((aligned(2)));
  65. // buffer for sending the whole page at once (used also as a temp buffer)
  66. uint8_t full_page[0xB4+1] = {0};
  67. // LED mask (which LEDs are present, selected by bits)
  68. // See page comment above, control alternates CA matrix/CB matrix
  69. // IC60 pcb uses only CA matrix.
  70. // Each byte is a control pin for 8 leds 8-1
  71. const uint8_t is31_ic60_leds_mask[0x12] = {
  72. 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
  73. 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x00, 0x00
  74. };
  75. /* ============================
  76. * communication functions
  77. * ============================ */
  78. msg_t is31_select_page(uint8_t page) {
  79. tx[0] = IS31_COMMANDREGISTER;
  80. tx[1] = page;
  81. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  82. }
  83. msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
  84. is31_select_page(page);
  85. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, US2ST(IS31_TIMEOUT));
  86. }
  87. msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
  88. is31_select_page(page);
  89. tx[0] = reg;
  90. tx[1] = data;
  91. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  92. }
  93. msg_t is31_read_register(uint8_t b, uint8_t reg, uint8_t *result) {
  94. is31_select_page(b);
  95. tx[0] = reg;
  96. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
  97. }
  98. /* ========================
  99. * initialise the IS31 chip
  100. * ======================== */
  101. void is31_init(void) {
  102. // just to be sure that it's all zeroes
  103. __builtin_memset(full_page,0,0xB4+1);
  104. // zero function page, all registers (assuming full_page is all zeroes)
  105. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  106. // disable hardware shutdown
  107. palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
  108. palSetPad(GPIOB, 16);
  109. chThdSleepMilliseconds(10);
  110. // software shutdown
  111. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
  112. chThdSleepMilliseconds(10);
  113. // zero function page, all registers
  114. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  115. chThdSleepMilliseconds(10);
  116. // software shutdown disable (i.e. turn stuff on)
  117. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  118. chThdSleepMilliseconds(10);
  119. // zero all LED registers on all 8 pages
  120. uint8_t i;
  121. for(i=0; i<8; i++) {
  122. is31_write_data(i, full_page, 0xB4 + 1);
  123. chThdSleepMilliseconds(1);
  124. }
  125. }
  126. /* ==================
  127. * LED control thread
  128. * ================== */
  129. #define LED_MAILBOX_NUM_MSGS 5
  130. static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
  131. mailbox_t led_mailbox;
  132. static THD_WORKING_AREA(waLEDthread, 256);
  133. static THD_FUNCTION(LEDthread, arg) {
  134. (void)arg;
  135. chRegSetThreadName("LEDthread");
  136. uint8_t temp;
  137. uint8_t save_page, save_breath1, save_breath2;
  138. msg_t msg, retval;
  139. while(true) {
  140. // wait for a message (asynchronous)
  141. // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
  142. // be processed right away)
  143. chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
  144. // process 'msg' here
  145. switch(msg) {
  146. //TODO: make this generic and able to turn on/off any address and loop through all(or current) pages
  147. case LED_MSG_CAPS_ON:
  148. // turn caps on on pages 1 and 2
  149. is31_write_register(0, CAPS_LOCK_LED_ADDRESS, 0xFF);
  150. is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0xFF);
  151. is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0xFF);
  152. break;
  153. case LED_MSG_CAPS_OFF:
  154. // turn caps off on pages 1 and 2
  155. is31_write_register(0, CAPS_LOCK_LED_ADDRESS, 0);
  156. is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0);
  157. is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0);
  158. break;
  159. case LED_MSG_SLEEP_LED_ON:
  160. // save current settings
  161. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &save_page);
  162. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, &save_breath1);
  163. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, &save_breath2);
  164. // use pages 7 and 8 for (hardware) breathing (assuming they're empty)
  165. is31_write_register(6, BREATHE_LED_ADDRESS, 0xFF);
  166. is31_write_register(7, BREATHE_LED_ADDRESS, 0x00);
  167. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (6<<4)|6);
  168. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  169. retval = MSG_TIMEOUT;
  170. temp = 6;
  171. while(retval == MSG_TIMEOUT) {
  172. // switch to the other page
  173. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, temp);
  174. temp = (temp == 6 ? 7 : 6);
  175. // the times should be sufficiently long for IS31 to finish switching pages
  176. retval = chMBFetch(&led_mailbox, &msg, MS2ST(temp == 6 ? 4000 : 6000));
  177. }
  178. // received a message (should be a wakeup), so restore previous state
  179. chThdSleepMilliseconds(3000); // need to wait until the page change finishes
  180. // note: any other messages are queued
  181. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, save_breath1);
  182. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, save_breath2);
  183. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, save_page);
  184. break;
  185. case LED_MSG_SLEEP_LED_OFF:
  186. // should not get here; wakeup should be received in the branch above
  187. break;
  188. case LED_MSG_ALL_TOGGLE:
  189. // read current page into 'temp'
  190. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  191. chThdSleepMilliseconds(1);
  192. // switch to 'the other' page
  193. if(temp==2) {
  194. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  195. } else {
  196. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 2);
  197. }
  198. break;
  199. case LED_MSG_GAME_TOGGLE:
  200. // read current page into 'temp'
  201. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  202. chThdSleepMilliseconds(1);
  203. // switch to 'the other' page
  204. if(temp==1) {
  205. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  206. } else {
  207. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 1);
  208. }
  209. break;
  210. }
  211. }
  212. }
  213. /* =============
  214. * hook into TMK
  215. * ============= */
  216. void led_controller_init(void) {
  217. uint8_t i;
  218. xprintf("led_controller_init");
  219. /* initialise I2C */
  220. /* I2C pins */
  221. palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
  222. palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
  223. /* start I2C */
  224. i2cStart(&I2CD1, &i2ccfg);
  225. // try high drive (from kiibohd)
  226. I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
  227. // try glitch fixing (from kiibohd)
  228. I2CD1.i2c->FLT = 4;
  229. chThdSleepMilliseconds(10);
  230. /* initialise IS31 chip */
  231. is31_init();
  232. /* enable LEDs on all pages */
  233. full_page[0] = 0;
  234. __builtin_memcpy(full_page+1, is31_ic60_leds_mask, 0x12);
  235. for(i=0; i<8; i++) {
  236. is31_write_data(i, full_page, 1+0x12);
  237. }
  238. /* enable breathing when the displayed page changes */
  239. // Fade-in Fade-out, time = 26ms * 2^N, N=3
  240. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
  241. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  242. // clean up the capslock LED
  243. is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0);
  244. is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0);
  245. /* more time consuming LED processing should be offloaded into
  246. * a thread, with asynchronous messaging. */
  247. chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
  248. chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
  249. }
  250. //TODO: Don't know equivalent QMK hooks for these
  251. //
  252. //void hook_usb_suspend_entry(void) {
  253. //#ifdef SLEEP_LED_ENABLE
  254. // chSysLockFromISR();
  255. // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_ON);
  256. // chSysUnlockFromISR();
  257. //#endif /* SLEEP_LED_ENABLE */
  258. //}
  259. //
  260. //void hook_usb_suspend_loop(void) {
  261. // chThdSleepMilliseconds(100);
  262. // /* Remote wakeup */
  263. // if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
  264. // send_remote_wakeup(&USB_DRIVER);
  265. // }
  266. //}
  267. //
  268. //void hook_usb_wakeup(void) {
  269. //#ifdef SLEEP_LED_ENABLE
  270. // chSysLockFromISR();
  271. // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_OFF);
  272. // chSysUnlockFromISR();
  273. //#endif /* SLEEP_LED_ENABLE */
  274. //}
  275. //*/