From 4bbfecae90e994b4a7d9bf5db06a995fb05d6ab2 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Thu, 14 Mar 2024 10:45:03 +0000 Subject: [PATCH 1/2] Infer eeconfig identifiers (#22135) Co-authored-by: Nick Brassel --- platforms/eeprom.h | 4 +++ quantum/eeconfig.c | 6 ++--- quantum/eeconfig.h | 64 ++++++++++++++++++++++++++++++---------------- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/platforms/eeprom.h b/platforms/eeprom.h index fbfef203341..8e69eecc4cb 100644 --- a/platforms/eeprom.h +++ b/platforms/eeprom.h @@ -22,6 +22,10 @@ void eeprom_update_dword(uint32_t *__p, uint32_t __value); void eeprom_update_block(const void *__src, void *__dst, size_t __n); #endif +static inline void eeprom_write_qword(uint64_t *__p, uint64_t __value) { + eeprom_update_block(&__value, __p, sizeof(uint64_t)); +} + #if defined(EEPROM_CUSTOM) # ifndef EEPROM_SIZE # error EEPROM_SIZE has not been defined for custom driver. diff --git a/quantum/eeconfig.c b/quantum/eeconfig.c index 2d2180b4b44..40690d6a97a 100644 --- a/quantum/eeconfig.c +++ b/quantum/eeconfig.c @@ -19,6 +19,8 @@ void via_eeprom_set_valid(bool valid); void eeconfig_init_via(void); #endif +_Static_assert((intptr_t)EECONFIG_HANDEDNESS == 14, "EEPROM handedness offset is incorrect"); + /** \brief eeconfig enable * * FIXME: needs doc @@ -57,11 +59,9 @@ void eeconfig_init_quantum(void) { eeprom_update_byte(EECONFIG_AUDIO, 0); eeprom_update_dword(EECONFIG_RGBLIGHT, 0); eeprom_update_byte(EECONFIG_RGBLIGHT_EXTENDED, 0); - eeprom_update_byte(EECONFIG_UNUSED, 0); eeprom_update_byte(EECONFIG_UNICODEMODE, 0); eeprom_update_byte(EECONFIG_STENOMODE, 0); - uint64_t dummy = 0; - eeprom_update_block(&dummy, EECONFIG_RGB_MATRIX, sizeof(uint64_t)); + eeprom_write_qword(EECONFIG_RGB_MATRIX, 0); eeprom_update_dword(EECONFIG_HAPTIC, 0); #if defined(HAPTIC_ENABLE) haptic_reset(); diff --git a/quantum/eeconfig.h b/quantum/eeconfig.h index d7cce166bd7..fa0dd799d19 100644 --- a/quantum/eeconfig.h +++ b/quantum/eeconfig.h @@ -19,37 +19,57 @@ along with this program. If not, see . #include #include +#include // offsetof #include "eeprom.h" +#include "util.h" #ifndef EECONFIG_MAGIC_NUMBER -# define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEE6 // When changing, decrement this value to avoid future re-init issues +# define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEE5 // When changing, decrement this value to avoid future re-init issues #endif #define EECONFIG_MAGIC_NUMBER_OFF (uint16_t)0xFFFF -/* EEPROM parameter address */ -#define EECONFIG_MAGIC (uint16_t *)0 -#define EECONFIG_DEBUG (uint8_t *)2 -#define EECONFIG_DEFAULT_LAYER (uint8_t *)3 -#define EECONFIG_KEYMAP (uint16_t *)4 -#define EECONFIG_BACKLIGHT (uint8_t *)6 -#define EECONFIG_AUDIO (uint8_t *)7 -#define EECONFIG_RGBLIGHT (uint32_t *)8 -#define EECONFIG_UNICODEMODE (uint8_t *)12 -#define EECONFIG_STENOMODE (uint8_t *)13 -// EEHANDS for two handed boards -#define EECONFIG_HANDEDNESS (uint8_t *)14 -#define EECONFIG_KEYBOARD (uint32_t *)15 -#define EECONFIG_USER (uint32_t *)19 -#define EECONFIG_UNUSED (uint8_t *)23 -// Mutually exclusive -#define EECONFIG_LED_MATRIX (uint32_t *)24 -#define EECONFIG_RGB_MATRIX (uint64_t *)24 +// Dummy struct only used to calculate offsets +typedef struct PACKED { + uint16_t magic; + uint8_t debug; + uint8_t default_layer; + uint16_t keymap; + uint8_t backlight; + uint8_t audio; + uint32_t rgblight; + uint8_t unicode; + uint8_t steno; + uint8_t handedness; + uint32_t keyboard; + uint32_t user; + union { // Mutually exclusive + uint32_t led_matrix; + uint64_t rgb_matrix; + }; + uint32_t haptic; + uint8_t rgblight_ext; +} eeprom_core_t; -#define EECONFIG_HAPTIC (uint32_t *)32 -#define EECONFIG_RGBLIGHT_EXTENDED (uint8_t *)36 +/* EEPROM parameter address */ +#define EECONFIG_MAGIC (uint16_t *)(offsetof(eeprom_core_t, magic)) +#define EECONFIG_DEBUG (uint8_t *)(offsetof(eeprom_core_t, debug)) +#define EECONFIG_DEFAULT_LAYER (uint8_t *)(offsetof(eeprom_core_t, default_layer)) +#define EECONFIG_KEYMAP (uint16_t *)(offsetof(eeprom_core_t, keymap)) +#define EECONFIG_BACKLIGHT (uint8_t *)(offsetof(eeprom_core_t, backlight)) +#define EECONFIG_AUDIO (uint8_t *)(offsetof(eeprom_core_t, audio)) +#define EECONFIG_RGBLIGHT (uint32_t *)(offsetof(eeprom_core_t, rgblight)) +#define EECONFIG_UNICODEMODE (uint8_t *)(offsetof(eeprom_core_t, unicode)) +#define EECONFIG_STENOMODE (uint8_t *)(offsetof(eeprom_core_t, steno)) +#define EECONFIG_HANDEDNESS (uint8_t *)(offsetof(eeprom_core_t, handedness)) +#define EECONFIG_KEYBOARD (uint32_t *)(offsetof(eeprom_core_t, keyboard)) +#define EECONFIG_USER (uint32_t *)(offsetof(eeprom_core_t, user)) +#define EECONFIG_LED_MATRIX (uint32_t *)(offsetof(eeprom_core_t, led_matrix)) +#define EECONFIG_RGB_MATRIX (uint64_t *)(offsetof(eeprom_core_t, rgb_matrix)) +#define EECONFIG_HAPTIC (uint32_t *)(offsetof(eeprom_core_t, haptic)) +#define EECONFIG_RGBLIGHT_EXTENDED (uint8_t *)(offsetof(eeprom_core_t, rgblight_ext)) // Size of EEPROM being used for core data storage -#define EECONFIG_BASE_SIZE 37 +#define EECONFIG_BASE_SIZE ((uint8_t)sizeof(eeprom_core_t)) // Size of EEPROM dedicated to keyboard- and user-specific data #ifndef EECONFIG_KB_DATA_SIZE From 63dd131d812be4b8d4894fc20ca9968e25996b07 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Thu, 14 Mar 2024 10:45:12 +0000 Subject: [PATCH 2/2] Refactor vusb to protocol use pre/post task (#14944) --- quantum/main.c | 35 +++++++++++++++-------------- tmk_core/protocol/chibios/chibios.c | 13 ----------- tmk_core/protocol/lufa/lufa.c | 6 +---- tmk_core/protocol/vusb/protocol.c | 32 ++++++-------------------- tmk_core/protocol/vusb/vusb.c | 10 ++++++++- 5 files changed, 35 insertions(+), 61 deletions(-) diff --git a/quantum/main.c b/quantum/main.c index 3b101c522c3..3159c558507 100644 --- a/quantum/main.c +++ b/quantum/main.c @@ -25,22 +25,9 @@ void protocol_pre_task(void); void protocol_post_task(void); // Bodge as refactoring this area sucks.... -void protocol_init(void) __attribute__((weak)); -void protocol_init(void) { - protocol_pre_init(); - - keyboard_init(); - - protocol_post_init(); -} - -void protocol_task(void) __attribute__((weak)); -void protocol_task(void) { - protocol_pre_task(); - +void protocol_keyboard_task(void) __attribute__((weak)); +void protocol_keyboard_task(void) { keyboard_task(); - - protocol_post_task(); } /** \brief Main @@ -53,11 +40,25 @@ int main(void) { protocol_setup(); keyboard_setup(); - protocol_init(); + protocol_pre_init(); + keyboard_init(); + protocol_post_init(); /* Main loop */ while (true) { - protocol_task(); + protocol_pre_task(); + protocol_keyboard_task(); + protocol_post_task(); + +#ifdef RAW_ENABLE + void raw_hid_task(void); + raw_hid_task(); +#endif + +#ifdef CONSOLE_ENABLE + void console_task(void); + console_task(); +#endif #ifdef QUANTUM_PAINTER_ENABLE // Run Quantum Painter task diff --git a/tmk_core/protocol/chibios/chibios.c b/tmk_core/protocol/chibios/chibios.c index 76a37ae538a..360e6b4b046 100644 --- a/tmk_core/protocol/chibios/chibios.c +++ b/tmk_core/protocol/chibios/chibios.c @@ -70,13 +70,6 @@ host_driver_t chibios_driver = {keyboard_leds, send_keyboard, send_nkro, send_mo void virtser_task(void); #endif -#ifdef RAW_ENABLE -void raw_hid_task(void); -#endif - -#ifdef CONSOLE_ENABLE -void console_task(void); -#endif #ifdef MIDI_ENABLE void midi_ep_task(void); #endif @@ -209,17 +202,11 @@ void protocol_pre_task(void) { } void protocol_post_task(void) { -#ifdef CONSOLE_ENABLE - console_task(); -#endif #ifdef MIDI_ENABLE midi_ep_task(); #endif #ifdef VIRTSER_ENABLE virtser_task(); -#endif -#ifdef RAW_ENABLE - raw_hid_task(); #endif usb_idle_task(); } diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 22cc0db8ced..d6f0c69b6b0 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -151,7 +151,7 @@ __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) { * * FIXME: Needs doc */ -static void raw_hid_task(void) { +void raw_hid_task(void) { // Create a temporary buffer to hold the read in data from the host uint8_t data[RAW_EPSIZE]; bool data_read = false; @@ -865,10 +865,6 @@ void protocol_post_task(void) { CDC_Device_USBTask(&cdc_device); #endif -#ifdef RAW_ENABLE - raw_hid_task(); -#endif - #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); #endif diff --git a/tmk_core/protocol/vusb/protocol.c b/tmk_core/protocol/vusb/protocol.c index 6178d48ef2a..41ccf451fdb 100644 --- a/tmk_core/protocol/vusb/protocol.c +++ b/tmk_core/protocol/vusb/protocol.c @@ -31,14 +31,6 @@ # include "sleep_led.h" #endif -#ifdef CONSOLE_ENABLE -void console_task(void); -#endif - -#ifdef RAW_ENABLE -void raw_hid_task(void); -#endif - /* This is from main.c of USBaspLoader */ static void initForUsbConnectivity(void) { uint8_t i = 0; @@ -136,7 +128,7 @@ static inline bool should_do_suspend(void) { return vusb_suspended; } -void protocol_task(void) { +void protocol_pre_task(void) { #if !defined(NO_USB_STARTUP_CHECK) if (should_do_suspend()) { dprintln("suspending keyboard"); @@ -159,7 +151,9 @@ void protocol_task(void) { vusb_wakeup(); } #endif +} +void protocol_keyboard_task(void) { usbPoll(); // TODO: configuration process is inconsistent. it sometime fails. @@ -167,20 +161,8 @@ void protocol_task(void) { if (usbConfiguration && usbInterruptIsReady()) { keyboard_task(); } - -#ifdef RAW_ENABLE - usbPoll(); - - if (usbConfiguration && usbInterruptIsReady4()) { - raw_hid_task(); - } -#endif - -#ifdef CONSOLE_ENABLE - usbPoll(); - - if (usbConfiguration && usbInterruptIsReady3()) { - console_task(); - } -#endif +} + +void protocol_post_task(void) { + // do nothing } diff --git a/tmk_core/protocol/vusb/vusb.c b/tmk_core/protocol/vusb/vusb.c index cfeeed37126..c8ab4942536 100644 --- a/tmk_core/protocol/vusb/vusb.c +++ b/tmk_core/protocol/vusb/vusb.c @@ -162,6 +162,12 @@ __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) { } void raw_hid_task(void) { + usbPoll(); + + if (!usbConfiguration || !usbInterruptIsReady4()) { + return; + } + if (raw_output_received_bytes == RAW_BUFFER_SIZE) { raw_hid_receive(raw_output_buffer, RAW_BUFFER_SIZE); raw_output_received_bytes = 0; @@ -182,7 +188,9 @@ int8_t sendchar(uint8_t c) { } void console_task(void) { - if (!usbConfiguration) { + usbPoll(); + + if (!usbConfiguration || !usbInterruptIsReady3()) { return; }