2
0

led_controller.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. * 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. #if !defined(CAPS_LOCK_LED_ADDRESS)
  48. #define CAPS_LOCK_LED_ADDRESS 46
  49. #endif
  50. #if !defined(NUM_LOCK_LED_ADDRESS)
  51. #define NUM_LOCK_LED_ADDRESS 85
  52. #endif
  53. /* Which LED should breathe during sleep */
  54. #if !defined(BREATHE_LED_ADDRESS)
  55. #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
  56. #endif
  57. /* =================
  58. * ChibiOS I2C setup
  59. * ================= */
  60. static const I2CConfig i2ccfg = {
  61. 400000 // clock speed (Hz); 400kHz max for IS31
  62. };
  63. /* ==============
  64. * variables
  65. * ============== */
  66. // internal communication buffers
  67. uint8_t tx[2] __attribute__((aligned(2)));
  68. uint8_t rx[1] __attribute__((aligned(2)));
  69. // buffer for sending the whole page at once (used also as a temp buffer)
  70. uint8_t full_page[0xB4+1] = {0};
  71. // LED mask (which LEDs are present, selected by bits)
  72. // IC60 pcb uses only CA matrix.
  73. // Each byte is a control pin for 8 leds ordered 8-1
  74. const uint8_t all_on_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_register_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. palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
  116. palSetPad(GPIOB, 16);
  117. chThdSleepMilliseconds(10);
  118. // software shutdown
  119. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, 0);
  120. chThdSleepMilliseconds(10);
  121. // software shutdown disable (i.e. turn stuff on)
  122. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  123. chThdSleepMilliseconds(10);
  124. // zero all LED registers on all 8 pages
  125. uint8_t i;
  126. for(i=0; i<8; i++) {
  127. is31_write_data(i, full_page, 0xB4 + 1);
  128. chThdSleepMilliseconds(1);
  129. }
  130. }
  131. /* ==================
  132. * LED control thread
  133. * ================== */
  134. #define LED_MAILBOX_NUM_MSGS 5
  135. static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
  136. mailbox_t led_mailbox;
  137. static THD_WORKING_AREA(waLEDthread, 256);
  138. static THD_FUNCTION(LEDthread, arg) {
  139. (void)arg;
  140. chRegSetThreadName("LEDthread");
  141. uint8_t i;
  142. uint8_t control_register_word[2] = {0};//2 bytes: register address, byte to write
  143. uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
  144. //persistent status variables
  145. uint8_t pwm_step_status, page_status;
  146. //mailbox variables
  147. uint8_t temp, msg_type, msg_pin, msg_col, msg_led;
  148. msg_t msg;
  149. // initialize persistent variables
  150. pwm_step_status = 4; //full brightness
  151. page_status = 0; //start frame 0 (all off/on)
  152. while(true) {
  153. // wait for a message (asynchronous)
  154. // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
  155. // be processed right away)
  156. chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
  157. msg_col = (msg >> 24) & 0xFF;//if needed
  158. msg_pin = (msg >> 16) & 0XFF;//if needed (e.g. SET_FULL_ROW)
  159. msg_type = (msg >> 8) & 0xFF; //second byte is msg type
  160. msg_led = (msg) & 0xFF; //first byte is action information
  161. switch (msg_type){
  162. case SET_FULL_ROW:
  163. //write full byte to pin address, msg_pin = pin #, msg_led = byte to write
  164. //writes only to current page
  165. write_led_byte(page_status,msg_pin,msg_led);
  166. break;
  167. case OFF_LED:
  168. //on/off/toggle single led, msg_led = row/col of led
  169. set_led_bit(7, control_register_word, msg_led, 0);
  170. is31_write_data (7, control_register_word, 0x02);
  171. break;
  172. case ON_LED:
  173. set_led_bit(7, control_register_word, msg_led, 1);
  174. is31_write_data (7, control_register_word, 0x02);
  175. break;
  176. case TOGGLE_LED:
  177. set_led_bit(7, control_register_word, msg_led, 2);
  178. is31_write_data (7, control_register_word, 0x02);
  179. break;
  180. case BLINK_OFF_LED:
  181. //on/off/toggle single led, msg_led = row/col of led
  182. set_led_bit(7, control_register_word, msg_led, 4);
  183. is31_write_data (7, control_register_word, 0x02);
  184. break;
  185. case BLINK_ON_LED:
  186. set_led_bit(7, control_register_word, msg_led, 5);
  187. is31_write_data (7, control_register_word, 0x02);
  188. break;
  189. case BLINK_TOGGLE_LED:
  190. set_led_bit(7, control_register_word, msg_led, 6);
  191. is31_write_data (7, control_register_word, 0x02);
  192. break;
  193. case TOGGLE_ALL:
  194. //msg_led = unused
  195. is31_read_register(0, 0x00, &temp);
  196. led_control_reg[0] = 0;
  197. //if first byte is on, then toggle frame 0 off
  198. if (temp==0 || page_status > 0) {
  199. __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
  200. } else {
  201. __builtin_memset(led_control_reg+1, 0, 0x12);
  202. }
  203. is31_write_data(0, led_control_reg, 0x13);
  204. if (page_status > 0) {
  205. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  206. page_status=0;
  207. //maintain lock leds
  208. led_set(host_keyboard_leds());
  209. }
  210. break;
  211. case TOGGLE_BACKLIGHT:
  212. //msg_led = on/off
  213. //populate the 9 byte rows to be written to each pin, first byte is register (pin) address
  214. if (msg_led == 1) {
  215. __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
  216. } else {
  217. __builtin_memset(pwm_register_array+1, 0, 8);
  218. }
  219. for(i=0; i<8; i++) {
  220. //first byte is register address, every 0x10 9 bytes is A-register pwm pins
  221. pwm_register_array[0] = 0x24 + (i * 0x10);
  222. is31_write_data(0,pwm_register_array,9);
  223. }
  224. break;
  225. case DISPLAY_PAGE:
  226. //msg_led = page to toggle on
  227. if (page_status != msg_led) {
  228. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_led);
  229. page_status = msg_led;
  230. //maintain lock leds
  231. led_set(host_keyboard_leds());
  232. }
  233. break;
  234. case RESET_PAGE:
  235. //led_msg = page to reset
  236. led_control_reg[0] = 0;
  237. __builtin_memset(led_control_reg+1, 0, 0x12);
  238. is31_write_data(msg_led, led_control_reg, 0x13);
  239. break;
  240. case TOGGLE_NUM_LOCK:
  241. //msg_led = 0 or 1, off/on
  242. set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_led, page_status);
  243. break;
  244. case TOGGLE_CAPS_LOCK:
  245. //msg_led = 0 or 1, off/on
  246. set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_led, page_status);
  247. break;
  248. case STEP_BRIGHTNESS:
  249. //led_msg = step pwm up or down
  250. switch (msg_led) {
  251. case 0:
  252. if (pwm_step_status == 0) {
  253. pwm_step_status = 4;
  254. } else {
  255. pwm_step_status--;
  256. }
  257. break;
  258. case 1:
  259. if (pwm_step_status == 4) {
  260. pwm_step_status = 0;
  261. } else {
  262. pwm_step_status++;
  263. }
  264. break;
  265. }
  266. //populate 8 byte rows to write on each pin
  267. //first byte is register address, every 0x10 9 bytes are A-register pwm pins
  268. __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
  269. for(i=0; i<8; i++) {
  270. pwm_register_array[0] = 0x24 + (i * 0x10);
  271. is31_write_data(0,pwm_register_array,9);
  272. }
  273. break;
  274. }
  275. }
  276. }
  277. /* ==============================
  278. * led processing functions
  279. * ============================== */
  280. void set_led_bit (uint8_t page, uint8_t *led_control_reg, uint8_t led_addr, uint8_t action) {
  281. //returns 2 bytes: led control register address and byte to write
  282. //0 - bit off, 1 - bit on, 2 - toggle bit
  283. uint8_t control_reg_addr, column_bit, column_byte, bit_temp, blink_on;
  284. //check for valid led address
  285. if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) {
  286. return;
  287. }
  288. //check for blink bit
  289. blink_on = action>>2;
  290. action &= ~(1<<2); //strip blink bit
  291. //first byte is led control register address 0x00
  292. //msg_led tens column is pin#, ones column is bit position in 8-bit mask
  293. control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-register is every other byte
  294. control_reg_addr += blink_on == 1 ? 0x12 : 0x00;//shift 12 bytes to blink register
  295. is31_read_register(page, control_reg_addr, &bit_temp);//maintain status of leds on this byte
  296. column_bit = 1<<(led_addr % 10 - 1);
  297. column_byte = bit_temp;
  298. switch(action) {
  299. case 0:
  300. column_byte &= ~column_bit;
  301. break;
  302. case 1:
  303. column_byte |= column_bit;
  304. break;
  305. case 2:
  306. column_byte ^= column_bit;
  307. break;
  308. }
  309. //return word to be written in register
  310. led_control_reg[0] = control_reg_addr;
  311. led_control_reg[1] = column_byte;
  312. }
  313. void write_led_byte (uint8_t page, uint8_t row, uint8_t led_byte) {
  314. uint8_t led_control_word[2] = {0};//register address and on/off byte
  315. led_control_word[0] = (row - 1 ) * 0x02;// A-register is every other byte
  316. led_control_word[1] = led_byte;
  317. is31_write_data(page, led_control_word, 0x02);
  318. }
  319. void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
  320. uint8_t i;
  321. uint8_t pin, col;
  322. uint8_t led_control_register[0x13] = {0};
  323. __builtin_memset(led_control_register,0,13);
  324. for(i=0;i<led_count;i++){
  325. // 1 byte shift for led register 0x00 address
  326. pin = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;
  327. col = user_led_array[i] % 10 - 1;
  328. led_control_register[pin] |= 1<<(col);
  329. }
  330. is31_write_data(page, led_control_register, 0x13);
  331. }
  332. void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) {
  333. uint8_t lock_temp;
  334. uint8_t led_control_word[2] = {0};
  335. //blink if all leds are on
  336. if (page == 0) {
  337. is31_read_register(0, 0x00, &lock_temp);
  338. if (lock_temp == 0xFF) {
  339. led_action |= (1<<2); //set blink bit
  340. }
  341. }
  342. set_led_bit(page,led_control_word,led_addr,led_action);
  343. is31_write_data(page, led_control_word, 0x02);
  344. }
  345. /* =====================
  346. * hook into user keymap
  347. * ===================== */
  348. void led_controller_init(void) {
  349. uint8_t i;
  350. /* initialise I2C */
  351. /* I2C pins */
  352. palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
  353. palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
  354. /* start I2C */
  355. i2cStart(&I2CD1, &i2ccfg);
  356. // try high drive (from kiibohd)
  357. I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
  358. // try glitch fixing (from kiibohd)
  359. I2CD1.i2c->FLT = 4;
  360. chThdSleepMilliseconds(10);
  361. /* initialise IS31 chip */
  362. is31_init();
  363. //set Display Option Register so all pwm intensity is controlled from page 0
  364. //enable blink and set blink period to 0.27s x rate
  365. is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME + IS31_REG_DISPLAYOPT_BLINK_ENABLE + 4);
  366. /* set full pwm on page 1 */
  367. pwm_register_array[0] = 0;
  368. __builtin_memset(pwm_register_array+1, 0xFF, 8);
  369. for(i=0; i<8; i++) {
  370. pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
  371. is31_write_data(0, pwm_register_array, 9);
  372. chThdSleepMilliseconds(5);
  373. }
  374. /* enable breathing when the displayed page changes */
  375. // Fade-in Fade-out, time = 26ms * 2^N, N=3
  376. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
  377. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  378. /* more time consuming LED processing should be offloaded into
  379. * a thread, with asynchronous messaging. */
  380. chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
  381. chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
  382. }