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.

380 lines
15 KiB

  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. #define RED_PIN 1
  11. #define GREEN_PIN 2
  12. #define BLUE_PIN 3
  13. #define CHANNEL_RED FTM0->CHANNEL[0]
  14. #define CHANNEL_GREEN FTM0->CHANNEL[1]
  15. #define CHANNEL_BLUE FTM0->CHANNEL[2]
  16. #define RGB_PORT PORTC
  17. #define RGB_PORT_GPIO GPIOC
  18. // Base FTM clock selection (72 MHz system clock)
  19. // @ 0xFFFF period, 72 MHz / (0xFFFF * 2) = Actual period
  20. // Higher pre-scalar will use the most power (also look the best)
  21. // Pre-scalar calculations
  22. // 0 - 72 MHz -> 549 Hz
  23. // 1 - 36 MHz -> 275 Hz
  24. // 2 - 18 MHz -> 137 Hz
  25. // 3 - 9 MHz -> 69 Hz (Slightly visible flicker)
  26. // 4 - 4 500 kHz -> 34 Hz (Visible flickering)
  27. // 5 - 2 250 kHz -> 17 Hz
  28. // 6 - 1 125 kHz -> 9 Hz
  29. // 7 - 562 500 Hz -> 4 Hz
  30. // Using a higher pre-scalar without flicker is possible but FTM0_MOD will need to be reduced
  31. // Which will reduce the brightness range
  32. #define PRESCALAR_DEFINE 0
  33. void lcd_backlight_hal_init(void) {
  34. // Setup Backlight
  35. SIM->SCGC6 |= SIM_SCGC6_FTM0;
  36. FTM0->CNT = 0; // Reset counter
  37. // PWM Period
  38. // 16-bit maximum
  39. FTM0->MOD = 0xFFFF;
  40. // Set FTM to PWM output - Edge Aligned, Low-true pulses
  41. #define CNSC_MODE FTM_SC_CPWMS | FTM_SC_PS(4) | FTM_SC_CLKS(0)
  42. CHANNEL_RED.CnSC = CNSC_MODE;
  43. CHANNEL_GREEN.CnSC = CNSC_MODE;
  44. CHANNEL_BLUE.CnSC = CNSC_MODE;
  45. // System clock, /w prescalar setting
  46. FTM0->SC = FTM_SC_CLKS(1) | FTM_SC_PS(PRESCALAR_DEFINE);
  47. CHANNEL_RED.CnV = 0;
  48. CHANNEL_GREEN.CnV = 0;
  49. CHANNEL_BLUE.CnV = 0;
  50. RGB_PORT_GPIO->PDDR |= (1 << RED_PIN);
  51. RGB_PORT_GPIO->PDDR |= (1 << GREEN_PIN);
  52. RGB_PORT_GPIO->PDDR |= (1 << BLUE_PIN);
  53. #define RGB_MODE PORTx_PCRn_SRE | PORTx_PCRn_DSE | PORTx_PCRn_MUX(4)
  54. RGB_PORT->PCR[RED_PIN] = RGB_MODE;
  55. RGB_PORT->PCR[GREEN_PIN] = RGB_MODE;
  56. RGB_PORT->PCR[BLUE_PIN] = RGB_MODE;
  57. }
  58. static uint16_t cie_lightness(uint16_t v) {
  59. // The CIE 1931 formula for lightness
  60. // Y = luminance (output) 0-1
  61. // L = lightness input 0 - 100
  62. // Y = (L* / 902.3) if L* <= 8
  63. // Y = ((L* + 16) / 116)^3 if L* > 8
  64. float l = 100.0f * (v / 65535.0f);
  65. float y = 0.0f;
  66. if (l <= 8.0f) {
  67. y = l / 902.3;
  68. } else {
  69. y = ((l + 16.0f) / 116.0f);
  70. y = y * y * y;
  71. if (y > 1.0f) {
  72. y = 1.0f;
  73. }
  74. }
  75. return y * 65535.0f;
  76. }
  77. #ifdef VISUALIZER_ENABLE
  78. void lcd_backlight_hal_color(uint16_t r, uint16_t g, uint16_t b) {
  79. #else
  80. void ergodox_infinity_lcd_color(uint16_t r, uint16_t g, uint16_t b) {
  81. #endif
  82. CHANNEL_RED.CnV = cie_lightness(r);
  83. CHANNEL_GREEN.CnV = cie_lightness(g);
  84. CHANNEL_BLUE.CnV = cie_lightness(b);
  85. }
  86. __attribute__ ((weak)) void matrix_init_user(void) {}
  87. __attribute__ ((weak)) void matrix_scan_user(void) {}
  88. void keyboard_pre_init_kb() {
  89. #ifdef LED_MATRIX_ENABLE
  90. // Turn on LED controller
  91. setPinOutput(B16);
  92. writePinHigh(B16);
  93. #endif
  94. #ifndef VISUALIZER_ENABLE
  95. // The backlight always has to be initialized, otherwise it will stay lit
  96. lcd_backlight_hal_init();
  97. # ifdef ST7565_ENABLE
  98. ergodox_infinity_lcd_color(UINT16_MAX / 2, UINT16_MAX / 2, UINT16_MAX / 2);
  99. # endif
  100. #endif
  101. keyboard_pre_init_user();
  102. }
  103. void matrix_init_kb(void) {
  104. // put your keyboard start-up code here
  105. // runs once when the firmware starts up
  106. #ifdef LED_MATRIX_ENABLE
  107. /*
  108. * Since K20x is stuck with a 32 byte EEPROM (see tmk_core/common/chibios/eeprom_teensy.c),
  109. * and neither led_matrix_eeconfig.speed or .flags fit in this boundary, just force their values to default on boot.
  110. */
  111. # if !defined(LED_MATRIX_STARTUP_SPD)
  112. # define LED_MATRIX_STARTUP_SPD UINT8_MAX / 2
  113. # endif
  114. led_matrix_set_speed(LED_MATRIX_STARTUP_SPD);
  115. led_matrix_set_flags(LED_FLAG_ALL);
  116. #endif
  117. matrix_init_user();
  118. }
  119. void matrix_scan_kb(void) {
  120. // put your looping keyboard code here
  121. // runs every cycle (a lot)
  122. matrix_scan_user();
  123. }
  124. __attribute__ ((weak)) void ergodox_board_led_on(void) {}
  125. __attribute__ ((weak)) void ergodox_right_led_1_on(void) {}
  126. __attribute__ ((weak)) void ergodox_right_led_2_on(void) {}
  127. __attribute__ ((weak)) void ergodox_right_led_3_on(void) {}
  128. __attribute__ ((weak)) void ergodox_board_led_off(void) {}
  129. __attribute__ ((weak)) void ergodox_right_led_1_off(void) {}
  130. __attribute__ ((weak)) void ergodox_right_led_2_off(void) {}
  131. __attribute__ ((weak)) void ergodox_right_led_3_off(void) {}
  132. __attribute__ ((weak)) void ergodox_right_led_1_set(uint8_t n) {}
  133. __attribute__ ((weak)) void ergodox_right_led_2_set(uint8_t n) {}
  134. __attribute__ ((weak)) void ergodox_right_led_3_set(uint8_t n) {}
  135. #ifdef SWAP_HANDS_ENABLE
  136. __attribute__ ((weak))
  137. const keypos_t PROGMEM hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
  138. {{0, 9}, {1, 9}, {2, 9}, {3, 9}, {4, 9}},
  139. {{0, 10}, {1, 10}, {2, 10}, {3, 10}, {4, 10}},
  140. {{0, 11}, {1, 11}, {2, 11}, {3, 11}, {4, 11}},
  141. {{0, 12}, {1, 12}, {2, 12}, {3, 12}, {4, 12}},
  142. {{0, 13}, {1, 13}, {2, 13}, {3, 13}, {4, 13}},
  143. {{0, 14}, {1, 14}, {2, 14}, {3, 14}, {4, 14}},
  144. {{0, 15}, {1, 15}, {2, 15}, {3, 15}, {4, 15}},
  145. {{0, 16}, {1, 16}, {2, 16}, {3, 16}, {4, 16}},
  146. {{0, 17}, {1, 17}, {2, 17}, {3, 17}, {4, 17}},
  147. {{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}},
  148. {{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}},
  149. {{0, 2}, {1, 2}, {2, 2}, {3, 2}, {4, 2}},
  150. {{0, 3}, {1, 3}, {2, 3}, {3, 3}, {4, 3}},
  151. {{0, 4}, {1, 4}, {2, 4}, {3, 4}, {4, 4}},
  152. {{0, 5}, {1, 5}, {2, 5}, {3, 5}, {4, 5}},
  153. {{0, 6}, {1, 6}, {2, 6}, {3, 6}, {4, 6}},
  154. {{0, 7}, {1, 7}, {2, 7}, {3, 7}, {4, 7}},
  155. {{0, 8}, {1, 8}, {2, 8}, {3, 8}, {4, 8}},
  156. };
  157. #endif
  158. #ifdef LED_MATRIX_ENABLE
  159. const is31_led __flash g_is31_leds[DRIVER_LED_TOTAL] = {
  160. // The numbers in the comments are the led numbers DXX on the PCB
  161. /* Refer to IS31 manual for these locations
  162. * driver
  163. * | LED address
  164. * | | */
  165. // Left half
  166. // 45 44 43 42 41 40 39
  167. { 0, C2_2 }, { 0, C1_2 }, { 0, C5_1 }, { 0, C4_1 }, { 0, C3_1 }, { 0, C2_1 }, { 0, C1_1 },
  168. // 52 51 50 49 48 47 46
  169. { 0, C4_3 }, { 0, C3_3 }, { 0, C2_3 }, { 0, C1_3 }, { 0, C5_2 }, { 0, C4_2 }, { 0, C3_2 },
  170. // 58 57 56 55 54 53
  171. { 0, C5_4 }, { 0, C4_4 }, { 0, C3_4 }, { 0, C2_4 }, { 0, C1_4 }, { 0, C5_3 },
  172. // 67 66 65 64 63 62 61
  173. { 0, C4_6 }, { 0, C3_6 }, { 0, C2_6 }, { 0, C1_6 }, { 0, C5_5 }, { 0, C4_5 }, { 0, C3_5 },
  174. // 76 75 74 73 72
  175. { 0, C4_8 }, { 0, C3_8 }, { 0, C2_8 }, { 0, C1_8 }, { 0, C4_7 },
  176. // 60 59
  177. { 0, C2_5 }, { 0, C1_5 },
  178. // 68
  179. { 0, C5_6 },
  180. // 71 70 69
  181. { 0, C3_7 }, { 0, C2_7 }, { 0, C1_7 },
  182. // Right half (mirrored)
  183. // Due to how LED_MATRIX_SPLIT is implemented, only the first half of g_is31_leds is actually used.
  184. // Luckily, the right half has the same LED pinouts, just mirrored.
  185. // 45 44 43 42 41 40 39
  186. { 0, C2_2 }, { 0, C1_2 }, { 0, C5_1 }, { 0, C4_1 }, { 0, C3_1 }, { 0, C2_1 }, { 0, C1_1 },
  187. // 52 51 50 49 48 47 46
  188. { 0, C4_3 }, { 0, C3_3 }, { 0, C2_3 }, { 0, C1_3 }, { 0, C5_2 }, { 0, C4_2 }, { 0, C3_2 },
  189. // 58 57 56 55 54 53
  190. { 0, C5_4 }, { 0, C4_4 }, { 0, C3_4 }, { 0, C2_4 }, { 0, C1_4 }, { 0, C5_3 },
  191. // 67 66 65 64 63 62 61
  192. { 0, C4_6 }, { 0, C3_6 }, { 0, C2_6 }, { 0, C1_6 }, { 0, C5_5 }, { 0, C4_5 }, { 0, C3_5 },
  193. // 76 75 74 73 72
  194. { 0, C4_8 }, { 0, C3_8 }, { 0, C2_8 }, { 0, C1_8 }, { 0, C4_7 },
  195. // 60 59
  196. { 0, C2_5 }, { 0, C1_5 },
  197. // 68
  198. { 0, C5_6 },
  199. // 71 70 69
  200. { 0, C3_7 }, { 0, C2_7 }, { 0, C1_7 },
  201. };
  202. led_config_t g_led_config = {
  203. {
  204. // Key Matrix to LED Index
  205. // Left half
  206. { NO_LED, NO_LED, NO_LED, 33, 34 },
  207. { NO_LED, NO_LED, NO_LED, 32, 37 },
  208. { 6, 13, NO_LED, 26, 36 },
  209. { 5, 12, 19, 25, 35 },
  210. { 4, 11, 18, 24, 31 },
  211. { 3, 10, 17, 23, 30 },
  212. { 2, 9, 16, 22, 29 },
  213. { 1, 8, 15, 21, 28 },
  214. { 0, 7, 14, 20, 27 },
  215. // Right half
  216. { NO_LED, NO_LED, NO_LED, 71, 72 },
  217. { NO_LED, NO_LED, NO_LED, 70, 75 },
  218. { 44, 51, NO_LED, 64, 74 },
  219. { 43, 50, 57, 63, 73 },
  220. { 42, 49, 56, 62, 69 },
  221. { 41, 48, 55, 61, 68 },
  222. { 40, 47, 54, 60, 67 },
  223. { 39, 46, 53, 59, 66 },
  224. { 38, 45, 52, 58, 65 },
  225. }, {
  226. // LED Index to Physical Position (assumes a reasonable gap between halves)
  227. // Left half
  228. { 0, 3 }, { 15, 3 }, { 27, 1 }, { 39, 0 }, { 51, 1 }, { 63, 2 }, { 75, 2 },
  229. { 0, 13 }, { 15, 13 }, { 27, 11 }, { 39, 10 }, { 51, 11 }, { 63, 12 }, { 78, 17 },
  230. { 0, 23 }, { 15, 23 }, { 27, 21 }, { 39, 20 }, { 51, 21 }, { 63, 22 },
  231. { 0, 33 }, { 15, 33 }, { 27, 31 }, { 39, 30 }, { 51, 31 }, { 63, 32 }, { 78, 32 },
  232. { 4, 43 }, { 15, 43 }, { 27, 41 }, { 39, 40 }, { 51, 41 },
  233. { 89, 41 }, { 100, 46 },
  234. { 95, 55 },
  235. { 72, 54 }, { 83, 59 }, { 90, 64 },
  236. // Right half (mirrored)
  237. { 224, 3 }, { 209, 3 }, { 197, 1 }, { 185, 0 }, { 173, 1 }, { 161, 2 }, { 149, 2 },
  238. { 224, 13 }, { 209, 13 }, { 197, 11 }, { 185, 10 }, { 173, 11 }, { 161, 12 }, { 146, 17 },
  239. { 224, 23 }, { 209, 23 }, { 197, 21 }, { 185, 20 }, { 173, 21 }, { 161, 22 },
  240. { 224, 33 }, { 209, 33 }, { 197, 31 }, { 185, 30 }, { 173, 31 }, { 161, 32 }, { 146, 32 },
  241. { 220, 43 }, { 209, 43 }, { 197, 41 }, { 185, 40 }, { 173, 41 },
  242. { 135, 41 }, { 124, 46 },
  243. { 129, 55 },
  244. { 152, 54 }, { 141, 59 }, { 134, 64 },
  245. }, {
  246. // LED Index to Flag
  247. // Left half
  248. 1, 4, 4, 4, 4, 4, 1,
  249. 1, 4, 4, 4, 4, 4, 1,
  250. 1, 4, 4, 4, 4, 4,
  251. 1, 4, 4, 4, 4, 4, 1,
  252. 1, 1, 1, 1, 1,
  253. 1, 1,
  254. 1,
  255. 1, 1, 1,
  256. // Right half (mirrored)
  257. 1, 4, 4, 4, 4, 4, 1,
  258. 1, 4, 4, 4, 4, 4, 1,
  259. 1, 4, 4, 4, 4, 4,
  260. 1, 4, 4, 4, 4, 4, 1,
  261. 1, 1, 1, 1, 1,
  262. 1, 1,
  263. 1,
  264. 1, 1, 1,
  265. }
  266. };
  267. #endif
  268. #ifdef ST7565_ENABLE
  269. __attribute__((weak)) void st7565_on_user(void) {
  270. ergodox_infinity_lcd_color(UINT16_MAX / 2, UINT16_MAX / 2, UINT16_MAX / 2);
  271. }
  272. __attribute__((weak)) void st7565_off_user(void) {
  273. ergodox_infinity_lcd_color(0, 0, 0);
  274. }
  275. static void format_layer_bitmap_string(char* buffer, uint8_t offset) {
  276. for (int i = 0; i < 16 && i + offset < MAX_LAYER; i++) {
  277. if (i == 0 || i == 4 || i == 8 || i == 12) {
  278. *buffer = ' ';
  279. ++buffer;
  280. }
  281. uint8_t layer = i + offset;
  282. if (layer_state_cmp(default_layer_state, layer)) {
  283. *buffer = 'D';
  284. } else if (layer_state_is(layer)) {
  285. *buffer = '1';
  286. } else {
  287. *buffer = '_';
  288. }
  289. ++buffer;
  290. }
  291. *buffer = 0;
  292. }
  293. __attribute__((weak)) void st7565_task_user(void) {
  294. if (is_keyboard_master()) {
  295. // Draw led and layer status
  296. led_t leds = host_keyboard_led_state();
  297. if(leds.num_lock) { st7565_write("Num ", false); }
  298. if(leds.caps_lock) { st7565_write("Cap ", false); }
  299. if(leds.scroll_lock) { st7565_write("Scrl ", false); }
  300. if(leds.compose) { st7565_write("Com ", false); }
  301. if(leds.kana) { st7565_write("Kana", false); }
  302. st7565_advance_page(true);
  303. char layer_buffer[16 + 5]; // 3 spaces and one null terminator
  304. st7565_set_cursor(0, 1);
  305. format_layer_bitmap_string(layer_buffer, 0);
  306. st7565_write_ln(layer_buffer, false);
  307. format_layer_bitmap_string(layer_buffer, 16);
  308. st7565_write_ln(layer_buffer, false);
  309. st7565_write_ln(" 1=On D=Default", false);
  310. } else {
  311. // Draw logo
  312. static const char qmk_logo[] = {
  313. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94,
  314. 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4,
  315. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0x00
  316. };
  317. st7565_write(qmk_logo, false);
  318. st7565_write(" Infinity Ergodox ", false);
  319. }
  320. }
  321. #endif
  322. #if defined(SPLIT_KEYBOARD)
  323. void usart_master_init(SerialDriver **driver) {
  324. PORTA->PCR[1] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(2);
  325. PORTA->PCR[2] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(2);
  326. // driver is set to SD1 in config.h
  327. }
  328. void usart_slave_init(SerialDriver **driver) {
  329. PORTE->PCR[0] = PORTx_PCRn_PE | PORTx_PCRn_PS | PORTx_PCRn_PFE | PORTx_PCRn_MUX(3);
  330. PORTE->PCR[1] = PORTx_PCRn_DSE | PORTx_PCRn_SRE | PORTx_PCRn_MUX(3);
  331. *driver = &SD2;
  332. }
  333. #endif