ergodox_infinity.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include QMK_KEYBOARD_H
  2. #include <ch.h>
  3. #include <hal.h>
  4. #include <string.h>
  5. #include "eeconfig.h"
  6. #include "serial_link/system/serial_link.h"
  7. #ifdef VISUALIZER_ENABLE
  8. # include "lcd_backlight.h"
  9. #endif
  10. #if (defined(LED_MATRIX_ENABLE) || defined(WPM_ENABLE))
  11. # include "serial_link/protocol/transport.h"
  12. # ifdef LED_MATRIX_ENABLE
  13. MASTER_TO_ALL_SLAVES_OBJECT(led_matrix, led_eeconfig_t);
  14. MASTER_TO_ALL_SLAVES_OBJECT(led_suspend_state, bool);
  15. static led_eeconfig_t last_sent_led_matrix;
  16. static uint16_t led_matrix_sent_timer = 0;
  17. void send_led_suspend_state(void) {
  18. if (is_serial_link_master()) {
  19. *begin_write_led_suspend_state() = led_matrix_get_suspend_state();
  20. end_write_led_suspend_state();
  21. }
  22. }
  23. # endif
  24. # ifdef WPM_ENABLE
  25. # include "wpm.h"
  26. MASTER_TO_ALL_SLAVES_OBJECT(current_wpm, uint8_t);
  27. static uint8_t last_sent_wpm = 0;
  28. # endif
  29. static remote_object_t *remote_objects[] = {
  30. # ifdef LED_MATRIX_ENABLE
  31. REMOTE_OBJECT(led_matrix),
  32. REMOTE_OBJECT(led_suspend_state),
  33. # endif
  34. # ifdef WPM_ENABLE
  35. REMOTE_OBJECT(current_wpm),
  36. # endif
  37. };
  38. #endif
  39. void init_serial_link_hal(void) {
  40. PORTA->PCR[1] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(2);
  41. PORTA->PCR[2] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(2);
  42. PORTE->PCR[0] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(3);
  43. PORTE->PCR[1] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(3);
  44. }
  45. #define RED_PIN 1
  46. #define GREEN_PIN 2
  47. #define BLUE_PIN 3
  48. #define CHANNEL_RED FTM0->CHANNEL[0]
  49. #define CHANNEL_GREEN FTM0->CHANNEL[1]
  50. #define CHANNEL_BLUE FTM0->CHANNEL[2]
  51. #define RGB_PORT PORTC
  52. #define RGB_PORT_GPIO GPIOC
  53. // Base FTM clock selection (72 MHz system clock)
  54. // @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
  55. // Higher pre-scalar will use the most power (also look the best)
  56. // Pre-scalar calculations
  57. // 0 - 72 MHz -> 549 Hz
  58. // 1 - 36 MHz -> 275 Hz
  59. // 2 - 18 MHz -> 137 Hz
  60. // 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
  61. // 4 - 4 500 kHz -> 34 Hz (Visible flickering)
  62. // 5 - 2 250 kHz -> 17 Hz
  63. // 6 - 1 125 kHz -> 9 Hz
  64. // 7 - 562 500 Hz -> 4 Hz
  65. // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
  66. // Which will reduce the brightness range
  67. #define PRESCALAR_DEFINE 0
  68. void lcd_backlight_hal_init(void) {
  69. // Setup Backlight
  70. SIM->SCGC6 |= SIM_SCGC6_FTM0;
  71. FTM0->CNT = 0; // Reset counter
  72. // PWM Period
  73. // 16-bit maximum
  74. FTM0->MOD = 0xFFFF;
  75. // Set FTM to PWM output - Edge Aligned, Low-true pulses
  76. #define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0)
  77. CHANNEL_RED.CnSC = CNSC_MODE;
  78. CHANNEL_GREEN.CnSC = CNSC_MODE;
  79. CHANNEL_BLUE.CnSC = CNSC_MODE;
  80. // System clock, /w prescalar setting
  81. FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);
  82. CHANNEL_RED.CnV = 0;
  83. CHANNEL_GREEN.CnV = 0;
  84. CHANNEL_BLUE.CnV = 0;
  85. RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
  86. RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
  87. RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);
  88. #define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4)
  89. RGB_PORT->PCR[RED_PIN] = RGB_MODE;
  90. RGB_PORT->PCR[GREEN_PIN] = RGB_MODE;
  91. RGB_PORT->PCR[BLUE_PIN] = RGB_MODE;
  92. }
  93. static uint16_t cie_lightness(uint16_t v) {
  94. // The CIE 1931 formula for lightness
  95. // Y = luminance (output) 0-1
  96. // L = lightness input 0 - 100
  97. // Y = (L* / 902.3) if L* <= 8
  98. // Y = ((L* + 16) / 116)^3 if L* > 8
  99. float l = 100.0f * (v / 65535.0f);
  100. float y = 0.0f;
  101. if (l <= 8.0f) {
  102. y = l / 902.3;
  103. } else {
  104. y = ((l + 16.0f) / 116.0f);
  105. y = y * y * y;
  106. if (y > 1.0f) {
  107. y = 1.0f;
  108. }
  109. }
  110. return y * 65535.0f;
  111. }
  112. void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) {
  113. CHANNEL_RED.CnV = cie_lightness(r);
  114. CHANNEL_GREEN.CnV = cie_lightness(g);
  115. CHANNEL_BLUE.CnV = cie_lightness(b);
  116. }
  117. __attribute__ ((weak)) void matrix_init_user(void) {}
  118. __attribute__ ((weak)) void matrix_scan_user(void) {}
  119. void keyboard_pre_init_kb() {
  120. #ifdef LED_MATRIX_ENABLE
  121. // Turn on LED controller
  122. setPinOutput(B16);
  123. writePinHigh(B16);
  124. #endif
  125. keyboard_pre_init_user();
  126. }
  127. void matrix_init_kb(void) {
  128. // put your keyboard start-up code here
  129. // runs once when the firmware starts up
  130. #ifdef LED_MATRIX_ENABLE
  131. /*
  132. * Since K20x is stuck with a 32 byte EEPROM (see tmk_core/common/chibios/eeprom_teensy.c),
  133. * and neither led_matrix_eeconfig.speed or .flags fit in this boundary, just force their values to default on boot.
  134. */
  135. # if !defined(LED_MATRIX_STARTUP_SPD)
  136. # define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2
  137. # endif
  138. led_matrix_set_speed(LED_MATRIX_STARTUP_SPD);
  139. led_matrix_set_flags(LED_FLAG_ALL);
  140. #endif
  141. matrix_init_user();
  142. // The backlight always has to be initialized, otherwise it will stay lit
  143. #ifndef VISUALIZER_ENABLE
  144. lcd_backlight_hal_init();
  145. #endif
  146. #if (defined(LED_MATRIX_ENABLE) || defined(WPM_ENABLE))
  147. add_remote_objects(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t *));
  148. #endif
  149. }
  150. void matrix_scan_kb(void) {
  151. // put your looping keyboard code here
  152. // runs every cycle (a lot)
  153. #ifdef LED_MATRIX_ENABLE
  154. if (is_serial_link_master()) {
  155. if (!led_matrix_get_suspend_state()) {
  156. if (timer_elapsed(led_matrix_sent_timer) >= 5000 || memcmp((void *)&last_sent_led_matrix, (void *)&led_matrix_eeconfig, sizeof(last_sent_led_matrix))) {
  157. led_matrix_sent_timer = timer_read();
  158. memcpy((void *)&last_sent_led_matrix, (void *)&led_matrix_eeconfig, sizeof(last_sent_led_matrix));
  159. *begin_write_led_matrix() = last_sent_led_matrix;
  160. end_write_led_matrix();
  161. }
  162. }
  163. } else if (is_serial_link_connected()) {
  164. bool *new_led_suspend_state = read_led_suspend_state();
  165. if (new_led_suspend_state) {
  166. led_matrix_set_suspend_state(*new_led_suspend_state);
  167. }
  168. if (!led_matrix_get_suspend_state()) {
  169. led_eeconfig_t *new_led_matrix = read_led_matrix();
  170. if (new_led_matrix) {
  171. memcpy((void *)&led_matrix_eeconfig, (void *)new_led_matrix, sizeof(last_sent_led_matrix));
  172. }
  173. }
  174. }
  175. #endif
  176. #ifdef WPM_ENABLE
  177. if (is_serial_link_master()) {
  178. uint8_t current_wpm = get_current_wpm();
  179. if (current_wpm != last_sent_wpm) {
  180. *begin_write_current_wpm() = current_wpm;
  181. end_write_current_wpm();
  182. last_sent_wpm = current_wpm;
  183. }
  184. } else if (is_serial_link_connected()) {
  185. uint8_t *new_wpm = read_current_wpm();
  186. if (new_wpm) {
  187. set_current_wpm(*new_wpm);
  188. }
  189. }
  190. #endif
  191. matrix_scan_user();
  192. }
  193. bool is_keyboard_master(void) { return is_serial_link_master(); }
  194. bool is_keyboard_left(void) {
  195. #if defined(EE_HANDS)
  196. return eeconfig_read_handedness();
  197. #elif defined(MASTER_IS_ON_RIGHT)
  198. return !is_keyboard_master();
  199. #else
  200. return is_keyboard_master();
  201. #endif
  202. }
  203. __attribute__ ((weak)) void ergodox_board_led_on(void) {}
  204. __attribute__ ((weak)) void ergodox_right_led_1_on(void) {}
  205. __attribute__ ((weak)) void ergodox_right_led_2_on(void) {}
  206. __attribute__ ((weak)) void ergodox_right_led_3_on(void) {}
  207. __attribute__ ((weak)) void ergodox_board_led_off(void) {}
  208. __attribute__ ((weak)) void ergodox_right_led_1_off(void) {}
  209. __attribute__ ((weak)) void ergodox_right_led_2_off(void) {}
  210. __attribute__ ((weak)) void ergodox_right_led_3_off(void) {}
  211. __attribute__ ((weak)) void ergodox_right_led_1_set(uint8_t n) {}
  212. __attribute__ ((weak)) void ergodox_right_led_2_set(uint8_t n) {}
  213. __attribute__ ((weak)) void ergodox_right_led_3_set(uint8_t n) {}
  214. void suspend_power_down_kb(void) {
  215. #ifdef LED_MATRIX_ENABLE
  216. send_led_suspend_state();
  217. #endif
  218. suspend_power_down_user();
  219. }
  220. void suspend_wakeup_init_kb(void) {
  221. #ifdef LED_MATRIX_ENABLE
  222. send_led_suspend_state();
  223. #endif
  224. suspend_wakeup_init_user();
  225. }
  226. #ifdef SWAP_HANDS_ENABLE
  227. __attribute__ ((weak))
  228. const keypos_t PROGMEM hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
  229. {{0, 9}, {1, 9}, {2, 9}, {3, 9}, {4, 9}},
  230. {{0, 10}, {1, 10}, {2, 10}, {3, 10}, {4, 10}},
  231. {{0, 11}, {1, 11}, {2, 11}, {3, 11}, {4, 11}},
  232. {{0, 12}, {1, 12}, {2, 12}, {3, 12}, {4, 12}},
  233. {{0, 13}, {1, 13}, {2, 13}, {3, 13}, {4, 13}},
  234. {{0, 14}, {1, 14}, {2, 14}, {3, 14}, {4, 14}},
  235. {{0, 15}, {1, 15}, {2, 15}, {3, 15}, {4, 15}},
  236. {{0, 16}, {1, 16}, {2, 16}, {3, 16}, {4, 16}},
  237. {{0, 17}, {1, 17}, {2, 17}, {3, 17}, {4, 17}},
  238. {{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}},
  239. {{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}},
  240. {{0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2}},
  241. {{0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 3}},
  242. {{0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}},
  243. {{0, 5}, {1, 5}, {2, 5}, {3, 5}, {4, 5}},
  244. {{0, 6}, {1, 6}, {2, 6}, {3, 6}, {4, 6}},
  245. {{0, 7}, {1, 7}, {2, 7}, {3, 7}, {4, 7}},
  246. {{0, 8}, {1, 8}, {2, 8}, {3, 8}, {4, 8}},
  247. };
  248. #endif
  249. #ifdef LED_MATRIX_ENABLE
  250. const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
  251. // The numbers in the comments are the led numbers DXX on the PCB
  252. /* Refer to IS31 manual for these locations
  253. * driver
  254. * | LED address
  255. * | | */
  256. // Left half
  257. // 45 44 43 42 41 40 39
  258. { 0, C2_2 }, { 0, C1_2 }, { 0, C5_1 }, { 0, C4_1 }, { 0, C3_1 }, { 0, C2_1 }, { 0, C1_1 },
  259. // 52 51 50 49 48 47 46
  260. { 0, C4_3 }, { 0, C3_3 }, { 0, C2_3 }, { 0, C1_3 }, { 0, C5_2 }, { 0, C4_2 }, { 0, C3_2 },
  261. // 58 57 56 55 54 53
  262. { 0, C5_4 }, { 0, C4_4 }, { 0, C3_4 }, { 0, C2_4 }, { 0, C1_4 }, { 0, C5_3 },
  263. // 67 66 65 64 63 62 61
  264. { 0, C4_6 }, { 0, C3_6 }, { 0, C2_6 }, { 0, C1_6 }, { 0, C5_5 }, { 0, C4_5 }, { 0, C3_5 },
  265. // 76 75 74 73 72
  266. { 0, C4_8 }, { 0, C3_8 }, { 0, C2_8 }, { 0, C1_8 }, { 0, C4_7 },
  267. // 60 59
  268. { 0, C2_5 }, { 0, C1_5 },
  269. // 68
  270. { 0, C5_6 },
  271. // 71 70 69
  272. { 0, C3_7 }, { 0, C2_7 }, { 0, C1_7 },
  273. // Right half (mirrored)
  274. // Due to how LED_MATRIX_SPLIT is implemented, only the first half of g_is31_leds is actually used.
  275. // Luckily, the right half has the same LED pinouts, just mirrored.
  276. // 45 44 43 42 41 40 39
  277. { 0, C2_2 }, { 0, C1_2 }, { 0, C5_1 }, { 0, C4_1 }, { 0, C3_1 }, { 0, C2_1 }, { 0, C1_1 },
  278. // 52 51 50 49 48 47 46
  279. { 0, C4_3 }, { 0, C3_3 }, { 0, C2_3 }, { 0, C1_3 }, { 0, C5_2 }, { 0, C4_2 }, { 0, C3_2 },
  280. // 58 57 56 55 54 53
  281. { 0, C5_4 }, { 0, C4_4 }, { 0, C3_4 }, { 0, C2_4 }, { 0, C1_4 }, { 0, C5_3 },
  282. // 67 66 65 64 63 62 61
  283. { 0, C4_6 }, { 0, C3_6 }, { 0, C2_6 }, { 0, C1_6 }, { 0, C5_5 }, { 0, C4_5 }, { 0, C3_5 },
  284. // 76 75 74 73 72
  285. { 0, C4_8 }, { 0, C3_8 }, { 0, C2_8 }, { 0, C1_8 }, { 0, C4_7 },
  286. // 60 59
  287. { 0, C2_5 }, { 0, C1_5 },
  288. // 68
  289. { 0, C5_6 },
  290. // 71 70 69
  291. { 0, C3_7 }, { 0, C2_7 }, { 0, C1_7 },
  292. };
  293. led_config_t g_led_config = {
  294. {
  295. // Key Matrix to LED Index
  296. // Left half
  297. { NO_LED, NO_LED, NO_LED, 33, 34 },
  298. { NO_LED, NO_LED, NO_LED, 32, 37 },
  299. { 6, 13, NO_LED, 26, 36 },
  300. { 5, 12, 19, 25, 35 },
  301. { 4, 11, 18, 24, 31 },
  302. { 3, 10, 17, 23, 30 },
  303. { 2, 9, 16, 22, 29 },
  304. { 1, 8, 15, 21, 28 },
  305. { 0, 7, 14, 20, 27 },
  306. // Right half
  307. { NO_LED, NO_LED, NO_LED, 71, 72 },
  308. { NO_LED, NO_LED, NO_LED, 70, 75 },
  309. { 44, 51, NO_LED, 64, 74 },
  310. { 43, 50, 57, 63, 73 },
  311. { 42, 49, 56, 62, 69 },
  312. { 41, 48, 55, 61, 68 },
  313. { 40, 47, 54, 60, 67 },
  314. { 39, 46, 53, 59, 66 },
  315. { 38, 45, 52, 58, 65 },
  316. }, {
  317. // LED Index to Physical Position (assumes a reasonable gap between halves)
  318. // Left half
  319. { 0, 3 }, { 15, 3 }, { 27, 1 }, { 39, 0 }, { 51, 1 }, { 63, 2 }, { 75, 2 },
  320. { 0, 13 }, { 15, 13 }, { 27, 11 }, { 39, 10 }, { 51, 11 }, { 63, 12 }, { 78, 17 },
  321. { 0, 23 }, { 15, 23 }, { 27, 21 }, { 39, 20 }, { 51, 21 }, { 63, 22 },
  322. { 0, 33 }, { 15, 33 }, { 27, 31 }, { 39, 30 }, { 51, 31 }, { 63, 32 }, { 78, 32 },
  323. { 4, 43 }, { 15, 43 }, { 27, 41 }, { 39, 40 }, { 51, 41 },
  324. { 89, 41 }, { 100, 46 },
  325. { 95, 55 },
  326. { 72, 54 }, { 83, 59 }, { 90, 64 },
  327. // Right half (mirrored)
  328. { 224, 3 }, { 209, 3 }, { 197, 1 }, { 185, 0 }, { 173, 1 }, { 161, 2 }, { 149, 2 },
  329. { 224, 13 }, { 209, 13 }, { 197, 11 }, { 185, 10 }, { 173, 11 }, { 161, 12 }, { 146, 17 },
  330. { 224, 23 }, { 209, 23 }, { 197, 21 }, { 185, 20 }, { 173, 21 }, { 161, 22 },
  331. { 224, 33 }, { 209, 33 }, { 197, 31 }, { 185, 30 }, { 173, 31 }, { 161, 32 }, { 146, 32 },
  332. { 220, 43 }, { 209, 43 }, { 197, 41 }, { 185, 40 }, { 173, 41 },
  333. { 135, 41 }, { 124, 46 },
  334. { 129, 55 },
  335. { 152, 54 }, { 141, 59 }, { 134, 64 },
  336. }, {
  337. // LED Index to Flag
  338. // Left half
  339. 1, 4, 4, 4, 4, 4, 1,
  340. 1, 4, 4, 4, 4, 4, 1,
  341. 1, 4, 4, 4, 4, 4,
  342. 1, 4, 4, 4, 4, 4, 1,
  343. 1, 1, 1, 1, 1,
  344. 1, 1,
  345. 1,
  346. 1, 1, 1,
  347. // Right half (mirrored)
  348. 1, 4, 4, 4, 4, 4, 1,
  349. 1, 4, 4, 4, 4, 4, 1,
  350. 1, 4, 4, 4, 4, 4,
  351. 1, 4, 4, 4, 4, 4, 1,
  352. 1, 1, 1, 1, 1,
  353. 1, 1,
  354. 1,
  355. 1, 1, 1,
  356. }
  357. };
  358. #endif