From c66df1664497546f32662409778731143e45a552 Mon Sep 17 00:00:00 2001 From: James Young <18669334+noroadsleft@users.noreply.github.com> Date: Sat, 28 Nov 2020 12:02:18 -0800 Subject: 2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change --- tmk_core/chibios.mk | 10 +- tmk_core/common/action.c | 66 ++- tmk_core/common/action_util.c | 31 +- tmk_core/common/action_util.h | 7 +- tmk_core/common/arm_atsam/suspend.c | 2 +- tmk_core/common/chibios/bootloader.c | 10 +- tmk_core/common/eeconfig.c | 13 + tmk_core/common/keyboard.c | 21 + tmk_core/common/keyboard.h | 3 + tmk_core/common/matrix.h | 10 - tmk_core/common/report.h | 4 + tmk_core/protocol/arm_atsam.mk | 6 +- tmk_core/protocol/arm_atsam/arm_atsam_protocol.h | 2 +- tmk_core/protocol/arm_atsam/led_matrix.c | 472 --------------------- tmk_core/protocol/arm_atsam/led_matrix.h | 158 ------- tmk_core/protocol/arm_atsam/led_matrix_programs.c | 99 ----- tmk_core/protocol/arm_atsam/main_arm_atsam.c | 4 + tmk_core/protocol/arm_atsam/md_rgb_matrix.c | 472 +++++++++++++++++++++ tmk_core/protocol/arm_atsam/md_rgb_matrix.h | 158 +++++++ .../protocol/arm_atsam/md_rgb_matrix_programs.c | 99 +++++ tmk_core/protocol/chibios/main.c | 11 + tmk_core/protocol/chibios/usb_main.c | 81 ++++ tmk_core/protocol/chibios/usb_main.h | 3 + tmk_core/protocol/lufa/lufa.c | 4 + tmk_core/protocol/usb_descriptor.c | 15 +- tmk_core/protocol/usb_descriptor.h | 20 + tmk_core/protocol/vusb/main.c | 4 + 27 files changed, 1010 insertions(+), 775 deletions(-) delete mode 100644 tmk_core/protocol/arm_atsam/led_matrix.c delete mode 100644 tmk_core/protocol/arm_atsam/led_matrix.h delete mode 100644 tmk_core/protocol/arm_atsam/led_matrix_programs.c create mode 100644 tmk_core/protocol/arm_atsam/md_rgb_matrix.c create mode 100644 tmk_core/protocol/arm_atsam/md_rgb_matrix.h create mode 100644 tmk_core/protocol/arm_atsam/md_rgb_matrix_programs.c (limited to 'tmk_core') diff --git a/tmk_core/chibios.mk b/tmk_core/chibios.mk index 2bd53f0dc8..e53edccee2 100644 --- a/tmk_core/chibios.mk +++ b/tmk_core/chibios.mk @@ -123,6 +123,8 @@ else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/chconf.h)","") CHCONFDIR = $(KEYBOARD_PATH_1) else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/$(BOARD)/configs/chconf.h)","") CHCONFDIR = $(TOP_DIR)/platforms/chibios/$(BOARD)/configs +else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/common/configs/chconf.h)","") + CHCONFDIR = $(TOP_DIR)/platforms/chibios/common/configs endif ifneq ("$(wildcard $(KEYBOARD_PATH_5)/halconf.h)","") @@ -137,6 +139,8 @@ else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/halconf.h)","") HALCONFDIR = $(KEYBOARD_PATH_1) else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/$(BOARD)/configs/halconf.h)","") HALCONFDIR = $(TOP_DIR)/platforms/chibios/$(BOARD)/configs +else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/common/configs/halconf.h)","") + HALCONFDIR = $(TOP_DIR)/platforms/chibios/common/configs endif # HAL-OSAL files (optional). @@ -183,8 +187,8 @@ else ifneq ("$(wildcard $(KEYBOARD_PATH_2)/ld/$(MCU_LDSCRIPT).ld)","") LDSCRIPT = $(KEYBOARD_PATH_2)/ld/$(MCU_LDSCRIPT).ld else ifneq ("$(wildcard $(KEYBOARD_PATH_1)/ld/$(MCU_LDSCRIPT).ld)","") LDSCRIPT = $(KEYBOARD_PATH_1)/ld/$(MCU_LDSCRIPT).ld -else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/ld/$(MCU_LDSCRIPT).ld)","") - LDSCRIPT = $(TOP_DIR)/platforms/chibios/ld/$(MCU_LDSCRIPT).ld +else ifneq ("$(wildcard $(TOP_DIR)/platforms/chibios/common/ld/$(MCU_LDSCRIPT).ld)","") + LDSCRIPT = $(TOP_DIR)/platforms/chibios/common/ld/$(MCU_LDSCRIPT).ld else ifneq ("$(wildcard $(STARTUPLD_CONTRIB)/$(MCU_LDSCRIPT).ld)","") LDSCRIPT = $(STARTUPLD_CONTRIB)/$(MCU_LDSCRIPT).ld USE_CHIBIOS_CONTRIB = yes @@ -209,6 +213,8 @@ CHIBISRC := $(patsubst $(TOP_DIR)/%,%,$(CHIBISRC)) EXTRAINCDIRS += $(CHIBIOS)/os/license $(CHIBIOS)/os/oslib/include \ $(TOP_DIR)/platforms/chibios/$(BOARD)/configs \ + $(TOP_DIR)/platforms/chibios/common/configs \ + $(HALCONFDIR) $(CHCONFDIR) \ $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \ $(HALINC) $(PLATFORMINC) $(BOARDINC) $(TESTINC) \ $(STREAMSINC) $(CHIBIOS)/os/various $(COMMON_VPATH) diff --git a/tmk_core/common/action.c b/tmk_core/common/action.c index ee9aa0df77..77da0139f2 100644 --- a/tmk_core/common/action.c +++ b/tmk_core/common/action.c @@ -37,9 +37,13 @@ along with this program. If not, see . # include "nodebug.h" #endif +#ifdef POINTING_DEVICE_ENABLE +# include "pointing_device.h" +#endif + int tp_buttons; -#ifdef RETRO_TAPPING +#if defined(RETRO_TAPPING) || defined(RETRO_TAPPING_PER_KEY) int retro_tapping_counter = 0; #endif @@ -51,6 +55,10 @@ int retro_tapping_counter = 0; __attribute__((weak)) bool get_ignore_mod_tap_interrupt(uint16_t keycode, keyrecord_t *record) { return false; } #endif +#ifdef RETRO_TAPPING_PER_KEY +__attribute__((weak)) bool get_retro_tapping(uint16_t keycode, keyrecord_t *record) { return false; } +#endif + #ifndef TAP_CODE_DELAY # define TAP_CODE_DELAY 0 #endif @@ -67,7 +75,7 @@ void action_exec(keyevent_t event) { dprint("EVENT: "); debug_event(event); dprintln(); -#ifdef RETRO_TAPPING +#if defined(RETRO_TAPPING) || defined(RETRO_TAPPING_PER_KEY) retro_tapping_counter++; #endif } @@ -220,6 +228,19 @@ void process_record_handler(keyrecord_t *record) { process_action(record, action); } +#if defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE) +void register_button(bool pressed, enum mouse_buttons button) { +# ifdef PS2_MOUSE_ENABLE + tp_buttons = pressed ? tp_buttons | button : tp_buttons & ~button; +# endif +# ifdef POINTING_DEVICE_ENABLE + report_mouse_t currentReport = pointing_device_get_report(); + currentReport.buttons = pressed ? currentReport.buttons | button : currentReport.buttons & ~button; + pointing_device_set_report(currentReport); +# endif +} +#endif + /** \brief Take an action and processes it. * * FIXME: Needs documentation. @@ -404,15 +425,23 @@ void process_action(keyrecord_t *record, action_t action) { if (event.pressed) { mousekey_on(action.key.code); switch (action.key.code) { -# ifdef PS2_MOUSE_ENABLE +# if defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE) case KC_MS_BTN1: - tp_buttons |= (1 << 0); + register_button(true, MOUSE_BTN1); break; case KC_MS_BTN2: - tp_buttons |= (1 << 1); + register_button(true, MOUSE_BTN2); break; case KC_MS_BTN3: - tp_buttons |= (1 << 2); + register_button(true, MOUSE_BTN3); + break; +# endif +# ifdef POINTING_DEVICE_ENABLE + case KC_MS_BTN4: + register_button(true, MOUSE_BTN4); + break; + case KC_MS_BTN5: + register_button(true, MOUSE_BTN5); break; # endif default: @@ -422,15 +451,23 @@ void process_action(keyrecord_t *record, action_t action) { } else { mousekey_off(action.key.code); switch (action.key.code) { -# ifdef PS2_MOUSE_ENABLE +# if defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE) case KC_MS_BTN1: - tp_buttons &= ~(1 << 0); + register_button(false, MOUSE_BTN1); break; case KC_MS_BTN2: - tp_buttons &= ~(1 << 1); + register_button(false, MOUSE_BTN2); break; case KC_MS_BTN3: - tp_buttons &= ~(1 << 2); + register_button(false, MOUSE_BTN3); + break; +# endif +# ifdef POINTING_DEVICE_ENABLE + case KC_MS_BTN4: + register_button(false, MOUSE_BTN4); + break; + case KC_MS_BTN5: + register_button(false, MOUSE_BTN5); break; # endif default: @@ -692,20 +729,23 @@ void process_action(keyrecord_t *record, action_t action) { #endif #ifndef NO_ACTION_TAPPING -# ifdef RETRO_TAPPING +# if defined(RETRO_TAPPING) || defined(RETRO_TAPPING_PER_KEY) if (!is_tap_action(action)) { retro_tapping_counter = 0; } else { if (event.pressed) { if (tap_count > 0) { retro_tapping_counter = 0; - } else { } } else { if (tap_count > 0) { retro_tapping_counter = 0; } else { - if (retro_tapping_counter == 2) { + if ( +# ifdef RETRO_TAPPING_PER_KEY + get_retro_tapping(get_event_keycode(record->event, false), record) && +# endif + retro_tapping_counter == 2) { tap_code(action.layer_tap.code); } retro_tapping_counter = 0; diff --git a/tmk_core/common/action_util.c b/tmk_core/common/action_util.c index fd0e4409f0..000503b082 100644 --- a/tmk_core/common/action_util.c +++ b/tmk_core/common/action_util.c @@ -290,6 +290,32 @@ void set_macro_mods(uint8_t mods) { macro_mods = mods; } void clear_macro_mods(void) { macro_mods = 0; } #ifndef NO_ACTION_ONESHOT +/** \brief get oneshot mods + * + * FIXME: needs doc + */ +uint8_t get_oneshot_mods(void) { return oneshot_mods; } + +void add_oneshot_mods(uint8_t mods) { + if ((oneshot_mods & mods) != mods) { +# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) + oneshot_time = timer_read(); +# endif + oneshot_mods |= mods; + oneshot_mods_changed_kb(mods); + } +} + +void del_oneshot_mods(uint8_t mods) { + if (oneshot_mods & mods) { + oneshot_mods &= ~mods; +# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) + oneshot_time = oneshot_mods ? timer_read() : 0; +# endif + oneshot_mods_changed_kb(oneshot_mods); + } +} + /** \brief set oneshot mods * * FIXME: needs doc @@ -316,11 +342,6 @@ void clear_oneshot_mods(void) { oneshot_mods_changed_kb(oneshot_mods); } } -/** \brief get oneshot mods - * - * FIXME: needs doc - */ -uint8_t get_oneshot_mods(void) { return oneshot_mods; } #endif /** \brief Called when the one shot modifiers have been changed. diff --git a/tmk_core/common/action_util.h b/tmk_core/common/action_util.h index 5dd8393da4..743ff1406b 100644 --- a/tmk_core/common/action_util.h +++ b/tmk_core/common/action_util.h @@ -57,12 +57,11 @@ void set_macro_mods(uint8_t mods); void clear_macro_mods(void); /* oneshot modifier */ -void set_oneshot_mods(uint8_t mods); uint8_t get_oneshot_mods(void); +void add_oneshot_mods(uint8_t mods); +void del_oneshot_mods(uint8_t mods); +void set_oneshot_mods(uint8_t mods); void clear_oneshot_mods(void); -void oneshot_toggle(void); -void oneshot_enable(void); -void oneshot_disable(void); bool has_oneshot_mods_timed_out(void); uint8_t get_oneshot_locked_mods(void); diff --git a/tmk_core/common/arm_atsam/suspend.c b/tmk_core/common/arm_atsam/suspend.c index d1077be4c2..e51426128d 100644 --- a/tmk_core/common/arm_atsam/suspend.c +++ b/tmk_core/common/arm_atsam/suspend.c @@ -1,6 +1,6 @@ #include "matrix.h" #include "i2c_master.h" -#include "led_matrix.h" +#include "md_rgb_matrix.h" #include "suspend.h" /** \brief Suspend idle diff --git a/tmk_core/common/chibios/bootloader.c b/tmk_core/common/chibios/bootloader.c index 7b2cf5c435..f6d016ec9d 100644 --- a/tmk_core/common/chibios/bootloader.c +++ b/tmk_core/common/chibios/bootloader.c @@ -32,7 +32,7 @@ extern uint32_t __ram0_end__; -void bootloader_jump(void) { +__attribute__((weak)) void bootloader_jump(void) { // For STM32 MCUs with dual-bank flash, and we're incapable of jumping to the bootloader. The first valid flash // bank is executed unconditionally after a reset, so it doesn't enter DFU unless BOOT0 is high. Instead, we do // it with hardware...in this case, we pull a GPIO high/low depending on the configuration, connects 3.3V to @@ -58,7 +58,7 @@ void enter_bootloader_mode_if_requested(void) {} // not needed at all, but if a extern uint32_t __ram0_end__; -void bootloader_jump(void) { +__attribute__((weak)) void bootloader_jump(void) { *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader NVIC_SystemReset(); } @@ -85,8 +85,8 @@ void enter_bootloader_mode_if_requested(void) { # if defined(BOOTLOADER_KIIBOHD) /* Kiibohd Bootloader (MCHCK and Infinity KB) */ # define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000 -const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff"; -void bootloader_jump(void) { +const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff"; +__attribute__((weak)) void bootloader_jump(void) { __builtin_memcpy((void *)VBAT, (const void *)sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic)); // request reset SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk; @@ -95,7 +95,7 @@ void bootloader_jump(void) { # else /* defined(BOOTLOADER_KIIBOHD) */ /* Default for Kinetis - expecting an ARM Teensy */ # include "wait.h" -void bootloader_jump(void) { +__attribute__((weak)) void bootloader_jump(void) { wait_ms(100); __BKPT(0); } diff --git a/tmk_core/common/eeconfig.c b/tmk_core/common/eeconfig.c index fe56c57d11..e15897552f 100644 --- a/tmk_core/common/eeconfig.c +++ b/tmk_core/common/eeconfig.c @@ -13,6 +13,10 @@ # include "eeprom_driver.h" #endif +#if defined(HAPTIC_ENABLE) +# include "haptic.h" +#endif + /** \brief eeconfig enable * * FIXME: needs doc @@ -65,6 +69,15 @@ void eeconfig_init_quantum(void) { eeprom_update_byte(EECONFIG_HANDEDNESS, 0); #endif +#if defined(HAPTIC_ENABLE) + haptic_reset(); +#else + // this is used in case haptic is disabled, but we still want sane defaults + // in the haptic configuration eeprom. All zero will trigger a haptic_reset + // when a haptic-enabled firmware is loaded onto the keyboard. + eeprom_update_dword(EECONFIG_HAPTIC, 0); +#endif + eeconfig_init_kb(); } diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c index b0394af003..8c7bdc8b55 100644 --- a/tmk_core/common/keyboard.c +++ b/tmk_core/common/keyboard.c @@ -235,6 +235,20 @@ __attribute__((weak)) bool is_keyboard_left(void) { return true; } */ __attribute__((weak)) bool should_process_keypress(void) { return is_keyboard_master(); } +/** \brief housekeeping_task_kb + * + * Override this function if you have a need to execute code for every keyboard main loop iteration. + * This is specific to keyboard-level functionality. + */ +__attribute__((weak)) void housekeeping_task_kb(void) {} + +/** \brief housekeeping_task_user + * + * Override this function if you have a need to execute code for every keyboard main loop iteration. + * This is specific to user/keymap-level functionality. + */ +__attribute__((weak)) void housekeeping_task_user(void) {} + /** \brief keyboard_init * * FIXME: needs doc @@ -291,6 +305,10 @@ void keyboard_init(void) { dip_switch_init(); #endif +#if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE) + debug_enable = true; +#endif + keyboard_post_init_kb(); /* Always keep this last */ } @@ -315,6 +333,9 @@ void keyboard_task(void) { uint8_t keys_processed = 0; #endif + housekeeping_task_kb(); + housekeeping_task_user(); + #if defined(OLED_DRIVER_ENABLE) && !defined(OLED_DISABLE_TIMEOUT) uint8_t ret = matrix_scan(); #else diff --git a/tmk_core/common/keyboard.h b/tmk_core/common/keyboard.h index 79d9490898..caa18e7e76 100644 --- a/tmk_core/common/keyboard.h +++ b/tmk_core/common/keyboard.h @@ -71,6 +71,9 @@ void keyboard_pre_init_user(void); void keyboard_post_init_kb(void); void keyboard_post_init_user(void); +void housekeeping_task_kb(void); +void housekeeping_task_user(void); + #ifdef __cplusplus } #endif diff --git a/tmk_core/common/matrix.h b/tmk_core/common/matrix.h index 78506059e6..31ec844302 100644 --- a/tmk_core/common/matrix.h +++ b/tmk_core/common/matrix.h @@ -30,16 +30,6 @@ typedef uint32_t matrix_row_t; # error "MATRIX_COLS: invalid value" #endif -#if (MATRIX_ROWS <= 8) -typedef uint8_t matrix_col_t; -#elif (MATRIX_ROWS <= 16) -typedef uint16_t matrix_col_t; -#elif (MATRIX_ROWS <= 32) -typedef uint32_t matrix_col_t; -#else -# error "MATRIX_ROWS: invalid value" -#endif - #define MATRIX_ROW_SHIFTER ((matrix_row_t)1) #define MATRIX_IS_ON(row, col) (matrix_get_row(row) && (1 << col)) diff --git a/tmk_core/common/report.h b/tmk_core/common/report.h index 1aa33c998d..610addf431 100644 --- a/tmk_core/common/report.h +++ b/tmk_core/common/report.h @@ -192,7 +192,11 @@ typedef struct { typedef struct { #if JOYSTICK_AXES_COUNT > 0 +# if JOYSTICK_AXES_RESOLUTION > 8 + int16_t axes[JOYSTICK_AXES_COUNT]; +# else int8_t axes[JOYSTICK_AXES_COUNT]; +# endif #endif #if JOYSTICK_BUTTON_COUNT > 0 diff --git a/tmk_core/protocol/arm_atsam.mk b/tmk_core/protocol/arm_atsam.mk index 8d6f724f09..5bb45d658e 100644 --- a/tmk_core/protocol/arm_atsam.mk +++ b/tmk_core/protocol/arm_atsam.mk @@ -4,9 +4,9 @@ SRC += $(ARM_ATSAM_DIR)/adc.c SRC += $(ARM_ATSAM_DIR)/clks.c SRC += $(ARM_ATSAM_DIR)/d51_util.c SRC += $(ARM_ATSAM_DIR)/i2c_master.c -ifeq ($(RGB_MATRIX_ENABLE),custom) - SRC += $(ARM_ATSAM_DIR)/led_matrix_programs.c - SRC += $(ARM_ATSAM_DIR)/led_matrix.c +ifeq ($(RGB_MATRIX_DRIVER),custom) + SRC += $(ARM_ATSAM_DIR)/md_rgb_matrix_programs.c + SRC += $(ARM_ATSAM_DIR)/md_rgb_matrix.c endif SRC += $(ARM_ATSAM_DIR)/main_arm_atsam.c SRC += $(ARM_ATSAM_DIR)/spi.c diff --git a/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h b/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h index 8cb00b872a..d126c66e7d 100644 --- a/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h +++ b/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h @@ -35,7 +35,7 @@ along with this program. If not, see . # include "main_arm_atsam.h" # ifdef RGB_MATRIX_ENABLE -# include "led_matrix.h" +# include "md_rgb_matrix.h" # include "rgb_matrix.h" # endif # include "issi3733_driver.h" diff --git a/tmk_core/protocol/arm_atsam/led_matrix.c b/tmk_core/protocol/arm_atsam/led_matrix.c deleted file mode 100644 index 69cb03a9f7..0000000000 --- a/tmk_core/protocol/arm_atsam/led_matrix.c +++ /dev/null @@ -1,472 +0,0 @@ -/* -Copyright 2018 Massdrop Inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -#include "arm_atsam_protocol.h" -#include "tmk_core/common/led.h" -#include "rgb_matrix.h" -#include -#include - -#ifdef USE_MASSDROP_CONFIGURATOR -__attribute__((weak)) led_instruction_t led_instructions[] = {{.end = 1}}; -static void led_matrix_massdrop_config_override(int i); -#endif // USE_MASSDROP_CONFIGURATOR - -void SERCOM1_0_Handler(void) { - if (SERCOM1->I2CM.INTFLAG.bit.ERROR) { - SERCOM1->I2CM.INTFLAG.reg = SERCOM_I2CM_INTENCLR_ERROR; - } -} - -void DMAC_0_Handler(void) { - if (DMAC->Channel[0].CHINTFLAG.bit.TCMPL) { - DMAC->Channel[0].CHINTFLAG.reg = DMAC_CHINTENCLR_TCMPL; - - i2c1_stop(); - - i2c_led_q_running = 0; - - i2c_led_q_run(); - - return; - } - - if (DMAC->Channel[0].CHINTFLAG.bit.TERR) { - DMAC->Channel[0].CHINTFLAG.reg = DMAC_CHINTENCLR_TERR; - } -} - -issi3733_driver_t issidrv[ISSI3733_DRIVER_COUNT]; - -issi3733_led_t led_map[ISSI3733_LED_COUNT] = ISSI3733_LED_MAP; -RGB led_buffer[ISSI3733_LED_COUNT]; - -uint8_t gcr_desired; -uint8_t gcr_actual; -uint8_t gcr_actual_last; -#ifdef USE_MASSDROP_CONFIGURATOR -uint8_t gcr_breathe; -float breathe_mult; -float pomod; -#endif - -#define ACT_GCR_NONE 0 -#define ACT_GCR_INC 1 -#define ACT_GCR_DEC 2 - -#define LED_GCR_STEP_AUTO 2 - -static uint8_t gcr_min_counter; -static uint8_t v_5v_cat_hit; - -// WARNING: Automatic GCR is in place to prevent USB shutdown and LED driver overloading -void gcr_compute(void) { - uint8_t action = ACT_GCR_NONE; - uint8_t gcr_use = gcr_desired; - -#ifdef USE_MASSDROP_CONFIGURATOR - if (led_animation_breathing) { - gcr_use = gcr_breathe; - } -#endif - - // If the 5v takes a catastrophic hit, disable the LED drivers briefly, assert auto gcr mode, min gcr and let the auto take over - if (v_5v < V5_CAT) { - I2C3733_Control_Set(0); - // CDC_print("USB: WARNING: 5V catastrophic level reached! Disabling LED drivers!\r\n"); //Blocking print is bad here! - v_5v_cat_hit = 20; //~100ms recover - gcr_actual = 0; // Minimize GCR - usb_gcr_auto = 1; // Force auto mode enabled - return; - } else if (v_5v_cat_hit > 1) { - v_5v_cat_hit--; - return; - } else if (v_5v_cat_hit == 1) { - I2C3733_Control_Set(1); - CDC_print("USB: WARNING: Re-enabling LED drivers\r\n"); - v_5v_cat_hit = 0; - return; - } - - if (usb_gcr_auto) { - if (v_5v_avg < V5_LOW) - action = ACT_GCR_DEC; - else if (v_5v_avg > V5_HIGH && gcr_actual < gcr_use) - action = ACT_GCR_INC; - else if (gcr_actual > gcr_use) - action = ACT_GCR_DEC; - } else { - if (gcr_actual < gcr_use) - action = ACT_GCR_INC; - else if (gcr_actual > gcr_use) - action = ACT_GCR_DEC; - } - - if (action == ACT_GCR_NONE) { - gcr_min_counter = 0; - } else if (action == ACT_GCR_INC) { - if (LED_GCR_STEP_AUTO > LED_GCR_MAX - gcr_actual) - gcr_actual = LED_GCR_MAX; // Obey max and prevent wrapping - else - gcr_actual += LED_GCR_STEP_AUTO; - gcr_min_counter = 0; - } else if (action == ACT_GCR_DEC) { - if (LED_GCR_STEP_AUTO > gcr_actual) // Prevent wrapping - { - gcr_actual = 0; - // At this point, power can no longer be cut from the LED drivers, so focus on cutting out extra port if active - if (usb_extra_state != USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) // If not in a wait for replug state - { - if (usb_extra_state == USB_EXTRA_STATE_ENABLED) // If extra usb is enabled - { - gcr_min_counter++; - if (gcr_min_counter > 200) // 5ms per check = 1s delay - { - USB_ExtraSetState(USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG); - usb_extra_manual = 0; // Force disable manual mode of extra port - if (usb_extra_manual) - CDC_print("USB: Disabling extra port until replug and manual mode toggle!\r\n"); - else - CDC_print("USB: Disabling extra port until replug!\r\n"); - } - } - } - } else { - // Power successfully cut back from LED drivers - gcr_actual -= LED_GCR_STEP_AUTO; - gcr_min_counter = 0; - -#ifdef USE_MASSDROP_CONFIGURATOR - // If breathe mode is active, the top end can fluctuate if the host can not supply enough current - // So set the breathe GCR to where it becomes stable - if (led_animation_breathing == 1) { - gcr_breathe = gcr_actual; - // PS: At this point, setting breathing to exhale makes a noticebly shorter cycle - // and the same would happen maybe one or two more times. Therefore I'm favoring - // powering through one full breathe and letting gcr settle completely - } -#endif - } - } -} - -void issi3733_prepare_arrays(void) { - memset(issidrv, 0, sizeof(issi3733_driver_t) * ISSI3733_DRIVER_COUNT); - - int i; - uint8_t addrs[ISSI3733_DRIVER_COUNT] = ISSI3773_DRIVER_ADDRESSES; - - for (i = 0; i < ISSI3733_DRIVER_COUNT; i++) { - issidrv[i].addr = addrs[i]; - } - - for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { - // BYTE: 1 + (SW-1)*16 + (CS-1) - led_map[i].rgb.g = issidrv[led_map[i].adr.drv - 1].pwm + 1 + ((led_map[i].adr.swg - 1) * 16 + (led_map[i].adr.cs - 1)); - led_map[i].rgb.r = issidrv[led_map[i].adr.drv - 1].pwm + 1 + ((led_map[i].adr.swr - 1) * 16 + (led_map[i].adr.cs - 1)); - led_map[i].rgb.b = issidrv[led_map[i].adr.drv - 1].pwm + 1 + ((led_map[i].adr.swb - 1) * 16 + (led_map[i].adr.cs - 1)); - - // BYTE: 1 + (SW-1)*2 + (CS-1)/8 - // BIT: (CS-1)%8 - *(issidrv[led_map[i].adr.drv - 1].onoff + 1 + (led_map[i].adr.swg - 1) * 2 + (led_map[i].adr.cs - 1) / 8) |= (1 << ((led_map[i].adr.cs - 1) % 8)); - *(issidrv[led_map[i].adr.drv - 1].onoff + 1 + (led_map[i].adr.swr - 1) * 2 + (led_map[i].adr.cs - 1) / 8) |= (1 << ((led_map[i].adr.cs - 1) % 8)); - *(issidrv[led_map[i].adr.drv - 1].onoff + 1 + (led_map[i].adr.swb - 1) * 2 + (led_map[i].adr.cs - 1) / 8) |= (1 << ((led_map[i].adr.cs - 1) % 8)); - } -} - -void led_matrix_prepare(void) { - for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { - *led_map[i].rgb.r = 0; - *led_map[i].rgb.g = 0; - *led_map[i].rgb.b = 0; - } -} - -void led_set_one(int i, uint8_t r, uint8_t g, uint8_t b) { - if (i < ISSI3733_LED_COUNT) { -#ifdef USE_MASSDROP_CONFIGURATOR - led_matrix_massdrop_config_override(i); -#else - led_buffer[i].r = r; - led_buffer[i].g = g; - led_buffer[i].b = b; -#endif - } -} - -void led_set_all(uint8_t r, uint8_t g, uint8_t b) { - for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { - led_set_one(i, r, g, b); - } -} - -void init(void) { - DBGC(DC_LED_MATRIX_INIT_BEGIN); - - issi3733_prepare_arrays(); - - led_matrix_prepare(); - - gcr_min_counter = 0; - v_5v_cat_hit = 0; - - DBGC(DC_LED_MATRIX_INIT_COMPLETE); -} - -void flush(void) { -#ifdef USE_MASSDROP_CONFIGURATOR - if (!led_enabled) { - return; - } // Prevent calculations and I2C traffic if LED drivers are not enabled -#else - if (!sr_exp_data.bit.SDB_N) { - return; - } // Prevent calculations and I2C traffic if LED drivers are not enabled -#endif - - // Wait for previous transfer to complete - while (i2c_led_q_running) { - } - - // Copy buffer to live DMA region - for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { - *led_map[i].rgb.r = led_buffer[i].r; - *led_map[i].rgb.g = led_buffer[i].g; - *led_map[i].rgb.b = led_buffer[i].b; - } - -#ifdef USE_MASSDROP_CONFIGURATOR - breathe_mult = 1; - - if (led_animation_breathing) { - //+60us 119 LED - led_animation_breathe_cur += BREATHE_STEP * breathe_dir; - - if (led_animation_breathe_cur >= BREATHE_MAX_STEP) - breathe_dir = -1; - else if (led_animation_breathe_cur <= BREATHE_MIN_STEP) - breathe_dir = 1; - - // Brightness curve created for 256 steps, 0 - ~98% - breathe_mult = 0.000015 * led_animation_breathe_cur * led_animation_breathe_cur; - if (breathe_mult > 1) - breathe_mult = 1; - else if (breathe_mult < 0) - breathe_mult = 0; - } - - // This should only be performed once per frame - pomod = (float)((g_rgb_timer / 10) % (uint32_t)(1000.0f / led_animation_speed)) / 10.0f * led_animation_speed; - pomod *= 100.0f; - pomod = (uint32_t)pomod % 10000; - pomod /= 100.0f; - -#endif // USE_MASSDROP_CONFIGURATOR - - uint8_t drvid; - - // NOTE: GCR does not need to be timed with LED processing, but there is really no harm - if (gcr_actual != gcr_actual_last) { - for (drvid = 0; drvid < ISSI3733_DRIVER_COUNT; drvid++) I2C_LED_Q_GCR(drvid); // Queue data - gcr_actual_last = gcr_actual; - } - - for (drvid = 0; drvid < ISSI3733_DRIVER_COUNT; drvid++) I2C_LED_Q_PWM(drvid); // Queue data - - i2c_led_q_run(); -} - -void led_matrix_indicators(void) { - uint8_t kbled = keyboard_leds(); - if (kbled && rgb_matrix_config.enable) { - for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { - if ( -#if USB_LED_NUM_LOCK_SCANCODE != 255 - (led_map[i].scan == USB_LED_NUM_LOCK_SCANCODE && (kbled & (1 << USB_LED_NUM_LOCK))) || -#endif // NUM LOCK -#if USB_LED_CAPS_LOCK_SCANCODE != 255 - (led_map[i].scan == USB_LED_CAPS_LOCK_SCANCODE && (kbled & (1 << USB_LED_CAPS_LOCK))) || -#endif // CAPS LOCK -#if USB_LED_SCROLL_LOCK_SCANCODE != 255 - (led_map[i].scan == USB_LED_SCROLL_LOCK_SCANCODE && (kbled & (1 << USB_LED_SCROLL_LOCK))) || -#endif // SCROLL LOCK -#if USB_LED_COMPOSE_SCANCODE != 255 - (led_map[i].scan == USB_LED_COMPOSE_SCANCODE && (kbled & (1 << USB_LED_COMPOSE))) || -#endif // COMPOSE -#if USB_LED_KANA_SCANCODE != 255 - (led_map[i].scan == USB_LED_KANA_SCANCODE && (kbled & (1 << USB_LED_KANA))) || -#endif // KANA - (0)) { - if (rgb_matrix_get_flags() & LED_FLAG_INDICATOR) { - led_buffer[i].r = 255 - led_buffer[i].r; - led_buffer[i].g = 255 - led_buffer[i].g; - led_buffer[i].b = 255 - led_buffer[i].b; - } - } - } - } -} - -const rgb_matrix_driver_t rgb_matrix_driver = {.init = init, .flush = flush, .set_color = led_set_one, .set_color_all = led_set_all}; - -/*============================================================================== -= Legacy Lighting Support = -==============================================================================*/ - -#ifdef USE_MASSDROP_CONFIGURATOR -// Ported from Massdrop QMK GitHub Repo - -// TODO?: wire these up to keymap.c -uint8_t led_animation_orientation = 0; -uint8_t led_animation_direction = 0; -uint8_t led_animation_breathing = 0; -uint8_t led_animation_id = 0; -float led_animation_speed = 4.0f; -uint8_t led_lighting_mode = LED_MODE_NORMAL; -uint8_t led_enabled = 1; -uint8_t led_animation_breathe_cur = BREATHE_MIN_STEP; -uint8_t breathe_dir = 1; - -static void led_run_pattern(led_setup_t* f, float* ro, float* go, float* bo, float pos) { - float po; - - while (f->end != 1) { - po = pos; // Reset po for new frame - - // Add in any moving effects - if ((!led_animation_direction && f->ef & EF_SCR_R) || (led_animation_direction && (f->ef & EF_SCR_L))) { - po -= pomod; - - if (po > 100) - po -= 100; - else if (po < 0) - po += 100; - } else if ((!led_animation_direction && f->ef & EF_SCR_L) || (led_animation_direction && (f->ef & EF_SCR_R))) { - po += pomod; - - if (po > 100) - po -= 100; - else if (po < 0) - po += 100; - } - - // Check if LED's po is in current frame - if (po < f->hs) { - f++; - continue; - } - if (po > f->he) { - f++; - continue; - } - // note: < 0 or > 100 continue - - // Calculate the po within the start-stop percentage for color blending - po = (po - f->hs) / (f->he - f->hs); - - // Add in any color effects - if (f->ef & EF_OVER) { - *ro = (po * (f->re - f->rs)) + f->rs; // + 0.5; - *go = (po * (f->ge - f->gs)) + f->gs; // + 0.5; - *bo = (po * (f->be - f->bs)) + f->bs; // + 0.5; - } else if (f->ef & EF_SUBTRACT) { - *ro -= (po * (f->re - f->rs)) + f->rs; // + 0.5; - *go -= (po * (f->ge - f->gs)) + f->gs; // + 0.5; - *bo -= (po * (f->be - f->bs)) + f->bs; // + 0.5; - } else { - *ro += (po * (f->re - f->rs)) + f->rs; // + 0.5; - *go += (po * (f->ge - f->gs)) + f->gs; // + 0.5; - *bo += (po * (f->be - f->bs)) + f->bs; // + 0.5; - } - - f++; - } -} - -static void led_matrix_massdrop_config_override(int i) { - float ro = 0; - float go = 0; - float bo = 0; - - float po = (led_animation_orientation) ? (float)g_led_config.point[i].y / 64.f * 100 : (float)g_led_config.point[i].x / 224.f * 100; - - uint8_t highest_active_layer = biton32(layer_state); - - if (led_lighting_mode == LED_MODE_KEYS_ONLY && HAS_FLAGS(g_led_config.flags[i], LED_FLAG_UNDERGLOW)) { - // Do not act on this LED - } else if (led_lighting_mode == LED_MODE_NON_KEYS_ONLY && !HAS_FLAGS(g_led_config.flags[i], LED_FLAG_UNDERGLOW)) { - // Do not act on this LED - } else if (led_lighting_mode == LED_MODE_INDICATORS_ONLY) { - // Do not act on this LED (Only show indicators) - } else { - led_instruction_t* led_cur_instruction = led_instructions; - while (!led_cur_instruction->end) { - // Check if this applies to current layer - if ((led_cur_instruction->flags & LED_FLAG_MATCH_LAYER) && (led_cur_instruction->layer != highest_active_layer)) { - goto next_iter; - } - - // Check if this applies to current index - if (led_cur_instruction->flags & LED_FLAG_MATCH_ID) { - uint8_t modid = i / 32; // Calculate which id# contains the led bit - uint32_t modidbit = 1 << (i % 32); // Calculate the bit within the id# - uint32_t* bitfield = &led_cur_instruction->id0 + modid; // Add modid as offset to id0 address. *bitfield is now idX of the led id - if (~(*bitfield) & modidbit) { // Check if led bit is not set in idX - goto next_iter; - } - } - - if (led_cur_instruction->flags & LED_FLAG_USE_RGB) { - ro = led_cur_instruction->r; - go = led_cur_instruction->g; - bo = led_cur_instruction->b; - } else if (led_cur_instruction->flags & LED_FLAG_USE_PATTERN) { - led_run_pattern(led_setups[led_cur_instruction->pattern_id], &ro, &go, &bo, po); - } else if (led_cur_instruction->flags & LED_FLAG_USE_ROTATE_PATTERN) { - led_run_pattern(led_setups[led_animation_id], &ro, &go, &bo, po); - } - - next_iter: - led_cur_instruction++; - } - - if (ro > 255) - ro = 255; - else if (ro < 0) - ro = 0; - if (go > 255) - go = 255; - else if (go < 0) - go = 0; - if (bo > 255) - bo = 255; - else if (bo < 0) - bo = 0; - - if (led_animation_breathing) { - ro *= breathe_mult; - go *= breathe_mult; - bo *= breathe_mult; - } - } - - led_buffer[i].r = (uint8_t)ro; - led_buffer[i].g = (uint8_t)go; - led_buffer[i].b = (uint8_t)bo; -} - -#endif // USE_MASSDROP_CONFIGURATOR diff --git a/tmk_core/protocol/arm_atsam/led_matrix.h b/tmk_core/protocol/arm_atsam/led_matrix.h deleted file mode 100644 index 8eaa5623bd..0000000000 --- a/tmk_core/protocol/arm_atsam/led_matrix.h +++ /dev/null @@ -1,158 +0,0 @@ -/* -Copyright 2018 Massdrop Inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -#ifndef _LED_MATRIX_H_ -#define _LED_MATRIX_H_ - -#include "quantum.h" - -// From keyboard -#include "config_led.h" - -// CS1-CS16 Current Source "Col" -#define ISSI3733_CS_COUNT 16 - -// SW1-SW12 Switch "Row" -#define ISSI3733_SW_COUNT 12 - -#define ISSI3733_LED_RGB_COUNT ISSI3733_CS_COUNT *ISSI3733_SW_COUNT -#define ISSI3733_PG0_BYTES ISSI3733_LED_RGB_COUNT / 8 + 1 //+1 for first byte being memory start offset for I2C transfer -#define ISSI3733_PG1_BYTES ISSI3733_LED_RGB_COUNT + 1 //+1 for first byte being memory start offset for I2C transfer -#define ISSI3733_PG2_BYTES ISSI3733_LED_RGB_COUNT + 1 //+1 for first byte being memory start offset for I2C transfer -#define ISSI3733_PG3_BYTES 18 + 1 //+1 for first byte being memory start offset for I2C transfer - -#define ISSI3733_PG_ONOFF_BYTES ISSI3733_PG0_BYTES -#define ISSI3733_PG_OR_BYTES ISSI3733_PG0_BYTES -#define ISSI3733_PG_SR_BYTES ISSI3733_PG0_BYTES -#define ISSI3733_PG_PWM_BYTES ISSI3733_PG1_BYTES -#define ISSI3733_PG_ABM_BYTES ISSI3733_PG2_BYTES -#define ISSI3733_PG_FN_BYTES ISSI3733_PG3_BYTES - -typedef struct issi3733_driver_s { - uint8_t addr; // Address of the driver according to wiring "ISSI3733: Table 1 Slave Address" - uint8_t onoff[ISSI3733_PG_ONOFF_BYTES]; // PG0 - LED Control Register - LED On/Off Register - uint8_t open[ISSI3733_PG_OR_BYTES]; // PG0 - LED Control Register - LED Open Register - uint8_t shrt[ISSI3733_PG_SR_BYTES]; // PG0 - LED Control Register - LED Short Register - uint8_t pwm[ISSI3733_PG_PWM_BYTES]; // PG1 - PWM Register - uint8_t abm[ISSI3733_PG_ABM_BYTES]; // PG2 - Auto Breath Mode Register - uint8_t conf[ISSI3733_PG_FN_BYTES]; // PG3 - Function Register -} issi3733_driver_t; - -typedef struct issi3733_rgb_s { - uint8_t *r; // Direct access into PWM data - uint8_t *g; // Direct access into PWM data - uint8_t *b; // Direct access into PWM data -} issi3733_rgb_t; - -typedef struct issi3733_rgb_adr_s { - uint8_t drv; // Driver from given list - uint8_t cs; // CS - uint8_t swr; // SW Red - uint8_t swg; // SW Green - uint8_t swb; // SW Blue -} issi3733_rgb_adr_t; - -typedef struct issi3733_led_s { - uint8_t id; // According to PCB ref - issi3733_rgb_t rgb; // PWM settings of R G B - issi3733_rgb_adr_t adr; // Hardware addresses - float x; // Physical position X - float y; // Physical position Y - float px; // Physical position X in percent - float py; // Physical position Y in percent - uint8_t scan; // Key scan code from wiring (set 0xFF if no key) -} issi3733_led_t; - -extern issi3733_driver_t issidrv[ISSI3733_DRIVER_COUNT]; - -extern uint8_t gcr_desired; -extern uint8_t gcr_breathe; -extern uint8_t gcr_actual; -extern uint8_t gcr_actual_last; - -void gcr_compute(void); - -void led_matrix_indicators(void); - -/*------------------------- Legacy Lighting Support ------------------------*/ - -#ifdef USE_MASSDROP_CONFIGURATOR - -# define EF_NONE 0x00000000 // No effect -# define EF_OVER 0x00000001 // Overwrite any previous color information with new -# define EF_SCR_L 0x00000002 // Scroll left -# define EF_SCR_R 0x00000004 // Scroll right -# define EF_SUBTRACT 0x00000008 // Subtract color values - -typedef struct led_setup_s { - float hs; // Band begin at percent - float he; // Band end at percent - uint8_t rs; // Red start value - uint8_t re; // Red end value - uint8_t gs; // Green start value - uint8_t ge; // Green end value - uint8_t bs; // Blue start value - uint8_t be; // Blue end value - uint32_t ef; // Animation and color effects - uint8_t end; // Set to signal end of the setup -} led_setup_t; - -extern const uint8_t led_setups_count; -extern void * led_setups[]; - -// LED Extra Instructions -# define LED_FLAG_NULL 0x00 // Matching and coloring not used (default) -# define LED_FLAG_MATCH_ID 0x01 // Match on the ID of the LED (set id#'s to desired bit pattern, first LED is id 1) -# define LED_FLAG_MATCH_LAYER 0x02 // Match on the current active layer (set layer to desired match layer) -# define LED_FLAG_USE_RGB 0x10 // Use a specific RGB value (set r, g, b to desired output color values) -# define LED_FLAG_USE_PATTERN 0x20 // Use a specific pattern ID (set pattern_id to desired output pattern) -# define LED_FLAG_USE_ROTATE_PATTERN 0x40 // Use pattern the user has cycled to manually - -typedef struct led_instruction_s { - uint16_t flags; // Bitfield for LED instructions - uint32_t id0; // Bitwise id, IDs 0-31 - uint32_t id1; // Bitwise id, IDs 32-63 - uint32_t id2; // Bitwise id, IDs 64-95 - uint32_t id3; // Bitwise id, IDs 96-127 - uint8_t layer; - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t pattern_id; - uint8_t end; -} led_instruction_t; - -extern led_instruction_t led_instructions[]; - -extern uint8_t led_animation_breathing; -extern uint8_t led_animation_id; -extern float led_animation_speed; -extern uint8_t led_lighting_mode; -extern uint8_t led_enabled; -extern uint8_t led_animation_breathe_cur; -extern uint8_t led_animation_direction; -extern uint8_t breathe_dir; - -# define LED_MODE_NORMAL 0 // Must be 0 -# define LED_MODE_KEYS_ONLY 1 -# define LED_MODE_NON_KEYS_ONLY 2 -# define LED_MODE_INDICATORS_ONLY 3 -# define LED_MODE_MAX_INDEX LED_MODE_INDICATORS_ONLY // Must be highest value - -#endif // USE_MASSDROP_CONFIGURATOR - -#endif //_LED_MATRIX_H_ diff --git a/tmk_core/protocol/arm_atsam/led_matrix_programs.c b/tmk_core/protocol/arm_atsam/led_matrix_programs.c deleted file mode 100644 index 360102ba84..0000000000 --- a/tmk_core/protocol/arm_atsam/led_matrix_programs.c +++ /dev/null @@ -1,99 +0,0 @@ -/* -Copyright 2018 Massdrop Inc. - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ - -#ifdef USE_MASSDROP_CONFIGURATOR - -# include "led_matrix.h" - -// Teal <-> Salmon -led_setup_t leds_teal_salmon[] = { - {.hs = 0, .he = 33, .rs = 24, .re = 24, .gs = 215, .ge = 215, .bs = 204, .be = 204, .ef = EF_NONE}, - {.hs = 33, .he = 66, .rs = 24, .re = 255, .gs = 215, .ge = 114, .bs = 204, .be = 118, .ef = EF_NONE}, - {.hs = 66, .he = 100, .rs = 255, .re = 255, .gs = 114, .ge = 114, .bs = 118, .be = 118, .ef = EF_NONE}, - {.end = 1}, -}; - -// Yellow -led_setup_t leds_yellow[] = { - {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_NONE}, - {.end = 1}, -}; - -// Off -led_setup_t leds_off[] = { - {.hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_NONE}, - {.end = 1}, -}; - -// Red -led_setup_t leds_red[] = { - {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_NONE}, - {.end = 1}, -}; - -// Green -led_setup_t leds_green[] = { - {.hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_NONE}, - {.end = 1}, -}; - -// Blue -led_setup_t leds_blue[] = { - {.hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_NONE}, - {.end = 1}, -}; - -// White -led_setup_t leds_white[] = { - {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 255, .be = 255, .ef = EF_NONE}, - {.end = 1}, -}; - -// White with moving red stripe -led_setup_t leds_white_with_red_stripe[] = { - {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 255, .be = 255, .ef = EF_NONE}, - {.hs = 0, .he = 15, .rs = 0, .re = 0, .gs = 0, .ge = 255, .bs = 0, .be = 255, .ef = EF_SCR_R | EF_SUBTRACT}, - {.hs = 15, .he = 30, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 0, .ef = EF_SCR_R | EF_SUBTRACT}, - {.end = 1}, -}; - -// Black with moving red stripe -led_setup_t leds_black_with_red_stripe[] = { - {.hs = 0, .he = 15, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_SCR_R}, - {.hs = 15, .he = 30, .rs = 255, .re = 0, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_SCR_R}, - {.end = 1}, -}; - -// Rainbow no scrolling -led_setup_t leds_rainbow_ns[] = { - {.hs = 0, .he = 16.67, .rs = 255, .re = 255, .gs = 0, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER}, {.hs = 16.67, .he = 33.33, .rs = 255, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER}, {.hs = 33.33, .he = 50, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 255, .ef = EF_OVER}, {.hs = 50, .he = 66.67, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER}, {.hs = 66.67, .he = 83.33, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER}, {.hs = 83.33, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 0, .ef = EF_OVER}, {.end = 1}, -}; - -// Rainbow scrolling -led_setup_t leds_rainbow_s[] = { - {.hs = 0, .he = 16.67, .rs = 255, .re = 255, .gs = 0, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER | EF_SCR_R}, {.hs = 16.67, .he = 33.33, .rs = 255, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER | EF_SCR_R}, {.hs = 33.33, .he = 50, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 255, .ef = EF_OVER | EF_SCR_R}, {.hs = 50, .he = 66.67, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER | EF_SCR_R}, {.hs = 66.67, .he = 83.33, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER | EF_SCR_R}, {.hs = 83.33, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 0, .ef = EF_OVER | EF_SCR_R}, {.end = 1}, -}; - -// Add new LED animations here using one from above as example -// The last entry must be { .end = 1 } -// Add the new animation name to the list below following its format - -void *led_setups[] = {leds_rainbow_s, leds_rainbow_ns, leds_teal_salmon, leds_yellow, leds_red, leds_green, leds_blue, leds_white, leds_white_with_red_stripe, leds_black_with_red_stripe, leds_off}; - -const uint8_t led_setups_count = sizeof(led_setups) / sizeof(led_setups[0]); - -#endif diff --git a/tmk_core/protocol/arm_atsam/main_arm_atsam.c b/tmk_core/protocol/arm_atsam/main_arm_atsam.c index e4e79d3510..e10be52fb8 100644 --- a/tmk_core/protocol/arm_atsam/main_arm_atsam.c +++ b/tmk_core/protocol/arm_atsam/main_arm_atsam.c @@ -305,6 +305,10 @@ int main(void) { // dprintf("5v=%u 5vu=%u dlow=%u dhi=%u gca=%u gcd=%u\r\n", v_5v, v_5v_avg, v_5v_avg - V5_LOW, v_5v_avg - V5_HIGH, gcr_actual, gcr_desired); } #endif // CONSOLE_ENABLE + + // Run housekeeping + housekeeping_task_kb(); + housekeeping_task_user(); } return 1; diff --git a/tmk_core/protocol/arm_atsam/md_rgb_matrix.c b/tmk_core/protocol/arm_atsam/md_rgb_matrix.c new file mode 100644 index 0000000000..439aaf8b38 --- /dev/null +++ b/tmk_core/protocol/arm_atsam/md_rgb_matrix.c @@ -0,0 +1,472 @@ +/* +Copyright 2018 Massdrop Inc. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include "arm_atsam_protocol.h" +#include "tmk_core/common/led.h" +#include "rgb_matrix.h" +#include +#include + +#ifdef USE_MASSDROP_CONFIGURATOR +__attribute__((weak)) led_instruction_t led_instructions[] = {{.end = 1}}; +static void md_rgb_matrix_config_override(int i); +#endif // USE_MASSDROP_CONFIGURATOR + +void SERCOM1_0_Handler(void) { + if (SERCOM1->I2CM.INTFLAG.bit.ERROR) { + SERCOM1->I2CM.INTFLAG.reg = SERCOM_I2CM_INTENCLR_ERROR; + } +} + +void DMAC_0_Handler(void) { + if (DMAC->Channel[0].CHINTFLAG.bit.TCMPL) { + DMAC->Channel[0].CHINTFLAG.reg = DMAC_CHINTENCLR_TCMPL; + + i2c1_stop(); + + i2c_led_q_running = 0; + + i2c_led_q_run(); + + return; + } + + if (DMAC->Channel[0].CHINTFLAG.bit.TERR) { + DMAC->Channel[0].CHINTFLAG.reg = DMAC_CHINTENCLR_TERR; + } +} + +issi3733_driver_t issidrv[ISSI3733_DRIVER_COUNT]; + +issi3733_led_t led_map[ISSI3733_LED_COUNT] = ISSI3733_LED_MAP; +RGB led_buffer[ISSI3733_LED_COUNT]; + +uint8_t gcr_desired; +uint8_t gcr_actual; +uint8_t gcr_actual_last; +#ifdef USE_MASSDROP_CONFIGURATOR +uint8_t gcr_breathe; +float breathe_mult; +float pomod; +#endif + +#define ACT_GCR_NONE 0 +#define ACT_GCR_INC 1 +#define ACT_GCR_DEC 2 + +#define LED_GCR_STEP_AUTO 2 + +static uint8_t gcr_min_counter; +static uint8_t v_5v_cat_hit; + +// WARNING: Automatic GCR is in place to prevent USB shutdown and LED driver overloading +void gcr_compute(void) { + uint8_t action = ACT_GCR_NONE; + uint8_t gcr_use = gcr_desired; + +#ifdef USE_MASSDROP_CONFIGURATOR + if (led_animation_breathing) { + gcr_use = gcr_breathe; + } +#endif + + // If the 5v takes a catastrophic hit, disable the LED drivers briefly, assert auto gcr mode, min gcr and let the auto take over + if (v_5v < V5_CAT) { + I2C3733_Control_Set(0); + // CDC_print("USB: WARNING: 5V catastrophic level reached! Disabling LED drivers!\r\n"); //Blocking print is bad here! + v_5v_cat_hit = 20; //~100ms recover + gcr_actual = 0; // Minimize GCR + usb_gcr_auto = 1; // Force auto mode enabled + return; + } else if (v_5v_cat_hit > 1) { + v_5v_cat_hit--; + return; + } else if (v_5v_cat_hit == 1) { + I2C3733_Control_Set(1); + CDC_print("USB: WARNING: Re-enabling LED drivers\r\n"); + v_5v_cat_hit = 0; + return; + } + + if (usb_gcr_auto) { + if (v_5v_avg < V5_LOW) + action = ACT_GCR_DEC; + else if (v_5v_avg > V5_HIGH && gcr_actual < gcr_use) + action = ACT_GCR_INC; + else if (gcr_actual > gcr_use) + action = ACT_GCR_DEC; + } else { + if (gcr_actual < gcr_use) + action = ACT_GCR_INC; + else if (gcr_actual > gcr_use) + action = ACT_GCR_DEC; + } + + if (action == ACT_GCR_NONE) { + gcr_min_counter = 0; + } else if (action == ACT_GCR_INC) { + if (LED_GCR_STEP_AUTO > LED_GCR_MAX - gcr_actual) + gcr_actual = LED_GCR_MAX; // Obey max and prevent wrapping + else + gcr_actual += LED_GCR_STEP_AUTO; + gcr_min_counter = 0; + } else if (action == ACT_GCR_DEC) { + if (LED_GCR_STEP_AUTO > gcr_actual) // Prevent wrapping + { + gcr_actual = 0; + // At this point, power can no longer be cut from the LED drivers, so focus on cutting out extra port if active + if (usb_extra_state != USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG) // If not in a wait for replug state + { + if (usb_extra_state == USB_EXTRA_STATE_ENABLED) // If extra usb is enabled + { + gcr_min_counter++; + if (gcr_min_counter > 200) // 5ms per check = 1s delay + { + USB_ExtraSetState(USB_EXTRA_STATE_DISABLED_UNTIL_REPLUG); + usb_extra_manual = 0; // Force disable manual mode of extra port + if (usb_extra_manual) + CDC_print("USB: Disabling extra port until replug and manual mode toggle!\r\n"); + else + CDC_print("USB: Disabling extra port until replug!\r\n"); + } + } + } + } else { + // Power successfully cut back from LED drivers + gcr_actual -= LED_GCR_STEP_AUTO; + gcr_min_counter = 0; + +#ifdef USE_MASSDROP_CONFIGURATOR + // If breathe mode is active, the top end can fluctuate if the host can not supply enough current + // So set the breathe GCR to where it becomes stable + if (led_animation_breathing == 1) { + gcr_breathe = gcr_actual; + // PS: At this point, setting breathing to exhale makes a noticebly shorter cycle + // and the same would happen maybe one or two more times. Therefore I'm favoring + // powering through one full breathe and letting gcr settle completely + } +#endif + } + } +} + +void issi3733_prepare_arrays(void) { + memset(issidrv, 0, sizeof(issi3733_driver_t) * ISSI3733_DRIVER_COUNT); + + int i; + uint8_t addrs[ISSI3733_DRIVER_COUNT] = ISSI3773_DRIVER_ADDRESSES; + + for (i = 0; i < ISSI3733_DRIVER_COUNT; i++) { + issidrv[i].addr = addrs[i]; + } + + for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { + // BYTE: 1 + (SW-1)*16 + (CS-1) + led_map[i].rgb.g = issidrv[led_map[i].adr.drv - 1].pwm + 1 + ((led_map[i].adr.swg - 1) * 16 + (led_map[i].adr.cs - 1)); + led_map[i].rgb.r = issidrv[led_map[i].adr.drv - 1].pwm + 1 + ((led_map[i].adr.swr - 1) * 16 + (led_map[i].adr.cs - 1)); + led_map[i].rgb.b = issidrv[led_map[i].adr.drv - 1].pwm + 1 + ((led_map[i].adr.swb - 1) * 16 + (led_map[i].adr.cs - 1)); + + // BYTE: 1 + (SW-1)*2 + (CS-1)/8 + // BIT: (CS-1)%8 + *(issidrv[led_map[i].adr.drv - 1].onoff + 1 + (led_map[i].adr.swg - 1) * 2 + (led_map[i].adr.cs - 1) / 8) |= (1 << ((led_map[i].adr.cs - 1) % 8)); + *(issidrv[led_map[i].adr.drv - 1].onoff + 1 + (led_map[i].adr.swr - 1) * 2 + (led_map[i].adr.cs - 1) / 8) |= (1 << ((led_map[i].adr.cs - 1) % 8)); + *(issidrv[led_map[i].adr.drv - 1].onoff + 1 + (led_map[i].adr.swb - 1) * 2 + (led_map[i].adr.cs - 1) / 8) |= (1 << ((led_map[i].adr.cs - 1) % 8)); + } +} + +void md_rgb_matrix_prepare(void) { + for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { + *led_map[i].rgb.r = 0; + *led_map[i].rgb.g = 0; + *led_map[i].rgb.b = 0; + } +} + +void led_set_one(int i, uint8_t r, uint8_t g, uint8_t b) { + if (i < ISSI3733_LED_COUNT) { +#ifdef USE_MASSDROP_CONFIGURATOR + md_rgb_matrix_config_override(i); +#else + led_buffer[i].r = r; + led_buffer[i].g = g; + led_buffer[i].b = b; +#endif + } +} + +void led_set_all(uint8_t r, uint8_t g, uint8_t b) { + for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { + led_set_one(i, r, g, b); + } +} + +void init(void) { + DBGC(DC_LED_MATRIX_INIT_BEGIN); + + issi3733_prepare_arrays(); + + md_rgb_matrix_prepare(); + + gcr_min_counter = 0; + v_5v_cat_hit = 0; + + DBGC(DC_LED_MATRIX_INIT_COMPLETE); +} + +void flush(void) { +#ifdef USE_MASSDROP_CONFIGURATOR + if (!led_enabled) { + return; + } // Prevent calculations and I2C traffic if LED drivers are not enabled +#else + if (!sr_exp_data.bit.SDB_N) { + return; + } // Prevent calculations and I2C traffic if LED drivers are not enabled +#endif + + // Wait for previous transfer to complete + while (i2c_led_q_running) { + } + + // Copy buffer to live DMA region + for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { + *led_map[i].rgb.r = led_buffer[i].r; + *led_map[i].rgb.g = led_buffer[i].g; + *led_map[i].rgb.b = led_buffer[i].b; + } + +#ifdef USE_MASSDROP_CONFIGURATOR + breathe_mult = 1; + + if (led_animation_breathing) { + //+60us 119 LED + led_animation_breathe_cur += BREATHE_STEP * breathe_dir; + + if (led_animation_breathe_cur >= BREATHE_MAX_STEP) + breathe_dir = -1; + else if (led_animation_breathe_cur <= BREATHE_MIN_STEP) + breathe_dir = 1; + + // Brightness curve created for 256 steps, 0 - ~98% + breathe_mult = 0.000015 * led_animation_breathe_cur * led_animation_breathe_cur; + if (breathe_mult > 1) + breathe_mult = 1; + else if (breathe_mult < 0) + breathe_mult = 0; + } + + // This should only be performed once per frame + pomod = (float)((g_rgb_timer / 10) % (uint32_t)(1000.0f / led_animation_speed)) / 10.0f * led_animation_speed; + pomod *= 100.0f; + pomod = (uint32_t)pomod % 10000; + pomod /= 100.0f; + +#endif // USE_MASSDROP_CONFIGURATOR + + uint8_t drvid; + + // NOTE: GCR does not need to be timed with LED processing, but there is really no harm + if (gcr_actual != gcr_actual_last) { + for (drvid = 0; drvid < ISSI3733_DRIVER_COUNT; drvid++) I2C_LED_Q_GCR(drvid); // Queue data + gcr_actual_last = gcr_actual; + } + + for (drvid = 0; drvid < ISSI3733_DRIVER_COUNT; drvid++) I2C_LED_Q_PWM(drvid); // Queue data + + i2c_led_q_run(); +} + +void md_rgb_matrix_indicators(void) { + uint8_t kbled = keyboard_leds(); + if (kbled && rgb_matrix_config.enable) { + for (uint8_t i = 0; i < ISSI3733_LED_COUNT; i++) { + if ( +#if USB_LED_NUM_LOCK_SCANCODE != 255 + (led_map[i].scan == USB_LED_NUM_LOCK_SCANCODE && (kbled & (1 << USB_LED_NUM_LOCK))) || +#endif // NUM LOCK +#if USB_LED_CAPS_LOCK_SCANCODE != 255 + (led_map[i].scan == USB_LED_CAPS_LOCK_SCANCODE && (kbled & (1 << USB_LED_CAPS_LOCK))) || +#endif // CAPS LOCK +#if USB_LED_SCROLL_LOCK_SCANCODE != 255 + (led_map[i].scan == USB_LED_SCROLL_LOCK_SCANCODE && (kbled & (1 << USB_LED_SCROLL_LOCK))) || +#endif // SCROLL LOCK +#if USB_LED_COMPOSE_SCANCODE != 255 + (led_map[i].scan == USB_LED_COMPOSE_SCANCODE && (kbled & (1 << USB_LED_COMPOSE))) || +#endif // COMPOSE +#if USB_LED_KANA_SCANCODE != 255 + (led_map[i].scan == USB_LED_KANA_SCANCODE && (kbled & (1 << USB_LED_KANA))) || +#endif // KANA + (0)) { + if (rgb_matrix_get_flags() & LED_FLAG_INDICATOR) { + led_buffer[i].r = 255 - led_buffer[i].r; + led_buffer[i].g = 255 - led_buffer[i].g; + led_buffer[i].b = 255 - led_buffer[i].b; + } + } + } + } +} + +const rgb_matrix_driver_t rgb_matrix_driver = {.init = init, .flush = flush, .set_color = led_set_one, .set_color_all = led_set_all}; + +/*============================================================================== += Legacy Lighting Support = +==============================================================================*/ + +#ifdef USE_MASSDROP_CONFIGURATOR +// Ported from Massdrop QMK GitHub Repo + +// TODO?: wire these up to keymap.c +uint8_t led_animation_orientation = 0; +uint8_t led_animation_direction = 0; +uint8_t led_animation_breathing = 0; +uint8_t led_animation_id = 0; +float led_animation_speed = 4.0f; +uint8_t led_lighting_mode = LED_MODE_NORMAL; +uint8_t led_enabled = 1; +uint8_t led_animation_breathe_cur = BREATHE_MIN_STEP; +uint8_t breathe_dir = 1; + +static void led_run_pattern(led_setup_t* f, float* ro, float* go, float* bo, float pos) { + float po; + + while (f->end != 1) { + po = pos; // Reset po for new frame + + // Add in any moving effects + if ((!led_animation_direction && f->ef & EF_SCR_R) || (led_animation_direction && (f->ef & EF_SCR_L))) { + po -= pomod; + + if (po > 100) + po -= 100; + else if (po < 0) + po += 100; + } else if ((!led_animation_direction && f->ef & EF_SCR_L) || (led_animation_direction && (f->ef & EF_SCR_R))) { + po += pomod; + + if (po > 100) + po -= 100; + else if (po < 0) + po += 100; + } + + // Check if LED's po is in current frame + if (po < f->hs) { + f++; + continue; + } + if (po > f->he) { + f++; + continue; + } + // note: < 0 or > 100 continue + + // Calculate the po within the start-stop percentage for color blending + po = (po - f->hs) / (f->he - f->hs); + + // Add in any color effects + if (f->ef & EF_OVER) { + *ro = (po * (f->re - f->rs)) + f->rs; // + 0.5; + *go = (po * (f->ge - f->gs)) + f->gs; // + 0.5; + *bo = (po * (f->be - f->bs)) + f->bs; // + 0.5; + } else if (f->ef & EF_SUBTRACT) { + *ro -= (po * (f->re - f->rs)) + f->rs; // + 0.5; + *go -= (po * (f->ge - f->gs)) + f->gs; // + 0.5; + *bo -= (po * (f->be - f->bs)) + f->bs; // + 0.5; + } else { + *ro += (po * (f->re - f->rs)) + f->rs; // + 0.5; + *go += (po * (f->ge - f->gs)) + f->gs; // + 0.5; + *bo += (po * (f->be - f->bs)) + f->bs; // + 0.5; + } + + f++; + } +} + +static void md_rgb_matrix_config_override(int i) { + float ro = 0; + float go = 0; + float bo = 0; + + float po = (led_animation_orientation) ? (float)g_led_config.point[i].y / 64.f * 100 : (float)g_led_config.point[i].x / 224.f * 100; + + uint8_t highest_active_layer = biton32(layer_state); + + if (led_lighting_mode == LED_MODE_KEYS_ONLY && HAS_FLAGS(g_led_config.flags[i], LED_FLAG_UNDERGLOW)) { + // Do not act on this LED + } else if (led_lighting_mode == LED_MODE_NON_KEYS_ONLY && !HAS_FLAGS(g_led_config.flags[i], LED_FLAG_UNDERGLOW)) { + // Do not act on this LED + } else if (led_lighting_mode == LED_MODE_INDICATORS_ONLY) { + // Do not act on this LED (Only show indicators) + } else { + led_instruction_t* led_cur_instruction = led_instructions; + while (!led_cur_instruction->end) { + // Check if this applies to current layer + if ((led_cur_instruction->flags & LED_FLAG_MATCH_LAYER) && (led_cur_instruction->layer != highest_active_layer)) { + goto next_iter; + } + + // Check if this applies to current index + if (led_cur_instruction->flags & LED_FLAG_MATCH_ID) { + uint8_t modid = i / 32; // Calculate which id# contains the led bit + uint32_t modidbit = 1 << (i % 32); // Calculate the bit within the id# + uint32_t* bitfield = &led_cur_instruction->id0 + modid; // Add modid as offset to id0 address. *bitfield is now idX of the led id + if (~(*bitfield) & modidbit) { // Check if led bit is not set in idX + goto next_iter; + } + } + + if (led_cur_instruction->flags & LED_FLAG_USE_RGB) { + ro = led_cur_instruction->r; + go = led_cur_instruction->g; + bo = led_cur_instruction->b; + } else if (led_cur_instruction->flags & LED_FLAG_USE_PATTERN) { + led_run_pattern(led_setups[led_cur_instruction->pattern_id], &ro, &go, &bo, po); + } else if (led_cur_instruction->flags & LED_FLAG_USE_ROTATE_PATTERN) { + led_run_pattern(led_setups[led_animation_id], &ro, &go, &bo, po); + } + + next_iter: + led_cur_instruction++; + } + + if (ro > 255) + ro = 255; + else if (ro < 0) + ro = 0; + if (go > 255) + go = 255; + else if (go < 0) + go = 0; + if (bo > 255) + bo = 255; + else if (bo < 0) + bo = 0; + + if (led_animation_breathing) { + ro *= breathe_mult; + go *= breathe_mult; + bo *= breathe_mult; + } + } + + led_buffer[i].r = (uint8_t)ro; + led_buffer[i].g = (uint8_t)go; + led_buffer[i].b = (uint8_t)bo; +} + +#endif // USE_MASSDROP_CONFIGURATOR diff --git a/tmk_core/protocol/arm_atsam/md_rgb_matrix.h b/tmk_core/protocol/arm_atsam/md_rgb_matrix.h new file mode 100644 index 0000000000..322b0f99d1 --- /dev/null +++ b/tmk_core/protocol/arm_atsam/md_rgb_matrix.h @@ -0,0 +1,158 @@ +/* +Copyright 2018 Massdrop Inc. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef _LED_MATRIX_H_ +#define _LED_MATRIX_H_ + +#include "quantum.h" + +// From keyboard +#include "config_led.h" + +// CS1-CS16 Current Source "Col" +#define ISSI3733_CS_COUNT 16 + +// SW1-SW12 Switch "Row" +#define ISSI3733_SW_COUNT 12 + +#define ISSI3733_LED_RGB_COUNT ISSI3733_CS_COUNT *ISSI3733_SW_COUNT +#define ISSI3733_PG0_BYTES ISSI3733_LED_RGB_COUNT / 8 + 1 //+1 for first byte being memory start offset for I2C transfer +#define ISSI3733_PG1_BYTES ISSI3733_LED_RGB_COUNT + 1 //+1 for first byte being memory start offset for I2C transfer +#define ISSI3733_PG2_BYTES ISSI3733_LED_RGB_COUNT + 1 //+1 for first byte being memory start offset for I2C transfer +#define ISSI3733_PG3_BYTES 18 + 1 //+1 for first byte being memory start offset for I2C transfer + +#define ISSI3733_PG_ONOFF_BYTES ISSI3733_PG0_BYTES +#define ISSI3733_PG_OR_BYTES ISSI3733_PG0_BYTES +#define ISSI3733_PG_SR_BYTES ISSI3733_PG0_BYTES +#define ISSI3733_PG_PWM_BYTES ISSI3733_PG1_BYTES +#define ISSI3733_PG_ABM_BYTES ISSI3733_PG2_BYTES +#define ISSI3733_PG_FN_BYTES ISSI3733_PG3_BYTES + +typedef struct issi3733_driver_s { + uint8_t addr; // Address of the driver according to wiring "ISSI3733: Table 1 Slave Address" + uint8_t onoff[ISSI3733_PG_ONOFF_BYTES]; // PG0 - LED Control Register - LED On/Off Register + uint8_t open[ISSI3733_PG_OR_BYTES]; // PG0 - LED Control Register - LED Open Register + uint8_t shrt[ISSI3733_PG_SR_BYTES]; // PG0 - LED Control Register - LED Short Register + uint8_t pwm[ISSI3733_PG_PWM_BYTES]; // PG1 - PWM Register + uint8_t abm[ISSI3733_PG_ABM_BYTES]; // PG2 - Auto Breath Mode Register + uint8_t conf[ISSI3733_PG_FN_BYTES]; // PG3 - Function Register +} issi3733_driver_t; + +typedef struct issi3733_rgb_s { + uint8_t *r; // Direct access into PWM data + uint8_t *g; // Direct access into PWM data + uint8_t *b; // Direct access into PWM data +} issi3733_rgb_t; + +typedef struct issi3733_rgb_adr_s { + uint8_t drv; // Driver from given list + uint8_t cs; // CS + uint8_t swr; // SW Red + uint8_t swg; // SW Green + uint8_t swb; // SW Blue +} issi3733_rgb_adr_t; + +typedef struct issi3733_led_s { + uint8_t id; // According to PCB ref + issi3733_rgb_t rgb; // PWM settings of R G B + issi3733_rgb_adr_t adr; // Hardware addresses + float x; // Physical position X + float y; // Physical position Y + float px; // Physical position X in percent + float py; // Physical position Y in percent + uint8_t scan; // Key scan code from wiring (set 0xFF if no key) +} issi3733_led_t; + +extern issi3733_driver_t issidrv[ISSI3733_DRIVER_COUNT]; + +extern uint8_t gcr_desired; +extern uint8_t gcr_breathe; +extern uint8_t gcr_actual; +extern uint8_t gcr_actual_last; + +void gcr_compute(void); + +void md_rgb_matrix_indicators(void); + +/*------------------------- Legacy Lighting Support ------------------------*/ + +#ifdef USE_MASSDROP_CONFIGURATOR + +# define EF_NONE 0x00000000 // No effect +# define EF_OVER 0x00000001 // Overwrite any previous color information with new +# define EF_SCR_L 0x00000002 // Scroll left +# define EF_SCR_R 0x00000004 // Scroll right +# define EF_SUBTRACT 0x00000008 // Subtract color values + +typedef struct led_setup_s { + float hs; // Band begin at percent + float he; // Band end at percent + uint8_t rs; // Red start value + uint8_t re; // Red end value + uint8_t gs; // Green start value + uint8_t ge; // Green end value + uint8_t bs; // Blue start value + uint8_t be; // Blue end value + uint32_t ef; // Animation and color effects + uint8_t end; // Set to signal end of the setup +} led_setup_t; + +extern const uint8_t led_setups_count; +extern void * led_setups[]; + +// LED Extra Instructions +# define LED_FLAG_NULL 0x00 // Matching and coloring not used (default) +# define LED_FLAG_MATCH_ID 0x01 // Match on the ID of the LED (set id#'s to desired bit pattern, first LED is id 1) +# define LED_FLAG_MATCH_LAYER 0x02 // Match on the current active layer (set layer to desired match layer) +# define LED_FLAG_USE_RGB 0x10 // Use a specific RGB value (set r, g, b to desired output color values) +# define LED_FLAG_USE_PATTERN 0x20 // Use a specific pattern ID (set pattern_id to desired output pattern) +# define LED_FLAG_USE_ROTATE_PATTERN 0x40 // Use pattern the user has cycled to manually + +typedef struct led_instruction_s { + uint16_t flags; // Bitfield for LED instructions + uint32_t id0; // Bitwise id, IDs 0-31 + uint32_t id1; // Bitwise id, IDs 32-63 + uint32_t id2; // Bitwise id, IDs 64-95 + uint32_t id3; // Bitwise id, IDs 96-127 + uint8_t layer; + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t pattern_id; + uint8_t end; +} led_instruction_t; + +extern led_instruction_t led_instructions[]; + +extern uint8_t led_animation_breathing; +extern uint8_t led_animation_id; +extern float led_animation_speed; +extern uint8_t led_lighting_mode; +extern uint8_t led_enabled; +extern uint8_t led_animation_breathe_cur; +extern uint8_t led_animation_direction; +extern uint8_t breathe_dir; + +# define LED_MODE_NORMAL 0 // Must be 0 +# define LED_MODE_KEYS_ONLY 1 +# define LED_MODE_NON_KEYS_ONLY 2 +# define LED_MODE_INDICATORS_ONLY 3 +# define LED_MODE_MAX_INDEX LED_MODE_INDICATORS_ONLY // Must be highest value + +#endif // USE_MASSDROP_CONFIGURATOR + +#endif //_LED_MATRIX_H_ diff --git a/tmk_core/protocol/arm_atsam/md_rgb_matrix_programs.c b/tmk_core/protocol/arm_atsam/md_rgb_matrix_programs.c new file mode 100644 index 0000000000..b43008cc5b --- /dev/null +++ b/tmk_core/protocol/arm_atsam/md_rgb_matrix_programs.c @@ -0,0 +1,99 @@ +/* +Copyright 2018 Massdrop Inc. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifdef USE_MASSDROP_CONFIGURATOR + +# include "md_rgb_matrix.h" + +// Teal <-> Salmon +led_setup_t leds_teal_salmon[] = { + {.hs = 0, .he = 33, .rs = 24, .re = 24, .gs = 215, .ge = 215, .bs = 204, .be = 204, .ef = EF_NONE}, + {.hs = 33, .he = 66, .rs = 24, .re = 255, .gs = 215, .ge = 114, .bs = 204, .be = 118, .ef = EF_NONE}, + {.hs = 66, .he = 100, .rs = 255, .re = 255, .gs = 114, .ge = 114, .bs = 118, .be = 118, .ef = EF_NONE}, + {.end = 1}, +}; + +// Yellow +led_setup_t leds_yellow[] = { + {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_NONE}, + {.end = 1}, +}; + +// Off +led_setup_t leds_off[] = { + {.hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_NONE}, + {.end = 1}, +}; + +// Red +led_setup_t leds_red[] = { + {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_NONE}, + {.end = 1}, +}; + +// Green +led_setup_t leds_green[] = { + {.hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_NONE}, + {.end = 1}, +}; + +// Blue +led_setup_t leds_blue[] = { + {.hs = 0, .he = 100, .rs = 0, .re = 0, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_NONE}, + {.end = 1}, +}; + +// White +led_setup_t leds_white[] = { + {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 255, .be = 255, .ef = EF_NONE}, + {.end = 1}, +}; + +// White with moving red stripe +led_setup_t leds_white_with_red_stripe[] = { + {.hs = 0, .he = 100, .rs = 255, .re = 255, .gs = 255, .ge = 255, .bs = 255, .be = 255, .ef = EF_NONE}, + {.hs = 0, .he = 15, .rs = 0, .re = 0, .gs = 0, .ge = 255, .bs = 0, .be = 255, .ef = EF_SCR_R | EF_SUBTRACT}, + {.hs = 15, .he = 30, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 0, .ef = EF_SCR_R | EF_SUBTRACT}, + {.end = 1}, +}; + +// Black with moving red stripe +led_setup_t leds_black_with_red_stripe[] = { + {.hs = 0, .he = 15, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_SCR_R}, + {.hs = 15, .he = 30, .rs = 255, .re = 0, .gs = 0, .ge = 0, .bs = 0, .be = 0, .ef = EF_SCR_R}, + {.end = 1}, +}; + +// Rainbow no scrolling +led_setup_t leds_rainbow_ns[] = { + {.hs = 0, .he = 16.67, .rs = 255, .re = 255, .gs = 0, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER}, {.hs = 16.67, .he = 33.33, .rs = 255, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER}, {.hs = 33.33, .he = 50, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 255, .ef = EF_OVER}, {.hs = 50, .he = 66.67, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER}, {.hs = 66.67, .he = 83.33, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER}, {.hs = 83.33, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 0, .ef = EF_OVER}, {.end = 1}, +}; + +// Rainbow scrolling +led_setup_t leds_rainbow_s[] = { + {.hs = 0, .he = 16.67, .rs = 255, .re = 255, .gs = 0, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER | EF_SCR_R}, {.hs = 16.67, .he = 33.33, .rs = 255, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 0, .ef = EF_OVER | EF_SCR_R}, {.hs = 33.33, .he = 50, .rs = 0, .re = 0, .gs = 255, .ge = 255, .bs = 0, .be = 255, .ef = EF_OVER | EF_SCR_R}, {.hs = 50, .he = 66.67, .rs = 0, .re = 0, .gs = 255, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER | EF_SCR_R}, {.hs = 66.67, .he = 83.33, .rs = 0, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 255, .ef = EF_OVER | EF_SCR_R}, {.hs = 83.33, .he = 100, .rs = 255, .re = 255, .gs = 0, .ge = 0, .bs = 255, .be = 0, .ef = EF_OVER | EF_SCR_R}, {.end = 1}, +}; + +// Add new LED animations here using one from above as example +// The last entry must be { .end = 1 } +// Add the new animation name to the list below following its format + +void *led_setups[] = {leds_rainbow_s, leds_rainbow_ns, leds_teal_salmon, leds_yellow, leds_red, leds_green, leds_blue, leds_white, leds_white_with_red_stripe, leds_black_with_red_stripe, leds_off}; + +const uint8_t led_setups_count = sizeof(led_setups) / sizeof(led_setups[0]); + +#endif diff --git a/tmk_core/protocol/chibios/main.c b/tmk_core/protocol/chibios/main.c index a0d28f9afc..400c0b8f53 100644 --- a/tmk_core/protocol/chibios/main.c +++ b/tmk_core/protocol/chibios/main.c @@ -54,6 +54,9 @@ #ifdef STM32_EEPROM_ENABLE # include "eeprom_stm32.h" #endif +#ifdef EEPROM_DRIVER +# include "eeprom_driver.h" +#endif #include "suspend.h" #include "wait.h" @@ -150,6 +153,9 @@ int main(void) { #ifdef STM32_EEPROM_ENABLE EEPROM_Init(); #endif +#ifdef EEPROM_DRIVER + eeprom_driver_init(); +#endif // TESTING // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); @@ -230,6 +236,7 @@ int main(void) { /* Remote wakeup */ if (suspend_wakeup_condition()) { usbWakeupHost(&USB_DRIVER); + restart_usb_driver(&USB_DRIVER); } } /* Woken up */ @@ -258,5 +265,9 @@ int main(void) { #ifdef RAW_ENABLE raw_hid_task(); #endif + + // Run housekeeping + housekeeping_task_kb(); + housekeeping_task_user(); } } diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index ae33e86a70..bb4bf6a580 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -165,6 +165,19 @@ static const USBEndpointConfig shared_ep_config = { }; #endif +#if STM32_USB_USE_OTG1 +typedef struct { + size_t queue_capacity_in; + size_t queue_capacity_out; + USBInEndpointState in_ep_state; + USBOutEndpointState out_ep_state; + USBInEndpointState int_ep_state; + USBEndpointConfig inout_ep_config; + USBEndpointConfig int_ep_config; + const QMKUSBConfig config; + QMKUSBDriver driver; +} usb_driver_config_t; +#else typedef struct { size_t queue_capacity_in; size_t queue_capacity_out; @@ -177,7 +190,54 @@ typedef struct { const QMKUSBConfig config; QMKUSBDriver driver; } usb_driver_config_t; +#endif +#if STM32_USB_USE_OTG1 +/* Reusable initialization structure - see USBEndpointConfig comment at top of file */ +#define QMK_USB_DRIVER_CONFIG(stream, notification, fixedsize) \ + { \ + .queue_capacity_in = stream##_IN_CAPACITY, .queue_capacity_out = stream##_OUT_CAPACITY, \ + .inout_ep_config = \ + { \ + stream##_IN_MODE, /* Interrupt EP */ \ + NULL, /* SETUP packet notification callback */ \ + qmkusbDataTransmitted, /* IN notification callback */ \ + qmkusbDataReceived, /* OUT notification callback */ \ + stream##_EPSIZE, /* IN maximum packet size */ \ + stream##_EPSIZE, /* OUT maximum packet size */ \ + NULL, /* IN Endpoint state */ \ + NULL, /* OUT endpoint state */ \ + 2, /* IN multiplier */ \ + NULL /* SETUP buffer (not a SETUP endpoint) */ \ + }, \ + .int_ep_config = \ + { \ + USB_EP_MODE_TYPE_INTR, /* Interrupt EP */ \ + NULL, /* SETUP packet notification callback */ \ + qmkusbInterruptTransmitted, /* IN notification callback */ \ + NULL, /* OUT notification callback */ \ + CDC_NOTIFICATION_EPSIZE, /* IN maximum packet size */ \ + 0, /* OUT maximum packet size */ \ + NULL, /* IN Endpoint state */ \ + NULL, /* OUT endpoint state */ \ + 2, /* IN multiplier */ \ + NULL, /* SETUP buffer (not a SETUP endpoint) */ \ + }, \ + .config = { \ + .usbp = &USB_DRIVER, \ + .bulk_in = stream##_IN_EPNUM, \ + .bulk_out = stream##_OUT_EPNUM, \ + .int_in = notification, \ + .in_buffers = stream##_IN_CAPACITY, \ + .out_buffers = stream##_OUT_CAPACITY, \ + .in_size = stream##_EPSIZE, \ + .out_size = stream##_EPSIZE, \ + .fixed_size = fixedsize, \ + .ib = (__attribute__((aligned(4))) uint8_t[BQ_BUFFER_SIZE(stream##_IN_CAPACITY, stream##_EPSIZE)]){}, \ + .ob = (__attribute__((aligned(4))) uint8_t[BQ_BUFFER_SIZE(stream##_OUT_CAPACITY, stream##_EPSIZE)]){}, \ + } \ + } +#else /* Reusable initialization structure - see USBEndpointConfig comment at top of file */ #define QMK_USB_DRIVER_CONFIG(stream, notification, fixedsize) \ { \ @@ -235,6 +295,7 @@ typedef struct { .ob = (__attribute__((aligned(4))) uint8_t[BQ_BUFFER_SIZE(stream##_OUT_CAPACITY, stream##_EPSIZE)]){}, \ } \ } +#endif typedef struct { union { @@ -327,8 +388,12 @@ static void usb_event_cb(USBDriver *usbp, usbevent_t event) { usbInitEndpointI(usbp, SHARED_IN_EPNUM, &shared_ep_config); #endif for (int i = 0; i < NUM_USB_DRIVERS; i++) { + #if STM32_USB_USE_OTG1 + usbInitEndpointI(usbp, drivers.array[i].config.bulk_in, &drivers.array[i].inout_ep_config); + #else usbInitEndpointI(usbp, drivers.array[i].config.bulk_in, &drivers.array[i].in_ep_config); usbInitEndpointI(usbp, drivers.array[i].config.bulk_out, &drivers.array[i].out_ep_config); + #endif if (drivers.array[i].config.int_in) { usbInitEndpointI(usbp, drivers.array[i].config.int_in, &drivers.array[i].int_ep_config); } @@ -553,12 +618,21 @@ static const USBConfig usbcfg = { */ void init_usb_driver(USBDriver *usbp) { for (int i = 0; i < NUM_USB_DRIVERS; i++) { + #if STM32_USB_USE_OTG1 + QMKUSBDriver *driver = &drivers.array[i].driver; + drivers.array[i].inout_ep_config.in_state = &drivers.array[i].in_ep_state; + drivers.array[i].inout_ep_config.out_state = &drivers.array[i].out_ep_state; + drivers.array[i].int_ep_config.in_state = &drivers.array[i].int_ep_state; + qmkusbObjectInit(driver, &drivers.array[i].config); + qmkusbStart(driver, &drivers.array[i].config); + #else QMKUSBDriver *driver = &drivers.array[i].driver; drivers.array[i].in_ep_config.in_state = &drivers.array[i].in_ep_state; drivers.array[i].out_ep_config.out_state = &drivers.array[i].out_ep_state; drivers.array[i].int_ep_config.in_state = &drivers.array[i].int_ep_state; qmkusbObjectInit(driver, &drivers.array[i].config); qmkusbStart(driver, &drivers.array[i].config); + #endif } /* @@ -574,6 +648,13 @@ void init_usb_driver(USBDriver *usbp) { chVTObjectInit(&keyboard_idle_timer); } +void restart_usb_driver(USBDriver *usbp) { + usbStop(usbp); + usbDisconnectBus(usbp); + usbStart(usbp, &usbcfg); + usbConnectBus(usbp); +} + /* --------------------------------------------------------- * Keyboard functions * --------------------------------------------------------- diff --git a/tmk_core/protocol/chibios/usb_main.h b/tmk_core/protocol/chibios/usb_main.h index 94baf9b35e..d8813f4801 100644 --- a/tmk_core/protocol/chibios/usb_main.h +++ b/tmk_core/protocol/chibios/usb_main.h @@ -35,6 +35,9 @@ /* Initialize the USB driver and bus */ void init_usb_driver(USBDriver *usbp); +/* Restart the USB driver and bus */ +void restart_usb_driver(USBDriver *usbp); + /* --------------- * Keyboard header * --------------- diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index cec0044026..878be7d342 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -1104,6 +1104,10 @@ int main(void) { #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); #endif + + // Run housekeeping + housekeeping_task_kb(); + housekeeping_task_user(); } } diff --git a/tmk_core/protocol/usb_descriptor.c b/tmk_core/protocol/usb_descriptor.c index f5d32445de..a0c0d4cdc8 100644 --- a/tmk_core/protocol/usb_descriptor.c +++ b/tmk_core/protocol/usb_descriptor.c @@ -41,6 +41,10 @@ #include "usb_descriptor.h" #include "usb_descriptor_common.h" +#ifdef JOYSTICK_ENABLE +# include "joystick.h" +#endif + // clang-format off /* @@ -308,10 +312,17 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM JoystickReport[] = { HID_RI_USAGE(8, 0x35), // Rz # endif # if JOYSTICK_AXES_COUNT >= 1 - HID_RI_LOGICAL_MINIMUM(8, -127), - HID_RI_LOGICAL_MAXIMUM(8, 127), + # if JOYSTICK_AXES_RESOLUTION == 8 + HID_RI_LOGICAL_MINIMUM(8, -JOYSTICK_RESOLUTION), + HID_RI_LOGICAL_MAXIMUM(8, JOYSTICK_RESOLUTION), HID_RI_REPORT_COUNT(8, JOYSTICK_AXES_COUNT), HID_RI_REPORT_SIZE(8, 0x08), + # else + HID_RI_LOGICAL_MINIMUM(16, -JOYSTICK_RESOLUTION), + HID_RI_LOGICAL_MAXIMUM(16, JOYSTICK_RESOLUTION), + HID_RI_REPORT_COUNT(8, JOYSTICK_AXES_COUNT), + HID_RI_REPORT_SIZE(8, 0x10), + # endif HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE), # endif diff --git a/tmk_core/protocol/usb_descriptor.h b/tmk_core/protocol/usb_descriptor.h index 02a4b1ce59..aa8863f43f 100644 --- a/tmk_core/protocol/usb_descriptor.h +++ b/tmk_core/protocol/usb_descriptor.h @@ -205,7 +205,11 @@ enum usb_endpoints { #ifdef RAW_ENABLE RAW_IN_EPNUM = NEXT_EPNUM, + #if STM32_USB_USE_OTG1 + #define RAW_OUT_EPNUM RAW_IN_EPNUM + #else RAW_OUT_EPNUM = NEXT_EPNUM, + #endif #endif #ifdef SHARED_EP_ENABLE @@ -219,7 +223,11 @@ enum usb_endpoints { // ChibiOS has enough memory and descriptor to actually enable the endpoint // It could use the same endpoint numbers, as that's supported by ChibiOS // But the QMK code currently assumes that the endpoint numbers are different + #if STM32_USB_USE_OTG1 + #define CONSOLE_OUT_EPNUM CONSOLE_IN_EPNUM + #else CONSOLE_OUT_EPNUM = NEXT_EPNUM, + #endif # else # define CONSOLE_OUT_EPNUM CONSOLE_IN_EPNUM # endif @@ -227,17 +235,29 @@ enum usb_endpoints { #ifdef MIDI_ENABLE MIDI_STREAM_IN_EPNUM = NEXT_EPNUM, + #if STM32_USB_USE_OTG1 + #define MIDI_STREAM_OUT_EPNUM MIDI_STREAM_IN_EPNUM + #else MIDI_STREAM_OUT_EPNUM = NEXT_EPNUM, + #endif #endif #ifdef VIRTSER_ENABLE CDC_NOTIFICATION_EPNUM = NEXT_EPNUM, CDC_IN_EPNUM = NEXT_EPNUM, + #if STM32_USB_USE_OTG1 + #define CDC_OUT_EPNUM CDC_IN_EPNUM + #else CDC_OUT_EPNUM = NEXT_EPNUM, + #endif #endif #ifdef JOYSTICK_ENABLE JOYSTICK_IN_EPNUM = NEXT_EPNUM, + #if STM32_USB_USE_OTG1 + JOYSTICK_OUT_EPNUM = JOYSTICK_IN_EPNUM, + #else JOYSTICK_OUT_EPNUM = NEXT_EPNUM, + #endif #endif }; diff --git a/tmk_core/protocol/vusb/main.c b/tmk_core/protocol/vusb/main.c index a57df5ce06..0e3447d926 100644 --- a/tmk_core/protocol/vusb/main.c +++ b/tmk_core/protocol/vusb/main.c @@ -153,6 +153,10 @@ int main(void) { console_task(); } #endif + + // Run housekeeping + housekeeping_task_kb(); + housekeeping_task_user(); } else if (suspend_wakeup_condition()) { usb_remote_wakeup(); } -- cgit v1.2.3