ergodox_infinity.c 15 KB

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