aboutsummaryrefslogtreecommitdiff
path: root/quantum/via.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/via.c')
-rw-r--r--quantum/via.c247
1 files changed, 102 insertions, 145 deletions
diff --git a/quantum/via.c b/quantum/via.c
index 64b05324a..b3934d9f0 100644
--- a/quantum/via.c
+++ b/quantum/via.c
@@ -28,41 +28,36 @@
28#include "raw_hid.h" 28#include "raw_hid.h"
29#include "dynamic_keymap.h" 29#include "dynamic_keymap.h"
30#include "tmk_core/common/eeprom.h" 30#include "tmk_core/common/eeprom.h"
31#include "version.h" // for QMK_BUILDDATE used in EEPROM magic 31#include "version.h" // for QMK_BUILDDATE used in EEPROM magic
32 32
33// Can be called in an overriding via_init_kb() to test if keyboard level code usage of 33// Can be called in an overriding via_init_kb() to test if keyboard level code usage of
34// EEPROM is invalid and use/save defaults. 34// EEPROM is invalid and use/save defaults.
35bool via_eeprom_is_valid(void) 35bool via_eeprom_is_valid(void) {
36{ 36 char * p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
37 char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54" 37 uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
38 uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F ); 38 uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
39 uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F ); 39 uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
40 uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
41 40
42 return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 && 41 return (eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0) == magic0 && eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1) == magic1 && eeprom_read_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2) == magic2);
43 eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
44 eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
45} 42}
46 43
47// Sets VIA/keyboard level usage of EEPROM to valid/invalid 44// Sets VIA/keyboard level usage of EEPROM to valid/invalid
48// Keyboard level code (eg. via_init_kb()) should not call this 45// Keyboard level code (eg. via_init_kb()) should not call this
49void via_eeprom_set_valid(bool valid) 46void via_eeprom_set_valid(bool valid) {
50{ 47 char * p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
51 char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54" 48 uint8_t magic0 = ((p[2] & 0x0F) << 4) | (p[3] & 0x0F);
52 uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F ); 49 uint8_t magic1 = ((p[5] & 0x0F) << 4) | (p[6] & 0x0F);
53 uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F ); 50 uint8_t magic2 = ((p[8] & 0x0F) << 4) | (p[9] & 0x0F);
54 uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
55 51
56 eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF); 52 eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 0, valid ? magic0 : 0xFF);
57 eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF); 53 eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 1, valid ? magic1 : 0xFF);
58 eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF); 54 eeprom_update_byte((void *)VIA_EEPROM_MAGIC_ADDR + 2, valid ? magic2 : 0xFF);
59} 55}
60 56
61// Flag QMK and VIA/keyboard level EEPROM as invalid. 57// Flag QMK and VIA/keyboard level EEPROM as invalid.
62// Used in bootmagic_lite() and VIA command handler. 58// Used in bootmagic_lite() and VIA command handler.
63// Keyboard level code should not need to call this. 59// Keyboard level code should not need to call this.
64void via_eeprom_reset(void) 60void via_eeprom_reset(void) {
65{
66 // Set the VIA specific EEPROM state as invalid. 61 // Set the VIA specific EEPROM state as invalid.
67 via_eeprom_set_valid(false); 62 via_eeprom_set_valid(false);
68 // Set the TMK/QMK EEPROM state as invalid. 63 // Set the TMK/QMK EEPROM state as invalid.
@@ -72,8 +67,7 @@ void via_eeprom_reset(void)
72// Override bootmagic_lite() so it can flag EEPROM as invalid 67// Override bootmagic_lite() so it can flag EEPROM as invalid
73// as well as jump to bootloader, thus performing a "factory reset" 68// as well as jump to bootloader, thus performing a "factory reset"
74// of dynamic keymaps and optionally backlight/other settings. 69// of dynamic keymaps and optionally backlight/other settings.
75void bootmagic_lite(void) 70void bootmagic_lite(void) {
76{
77 // The lite version of TMK's bootmagic based on Wilba. 71 // The lite version of TMK's bootmagic based on Wilba.
78 // 100% less potential for accidentally making the 72 // 100% less potential for accidentally making the
79 // keyboard do stupid things. 73 // keyboard do stupid things.
@@ -106,12 +100,10 @@ void bootmagic_lite(void)
106// for backlight, rotary encoders, etc. 100// for backlight, rotary encoders, etc.
107// The override should not set via_eeprom_set_valid(true) as 101// The override should not set via_eeprom_set_valid(true) as
108// the caller also needs to check the valid state. 102// the caller also needs to check the valid state.
109__attribute__((weak)) void via_init_kb(void) { 103__attribute__((weak)) void via_init_kb(void) {}
110}
111 104
112// Called by QMK core to initialize dynamic keymaps etc. 105// Called by QMK core to initialize dynamic keymaps etc.
113void via_init(void) 106void via_init(void) {
114{
115 // Let keyboard level test EEPROM valid state, 107 // Let keyboard level test EEPROM valid state,
116 // but not set it valid, it is done here. 108 // but not set it valid, it is done here.
117 via_init_kb(); 109 via_init_kb();
@@ -119,7 +111,7 @@ void via_init(void)
119 // If the EEPROM has the magic, the data is good. 111 // If the EEPROM has the magic, the data is good.
120 // OK to load from EEPROM. 112 // OK to load from EEPROM.
121 if (via_eeprom_is_valid()) { 113 if (via_eeprom_is_valid()) {
122 } else { 114 } else {
123 // This resets the layout options 115 // This resets the layout options
124 via_set_layout_options(0); 116 via_set_layout_options(0);
125 // This resets the keymaps in EEPROM to what is in flash. 117 // This resets the keymaps in EEPROM to what is in flash.
@@ -133,12 +125,11 @@ void via_init(void)
133 125
134// This is generalized so the layout options EEPROM usage can be 126// This is generalized so the layout options EEPROM usage can be
135// variable, between 1 and 4 bytes. 127// variable, between 1 and 4 bytes.
136uint32_t via_get_layout_options(void) 128uint32_t via_get_layout_options(void) {
137{
138 uint32_t value = 0; 129 uint32_t value = 0;
139 // Start at the most significant byte 130 // Start at the most significant byte
140 void * source = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR); 131 void *source = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR);
141 for ( uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++ ) { 132 for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
142 value = value << 8; 133 value = value << 8;
143 value |= eeprom_read_byte(source); 134 value |= eeprom_read_byte(source);
144 source++; 135 source++;
@@ -146,24 +137,21 @@ uint32_t via_get_layout_options(void)
146 return value; 137 return value;
147} 138}
148 139
149void via_set_layout_options(uint32_t value) 140void via_set_layout_options(uint32_t value) {
150{
151 // Start at the least significant byte 141 // Start at the least significant byte
152 void * target = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR+VIA_EEPROM_LAYOUT_OPTIONS_SIZE-1); 142 void *target = (void *)(VIA_EEPROM_LAYOUT_OPTIONS_ADDR + VIA_EEPROM_LAYOUT_OPTIONS_SIZE - 1);
153 for ( uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++ ) { 143 for (uint8_t i = 0; i < VIA_EEPROM_LAYOUT_OPTIONS_SIZE; i++) {
154 eeprom_update_byte(target, value & 0xFF ); 144 eeprom_update_byte(target, value & 0xFF);
155 value = value >> 8; 145 value = value >> 8;
156 target--; 146 target--;
157 } 147 }
158} 148}
159 149
160// Called by QMK core to process VIA-specific keycodes. 150// Called by QMK core to process VIA-specific keycodes.
161bool process_record_via(uint16_t keycode, keyrecord_t *record) 151bool process_record_via(uint16_t keycode, keyrecord_t *record) {
162{
163 // Handle macros 152 // Handle macros
164 if (record->event.pressed) { 153 if (record->event.pressed) {
165 if ( keycode >= MACRO00 && keycode <= MACRO15 ) 154 if (keycode >= MACRO00 && keycode <= MACRO15) {
166 {
167 uint8_t id = keycode - MACRO00; 155 uint8_t id = keycode - MACRO00;
168 dynamic_keymap_macro_send(id); 156 dynamic_keymap_macro_send(id);
169 return false; 157 return false;
@@ -173,7 +161,7 @@ bool process_record_via(uint16_t keycode, keyrecord_t *record)
173 // TODO: ideally this would be generalized and refactored into 161 // TODO: ideally this would be generalized and refactored into
174 // QMK core as advanced keycodes, until then, the simple case 162 // QMK core as advanced keycodes, until then, the simple case
175 // can be available here to keyboards using VIA 163 // can be available here to keyboards using VIA
176 switch(keycode) { 164 switch (keycode) {
177 case FN_MO13: 165 case FN_MO13:
178 if (record->event.pressed) { 166 if (record->event.pressed) {
179 layer_on(1); 167 layer_on(1);
@@ -203,7 +191,7 @@ bool process_record_via(uint16_t keycode, keyrecord_t *record)
203// DO NOT call raw_hid_send() in the overide function. 191// DO NOT call raw_hid_send() in the overide function.
204__attribute__((weak)) void raw_hid_receive_kb(uint8_t *data, uint8_t length) { 192__attribute__((weak)) void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
205 uint8_t *command_id = &(data[0]); 193 uint8_t *command_id = &(data[0]);
206 *command_id = id_unhandled; 194 *command_id = id_unhandled;
207} 195}
208 196
209// VIA handles received HID messages first, and will route to 197// VIA handles received HID messages first, and will route to
@@ -211,182 +199,151 @@ __attribute__((weak)) void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
211// This gives the keyboard code level the ability to handle the command 199// This gives the keyboard code level the ability to handle the command
212// specifically. 200// specifically.
213// 201//
214// raw_hid_send() is called at the end, with the same buffer, which was 202// raw_hid_send() is called at the end, with the same buffer, which was
215// possibly modified with returned values. 203// possibly modified with returned values.
216void raw_hid_receive( uint8_t *data, uint8_t length ) 204void raw_hid_receive(uint8_t *data, uint8_t length) {
217{ 205 uint8_t *command_id = &(data[0]);
218 uint8_t *command_id = &(data[0]);
219 uint8_t *command_data = &(data[1]); 206 uint8_t *command_data = &(data[1]);
220 switch ( *command_id ) 207 switch (*command_id) {
221 { 208 case id_get_protocol_version: {
222 case id_get_protocol_version:
223 {
224 command_data[0] = VIA_PROTOCOL_VERSION >> 8; 209 command_data[0] = VIA_PROTOCOL_VERSION >> 8;
225 command_data[1] = VIA_PROTOCOL_VERSION & 0xFF; 210 command_data[1] = VIA_PROTOCOL_VERSION & 0xFF;
226 break; 211 break;
227 } 212 }
228 case id_get_keyboard_value: 213 case id_get_keyboard_value: {
229 { 214 switch (command_data[0]) {
230 switch ( command_data[0] ) 215 case id_uptime: {
231 { 216 uint32_t value = timer_read32();
232 case id_uptime: 217 command_data[1] = (value >> 24) & 0xFF;
233 { 218 command_data[2] = (value >> 16) & 0xFF;
234 uint32_t value = timer_read32(); 219 command_data[3] = (value >> 8) & 0xFF;
235 command_data[1] = (value >> 24 ) & 0xFF;
236 command_data[2] = (value >> 16 ) & 0xFF;
237 command_data[3] = (value >> 8 ) & 0xFF;
238 command_data[4] = value & 0xFF; 220 command_data[4] = value & 0xFF;
239 break; 221 break;
240 } 222 }
241 case id_layout_options: 223 case id_layout_options: {
242 { 224 uint32_t value = via_get_layout_options();
243 uint32_t value = via_get_layout_options(); 225 command_data[1] = (value >> 24) & 0xFF;
244 command_data[1] = (value >> 24 ) & 0xFF; 226 command_data[2] = (value >> 16) & 0xFF;
245 command_data[2] = (value >> 16 ) & 0xFF; 227 command_data[3] = (value >> 8) & 0xFF;
246 command_data[3] = (value >> 8 ) & 0xFF;
247 command_data[4] = value & 0xFF; 228 command_data[4] = value & 0xFF;
248 break; 229 break;
249 } 230 }
250 case id_switch_matrix_state: 231 case id_switch_matrix_state: {
251 { 232#if ((MATRIX_COLS / 8 + 1) * MATRIX_ROWS <= 28)
252#if ( (MATRIX_COLS/8+1)*MATRIX_ROWS <= 28 )
253 uint8_t i = 1; 233 uint8_t i = 1;
254 for ( uint8_t row=0; row<MATRIX_ROWS; row++ ) { 234 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
255 matrix_row_t value = matrix_get_row(row); 235 matrix_row_t value = matrix_get_row(row);
256#if (MATRIX_COLS > 24) 236# if (MATRIX_COLS > 24)
257 command_data[i++] = (value >> 24 ) & 0xFF; 237 command_data[i++] = (value >> 24) & 0xFF;
258#endif 238# endif
259#if (MATRIX_COLS > 16) 239# if (MATRIX_COLS > 16)
260 command_data[i++] = (value >> 16 ) & 0xFF; 240 command_data[i++] = (value >> 16) & 0xFF;
261#endif 241# endif
262#if (MATRIX_COLS > 8) 242# if (MATRIX_COLS > 8)
263 command_data[i++] = (value >> 8 ) & 0xFF; 243 command_data[i++] = (value >> 8) & 0xFF;
264#endif 244# endif
265 command_data[i++] = value & 0xFF; 245 command_data[i++] = value & 0xFF;
266 } 246 }
267#endif 247#endif
268 break; 248 break;
269 } 249 }
270 default: 250 default: {
271 { 251 raw_hid_receive_kb(data, length);
272 raw_hid_receive_kb(data,length);
273 break; 252 break;
274 } 253 }
275 } 254 }
276 break; 255 break;
277 } 256 }
278 case id_set_keyboard_value: 257 case id_set_keyboard_value: {
279 { 258 switch (command_data[0]) {
280 switch ( command_data[0] ) 259 case id_layout_options: {
281 { 260 uint32_t value = ((uint32_t)command_data[1] << 24) | ((uint32_t)command_data[2] << 16) | ((uint32_t)command_data[3] << 8) | (uint32_t)command_data[4];
282 case id_layout_options:
283 {
284 uint32_t value = ( (uint32_t)command_data[1] << 24 ) |
285 ( (uint32_t)command_data[2] << 16 ) |
286 ( (uint32_t)command_data[3] << 8 ) |
287 (uint32_t)command_data[4];
288 via_set_layout_options(value); 261 via_set_layout_options(value);
289 break; 262 break;
290 } 263 }
291 default: 264 default: {
292 { 265 raw_hid_receive_kb(data, length);
293 raw_hid_receive_kb(data,length);
294 break; 266 break;
295 } 267 }
296 } 268 }
297 break; 269 break;
298 } 270 }
299 case id_dynamic_keymap_get_keycode: 271 case id_dynamic_keymap_get_keycode: {
300 { 272 uint16_t keycode = dynamic_keymap_get_keycode(command_data[0], command_data[1], command_data[2]);
301 uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] ); 273 command_data[3] = keycode >> 8;
302 command_data[3] = keycode >> 8; 274 command_data[4] = keycode & 0xFF;
303 command_data[4] = keycode & 0xFF;
304 break; 275 break;
305 } 276 }
306 case id_dynamic_keymap_set_keycode: 277 case id_dynamic_keymap_set_keycode: {
307 { 278 dynamic_keymap_set_keycode(command_data[0], command_data[1], command_data[2], (command_data[3] << 8) | command_data[4]);
308 dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
309 break; 279 break;
310 } 280 }
311 case id_dynamic_keymap_reset: 281 case id_dynamic_keymap_reset: {
312 {
313 dynamic_keymap_reset(); 282 dynamic_keymap_reset();
314 break; 283 break;
315 } 284 }
316 case id_backlight_config_set_value: 285 case id_backlight_config_set_value:
317 case id_backlight_config_get_value: 286 case id_backlight_config_get_value:
318 case id_backlight_config_save: 287 case id_backlight_config_save: {
319 {
320 raw_hid_receive_kb(data, length); 288 raw_hid_receive_kb(data, length);
321 break; 289 break;
322 } 290 }
323 case id_dynamic_keymap_macro_get_count: 291 case id_dynamic_keymap_macro_get_count: {
324 {
325 command_data[0] = dynamic_keymap_macro_get_count(); 292 command_data[0] = dynamic_keymap_macro_get_count();
326 break; 293 break;
327 } 294 }
328 case id_dynamic_keymap_macro_get_buffer_size: 295 case id_dynamic_keymap_macro_get_buffer_size: {
329 { 296 uint16_t size = dynamic_keymap_macro_get_buffer_size();
330 uint16_t size = dynamic_keymap_macro_get_buffer_size();
331 command_data[0] = size >> 8; 297 command_data[0] = size >> 8;
332 command_data[1] = size & 0xFF; 298 command_data[1] = size & 0xFF;
333 break; 299 break;
334 } 300 }
335 case id_dynamic_keymap_macro_get_buffer: 301 case id_dynamic_keymap_macro_get_buffer: {
336 { 302 uint16_t offset = (command_data[0] << 8) | command_data[1];
337 uint16_t offset = ( command_data[0] << 8 ) | command_data[1]; 303 uint16_t size = command_data[2]; // size <= 28
338 uint16_t size = command_data[2]; // size <= 28 304 dynamic_keymap_macro_get_buffer(offset, size, &command_data[3]);
339 dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
340 break; 305 break;
341 } 306 }
342 case id_dynamic_keymap_macro_set_buffer: 307 case id_dynamic_keymap_macro_set_buffer: {
343 { 308 uint16_t offset = (command_data[0] << 8) | command_data[1];
344 uint16_t offset = ( command_data[0] << 8 ) | command_data[1]; 309 uint16_t size = command_data[2]; // size <= 28
345 uint16_t size = command_data[2]; // size <= 28 310 dynamic_keymap_macro_set_buffer(offset, size, &command_data[3]);
346 dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
347 break; 311 break;
348 } 312 }
349 case id_dynamic_keymap_macro_reset: 313 case id_dynamic_keymap_macro_reset: {
350 {
351 dynamic_keymap_macro_reset(); 314 dynamic_keymap_macro_reset();
352 break; 315 break;
353 } 316 }
354 case id_dynamic_keymap_get_layer_count: 317 case id_dynamic_keymap_get_layer_count: {
355 {
356 command_data[0] = dynamic_keymap_get_layer_count(); 318 command_data[0] = dynamic_keymap_get_layer_count();
357 break; 319 break;
358 } 320 }
359 case id_dynamic_keymap_get_buffer: 321 case id_dynamic_keymap_get_buffer: {
360 { 322 uint16_t offset = (command_data[0] << 8) | command_data[1];
361 uint16_t offset = ( command_data[0] << 8 ) | command_data[1]; 323 uint16_t size = command_data[2]; // size <= 28
362 uint16_t size = command_data[2]; // size <= 28 324 dynamic_keymap_get_buffer(offset, size, &command_data[3]);
363 dynamic_keymap_get_buffer( offset, size, &command_data[3] );
364 break; 325 break;
365 } 326 }
366 case id_dynamic_keymap_set_buffer: 327 case id_dynamic_keymap_set_buffer: {
367 { 328 uint16_t offset = (command_data[0] << 8) | command_data[1];
368 uint16_t offset = ( command_data[0] << 8 ) | command_data[1]; 329 uint16_t size = command_data[2]; // size <= 28
369 uint16_t size = command_data[2]; // size <= 28 330 dynamic_keymap_set_buffer(offset, size, &command_data[3]);
370 dynamic_keymap_set_buffer( offset, size, &command_data[3] );
371 break; 331 break;
372 } 332 }
373 case id_eeprom_reset: 333 case id_eeprom_reset: {
374 {
375 via_eeprom_reset(); 334 via_eeprom_reset();
376 break; 335 break;
377 } 336 }
378 case id_bootloader_jump: 337 case id_bootloader_jump: {
379 {
380 // Need to send data back before the jump 338 // Need to send data back before the jump
381 // Informs host that the command is handled 339 // Informs host that the command is handled
382 raw_hid_send( data, length ); 340 raw_hid_send(data, length);
383 // Give host time to read it 341 // Give host time to read it
384 wait_ms(100); 342 wait_ms(100);
385 bootloader_jump(); 343 bootloader_jump();
386 break; 344 break;
387 } 345 }
388 default: 346 default: {
389 {
390 // The command ID is not known 347 // The command ID is not known
391 // Return the unhandled state 348 // Return the unhandled state
392 *command_id = id_unhandled; 349 *command_id = id_unhandled;
@@ -396,5 +353,5 @@ void raw_hid_receive( uint8_t *data, uint8_t length )
396 353
397 // Return the same buffer, optionally with values changed 354 // Return the same buffer, optionally with values changed
398 // (i.e. returning state to the host, or the unhandled state). 355 // (i.e. returning state to the host, or the unhandled state).
399 raw_hid_send( data, length ); 356 raw_hid_send(data, length);
400} 357}