led_controller.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 page, uint8_t reg, uint8_t *result) {
  94. is31_select_page(page);
  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. // TODO: This already done above, remove?
  114. // zero function page, all registers
  115. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  116. chThdSleepMilliseconds(10);
  117. // software shutdown disable (i.e. turn stuff on)
  118. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  119. chThdSleepMilliseconds(10);
  120. // zero all LED registers on all 8 pages
  121. uint8_t i;
  122. for(i=0; i<8; i++) {
  123. is31_write_data(i, full_page, 0xB4 + 1);
  124. chThdSleepMilliseconds(1);
  125. }
  126. }
  127. /* ==================
  128. * LED control thread
  129. * ================== */
  130. #define LED_MAILBOX_NUM_MSGS 5
  131. static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
  132. mailbox_t led_mailbox;
  133. static THD_WORKING_AREA(waLEDthread, 256);
  134. static THD_FUNCTION(LEDthread, arg) {
  135. (void)arg;
  136. chRegSetThreadName("LEDthread");
  137. uint8_t i;
  138. uint8_t temp, pwm;
  139. uint8_t save_page, save_breath1, save_breath2;
  140. msg_t msg, retval;
  141. while(true) {
  142. // wait for a message (asynchronous)
  143. // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
  144. // be processed right away)
  145. chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
  146. // process 'msg' here
  147. // if msg between 0-7, then process as page#, otherwise a specific LED address
  148. xprintf("--------------------\n");
  149. xprintf("mailbox fetch\ntemp: %X - msg: %X\n", temp, msg);
  150. if (msg < 8) {
  151. // read current page into 'temp'
  152. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &temp);
  153. chThdSleepMilliseconds(1);
  154. // If page is already in layer, switch off (layer 0)
  155. xprintf("Layer: post-read\ntemp: %X\n", temp);
  156. if(temp == msg) {
  157. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  158. } else {
  159. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg);
  160. }
  161. xprintf("Layer: post-change\ntemp: %X\n", temp);
  162. } else {
  163. switch(msg) {
  164. //TODO: make this generic and able to turn on/off any address and loop through all(or current) pages
  165. //TODO: set number of layers somewhere and loop through all when setting specific led
  166. case LED_MSG_SLEEP_LED_ON:
  167. // save current settings
  168. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &save_page);
  169. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, &save_breath1);
  170. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, &save_breath2);
  171. // use pages 7 and 8 for (hardware) breathing (assuming they're empty)
  172. is31_write_register(6, BREATHE_LED_ADDRESS, 0xFF);
  173. is31_write_register(7, BREATHE_LED_ADDRESS, 0x00);
  174. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (6<<4)|6);
  175. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  176. retval = MSG_TIMEOUT;
  177. temp = 6;
  178. while(retval == MSG_TIMEOUT) {
  179. // switch to the other page
  180. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, temp);
  181. temp = (temp == 6 ? 7 : 6);
  182. // the times should be sufficiently long for IS31 to finish switching pages
  183. retval = chMBFetch(&led_mailbox, &msg, MS2ST(temp == 6 ? 4000 : 6000));
  184. }
  185. // received a message (should be a wakeup), so restore previous state
  186. chThdSleepMilliseconds(3000); // need to wait until the page change finishes
  187. // note: any other messages are queued
  188. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, save_breath1);
  189. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, save_breath2);
  190. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, save_page);
  191. break;
  192. case LED_MSG_SLEEP_LED_OFF:
  193. // should not get here; wakeup should be received in the branch above break;
  194. break;
  195. default:
  196. if(msg >= 0x24) {
  197. xprintf("Power pre-read\ntemp: %X - msg: %X - pwm: %X\n", temp, msg, pwm);
  198. is31_read_register(0, msg, &temp);
  199. chThdSleepMilliseconds(10);
  200. xprintf("Post-read\ntemp: %X - msg: %X - pwm: %X\n", temp, msg, pwm);
  201. chThdSleepMilliseconds(10);
  202. pwm = (temp > 0x00 ? 0x00 : 0xFF);
  203. xprintf("pwm after: %X\n", pwm);
  204. chThdSleepMilliseconds(10);
  205. for(i=0; i<8; i++) {
  206. is31_write_register(i, msg, pwm);
  207. }
  208. xprintf("Power post-change\ntemp: %X - msg: %X - pwm: %X\n", temp, msg, pwm);
  209. chThdSleepMilliseconds(10);
  210. }
  211. break;
  212. }
  213. }
  214. xprintf("--------------------\n");
  215. }
  216. }
  217. /* =====================
  218. * hook into user keymap
  219. * ===================== */
  220. void led_controller_init(void) {
  221. uint8_t i;
  222. xprintf("led_controller_init");
  223. /* initialise I2C */
  224. /* I2C pins */
  225. palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
  226. palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
  227. /* start I2C */
  228. i2cStart(&I2CD1, &i2ccfg);
  229. // try high drive (from kiibohd)
  230. I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
  231. // try glitch fixing (from kiibohd)
  232. I2CD1.i2c->FLT = 4;
  233. chThdSleepMilliseconds(10);
  234. /* initialise IS31 chip */
  235. is31_init();
  236. /* enable LEDs on all pages */
  237. full_page[0] = 0;
  238. __builtin_memcpy(full_page+1, is31_ic60_leds_mask, 0x12);
  239. for(i=0; i<8; i++) {
  240. is31_write_data(i, full_page, 1+0x12);
  241. }
  242. /* enable breathing when the displayed page changes */
  243. // Fade-in Fade-out, time = 26ms * 2^N, N=3
  244. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
  245. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  246. // clean up the capslock LED
  247. is31_write_register(1, CAPS_LOCK_LED_ADDRESS, 0);
  248. is31_write_register(2, CAPS_LOCK_LED_ADDRESS, 0);
  249. /* more time consuming LED processing should be offloaded into
  250. * a thread, with asynchronous messaging. */
  251. chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
  252. chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
  253. }
  254. //TODO: Don't know equivalent QMK hooks for these
  255. //
  256. //void hook_usb_suspend_entry(void) {
  257. //#ifdef SLEEP_LED_ENABLE
  258. // chSysLockFromISR();
  259. // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_ON);
  260. // chSysUnlockFromISR();
  261. //#endif /* SLEEP_LED_ENABLE */
  262. //}
  263. //
  264. //void hook_usb_suspend_loop(void) {
  265. // chThdSleepMilliseconds(100);
  266. // /* Remote wakeup */
  267. // if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
  268. // send_remote_wakeup(&USB_DRIVER);
  269. // }
  270. //}
  271. //
  272. //void hook_usb_wakeup(void) {
  273. //#ifdef SLEEP_LED_ENABLE
  274. // chSysLockFromISR();
  275. // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_OFF);
  276. // chSysUnlockFromISR();
  277. //#endif /* SLEEP_LED_ENABLE */
  278. //}
  279. //*/