led_controller.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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.h"
  23. #include "action_layer.h"
  24. #include "host.h"
  25. #include "led_controller.h"
  26. #include "suspend.h"
  27. #include "usb_main.h"
  28. /* Infinity60 LED MAP
  29. - digits mean "row" and "col", i.e. 45 means C4-5 in the IS31 datasheet, matrix A
  30. 11 12 13 14 15 16 17 18 21 22 23 24 25 26 27*
  31. 28 31 32 33 34 35 36 37 38 41 42 43 44 45
  32. 46 47 48 51 52 53 54 55 56 57 58 61 62
  33. 63 64 65 66 67 68 71 72 73 74 75 76 77*
  34. 78 81 82 83 84 85 86 87
  35. *Unused in Alphabet Layout
  36. */
  37. /*
  38. each page has 0xB4 bytes
  39. 0 - 0x11: LED control (on/off):
  40. order: CA1, CB1, CA2, CB2, .... (CA - matrix A, CB - matrix B)
  41. CAn controls Cn-8 .. Cn-1 (LSbit)
  42. 0x12 - 0x23: blink control (like "LED control")
  43. 0x24 - 0xB3: PWM control: byte per LED, 0xFF max on
  44. order same as above (CA 1st row (8bytes), CB 1st row (8bytes), ...)
  45. */
  46. /* Which LED should be used for CAPS LOCK indicator
  47. * The usual Caps Lock position is C4-6, so the address is
  48. * 0x24 + (4-1)*0x10 + (8-1) = 0x59 */
  49. #if !defined(CAPS_LOCK_LED_ADDRESS)
  50. #define CAPS_LOCK_LED_ADDRESS 46
  51. #endif
  52. #if !defined(NUM_LOCK_LED_ADDRESS)
  53. #define NUM_LOCK_LED_ADDRESS 85
  54. #endif
  55. /* Which LED should breathe during sleep */
  56. #if !defined(BREATHE_LED_ADDRESS)
  57. #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
  58. #endif
  59. /* =================
  60. * ChibiOS I2C setup
  61. * ================= */
  62. static const I2CConfig i2ccfg = {
  63. 400000 // clock speed (Hz); 400kHz max for IS31
  64. };
  65. /* ==============
  66. * variables
  67. * ============== */
  68. // internal communication buffers
  69. uint8_t tx[2] __attribute__((aligned(2)));
  70. uint8_t rx[1] __attribute__((aligned(2)));
  71. // buffer for sending the whole page at once (used also as a temp buffer)
  72. uint8_t full_page[0xB4+1] = {0};
  73. // LED mask (which LEDs are present, selected by bits)
  74. // See page comment above, control alternates CA matrix/CB matrix
  75. // IC60 pcb uses only CA matrix.
  76. // Each byte is a control pin for 8 leds ordered 8-1
  77. const uint8_t all_on_leds_mask[0x12] = {
  78. 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
  79. 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x00, 0x00
  80. };
  81. // array to hold brightness pwm steps
  82. const uint8_t pwm_levels[5] = {
  83. 0x00, 0x16, 0x4E, 0xA1, 0xFF
  84. };
  85. // array to write to pwm register
  86. uint8_t pwm_register_array[9] = {0};
  87. /* ============================
  88. * communication functions
  89. * ============================ */
  90. msg_t is31_select_page(uint8_t page) {
  91. tx[0] = IS31_COMMANDREGISTER;
  92. tx[1] = page;
  93. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  94. }
  95. msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
  96. is31_select_page(page);
  97. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, US2ST(IS31_TIMEOUT));
  98. }
  99. msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
  100. is31_select_page(page);
  101. tx[0] = reg;
  102. tx[1] = data;
  103. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  104. }
  105. msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) {
  106. is31_select_page(page);
  107. tx[0] = reg;
  108. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
  109. }
  110. /* ========================
  111. * initialise the IS31 chip
  112. * ======================== */
  113. void is31_init(void) {
  114. // just to be sure that it's all zeroes
  115. __builtin_memset(full_page,0,0xB4+1);
  116. // zero function page, all registers (assuming full_page is all zeroes)
  117. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  118. palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
  119. palSetPad(GPIOB, 16);
  120. chThdSleepMilliseconds(10);
  121. // software shutdown
  122. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
  123. chThdSleepMilliseconds(10);
  124. // software shutdown disable (i.e. turn stuff on)
  125. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  126. chThdSleepMilliseconds(10);
  127. // zero all LED registers on all 8 pages
  128. uint8_t i;
  129. for(i=0; i<8; i++) {
  130. is31_write_data(i, full_page, 0xB4 + 1);
  131. chThdSleepMilliseconds(1);
  132. }
  133. }
  134. /* ==================
  135. * LED control thread
  136. * ================== */
  137. #define LED_MAILBOX_NUM_MSGS 5
  138. static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
  139. mailbox_t led_mailbox;
  140. static THD_WORKING_AREA(waLEDthread, 256);
  141. static THD_FUNCTION(LEDthread, arg) {
  142. (void)arg;
  143. chRegSetThreadName("LEDthread");
  144. uint8_t i;
  145. uint8_t control_register_word[2] = {0};//2 bytes: register address, byte to write
  146. uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
  147. //persistent status variables
  148. uint8_t pwm_step_status, page_status;
  149. //mailbox variables
  150. uint8_t temp, msg_type, msg_pin, msg_col, msg_led;
  151. msg_t msg;
  152. /* //control register variables
  153. uint8_t page, save_page, save_breath1, save_breath2;
  154. msg_t msg, retval;
  155. */
  156. // initialize persistent variables
  157. pwm_step_status = 4; //full brightness
  158. page_status = 0; //start frame 0 (all off/on)
  159. while(true) {
  160. // wait for a message (asynchronous)
  161. // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
  162. // be processed right away)
  163. chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
  164. msg_col = (msg >> 24) & 0xFF;//if needed
  165. msg_pin = (msg >> 16) & 0XFF;//if needed (SET_FULL_ROW)
  166. msg_type = (msg >> 8) & 0xFF; //second byte is msg type
  167. msg_led = (msg) & 0xFF; //first byte is action information
  168. xprintf("--------------------\n");
  169. xprintf("mailbox fetch\nmsg: %X\n", msg);
  170. chThdSleepMilliseconds(20);
  171. xprintf("type: %X - pin: %X\n", msg_type, msg_pin);
  172. chThdSleepMilliseconds(20);
  173. xprintf("col: %X - led: %X\n", msg_col, msg_led);
  174. chThdSleepMilliseconds(10);
  175. switch (msg_type){
  176. case KEY_LIGHT:
  177. //TODO: lighting key led on keypress
  178. break;
  179. case SET_FULL_ROW:
  180. //write full byte to pin address, msg_pin = pin #, msg_led = byte to write
  181. //writes only to current page
  182. xprintf("SET_FULL_ROW\n");
  183. write_led_byte(page_status,msg_pin,msg_led);
  184. break;
  185. case OFF_LED:
  186. //on/off/toggle single led, msg_led = row/col of led
  187. xprintf("OFF_LED: %d\n", msg_led);
  188. chThdSleepMilliseconds(10);
  189. set_led_bit(7, control_register_word, msg_led, 0);
  190. is31_write_data (7, control_register_word, 0x02);
  191. break;
  192. case ON_LED:
  193. xprintf("ON_LED: %d\n", msg_led);
  194. chThdSleepMilliseconds(10);
  195. set_led_bit(7, control_register_word, msg_led, 1);
  196. is31_write_data (7, control_register_word, 0x02);
  197. break;
  198. case TOGGLE_LED:
  199. xprintf("TOGGLE_LED: %d\n", msg_led);
  200. chThdSleepMilliseconds(10);
  201. set_led_bit(7, control_register_word, msg_led, 2);
  202. is31_write_data (7, control_register_word, 0x02);
  203. break;
  204. case BLINK_OFF_LED:
  205. //on/off/toggle single led, msg_led = row/col of led
  206. xprintf("BLINK_ON: %d\n", msg_led);
  207. chThdSleepMilliseconds(10);
  208. set_led_bit(7, control_register_word, msg_led, 4);
  209. is31_write_data (7, control_register_word, 0x02);
  210. break;
  211. case BLINK_ON_LED:
  212. xprintf("BLINK_OFF: %d\n", msg_led);
  213. chThdSleepMilliseconds(10);
  214. set_led_bit(7, control_register_word, msg_led, 5);
  215. is31_write_data (7, control_register_word, 0x02);
  216. break;
  217. case BLINK_TOGGLE_LED:
  218. xprintf("BLINK_TOGGLE: %d\n", msg_led);
  219. chThdSleepMilliseconds(10);
  220. set_led_bit(7, control_register_word, msg_led, 6);
  221. is31_write_data (7, control_register_word, 0x02);
  222. break;
  223. case TOGGLE_ALL:
  224. xprintf("TOGGLE_ALL: %d\n", msg_led);
  225. chThdSleepMilliseconds(10);
  226. //msg_led = unused
  227. is31_read_register(0, 0x00, &temp);//if first byte is on, then toggle frame 0 off
  228. led_control_reg[0] = 0;
  229. if (temp==0 || page_status > 0) {
  230. __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
  231. } else {
  232. __builtin_memset(led_control_reg+1, 0, 0x12);
  233. }
  234. is31_write_data(0, led_control_reg, 0x13);
  235. if (page_status > 0) {
  236. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  237. page_status=0;
  238. //maintain lock leds
  239. led_set(host_keyboard_leds());
  240. }
  241. break;
  242. case TOGGLE_BACKLIGHT:
  243. //msg_led = on/off
  244. xprintf("TOGGLE_BACKLIGHT\n");
  245. chThdSleepMilliseconds(10);
  246. //populate the 9 byte rows to be written to each pin, first byte is register (pin) address
  247. if (msg_led == 1) {
  248. __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
  249. } else {
  250. __builtin_memset(pwm_register_array+1, 0, 8);
  251. }
  252. for(i=0; i<8; i++) {
  253. //first byte is register address, every 0x10 9 bytes is A-register pwm pins
  254. pwm_register_array[0] = 0x24 + (i * 0x10);
  255. is31_write_data(0,pwm_register_array,9);
  256. }
  257. break;
  258. case DISPLAY_PAGE://show single layer indicator or full map of layer
  259. //msg_led = page to toggle on
  260. xprintf("DISPLAY_PAGE\n");
  261. chThdSleepMilliseconds(10);
  262. if (page_status != msg_led) {
  263. xprintf(" - new page\n");
  264. chThdSleepMilliseconds(10);
  265. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_led);
  266. page_status = msg_led;
  267. //maintain lock leds
  268. led_set(host_keyboard_leds());
  269. }
  270. break;
  271. case RESET_PAGE:
  272. //led_msg = page to reset
  273. xprintf("RESET_PAGE\n");
  274. chThdSleepMilliseconds(10);
  275. led_control_reg[0] = 0;
  276. __builtin_memset(led_control_reg+1, 0, 0x12);
  277. is31_write_data(msg_led, led_control_reg, 0x13);
  278. break;
  279. case TOGGLE_NUM_LOCK:
  280. //msg_led = 0 or 1, off/on
  281. xprintf("NUMLOCK: %d\n", msg_led);
  282. chThdSleepMilliseconds(10);
  283. set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_led, page_status);
  284. break;
  285. case TOGGLE_CAPS_LOCK:
  286. xprintf("CAPSLOCK: %d\n", msg_led);
  287. chThdSleepMilliseconds(10);
  288. //msg_led = 0 or 1, off/on
  289. set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_led, page_status);
  290. break;
  291. //TODO: MODE_BREATH
  292. case STEP_BRIGHTNESS:
  293. xprintf("STEP_BACKLIGHT\n");
  294. chThdSleepMilliseconds(10);
  295. //led_msg = step pwm up or down
  296. switch (msg_led) {
  297. case 0:
  298. if (pwm_step_status == 0) {
  299. pwm_step_status = 4;
  300. } else {
  301. pwm_step_status--;
  302. }
  303. break;
  304. case 1:
  305. if (pwm_step_status == 4) {
  306. pwm_step_status = 0;
  307. } else {
  308. pwm_step_status++;
  309. }
  310. break;
  311. }
  312. //populate 8 byte rows to write on each pin
  313. //first byte is register address, every 0x10 9 bytes are A-register pwm pins
  314. __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
  315. for(i=0; i<8; i++) {
  316. pwm_register_array[0] = 0x24 + (i * 0x10);
  317. is31_write_data(0,pwm_register_array,9);
  318. }
  319. break;
  320. /* case LED_MSG_SLEEP_LED_ON:
  321. // save current settings
  322. is31_read_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, &save_page);
  323. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, &save_breath1);
  324. is31_read_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, &save_breath2);
  325. // use pages 7 and 8 for (hardware) breathing (assuming they're empty)
  326. is31_write_register(6, BREATHE_LED_ADDRESS, 0xFF);
  327. is31_write_register(7, BREATHE_LED_ADDRESS, 0x00);
  328. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (6<<4)|6);
  329. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  330. retval = MSG_TIMEOUT;
  331. temp = 6;
  332. while(retval == MSG_TIMEOUT) {
  333. // switch to the other page
  334. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, temp);
  335. temp = (temp == 6 ? 7 : 6);
  336. // the times should be sufficiently long for IS31 to finish switching pages
  337. retval = chMBFetch(&led_mailbox, &msg, MS2ST(temp == 6 ? 4000 : 6000));
  338. }
  339. // received a message (should be a wakeup), so restore previous state
  340. chThdSleepMilliseconds(3000); // need to wait until the page change finishes
  341. // note: any other messages are queued
  342. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, save_breath1);
  343. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, save_breath2);
  344. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, save_page);
  345. break;
  346. case LED_MSG_SLEEP_LED_OFF:
  347. // should not get here; wakeup should be received in the branch above break;
  348. break;
  349. */
  350. xprintf("--------------------\n");
  351. chThdSleepMilliseconds(10);
  352. }
  353. }
  354. }
  355. /* ==============================
  356. * debug function
  357. * ============================== */
  358. void print_debug(uint8_t page) {
  359. uint8_t j, debug_temp;
  360. //debugging code - print full led/blink/pwm registers on each frame
  361. xprintf("----layer state----: %X\n", layer_state);
  362. xprintf("page: %d\n", page);
  363. chThdSleepMilliseconds(10);
  364. for(j=0;j<0x24;j++){
  365. if(j > 0 && j % 9 == 0){
  366. xprintf("\n");
  367. }
  368. switch (j) {
  369. case 0:
  370. xprintf("\n--on-off--\n");
  371. chThdSleepMilliseconds(10);
  372. break;
  373. case 0x12:
  374. xprintf("\n--blink--\n");
  375. chThdSleepMilliseconds(10);
  376. break;
  377. }
  378. is31_read_register(page,j,&debug_temp);
  379. xprintf("%02X, ", debug_temp);
  380. chThdSleepMilliseconds(10);
  381. }
  382. xprintf("\n--pwm--\n");
  383. chThdSleepMilliseconds(10);
  384. for(j=0x24;j<0xB4;j++) {
  385. is31_read_register(page,j,&debug_temp);
  386. xprintf("%02X, ", debug_temp);
  387. chThdSleepMilliseconds(10);
  388. if(j > 0x24 && (j-3) % 8 == 0){
  389. xprintf("\n");
  390. }
  391. }
  392. xprintf("\n");
  393. //Function Register
  394. xprintf("\n--FUNCTION--\n");
  395. chThdSleepMilliseconds(10);
  396. for(j=0;j<0x0D;j++) {
  397. is31_read_register(0x0B,j,&debug_temp);
  398. switch(j) {
  399. case 0:
  400. xprintf("Config %02X", debug_temp);
  401. chThdSleepMilliseconds(2);
  402. break;
  403. case 1:
  404. xprintf(" - Pict %02X\n", debug_temp);
  405. chThdSleepMilliseconds(2);
  406. break;
  407. case 2:
  408. xprintf("Auto1 %02X", debug_temp);
  409. chThdSleepMilliseconds(2);
  410. break;
  411. case 3:
  412. xprintf(" - Auto2 %02X\n", debug_temp);
  413. chThdSleepMilliseconds(2);
  414. break;
  415. case 5:
  416. xprintf("Disp %02X", debug_temp);
  417. chThdSleepMilliseconds(2);
  418. break;
  419. case 6:
  420. xprintf(" - Audio %02X\n", debug_temp);
  421. chThdSleepMilliseconds(2);
  422. break;
  423. case 7:
  424. xprintf("Frame %02X", debug_temp);
  425. chThdSleepMilliseconds(2);
  426. break;
  427. case 8:
  428. xprintf(" - Breath1 %02X\n", debug_temp);
  429. chThdSleepMilliseconds(2);
  430. break;
  431. case 9:
  432. xprintf("Breath2 %02X - ", debug_temp);
  433. chThdSleepMilliseconds(2);
  434. break;
  435. case 10:
  436. xprintf(" - Shut %02X\n", debug_temp);
  437. chThdSleepMilliseconds(2);
  438. break;
  439. case 11:
  440. xprintf("AGC %02X", debug_temp);
  441. chThdSleepMilliseconds(2);
  442. break;
  443. case 12:
  444. xprintf(" - ADC %02X\n", debug_temp);
  445. chThdSleepMilliseconds(2);
  446. break;
  447. }
  448. }
  449. }
  450. /* ==============================
  451. * led processing functions
  452. * ============================== */
  453. void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint8_t action) {
  454. //returns 2 bytes led control register address and byte to write
  455. //0 - bit off, 1 - bit on, 2 - toggle bit
  456. uint8_t control_reg_addr, column_bit, column_byte, bit_temp, blink_on;
  457. //check for valid led address
  458. if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) {
  459. xprintf("Invalid address: %d\n", led_addr);
  460. return;
  461. }
  462. xprintf("set_led_bit: %d\n", led_addr);
  463. xprintf("action: %d\n", action);
  464. chThdSleepMilliseconds(10);
  465. //check blink bit
  466. blink_on = action>>2;
  467. action &= ~(1<<2); //strip blink bit
  468. //first byte is led control register address 0x00
  469. //msg_led tens column is pin#, ones column is bit position in 8-bit mask
  470. control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-register is every other byte
  471. xprintf("pre-reg_addr: %2X\n", control_reg_addr);
  472. chThdSleepMilliseconds(10);
  473. control_reg_addr += blink_on == 1 ? 0x12 : 0x00;//shift 12 bytes to blink register
  474. xprintf("blink-reg_addr: %2X\n", control_reg_addr);
  475. chThdSleepMilliseconds(10);
  476. xprintf("page: %2X\n", page);
  477. chThdSleepMilliseconds(10);
  478. is31_read_register(page, 0x06, &bit_temp);//maintain status of leds on this byte
  479. xprintf("reg 06: %2X\n", bit_temp);
  480. is31_read_register(page, 0x17, &bit_temp);//maintain status of leds on this byte
  481. xprintf("reg 17: %2X\n", bit_temp);
  482. is31_read_register(page, 0x18, &bit_temp);//maintain status of leds on this byte
  483. xprintf("reg 18: %2X\n", bit_temp);
  484. is31_read_register(page, 0x19, &bit_temp);//maintain status of leds on this byte
  485. xprintf("reg 19: %2X\n", bit_temp);
  486. is31_read_register(page, control_reg_addr, &bit_temp);//maintain status of leds on this byte
  487. column_bit = 1<<(led_addr % 10 - 1);
  488. column_byte = bit_temp;
  489. xprintf("column_byte read: %2X\n", column_byte);
  490. chThdSleepMilliseconds(10);
  491. switch(action) {
  492. case 0:
  493. column_byte &= ~column_bit;
  494. break;
  495. case 1:
  496. column_byte |= column_bit;
  497. break;
  498. case 2:
  499. column_byte ^= column_bit;
  500. break;
  501. }
  502. xprintf("column_byte write: %2X\n", column_byte);
  503. chThdSleepMilliseconds(10);
  504. //return word to be written in register
  505. led_control_reg[0] = control_reg_addr;
  506. led_control_reg[1] = column_byte;
  507. }
  508. void write_led_byte (uint8_t page, uint8_t row, uint8_t led_byte) {
  509. uint8_t led_control_word[2] = {0};//register address and on/off byte
  510. led_control_word[0] = (row - 1 ) * 0x02;// A-register is every other byte
  511. led_control_word[1] = led_byte;
  512. is31_write_data(page, led_control_word, 0x02);
  513. }
  514. void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
  515. uint8_t i;
  516. uint8_t pin, col;
  517. uint8_t led_control_register[0x13] = {0};//control register start address + 0x12 bytes
  518. __builtin_memset(led_control_register,0,13);
  519. for(i=0;i<led_count;i++){
  520. // 1 byte shift for led register 0x00 address
  521. pin = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;
  522. col = user_led_array[i] % 10 - 1;
  523. led_control_register[pin] |= 1<<(col);
  524. }
  525. is31_write_data(page, led_control_register, 0x13);
  526. }
  527. void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) {
  528. uint8_t lock_temp;
  529. uint8_t led_control_word[2] = {0};
  530. xprintf("---set lock---\n");
  531. chThdSleepMilliseconds(10);
  532. //blink if all leds are on
  533. if (page == 0) {
  534. is31_read_register(0, 0x00, &lock_temp);
  535. xprintf("AllOnReg: %2X\n", lock_temp);
  536. chThdSleepMilliseconds(10);
  537. if (lock_temp == 0xFF) {
  538. xprintf("AllOntrue\n");
  539. chThdSleepMilliseconds(10);
  540. led_action |= (1<<2); //set blink bit
  541. } else {
  542. xprintf("AllOnfalse\n");
  543. chThdSleepMilliseconds(10);
  544. }
  545. }
  546. set_led_bit(page,led_control_word,led_addr,led_action);
  547. xprintf("led_word: %2X", led_control_word[0]);
  548. xprintf("%X\n", led_control_word[1]);
  549. chThdSleepMilliseconds(10);
  550. is31_write_data(page, led_control_word, 0x02);
  551. }
  552. /* =====================
  553. * hook into user keymap
  554. * ===================== */
  555. void led_controller_init(void) {
  556. uint8_t i;
  557. /* initialise I2C */
  558. /* I2C pins */
  559. palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
  560. palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
  561. /* start I2C */
  562. i2cStart(&I2CD1, &i2ccfg);
  563. // try high drive (from kiibohd)
  564. I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
  565. // try glitch fixing (from kiibohd)
  566. I2CD1.i2c->FLT = 4;
  567. chThdSleepMilliseconds(10);
  568. /* initialise IS31 chip */
  569. is31_init();
  570. //set Display Option Register so all pwm intensity is controlled from page 0
  571. //enable blink and set blink period to 0.27s x rate
  572. is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME + IS31_REG_DISPLAYOPT_BLINK_ENABLE + 4);
  573. /* set full pwm on page 1 */
  574. pwm_register_array[0] = 0;
  575. __builtin_memset(pwm_register_array+1, 0xFF, 8);
  576. for(i=0; i<8; i++) {
  577. pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
  578. is31_write_data(0, pwm_register_array, 9);
  579. chThdSleepMilliseconds(5);
  580. }
  581. /* enable breathing when the displayed page changes */
  582. // Fade-in Fade-out, time = 26ms * 2^N, N=3
  583. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
  584. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  585. /* more time consuming LED processing should be offloaded into
  586. * a thread, with asynchronous messaging. */
  587. chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
  588. chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
  589. }
  590. //TODO: Don't know equivalent QMK hooks for these
  591. //
  592. //void hook_usb_suspend_entry(void) {
  593. //#ifdef SLEEP_LED_ENABLE
  594. // chSysLockFromISR();
  595. // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_ON);
  596. // chSysUnlockFromISR();
  597. //#endif /* SLEEP_LED_ENABLE */
  598. //}
  599. //
  600. //void hook_usb_suspend_loop(void) {
  601. // chThdSleepMilliseconds(100);
  602. // /* Remote wakeup */
  603. // if((USB_DRIVER.status & 2) && suspend_wakeup_condition()) {
  604. // send_remote_wakeup(&USB_DRIVER);
  605. // }
  606. //}
  607. //
  608. //void hook_usb_wakeup(void) {
  609. //#ifdef SLEEP_LED_ENABLE
  610. // chSysLockFromISR();
  611. // chMBPostI(&led_mailbox, LED_MSG_SLEEP_LED_OFF);
  612. // chSysUnlockFromISR();
  613. //#endif /* SLEEP_LED_ENABLE */
  614. //}
  615. //*/