You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

487 lines
15 KiB

  1. /*
  2. Copyright 2016 flabbergast <s3+flabbergast@sdfeu.org>
  3. Copyright 2017 jpetermans <tibcmhhm@gmail.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. * LED controller code
  17. * IS31FL3731C matrix LED driver from ISSI
  18. * datasheet: http://www.issi.com/WW/pdf/31FL3731C.pdf
  19. */
  20. #include <ch.h>
  21. #include <hal.h>
  22. #include "print.h"
  23. #include "led.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, TIME_US2I(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, TIME_US2I(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, TIME_US2I(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, TIME_US2I(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. // disable hardware shutdown
  116. palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
  117. palSetPad(GPIOB, 16);
  118. chThdSleepMilliseconds(10);
  119. // software shutdown
  120. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  121. chThdSleepMilliseconds(10);
  122. // zero function page, all registers
  123. is31_write_data(IS31_FUNCTIONREG, full_page, 0xD + 1);
  124. chThdSleepMilliseconds(10);
  125. // software shutdown disable (i.e. turn stuff on)
  126. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
  127. chThdSleepMilliseconds(10);
  128. // zero all LED registers on all 8 pages
  129. uint8_t i;
  130. for(i=0; i<8; i++) {
  131. is31_write_data(i, full_page, 0xB4 + 1);
  132. chThdSleepMilliseconds(5);
  133. }
  134. }
  135. /* ==================
  136. * LED control thread
  137. * ================== */
  138. #define LED_MAILBOX_NUM_MSGS 5
  139. static msg_t led_mailbox_queue[LED_MAILBOX_NUM_MSGS];
  140. mailbox_t led_mailbox;
  141. static THD_WORKING_AREA(waLEDthread, 256);
  142. static THD_FUNCTION(LEDthread, arg) {
  143. (void)arg;
  144. chRegSetThreadName("LEDthread");
  145. uint8_t i;
  146. uint8_t control_register_word[2] = {0};//2 bytes: register address, byte to write
  147. uint8_t led_control_reg[0x13] = {0};//led control register start address + 0x12 bytes
  148. //persistent status variables
  149. uint8_t pwm_step_status, page_status, capslock_status, numlock_status;
  150. //mailbox variables
  151. uint8_t temp, msg_type;
  152. uint8_t msg_args[3];
  153. msg_t msg;
  154. // initialize persistent variables
  155. pwm_step_status = 4; //full brightness
  156. page_status = 0; //start frame 0 (all off/on)
  157. numlock_status = (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? 1 : 0;
  158. capslock_status = (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) ? 1 : 0;
  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. chMBFetchTimeout(&led_mailbox, &msg, TIME_INFINITE);
  164. msg_type = msg & 0xFF; //first byte is action information
  165. msg_args[0] = (msg >> 8) & 0xFF;
  166. msg_args[1] = (msg >> 16) & 0XFF;
  167. msg_args[2] = (msg >> 24) & 0xFF;
  168. switch (msg_type){
  169. case SET_FULL_ROW:
  170. //write full byte to pin address, msg_args[1] = pin #, msg_args[0] = 8 bits to write
  171. //writes only to currently displayed page
  172. write_led_byte(page_status, msg_args[1], msg_args[0]);
  173. break;
  174. case OFF_LED:
  175. //on/off/toggle single led, msg_args[0] = row/col of led, msg_args[1] = page
  176. set_led_bit(msg_args[1], control_register_word, msg_args[0], 0);
  177. break;
  178. case ON_LED:
  179. set_led_bit(msg_args[1], control_register_word, msg_args[0], 1);
  180. break;
  181. case TOGGLE_LED:
  182. set_led_bit(msg_args[1], control_register_word, msg_args[0], 2);
  183. break;
  184. case BLINK_OFF_LED:
  185. //on/off/toggle single led, msg_args[0] = row/col of led
  186. set_led_bit(msg_args[1], control_register_word, msg_args[0], 4);
  187. break;
  188. case BLINK_ON_LED:
  189. set_led_bit(msg_args[1], control_register_word, msg_args[0], 5);
  190. break;
  191. case BLINK_TOGGLE_LED:
  192. set_led_bit(msg_args[1], control_register_word, msg_args[0], 6);
  193. break;
  194. case TOGGLE_ALL:
  195. //turn on/off all leds, msg_args = unused
  196. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  197. chThdSleepMilliseconds(5);
  198. is31_read_register(0, 0x00, &temp);
  199. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
  200. led_control_reg[0] = 0;
  201. //toggle led mask based on current state (temp)
  202. if (temp==0 || page_status > 0) {
  203. __builtin_memcpy(led_control_reg+1, all_on_leds_mask, 0x12);
  204. } else {
  205. __builtin_memset(led_control_reg+1, 0, 0x12);
  206. }
  207. is31_write_data(0, led_control_reg, 0x13);
  208. if (page_status > 0) {
  209. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, 0);
  210. page_status=0;
  211. //maintain lock leds, reset to off and force recheck to blink of all leds toggled on
  212. numlock_status = 0;
  213. capslock_status = 0;
  214. led_set(host_keyboard_leds());
  215. }
  216. break;
  217. case TOGGLE_BACKLIGHT:
  218. //msg_args[0] = on/off
  219. //populate 9 byte rows to be written to each pin, first byte is register (pin) address
  220. if (msg_args[0] == 1) {
  221. __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
  222. } else {
  223. __builtin_memset(pwm_register_array+1, 0, 8);
  224. }
  225. for(i=0; i<8; i++) {
  226. //first byte is register address, every 0x10 9 bytes is A-matrix pwm pins
  227. pwm_register_array[0] = 0x24 + (i * 0x10);
  228. is31_write_data(0,pwm_register_array,9);
  229. }
  230. break;
  231. case DISPLAY_PAGE:
  232. //msg_args[0] = page to toggle on
  233. if (page_status != msg_args[0]) {
  234. is31_write_register(IS31_FUNCTIONREG, IS31_REG_PICTDISP, msg_args[0]);
  235. page_status = msg_args[0];
  236. //maintain lock leds, reset to off and force recheck for new page
  237. numlock_status = 0;
  238. capslock_status = 0;
  239. led_set(host_keyboard_leds());
  240. }
  241. break;
  242. case RESET_PAGE:
  243. //led_args[0] = page to reset
  244. led_control_reg[0] = 0;
  245. __builtin_memset(led_control_reg+1, 0, 0x12);
  246. is31_write_data(msg_args[0], led_control_reg, 0x13);
  247. //repeat for blink register
  248. led_control_reg[0] = 0x12;
  249. is31_write_data(msg_args[0], led_control_reg, 0x13);
  250. break;
  251. case TOGGLE_NUM_LOCK:
  252. //msg_args[0] = 0 or 1, off/on
  253. if (numlock_status != msg_args[0]) {
  254. set_lock_leds(NUM_LOCK_LED_ADDRESS, msg_args[0], page_status);
  255. numlock_status = msg_args[0];
  256. }
  257. break;
  258. case TOGGLE_CAPS_LOCK:
  259. //msg_args[0] = 0 or 1, off/on
  260. if (capslock_status != msg_args[0]) {
  261. set_lock_leds(CAPS_LOCK_LED_ADDRESS, msg_args[0], page_status);
  262. capslock_status = msg_args[0];
  263. }
  264. break;
  265. case STEP_BRIGHTNESS:
  266. //led_args[0] = step up (1) or down (0)
  267. switch (msg_args[0]) {
  268. case 0:
  269. if (pwm_step_status == 0) {
  270. pwm_step_status = 4;
  271. } else {
  272. pwm_step_status--;
  273. }
  274. break;
  275. case 1:
  276. if (pwm_step_status == 4) {
  277. pwm_step_status = 0;
  278. } else {
  279. pwm_step_status++;
  280. }
  281. break;
  282. }
  283. //populate 8 byte arrays to write on each pin
  284. //first byte is register address, every 0x10 9 bytes are A-matrix pwm pins
  285. __builtin_memset(pwm_register_array+1, pwm_levels[pwm_step_status], 8);
  286. for(i=0; i<8; i++) {
  287. pwm_register_array[0] = 0x24 + (i * 0x10);
  288. is31_write_data(0,pwm_register_array,9);
  289. }
  290. break;
  291. }
  292. }
  293. }
  294. /* ==============================
  295. * led processing functions
  296. * ============================== */
  297. void set_led_bit (uint8_t page, uint8_t *led_control_word, uint8_t led_addr, uint8_t action) {
  298. //returns 2 bytes: led control register address and byte to write
  299. //action: 0 - off, 1 - on, 2 - toggle, 4 - blink on, 5 - blink off, 6 - toggle blink
  300. uint8_t control_reg_addr, column_bit, column_byte, temp, blink_bit;
  301. //check for valid led address
  302. if (led_addr < 0 || led_addr > 87 || led_addr % 10 > 8) {
  303. return;
  304. }
  305. blink_bit = action>>2;//check for blink bit
  306. action &= ~(1<<2); //strip blink bit
  307. //led_addr tens column is pin#, ones column is bit position in 8-bit mask
  308. control_reg_addr = ((led_addr / 10) % 10 - 1 ) * 0x02;// A-matrix is every other byte
  309. control_reg_addr += blink_bit == 1 ? 0x12 : 0x00;//if blink_bit, shift 12 bytes to blink register
  310. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  311. chThdSleepMilliseconds(5);
  312. is31_read_register(page, control_reg_addr, &temp);//maintain status of leds on this byte
  313. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
  314. column_bit = 1<<(led_addr % 10 - 1);
  315. column_byte = temp;
  316. switch(action) {
  317. case 0:
  318. column_byte &= ~column_bit;
  319. break;
  320. case 1:
  321. column_byte |= column_bit;
  322. break;
  323. case 2:
  324. column_byte ^= column_bit;
  325. break;
  326. }
  327. //return word to be written in register
  328. led_control_word[0] = control_reg_addr;
  329. led_control_word[1] = column_byte;
  330. is31_write_data (page, led_control_word, 0x02);
  331. }
  332. void write_led_byte (uint8_t page, uint8_t row, uint8_t led_byte) {
  333. uint8_t led_control_word[2] = {0};//register address and on/off byte
  334. led_control_word[0] = (row - 1 ) * 0x02;// A-matrix is every other byte
  335. led_control_word[1] = led_byte;
  336. is31_write_data(page, led_control_word, 0x02);
  337. }
  338. void write_led_page (uint8_t page, uint8_t *user_led_array, uint8_t led_count) {
  339. uint8_t i;
  340. uint8_t pin, col;
  341. uint8_t led_control_register[0x13] = {0};
  342. __builtin_memset(led_control_register,0,13);
  343. for(i=0;i<led_count;i++){
  344. //shift pin by 1 for led register 0x00 address
  345. pin = ((user_led_array[i] / 10) % 10 - 1 ) * 2 + 1;
  346. col = user_led_array[i] % 10 - 1;
  347. led_control_register[pin] |= 1<<(col);
  348. }
  349. is31_write_data(page, led_control_register, 0x13);
  350. }
  351. void set_lock_leds(uint8_t led_addr, uint8_t led_action, uint8_t page) {
  352. uint8_t temp;
  353. uint8_t led_control_word[2] = {0};
  354. //blink if all leds are on
  355. if (page == 0) {
  356. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  357. chThdSleepMilliseconds(5);
  358. is31_read_register(0, 0x00, &temp);
  359. is31_write_register(IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
  360. if (temp == 0xFF) {
  361. led_action |= (1<<2); //set blink bit
  362. }
  363. }
  364. set_led_bit(page,led_control_word,led_addr,led_action);
  365. }
  366. /* =====================
  367. * hook into user keymap
  368. * ===================== */
  369. void led_controller_init(void) {
  370. uint8_t i;
  371. /* initialise I2C */
  372. /* I2C pins */
  373. palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
  374. palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
  375. /* start I2C */
  376. i2cStart(&I2CD1, &i2ccfg);
  377. // try high drive (from kiibohd)
  378. I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
  379. // try glitch fixing (from kiibohd)
  380. I2CD1.i2c->FLT = 4;
  381. chThdSleepMilliseconds(10);
  382. /* initialise IS31 chip */
  383. is31_init();
  384. //set Display Option Register so all pwm intensity is controlled from page 0
  385. //enable blink and set blink period to 0.27s x rate
  386. is31_write_register(IS31_FUNCTIONREG, IS31_REG_DISPLAYOPT, IS31_REG_DISPLAYOPT_INTENSITY_SAME + IS31_REG_DISPLAYOPT_BLINK_ENABLE + 4);
  387. /* set full pwm on page 1 */
  388. pwm_register_array[0] = 0;
  389. __builtin_memset(pwm_register_array+1, 0xFF, 8);
  390. for(i=0; i<8; i++) {
  391. pwm_register_array[0] = 0x24 + (i * 0x10);//first byte of 9 bytes must be register address
  392. is31_write_data(0, pwm_register_array, 9);
  393. chThdSleepMilliseconds(5);
  394. }
  395. /* enable breathing when the displayed page changes */
  396. // Fade-in Fade-out, time = 26ms * 2^N, N=3
  397. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL1, (3<<4)|3);
  398. is31_write_register(IS31_FUNCTIONREG, IS31_REG_BREATHCTRL2, IS31_REG_BREATHCTRL2_ENABLE|3);
  399. /* more time consuming LED processing should be offloaded into
  400. * a thread, with asynchronous messaging. */
  401. chMBObjectInit(&led_mailbox, led_mailbox_queue, LED_MAILBOX_NUM_MSGS);
  402. chThdCreateStatic(waLEDthread, sizeof(waLEDthread), LOWPRIO, LEDthread, NULL);
  403. }