via.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* Copyright 2019 Jason Williams (Wilba)
  2. *
  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. *
  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. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef RAW_ENABLE
  17. # error "RAW_ENABLE is not enabled"
  18. #endif
  19. #ifndef DYNAMIC_KEYMAP_ENABLE
  20. # error "DYNAMIC_KEYMAP_ENABLE is not enabled"
  21. #endif
  22. #include "quantum.h"
  23. #include "via.h"
  24. #include "raw_hid.h"
  25. #include "dynamic_keymap.h"
  26. #include "tmk_core/common/eeprom.h"
  27. #include "version.h" // for QMK_BUILDDATE used in EEPROM magic
  28. // Can be called in an overriding via_init_kb() to test if keyboard level code usage of
  29. // EEPROM is invalid and use/save defaults.
  30. bool via_eeprom_is_valid(void)
  31. {
  32. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  33. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  34. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  35. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  36. return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
  37. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
  38. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
  39. }
  40. // Sets VIA/keyboard level usage of EEPROM to valid/invalid
  41. // Keyboard level code (eg. via_init_kb()) should not call this
  42. void via_eeprom_set_valid(bool valid)
  43. {
  44. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  45. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  46. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  47. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  48. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF);
  49. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF);
  50. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF);
  51. }
  52. // Flag QMK and VIA/keyboard level EEPROM as invalid.
  53. // Used in bootmagic_lite() and VIA command handler.
  54. // Keyboard level code should not need to call this.
  55. void via_eeprom_reset(void)
  56. {
  57. // Set the VIA specific EEPROM state as invalid.
  58. via_eeprom_set_valid(false);
  59. // Set the TMK/QMK EEPROM state as invalid.
  60. eeconfig_disable();
  61. }
  62. // Override bootmagic_lite() so it can flag EEPROM as invalid
  63. // as well as jump to bootloader, thus performing a "factory reset"
  64. // of dynamic keymaps and optionally backlight/other settings.
  65. void bootmagic_lite(void)
  66. {
  67. // The lite version of TMK's bootmagic based on Wilba.
  68. // 100% less potential for accidentally making the
  69. // keyboard do stupid things.
  70. // We need multiple scans because debouncing can't be turned off.
  71. matrix_scan();
  72. #if defined(DEBOUNCE) && DEBOUNCE > 0
  73. wait_ms(DEBOUNCE * 2);
  74. #else
  75. wait_ms(30);
  76. #endif
  77. matrix_scan();
  78. // If the Esc and space bar are held down on power up,
  79. // reset the EEPROM valid state and jump to bootloader.
  80. // Assumes Esc is at [0,0].
  81. // This isn't very generalized, but we need something that doesn't
  82. // rely on user's keymaps in firmware or EEPROM.
  83. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  84. // This is the only difference from the default implementation.
  85. via_eeprom_reset();
  86. // Jump to bootloader.
  87. bootloader_jump();
  88. }
  89. }
  90. // Override this at the keyboard code level to check
  91. // VIA's EEPROM valid state and reset to defaults as needed.
  92. // Used by keyboards that store their own state in EEPROM,
  93. // for backlight, rotary encoders, etc.
  94. // The override should not set via_eeprom_set_valid(true) as
  95. // the caller also needs to check the valid state.
  96. __attribute__((weak)) void via_init_kb(void) {
  97. }
  98. // Called by QMK core to initialize dynamic keymaps etc.
  99. void via_init(void)
  100. {
  101. // Let keyboard level test EEPROM valid state,
  102. // but not set it valid, it is done here.
  103. via_init_kb();
  104. // If the EEPROM has the magic, the data is good.
  105. // OK to load from EEPROM.
  106. if (via_eeprom_is_valid()) {
  107. } else {
  108. // This resets the layout options
  109. via_set_layout_options(0);
  110. // This resets the keymaps in EEPROM to what is in flash.
  111. dynamic_keymap_reset();
  112. // This resets the macros in EEPROM to nothing.
  113. dynamic_keymap_macro_reset();
  114. // Save the magic number last, in case saving was interrupted
  115. via_eeprom_set_valid(true);
  116. }
  117. }
  118. // This is generalized so the layout options EEPROM usage can be
  119. // variable, between 1 and 4 bytes.
  120. uint32_t via_get_layout_options(void)
  121. {
  122. uint32_t value = 0;
  123. // Start at the most significant byte
  124. void * source = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR);
  125. for ( uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++ ) {
  126. value = value << 8;
  127. value |= eeprom_read_byte(source);
  128. source++;
  129. }
  130. return value;
  131. }
  132. void via_set_layout_options(uint32_t value)
  133. {
  134. // Start at the least significant byte
  135. void * target = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR+VIA_EEPROM_LAYOUT_OPTIONS_SIZE-1);
  136. for ( uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++ ) {
  137. eeprom_update_byte(target, value & 0xFF );
  138. value = value >> 8;
  139. target--;
  140. }
  141. }
  142. // Called by QMK core to process VIA-specific keycodes.
  143. bool process_record_via(uint16_t keycode, keyrecord_t *record)
  144. {
  145. // Handle macros
  146. if (record->event.pressed) {
  147. if ( keycode >= MACRO00 && keycode <= MACRO15 )
  148. {
  149. uint8_t id = keycode - MACRO00;
  150. dynamic_keymap_macro_send(id);
  151. return false;
  152. }
  153. }
  154. // TODO: ideally this would be generalized and refactored into
  155. // QMK core as advanced keycodes, until then, the simple case
  156. // can be available here to keyboards using VIA
  157. switch(keycode) {
  158. case FN_MO13:
  159. if (record->event.pressed) {
  160. layer_on(1);
  161. update_tri_layer(1, 2, 3);
  162. } else {
  163. layer_off(1);
  164. update_tri_layer(1, 2, 3);
  165. }
  166. return false;
  167. break;
  168. case FN_MO23:
  169. if (record->event.pressed) {
  170. layer_on(2);
  171. update_tri_layer(1, 2, 3);
  172. } else {
  173. layer_off(2);
  174. update_tri_layer(1, 2, 3);
  175. }
  176. return false;
  177. break;
  178. }
  179. return true;
  180. }
  181. // Keyboard level code can override this to handle custom messages from VIA.
  182. // See raw_hid_receive() implementation.
  183. // DO NOT call raw_hid_send() in the overide function.
  184. __attribute__((weak)) void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
  185. uint8_t *command_id = &(data[0]);
  186. *command_id = id_unhandled;
  187. }
  188. // VIA handles received HID messages first, and will route to
  189. // raw_hid_receive_kb() for command IDs that are not handled here.
  190. // This gives the keyboard code level the ability to handle the command
  191. // specifically.
  192. //
  193. // raw_hid_send() is called at the end, with the same buffer, which was
  194. // possibly modified with returned values.
  195. void raw_hid_receive( uint8_t *data, uint8_t length )
  196. {
  197. uint8_t *command_id = &(data[0]);
  198. uint8_t *command_data = &(data[1]);
  199. switch ( *command_id )
  200. {
  201. case id_get_protocol_version:
  202. {
  203. command_data[0] = VIA_PROTOCOL_VERSION >> 8;
  204. command_data[1] = VIA_PROTOCOL_VERSION & 0xFF;
  205. break;
  206. }
  207. case id_get_keyboard_value:
  208. {
  209. switch ( command_data[0] )
  210. {
  211. case id_uptime:
  212. {
  213. uint32_t value = timer_read32();
  214. command_data[1] = (value >> 24 ) & 0xFF;
  215. command_data[2] = (value >> 16 ) & 0xFF;
  216. command_data[3] = (value >> 8 ) & 0xFF;
  217. command_data[4] = value & 0xFF;
  218. break;
  219. }
  220. case id_layout_options:
  221. {
  222. uint32_t value = via_get_layout_options();
  223. command_data[1] = (value >> 24 ) & 0xFF;
  224. command_data[2] = (value >> 16 ) & 0xFF;
  225. command_data[3] = (value >> 8 ) & 0xFF;
  226. command_data[4] = value & 0xFF;
  227. break;
  228. }
  229. case id_switch_matrix_state:
  230. {
  231. #if ( (MATRIX_COLS/8+1)*MATRIX_ROWS <= 28 )
  232. uint8_t i = 1;
  233. for ( uint8_t row=0; row<MATRIX_ROWS; row++ ) {
  234. matrix_row_t value = matrix_get_row(row);
  235. #if (MATRIX_COLS > 24)
  236. command_data[i++] = (value >> 24 ) & 0xFF;
  237. #endif
  238. #if (MATRIX_COLS > 16)
  239. command_data[i++] = (value >> 16 ) & 0xFF;
  240. #endif
  241. #if (MATRIX_COLS > 8)
  242. command_data[i++] = (value >> 8 ) & 0xFF;
  243. #endif
  244. command_data[i++] = value & 0xFF;
  245. }
  246. #endif
  247. break;
  248. }
  249. default:
  250. {
  251. raw_hid_receive_kb(data,length);
  252. break;
  253. }
  254. }
  255. break;
  256. }
  257. case id_set_keyboard_value:
  258. {
  259. switch ( command_data[0] )
  260. {
  261. case id_layout_options:
  262. {
  263. uint32_t value = ( (uint32_t)command_data[1] << 24 ) |
  264. ( (uint32_t)command_data[2] << 16 ) |
  265. ( (uint32_t)command_data[3] << 8 ) |
  266. (uint32_t)command_data[4];
  267. via_set_layout_options(value);
  268. break;
  269. }
  270. default:
  271. {
  272. raw_hid_receive_kb(data,length);
  273. break;
  274. }
  275. }
  276. break;
  277. }
  278. case id_dynamic_keymap_get_keycode:
  279. {
  280. uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
  281. command_data[3] = keycode >> 8;
  282. command_data[4] = keycode & 0xFF;
  283. break;
  284. }
  285. case id_dynamic_keymap_set_keycode:
  286. {
  287. dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
  288. break;
  289. }
  290. case id_dynamic_keymap_reset:
  291. {
  292. dynamic_keymap_reset();
  293. break;
  294. }
  295. case id_backlight_config_set_value:
  296. case id_backlight_config_get_value:
  297. case id_backlight_config_save:
  298. {
  299. raw_hid_receive_kb(data, length);
  300. break;
  301. }
  302. case id_dynamic_keymap_macro_get_count:
  303. {
  304. command_data[0] = dynamic_keymap_macro_get_count();
  305. break;
  306. }
  307. case id_dynamic_keymap_macro_get_buffer_size:
  308. {
  309. uint16_t size = dynamic_keymap_macro_get_buffer_size();
  310. command_data[0] = size >> 8;
  311. command_data[1] = size & 0xFF;
  312. break;
  313. }
  314. case id_dynamic_keymap_macro_get_buffer:
  315. {
  316. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  317. uint16_t size = command_data[2]; // size <= 28
  318. dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
  319. break;
  320. }
  321. case id_dynamic_keymap_macro_set_buffer:
  322. {
  323. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  324. uint16_t size = command_data[2]; // size <= 28
  325. dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
  326. break;
  327. }
  328. case id_dynamic_keymap_macro_reset:
  329. {
  330. dynamic_keymap_macro_reset();
  331. break;
  332. }
  333. case id_dynamic_keymap_get_layer_count:
  334. {
  335. command_data[0] = dynamic_keymap_get_layer_count();
  336. break;
  337. }
  338. case id_dynamic_keymap_get_buffer:
  339. {
  340. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  341. uint16_t size = command_data[2]; // size <= 28
  342. dynamic_keymap_get_buffer( offset, size, &command_data[3] );
  343. break;
  344. }
  345. case id_dynamic_keymap_set_buffer:
  346. {
  347. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  348. uint16_t size = command_data[2]; // size <= 28
  349. dynamic_keymap_set_buffer( offset, size, &command_data[3] );
  350. break;
  351. }
  352. case id_eeprom_reset:
  353. {
  354. via_eeprom_reset();
  355. break;
  356. }
  357. case id_bootloader_jump:
  358. {
  359. // Need to send data back before the jump
  360. // Informs host that the command is handled
  361. raw_hid_send( data, length );
  362. // Give host time to read it
  363. wait_ms(100);
  364. bootloader_jump();
  365. break;
  366. }
  367. default:
  368. {
  369. // The command ID is not known
  370. // Return the unhandled state
  371. *command_id = id_unhandled;
  372. break;
  373. }
  374. }
  375. // Return the same buffer, optionally with values changed
  376. // (i.e. returning state to the host, or the unhandled state).
  377. raw_hid_send( data, length );
  378. }