led_controller.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "led_controller.h"
  22. #include "hook.h"
  23. #include "suspend.h"
  24. #include "usb_main.h"
  25. /* WF 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 46 47
  29. 48 51 52 53 54 55 56 57 58 61 62 63 64 65 66
  30. 67 68 71 72 73 74 75 76 77 78 81 82 83 84 85
  31. 86 87 88 91 92 93 (94) 95 96 97
  32. */
  33. /*
  34. each page has 0xB4 bytes
  35. 0 - 0x11: LED control (on/off):
  36. order: CA1, CB1, CA2, CB2, .... (CA - matrix A, CB - matrix B)
  37. CAn controls Cn-8 .. Cn-1 (LSbit)
  38. 0x12 - 0x23: blink control (like "LED control")
  39. 0x24 - 0xB3: PWM control: byte per LED, 0xFF max on
  40. order same as above (CA 1st row (8bytes), CB 1st row (8bytes), ...)
  41. */
  42. /* Which LED should be used for CAPS LOCK indicator
  43. * The usual Caps Lock position is C4-8, so the address is
  44. * 0x24 + (4-1)*0x10 + (8-1) = 0x5B */
  45. #if !defined(CAPS_LOCK_LED_ADDRESS)
  46. #define CAPS_LOCK_LED_ADDRESS 0x5B
  47. #endif
  48. /* Which LED should breathe during sleep */
  49. #if !defined(BREATHE_LED_ADDRESS)
  50. #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
  51. #endif
  52. /* =================
  53. * ChibiOS I2C setup
  54. * ================= */
  55. static const I2CConfig i2ccfg = {
  56. 400000 // clock speed (Hz); 400kHz max for IS31
  57. };
  58. /* ==============
  59. * variables
  60. * ============== */
  61. // internal communication buffers
  62. uint8_t tx[2] __attribute__((aligned(2)));
  63. uint8_t rx[1] __attribute__((aligned(2)));
  64. // buffer for sending the whole page at once (used also as a temp buffer)
  65. uint8_t full_page[0xB4+1] = {0};
  66. // LED mask (which LEDs are present, selected by bits)
  67. const uint8_t is31_wf_leds_mask[0x12] = {
  68. 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
  69. 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00
  70. };
  71. /* ============================
  72. * communication functions
  73. * ============================ */
  74. msg_t is31_select_page(uint8_t page) {
  75. tx[0] = IS31_COMMANDREGISTER;
  76. tx[1] = page;
  77. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  78. }
  79. msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
  80. is31_select_page(page);
  81. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, US2ST(IS31_TIMEOUT));
  82. }
  83. msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
  84. is31_select_page(page);
  85. tx[0] = reg;
  86. tx[1] = data;
  87. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  88. }
  89. msg_t is31_read_register(uint8_t b, uint8_t reg, uint8_t *result) {
  90. is31_select_page(b);
  91. tx[0] = reg;
  92. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
  93. }
  94. /* ========================
  95. * initialise the IS31 chip
  96. * ======================== */
  97. void is31_init(void) {
  98. // just to be sure that it's all zeroes
  99. __builtin_memset(full_page,0,0xB4+1);
  100. // zero function page, all registers (assuming full_page is all zeroes)
  101. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  102. // disable hardware shutdown
  103. palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
  104. palSetPad(GPIOB, 16);
  105. chThdSleepMilliseconds(10);
  106. // software shutdown
  107. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
  108. chThdSleepMilliseconds(10);
  109. // zero function page, all registers
  110. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  111. chThdSleepMilliseconds(10);
  112. // software shutdown disable (i.e. turn stuff on)
  113. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  114. chThdSleepMilliseconds(10);
  115. // zero all LED registers on all 8 pages
  116. uint8_t i;
  117. for(i=0; i<8; i++) {
  118. is31_write_data(i, full_page, 0xB4 + 1);
  119. chThdSleepMilliseconds(1);
  120. }
  121. }
  122. /* ==================
  123. * LED control thread
  124. * ================== */
  125. #define LED_MAILBOX_NUM_MSGS 5
  126. static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
  127. mailbox_t led_mailbox;
  128. static THD_WORKING_AREA(waLEDthread, 256);
  129. static THD_FUNCTION(LEDthread, arg) {
  130. (void)arg;
  131. chRegSetThreadName("LEDthread");
  132. uint8_t temp;
  133. uint8_t save_page, save_breath1, save_breath2;
  134. msg_t msg, retval;
  135. while(true) {
  136. // wait for a message (asynchronous)
  137. // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
  138. // be processed right away)
  139. chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
  140. // process 'msg' here
  141. switch(msg) {
  142. case LED_MSG_CAPS_ON:
  143. // turn caps on on pages 1 and 2
  144. is31_write_register(0, CAPS_LOCK_LED_ADDRESS, 0xFF);
  145. is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0xFF);
  146. is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0xFF);
  147. break;
  148. case LED_MSG_CAPS_OFF:
  149. // turn caps off on pages 1 and 2
  150. is31_write_register(0, CAPS_LOCK_LED_ADDRESS, 0);
  151. is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0);
  152. is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0);
  153. break;
  154. case LED_MSG_SLEEP_LED_ON:
  155. // save current settings
  156. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &save_page);
  157. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, &save_breath1);
  158. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, &save_breath2);
  159. // use pages 7 and 8 for (hardware) breathing (assuming they're empty)
  160. is31_write_register(6, BREATHE_LED_ADDRESS, 0xFF);
  161. is31_write_register(7, BREATHE_LED_ADDRESS, 0x00);
  162. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (6<<4)|6);
  163. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  164. retval = MSG_TIMEOUT;
  165. temp = 6;
  166. while(retval == MSG_TIMEOUT) {
  167. // switch to the other page
  168. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, temp);
  169. temp = (temp == 6 ? 7 : 6);
  170. // the times should be sufficiently long for IS31 to finish switching pages
  171. retval = chMBFetch(&led_mailbox, &msg, MS2ST(temp == 6 ? 4000 : 6000));
  172. }
  173. // received a message (should be a wakeup), so restore previous state
  174. chThdSleepMilliseconds(3000); // need to wait until the page change finishes
  175. // note: any other messages are queued
  176. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, save_breath1);
  177. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, save_breath2);
  178. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, save_page);
  179. break;
  180. case LED_MSG_SLEEP_LED_OFF:
  181. // should not get here; wakeup should be received in the branch above
  182. break;
  183. case LED_MSG_ALL_TOGGLE:
  184. // read current page into 'temp'
  185. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  186. chThdSleepMilliseconds(1);
  187. // switch to 'the other' page
  188. if(temp==2) {
  189. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  190. } else {
  191. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 2);
  192. }
  193. break;
  194. case LED_MSG_GAME_TOGGLE:
  195. // read current page into 'temp'
  196. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  197. chThdSleepMilliseconds(1);
  198. // switch to 'the other' page
  199. if(temp==1) {
  200. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  201. } else {
  202. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 1);
  203. }
  204. break;
  205. }
  206. }
  207. }
  208. /* LED game mode */
  209. const uint8_t led_game[83] = {
  210. 0x24,
  211. 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  212. 0x34,
  213. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  214. 0x44,
  215. 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
  216. 0x54,
  217. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  218. 0x64,
  219. 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
  220. 0x74,
  221. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  222. 0x84,
  223. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  224. 0x94,
  225. 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00,
  226. 0xA4,
  227. 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00,
  228. };
  229. /* ALL LEDs */
  230. const uint8_t led_all[83] = {
  231. 0x24,
  232. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  233. 0x34,
  234. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  235. 0x44,
  236. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  237. 0x54,
  238. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  239. 0x64,
  240. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  241. 0x74,
  242. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  243. 0x84,
  244. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  245. 0x94,
  246. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  247. 0xA4,
  248. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  249. };
  250. /* =============
  251. * hook into TMK
  252. * ============= */
  253. void hook_early_init(void) {
  254. uint8_t i;
  255. /* initialise I2C */
  256. /* I2C pins */
  257. palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
  258. palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
  259. /* start I2C */
  260. i2cStart(&I2CD1, &i2ccfg);
  261. // try high drive (from kiibohd)
  262. I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
  263. // try glitch fixing (from kiibohd)
  264. I2CD1.i2c->FLT = 4;
  265. chThdSleepMilliseconds(10);
  266. /* initialise IS31 chip */
  267. is31_init();
  268. /* enable WF LEDs on all pages */
  269. full_page[0] = 0;
  270. __builtin_memcpy(full_page+1, is31_wf_leds_mask, 0x12);
  271. for(i=0; i<8; i++) {
  272. is31_write_data(i, full_page, 1+0x12);
  273. }
  274. /* enable breathing when the displayed page changes */
  275. // Fade-in Fade-out, time = 26ms * 2^N, N=3
  276. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
  277. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  278. /* Write pages */
  279. for(i=0; i<9; i++) {
  280. is31_write_data(1,(uint8_t *)(led_game+(9*i)),9);
  281. chThdSleepMilliseconds(5);
  282. is31_write_data(2,(uint8_t *)(led_all+(9*i)),9);
  283. chThdSleepMilliseconds(5);
  284. }
  285. // clean up the capslock LED
  286. is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0);
  287. is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0);
  288. /* more time consuming LED processing should be offloaded into
  289. * a thread, with asynchronous messaging. */
  290. chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
  291. chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
  292. }
  293. void hook_usb_suspend_entry(void) {
  294. #ifdef SLEEP_LED_ENABLE
  295. chSysLockFromISR();
  296. chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_ON);
  297. chSysUnlockFromISR();
  298. #endif /* SLEEP_LED_ENABLE */
  299. }
  300. void hook_usb_suspend_loop(void) {
  301. chThdSleepMilliseconds(100);
  302. /* Remote wakeup */
  303. if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
  304. send_remote_wakeup(&USB_DRIVER);
  305. }
  306. }
  307. void hook_usb_wakeup(void) {
  308. #ifdef SLEEP_LED_ENABLE
  309. chSysLockFromISR();
  310. chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_OFF);
  311. chSysUnlockFromISR();
  312. #endif /* SLEEP_LED_ENABLE */
  313. }