led_controller.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 "host.h"
  24. #include "led_controller.h"
  25. #include "suspend.h"
  26. #include "usb_main.h"
  27. /* Infinity60 LED MAP
  28. - digits mean "row" and "col", i.e. 45 means C4-5 in the IS31 datasheet, matrix A
  29. 11 12 13 14 15 16 17 18 21 22 23 24 25 26 27*
  30. 28 31 32 33 34 35 36 37 38 41 42 43 44 45
  31. 46 47 48 51 52 53 54 55 56 57 58 61 62
  32. 63 64 65 66 67 68 71 72 73 74 75 76 77*
  33. 78 81 82 83 84 85 86 87
  34. *Unused in Alphabet Layout
  35. */
  36. /*
  37. each page has 0xB4 bytes
  38. 0 - 0x11: LED control (on/off):
  39. order: CA1, CB1, CA2, CB2, .... (CA - matrix A, CB - matrix B)
  40. CAn controls Cn-8 .. Cn-1 (LSbit)
  41. 0x12 - 0x23: blink control (like "LED control")
  42. 0x24 - 0xB3: PWM control: byte per LED, 0xFF max on
  43. order same as above (CA 1st row (8bytes), CB 1st row (8bytes), ...)
  44. */
  45. // Which LED should be used for CAPS LOCK indicator
  46. #if !defined(CAPS_LOCK_LED_ADDRESS)
  47. #define CAPS_LOCK_LED_ADDRESS 46
  48. #endif
  49. #if !defined(NUM_LOCK_LED_ADDRESS)
  50. #define NUM_LOCK_LED_ADDRESS 85
  51. #endif
  52. /* Which LED should breathe during sleep */
  53. #if !defined(BREATHE_LED_ADDRESS)
  54. #define BREATHE_LED_ADDRESS CAPS_LOCK_LED_ADDRESS
  55. #endif
  56. /* =================
  57. * ChibiOS I2C setup
  58. * ================= */
  59. static const I2CConfig i2ccfg = {
  60. 400000 // clock speed (Hz); 400kHz max for IS31
  61. };
  62. /* ==============
  63. * variables
  64. * ============== */
  65. // internal communication buffers
  66. uint8_t tx[2] __attribute__((aligned(2)));
  67. uint8_t rx[1] __attribute__((aligned(2)));
  68. // buffer for sending the whole page at once (used also as a temp buffer)
  69. uint8_t full_page[0xB4+1] = {0};
  70. // LED mask (which LEDs are present, selected by bits)
  71. // IC60 pcb uses only CA matrix.
  72. // Each byte is a control pin for 8 leds ordered 8-1
  73. const uint8_t all_on_leds_mask[0x12] = {
  74. 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
  75. 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x7F, 0x00, 0x00, 0x00
  76. };
  77. // array to hold brightness pwm steps
  78. const uint8_t pwm_levels[5] = {
  79. 0x00, 0x16, 0x4E, 0xA1, 0xFF
  80. };
  81. // array to write to pwm register
  82. uint8_t pwm_register_array[9] = {0};
  83. /* ============================
  84. * communication functions
  85. * ============================ */
  86. msg_t is31_select_page(uint8_t page) {
  87. tx[0] = IS31_COMMANDREGISTER;
  88. tx[1] = page;
  89. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  90. }
  91. msg_t is31_write_data(uint8_t page, uint8_t *buffer, uint8_t size) {
  92. is31_select_page(page);
  93. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, buffer, size, NULL, 0, US2ST(IS31_TIMEOUT));
  94. }
  95. msg_t is31_write_register(uint8_t page, uint8_t reg, uint8_t data) {
  96. is31_select_page(page);
  97. tx[0] = reg;
  98. tx[1] = data;
  99. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 2, NULL, 0, US2ST(IS31_TIMEOUT));
  100. }
  101. msg_t is31_read_register(uint8_t page, uint8_t reg, uint8_t *result) {
  102. is31_select_page(page);
  103. tx[0] = reg;
  104. return i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, tx, 1, result, 1, US2ST(IS31_TIMEOUT));
  105. }
  106. /* ========================
  107. * initialise the IS31 chip
  108. * ======================== */
  109. void is31_init(void) {
  110. // just to be sure that it's all zeroes
  111. __builtin_memset(full_page,0,0xB4+1);
  112. // zero function page, all registers (assuming full_page is all zeroes)
  113. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  114. palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
  115. palSetPad(GPIOB, 16);
  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(5);
  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 control_register_word[2] = {0};//2 bytes: register address, byte to write
  139. uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
  140. //persistent status variables
  141. uint8_t pwm_step_status, page_status;
  142. //mailbox variables
  143. uint8_t temp, msg_type;
  144. uint8_t msg_args[3];
  145. msg_t msg;
  146. // initialize persistent variables
  147. pwm_step_status = 4; //full brightness
  148. page_status = 0; //start frame 0 (all off/on)
  149. while(true) {
  150. // wait for a message (asynchronous)
  151. // (messages are queued (up to LED_MAILBOX_NUM_MSGS) if they can't
  152. // be processed right away
  153. chMBFetch(&led_mailbox, &msg, TIME_INFINITE);
  154. msg_type = msg & 0xFF; //first byte is action information
  155. msg_args[0] = (msg >> 8) & 0xFF;
  156. msg_args[1] = (msg >> 16) & 0XFF;
  157. msg_args[2] = (msg >> 24) & 0xFF;
  158. switch (msg_type){
  159. case SET_FULL_ROW:
  160. //write full byte to pin address, msg_args[1] = pin #, msg_args[0] = 8 bits to write
  161. //writes only to currently displayed page
  162. write_led_byte(page_status, msg_args[1], msg_args[0]);
  163. break;
  164. case OFF_LED:
  165. //on/off/toggle single led, msg_args[0] = row/col of led, msg_args[1] = page
  166. set_led_bit(msg_args[1], control_register_word, msg_args[0], 0);
  167. break;
  168. case ON_LED:
  169. set_led_bit(msg_args[1], control_register_word, msg_args[0], 1);
  170. break;
  171. case TOGGLE_LED:
  172. set_led_bit(msg_args[1], control_register_word, msg_args[0], 2);
  173. break;
  174. case BLINK_OFF_LED:
  175. //on/off/toggle single led, msg_args[0] = row/col of led
  176. set_led_bit(msg_args[1], control_register_word, msg_args[0], 4);
  177. break;
  178. case BLINK_ON_LED:
  179. set_led_bit(msg_args[1], control_register_word, msg_args[0], 5);
  180. break;
  181. case BLINK_TOGGLE_LED:
  182. set_led_bit(msg_args[1], control_register_word, msg_args[0], 6);
  183. break;
  184. case TOGGLE_ALL:
  185. //turn on/off all leds, msg_args = unused
  186. is31_read_register(0, 0x00, &temp);
  187. led_control_reg[0] = 0;
  188. //if first leds are already on, toggle frame 0 off
  189. if (temp==0 || page_status > 0) {
  190. __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
  191. } else {
  192. __builtin_memset(led_control_reg+1, 0, 0x12);
  193. }
  194. is31_write_data(0, led_control_reg, 0x13);
  195. if (page_status > 0) {
  196. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  197. page_status=0;
  198. //maintain lock leds
  199. led_set(host_keyboard_leds());
  200. }
  201. break;
  202. case TOGGLE_BACKLIGHT:
  203. //msg_args[0] = on/off
  204. //populate 9 byte rows to be written to each pin, first byte is register (pin) address
  205. if (msg_args[0] == 1) {
  206. __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
  207. } else {
  208. __builtin_memset(pwm_register_array+1, 0, 8);
  209. }
  210. for(i=0; i<8; i++) {
  211. //first byte is register address, every 0x10 9 bytes is A-matrix pwm pins
  212. pwm_register_array[0] = 0x24 + (i * 0x10);
  213. is31_write_data(0,pwm_register_array,9);
  214. }
  215. break;
  216. case DISPLAY_PAGE:
  217. //msg_args[0] = page to toggle on
  218. if (page_status != msg_args[0]) {
  219. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_args[0]);
  220. page_status = msg_args[0];
  221. //maintain lock leds
  222. led_set(host_keyboard_leds());
  223. }
  224. break;
  225. case RESET_PAGE:
  226. //led_args[0] = page to reset
  227. led_control_reg[0] = 0;
  228. __builtin_memset(led_control_reg+1, 0, 0x12);
  229. is31_write_data(msg_args[0], led_control_reg, 0x13);
  230. //repeat for blink register
  231. led_control_reg[0] = 0x12;
  232. is31_write_data(msg_args[0], led_control_reg, 0x13);
  233. break;
  234. case TOGGLE_NUM_LOCK:
  235. //msg_args[0] = 0 or 1, off/on
  236. set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_args[0], page_status);
  237. break;
  238. case TOGGLE_CAPS_LOCK:
  239. //msg_args[0] = 0 or 1, off/on
  240. set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_args[0], page_status);
  241. break;
  242. case STEP_BRIGHTNESS:
  243. //led_args[0] = step up (1) or down (0)
  244. switch (msg_args[0]) {
  245. case 0:
  246. if (pwm_step_status == 0) {
  247. pwm_step_status = 4;
  248. } else {
  249. pwm_step_status--;
  250. }
  251. break;
  252. case 1:
  253. if (pwm_step_status == 4) {
  254. pwm_step_status = 0;
  255. } else {
  256. pwm_step_status++;
  257. }
  258. break;
  259. }
  260. //populate 8 byte arrays to write on each pin
  261. //first byte is register address, every 0x10 9 bytes are A-matrix pwm pins
  262. __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
  263. for(i=0; i<8; i++) {
  264. pwm_register_array[0] = 0x24 + (i * 0x10);
  265. is31_write_data(0,pwm_register_array,9);
  266. }
  267. break;
  268. }
  269. }
  270. }
  271. /* ==============================
  272. * led processing functions
  273. * ============================== */
  274. void set_led_bit (uint8_t page, uint8_t *led_control_word, uint8_t led_addr, uint8_t action) {
  275. //returns 2 bytes: led control register address and byte to write
  276. //action: 0 - off, 1 - on, 2 - toggle, 4 - blink on, 5 - blink off, 6 - toggle blink
  277. uint8_t control_reg_addr, column_bit, column_byte, temp, blink_bit;
  278. //check for valid led address
  279. if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) {
  280. return;
  281. }
  282. blink_bit = action>>2;//check for blink bit
  283. action &= ~(1<<2); //strip blink bit
  284. //led_addr tens column is pin#, ones column is bit position in 8-bit mask
  285. control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-matrix is every other byte
  286. control_reg_addr += blink_bit == 1 ? 0x12 : 0x00;//if blink_bit, shift 12 bytes to blink register
  287. is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte
  288. column_bit = 1<<(led_addr % 10 - 1);
  289. column_byte = temp;
  290. switch(action) {
  291. case 0:
  292. column_byte &= ~column_bit;
  293. break;
  294. case 1:
  295. column_byte |= column_bit;
  296. break;
  297. case 2:
  298. column_byte ^= column_bit;
  299. break;
  300. }
  301. //return word to be written in register
  302. led_control_word[0] = control_reg_addr;
  303. led_control_word[1] = column_byte;
  304. is31_write_data (page, led_control_word, 0x02);
  305. }
  306. void write_led_byte (uint8_t page, uint8_t row, uint8_t led_byte) {
  307. uint8_t led_control_word[2] = {0};//register address and on/off byte
  308. led_control_word[0] = (row - 1 ) * 0x02;// A-matrix is every other byte
  309. led_control_word[1] = led_byte;
  310. is31_write_data(page, led_control_word, 0x02);
  311. }
  312. void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
  313. uint8_t i;
  314. uint8_t pin, col;
  315. uint8_t led_control_register[0x13] = {0};
  316. __builtin_memset(led_control_register,0,13);
  317. for(i=0;i<led_count;i++){
  318. //shift pin by 1 for led register 0x00 address
  319. pin = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;
  320. col = user_led_array[i] % 10 - 1;
  321. led_control_register[pin] |= 1<<(col);
  322. }
  323. is31_write_data(page, led_control_register, 0x13);
  324. }
  325. void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) {
  326. uint8_t temp;
  327. uint8_t led_control_word[2] = {0};
  328. //blink if all leds are on
  329. if (page == 0) {
  330. is31_read_register(0, 0x00, &temp);
  331. chThdSleepMilliseconds(10);
  332. if (temp == 0xFF) {
  333. led_action |= (1<<2); //set blink bit
  334. }
  335. }
  336. set_led_bit(page,led_control_word,led_addr,led_action);
  337. }
  338. /* =====================
  339. * hook into user keymap
  340. * ===================== */
  341. void led_controller_init(void) {
  342. uint8_t i;
  343. /* initialise I2C */
  344. /* I2C pins */
  345. palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
  346. palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
  347. /* start I2C */
  348. i2cStart(&I2CD1, &i2ccfg);
  349. // try high drive (from kiibohd)
  350. I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
  351. // try glitch fixing (from kiibohd)
  352. I2CD1.i2c->FLT = 4;
  353. chThdSleepMilliseconds(10);
  354. /* initialise IS31 chip */
  355. is31_init();
  356. //set Display Option Register so all pwm intensity is controlled from page 0
  357. //enable blink and set blink period to 0.27s x rate
  358. is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME + IS31_REG_DISPLAYOPT_BLINK_ENABLE + 4);
  359. /* set full pwm on page 1 */
  360. pwm_register_array[0] = 0;
  361. __builtin_memset(pwm_register_array+1, 0xFF, 8);
  362. for(i=0; i<8; i++) {
  363. pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
  364. is31_write_data(0, pwm_register_array, 9);
  365. chThdSleepMilliseconds(5);
  366. }
  367. /* enable breathing when the displayed page changes */
  368. // Fade-in Fade-out, time = 26ms * 2^N, N=3
  369. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
  370. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  371. /* more time consuming LED processing should be offloaded into
  372. * a thread, with asynchronous messaging. */
  373. chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
  374. chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
  375. }