From 968655c74cc13a1a63320ef795fe436449708429 Mon Sep 17 00:00:00 2001 From: lokher Date: Wed, 3 Aug 2022 16:53:42 +0800 Subject: [PATCH] Add K8 pro --- builddefs/common_features.mk | 15 +- drivers/eeprom/eeprom_driver.h | 2 +- drivers/led/ckled2001-simple.c | 218 +++++++ drivers/led/ckled2001-simple.h | 337 ++++++++++ drivers/led/ckled2001.c | 24 +- drivers/led/ckled2001.h | 4 +- .../keychron/bluetooth/bat_level_animation.c | 128 ++++ .../keychron/bluetooth/bat_level_animation.h | 23 + keyboards/keychron/bluetooth/battery.c | 107 ++++ keyboards/keychron/bluetooth/battery.h | 51 ++ keyboards/keychron/bluetooth/bluetooth.c | 432 +++++++++++++ keyboards/keychron/bluetooth/bluetooth.h | 85 +++ keyboards/keychron/bluetooth/bluetooth.mk | 14 + .../keychron/bluetooth/bluetooth_config.h | 32 + .../keychron/bluetooth/bluetooth_event_type.h | 44 ++ keyboards/keychron/bluetooth/bluetooth_main.c | 37 ++ keyboards/keychron/bluetooth/ckbt51.c | 575 +++++++++++++++++ keyboards/keychron/bluetooth/ckbt51.h | 155 +++++ keyboards/keychron/bluetooth/factory_test.c | 320 ++++++++++ keyboards/keychron/bluetooth/factory_test.h | 24 + keyboards/keychron/bluetooth/indicator.c | 434 +++++++++++++ keyboards/keychron/bluetooth/indicator.h | 89 +++ keyboards/keychron/bluetooth/lpm.c | 88 +++ keyboards/keychron/bluetooth/lpm.h | 31 + keyboards/keychron/bluetooth/lpm_stm32l432.c | 339 ++++++++++ keyboards/keychron/bluetooth/lpm_stm32l432.h | 19 + keyboards/keychron/bluetooth/report_buffer.c | 141 +++++ keyboards/keychron/bluetooth/report_buffer.h | 50 ++ keyboards/keychron/bluetooth/transport.c | 182 ++++++ keyboards/keychron/bluetooth/transport.h | 32 + keyboards/keychron/k8_pro/ansi/info.json | 103 +++ keyboards/keychron/k8_pro/ansi/rgb/config.h | 78 +++ .../k8_pro/ansi/rgb/keymaps/default/keymap.c | 59 ++ .../k8_pro/ansi/rgb/keymaps/via/keymap.c | 59 ++ .../k8_pro/ansi/rgb/keymaps/via/rules.mk | 1 + keyboards/keychron/k8_pro/ansi/rgb/rgb.c | 148 +++++ keyboards/keychron/k8_pro/ansi/rgb/rules.mk | 3 + keyboards/keychron/k8_pro/ansi/white/config.h | 64 ++ .../ansi/white/keymaps/default/keymap.c | 59 ++ .../k8_pro/ansi/white/keymaps/via/keymap.c | 59 ++ .../k8_pro/ansi/white/keymaps/via/rules.mk | 1 + keyboards/keychron/k8_pro/ansi/white/rules.mk | 2 + keyboards/keychron/k8_pro/ansi/white/white.c | 147 +++++ keyboards/keychron/k8_pro/config.h | 106 ++++ keyboards/keychron/k8_pro/halconf.h | 27 + keyboards/keychron/k8_pro/iso/info.json | 104 +++ keyboards/keychron/k8_pro/iso/rgb/config.h | 78 +++ .../k8_pro/iso/rgb/keymaps/default/keymap.c | 59 ++ .../k8_pro/iso/rgb/keymaps/via/keymap.c | 59 ++ .../k8_pro/iso/rgb/keymaps/via/rules.mk | 1 + keyboards/keychron/k8_pro/iso/rgb/rgb.c | 149 +++++ keyboards/keychron/k8_pro/iso/rgb/rules.mk | 3 + keyboards/keychron/k8_pro/iso/white/config.h | 63 ++ .../k8_pro/iso/white/keymaps/default/keymap.c | 59 ++ .../k8_pro/iso/white/keymaps/via/keymap.c | 59 ++ .../k8_pro/iso/white/keymaps/via/rules.mk | 1 + keyboards/keychron/k8_pro/iso/white/rules.mk | 2 + keyboards/keychron/k8_pro/iso/white/white.c | 148 +++++ keyboards/keychron/k8_pro/k8_pro.c | 297 +++++++++ keyboards/keychron/k8_pro/k8_pro.h | 99 +++ keyboards/keychron/k8_pro/matrix.c | 173 +++++ keyboards/keychron/k8_pro/mcuconf.h | 37 ++ keyboards/keychron/k8_pro/readme.md | 21 + keyboards/keychron/k8_pro/rules.mk | 36 ++ platforms/chibios/eeprom_stm32_defs.h | 7 +- platforms/chibios/eeprom_stm32_l4.c | 598 ++++++++++++++++++ platforms/chibios/eeprom_stm32_l4.h | 34 + platforms/chibios/flash_stm32.c | 55 ++ platforms/chibios/flash_stm32.h | 1 + platforms/eeprom.h | 3 + quantum/keyboard.c | 5 +- quantum/led_matrix/led_matrix.c | 86 ++- quantum/led_matrix/led_matrix.h | 28 + quantum/led_matrix/led_matrix_drivers.c | 94 ++- quantum/rgb_matrix/rgb_matrix.c | 80 ++- quantum/rgb_matrix/rgb_matrix.h | 23 + quantum/rgb_matrix/rgb_matrix_drivers.c | 45 ++ quantum/via.c | 6 + tmk_core/protocol.mk | 2 - tmk_core/protocol/chibios/chibios.c | 2 +- tmk_core/protocol/chibios/usb_driver.c | 2 +- tmk_core/protocol/chibios/usb_main.c | 28 +- tmk_core/protocol/chibios/usb_main.h | 7 +- tmk_core/protocol/host.h | 1 + tmk_core/protocol/report.c | 40 ++ 85 files changed, 7497 insertions(+), 46 deletions(-) create mode 100644 drivers/led/ckled2001-simple.c create mode 100644 drivers/led/ckled2001-simple.h create mode 100644 keyboards/keychron/bluetooth/bat_level_animation.c create mode 100644 keyboards/keychron/bluetooth/bat_level_animation.h create mode 100644 keyboards/keychron/bluetooth/battery.c create mode 100644 keyboards/keychron/bluetooth/battery.h create mode 100644 keyboards/keychron/bluetooth/bluetooth.c create mode 100644 keyboards/keychron/bluetooth/bluetooth.h create mode 100644 keyboards/keychron/bluetooth/bluetooth.mk create mode 100644 keyboards/keychron/bluetooth/bluetooth_config.h create mode 100644 keyboards/keychron/bluetooth/bluetooth_event_type.h create mode 100644 keyboards/keychron/bluetooth/bluetooth_main.c create mode 100644 keyboards/keychron/bluetooth/ckbt51.c create mode 100644 keyboards/keychron/bluetooth/ckbt51.h create mode 100644 keyboards/keychron/bluetooth/factory_test.c create mode 100644 keyboards/keychron/bluetooth/factory_test.h create mode 100644 keyboards/keychron/bluetooth/indicator.c create mode 100644 keyboards/keychron/bluetooth/indicator.h create mode 100644 keyboards/keychron/bluetooth/lpm.c create mode 100644 keyboards/keychron/bluetooth/lpm.h create mode 100644 keyboards/keychron/bluetooth/lpm_stm32l432.c create mode 100644 keyboards/keychron/bluetooth/lpm_stm32l432.h create mode 100644 keyboards/keychron/bluetooth/report_buffer.c create mode 100644 keyboards/keychron/bluetooth/report_buffer.h create mode 100644 keyboards/keychron/bluetooth/transport.c create mode 100644 keyboards/keychron/bluetooth/transport.h create mode 100644 keyboards/keychron/k8_pro/ansi/info.json create mode 100644 keyboards/keychron/k8_pro/ansi/rgb/config.h create mode 100644 keyboards/keychron/k8_pro/ansi/rgb/keymaps/default/keymap.c create mode 100644 keyboards/keychron/k8_pro/ansi/rgb/keymaps/via/keymap.c create mode 100644 keyboards/keychron/k8_pro/ansi/rgb/keymaps/via/rules.mk create mode 100644 keyboards/keychron/k8_pro/ansi/rgb/rgb.c create mode 100644 keyboards/keychron/k8_pro/ansi/rgb/rules.mk create mode 100644 keyboards/keychron/k8_pro/ansi/white/config.h create mode 100644 keyboards/keychron/k8_pro/ansi/white/keymaps/default/keymap.c create mode 100644 keyboards/keychron/k8_pro/ansi/white/keymaps/via/keymap.c create mode 100644 keyboards/keychron/k8_pro/ansi/white/keymaps/via/rules.mk create mode 100644 keyboards/keychron/k8_pro/ansi/white/rules.mk create mode 100644 keyboards/keychron/k8_pro/ansi/white/white.c create mode 100644 keyboards/keychron/k8_pro/config.h create mode 100644 keyboards/keychron/k8_pro/halconf.h create mode 100644 keyboards/keychron/k8_pro/iso/info.json create mode 100644 keyboards/keychron/k8_pro/iso/rgb/config.h create mode 100644 keyboards/keychron/k8_pro/iso/rgb/keymaps/default/keymap.c create mode 100644 keyboards/keychron/k8_pro/iso/rgb/keymaps/via/keymap.c create mode 100644 keyboards/keychron/k8_pro/iso/rgb/keymaps/via/rules.mk create mode 100644 keyboards/keychron/k8_pro/iso/rgb/rgb.c create mode 100644 keyboards/keychron/k8_pro/iso/rgb/rules.mk create mode 100644 keyboards/keychron/k8_pro/iso/white/config.h create mode 100644 keyboards/keychron/k8_pro/iso/white/keymaps/default/keymap.c create mode 100644 keyboards/keychron/k8_pro/iso/white/keymaps/via/keymap.c create mode 100644 keyboards/keychron/k8_pro/iso/white/keymaps/via/rules.mk create mode 100644 keyboards/keychron/k8_pro/iso/white/rules.mk create mode 100644 keyboards/keychron/k8_pro/iso/white/white.c create mode 100644 keyboards/keychron/k8_pro/k8_pro.c create mode 100644 keyboards/keychron/k8_pro/k8_pro.h create mode 100644 keyboards/keychron/k8_pro/matrix.c create mode 100644 keyboards/keychron/k8_pro/mcuconf.h create mode 100644 keyboards/keychron/k8_pro/readme.md create mode 100644 keyboards/keychron/k8_pro/rules.mk create mode 100644 platforms/chibios/eeprom_stm32_l4.c create mode 100644 platforms/chibios/eeprom_stm32_l4.h diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk index c976b8296d..6308c0bea3 100644 --- a/builddefs/common_features.mk +++ b/builddefs/common_features.mk @@ -202,6 +202,12 @@ else COMMON_VPATH += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/eeprom SRC += eeprom_driver.c SRC += eeprom_stm32_L0_L1.c + else ifneq ($(filter $(MCU_SERIES),STM32L4xx),) + OPT_DEFS += -DEEPROM_DRIVER -DEEPROM_STM32_L4 + COMMON_VPATH += $(DRIVER_PATH)/eeprom + SRC += eeprom_driver.c + SRC += $(PLATFORM_COMMON_DIR)/eeprom_stm32_l4.c + SRC += $(PLATFORM_COMMON_DIR)/flash_stm32.c else ifneq ($(filter $(MCU_SERIES),KL2x K20x),) # Teensy EEPROM implementations OPT_DEFS += -DEEPROM_TEENSY @@ -275,7 +281,7 @@ ifeq ($(strip $(RGBLIGHT_ENABLE)), yes) endif LED_MATRIX_ENABLE ?= no -VALID_LED_MATRIX_TYPES := IS31FL3731 IS31FL3742A IS31FL3743A IS31FL3745 IS31FL3746A custom +VALID_LED_MATRIX_TYPES := IS31FL3731 IS31FL3742A IS31FL3743A IS31FL3745 IS31FL3746A CKLED2001 custom # TODO: IS31FL3733 IS31FL3737 IS31FL3741 ifeq ($(strip $(LED_MATRIX_ENABLE)), yes) @@ -331,6 +337,13 @@ endif QUANTUM_LIB_SRC += i2c_master.c endif + ifeq ($(strip $(LED_MATRIX_DRIVER)), CKLED2001) + OPT_DEFS += -DCKLED2001 -DSTM32_I2C -DHAL_USE_I2C=TRUE + COMMON_VPATH += $(DRIVER_PATH)/led + SRC += ckled2001-simple.c + QUANTUM_LIB_SRC += i2c_master.c + endif + endif RGB_MATRIX_ENABLE ?= no diff --git a/drivers/eeprom/eeprom_driver.h b/drivers/eeprom/eeprom_driver.h index 74592bc8f0..0249a96fd8 100644 --- a/drivers/eeprom/eeprom_driver.h +++ b/drivers/eeprom/eeprom_driver.h @@ -18,5 +18,5 @@ #include "eeprom.h" -void eeprom_driver_init(void); +uint16_t eeprom_driver_init(void); void eeprom_driver_erase(void); diff --git a/drivers/led/ckled2001-simple.c b/drivers/led/ckled2001-simple.c new file mode 100644 index 0000000000..da4bf20b99 --- /dev/null +++ b/drivers/led/ckled2001-simple.c @@ -0,0 +1,218 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 "ckled2001-simple.h" +#include "i2c_master.h" +#include "wait.h" + +#ifndef CKLED2001_TIMEOUT +# define CKLED2001_TIMEOUT 100 +#endif + +#ifndef CKLED2001_PERSISTENCE +# define CKLED2001_PERSISTENCE 0 +#endif + +#ifndef PHASE_CHANNEL +# define PHASE_CHANNEL MSKPHASE_12CHANNEL +#endif + +#ifndef CKLED2001_CURRENT_TUNE +# define CKLED2001_CURRENT_TUNE \ + { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } +#endif + +// Transfer buffer for TWITransmitData() +uint8_t g_twi_transfer_buffer[20]; + +// These buffers match the CKLED2001 PWM registers. +// The control buffers match the PG0 LED On/Off registers. +// Storing them like this is optimal for I2C transfers to the registers. +// We could optimize this and take out the unused registers from these +// buffers and the transfers in CKLED2001_write_pwm_buffer() but it's +// probably not worth the extra complexity. +uint8_t g_pwm_buffer[DRIVER_COUNT][192]; +bool g_pwm_buffer_update_required[DRIVER_COUNT] = {false}; + +uint8_t g_led_control_registers[DRIVER_COUNT][24] = {0}; +bool g_led_control_registers_update_required[DRIVER_COUNT] = {false}; + +bool CKLED2001_write_register(uint8_t addr, uint8_t reg, uint8_t data) { + // If the transaction fails function returns false. + g_twi_transfer_buffer[0] = reg; + g_twi_transfer_buffer[1] = data; + +#if CKLED2001_PERSISTENCE > 0 + for (uint8_t i = 0; i < CKLED2001_PERSISTENCE; i++) { + if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, CKLED2001_TIMEOUT) != 0) { + return false; + } + } +#else + if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 2, CKLED2001_TIMEOUT) != 0) { + return false; + } +#endif + return true; +} + +bool CKLED2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer) { + // Assumes PG1 is already selected. + // If any of the transactions fails function returns false. + // Transmit PWM registers in 12 transfers of 16 bytes. + // g_twi_transfer_buffer[] is 20 bytes + + // Iterate over the pwm_buffer contents at 16 byte intervals. + for (int i = 0; i < 192; i += 16) { + g_twi_transfer_buffer[0] = i; + // Copy the data from i to i+15. + // Device will auto-increment register for data after the first byte + // Thus this sets registers 0x00-0x0F, 0x10-0x1F, etc. in one transfer. + for (int j = 0; j < 16; j++) { + g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j]; + } + +#if CKLED2001_PERSISTENCE > 0 + for (uint8_t i = 0; i < CKLED2001_PERSISTENCE; i++) { + if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, CKLED2001_TIMEOUT) != 0) { + return false; + } + } +#else + if (i2c_transmit(addr << 1, g_twi_transfer_buffer, 17, CKLED2001_TIMEOUT) != 0) { + return false; + } +#endif + } + return true; +} + +void CKLED2001_init(uint8_t addr) { + // Select to function page + CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + // Setting LED driver to shutdown mode + CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); + // Setting internal channel pulldown/pullup + CKLED2001_write_register(addr, PDU_REG, MSKSET_CA_CB_CHANNEL); + // Select number of scan phase + CKLED2001_write_register(addr, SCAN_PHASE_REG, PHASE_CHANNEL); + // Setting PWM Delay Phase + CKLED2001_write_register(addr, SLEW_RATE_CONTROL_MODE1_REG, MSKPWM_DELAY_PHASE_ENABLE); + // Setting Driving/Sinking Channel Slew Rate + CKLED2001_write_register(addr, SLEW_RATE_CONTROL_MODE2_REG, MSKDRIVING_SINKING_CHHANNEL_SLEWRATE_ENABLE); + // Setting Iref + CKLED2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_DISABLE); + // Set LED CONTROL PAGE (Page 0) + CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); + for (int i = 0; i < LED_CONTROL_ON_OFF_LENGTH; i++) { + CKLED2001_write_register(addr, i, 0x00); + } + + // Set PWM PAGE (Page 1) + CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_PWM_PAGE); + for (int i = 0; i < LED_CURRENT_TUNE_LENGTH; i++) { + CKLED2001_write_register(addr, i, 0x00); + } + + // Set CURRENT PAGE (Page 4) + uint8_t current_tuen_reg_list[LED_CURRENT_TUNE_LENGTH] = CKLED2001_CURRENT_TUNE; + CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, CURRENT_TUNE_PAGE); + for (int i = 0; i < LED_CURRENT_TUNE_LENGTH; i++) { + CKLED2001_write_register(addr, i, current_tuen_reg_list[i]); + } + + // Enable LEDs ON/OFF + CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); + for (int i = 0; i < LED_CONTROL_ON_OFF_LENGTH; i++) { + CKLED2001_write_register(addr, i, 0xFF); + } + + // Select to function page + CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + // Setting LED driver to normal mode + CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); +} + +void CKLED2001_set_value(int index, uint8_t value) { + ckled2001_led led; + if (index >= 0 && index < DRIVER_LED_TOTAL) { + memcpy_P(&led, (&g_ckled2001_leds[index]), sizeof(led)); + + g_pwm_buffer[led.driver][led.v] = value; + g_pwm_buffer_update_required[led.driver] = true; + } +} + +void CKLED2001_set_value_all(uint8_t value) { + for (int i = 0; i < DRIVER_LED_TOTAL; i++) { + CKLED2001_set_value(i, value); + } +} + +void CKLED2001_set_led_control_register(uint8_t index, bool value) { + ckled2001_led led; + memcpy_P(&led, (&g_ckled2001_leds[index]), sizeof(led)); + + uint8_t control_register = led.v / 8; + uint8_t bit_value = led.v % 8; + + if (value) { + g_led_control_registers[led.driver][control_register] |= (1 << bit_value); + } else { + g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value); + } + + g_led_control_registers_update_required[led.driver] = true; +} + +void CKLED2001_update_pwm_buffers(uint8_t addr, uint8_t index) { + if (g_pwm_buffer_update_required[index]) { + CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_PWM_PAGE); + + // If any of the transactions fail we risk writing dirty PG0, + // refresh page 0 just in case. + if (!CKLED2001_write_pwm_buffer(addr, g_pwm_buffer[index])) { + g_led_control_registers_update_required[index] = true; + } + } + g_pwm_buffer_update_required[index] = false; +} + +void CKLED2001_update_led_control_registers(uint8_t addr, uint8_t index) { + if (g_led_control_registers_update_required[index]) { + CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, LED_CONTROL_PAGE); + for (int i = 0; i < 24; i++) { + CKLED2001_write_register(addr, i, g_led_control_registers[index][i]); + } + } + g_led_control_registers_update_required[index] = false; +} + +void CKLED2001_sw_return_normal(uint8_t addr) { + // Select to function page + CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + // Setting LED driver to normal mode + CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); +} + +void CKLED2001_sw_shutdown(uint8_t addr) { + // Select to function page + CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); + // Setting LED driver to shutdown mode + CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); + // Write SW Sleep Register + CKLED2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_ENABLE); +} diff --git a/drivers/led/ckled2001-simple.h b/drivers/led/ckled2001-simple.h new file mode 100644 index 0000000000..731da2e1cd --- /dev/null +++ b/drivers/led/ckled2001-simple.h @@ -0,0 +1,337 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +#include +#include +#include "progmem.h" + +typedef struct ckled2001_led { + uint8_t driver : 2; + uint8_t v; +} __attribute__((packed)) ckled2001_led; + +extern const ckled2001_led PROGMEM g_ckled2001_leds[DRIVER_LED_TOTAL]; + +void CKLED2001_init(uint8_t addr); +bool CKLED2001_write_register(uint8_t addr, uint8_t reg, uint8_t data); +bool CKLED2001_write_pwm_buffer(uint8_t addr, uint8_t *pwm_buffer); + +void CKLED2001_set_value(int index, uint8_t value); +void CKLED2001_set_value_all(uint8_t value); + +void CKLED2001_set_led_control_register(uint8_t index, bool value); + +// This should not be called from an interrupt +// (eg. from a timer interrupt). +// Call this while idle (in between matrix scans). +// If the buffer is dirty, it will update the driver with the buffer. +void CKLED2001_update_pwm_buffers(uint8_t addr, uint8_t index); +void CKLED2001_update_led_control_registers(uint8_t addr, uint8_t index); + +void CKLED2001_sw_return_normal(uint8_t addr); +void CKLED2001_sw_shutdown(uint8_t addr); + +// Registers Page Define +#define CONFIGURE_CMD_PAGE 0xFD +#define LED_CONTROL_PAGE 0x00 +#define LED_PWM_PAGE 0x01 +#define FUNCTION_PAGE 0x03 +#define CURRENT_TUNE_PAGE 0x04 + +// Function Register: address 0x00 +#define CONFIGURATION_REG 0x00 +#define MSKSW_SHUT_DOWN_MODE (0x0 << 0) +#define MSKSW_NORMAL_MODE (0x1 << 0) + +#define DRIVER_ID_REG 0x11 +#define CKLED2001_ID 0x8A + +#define PDU_REG 0x13 +#define MSKSET_CA_CB_CHANNEL 0xAA +#define MSKCLR_CA_CB_CHANNEL 0x00 + +#define SCAN_PHASE_REG 0x14 +#define MSKPHASE_12CHANNEL 0x00 +#define MSKPHASE_11CHANNEL 0x01 +#define MSKPHASE_10CHANNEL 0x02 +#define MSKPHASE_9CHANNEL 0x03 +#define MSKPHASE_8CHANNEL 0x04 +#define MSKPHASE_7CHANNEL 0x05 +#define MSKPHASE_6CHANNEL 0x06 +#define MSKPHASE_5CHANNEL 0x07 +#define MSKPHASE_4CHANNEL 0x08 +#define MSKPHASE_3CHANNEL 0x09 +#define MSKPHASE_2CHANNEL 0x0A +#define MSKPHASE_1CHANNEL 0x0B + +#define SLEW_RATE_CONTROL_MODE1_REG 0x15 +#define MSKPWM_DELAY_PHASE_ENABLE 0x04 +#define MSKPWM_DELAY_PHASE_DISABLE 0x00 + +#define SLEW_RATE_CONTROL_MODE2_REG 0x16 +#define MSKDRIVING_SINKING_CHHANNEL_SLEWRATE_ENABLE 0xC0 +#define MSKDRIVING_SINKING_CHHANNEL_SLEWRATE_DISABLE 0x00 + +#define OPEN_SHORT_ENABLE_REG 0x17 +#define MSKOPEN_DETECTION_ENABLE (0x01 << 7) +#define MSKOPEN_DETECTION_DISABLE (0x00) + +#define MSKSHORT_DETECTION_ENABLE (0x01 << 6) +#define MSKSHORT_DETECTION_DISABLE (0x00) + +#define OPEN_SHORT_DUTY_REG 0x18 +#define OPEN_SHORT_FLAG_REG 0x19 + +#define MSKOPEN_DETECTION_INTERRUPT_ENABLE (0x01 << 7) +#define MSKOPEN_DETECTION_INTERRUPT_DISABLE (0x00) + +#define MSKSHORT_DETECTION_INTERRUPT_ENABLE (0x01 << 6) +#define MSKSHORT_DETECTION_INTERRUPT_DISABLE (0x00) + +#define SOFTWARE_SLEEP_REG 0x1A +#define MSKSLEEP_ENABLE 0x02 +#define MSKSLEEP_DISABLE 0x00 + +// LED Control Registers +#define LED_CONTROL_ON_OFF_FIRST_ADDR 0x0 +#define LED_CONTROL_ON_OFF_LAST_ADDR 0x17 +#define LED_CONTROL_ON_OFF_LENGTH ((LED_CONTROL_ON_OFF_LAST_ADDR - LED_CONTROL_ON_OFF_FIRST_ADDR) + 1) + +#define LED_CONTROL_OPEN_FIRST_ADDR 0x18 +#define LED_CONTROL_OPEN_LAST_ADDR 0x2F +#define LED_CONTROL_OPEN_LENGTH ((LED_CONTROL_OPEN_LAST_ADDR - LED_CONTROL_OPEN_FIRST_ADDR) + 1) + +#define LED_CONTROL_SHORT_FIRST_ADDR 0x30 +#define LED_CONTROL_SHORT_LAST_ADDR 0x47 +#define LED_CONTROL_SHORT_LENGTH ((LED_CONTROL_SHORT_LAST_ADDR - LED_CONTROL_SHORT_FIRST_ADDR) + 1) + +#define LED_CONTROL_PAGE_LENGTH 0x48 + +// LED Control Registers +#define LED_PWM_FIRST_ADDR 0x00 +#define LED_PWM_LAST_ADDR 0xBF +#define LED_PWM_LENGTH 0xC0 + +// Current Tune Registers +#define LED_CURRENT_TUNE_FIRST_ADDR 0x00 +#define LED_CURRENT_TUNE_LAST_ADDR 0x0B +#define LED_CURRENT_TUNE_LENGTH 0x0C + +#define A_1 0x00 +#define A_2 0x01 +#define A_3 0x02 +#define A_4 0x03 +#define A_5 0x04 +#define A_6 0x05 +#define A_7 0x06 +#define A_8 0x07 +#define A_9 0x08 +#define A_10 0x09 +#define A_11 0x0A +#define A_12 0x0B +#define A_13 0x0C +#define A_14 0x0D +#define A_15 0x0E +#define A_16 0x0F + +#define B_1 0x10 +#define B_2 0x11 +#define B_3 0x12 +#define B_4 0x13 +#define B_5 0x14 +#define B_6 0x15 +#define B_7 0x16 +#define B_8 0x17 +#define B_9 0x18 +#define B_10 0x19 +#define B_11 0x1A +#define B_12 0x1B +#define B_13 0x1C +#define B_14 0x1D +#define B_15 0x1E +#define B_16 0x1F + +#define C_1 0x20 +#define C_2 0x21 +#define C_3 0x22 +#define C_4 0x23 +#define C_5 0x24 +#define C_6 0x25 +#define C_7 0x26 +#define C_8 0x27 +#define C_9 0x28 +#define C_10 0x29 +#define C_11 0x2A +#define C_12 0x2B +#define C_13 0x2C +#define C_14 0x2D +#define C_15 0x2E +#define C_16 0x2F + +#define D_1 0x30 +#define D_2 0x31 +#define D_3 0x32 +#define D_4 0x33 +#define D_5 0x34 +#define D_6 0x35 +#define D_7 0x36 +#define D_8 0x37 +#define D_9 0x38 +#define D_10 0x39 +#define D_11 0x3A +#define D_12 0x3B +#define D_13 0x3C +#define D_14 0x3D +#define D_15 0x3E +#define D_16 0x3F + +#define E_1 0x40 +#define E_2 0x41 +#define E_3 0x42 +#define E_4 0x43 +#define E_5 0x44 +#define E_6 0x45 +#define E_7 0x46 +#define E_8 0x47 +#define E_9 0x48 +#define E_10 0x49 +#define E_11 0x4A +#define E_12 0x4B +#define E_13 0x4C +#define E_14 0x4D +#define E_15 0x4E +#define E_16 0x4F + +#define F_1 0x50 +#define F_2 0x51 +#define F_3 0x52 +#define F_4 0x53 +#define F_5 0x54 +#define F_6 0x55 +#define F_7 0x56 +#define F_8 0x57 +#define F_9 0x58 +#define F_10 0x59 +#define F_11 0x5A +#define F_12 0x5B +#define F_13 0x5C +#define F_14 0x5D +#define F_15 0x5E +#define F_16 0x5F + +#define G_1 0x60 +#define G_2 0x61 +#define G_3 0x62 +#define G_4 0x63 +#define G_5 0x64 +#define G_6 0x65 +#define G_7 0x66 +#define G_8 0x67 +#define G_9 0x68 +#define G_10 0x69 +#define G_11 0x6A +#define G_12 0x6B +#define G_13 0x6C +#define G_14 0x6D +#define G_15 0x6E +#define G_16 0x6F + +#define H_1 0x70 +#define H_2 0x71 +#define H_3 0x72 +#define H_4 0x73 +#define H_5 0x74 +#define H_6 0x75 +#define H_7 0x76 +#define H_8 0x77 +#define H_9 0x78 +#define H_10 0x79 +#define H_11 0x7A +#define H_12 0x7B +#define H_13 0x7C +#define H_14 0x7D +#define H_15 0x7E +#define H_16 0x7F + +#define I_1 0x80 +#define I_2 0x81 +#define I_3 0x82 +#define I_4 0x83 +#define I_5 0x84 +#define I_6 0x85 +#define I_7 0x86 +#define I_8 0x87 +#define I_9 0x88 +#define I_10 0x89 +#define I_11 0x8A +#define I_12 0x8B +#define I_13 0x8C +#define I_14 0x8D +#define I_15 0x8E +#define I_16 0x8F + +#define J_1 0x90 +#define J_2 0x91 +#define J_3 0x92 +#define J_4 0x93 +#define J_5 0x94 +#define J_6 0x95 +#define J_7 0x96 +#define J_8 0x97 +#define J_9 0x98 +#define J_10 0x99 +#define J_11 0x9A +#define J_12 0x9B +#define J_13 0x9C +#define J_14 0x9D +#define J_15 0x9E +#define J_16 0x9F + +#define K_1 0xA0 +#define K_2 0xA1 +#define K_3 0xA2 +#define K_4 0xA3 +#define K_5 0xA4 +#define K_6 0xA5 +#define K_7 0xA6 +#define K_8 0xA7 +#define K_9 0xA8 +#define K_10 0xA9 +#define K_11 0xAA +#define K_12 0xAB +#define K_13 0xAC +#define K_14 0xAD +#define K_15 0xAE +#define K_16 0xAF + +#define L_1 0xB0 +#define L_2 0xB1 +#define L_3 0xB2 +#define L_4 0xB3 +#define L_5 0xB4 +#define L_6 0xB5 +#define L_7 0xB6 +#define L_8 0xB7 +#define L_9 0xB8 +#define L_10 0xB9 +#define L_11 0xBA +#define L_12 0xBB +#define L_13 0xBC +#define L_14 0xBD +#define L_15 0xBE +#define L_16 0xBF \ No newline at end of file diff --git a/drivers/led/ckled2001.c b/drivers/led/ckled2001.c index 8d71805a24..dac61f7816 100644 --- a/drivers/led/ckled2001.c +++ b/drivers/led/ckled2001.c @@ -30,6 +30,10 @@ # define PHASE_CHANNEL MSKPHASE_12CHANNEL #endif +#ifndef CKLED2001_CURRENT_TUNE +# define CKLED2001_CURRENT_TUNE {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} +#endif + // Transfer buffer for TWITransmitData() uint8_t g_twi_transfer_buffer[20]; @@ -123,18 +127,10 @@ void CKLED2001_init(uint8_t addr) { } // Set CURRENT PAGE (Page 4) + uint8_t current_tuen_reg_list[LED_CURRENT_TUNE_LENGTH] = CKLED2001_CURRENT_TUNE; CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, CURRENT_TUNE_PAGE); for (int i = 0; i < LED_CURRENT_TUNE_LENGTH; i++) { - switch (i) { - case 2: - case 5: - case 8: - case 11: - CKLED2001_write_register(addr, i, 0xA0); - break; - default: - CKLED2001_write_register(addr, i, 0xFF); - } + CKLED2001_write_register(addr, i, current_tuen_reg_list[i]); } // Enable LEDs ON/OFF @@ -220,18 +216,20 @@ void CKLED2001_update_led_control_registers(uint8_t addr, uint8_t index) { g_led_control_registers_update_required[index] = false; } -void CKLED2001_return_normal(uint8_t addr) { +void CKLED2001_sw_return_normal(uint8_t addr) { // Select to function page CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); // Setting LED driver to normal mode CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_NORMAL_MODE); } -void CKLED2001_shutdown(uint8_t addr) { +void CKLED2001_sw_shutdown(uint8_t addr) { // Select to function page CKLED2001_write_register(addr, CONFIGURE_CMD_PAGE, FUNCTION_PAGE); // Setting LED driver to shutdown mode CKLED2001_write_register(addr, CONFIGURATION_REG, MSKSW_SHUT_DOWN_MODE); // Write SW Sleep Register CKLED2001_write_register(addr, SOFTWARE_SLEEP_REG, MSKSLEEP_ENABLE); -} +} + + diff --git a/drivers/led/ckled2001.h b/drivers/led/ckled2001.h index 1967961d20..7d5ad34f95 100644 --- a/drivers/led/ckled2001.h +++ b/drivers/led/ckled2001.h @@ -45,8 +45,8 @@ void CKLED2001_set_led_control_register(uint8_t index, bool red, bool green, boo void CKLED2001_update_pwm_buffers(uint8_t addr, uint8_t index); void CKLED2001_update_led_control_registers(uint8_t addr, uint8_t index); -void CKLED2001_return_normal(uint8_t addr); -void CKLED2001_shutdown(uint8_t addr); +void CKLED2001_sw_return_normal(uint8_t addr); +void CKLED2001_sw_shutdown(uint8_t addr); // Registers Page Define #define CONFIGURE_CMD_PAGE 0xFD diff --git a/keyboards/keychron/bluetooth/bat_level_animation.c b/keyboards/keychron/bluetooth/bat_level_animation.c new file mode 100644 index 0000000000..94a21f11e1 --- /dev/null +++ b/keyboards/keychron/bluetooth/bat_level_animation.c @@ -0,0 +1,128 @@ + +#include "quantum.h" +#include "bluetooth.h" +#include "indicator.h" +#include "lpm.h" +#if defined(PROTOCOL_CHIBIOS) +# include +#elif if defined(PROTOCOL_LUFA) +# include "lufa.h" +#endif + +#ifndef BAT_LEVEL_GROWING_INTERVAL +# define BAT_LEVEL_GROWING_INTERVAL 150 +#endif + +#ifndef BAT_LEVEL_ON_INTERVAL +# define BAT_LEVEL_ON_INTERVAL 3000 +#endif + +enum { + BAT_LVL_ANI_NONE, + BAT_LVL_ANI_GROWING, + BAT_LVL_ANI_BLINK_OFF, + BAT_LVL_ANI_BLINK_ON, +}; + +static uint8_t animation_state; +static uint32_t bat_lvl_ani_timer_buffer; +static uint8_t bat_percentage; +static uint8_t cur_percentage; +static uint8_t bat_lvl_led_list[10] = BAT_LEVEL_LED_LIST; +static uint32_t time_interval; +#ifdef RGB_MATRIX_ENABLE +static uint8_t r, g, b; +#endif + +extern indicator_config_t indicator_config; +extern backlight_state_t original_backlight_state; + +void bat_level_animiation_start(uint8_t percentage) { + /* Turn on backlight mode for indicator */ + indicator_enable(); + + animation_state = BAT_LVL_ANI_GROWING; + bat_percentage = percentage; + bat_lvl_ani_timer_buffer = sync_timer_read32(); + cur_percentage = 0; + time_interval = BAT_LEVEL_GROWING_INTERVAL; +#ifdef RGB_MATRIX_ENABLE + r = g = b = 255; +#endif +} + +void bat_level_animiation_stop(void) { + animation_state = BAT_LVL_ANI_NONE; +} + +bool bat_level_animiation_actived(void) { + return animation_state; +} + +void bat_level_animiation_indicate(void) { +#ifdef LED_MATRIX_ENABLE + for (uint8_t i = 0; i <= DRIVER_LED_TOTAL; i++) { + led_matrix_set_value(i, 0); + } + + if (animation_state == BAT_LVL_ANI_GROWING || animation_state == BAT_LVL_ANI_BLINK_ON) + for (uint8_t i = 0; i < cur_percentage / 10; i++) + led_matrix_set_value(bat_lvl_led_list[i], 255); +#endif + +#ifdef RGB_MATRIX_ENABLE + for (uint8_t i = 0; i <= DRIVER_LED_TOTAL; i++) { + rgb_matrix_set_color(i, 0, 0, 0); + } + + if (animation_state == BAT_LVL_ANI_GROWING || animation_state == BAT_LVL_ANI_BLINK_ON) + for (uint8_t i = 0; i < cur_percentage / 10; i++) { + rgb_matrix_set_color(bat_lvl_led_list[i], r, g, b); + } +#endif +} + +void bat_level_animiation_update(void) { + switch (animation_state) { + case BAT_LVL_ANI_GROWING: + if (cur_percentage < bat_percentage) + cur_percentage += 10; + else { + if (cur_percentage == 0) cur_percentage = 10; + animation_state = BAT_LVL_ANI_BLINK_OFF; + } + break; + + case BAT_LVL_ANI_BLINK_OFF: +#ifdef RGB_MATRIX_ENABLE + if (bat_percentage < 30) { + r = 255; + b = g = 0; + } else { + r = b = 0; + g = 255; + } +#endif + time_interval = BAT_LEVEL_ON_INTERVAL; + animation_state = BAT_LVL_ANI_BLINK_ON; + break; + + case BAT_LVL_ANI_BLINK_ON: + animation_state = BAT_LVL_ANI_NONE; + if (indicator_config.value == 0 && indicator_is_backlit_enabled_eeprom()) { + indicator_disable(); + } + break; + + default: + break; + } + + bat_lvl_ani_timer_buffer = sync_timer_read32(); +} + +void bat_level_animiation_task(void) { + if (animation_state && sync_timer_elapsed32(bat_lvl_ani_timer_buffer) > time_interval) { + bat_level_animiation_update(); + } +} diff --git a/keyboards/keychron/bluetooth/bat_level_animation.h b/keyboards/keychron/bluetooth/bat_level_animation.h new file mode 100644 index 0000000000..716e924103 --- /dev/null +++ b/keyboards/keychron/bluetooth/bat_level_animation.h @@ -0,0 +1,23 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +void bat_level_animiation_start(uint8_t percentage); +void bat_level_animiation_stop(void); +bool bat_level_animiation_actived(void); +void bat_level_animiation_indicate(void); +void bat_level_animiation_task(void); diff --git a/keyboards/keychron/bluetooth/battery.c b/keyboards/keychron/bluetooth/battery.c new file mode 100644 index 0000000000..6dd453357e --- /dev/null +++ b/keyboards/keychron/bluetooth/battery.c @@ -0,0 +1,107 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 "quantum.h" +#include "bluetooth.h" +#include "battery.h" +#include "transport.h" +#include "ckbt51.h" +#include "lpm.h" + +#define BATTERY_EMPTY_COUNT 10 +#define CRITICAL_LOW_COUNT 20 + +static uint32_t bat_monitor_timer_buffer = 0; +static uint16_t voltage = FULL_VOLTAGE_VALUE; +static uint8_t bat_empty = 0; +static uint8_t critical_low = 0; +static uint8_t bat_state; + +void battery_init(void) { + bat_monitor_timer_buffer = 0; + bat_state = BAT_NOT_CHARGING; +} +__attribute__((weak)) void battery_measure(void) {} + +/* Calculate the voltage */ +__attribute__((weak)) void battery_calculate_voltage(uint16_t value) {} + +void battery_set_voltage(uint16_t value) { + voltage = value; +} + +uint16_t battery_get_voltage(void) { + return voltage; +} + +uint8_t battery_get_percentage(void) { + if (voltage > FULL_VOLTAGE_VALUE) return 100; + + if (voltage > EMPTY_VOLTAGE_VALUE) { + return ((uint32_t)voltage - EMPTY_VOLTAGE_VALUE) * 80 / (FULL_VOLTAGE_VALUE - EMPTY_VOLTAGE_VALUE) + 20; + } + + if (voltage > SHUTDOWN_VOLTAGE_VALUE) { + return ((uint32_t)voltage - SHUTDOWN_VOLTAGE_VALUE) * 20 / (EMPTY_VOLTAGE_VALUE - SHUTDOWN_VOLTAGE_VALUE); + } else + return 0; +} + +bool battery_is_empty(void) { + return bat_empty > BATTERY_EMPTY_COUNT; +} + +bool battery_is_critical_low(void) { + return critical_low > CRITICAL_LOW_COUNT; +} + +void battery_check_empty(void) { + if (voltage < EMPTY_VOLTAGE_VALUE) { + if (bat_empty <= BATTERY_EMPTY_COUNT) { + if (++bat_empty > BATTERY_EMPTY_COUNT) indicator_battery_low_enable(true); + } + } else if (bat_empty <= BATTERY_EMPTY_COUNT) { + bat_empty = BATTERY_EMPTY_COUNT; + } +} + +void battery_check_critical_low(void) { + if (voltage < SHUTDOWN_VOLTAGE_VALUE) { + if (critical_low <= CRITICAL_LOW_COUNT) { + if (++critical_low > CRITICAL_LOW_COUNT) bluetooth_low_battery_shutdown(); + } + } else if (critical_low <= CRITICAL_LOW_COUNT) { + critical_low = 0; + } +} + +void battery_task(void) { + if (get_transport() == TRANSPORT_BLUETOOTH && bluetooth_get_state() == BLUETOOTH_CONNECTED) { + if (sync_timer_elapsed32(bat_monitor_timer_buffer) > VOLTAGE_MEASURE_INTERVAL) { + battery_check_empty(); + battery_check_critical_low(); + + bat_monitor_timer_buffer = sync_timer_read32(); + battery_measure(); + } + } + + if ((bat_empty || critical_low) && usb_power_connected()) { + bat_empty = false; + critical_low = false; + indicator_battery_low_enable(false); + } +} diff --git a/keyboards/keychron/bluetooth/battery.h b/keyboards/keychron/bluetooth/battery.h new file mode 100644 index 0000000000..5d00a529c6 --- /dev/null +++ b/keyboards/keychron/bluetooth/battery.h @@ -0,0 +1,51 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +enum { + BAT_NOT_CHARGING = 0, + BAT_CHARGING, + BAT_CHARGING_FINISHED, +}; + +#ifndef FULL_VOLTAGE_VALUE +# define FULL_VOLTAGE_VALUE 4100 +#endif + +#ifndef EMPTY_VOLTAGE_VALUE +# define EMPTY_VOLTAGE_VALUE 3500 +#endif + +#ifndef SHUTDOWN_VOLTAGE_VALUE +# define SHUTDOWN_VOLTAGE_VALUE 3300 +#endif + +#ifndef VOLTAGE_MEASURE_INTERVAL +# define VOLTAGE_MEASURE_INTERVAL 3000 +#endif + +void battery_init(void); +void battery_measure(void); +void battery_calculte_voltage(uint16_t value); +void battery_set_voltage(uint16_t value); +uint16_t battery_get_voltage(void); +uint8_t battery_get_percentage(void); +void indicator_battery_low_enable(bool enable); +bool battery_is_empty(void); +bool battery_is_critical_low(void); + +void battery_task(void); diff --git a/keyboards/keychron/bluetooth/bluetooth.c b/keyboards/keychron/bluetooth/bluetooth.c new file mode 100644 index 0000000000..ba71b2b05a --- /dev/null +++ b/keyboards/keychron/bluetooth/bluetooth.c @@ -0,0 +1,432 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 "quantum.h" +#include "bluetooth.h" +#include "report_buffer.h" +#include "lpm.h" +#include "battery.h" +#include "indicator.h" +#include "transport.h" + +extern uint8_t pairing_indication; +extern host_driver_t chibios_driver; +extern report_buffer_t kb_rpt; +extern uint32_t retry_time_buffer; +extern uint8_t retry; + +static uint8_t host_index = 0; +static uint8_t led_state = 0; + +extern bluetooth_transport_t bluetooth_transport; +static bluetooth_state_t bt_state = BLUETOOTH_RESET; +static bool pincodeEntry = false; +uint8_t bluetooth_report_protocol = true; + +/* declarations */ +uint8_t bluetooth_keyboard_leds(void); +void bluetooth_send_keyboard(report_keyboard_t *report); +void bluetooth_send_mouse(report_mouse_t *report); +void bluetooth_send_system(uint16_t data); +void bluetooth_send_consumer(uint16_t data); +/* host struct */ +host_driver_t bluetooth_driver = {bluetooth_keyboard_leds, bluetooth_send_keyboard, bluetooth_send_mouse, bluetooth_send_system, bluetooth_send_consumer}; + +#define BLUETOOTH_EVENT_QUEUE_SIZE 16 +bluetooth_event_t bt_event_queue[BLUETOOTH_EVENT_QUEUE_SIZE]; +uint8_t bt_event_queue_head; +uint8_t bt_event_queue_tail; + +void bluetooth_bt_event_queue_init(void) { + // Initialise the event queue + memset(&bt_event_queue, 0, sizeof(bt_event_queue)); + bt_event_queue_head = 0; + bt_event_queue_tail = 0; +} + +bool bluetooth_event_queue_enqueue(bluetooth_event_t event) { + uint8_t next = (bt_event_queue_head + 1) % BLUETOOTH_EVENT_QUEUE_SIZE; + if (next == bt_event_queue_tail) { + /* Override the first report */ + bt_event_queue_tail = (bt_event_queue_tail + 1) % BLUETOOTH_EVENT_QUEUE_SIZE; + } + bt_event_queue[bt_event_queue_head] = event; + bt_event_queue_head = next; + return true; +} + +static inline bool bluetooth_event_queue_dequeue(bluetooth_event_t *event) { + if (bt_event_queue_head == bt_event_queue_tail) { + return false; + } + *event = bt_event_queue[bt_event_queue_tail]; + bt_event_queue_tail = (bt_event_queue_tail + 1) % BLUETOOTH_EVENT_QUEUE_SIZE; + return true; +} + +/* + * Bluetooth init. + */ +void bluetooth_init(void) { + bt_state = BLUETOOTH_INITIALIZED; + + bluetooth_bt_event_queue_init(); +#ifndef DISABLE_REPORT_BUFFER + report_buffer_init(); +#endif + indicator_init(); +#ifdef BLUETOOTH_INT_INPUT_PIN + setPinInputHigh(BLUETOOTH_INT_INPUT_PIN); +#endif + + lpm_init(); +} + +/* + * Bluetooth trasponrt init. Bluetooth module driver shall use this function to register a callback + * to its implementation. + */ +void bluetooth_set_transport(bluetooth_transport_t *transport) { + if (transport) memcpy(&bluetooth_transport, transport, sizeof(bluetooth_transport_t)); +} + +/* + * Enter pairing with current host index + */ +void bluetooth_pairing(void) { + if (battery_is_critical_low()) return; + + bluetooth_pairing_ex(0, NULL); + bt_state = BLUETOOTH_PARING; +} + +/* + * Enter pairing with specified host index and param + */ +void bluetooth_pairing_ex(uint8_t host_idx, void *param) { + if (battery_is_critical_low()) return; + + if (bluetooth_transport.pairing_ex) bluetooth_transport.pairing_ex(host_idx, param); + bt_state = BLUETOOTH_PARING; + + host_index = host_idx; +} + +/* + * Initiate connection request to paired host + */ +void bluetooth_connect(void) { + /* Work around empty report after wakeup, which leads to reconneect/disconnected loop */ + if (battery_is_critical_low() || sync_timer_read32() == 0) return; + + bluetooth_transport.connect_ex(0, 0); + bt_state = BLUETOOTH_RECONNECTING; +} + +/* + * Initiate connection request to paired host with argument + */ +void bluetooth_connect_ex(uint8_t host_idx, uint16_t timeout) { + if (battery_is_critical_low()) return; + + if (host_idx != 0) { + if (host_index == host_idx && bt_state == BLUETOOTH_CONNECTED) return; + host_index = host_idx; + led_state = 0; + } + bluetooth_transport.connect_ex(host_idx, timeout); + bt_state = BLUETOOTH_RECONNECTING; +} + +/* Initiate a disconnection */ +void bluetooth_disconnect(void) { + if (bluetooth_transport.disconnect) bluetooth_transport.disconnect(); +} + + +/* Called when the BT device is reset. */ +static void bluetooth_enter_reset(uint8_t reason) { + bt_state = BLUETOOTH_RESET; + bluetooth_enter_reset_kb(reason); +} + +/* Enters discoverable state. Upon entering this state we perform the following actions: + * - change state to BLUETOOTH_PARING + * - set pairing indication + */ +static void bluetooth_enter_discoverable(uint8_t host_idx) { + bt_state = BLUETOOTH_PARING; + indicator_set(bt_state, host_idx); + bluetooth_enter_discoverable_kb(host_idx); +} + +/* + * Enters reconnecting state. Upon entering this state we perform the following actions: + * - change state to RECONNECTING + * - set reconnect indication + */ +static void bluetooth_enter_reconnecting(uint8_t host_idx) { + bt_state = BLUETOOTH_RECONNECTING; + indicator_set(bt_state, host_idx); + bluetooth_enter_reconnecting_kb(host_idx); +} + +/* Enters connected state. Upon entering this state we perform the following actions: + * - change state to CONNECTED + * - set connected indication + * - enable bluetooth NKRO is support + */ +static void bluetooth_enter_connected(uint8_t host_idx) { + bt_state = BLUETOOTH_CONNECTED; + indicator_set(bt_state, host_idx); + host_index = host_idx; + + clear_keyboard(); + + /* Enable NKRO since it may be disabled in pin code entry */ +#if defined(NKRO_ENABLE) && defined(BLUETOOTH_NKRO_ENABLE) + keymap_config.nkro = true; +#else + keymap_config.nkro = false; +#endif + + bluetooth_enter_connected_kb(host_idx); + + if (battery_is_empty()) indicator_battery_low_enable(true); +} + +/* Enters disconnected state. Upon entering this state we perform the following actions: + * - change state to DISCONNECTED + * - set disconnected indication + */ +static void bluetooth_enter_disconnected(uint8_t host_idx) { + uint8_t previous_state = bt_state; + bt_state = BLUETOOTH_DISCONNECTED; + + if (previous_state == BLUETOOTH_CONNECTED) { + lpm_timer_reset(); + indicator_set(BLUETOOTH_SUSPEND, host_idx); + } else + indicator_set(bt_state, host_idx); + +#ifndef DISABLE_REPORT_BUFFER + report_buffer_init(); +#endif + retry = 0; + bluetooth_enter_disconnected_kb(host_idx); + indicator_battery_low_enable(false); +} + + +/* Enter pin code entry state. */ +static void bluetooth_enter_pin_code_entry(void) { +#if defined(NKRO_ENABLE) + keymap_config.nkro = FALSE; +#endif + pincodeEntry = true; + bluetooth_enter_pin_code_entry_kb(); +} + +/* Exit pin code entry state. */ +static void bluetooth_exit_pin_code_entry(void) { +#if defined(NKRO_ENABLE) + keymap_config.nkro = true; +#endif + pincodeEntry = false; + bluetooth_exit_pin_code_entry_kb(); +} + +__attribute__((weak)) void bluetooth_enter_reset_kb(uint8_t reason){}; +__attribute__((weak)) void bluetooth_enter_discoverable_kb(uint8_t host_idx){}; +__attribute__((weak)) void bluetooth_enter_reconnecting_kb(uint8_t host_idx){}; +__attribute__((weak)) void bluetooth_enter_connected_kb(uint8_t host_idx){}; +__attribute__((weak)) void bluetooth_enter_disconnected_kb(uint8_t host_idx){}; +__attribute__((weak)) void bluetooth_enter_pin_code_entry_kb(void) {} +__attribute__((weak)) void bluetooth_exit_pin_code_entry_kb(void){}; + +/* */ +static void bluetooth_hid_set_protocol(bool report_protocol) { + bluetooth_report_protocol = false; +} + +uint8_t bluetooth_keyboard_leds(void) { + if (bt_state == BLUETOOTH_CONNECTED) { + return led_state; + } + + return 0; +} + +extern keymap_config_t keymap_config; + +void bluetooth_send_keyboard(report_keyboard_t *report) { + if (bt_state == BLUETOOTH_PARING && !pincodeEntry) return; + + if (bt_state == BLUETOOTH_CONNECTED || (bt_state == BLUETOOTH_PARING && pincodeEntry)) { +# if defined(NKRO_ENABLE) + if (bluetooth_report_protocol && keymap_config.nkro) { + if (bluetooth_transport.send_nkro) { +# ifndef DISABLE_REPORT_BUFFER + bool firstBuffer = false; + if (report_buffer_is_empty() && report_buffer_next_inverval() && report_buffer_get_retry() == 0) { + firstBuffer = true; + } + + report_buffer_t report_buffer; + report_buffer.type = REPORT_TYPE_NKRO; + memcpy(&report_buffer.keyboard, report, sizeof(report_keyboard_t)); + report_buffer_enqueue(&report_buffer); + + if (firstBuffer) { + report_buffer_set_retry(0); + report_buffer_task(); + } +# else + bluetooth_transport.send_nkro(&report->nkro.mods); +# endif + } + } else +# endif + { + //#ifdef KEYBOARD_SHARED_EP + if (bluetooth_transport.send_keyboard) { +# ifndef DISABLE_REPORT_BUFFER + if (report_buffer_is_empty() && report_buffer_next_inverval()) { + bluetooth_transport.send_keyboard(&report->mods); + report_buffer_update_timer(); + } else { + report_buffer_t report_buffer; + report_buffer.type = REPORT_TYPE_KB; + memcpy(&report_buffer.keyboard, report, sizeof(report_keyboard_t)); + report_buffer_enqueue(&report_buffer); + } +# else + bluetooth_transport.send_keyboard(&report->mods); +# endif + } + //#endif + } + + } else if (bt_state != BLUETOOTH_RESET) { + bluetooth_connect(); + } +} + +void bluetooth_send_mouse(report_mouse_t *report) { + if (bt_state == BLUETOOTH_CONNECTED) { + if (bluetooth_transport.send_mouse) bluetooth_transport.send_mouse((uint8_t *)report); + } else if (bt_state != BLUETOOTH_RESET) { + bluetooth_connect(); + } +} + +void bluetooth_send_system(uint16_t data) { + if (bt_state == BLUETOOTH_CONNECTED) { + if (bluetooth_transport.send_system) bluetooth_transport.send_system(data); + } else if (bt_state != BLUETOOTH_RESET) { + bluetooth_connect(); + } +} + +void bluetooth_send_consumer(uint16_t data) { + if (bt_state == BLUETOOTH_CONNECTED) { +#ifndef DISABLE_REPORT_BUFFER + if (report_buffer_is_empty() && report_buffer_next_inverval()) { + if (bluetooth_transport.send_consumer) bluetooth_transport.send_consumer(data); + report_buffer_update_timer(); + } else { + report_buffer_t report_buffer; + report_buffer.type = REPORT_TYPE_CONSUMER; + report_buffer.consumer = data; + report_buffer_enqueue(&report_buffer); + } +#else + if (bluetooth_transport.send_consumer) bluetooth_transport.send_consumer(data); +#endif + } else if (bt_state != BLUETOOTH_RESET) { + bluetooth_connect(); + } +} + +void bluetooth_low_battery_shutdown(void) { + indicator_battery_low_enable(false); + bluetooth_disconnect(); +} + +void bluetooth_event_queue_task(void) { + bluetooth_event_t event; + while (bluetooth_event_queue_dequeue(&event)) { + switch (event.evt_type) { + case EVT_RESET: + bluetooth_enter_reset(event.params.reason); + break; + case EVT_CONNECTED: + bluetooth_enter_connected(event.params.hostIndex); + break; + case EVT_DISCOVERABLE: + bluetooth_enter_discoverable(event.params.hostIndex); + break; + case EVT_RECONNECTING: + bluetooth_enter_reconnecting(event.params.hostIndex); + break; + case EVT_DISCONNECTED: + led_state = 0; + bluetooth_enter_disconnected(event.params.hostIndex); + break; + case EVT_PINCODE_ENTRY: + bluetooth_enter_pin_code_entry(); + break; + case EVT_EXIT_PINCODE_ENTRY: + bluetooth_exit_pin_code_entry(); + break; + case EVT_HID_INDICATOR: + led_state = event.params.led; + break; + case EVT_HID_SET_PROTOCOL: + bluetooth_hid_set_protocol(event.params.protocol); + break; + case EVT_CONECTION_INTERVAL: + report_buffer_set_inverval(event.params.interval); + break; + default: + break; + } + } +} + +void bluetooth_task(void) { + + bluetooth_transport.task(); + bluetooth_event_queue_task(); +#ifndef DISABLE_REPORT_BUFFER + report_buffer_task(); +#endif + indicator_task(); + battery_task(); + lpm_task(); +} + +bluetooth_state_t bluetooth_get_state(void) { + return bt_state; +}; + +bool process_record_kb(uint16_t keycode, keyrecord_t *record) { + if (get_transport() == TRANSPORT_BLUETOOTH) { + lpm_timer_reset(); + } + + return process_record_user(keycode, record); +} diff --git a/keyboards/keychron/bluetooth/bluetooth.h b/keyboards/keychron/bluetooth/bluetooth.h new file mode 100644 index 0000000000..b24df48845 --- /dev/null +++ b/keyboards/keychron/bluetooth/bluetooth.h @@ -0,0 +1,85 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +#include "bluetooth_event_type.h" + +/* Low power mode */ +#ifndef LOW_POWER_MODE +# define LOW_POWER_MODE PM_STOP1 +#endif + +/* Wake pin used for blueooth module/controller to wake up MCU in low power mode*/ +#ifndef BLUETOOTH_INT_INPUT_PIN +# define WAKE_PIN A5 +#endif + +/* Type of an enumeration of the possible BT state.*/ +typedef enum { + BLUETOOTH_RESET, + BLUETOOTH_INITIALIZED, // 1 + BLUETOOTH_DISCONNECTED, // 2 + BLUETOOTH_CONNECTED, // 3 + BLUETOOTH_PARING, // 4 + BLUETOOTH_RECONNECTING, // 5 + BLUETOOTH_SUSPEND +} bluetooth_state_t; + +extern event_listener_t bt_driver; + +typedef struct { + void (*init)(bool); + void (*connect_ex)(uint8_t, uint16_t); + void (*pairing_ex)(uint8_t, void *); + void (*disconnect)(void); + void (*send_keyboard)(uint8_t *); + void (*send_nkro)(uint8_t *); + void (*send_consumer)(uint16_t); + void (*send_system)(uint16_t); + void (*send_mouse)(uint8_t *); + void (*task)(void); +} bluetooth_transport_t; + +void bluetooth_init(void); +void bluetooth_set_transport(bluetooth_transport_t *transport); +void bluetooth_task(void); + +bool bluetooth_event_queue_enqueue(bluetooth_event_t event); + +void bluetooth_connect(void); +void bluetooth_connect_ex(uint8_t host_idx, uint16_t timeout); +void bluetooth_disconnect(void); + +void bluetooth_pairing(void); +void bluetooth_pairing_ex(uint8_t host_idx, void *param); +bool bluetooth_is_activated(void); + +void bluetooth_enter_reset_kb(uint8_t reason); +void bluetooth_enter_discoverable_kb(uint8_t host_idx); +void bluetooth_enter_reconnecting_kb(uint8_t host_idx); +void bluetooth_enter_connected_kb(uint8_t host_idx); +void bluetooth_enter_disconnected_kb(uint8_t host_idx); +void bluetooth_enter_pin_code_entry_kb(void); +void bluetooth_exit_pin_code_entry_kb(void); + +void bluetooth_task(void); +void bluetooth_pre_task(void); +void bluetooth_post_task(void); +bluetooth_state_t bluetooth_get_state(void); + +void bluetooth_low_battery_shutdown(void); + diff --git a/keyboards/keychron/bluetooth/bluetooth.mk b/keyboards/keychron/bluetooth/bluetooth.mk new file mode 100644 index 0000000000..22a22eef6e --- /dev/null +++ b/keyboards/keychron/bluetooth/bluetooth.mk @@ -0,0 +1,14 @@ +BLUETOOTH_DIR = bluetooth +SRC += \ + $(BLUETOOTH_DIR)/bluetooth.c \ + $(BLUETOOTH_DIR)/report_buffer.c \ + $(BLUETOOTH_DIR)/ckbt51.c \ + $(BLUETOOTH_DIR)/indicator.c \ + $(BLUETOOTH_DIR)/bluetooth_main.c \ + $(BLUETOOTH_DIR)/transport.c \ + $(BLUETOOTH_DIR)/lpm.c \ + $(BLUETOOTH_DIR)/lpm_stm32l432.c \ + $(BLUETOOTH_DIR)/battery.c \ + $(BLUETOOTH_DIR)/factory_test.c \ + $(BLUETOOTH_DIR)/bat_level_animation.c +VPATH += $(TOP_DIR)/keyboards/keychron/$(BLUETOOTH_DIR) diff --git a/keyboards/keychron/bluetooth/bluetooth_config.h b/keyboards/keychron/bluetooth/bluetooth_config.h new file mode 100644 index 0000000000..075e1a683c --- /dev/null +++ b/keyboards/keychron/bluetooth/bluetooth_config.h @@ -0,0 +1,32 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 BLUETOOTH_CONFIG_H +#define BLUETOOTH_CONFIG_H + +#include "config.h" + +// Uint: Second +#ifndef DISCONNECTED_BACKLIGHT_OFF_DELAY_TIME +# define DISCONNECTED_BACKLIGHT_OFF_DELAY_TIME 40 +#endif + +// Uint: Second, the timer restarts on key activities. +#ifndef CONNECTED_BACKLIGHT_OFF_DELAY_TIME +# define CONNECTED_BACKLIGHT_OFF_DELAY_TIME 600 +#endif + +#endif diff --git a/keyboards/keychron/bluetooth/bluetooth_event_type.h b/keyboards/keychron/bluetooth/bluetooth_event_type.h new file mode 100644 index 0000000000..8ce55d7616 --- /dev/null +++ b/keyboards/keychron/bluetooth/bluetooth_event_type.h @@ -0,0 +1,44 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +/* Type of an enumeration of the possible BT events.*/ +typedef enum { + EVT_NONE = 0, + EVT_RESET, + EVT_DISCOVERABLE, + EVT_RECONNECTING, + EVT_CONNECTED, + EVT_DISCONNECTED, + EVT_PINCODE_ENTRY, + EVT_EXIT_PINCODE_ENTRY, + EVT_HID_SET_PROTOCOL, + EVT_HID_INDICATOR, + EVT_CONECTION_INTERVAL, +} event_type_t; + +typedef struct { + event_type_t evt_type; /*The type of the event. */ + union { + uint8_t reason; /* Parameters to BLUETOOTH_RESET event */ + uint8_t hostIndex; /* Parameters to connection event from EVT_DISCOVERABLE to EVT_DISCONECTED */ + uint8_t led; /* Parameters to EVT_HID_INDICATOR event */ + uint8_t protocol; /* Parameters to EVT_HID_SET_PROTOCOL event */ + uint8_t interval; /* Parameters to EVT_CONECTION_INTERVAL event */ + } params; +} bluetooth_event_t; + diff --git a/keyboards/keychron/bluetooth/bluetooth_main.c b/keyboards/keychron/bluetooth/bluetooth_main.c new file mode 100644 index 0000000000..eabcc83826 --- /dev/null +++ b/keyboards/keychron/bluetooth/bluetooth_main.c @@ -0,0 +1,37 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 "quantum.h" +#include "bluetooth.h" +#include "transport.h" + +__attribute__((weak)) void bluetooth_pre_task(void) {} +__attribute__((weak)) void bluetooth_post_task(void) {} + +void bluetooth_tasks(void) { + bluetooth_pre_task(); + bluetooth_task(); + bluetooth_post_task(); + + /* usb_remote_wakeup() should be invoked last so that we have chance + * to switch to bluetooth after start-up when usb is not connected + */ + if (get_transport() == TRANSPORT_USB) usb_remote_wakeup(); +} + +void housekeeping_task_kb(void) { + bluetooth_tasks(); +} diff --git a/keyboards/keychron/bluetooth/ckbt51.c b/keyboards/keychron/bluetooth/ckbt51.c new file mode 100644 index 0000000000..617f00551d --- /dev/null +++ b/keyboards/keychron/bluetooth/ckbt51.c @@ -0,0 +1,575 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 "quantum.h" +#include "ckbt51.h" +#include "bluetooth.h" +#include "battery.h" +#include "raw_hid.h" +#include "report_buffer.h" + +#ifndef CKBT51_INT_INPUT_PIN +# error "CKBT51_INT_INPUT_PIN is not defined" +#endif + +#ifndef CKBT51_TX_RETRY_COUNT +# define CKBT51_TX_RETRY_COUNT 3 +#endif + +/* CKBT51 disable its uart peripheral to save power if uart inactivity for 3s, need to + * assert this pin and wait some time for its uart getting ready before sending data*/ +#define CKBT51_WAKE_WAIT_TIME 3000 // us + +enum { + /* HID Report */ + CKBT51_CMD_SEND_KB = 0x11, + CKBT51_CMD_SEND_KB_NKRO = 0x12, + CKBT51_CMD_SEND_CONSUMER = 0x13, + CKBT51_CMD_SEND_SYSTEM = 0x14, + CKBT51_CMD_SEND_FN = 0x15, // Not used currently + CKBT51_CMD_SEND_MOUSE = 0x16, // Not used currently + CKBT51_CMD_SEND_BOOT_KB = 0x17, + /* Bluetooth connections */ + CKBT51_CMD_PAIRING = 0x21, + CKBT51_CMD_CONNECT = 0x22, + CKBT51_CMD_DISCONNECT = 0x23, + CKBT51_CMD_SWITCH_HOST = 0x24, + CKBT51_CMD_READ_STATE_REG = 0x25, + /* Battery */ + CKBT51_CMD_BATTERY_MANAGE = 0x31, + CKBT51_CMD_UPDATE_BAT_LVL = 0x32, + /* Set/get parameters */ + CKBT51_CMD_GET_MODULE_INFO = 0x40, + CKBT51_CMD_SET_CONFIG = 0x41, + CKBT51_CMD_GET_CONFIG = 0x42, + CKBT51_CMD_SET_BDA = 0x43, + CKBT51_CMD_GET_BDA = 0x44, + CKBT51_CMD_SET_NAME = 0x45, + CKBT51_CMD_GET_NAME = 0x46, + /* DFU */ + CKBT51_CMD_GET_DFU_VER = 0x60, + CKBT51_CMD_HAND_SHAKE_TOKEN = 0x61, + CKBT51_CMD_START_DFU = 0x62, + CKBT51_CMD_SEND_FW_DATA = 0x63, + CKBT51_CMD_VERIFY_CRC32 = 0x64, + CKBT51_CMD_SWITCH_FW = 0x65, + /* Factory test */ + CKBT51_CMD_FACTORY_RESET = 0x71, + CKBT51_CMD_INT_PIN_TEST = 0x72, + /* Event */ + CKBT51_EVT_CKBT51_CMD_RECEIVED = 0xA1, + CKBT51_EVT_OTA_RSP = 0xA3, + CKBT51_CONNECTION_EVT_ACK = 0xA4, +}; + +enum { + CKBT51_EVT_ACK = 0xA1, + CKBT51_EVT_QUERY_RSP = 0xA2, + CKBT51_EVT_RESET = 0xB0, + CKBT51_EVT_LE_CONNECTION = 0xB1, + CKBT51_EVT_HOST_TYPE = 0xB2, + CKBT51_EVT_CONNECTION = 0xB3, + CKBT51_EVT_HID_EVENT = 0xB4, + CKBT51_EVT_BATTERY = 0xB5, +}; + +enum { + CKBT51_CONNECTED = 0x20, + CKBT51_DISCOVERABLE = 0x21, + CKBT51_RECONNECTING = 0x22, + CKBT51_DISCONNECTED = 0x23, + CKBT51_PINCODE_ENTRY = 0x24, + CKBT51_EXIT_PINCODE_ENTRY = 0x25 +}; + +enum { + ACK_SUCCESS = 0x00, + ACK_CHECKSUM_ERROR, + ACK_FIFO_HALF_WARNING, + ACK_FIFO_FULL_ERROR, +}; + +static uint8_t payload[PACKET_MAX_LEN]; +static uint8_t reg_offset = 0xFF; + +bluetooth_transport_t bluetooth_transport = { + ckbt51_init, + ckbt51_connect, + ckbt51_become_discoverable, + ckbt51_disconnect, + ckbt51_send_keyboard, + ckbt51_send_nkro, + ckbt51_send_consumer, + ckbt51_send_system, + NULL, + ckbt51_task +}; + +void ckbt51_init(bool wakeup_from_low_power_mode) { +#if (HAL_USE_SERIAL == TRUE) + SerialConfig config = {460800, 0, USART_CR2_STOP1_BITS, 0}; + + if (wakeup_from_low_power_mode) { + sdInit(); + sdStart(&BT_DRIVER, &config); + + return; + } + + sdStart(&BT_DRIVER, &config); + palSetPadMode(BT_DRIVER_UART_TX_BANK, BT_DRIVER_UART_TX, PAL_MODE_ALTERNATE(BT_DRIVER_UART_TX_PAL_MODE)); + palSetPadMode(BT_DRIVER_UART_RX_BANK, BT_DRIVER_UART_RX, PAL_MODE_ALTERNATE(BT_DRIVER_UART_RX_PAL_MODE)); +#endif + + setPinOutput(CKBT51_INT_INPUT_PIN); + writePinHigh(CKBT51_INT_INPUT_PIN); +} + +void ckbt51_send_cmd(uint8_t* payload, uint8_t len, bool ack_enable) { + static uint8_t sn = 1; + uint8_t i; + uint8_t pkt[PACKET_MAX_LEN] = {0}; + memset(pkt, 0, PACKET_MAX_LEN); + + systime_t start = 0; + + for (i=0; i< 3; i++) { + writePin(CKBT51_INT_INPUT_PIN, i % 2); + start = chVTGetSystemTime(); + while (chTimeI2US(chVTTimeElapsedSinceX(start)) < CKBT51_WAKE_WAIT_TIME / 3) {}; + } + writePinHigh(CKBT51_INT_INPUT_PIN); + + uint16_t checksum = 0; + for (i = 0; i < len; i++) checksum += payload[i]; + + i = 0; + pkt[i++] = 0xAA; + pkt[i++] = ack_enable ? 0x56 : 0x55; + pkt[i++] = len + 2; + pkt[i++] = ~(len + 2) & 0xFF; + pkt[i++] = sn++; + memcpy(pkt + i, payload, len); + i += len; + pkt[i++] = checksum & 0xFF; + pkt[i++] = (checksum >> 8) & 0xFF; + + sdWrite(&BT_DRIVER, pkt, i); + + if (sn == 0) sn = 1; +} + +void ckbt51_send_keyboard(uint8_t* report) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_SEND_KB; + memcpy(payload + i, report, 8); + i += 8; + + ckbt51_send_cmd(payload, i, true); +} + +void ckbt51_send_nkro(uint8_t* report) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_SEND_KB_NKRO; + memcpy(payload + i, report, 20); // NKRO report lenght is limited to 20 bytes + i += 20; + + ckbt51_send_cmd(payload, i, true); +} + +void ckbt51_send_consumer(uint16_t report) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_SEND_CONSUMER; + payload[i++] = report & 0xFF; + payload[i++] = ((report) >> 8) & 0xFF; + i += 4; // QMK doesn't send multiple consumer reports, just skip 2nd and 3rd consumer reports + + ckbt51_send_cmd(payload, i, true); +} + +void ckbt51_send_system(uint16_t report) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_SEND_SYSTEM; + payload[i++] = report & 0xFF; + + ckbt51_send_cmd(payload, i, true); +} + +/* Send ack to connection event, bluetooth module will retry 2 times if no ack received */ +void ckbt51_send_conn_evt_ack(void) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CONNECTION_EVT_ACK; + + ckbt51_send_cmd(payload, i, false); +} + +void ckbt51_become_discoverable(uint8_t host_idx, void* param) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + pairing_param_t default_pairing_param = {0, 0, PAIRING_MODE_LESC_OR_SSP, BT_MODE_CLASSIC, 0, NULL}; + + if (param == NULL) { + param = &default_pairing_param; + } + pairing_param_t* p = (pairing_param_t*)param; + + payload[i++] = CKBT51_CMD_PAIRING; // Cmd type + payload[i++] = host_idx; // Host Index + payload[i++] = p->timeout & 0xFF; // Timeout + payload[i++] = (p->timeout >> 8) & 0xFF; + payload[i++] = p->pairingMode; + payload[i++] = p->BRorLE; // BR/LE + payload[i++] = p->txPower; // LE TX POWER + if (p->leName) { + memcpy(&payload[i], p->leName, strlen(p->leName)); + i += strlen(p->leName); + } + + ckbt51_send_cmd(payload, i, true); +} + +/* Timeout : 2 ~ 255 seconds */ +void ckbt51_connect(uint8_t hostIndex, uint16_t timeout) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_CONNECT; + payload[i++] = hostIndex; // Host index + payload[i++] = timeout & 0xFF; // Timeout + payload[i++] = (timeout >> 8) & 0xFF; + + ckbt51_send_cmd(payload, i, true); +} + +void ckbt51_disconnect(void) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_DISCONNECT; + payload[i++] = 0; // Sleep mode + + ckbt51_send_cmd(payload, i, true); +} + +void ckbt51_switch_host(uint8_t hostIndex) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_SWITCH_HOST; + payload[i++] = hostIndex; + + ckbt51_send_cmd(payload, i, true); +} + +void ckbt51_read_state_reg(uint8_t reg, uint8_t len) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_READ_STATE_REG; + payload[i++] = reg_offset = reg; + payload[i++] = len; + + // TODO + ckbt51_send_cmd(payload, i, false); +} + +void ckbt51_get_info(module_info_t* info) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_GET_MODULE_INFO; + ckbt51_send_cmd(payload, i, false); +} + +void ckbt51_set_param(module_param_t* param) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_SET_CONFIG; + memcpy(payload + i, param, sizeof(module_param_t)); + i += sizeof(module_param_t); + + ckbt51_send_cmd(payload, i, false); +} + +void ckbt51_get_param(module_param_t* param) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_GET_CONFIG; + + ckbt51_send_cmd(payload, i, false); +} + +void ckbt51_set_local_name(const char* name) { + uint8_t i = 0; + uint8_t len = strlen(name); + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_SET_NAME; + memcpy(payload + i, name, len); + i += len; + ckbt51_send_cmd(payload, i, false); +} + +void ckbt51_get_local_name(char* name) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_GET_NAME; + + ckbt51_send_cmd(payload, i, false); +} + +void ckbt51_factory_reset(void) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + + payload[i++] = CKBT51_CMD_FACTORY_RESET; + + ckbt51_send_cmd(payload, i, false); +} + +void ckbt51_int_pin_test(bool enable) { + uint8_t i = 0; + memset(payload, 0, PACKET_MAX_LEN); + payload[i++] = CKBT51_CMD_INT_PIN_TEST; + payload[i++] = enable; + + ckbt51_send_cmd(payload, i, false); +} + +void ckbt51_dfu_tx(uint8_t rsp, uint8_t* data, uint8_t len, uint8_t sn) { + uint16_t checksum = 0; + uint8_t buf[RAW_EPSIZE] = {0}; + uint8_t i = 0; + + buf[i++] = 0x03; + buf[i++] = 0xAA; + buf[i++] = 0x57; + buf[i++] = len; + buf[i++] = ~len; + buf[i++] = sn; + buf[i++] = rsp; + memcpy(&buf[i], data, len); + i += len; + + for (uint8_t k = 0; k < i; k++) checksum += buf[i]; + + raw_hid_send(buf, RAW_EPSIZE); + + if (len > 25) { + i = 0; + memset(buf, 0, RAW_EPSIZE); + buf[i++] = 0x03; + memcpy(&buf[i], data + 25, len - 25); + i = i + len - 25; + raw_hid_send(buf, RAW_EPSIZE); + } +} + +void ckbt51_dfu_rx(uint8_t* data, uint8_t length) { + if (data[0] == 0xAA && (data[1] == 0x55 || data[1] == 0x56) && data[2] == (~data[3] & 0xFF)) { + uint16_t checksum = 0; + uint8_t payload_len = data[2]; + + /* Check payload_len validity */ + if (payload_len > RAW_EPSIZE - PACKECT_HEADER_LEN) return; + + uint8_t* payload = &data[PACKECT_HEADER_LEN]; + + for (uint8_t i = 0; i < payload_len - 2; i++) { + checksum += payload[i]; + } + + /* Verify checksum */ + if ((checksum & 0xFF) != payload[payload_len - 2] || checksum >> 8 != payload[payload_len - 1]) return; + + if ((payload[0] & 0xF0) == 0x60) { + ckbt51_send_cmd(payload, payload_len - 2, data[1] == 0x56); + } + } +} + +static void ack_handler(uint8_t* data, uint8_t len) { + switch (data[1]) { + case CKBT51_CMD_SEND_KB: + case CKBT51_CMD_SEND_KB_NKRO: + case CKBT51_CMD_SEND_CONSUMER: + case CKBT51_CMD_SEND_SYSTEM: + case CKBT51_CMD_SEND_MOUSE: + switch (data[2]) { + case ACK_SUCCESS: + report_buffer_set_retry(0); + report_buffer_set_inverval(DEFAULT_REPORT_INVERVAL_MS); + break; + case ACK_FIFO_HALF_WARNING: + report_buffer_set_retry(0); + report_buffer_set_inverval(DEFAULT_REPORT_INVERVAL_MS + 5); + break; + case ACK_FIFO_FULL_ERROR: + report_buffer_set_retry(10); + break; + } + break; + default: break; + } +} + +static void query_rsp_handler(uint8_t* data, uint8_t len) { + + if (data[2]) return; + + switch (data[1]) { + case CKBT51_CMD_READ_STATE_REG: + switch (reg_offset) { + case 0x05: + battery_calculte_voltage(data[3] | (data[4] << 8)); + break; + } + reg_offset = 0xFF; + break; + default: + break; + } +} + +static void ckbt51_event_handler(uint8_t evt_type, uint8_t* data, uint8_t len, uint8_t sn) { + bluetooth_event_t event = {0}; + + switch (evt_type) { + case CKBT51_EVT_ACK: + ack_handler(data, len); + break; + case CKBT51_EVT_RESET: + dprintf("CKBT51_EVT_RESET\n"); + event.evt_type = EVT_RESET; + event.params.reason = data[0]; + break; + case CKBT51_EVT_LE_CONNECTION: + dprintf("CKBT51_EVT_LE_CONNECTION\n"); + break; + case CKBT51_EVT_HOST_TYPE: + dprintf("CKBT51_EVT_HOST_TYPE\n"); + break; + case CKBT51_EVT_CONNECTION: + dprintf("CKBT51_EVT_CONNECTION %d\n", data[0]); + /* Only connection status change message will retry 2 times if no ack */ + ckbt51_send_conn_evt_ack(); + switch (data[0]) { + case CKBT51_CONNECTED: + event.evt_type = EVT_CONNECTED; + break; + case CKBT51_DISCOVERABLE: + event.evt_type = EVT_DISCOVERABLE; + break; + case CKBT51_RECONNECTING: + event.evt_type = EVT_RECONNECTING; + break; + case CKBT51_DISCONNECTED: + event.evt_type = EVT_DISCONNECTED; + break; + case CKBT51_PINCODE_ENTRY: + event.evt_type = EVT_PINCODE_ENTRY; + break; + case CKBT51_EXIT_PINCODE_ENTRY: + event.evt_type = EVT_EXIT_PINCODE_ENTRY; + break; + } + event.params.hostIndex = data[2]; + break; + case CKBT51_EVT_HID_EVENT: + dprintf("CKBT51_EVT_HID_EVENT\n"); + event.evt_type = EVT_HID_INDICATOR; + event.params.led = data[0]; + break; + case CKBT51_EVT_QUERY_RSP: + dprintf("CKBT51_EVT_QUERY_RSP\n"); + query_rsp_handler(data, len); + break; + case CKBT51_EVT_OTA_RSP: + dprintf("CKBT51_EVT_OTA_RSP\n"); + ckbt51_dfu_tx(CKBT51_EVT_OTA_RSP, data, len, sn); + break; + case CKBT51_EVT_BATTERY: + if (data[0] == 0x01) { + dprintf("CKBT51_EVT_BATTERY\n"); + battery_calculte_voltage(data[1] | (data[2] << 8)); + } + break; + default: + dprintf("Unknown event!!!\n"); + break; + } + + if (event.evt_type) bluetooth_event_queue_enqueue(event); +} + +void ckbt51_task(void) { + static bool wait_for_new_pkt = true; + static uint8_t len = 0xff; + static uint8_t sn = 0; + + if (wait_for_new_pkt && BT_DRIVER.iqueue.q_counter >= PACKECT_HEADER_LEN) { + uint8_t buf[32] = {0}; + + if (wait_for_new_pkt) { + if (sdGet(&BT_DRIVER) == 0xAA && sdGet(&BT_DRIVER) == 0x57) { + for (uint8_t i = 0; i < 3; i++) { + buf[i] = sdGet(&BT_DRIVER); + } + // Check wheather len is valid + if ((~buf[0] & 0xFF) == buf[1]) { + len = buf[0]; + sn = buf[2]; + + wait_for_new_pkt = false; + } + } + } + } + + if (!wait_for_new_pkt && BT_DRIVER.iqueue.q_counter >= len) { + uint8_t buf[32] = {0}; + + for (uint8_t i = 0; i < len; i++) { + buf[i] = sdGetTimeout(&BT_DRIVER, TIME_IMMEDIATE); + + } + + wait_for_new_pkt = true; + + uint16_t checksum = 0; + for (int i = 0; i < len - 2; i++) checksum += buf[i]; + + if ((checksum & 0xff) == buf[len - 2] && ((checksum >> 8) & 0xff) == buf[len - 1]) { + ckbt51_event_handler(buf[0], buf + 1, len - 2, sn); + } else { + // TODO: Error handle + } + } +} diff --git a/keyboards/keychron/bluetooth/ckbt51.h b/keyboards/keychron/bluetooth/ckbt51.h new file mode 100644 index 0000000000..8ca82d4357 --- /dev/null +++ b/keyboards/keychron/bluetooth/ckbt51.h @@ -0,0 +1,155 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +#include "stdint.h" + +#ifdef BT_DRIVER_UART_BANK +# define BT_DRIVER_UART_TX_BANK BT_DRIVER_UART_BANK +# define BT_DRIVER_UART_RX_BANK BT_DRIVER_UART_BANK +#endif + +#ifndef BT_DRIVER_UART_TX_BANK +# define BT_DRIVER_UART_TX_BANK GPIOA +#endif + +#ifndef BT_DRIVER_UART_RX_BANK +# define BT_DRIVER_UART_RX_BANK GPIOA +#endif + +#ifndef BT_DRIVER_UART_TX +# define BT_DRIVER_UART_TX 2 +#endif + +#ifndef BT_DRIVER_UART_RX +# define BT_DRIVER_UART_RX 3 +#endif + +#ifndef BT_DRIVER +# define BT_DRIVER SD2 +#endif + +#ifdef USE_GPIOV1 +# ifndef BT_DRIVER_UART_TX_PAL_MODE +# define BT_DRIVER_UART_TX_PAL_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL +# endif +# ifndef BT_DRIVER_UART_RX_PAL_MODE +# define BT_DRIVER_UART_RX_PAL_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL +# endif +#else +// The default PAL alternate modes are used to signal that the pins are used for I2C +# ifndef BT_DRIVER_UART_TX_PAL_MODE +# define BT_DRIVER_UART_TX_PAL_MODE 7 +# endif +# ifndef BT_DRIVER_UART_RX_PAL_MODE +# define BT_DRIVER_UART_RX_PAL_MODE 7 +# endif +#endif + +// Error checking +#if !STM32_SERIAL_USE_USART1 && !STM32_SERIAL_USE_USART2 && !STM32_SERIAL_USE_USART3 && !STM32_SERIAL_USE_UART4 && !STM32_SERIAL_USE_UART5 && !STM32_SERIAL_USE_USART6 && !STM32_SERIAL_USE_UART7 && !STM32_SERIAL_USE_UART8 && !STM32_SERIAL_USE_LPUART1 +# error "BT driver activated but no USART/UART peripheral assigned" +#endif + +#define PACKECT_HEADER_LEN 5 +#define BDA_LEN 6 +#define PACKET_MAX_LEN 64 + +enum { + PAIRING_MODE_DEFAULT = 0x00, + PAIRING_MODE_JUST_WORK, + PAIRING_MODE_PASSKEY_ENTRY, + PAIRING_MODE_LESC_OR_SSP, + PAIRING_MODE_INVALID +}; + +enum { + BT_MODE_DEFAUL, + BT_MODE_CLASSIC, + BT_MODE_LE, // Note: CKBT51 doesn't support BLE + BT_MODE_INVALID, +}; + +typedef struct { + uint8_t hostIndex; + uint16_t timeout; /* Pairing timeout, valid value range from 30 to 3600 seconds, 0 for default */ + uint8_t pairingMode; /* 0: default, 1: Just Works, 2: Passkey Entry */ + uint8_t BRorLE; /* Only available for dual mode module. Keep 0 for single mode module */ + uint8_t txPower; /* Only available for BLE module */ + const char* leName; /* Only available for BLE module */ +} pairing_param_t; + +typedef struct { + uint8_t type; + uint16_t full_votage; + uint16_t empty_voltage; + uint16_t shutdown_voltage; +} battery_param_t; + +typedef struct { + uint8_t model_name[11]; + uint8_t mode; + uint8_t bluetooth_version; + uint8_t firmware_version[11]; + uint8_t hardware_version[11]; + uint16_t cmd_set_verson; +} __attribute__((packed)) module_info_t; + +typedef struct { + uint8_t event_mode; /* Must be 0x02 */ + uint16_t connected_idle_timeout; + uint16_t pairing_timeout; /* Range: 30 ~ 3600 second, 0 for default */ + uint8_t pairing_mode; /* 0: default, 1: Just Works, 2: Passkey Entry */ + uint16_t reconnect_timeout; /* 0: default, 0xFF: Unlimited time, 2 ~ 254 seconds */ + uint8_t report_rate; /* 90 or 133 */ + uint8_t rsvd1; + uint8_t rsvd2; + uint8_t vendor_id_source; /* 0: From Bluetooth SIG, 1: From USB-IF */ + uint16_t verndor_id; /* No effect, the vendor ID is 0x3434 */ + uint16_t product_id; + /* Below parametes is only available for BLE module */ + uint16_t le_connection_interval_min; + uint16_t le_connection_interval_max; + uint16_t le_connection_interval_timeout; +} __attribute__((packed)) module_param_t; + +void ckbt51_init(bool wakeup_from_low_power_mode); +void ckbt51_send_cmd(uint8_t* payload, uint8_t len, bool ack_enable); + +void ckbt51_send_keyboard(uint8_t* report); +void ckbt51_send_nkro(uint8_t* report); +void ckbt51_send_consumer(uint16_t report); +void ckbt51_send_system(uint16_t report); + +void ckbt51_become_discoverable(uint8_t host_idx, void* param); +void ckbt51_connect(uint8_t hostIndex, uint16_t timeout); +void ckbt51_disconnect(void); +void ckbt51_switch_host(uint8_t hostIndex); +void ckbt51_read_state_reg(uint8_t reg, uint8_t len); + +void ckbt51_get_info(module_info_t* info); +void ckbt51_set_param(module_param_t* param); +void ckbt51_get_param(module_param_t* param); +void ckbt51_set_local_name(const char* name); +void ckbt51_get_local_name(char* name); + +void ckbt51_factory_reset(void); +void ckbt51_int_pin_test(bool enable); +void ckbt51_dfu_rx(uint8_t* data, uint8_t length); + +void ckbt51_task(void); + diff --git a/keyboards/keychron/bluetooth/factory_test.c b/keyboards/keychron/bluetooth/factory_test.c new file mode 100644 index 0000000000..10fdf24d17 --- /dev/null +++ b/keyboards/keychron/bluetooth/factory_test.c @@ -0,0 +1,320 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 "quantum.h" +#include "ckbt51.h" +#include "raw_hid.h" +#include "transport.h" +#include "battery.h" +#include "via.h" +#include "indicator.h" +#include "lpm.h" + +extern bool bt_factory_reset; + +enum { + BACKLIGHT_TEST_OFF = 0, + BACKLIGHT_TEST_WHITE, + BACKLIGHT_TEST_RED, + BACKLIGHT_TEST_GREEN, + BACKLIGHT_TEST_BLUE, + BACKLIGHT_TEST_MAX, +}; + +enum { + KEY_PRESS_FN = 0x01 << 0, + KEY_PRESS_J = 0x01 << 1, + KEY_PRESS_Z = 0x01 << 2, + KEY_PRESS_RIGHT = 0x01 << 3, + KEY_PRESS_HOME = 0x01 << 4, + KEY_PRESS_FACTORY_RESET = KEY_PRESS_FN | KEY_PRESS_J | KEY_PRESS_Z, + KEY_PRESS_BACKLIGTH_TEST = KEY_PRESS_FN | KEY_PRESS_RIGHT | KEY_PRESS_HOME, +}; + +enum { + FACTORY_TEST_CMD_BACKLIGHT = 0x01, + FACTORY_TEST_CMD_OS_SWITCH, + FACTORY_TEST_CMD_JUMP_TO_BL, + FACTORY_TEST_CMD_INT_PIN, + FACTORY_TEST_CMD_GET_TRANSPORT, + FACTORY_TEST_CMD_CHARGING_ADC, +}; + +enum { + OS_SWITCH = 0x01, +}; + +static uint32_t factory_reset_timer = 0; +static uint8_t factory_reset_state = 0; +static uint8_t backlight_test_mode = BACKLIGHT_TEST_OFF; + +static uint32_t factory_reset_ind_timer = 0; +static uint8_t factory_reset_ind_state = 0; +static bool report_os_sw_state = false; + +void factory_timer_start(void) { + factory_reset_timer = sync_timer_read32() | 1; +} + +static inline void factory_timer_check(void) { + if (sync_timer_elapsed32(factory_reset_timer) > 3000) { + factory_reset_timer = 0; + + if (factory_reset_state == KEY_PRESS_FACTORY_RESET) { + factory_reset_ind_timer = sync_timer_read32() | 1; + factory_reset_ind_state++; + + layer_state_t default_layer_tmp = default_layer_state; + eeconfig_init(); + default_layer_set(default_layer_tmp); +#ifdef LED_MATRIX_ENABLE + if (!led_matrix_is_enabled()) led_matrix_enable(); + led_matrix_init(); +#endif +#ifdef RGB_MATRIX_ENABLE + if (!rgb_matrix_is_enabled()) rgb_matrix_enable(); + rgb_matrix_init(); +#endif +#ifdef BLUETOOTH_ENABLE + ckbt51_factory_reset(); + bt_factory_reset = true; +#endif + } else if (factory_reset_state == KEY_PRESS_BACKLIGTH_TEST) { +#ifdef LED_MATRIX_ENABLE + if (!led_matrix_is_enabled()) led_matrix_enable(); +#endif +#ifdef RGB_MATRIX_ENABLE + if (!rgb_matrix_is_enabled()) rgb_matrix_enable(); +#endif + backlight_test_mode = BACKLIGHT_TEST_WHITE; + } + + factory_reset_state = 0; + } +} + +static inline void factory_reset_ind_timer_check(void) { + if (factory_reset_ind_timer && sync_timer_elapsed32(factory_reset_ind_timer) > 250) { + if (factory_reset_ind_state++ > 6) { + factory_reset_ind_timer = factory_reset_ind_state = 0; + } else { + factory_reset_ind_timer = sync_timer_read32() | 1; + } + } +} + +void process_record_factory_reset(uint16_t keycode, keyrecord_t *record) { + switch (keycode) { + case MO(1): /* fall through */ + case MO(2): + case MO(3): + case MO(4): + if (record->event.pressed) { + factory_reset_state |= KEY_PRESS_FN; + } else { + factory_reset_state &= ~KEY_PRESS_FN; + factory_reset_timer = 0; + } + break; + case KC_J: + if (record->event.pressed) { + factory_reset_state |= KEY_PRESS_J; + if (factory_reset_state == 0x07) factory_timer_start(); + } else { + factory_reset_state &= ~KEY_PRESS_J; + factory_reset_timer = 0; + } + break; + case KC_Z: + if (record->event.pressed) { + factory_reset_state |= KEY_PRESS_Z; + if (factory_reset_state == 0x07) factory_timer_start(); + } else { + factory_reset_state &= ~KEY_PRESS_Z; + factory_reset_timer = 0; + } + break; + case KC_RIGHT: + if (record->event.pressed) { + if (backlight_test_mode) { + if (++backlight_test_mode >= BACKLIGHT_TEST_MAX) { + backlight_test_mode = BACKLIGHT_TEST_WHITE; + } + } else { + factory_reset_state |= KEY_PRESS_RIGHT; + if (factory_reset_state == 0x19) factory_timer_start(); + } + } else { + factory_reset_state &= ~KEY_PRESS_RIGHT; + factory_reset_timer = 0; + } + break; + case KC_HOME: + if (record->event.pressed) { + if (backlight_test_mode) { + backlight_test_mode = BACKLIGHT_TEST_OFF; + } else { + factory_reset_state |= KEY_PRESS_HOME; + if (factory_reset_state == 0x19) factory_timer_start(); + } + } else { + factory_reset_state &= ~KEY_PRESS_HOME; + factory_reset_timer = 0; + } + break; + } +} + +#ifdef LED_MATRIX_ENABLE +void led_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) { + if (factory_reset_ind_state) { + for (uint8_t i = led_min; i <= led_max; i++) { + led_matrix_set_value(i, factory_reset_ind_state % 2 ? 0 : 255); + } + } +} +#endif + +#ifdef RGB_MATRIX_ENABLE +void rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) { + if (factory_reset_ind_state) { + for (uint8_t i = led_min; i <= led_max; i++) { + rgb_matrix_set_color(i, factory_reset_ind_state % 2 ? 0 : 255, 0, 0); + } + } else if (backlight_test_mode) { + switch (backlight_test_mode) { + case BACKLIGHT_TEST_WHITE: + for (uint8_t i = led_min; i <= led_max; i++) { + rgb_matrix_set_color(i, 255, 255, 255); + } + break; + case BACKLIGHT_TEST_RED: + for (uint8_t i = led_min; i <= led_max; i++) { + rgb_matrix_set_color(i, 255, 0, 0); + } + break; + case BACKLIGHT_TEST_GREEN: + for (uint8_t i = led_min; i <= led_max; i++) { + rgb_matrix_set_color(i, 0, 255, 0); + } + break; + case BACKLIGHT_TEST_BLUE: + for (uint8_t i = led_min; i <= led_max; i++) { + rgb_matrix_set_color(i, 0, 0, 255); + } + break; + } + } +} +#endif + +void factory_reset_task(void) { + if (factory_reset_timer) factory_timer_check(); + if (factory_reset_ind_timer) factory_reset_ind_timer_check(); +} + +void factory_test_send(uint8_t *payload, uint8_t length) { + uint16_t checksum = 0; + uint8_t data[RAW_EPSIZE] = {0}; + + uint8_t i = 0; + data[i++] = 0xAB; + + memcpy(&data[i], payload, length); + i += length; + + for (uint8_t i = 1; i < RAW_EPSIZE - 3; i++) checksum += data[i]; + data[RAW_EPSIZE - 2] = checksum & 0xFF; + data[RAW_EPSIZE - 1] = (checksum >> 8) & 0xFF; + + raw_hid_send(data, RAW_EPSIZE); +} + +void factory_test_rx(uint8_t *data, uint8_t length) { + if (data[0] == 0xAB) { + uint16_t checksum = 0; + + for (uint8_t i = 1; i < RAW_EPSIZE - 3; i++) { + checksum += data[i]; + } + /* Verify checksum */ + if ((checksum & 0xFF) != data[RAW_EPSIZE - 2] || checksum >> 8 != data[RAW_EPSIZE - 1]) return; + + uint8_t payload[32]; + uint8_t len = 0; + + switch (data[1]) { + case FACTORY_TEST_CMD_BACKLIGHT: + backlight_test_mode = data[2]; + factory_reset_timer = 0; + break; + case FACTORY_TEST_CMD_OS_SWITCH: + report_os_sw_state = data[2]; + if (report_os_sw_state) { + dip_switch_read(true); + } + break; + case FACTORY_TEST_CMD_JUMP_TO_BL: + // if (memcmp(&data[2], "JumpToBootloader", strlen("JumpToBootloader")) == 0) bootloader_jump(); + break; + case FACTORY_TEST_CMD_INT_PIN: + switch (data[2]) { + /* Enalbe/disable test */ + case 0xA1: + ckbt51_int_pin_test(data[3]); + break; + /* Set INT state */ + case 0xA2: + writePin(CKBT51_INT_INPUT_PIN, data[3]); + break; + /* Report INT state */ + case 0xA3: + payload[len++] = FACTORY_TEST_CMD_INT_PIN; + payload[len++] = 0xA3; + payload[len++] = readPin(BLUETOOTH_INT_INPUT_PIN); + factory_test_send(payload, len); + break; + } + break; + case FACTORY_TEST_CMD_GET_TRANSPORT: + payload[len++] = FACTORY_TEST_CMD_GET_TRANSPORT; + payload[len++] = get_transport(); + payload[len++] = readPin(USB_POWER_SENSE_PIN); + factory_test_send(payload, len); + break; +#ifdef BATTERY_CHARGE_DONE_DETECT_ADC + case FACTORY_TEST_CMD_CHARGING_ADC: + case 0xA1: + battery_charging_monitor(data[3]); + break; + case 0xA2: + payload[len++] = FACTORY_TEST_CMD_CHARGING_ADC; + payload[len++] = battery_adc_read_charging_pin(); + factory_test_send(payload, len); + break; +#endif + } + } +} + +bool dip_switch_update_user(uint8_t index, bool active) { + if (report_os_sw_state) { + uint8_t payload[3] = {FACTORY_TEST_CMD_OS_SWITCH, OS_SWITCH, active}; + factory_test_send(payload, 3); + } + + return true; +} diff --git a/keyboards/keychron/bluetooth/factory_test.h b/keyboards/keychron/bluetooth/factory_test.h new file mode 100644 index 0000000000..d5ef301512 --- /dev/null +++ b/keyboards/keychron/bluetooth/factory_test.h @@ -0,0 +1,24 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +#define FACTORY_RESET_CHECK process_record_factory_reset +#define FACTORY_RESET_TASK factory_reset_task + +void process_record_factory_reset(uint16_t keycode, keyrecord_t *record); +void factory_reset_task(void); +void factory_test_rx(uint8_t *data, uint8_t length); diff --git a/keyboards/keychron/bluetooth/indicator.c b/keyboards/keychron/bluetooth/indicator.c new file mode 100644 index 0000000000..0936f4ea97 --- /dev/null +++ b/keyboards/keychron/bluetooth/indicator.c @@ -0,0 +1,434 @@ +/* Copyright 2021 @ lokher (https://www.keychron.com) + * + * 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 "quantum.h" +#include "indicator.h" +#include "transport.h" +#include "battery.h" +#include "eeconfig.h" +#include "bluetooth_config.h" +#include "config.h" +#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE) +# ifdef LED_MATRIX_ENABLE +# include "led_matrix.h" +# endif +# ifdef RGB_MATRIX_ENABLE +# include "rgb_matrix.h" +# endif +# include "i2c_master.h" +# include "bat_level_animation.h" +#endif + +#ifdef LED_MATRIX_ENABLE +# define DECIDE_TIME(t, duration) (duration == 0 ? LED_DISABLE_TIME_INFINITE : ((t > duration) ? t : duration)) +#endif +#ifdef RGB_MATRIX_ENABLE +# define DECIDE_TIME(t, duration) (duration == 0 ? RGB_DISABLE_TIME_INFINITE : ((t > duration) ? t : duration)) +#endif + +#define LED_ON 0x80 + +#define INDICATOR_SET(s) \ + indicator_config.type = s##_config.type; \ + indicator_config.on_time = s##_config.on_time; \ + indicator_config.off_time = s##_config.off_time; \ + indicator_config.duration = s##_config.duration; \ + indicator_config.highlight = s##_config.highlight; \ + indicator_config.elapsed = 0; + +indicator_config_t pairing_config = INDICATOR_CONFIG_PARING; +indicator_config_t connected_config = INDICATOR_CONFIG_CONNECTD; +indicator_config_t reconnecting_config = INDICATOR_CONFIG_RECONNECTING; +indicator_config_t disconnected_config = INDICATOR_CONFIG_DISCONNECTED; + +enum { + BACKLIGHT_OFF = 0x00, + BACKLIGHT_ON_CONNECTED = 0x01, + BACKLIGHT_ON_UNCONNECTED = 0x02, +}; + +indicator_config_t indicator_config; + +static bluetooth_state_t indicator_state; +static uint16_t next_period; +static indicator_type_t type; +static uint32_t indicator_timer_buffer; +static uint32_t battery_low_indicator = 0; + +#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE) +backlight_state_t original_backlight_state; + +static uint8_t host_led_matrix_list[HOST_DEVICES_COUNT] = HOST_LED_MATRIX_LIST; +#endif + +#ifdef HOST_LED_PIN_LIST +static pin_t host_led_pin_list[HOST_DEVICES_COUNT] = HOST_LED_PIN_LIST; +#endif + +void indicator_init(void) { +#ifdef HOST_LED_PIN_LIST + for (uint8_t i = 0; i < HOST_DEVICES_COUNT; i++) { + setPinOutput(host_led_pin_list[i]); + writePin(host_led_pin_list[i], !HOST_LED_PIN_ON_STATE); + } +#endif + +#ifdef BAT_LOW_LED_PIN + setPinOutput(BAT_LOW_LED_PIN); + writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE); +#endif +} + +#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE) +void indicator_enable(void) { +# ifdef LED_MATRIX_ENABLE + if (!led_matrix_is_enabled()) { + led_matrix_enable_noeeprom(); + } +# endif +# ifdef RGB_MATRIX_ENABLE + if (!rgb_matrix_is_enabled()) { + rgb_matrix_enable_noeeprom(); + } +# endif +} + +void indicator_disable(void) { +# ifdef LED_MATRIX_ENABLE + led_matrix_disable_noeeprom(); +# endif +# ifdef RGB_MATRIX_ENABLE + rgb_matrix_disable_noeeprom(); +# endif +} + +void indicator_set_backlit_timeout(uint32_t time) { +# ifdef LED_MATRIX_ENABLE + led_matrix_disable_timeout_set(time); +# endif +# ifdef RGB_MATRIX_ENABLE + rgb_matrix_disable_timeout_set(time); +# endif +} + +static inline void indicator_reset_backlit_time(void) { +# ifdef LED_MATRIX_ENABLE + led_matrix_disable_time_reset(); +# endif +# ifdef RGB_MATRIX_ENABLE + rgb_matrix_disable_time_reset(); +# endif +} + +bool indicator_is_enabled(void) { +# ifdef LED_MATRIX_ENABLE + return led_matrix_is_enabled(); +# endif +# ifdef RGB_MATRIX_ENABLE + return rgb_matrix_is_enabled(); +# endif +} + +bool indicator_is_backlit_enabled_eeprom(void) { +# ifdef LED_MATRIX_ENABLE + return led_matrix_is_enabled_eeprom(); +# endif +# ifdef RGB_MATRIX_ENABLE + return rgb_matrix_is_enabled_eeprom(); +# endif + return false; +} + +#endif + +bool indicator_is_running(void) { return !!indicator_config.value; } + +static void indicator_timer_cb(void *arg) { + if (*(indicator_type_t *)arg != INDICATOR_LAST) type = *(indicator_type_t *)arg; + + bool time_up = false; + switch (type) { + case INDICATOR_NONE: + break; + case INDICATOR_OFF: + next_period = 0; + time_up = true; + break; + + case INDICATOR_ON: + if (indicator_config.value) { + if (indicator_config.elapsed == 0) { + indicator_config.value |= LED_ON; + + if (indicator_config.duration) { + indicator_config.elapsed += indicator_config.duration; + } + } else + time_up = true; + } + break; + + case INDICATOR_ON_OFF: + if (indicator_config.value) { + if (indicator_config.elapsed == 0) { + indicator_config.value |= LED_ON; + next_period = indicator_config.on_time; + } else { + indicator_config.value = indicator_config.value & 0x0F; + next_period = indicator_config.duration - indicator_config.on_time; + } + + if ((indicator_config.duration == 0 || indicator_config.elapsed <= indicator_config.duration) && next_period != 0) { + indicator_config.elapsed += next_period; + } else { + time_up = true; + } + } + break; + + case INDICATOR_BLINK: + if (indicator_config.value) { + if (indicator_config.value & LED_ON) { + indicator_config.value = indicator_config.value & 0x0F; + next_period = indicator_config.off_time; + } else { + indicator_config.value |= LED_ON; + next_period = indicator_config.on_time; + } + + if ((indicator_config.duration == 0 || indicator_config.elapsed <= indicator_config.duration) && next_period != 0) { + indicator_config.elapsed += next_period; + } else + time_up = true; + } + break; + default: + time_up = true; + + next_period = 0; + break; + } + + if (time_up) { + /* Set indicator to off on timeup, avoid keeping light up until next update in raindrop effect */ + indicator_config.value = indicator_config.value & 0x0F; +#ifdef LED_MATRIX_ENABLE + led_matrix_indicators_kb(); +#endif +#ifdef RGB_MATRIX_ENABLE + rgb_matrix_indicators_kb(); +#endif + indicator_config.value = 0; + } + +#ifdef HOST_LED_PIN_LIST + if (indicator_config.value && (indicator_config.value & 0x80)) { + writePin(host_led_pin_list[indicator_config.value & 0x0F], HOST_LED_PIN_ON_STATE); + } else { + writePin(host_led_pin_list[indicator_config.value & 0x0F], !HOST_LED_PIN_ON_STATE); + } +#endif + + if (indicator_config.value == 0 && !indicator_is_backlit_enabled_eeprom()) { + indicator_disable(); + } +} + +void indicator_set(bluetooth_state_t state, uint8_t host_index) { + if (get_transport() != TRANSPORT_BLUETOOTH) return; + dprintf("indicator set: %d, %d\n", state, host_index); + + static uint8_t current_state = 0; + static uint8_t current_host = 0; + + bool host_index_changed = false; + if (current_host != host_index && state != BLUETOOTH_DISCONNECTED) { + host_index_changed = true; + current_host = host_index; + } + + if (current_state != state || host_index_changed) { + current_state = state; + } else { + return; + } + + indicator_timer_buffer = sync_timer_read32(); + + /* Turn on backlight mode for indicator */ + indicator_enable(); + indicator_reset_backlit_time(); + + switch (state) { + case BLUETOOTH_DISCONNECTED: + INDICATOR_SET(disconnected); + indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index; + indicator_timer_cb((void *)&indicator_config.type); + + if (battery_is_critical_low()) { + indicator_set_backlit_timeout(1000); + } else { + /* Set timer so that user has chance to turn on the backlight when is off */ + indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration)); + } + break; + + case BLUETOOTH_CONNECTED: + if (indicator_state != BLUETOOTH_CONNECTED) { + INDICATOR_SET(connected); + indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index; + indicator_timer_cb((void *)&indicator_config.type); + } + indicator_set_backlit_timeout(DECIDE_TIME(CONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration)); + break; + + case BLUETOOTH_PARING: + INDICATOR_SET(pairing); + indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : LED_ON | host_index; + indicator_timer_cb((void *)&indicator_config.type); + indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration)); + break; + + case BLUETOOTH_RECONNECTING: + INDICATOR_SET(reconnecting); + indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : LED_ON | host_index; + indicator_timer_cb((void *)&indicator_config.type); + indicator_set_backlit_timeout(DECIDE_TIME(DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT * 1000, indicator_config.duration)); + break; + + case BLUETOOTH_SUSPEND: + INDICATOR_SET(disconnected); + indicator_config.value = (indicator_config.type == INDICATOR_NONE) ? 0 : host_index; + indicator_timer_cb((void *)&indicator_config.type); + indicator_set_backlit_timeout(100); + break; + + default: + break; + } + + indicator_state = state; +} + +void indicator_stop(void) { + indicator_config.value = 0; + + if (indicator_is_backlit_enabled_eeprom()) { + indicator_enable(); + } else { + indicator_disable(); + } +} + +void indicator_battery_low_enable(bool enable) { + battery_low_indicator = enable ? (sync_timer_read32() | 1) : 0; +#ifdef BAT_LOW_LED_PIN + if (!enable) writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE); +#endif +} + +void indicator_task(void) { + bat_level_animiation_task(); + + if (indicator_config.value && sync_timer_elapsed32(indicator_timer_buffer) >= next_period) { + indicator_timer_cb((void *)&type); + indicator_timer_buffer = sync_timer_read32(); + } + + if (battery_low_indicator && sync_timer_elapsed32(battery_low_indicator) > LOW_BAT_ON_OFF_DURATION) { +#ifdef BAT_LOW_LED_PIN + togglePin(BAT_LOW_LED_PIN); +#endif + battery_low_indicator = sync_timer_read32() | 1; + } +} + +#ifdef LED_MATRIX_ENABLE +void led_matrix_indicators_kb(void) { + if (get_transport() == TRANSPORT_BLUETOOTH) { + if (battery_is_critical_low()) { + /* Prevent backlight flash caused by key activities */ + led_matrix_set_value_all(0); + return; + } + + if (bat_level_animiation_actived()) { + bat_level_animiation_indicate(); + } + + static uint8_t last_host_index = 0xFF; + if (indicator_config.value) { + uint8_t host_index = indicator_config.value & 0x0F; + + if (indicator_config.highlight) { + led_matrix_set_value_all(0); + } else if (last_host_index != host_index) { + led_matrix_set_value(host_led_matrix_list[last_host_index - 1], 0); + last_host_index = host_index; + } + + if (indicator_config.value & 0x80) + led_matrix_set_value(host_led_matrix_list[host_index - 1], 255); + else + led_matrix_set_value(host_led_matrix_list[host_index - 1], 0); + } +# if defined(DIM_CAPS_LOCK) && defined(CAPS_LOCK_INDEX) + else if (host_keyboard_led_state().caps_lock) { + led_matrix_set_value(CAPS_LOCK_INDEX, 0); + } +# endif + } +} +#endif + +#ifdef RGB_MATRIX_ENABLE +void rgb_matrix_indicators_kb(void) { + { + if (battery_is_critical_low()) { + /* Prevent backlight flash caused by key activities */ + rgb_matrix_set_color_all(0, 0, 0); + return; + } + + if (bat_level_animiation_actived()) { + bat_level_animiation_indicate(); + } + static uint8_t last_host_index = 0xFF; + + if (indicator_config.value) { + uint8_t host_index = indicator_config.value & 0x0F; + + if (indicator_config.highlight) { + rgb_matrix_set_color_all(0, 0, 0); + } else if (last_host_index != host_index) { + rgb_matrix_set_color(host_led_matrix_list[last_host_index - 1], 0, 0, 0); + last_host_index = host_index; + } + + if (indicator_config.value & 0x80) { + rgb_matrix_set_color(host_led_matrix_list[host_index - 1], 0, 0, 255); + } else { + rgb_matrix_set_color(host_led_matrix_list[host_index - 1], 0, 0, 0); + } + } +# if defined(DIM_CAPS_LOCK) && defined(CAPS_LOCK_INDEX) + else if (host_keyboard_led_state().caps_lock) { + rgb_matrix_set_color(CAPS_LOCK_INDEX, 0, 0, 0); + } +# endif + } +} +#endif diff --git a/keyboards/keychron/bluetooth/indicator.h b/keyboards/keychron/bluetooth/indicator.h new file mode 100644 index 0000000000..d09cf3c7cf --- /dev/null +++ b/keyboards/keychron/bluetooth/indicator.h @@ -0,0 +1,89 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +#include "config.h" +#include "bluetooth.h" + +/* Indication of pairing */ +#ifndef INDICATOR_CONFIG_PARING +# define INDICATOR_CONFIG_PARING {INDICATOR_BLINK, 1000, 1000, 0, true, 0}; +#endif + +/* Indication on Connected */ +#ifndef INDICATOR_CONFIG_CONNECTD +# define INDICATOR_CONFIG_CONNECTD {INDICATOR_ON_OFF, 2000, 250, 2000, true, 0}; +#endif + +/* Reconnecting indication */ +#ifndef INDICATOR_CONFIG_RECONNECTING +# define INDICATOR_CONFIG_RECONNECTING {INDICATOR_BLINK, 100, 100, 600, true, 0}; +#endif + +/* Disconnected indication */ +#ifndef INDICATOR_CONFIG_DISCONNECTED +# define INDICATOR_CONFIG_DISCONNECTED {INDICATOR_NONE, 100, 100, 600, false, 0}; +#endif + +/* Uint: Second */ +#ifndef DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT +# define DISCONNECTED_BACKLIGHT_OFF_DELAY_TIME 40 +#endif + +/* Uint: Second, the timer restarts on key activities. */ +#ifndef CONNECTED_BACKLIGHT_DISABLE_TIMEOUT +# define CONNECTED_BACKLIGHT_OFF_DELAY_TIME 600 +#endif + +#ifndef LOW_BAT_ON_OFF_DURATION +# define LOW_BAT_ON_OFF_DURATION 1000 +#endif + +#if BT_HOST_MAX_COUNT > 6 +# pragma error("HOST_COUNT max value is 6") +#endif + +typedef enum { INDICATOR_NONE, INDICATOR_OFF, INDICATOR_ON, INDICATOR_ON_OFF, INDICATOR_BLINK, INDICATOR_LAST } indicator_type_t; + +typedef struct PACKED { + indicator_type_t type; + uint32_t on_time; + uint32_t off_time; + uint32_t duration; + bool highlight; + uint8_t value; + uint32_t elapsed; +} indicator_config_t; + +typedef struct PACKED { + uint8_t value; + bool saved; +} backlight_state_t; + +void indicator_init(void); +void indicator_set(bluetooth_state_t state, uint8_t host_index); +void indicator_backlight_timer_reset(bool enable); +bool indicator_hook_key(uint16_t keycode); +void indicator_enable(void); +void indicator_disable(void); +void indicator_stop(void); +bool indicator_is_backlit_enabled_eeprom(void); +bool indicator_is_running(void); + +void indicator_battery_low_enable(bool enable); + +void indicator_task(void); diff --git a/keyboards/keychron/bluetooth/lpm.c b/keyboards/keychron/bluetooth/lpm.c new file mode 100644 index 0000000000..84f629a672 --- /dev/null +++ b/keyboards/keychron/bluetooth/lpm.c @@ -0,0 +1,88 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +/****************************************************************************** + * + * Filename: lpm.c + * + * Description: Contains low power mode implementation + * + ******************************************************************************/ + +#include "quantum.h" +#if defined(PROTOCOL_CHIBIOS) +# include +#endif +#include "bluetooth.h" +#include "indicator.h" +#include "lpm.h" +#include "transport.h" +#include "battery.h" + +extern matrix_row_t matrix[MATRIX_ROWS]; +extern bluetooth_transport_t bluetooth_transport; + +static uint32_t lpm_timer_buffer; +static bool lpm_time_up = false; +static matrix_row_t empty_matrix[MATRIX_ROWS] = {0}; + +void lpm_init(void) { +#ifdef USB_POWER_SENSE_PIN + setPinInputHigh(USB_POWER_SENSE_PIN); +#endif + lpm_timer_reset(); +} + +inline void lpm_timer_reset(void) { + lpm_time_up = false; + lpm_timer_buffer = sync_timer_read32(); +} + +void lpm_timer_stop(void) { + lpm_time_up = false; + lpm_timer_buffer = 0; +} + +static inline bool lpm_any_matrix_action(void) { return memcmp(matrix, empty_matrix, sizeof(empty_matrix)); } + +/* Implement of entering low power mode and wakeup varies per mcu or platform */ +__attribute__((weak)) void enter_power_mode(pm_t mode) {} + +__attribute__((weak)) bool usb_power_connected(void) { +#ifdef USB_POWER_SENSE_PIN + return readPin(USB_POWER_SENSE_PIN) == USB_POWER_CONNECTED_LEVEL; +#endif + + return true; +} + +void lpm_task(void) { + if (!lpm_time_up && sync_timer_elapsed32(lpm_timer_buffer) > RUN_MODE_PROCESS_TIME) { + lpm_time_up = true; + lpm_timer_buffer = 0; + } + + if (get_transport() == TRANSPORT_BLUETOOTH && lpm_time_up && !indicator_is_running() +#ifdef LED_MATRIX_ENABLE + && led_matrix_is_driver_shutdown() +#endif +#ifdef RGB_MATRIX_ENABLE + && rgb_matrix_is_driver_shutdown() +#endif + && !lpm_any_matrix_action()) + + enter_power_mode(LOW_POWER_MODE); +} diff --git a/keyboards/keychron/bluetooth/lpm.h b/keyboards/keychron/bluetooth/lpm.h new file mode 100644 index 0000000000..ddc0d235fe --- /dev/null +++ b/keyboards/keychron/bluetooth/lpm.h @@ -0,0 +1,31 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +#ifndef RUN_MODE_PROCESS_TIME +# define RUN_MODE_PROCESS_TIME 1000 +#endif + +typedef enum { PM_RUN, PM_LOW_POWER_RUN, PM_SLEEP, PM_LOW_POWER_SLEEP, PM_STOP0, PM_STOP1, PM_STOP2, PM_STANDBY_WITH_RAM, PM_STANDBY, PM_SHUTDOWN } pm_t; + +void lpm_init(void); +void lpm_timer_reset(void); +void lpm_timer_stop(void); +bool usb_power_connected(void); +void enter_power_mode(pm_t mode); + +void lpm_task(void); diff --git a/keyboards/keychron/bluetooth/lpm_stm32l432.c b/keyboards/keychron/bluetooth/lpm_stm32l432.c new file mode 100644 index 0000000000..a871a1b53c --- /dev/null +++ b/keyboards/keychron/bluetooth/lpm_stm32l432.c @@ -0,0 +1,339 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +/****************************************************************************** + * + * Filename: lpm_stm32l432.c + * + * Description: Contains low power mode implementation + * + ******************************************************************************/ + +#include "quantum.h" +#include +#include "bluetooth.h" +#include "indicator.h" +#include "lpm.h" +#include "transport.h" +#include "battery.h" +#include "report_buffer.h" + +extern pin_t row_pins[MATRIX_ROWS]; +extern void select_all_cols(void); +extern bluetooth_transport_t bluetooth_transport; + +static pm_t power_mode = PM_RUN; + +static inline void stm32_clock_fast_init(void); + +bool lpm_set(pm_t mode) { + switch (mode) { +#ifdef LOW_POWER_RUN_MODE_ENABLE + case PM_RUN: + if (power_mode != PM_LOW_POWER_RUN)) return; + /* Set main regulator */ + PWR->CR1 &= ~PWR_CR1_LPR; + while (PWR->SR2 & PWR_SR2_REGLPF) + ; + // TODO: restore sysclk + return true; + // break; + + case PM_LOW_POWER_RUN: + if (power_mode != PM_RUN) return; + + // FLASH->ACR |= FLASH_ACR_RUN_PD; // Optional + // TODO: Decrease sysclk below 2 MHz + PWR->CR1 |= PWR_CR1_LPR; + return true; + // break; +#endif + case PM_SLEEP: + /* Wake source: Any interrupt or event */ + if (power_mode != PM_RUN) return false; + + SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; + break; + +#ifdef LOW_POWER_RUN_MODE_ENABLE + case PM_LOW_POWER_SLEEP: + /* Wake source: Any interrupt or event */ + if (power_mode != PM_LOW_POWER_RUN) return; /* Can only transit from PM_LOW_POWER_RUN */ + + SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; + __WFI(); + exit_low_power_mode(); + break; +#endif + case PM_STOP0: + /* Wake source: Reset pin, all I/Os, BOR, PVD, PVM, RTC, LCD, IWDG, + COMPx, USARTx, LPUART1, I2Cx, LPTIMx, USB, SWPMI */ + if (power_mode != PM_RUN) return false; + + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + PWR->CR1 |= PWR_CR1_LPMS_STOP0; + break; + + case PM_STOP1: + /* Wake source: Reset pin, all I/Os, BOR, PVD, PVM, RTC, LCD, IWDG, + COMPx, USARTx, LPUART1, I2Cx, LPTIMx, USB, SWPMI */ + if (power_mode != PM_RUN && power_mode != PM_LOW_POWER_RUN) return false; + + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + PWR->CR1 |= PWR_CR1_LPMS_STOP1; + break; + + case PM_STOP2: + /* Wake source: Reset pin, all I/Os, BOR, PVD, PVM, RTC, LCD, IWDG, + COMPx (x=1, 2), I2C3, LPUART1, LPTIM1, LPTIM2 */ + if (power_mode != PM_RUN) return false; + + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + PWR->CR1 |= PWR_CR1_LPMS_STOP2; + break; + + case PM_STANDBY_WITH_RAM: + /* Wake source: Reset, 5 I/O(PA0, PC13, PE6, PA2, PC5), BOR, RTC, IWDG */ + if (power_mode != PM_RUN && power_mode != PM_LOW_POWER_RUN) return false; + + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + PWR->CR1 |= PWR_CR1_LPMS_STANDBY; + PWR->CR3 |= PWR_CR3_RRS; + break; + + case PM_STANDBY: + /* Wake source: Reset, 2 I/O(PA0, PA2) in STM32L432Kx,, BOR, RTC, IWDG */ + if (power_mode != PM_RUN && power_mode != PM_LOW_POWER_RUN) return false; + + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + PWR->CR1 |= PWR_CR1_LPMS_STANDBY; + PWR->CR3 &= ~PWR_CR3_RRS; + break; + + case PM_SHUTDOWN: + /* Wake source: Reset, 2 I/O(PA0, PA2) in STM32L432Kx, RTC */ + if (power_mode != PM_RUN && power_mode != PM_LOW_POWER_RUN) return false; + + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + PWR->CR1 |= PWR_CR1_LPMS_SHUTDOWN; + break; + + default: + return false; + } + + return true; +} + +static inline void enter_low_power_mode_prepare(void) { +#if HAL_USE_RTC + nvicEnableVector(STM32_EXTI20_NUMBER, 13); + + PWR->CR3 |= PWR_CR3_EIWF; + + RTCWakeup wakeupspec; + wakeupspec.wutr = 0x040014; + rtcSTM32SetPeriodicWakeup(&RTCD1, &wakeupspec); +#endif + +#if defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE) + /* Usb unit is actived and running, stop and disconnect first */ + usbStop(&USBD1); + usbDisconnectBus(&USBD1); + + /* Isolate USB to save power.*/ + PWR->CR2 &= ~PWR_CR2_USV; /*PWR_CR2_USV is available on STM32L4x2xx and STM32L4x3xx devices only. */ +#endif + + palEnableLineEvent(BLUETOOTH_INT_INPUT_PIN, PAL_EVENT_MODE_FALLING_EDGE); + palEnableLineEvent(USB_POWER_SENSE_PIN, PAL_EVENT_MODE_BOTH_EDGES); + + /* Enable key matrix wake up */ + pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; + + for (uint8_t x = 0; x < MATRIX_ROWS; x++) { + if (row_pins[x] != NO_PIN) { + palEnableLineEvent(row_pins[x], PAL_EVENT_MODE_BOTH_EDGES); + } + } + + select_all_cols(); + +#if defined(DIP_SWITCH_PINS) +# define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(pin_t)) + static pin_t dip_switch_pad[] = DIP_SWITCH_PINS; + + for (uint8_t i = 0; i < NUMBER_OF_DIP_SWITCHES; i++) { + setPinInputLow(dip_switch_pad[i]); + } +#endif +} + +static inline void lpm_wakeup(void) { + stm32_clock_fast_init(); + if (bluetooth_transport.init) bluetooth_transport.init(true); + + chSysLock(); + SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; + + PWR->SCR |= PWR_SCR_CWUF; + PWR->SCR |= PWR_SCR_CSBF; + +#if defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE) + /* Remove USB isolation.*/ + PWR->CR2 |= PWR_CR2_USV; /* PWR_CR2_USV is available on STM32L4x2xx and STM32L4x3xx devices only. */ + usb_start(&USBD1); +#endif + + /* TIMx is disable during stop/standby/sleep mode, init after wakeup */ + stInit(); + timer_init(); + chSysUnlock(); + battery_init(); + + /* Disable all wake up pins */ + for (uint8_t x = 0; x < MATRIX_ROWS; x++) { + if (row_pins[x] != NO_PIN) { + palDisableLineEvent(row_pins[x]); + } + } + palDisableLineEvent(BLUETOOTH_INT_INPUT_PIN); + palDisableLineEvent(USB_POWER_SENSE_PIN); + +#if defined(DIP_SWITCH_PINS) + dip_switch_init(); + dip_switch_read(true); +#endif + +#if HAL_USE_RTC + rtcSTM32SetPeriodicWakeup(&RTCD1, NULL); + nvicDisableVector(STM32_EXTI20_NUMBER); +#endif +} + +/* + * NOTE: + * 1. Shall not use PM_LOW_POWER_RUN, PM_LOW_POWER_SLEEP, due to PM_LOW_POWER_RUN + * need to decrease system clock below 2 MHz. Dynamic clock is not yet supported + * for STM32L432xx in latest ChibiOS 21.6.0 so far. + * 2. Care must be taken to use PM_STANDBY_WITH_RAM, PM_STANDBY, PM_SHUTDOWN due to + * limited wake source, thus can't be waken via keyscan. PM_SHUTDOWN need LSE. + * 3. Reference from AN4621: STM32L4 and STM32L4+ ultra-low-power features overview + * for detail wake source + */ + +void enter_power_mode(pm_t mode) { +#if defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE) + /* Don't enter low power mode if attached to the host */ + if (mode > PM_SLEEP && usb_power_connected()) return; +#endif + + if (!lpm_set(mode)) return; + enter_low_power_mode_prepare(); + + // __DSB(); + __WFI(); + // __ISB(); + + lpm_wakeup(); + lpm_timer_reset(); + report_buffer_init(); + power_mode = PM_RUN; +} + +void usb_power_connect(void) { + PWR->CR2 |= PWR_CR2_USV; +} + +void usb_power_disconnect(void) { + PWR->CR2 &= ~PWR_CR2_USV; +} + +/* + * This is a simplified version of stm32_clock_init() by removing unnecessary clock initlization + * code snippet. The original stm32_clock_init() take about 2ms, but ckbt51 sends data via uart + * about 200us after wakeup pin is assert, it means that we must get everything ready before + * uart data coming when wakeup pin interrupt of MCU is triggerred. + * Here we reduce clock init time to 100us. + */ +inline void stm32_clock_fast_init(void) { +#if !STM32_NO_INIT + /* Core voltage setup.*/ + PWR->CR1 = STM32_VOS; + while ((PWR->SR2 & PWR_SR2_VOSF) != 0) /* Wait until regulator is */ + ; /* stable. */ + +# if STM32_HSI16_ENABLED // 10.7us + /* HSI activation.*/ + RCC->CR |= RCC_CR_HSION; + while ((RCC->CR & RCC_CR_HSIRDY) == 0) + ; /* Wait until HSI16 is stable. */ +# endif + +# if STM32_CLOCK_HAS_HSI48 // 13us +# if STM32_HSI48_ENABLED + /* HSI activation.*/ + RCC->CRRCR |= RCC_CRRCR_HSI48ON; + while ((RCC->CRRCR & RCC_CRRCR_HSI48RDY) == 0) + ; /* Wait until HSI48 is stable. */ +# endif +# endif + +# if STM32_ACTIVATE_PLL || STM32_ACTIVATE_PLLSAI1 || STM32_ACTIVATE_PLLSAI2 + /* PLLM and PLLSRC are common to all PLLs.*/ +# if defined(STM32L496xx) || defined(STM32L4A6xx) + RCC->PLLCFGR = STM32_PLLPDIV | STM32_PLLR | STM32_PLLREN | STM32_PLLQ | STM32_PLLQEN | STM32_PLLP | STM32_PLLPEN | STM32_PLLN | STM32_PLLM | STM32_PLLSRC; +# else + RCC->PLLCFGR = STM32_PLLR | STM32_PLLREN | STM32_PLLQ | STM32_PLLQEN | STM32_PLLP | STM32_PLLPEN | STM32_PLLN | STM32_PLLM | STM32_PLLSRC; +# endif +# endif + +# if STM32_ACTIVATE_PLL + /* PLL activation.*/ + RCC->CR |= RCC_CR_PLLON; + + /* Waiting for PLL lock.*/ + while ((RCC->CR & RCC_CR_PLLRDY) == 0) + ; +# endif + + /* Set flash WS's for SYSCLK source */ + if (STM32_FLASHBITS > STM32_MSI_FLASHBITS) { + FLASH->ACR = (FLASH->ACR & ~FLASH_ACR_LATENCY_Msk) | STM32_FLASHBITS; + while ((FLASH->ACR & FLASH_ACR_LATENCY_Msk) != (STM32_FLASHBITS & FLASH_ACR_LATENCY_Msk)) { + } + } + + /* Switching to the configured SYSCLK source if it is different from MSI.*/ +# if (STM32_SW != STM32_SW_MSI) + RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */ + /* Wait until SYSCLK is stable.*/ + while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2)) + ; +# endif + + /* Reduce the flash WS's for SYSCLK source if they are less than MSI WSs */ + if (STM32_FLASHBITS < STM32_MSI_FLASHBITS) { + FLASH->ACR = (FLASH->ACR & ~FLASH_ACR_LATENCY_Msk) | STM32_FLASHBITS; + while ((FLASH->ACR & FLASH_ACR_LATENCY_Msk) != (STM32_FLASHBITS & FLASH_ACR_LATENCY_Msk)) { + } + } +#endif /* STM32_NO_INIT */ + + /* SYSCFG clock enabled here because it is a multi-functional unit shared + among multiple drivers.*/ + rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, true); +} diff --git a/keyboards/keychron/bluetooth/lpm_stm32l432.h b/keyboards/keychron/bluetooth/lpm_stm32l432.h new file mode 100644 index 0000000000..065bf96b47 --- /dev/null +++ b/keyboards/keychron/bluetooth/lpm_stm32l432.h @@ -0,0 +1,19 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +typedef enum { PM_RUN, PM_LOW_POWER_RUN, PM_SLEEP, PM_LOW_POWER_SLEEP, PM_STOP0, PM_STOP1, PM_STOP2, PM_STANDBY_WITH_RAM, PM_STANDBY, PM_SHUTDOWN } pm_t; diff --git a/keyboards/keychron/bluetooth/report_buffer.c b/keyboards/keychron/bluetooth/report_buffer.c new file mode 100644 index 0000000000..51225888c9 --- /dev/null +++ b/keyboards/keychron/bluetooth/report_buffer.c @@ -0,0 +1,141 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 "quantum.h" +#include "report_buffer.h" +#include "bluetooth.h" +#include "lpm.h" + +/* The report buffer is mainly used to fix key press lost issue of macro + * when bluetooth module fifo isn't large enough. The maximun macro + * string length is determined by this queue size, and should be + * REPORT_BUFFER_QUEUE_SIZE devided by 2 since each character is implemented + * by sending a key pressing then a key releasing report. + * Please note that it cosume sizeof(report_buffer_t) * REPORT_BUFFER_QUEUE_SIZE + * bytes RAM, with default setting, used RAM size is + * sizeof(report_buffer_t) * 256 = 34* 256 = 8704 bytes + */ +#ifndef REPORT_BUFFER_QUEUE_SIZE +# define REPORT_BUFFER_QUEUE_SIZE 256 +#endif + +extern bluetooth_transport_t bluetooth_transport; + +/* report_interval value should be less than bluetooth connection interval because + * it takes some time for communicating between mcu and bluetooth module. Carefully + * set this value to feed the bt module so that we don't lost the key report nor lost + * the anchor point of bluetooth interval. The bluetooth connection interval varies + * if BLE is used, invoke report_buffer_set_inverval() to update the value + */ +uint8_t report_interval = DEFAULT_REPORT_INVERVAL_MS; + +static uint32_t report_timer_buffer = 0; +uint32_t retry_time_buffer = 0; +report_buffer_t report_buffer_queue[REPORT_BUFFER_QUEUE_SIZE]; +uint8_t report_buffer_queue_head; +uint8_t report_buffer_queue_tail; +report_buffer_t kb_rpt; +uint8_t retry = 0; + +void report_buffer_init(void) { + // Initialise the report queue + memset(&report_buffer_queue, 0, sizeof(report_buffer_queue)); + report_buffer_queue_head = 0; + report_buffer_queue_tail = 0; + retry = 0; + report_timer_buffer = sync_timer_read32(); +} + +bool report_buffer_enqueue(report_buffer_t *report) { + uint8_t next = (report_buffer_queue_head + 1) % REPORT_BUFFER_QUEUE_SIZE; + if (next == report_buffer_queue_tail) { + return false; + } + + report_buffer_queue[report_buffer_queue_head] = *report; + report_buffer_queue_head = next; + return true; +} + +inline bool report_buffer_dequeue(report_buffer_t *report) { + if (report_buffer_queue_head == report_buffer_queue_tail) { + return false; + } + + *report = report_buffer_queue[report_buffer_queue_tail]; + report_buffer_queue_tail = (report_buffer_queue_tail + 1) % REPORT_BUFFER_QUEUE_SIZE; + return true; +} + +bool report_buffer_is_empty() { + return report_buffer_queue_head == report_buffer_queue_tail; +} + +void report_buffer_update_timer(void) { + report_timer_buffer = sync_timer_read32(); +} + +bool report_buffer_next_inverval(void) { + return sync_timer_elapsed32(report_timer_buffer) > report_interval; +} + +void report_buffer_set_inverval(uint8_t interval) { + report_interval = interval; +} + +uint8_t report_buffer_get_retry(void) { + return retry; +} + +void report_buffer_set_retry(uint8_t times) { + retry = times; +} + +void report_buffer_task(void) { + if (bluetooth_get_state() == BLUETOOTH_CONNECTED && (!report_buffer_is_empty() || retry) && report_buffer_next_inverval()) { + bool pending_data = false; + + if (!retry) { + if (report_buffer_dequeue(&kb_rpt) && kb_rpt.type != REPORT_TYPE_NONE) { + if (sync_timer_read32() > 2) { + pending_data = true; + retry = RETPORT_RETRY_COUNT; + retry_time_buffer = sync_timer_read32(); + } + } + } else { + if (sync_timer_elapsed32(retry_time_buffer) > 7) { + pending_data = true; + --retry; + retry_time_buffer = sync_timer_read32(); + } + } + + if (pending_data) { +#if defined(NKRO_ENABLE) && defined(BLUETOOTH_NKRO_ENABLE) + if (kb_rpt.type == REPORT_TYPE_NKRO && bluetooth_transport.send_nkro) { + bluetooth_transport.send_nkro(&kb_rpt.keyboard.nkro.mods); + } else if (kb_rpt.type == REPORT_TYPE_KB && bluetooth_transport.send_keyboard) + bluetooth_transport.send_keyboard(&kb_rpt.keyboard.mods); +#else + if (kb_rpt.type == REPORT_TYPE_KB && bluetooth_transport.send_keyboard) bluetooth_transport.send_keyboard(&kb_rpt.keyboard.mods); +#endif + if (kb_rpt.type == REPORT_TYPE_CONSUMER && bluetooth_transport.send_consumer) bluetooth_transport.send_consumer(kb_rpt.consumer); + report_timer_buffer = sync_timer_read32(); + lpm_timer_reset(); + } + } +} diff --git a/keyboards/keychron/bluetooth/report_buffer.h b/keyboards/keychron/bluetooth/report_buffer.h new file mode 100644 index 0000000000..ef53b6fc5a --- /dev/null +++ b/keyboards/keychron/bluetooth/report_buffer.h @@ -0,0 +1,50 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +#include "report.h" + +/* Default report interval value */ +#ifndef DEFAULT_REPORT_INVERVAL_MS +# define DEFAULT_REPORT_INVERVAL_MS 3 +#endif + +/* Default report interval value */ +#ifndef RETPORT_RETRY_COUNT +# define RETPORT_RETRY_COUNT 30 +#endif + +enum { REPORT_TYPE_NONE, REPORT_TYPE_KB, REPORT_TYPE_NKRO, REPORT_TYPE_CONSUMER }; + +typedef struct { + uint8_t type; + union { + report_keyboard_t keyboard; + uint16_t consumer; + }; +} report_buffer_t; + +void report_buffer_init(void); +bool report_buffer_enqueue(report_buffer_t *report); +bool report_buffer_dequeue(report_buffer_t *report); +bool report_buffer_is_empty(void); +void report_buffer_update_timer(void); +bool report_buffer_next_inverval(void); +void report_buffer_set_inverval(uint8_t interval); +uint8_t report_buffer_get_retry(void); +void report_buffer_set_retry(uint8_t times); +void report_buffer_task(void); diff --git a/keyboards/keychron/bluetooth/transport.c b/keyboards/keychron/bluetooth/transport.c new file mode 100644 index 0000000000..d011fb558f --- /dev/null +++ b/keyboards/keychron/bluetooth/transport.c @@ -0,0 +1,182 @@ + +#include "quantum.h" +#include "bluetooth.h" +#include "indicator.h" +#include "lpm.h" +#if defined(PROTOCOL_CHIBIOS) +# include +#endif +#include "transport.h" + +#ifndef REINIT_LED_DRIVER +# define REINIT_LED_DRIVER 1 +#endif + +#ifdef NKRO_ENABLE +typedef struct { + bool usb : 1; + bool bluetooth : 1; +} nkro_t; +#endif + +#if defined(PROTOCOL_CHIBIOS) +extern host_driver_t chibios_driver; +#endif +extern host_driver_t bluetooth_driver; +extern keymap_config_t keymap_config; + +static transport_t transport = TRANSPORT_USB; + +#ifdef NKRO_ENABLE +static nkro_t nkro = {false, false}; +#endif + +static void transport_changed(transport_t new_transport); + +__attribute__((weak)) void bt_transport_enable(bool enable) { + if (enable) { + if (host_get_driver() != &bluetooth_driver) { + host_set_driver(&bluetooth_driver); + + /* Disconnect and reconnect to sync the bluetooth state + * TODO: query bluetooth state to sync + */ + bluetooth_disconnect(); + bluetooth_connect(); + // TODO: Clear USB report + } + } else { + indicator_stop(); + + if (bluetooth_get_state() == BLUETOOTH_CONNECTED) { + report_keyboard_t empty_report = {0}; + bluetooth_driver.send_keyboard(&empty_report); + } + } +} + +/* There is no dedicated pin for USB power on chip such as STM32L432, but USB power + * can be connected and disconnected via registers. + * Overwrite these two functions if such chip is used. */ +__attribute__((weak)) void usb_power_connect(void) {} +__attribute__((weak)) void usb_power_disconnect(void) {} + +__attribute__((weak)) void usb_transport_enable(bool enable) { + if (enable) { + if (host_get_driver() != &chibios_driver) { + +#if !defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE) + usb_power_connect(); + usb_start(&USBD1); +#endif + host_set_driver(&chibios_driver); + } + } else { + if (USB_DRIVER.state == USB_ACTIVE) { + report_keyboard_t empty_report = {0}; + chibios_driver.send_keyboard(&empty_report); + } + +#if !defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE) + usbStop(&USBD1); + usbDisconnectBus(&USBD1); + usb_power_disconnect(); +#endif + } +} + +void set_transport(transport_t new_transport) { + if (transport != new_transport) { + transport = new_transport; + + clear_keyboard(); + + switch (transport) { + case TRANSPORT_USB: + usb_transport_enable(true); + bt_transport_enable(false); + lpm_timer_stop(); +#ifdef NKRO_ENABLE +# if defined(BLUETOOTH_NKRO_ENABLE) + nkro.bluetooth = keymap_config.nkro; +# endif + keymap_config.nkro = nkro.usb; +#endif + break; + + case TRANSPORT_BLUETOOTH: + bt_transport_enable(true); + usb_transport_enable(false); + lpm_timer_reset(); +#if defined(NKRO_ENABLE) + nkro.usb = keymap_config.nkro; +# if defined(BLUETOOTH_NKRO_ENABLE) + keymap_config.nkro = nkro.bluetooth; +# else + keymap_config.nkro = FALSE; +# endif +#endif + break; + default: + break; + } + + transport_changed(transport); + } +} + +transport_t get_transport(void) { + return transport; +} + +/* Changing transport may cause bronw-out reset of led driver + * withoug MCU reset, which lead backlight to not work, + * reinit the led driver workgound this issue */ +static void reinit_led_drvier(void) { + /* Wait circuit to discharge for a while */ + systime_t start = chVTGetSystemTime(); + while (chTimeI2MS(chVTTimeElapsedSinceX(start)) < 100) {}; + +#ifdef LED_MATRIX_ENABLE + led_matrix_init(); +#endif +#ifdef RGB_MATRIX_ENABLE + rgb_matrix_init(); +#endif +} + +void transport_changed(transport_t new_transport) { +#if (REINIT_LED_DRIVER) + reinit_led_drvier(); +#endif + +#if defined(RGB_MATRIX_ENABLE) && defined(RGB_DISABLE_TIMEOUT) +# if (RGB_DISABLE_TIMEOUT > 0) + rgb_matrix_disable_timeout_set(RGB_DISABLE_TIME_INFINITE); + rgb_matrix_disable_time_reset(); +# endif +#endif +#if defined(LED_MATRIX_ENABLE) && defined(LED_DISABLE_TIMEOUT) +# if (LED_DISABLE_TIMEOUT > 0) + led_matrix_disable_timeout_set(LED_DISABLE_TIME_INFINITE); + led_matrix_disable_time_reset(); +# endif +#endif +} + +void usb_remote_wakeup(void) { + if (USB_DRIVER.state == USB_SUSPENDED) { + while (USB_DRIVER.state == USB_SUSPENDED) { + /* Do this in the suspended state */ + suspend_power_down(); // on AVR this deep sleeps for 15ms + /* Remote wakeup */ + if (suspend_wakeup_condition()) { + usbWakeupHost(&USB_DRIVER); + usb_wakeup(&USB_DRIVER); + } + } + /* Woken up */ + // variables has been already cleared by the wakeup hook + send_keyboard_report(); + } +} diff --git a/keyboards/keychron/bluetooth/transport.h b/keyboards/keychron/bluetooth/transport.h new file mode 100644 index 0000000000..32fbf0b218 --- /dev/null +++ b/keyboards/keychron/bluetooth/transport.h @@ -0,0 +1,32 @@ +/* Copyright 2022 @ lokher (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +typedef enum { + TRANSPORT_NONE, + TRANSPORT_USB, + TRANSPORT_BLUETOOTH, +} transport_t; + +void set_transport(transport_t new_transport); +transport_t get_transport(void); + +void bt_transport_enable(bool enable); +void usb_power_connect(void); +void usb_power_disconnect(void); +void usb_transport_enable(bool enable); +void usb_remote_wakeup(void); diff --git a/keyboards/keychron/k8_pro/ansi/info.json b/keyboards/keychron/k8_pro/ansi/info.json new file mode 100644 index 0000000000..aa3824a88a --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/info.json @@ -0,0 +1,103 @@ +{ + "keyboard_name": "Keychron K8 Pro", + "url": "https://github.com/Keychron", + "maintainer": "lokher", + "layouts": { + "LAYOUT_ansi_87": { + "layout": [ + {"label":"Esc", "x":0, "y":0}, + {"label":"F1", "x":2, "y":0}, + {"label":"F2", "x":3, "y":0}, + {"label":"F3", "x":4, "y":0}, + {"label":"F4", "x":5, "y":0}, + {"label":"F5", "x":6.5, "y":0}, + {"label":"F5", "x":7.5, "y":0}, + {"label":"F7", "x":8.5, "y":0}, + {"label":"F8", "x":9.5, "y":0}, + {"label":"F9", "x":11, "y":0}, + {"label":"F10", "x":12, "y":0}, + {"label":"F11", "x":13, "y":0}, + {"label":"F12", "x":14, "y":0}, + {"label":"Print", "x":15.25, "y":0}, + {"label":"Cortana", "x":16.25, "y":0}, + {"label":"Light", "x":17.25, "y":0}, + + {"label":"`~", "x":0, "y":1.25}, + {"label":"1!", "x":1, "y":1.25}, + {"label":"2@", "x":2, "y":1.25}, + {"label":"3#", "x":3, "y":1.25}, + {"label":"4$", "x":4, "y":1.25}, + {"label":"5%", "x":5, "y":1.25}, + {"label":"6^", "x":6, "y":1.25}, + {"label":"7&", "x":7, "y":1.25}, + {"label":"8*", "x":8, "y":1.25}, + {"label":"9(", "x":9, "y":1.25}, + {"label":"0)", "x":10, "y":1.25}, + {"label":"-_", "x":11, "y":1.25}, + {"label":"=+", "x":12, "y":1.25}, + {"label":"Backspace", "x":13, "y":1.25, "w":2}, + {"label":"Insert", "x":15.25, "y":1.25}, + {"label":"Home", "x":16.25, "y":1.25}, + {"label":"Page Up", "x":17.25, "y":1.25}, + + {"label":"Tab", "x":0, "y":2.25, "w":1.5}, + {"label":"Q", "x":1.5, "y":2.25}, + {"label":"W", "x":2.5, "y":2.25}, + {"label":"E", "x":3.5, "y":2.25}, + {"label":"R", "x":4.5, "y":2.25}, + {"label":"T", "x":5.5, "y":2.25}, + {"label":"Y", "x":6.5, "y":2.25}, + {"label":"U", "x":7.5, "y":2.25}, + {"label":"I", "x":8.5, "y":2.25}, + {"label":"O", "x":9.5, "y":2.25}, + {"label":"P", "x":10.5, "y":2.25}, + {"label":"[{", "x":11.5, "y":2.25}, + {"label":"]}", "x":12.5, "y":2.25}, + {"label":"\\|", "x":13.5, "y":2.25, "w":1.5}, + {"label":"Delete", "x":15.25, "y":2.25}, + {"label":"End", "x":16.25, "y":2.25}, + {"label":"Page Down", "x":17.25, "y":2.25}, + + {"label":"Caps Lock", "x":0, "y":3.25, "w":1.75}, + {"label":"A", "x":1.75, "y":3.25}, + {"label":"S", "x":2.75, "y":3.25}, + {"label":"D", "x":3.75, "y":3.25}, + {"label":"F", "x":4.75, "y":3.25}, + {"label":"G", "x":5.75, "y":3.25}, + {"label":"H", "x":6.75, "y":3.25}, + {"label":"J", "x":7.75, "y":3.25}, + {"label":"K", "x":8.75, "y":3.25}, + {"label":"L", "x":9.75, "y":3.25}, + {"label":";:", "x":10.75, "y":3.25}, + {"label":"'\"", "x":11.75, "y":3.25}, + {"label":"Enter", "x":12.75, "y":3.25, "w":2.25}, + + {"label":"Left Shift", "x":0, "y":4.25, "w":2.25}, + {"label":"Z", "x":2.25, "y":4.25}, + {"label":"X", "x":3.25, "y":4.25}, + {"label":"C", "x":4.25, "y":4.25}, + {"label":"V", "x":5.25, "y":4.25}, + {"label":"B", "x":6.25, "y":4.25}, + {"label":"N", "x":7.25, "y":4.25}, + {"label":"M", "x":8.25, "y":4.25}, + {"label":",<", "x":9.25, "y":4.25}, + {"label":".>", "x":10.25, "y":4.25}, + {"label":"/?", "x":11.25, "y":4.25}, + {"label":"Right Shift", "x":12.25, "y":4.25, "w":2.75}, + {"label":"Up", "x":16.25, "y":4.25}, + + {"label":"Left Ctrl", "x":0, "y":5.25, "w":1.25}, + {"label":"Left Win", "x":1.25, "y":5.25, "w":1.25}, + {"label":"Left Alt", "x":2.5, "y":5.25, "w":1.25}, + {"label":"Space", "x":3.75, "y":5.25, "w":6.25}, + {"label":"Right Alt", "x":10, "y":5.25, "w":1.25}, + {"label":"Right Win", "x":11.25, "y":5.25, "w":1.25}, + {"label":"Fn", "x":12.5, "y":5.25, "w":1.25}, + {"label":"Right Ctrl", "x":13.75, "y":5.25, "w":1.25}, + {"label":"Left", "x":15.25, "y":5.25}, + {"label":"Down", "x":16.25, "y":5.25}, + {"label":"Right", "x":17.25, "y":5.25} + ] + } + } +} diff --git a/keyboards/keychron/k8_pro/ansi/rgb/config.h b/keyboards/keychron/k8_pro/ansi/rgb/config.h new file mode 100644 index 0000000000..b0ca2b1ef0 --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/rgb/config.h @@ -0,0 +1,78 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 . + */ + + +#define PRODUCT_ID 0x0280 +#define DEVICE_VER 0x0101 + +#ifdef RGB_MATRIX_ENABLE +/* RGB Matrix driver configuration */ +# define DRIVER_COUNT 2 + +# define DRIVER_ADDR_1 0b1110111 +# define DRIVER_ADDR_2 0b1110100 +# define DRIVER_1_LED_TOTAL 47 +# define DRIVER_2_LED_TOTAL 40 +# define DRIVER_LED_TOTAL (DRIVER_1_LED_TOTAL + DRIVER_2_LED_TOTAL) + +/* Set to infinit, which is use in USB mode by default */ +#define RGB_DISABLE_TIMEOUT RGB_DISABLE_TIME_INFINITE +/* Allow shutdown of led driver to save power */ +# define RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE +/* Turn off backlight on low brightness to save power */ +# define RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL 32 + +# ifdef VIA_ENABLE +# define VIA_QMK_RGBLIGHT_ENABLE +# endif + +/* RGB Matrix Animation modes. Explicitly enabled + * For full list of effects, see: + * https://docs.qmk.fm/#/feature_rgb_matrix?id=rgb-matrix-effects + */ + +# define RGB_MATRIX_KEYPRESSES +# define RGB_MATRIX_FRAMEBUFFER_EFFECTS +# define ENABLE_RGB_MATRIX_BREATHING +# define ENABLE_RGB_MATRIX_BAND_SPIRAL_VAL +# define ENABLE_RGB_MATRIX_CYCLE_ALL +# define ENABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT +# define ENABLE_RGB_MATRIX_CYCLE_UP_DOWN +# define ENABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON +# define ENABLE_RGB_MATRIX_CYCLE_OUT_IN +# define ENABLE_RGB_MATRIX_CYCLE_OUT_IN_DUAL +# define ENABLE_RGB_MATRIX_CYCLE_PINWHEEL +# define ENABLE_RGB_MATRIX_CYCLE_SPIRAL +# define ENABLE_RGB_MATRIX_DUAL_BEACON +# define ENABLE_RGB_MATRIX_RAINBOW_BEACON +# define ENABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS +# define ENABLE_RGB_MATRIX_PIXEL_RAIN + +/* enabled only if RGB_MATRIX_FRAMEBUFFER_EFFECTS is defined */ +# define ENABLE_RGB_MATRIX_TYPING_HEATMAP +# define ENABLE_RGB_MATRIX_DIGITAL_RAIN + +/* enabled only of RGB_MATRIX_KEYPRESSES or RGB_MATRIX_KEYRELEASES is defined*/ +# define ENABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE +# define ENABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE +# define ENABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS +# define ENABLE_RGB_MATRIX_SPLASH +# define ENABLE_RGB_MATRIX_SOLID_SPLASH + +/* Set LED driver current */ +#define CKLED2001_CURRENT_TUNE {0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38} + +#endif diff --git a/keyboards/keychron/k8_pro/ansi/rgb/keymaps/default/keymap.c b/keyboards/keychron/k8_pro/ansi/rgb/keymaps/default/keymap.c new file mode 100644 index 0000000000..e8fb59cc4c --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/rgb/keymaps/default/keymap.c @@ -0,0 +1,59 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 QMK_KEYBOARD_H + +enum layers{ + MAC_BASE, + MAC_FN, + WIN_BASE, + WIN_FN +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +[MAC_BASE] = LAYOUT_ansi_87( + KC_ESC, KC_BRID, KC_BRIU, KC_MCTL, KC_LNPD, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_SNAP, KC_SIRI, RGB_MOD, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LOPTN, KC_LCMMD, KC_SPC, KC_RCMMD, KC_ROPTN, MO(MAC_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[MAC_FN] = LAYOUT_ansi_87( + KC_TRNS, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_TRNS, KC_TRNS, RGB_TOG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, RGB_RMOD, RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), + +[WIN_BASE] = LAYOUT_ansi_87( + KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_CTANA, RGB_MOD, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RGUI, MO(WIN_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[WIN_FN] = LAYOUT_ansi_87( + KC_TRNS, KC_BRID, KC_BRIU, KC_TASK, KC_FILE, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_TRNS, KC_TRNS, RGB_TOG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, RGB_RMOD, RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS) + +}; \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/ansi/rgb/keymaps/via/keymap.c b/keyboards/keychron/k8_pro/ansi/rgb/keymaps/via/keymap.c new file mode 100644 index 0000000000..058e7f396c --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/rgb/keymaps/via/keymap.c @@ -0,0 +1,59 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 QMK_KEYBOARD_H + +enum layers{ + MAC_BASE, + MAC_FN, + WIN_BASE, + WIN_FN +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +[MAC_BASE] = LAYOUT_ansi_87( + KC_ESC, KC_BRID, KC_BRIU, KC_MCTL, KC_LNPD, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_SNAP, KC_SIRI, RGB_MOD, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LOPTN, KC_LCMMD, KC_SPC, KC_RCMMD, KC_ROPTN, MO(MAC_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[MAC_FN] = LAYOUT_ansi_87( + KC_TRNS, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_TRNS, KC_TRNS, RGB_TOG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, RGB_RMOD, RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), + +[WIN_BASE] = LAYOUT_ansi_87( + KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_CTANA, RGB_MOD, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RGUI, MO(WIN_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[WIN_FN] = LAYOUT_ansi_87( + KC_TRNS, KC_BRID, KC_BRIU, KC_TASK, KC_FILE, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_TRNS, KC_TRNS, RGB_TOG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, RGB_RMOD, RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS) + +}; diff --git a/keyboards/keychron/k8_pro/ansi/rgb/keymaps/via/rules.mk b/keyboards/keychron/k8_pro/ansi/rgb/keymaps/via/rules.mk new file mode 100644 index 0000000000..036bd6d1c3 --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/rgb/keymaps/via/rules.mk @@ -0,0 +1 @@ +VIA_ENABLE = yes \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/ansi/rgb/rgb.c b/keyboards/keychron/k8_pro/ansi/rgb/rgb.c new file mode 100644 index 0000000000..dfbbdc9d70 --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/rgb/rgb.c @@ -0,0 +1,148 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 "quantum.h" + +#ifdef RGB_MATRIX_ENABLE +const ckled2001_led g_ckled2001_leds[DRIVER_LED_TOTAL] = { +/* Refer to IS31 manual for these locations + * driver + * | R location + * | | G location + * | | | B location + * | | | | */ + {0, I_1, G_1, H_1}, + {0, G_2, H_2, I_2}, + {0, H_3, I_3, G_3}, + {0, I_4, G_4, H_4}, + {0, G_5, H_5, I_5}, + {0, H_6, I_6, G_6}, + {0, I_7, G_7, H_7}, + {0, G_8, H_8, I_8}, + {0, H_9, I_9, G_9}, + {0, I_10, G_10, H_10}, + {0, G_11, H_11, I_11}, + {0, H_12, I_12, G_12}, + {0, I_13, G_13, H_13}, + {0, H_15, I_15, G_15}, + {0, I_16, G_16, H_16}, + {1, A_2, B_2, C_2,}, + + {0, C_1, A_1, B_1}, + {0, A_2, B_2, C_2}, + {0, B_3, C_3, A_3}, + {0, C_4, A_4, B_4}, + {0, A_5, B_5, C_5}, + {0, B_6, C_6, A_6}, + {0, C_7, A_7, B_7}, + {0, A_8, B_8, C_8}, + {0, B_9, C_9, A_9}, + {0, C_10, A_10, B_10}, + {0, A_11, B_11, C_11}, + {0, B_12, C_12, A_12}, + {0, C_13, A_13, B_13}, + {0, A_14, B_14, C_14}, + {0, B_15, C_15, A_15}, + {0, C_16, A_16, B_16}, + {1, G_2, H_2, I_2}, + + {0, F_1, D_1, E_1}, + {0, D_2, E_2, F_2}, + {0, E_3, F_3, D_3}, + {0, F_4, D_4, E_4}, + {0, D_5, E_5, F_5}, + {0, E_6, F_6, D_6}, + {0, F_7, D_7, E_7}, + {0, D_8, E_8, F_8}, + {0, E_9, F_9, D_9}, + {0, F_10, D_10, E_10}, + {0, D_11, E_11, F_11}, + {0, E_12, F_12, D_12}, + {0, F_13, D_13, E_13}, + {0, D_14, E_14, F_14}, + {0, E_15, F_15, D_15}, + {0, F_16, D_16, E_16}, + {1, A_1, B_1, C_1}, + + {1, C_16, A_16, B_16}, + {1, A_15, B_15, C_15}, + {1, B_14, C_14, A_14}, + {1, C_13, A_13, B_13}, + {1, A_12, B_12, C_12}, + {1, B_11, C_11, A_11}, + {1, C_10, A_10, B_10}, + {1, A_9, B_9, C_9}, + {1, B_8, C_8, A_8}, + {1, C_7, A_7, B_7}, + {1, A_6, B_6, C_6}, + {1, B_5, C_5, A_5}, + {1, A_3, B_3, C_3}, + + {1, I_16, G_16, H_16}, + {1, H_14, I_14, G_14}, + {1, I_13, G_13, H_13}, + {1, G_12, H_12, I_12}, + {1, H_11, I_11, G_11}, + {1, I_10, G_10, H_10}, + {1, G_9, H_9, I_9}, + {1, H_8, I_8, G_8}, + {1, I_7, G_7, H_7}, + {1, G_6, H_6, I_6}, + {1, H_5, I_5, G_5}, + {1, G_3, H_3, I_3}, + {1, I_1, G_1, H_1}, + + {1, F_16, D_16, E_16}, + {1, D_15, E_15, F_15}, + {1, E_14, F_14, D_14}, + {1, F_10, D_10, E_10}, + {1, D_6, E_6, F_6}, + {1, E_5, F_5, D_5}, + {1, F_4, D_4, E_4}, + {1, D_3, E_3, F_3}, + {1, E_2, F_2, D_2}, + {1, F_1, D_1, E_1}, + {1, A_4, B_4, C_4} +}; + + +led_config_t g_led_config = { + { + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, NO_LED, 13, 14, 15 }, + { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 }, + { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 }, + { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, NO_LED, 62, NO_LED, NO_LED, NO_LED }, + { 63, NO_LED, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, NO_LED, 74, NO_LED, 75, NO_LED }, + { 76, 77, 78, NO_LED, NO_LED, NO_LED, 79, NO_LED, NO_LED, NO_LED, 80, 81, 82, 83, 84, 85, 86} + }, + { + {0, 0}, {25, 0}, {38, 0}, {51, 0}, {64, 0}, {84, 0}, { 97, 0}, {110, 0}, {123, 0}, {142, 0}, {155, 0}, {168, 0}, {181, 0}, {198, 0}, {211, 0}, {224, 0}, + {0,14}, {12,14}, {25,14}, {38,14}, {51,14}, {64,14}, {77,14}, { 90,14}, {103,14}, {116,14}, {129,14}, {142,14}, {155,14}, {175,14}, {198,14}, {211,14}, {224,14}, + {3,26}, {19,26}, {32,26}, {45,26}, {58,26}, {71,26}, {84,26}, { 97,26}, {110,26}, {123,26}, {136,26}, {149,26}, {162,26}, {178,26}, {198,26}, {211,26}, {224,26}, + {4,39}, {22,39}, {35,39}, {48,39}, {61,39}, {74,39}, {87,39}, {100,39}, {113,39}, {126,39}, {139,39}, {152,39}, {173,39}, + {8,51}, {29,51}, {42,51}, {55,51}, {68,51}, {81,51}, {94,51}, {107,51}, {120,51}, {132,51}, {145,51}, {170,51}, {211,51}, + {1,64}, {17,64}, {34,64}, {82,64}, {131,64}, {147,64}, {163,64}, {180,64}, {198,64}, {211,64}, {224,64} + }, + { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 + } +}; +#endif \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/ansi/rgb/rules.mk b/keyboards/keychron/k8_pro/ansi/rgb/rules.mk new file mode 100644 index 0000000000..5975e7843e --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/rgb/rules.mk @@ -0,0 +1,3 @@ +RGB_MATRIX_ENABLE = yes +RGB_MATRIX_DRIVER = CKLED2001 + diff --git a/keyboards/keychron/k8_pro/ansi/white/config.h b/keyboards/keychron/k8_pro/ansi/white/config.h new file mode 100644 index 0000000000..1c008bc0b5 --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/white/config.h @@ -0,0 +1,64 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 . + */ + + +#define PRODUCT_ID 0x0283 +#define DEVICE_VER 0x0101 + +#ifdef LED_MATRIX_ENABLE +/* LED matrix driver configuration */ +# define DRIVER_COUNT 1 +# define DRIVER_ADDR_1 0b1110100 +# define DRIVER_LED_TOTAL 87 + +/* Set to infinit, which is use in USB mode by default */ +#define LED_DISABLE_TIMEOUT LED_DISABLE_TIME_INFINITE +/* Allow shutdown of led driver to save power */ +# define LED_MATRIX_DRIVER_SHUTDOWN_ENABLE +/* Turn off backlight on low brightness to save power */ +# define LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL 32 + +/* LED Matrix Animation modes. Explicitly enabled + * For full list of effects, see: + * https://docs.qmk.fm/#/feature_led_matrix?id=led-matrix-effects + */ +# define LED_MATRIX_KEYPRESSES + +# define ENABLE_LED_MATRIX_NONE +# define ENABLE_LED_MATRIX_SOLID +# define ENABLE_LED_MATRIX_BREATHING +# define ENABLE_LED_MATRIX_BAND_PINWHEEL +# define ENABLE_LED_MATRIX_BAND_SPIRAL +# define ENABLE_LED_MATRIX_CYCLE_LEFT_RIGHT +# define ENABLE_LED_MATRIX_CYCLE_UP_DOWN +# define ENABLE_LED_MATRIX_CYCLE_OUT_IN +# define ENABLE_LED_MATRIX_DUAL_BEACON +# define ENABLE_LED_MATRIX_SOLID_REACTIVE_SIMPLE +# define ENABLE_LED_MATRIX_SOLID_REACTIVE_MULTIWIDE +# define ENABLE_LED_MATRIX_SOLID_REACTIVE_MULTINEXUS +# define ENABLE_LED_MATRIX_SOLID_SPLASH +# define ENABLE_LED_MATRIX_WAVE_LEFT_RIGHT +# define ENABLE_LED_MATRIX_WAVE_UP_DOWN +# define ENABLE_LED_MATRIX_EFFECT_MAX + +/* Use first 6 channels of LED driver */ +#define PHASE_CHANNEL MSKPHASE_6CHANNEL + +/* Set LED driver current */ +#define CKLED2001_CURRENT_TUNE { 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60 } + + +#endif diff --git a/keyboards/keychron/k8_pro/ansi/white/keymaps/default/keymap.c b/keyboards/keychron/k8_pro/ansi/white/keymaps/default/keymap.c new file mode 100644 index 0000000000..508cea5648 --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/white/keymaps/default/keymap.c @@ -0,0 +1,59 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 QMK_KEYBOARD_H + +enum layers{ + MAC_BASE, + MAC_FN, + WIN_BASE, + WIN_FN +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +[MAC_BASE] = LAYOUT_ansi_87( + KC_ESC, KC_BRID, KC_BRIU, KC_MCTL, KC_LNPD, BL_DEC, BL_INC, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_SNAP, KC_SIRI, BL_STEP, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LOPTN, KC_LCMMD, KC_SPC, KC_RCMMD, KC_ROPTN, MO(MAC_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[MAC_FN] = LAYOUT_ansi_87( + KC_TRNS, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_TRNS, KC_TRNS, BL_TOGG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + BL_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), + +[WIN_BASE] = LAYOUT_ansi_87( + KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_CTANA, BL_STEP, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RGUI, MO(WIN_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[WIN_FN] = LAYOUT_ansi_87( + KC_TRNS, KC_BRID, KC_BRIU, KC_TASK, KC_FILE, BL_DEC, BL_INC, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_TRNS, KC_TRNS, BL_TOGG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + BL_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS) + +}; \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/ansi/white/keymaps/via/keymap.c b/keyboards/keychron/k8_pro/ansi/white/keymaps/via/keymap.c new file mode 100644 index 0000000000..4ec6db80fd --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/white/keymaps/via/keymap.c @@ -0,0 +1,59 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 QMK_KEYBOARD_H + +enum layers{ + MAC_BASE, + MAC_FN, + WIN_BASE, + WIN_FN +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +[MAC_BASE] = LAYOUT_ansi_87( + KC_ESC, KC_BRID, KC_BRIU, KC_MCTL, KC_LNPD, BL_DEC, BL_INC, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_SNAP, KC_SIRI, BL_STEP, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LOPTN, KC_LCMMD, KC_SPC, KC_RCMMD, KC_ROPTN, MO(MAC_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[MAC_FN] = LAYOUT_ansi_87( + KC_TRNS, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_TRNS, KC_TRNS, BL_TOGG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + BL_TOGG, BL_STEP, BL_INC, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, BL_DEC, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), + +[WIN_BASE] = LAYOUT_ansi_87( + KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_CTANA, BL_STEP, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RGUI, MO(WIN_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[WIN_FN] = LAYOUT_ansi_87( + KC_TRNS, KC_BRID, KC_BRIU, KC_TASK, KC_FILE, BL_DEC, BL_INC, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_TRNS, KC_TRNS, BL_TOGG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + BL_TOGG, BL_STEP, BL_INC, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, BL_DEC, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS) + +}; diff --git a/keyboards/keychron/k8_pro/ansi/white/keymaps/via/rules.mk b/keyboards/keychron/k8_pro/ansi/white/keymaps/via/rules.mk new file mode 100644 index 0000000000..036bd6d1c3 --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/white/keymaps/via/rules.mk @@ -0,0 +1 @@ +VIA_ENABLE = yes \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/ansi/white/rules.mk b/keyboards/keychron/k8_pro/ansi/white/rules.mk new file mode 100644 index 0000000000..6b0c2a0769 --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/white/rules.mk @@ -0,0 +1,2 @@ +LED_MATRIX_ENABLE = yes +LED_MATRIX_DRIVER = CKLED2001 \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/ansi/white/white.c b/keyboards/keychron/k8_pro/ansi/white/white.c new file mode 100644 index 0000000000..2b8cd893d6 --- /dev/null +++ b/keyboards/keychron/k8_pro/ansi/white/white.c @@ -0,0 +1,147 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 "quantum.h" + +#ifdef LED_MATRIX_ENABLE +const ckled2001_led g_ckled2001_leds[DRIVER_LED_TOTAL] = { +/* Refer to IS31 manual for these locations + * driver + * | LED address + * | | */ + {0, A_16}, + {0, A_15}, + {0, A_14}, + {0, A_13}, + {0, A_12}, + {0, A_11}, + {0, A_10}, + {0, A_9}, + {0, A_8}, + {0, A_7}, + {0, A_6}, + {0, A_5}, + {0, A_4}, + {0, A_2}, + {0, A_1}, + {0, D_2}, + + {0, B_16}, + {0, B_15}, + {0, B_14}, + {0, B_13}, + {0, B_12}, + {0, B_11}, + {0, B_10}, + {0, B_9}, + {0, B_8}, + {0, B_7}, + {0, B_6}, + {0, B_5}, + {0, B_4}, + {0, B_3}, + {0, B_2}, + {0, B_1}, + {0, E_2}, + + {0, C_16}, + {0, C_15}, + {0, C_14}, + {0, C_13}, + {0, C_12}, + {0, C_11}, + {0, C_10}, + {0, C_9}, + {0, C_8}, + {0, C_7}, + {0, C_6}, + {0, C_5}, + {0, C_4}, + {0, C_3}, + {0, C_2}, + {0, C_1}, + {0, D_1}, + + {0, D_16}, + {0, D_15}, + {0, D_14}, + {0, D_13}, + {0, D_12}, + {0, D_11}, + {0, D_10}, + {0, D_9}, + {0, D_8}, + {0, D_7}, + {0, D_6}, + {0, D_5}, + {0, D_3}, + + {0, E_16}, + {0, E_14}, + {0, E_13}, + {0, E_12}, + {0, E_11}, + {0, E_10}, + {0, E_9}, + {0, E_8}, + {0, E_7}, + {0, E_6}, + {0, E_5}, + {0, E_3}, + {0, E_1}, + + {0, F_16}, + {0, F_15}, + {0, F_14}, + {0, F_10}, + {0, F_6}, + {0, F_5}, + {0, F_4}, + {0, F_3}, + {0, F_2}, + {0, F_1}, + {0, D_4}, +}; + + +led_config_t g_led_config = { + { + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, NO_LED, 13, 14, 15 }, + { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 }, + { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 }, + { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, NO_LED, 62, NO_LED, NO_LED, NO_LED }, + { 63, NO_LED, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, NO_LED, 74, NO_LED, 75, NO_LED }, + { 76, 77, 78, NO_LED, NO_LED, NO_LED, 79, NO_LED, NO_LED, NO_LED, 80, 81, 82, 83, 84, 85, 86} + }, + { + {0, 0}, {25, 0}, {38, 0}, {51, 0}, {64, 0}, {84, 0}, { 97, 0}, {110, 0}, {123, 0}, {142, 0}, {155, 0}, {168, 0}, {181, 0}, {198, 0}, {211, 0}, {224, 0}, + {0,14}, {12,14}, {25,14}, {38,14}, {51,14}, {64,14}, {77,14}, { 90,14}, {103,14}, {116,14}, {129,14}, {142,14}, {155,14}, {175,14}, {198,14}, {211,14}, {224,14}, + {3,26}, {19,26}, {32,26}, {45,26}, {58,26}, {71,26}, {84,26}, { 97,26}, {110,26}, {123,26}, {136,26}, {149,26}, {162,26}, {178,26}, {198,26}, {211,26}, {224,26}, + {4,39}, {22,39}, {35,39}, {48,39}, {61,39}, {74,39}, {87,39}, {100,39}, {113,39}, {126,39}, {139,39}, {152,39}, {173,39}, + {8,51}, {29,51}, {42,51}, {55,51}, {68,51}, {81,51}, {94,51}, {107,51}, {120,51}, {132,51}, {145,51}, {170,51}, {211,51}, + {1,64}, {17,64}, {34,64}, {82,64}, {131,64}, {147,64}, {163,64}, {180,64}, {198,64}, {211,64}, {224,64} + }, + { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 + } +}; + +#endif \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/config.h b/keyboards/keychron/k8_pro/config.h new file mode 100644 index 0000000000..3379c147b8 --- /dev/null +++ b/keyboards/keychron/k8_pro/config.h @@ -0,0 +1,106 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +/* USB Device descriptor parameter */ +#define VENDOR_ID 0x3434 +#define MANUFACTURER Keychron +#define PRODUCT Keychron K8 Pro + +/* key matrix size */ +#define MATRIX_ROWS 6 +#define MATRIX_COLS 17 + +/* Key matrix pins */ +#define MATRIX_ROW_PINS \ + { B5, B4, B3, A15, A14, A13 } +#define MATRIX_COL_PINS \ + { B0, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN, NO_PIN } + +/* COL2ROW or ROW2COL */ +#define DIODE_DIRECTION ROW2COL + +/* Turn off effects when suspended */ +#define RGB_DISABLE_WHEN_USB_SUSPENDED + +/* Set 0 if debouncing isn't needed */ +#define DEBOUNCE 5 + +/* DIP switch for Mac/win OS switch */ +#define DIP_SWITCH_PINS \ + { A8 } + +/* Caps lock LED Pin */ +#define LED_CAPS_LOCK_PIN A7 +#define LED_PIN_ON_STATE 1 + +#ifdef BLUETOOTH_ENABLE +/* Hardware configuration */ +# define USB_BT_MODE_SELECT_PIN A10 + +# define CKBT51_RESET_PIN A9 +# define CKBT51_INT_INPUT_PIN A5 +# define BLUETOOTH_INT_INPUT_PIN A6 + +# define USB_POWER_SENSE_PIN B1 +# define USB_POWER_CONNECTED_LEVEL 0 + +# define BAT_LOW_LED_PIN A4 +# define BAT_LOW_LED_PIN_ON_STATE 1 + +# define HOST_DEVICES_COUNT 3 + +# define HOST_LED_PIN_LIST \ + { H3, H3, H3 } +# define HOST_LED_PIN_ON_STATE 1 + +# if defined(RGB_MATRIX_ENABLE) || defined(LED_MATRIX_ENABLE) + +# define LED_DRIVER_SHUTDOWN_PIN C14 + +# define HOST_LED_MATRIX_LIST \ + { 17, 18, 19 } + +# define BAT_LEVEL_LED_LIST \ + { 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 } + +/* Backlit disable timeout when keyboard is disconnected(unit: second) */ +# define DISCONNECTED_BACKLIGHT_DISABLE_TIMEOUT 40 + +/* Backlit disable timeout when keyboard is connected(unit: second) */ +# define CONNECTED_BACKLIGHT_DISABLE_TIMEOUT 600 +# endif + +/* Keep USB connection in blueooth mode */ +# define KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE + +/* Enable bluetooth NKRO */ +# define BLUETOOTH_NKRO_ENABLE + +/* Raw hid command for factory test and bluetooth DFU */ +# define RAW_HID_CMD 0xAA ... 0xAB +#else +/* Raw hid command for factory test */ +# define RAW_HID_CMD 0xAB +#endif + +/* Emulated EEPROM configuration */ +#define FEE_DENSITY_BYTES FEE_PAGE_SIZE +#define DYNAMIC_KEYMAP_EEPROM_MAX_ADDR 2047 + + +//#define NO_PRINT diff --git a/keyboards/keychron/k8_pro/halconf.h b/keyboards/keychron/k8_pro/halconf.h new file mode 100644 index 0000000000..1a36798b0e --- /dev/null +++ b/keyboards/keychron/k8_pro/halconf.h @@ -0,0 +1,27 @@ +/* Copyright 2020 QMK + * + * 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 . + */ + +#pragma once + +#define HAL_USE_I2C TRUE + +#ifdef BLUETOOTH_ENABLE +# define PAL_USE_CALLBACKS TRUE +# define HAL_USE_SERIAL TRUE +#endif + +#include_next + diff --git a/keyboards/keychron/k8_pro/iso/info.json b/keyboards/keychron/k8_pro/iso/info.json new file mode 100644 index 0000000000..d56bfe5533 --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/info.json @@ -0,0 +1,104 @@ +{ + "keyboard_name": "Keychron K8 Pro", + "url": "https://github.com/Keychron", + "maintainer": "lokher", + "layouts": { + "LAYOUT_iso_88": { + "layout": [ + {"label":"Esc", "x":0, "y":0}, + {"label":"F1", "x":2, "y":0}, + {"label":"F2", "x":3, "y":0}, + {"label":"F3", "x":4, "y":0}, + {"label":"F4", "x":5, "y":0}, + {"label":"F5", "x":6.5, "y":0}, + {"label":"F5", "x":7.5, "y":0}, + {"label":"F7", "x":8.5, "y":0}, + {"label":"F8", "x":9.5, "y":0}, + {"label":"F9", "x":11, "y":0}, + {"label":"F10", "x":12, "y":0}, + {"label":"F11", "x":13, "y":0}, + {"label":"F12", "x":14, "y":0}, + {"label":"Print", "x":15.25, "y":0}, + {"label":"Cortana", "x":16.25, "y":0}, + {"label":"Light", "x":17.25, "y":0}, + + {"label":"`~", "x":0, "y":1.25}, + {"label":"1!", "x":1, "y":1.25}, + {"label":"2@", "x":2, "y":1.25}, + {"label":"3#", "x":3, "y":1.25}, + {"label":"4$", "x":4, "y":1.25}, + {"label":"5%", "x":5, "y":1.25}, + {"label":"6^", "x":6, "y":1.25}, + {"label":"7&", "x":7, "y":1.25}, + {"label":"8*", "x":8, "y":1.25}, + {"label":"9(", "x":9, "y":1.25}, + {"label":"0)", "x":10, "y":1.25}, + {"label":"-_", "x":11, "y":1.25}, + {"label":"=+", "x":12, "y":1.25}, + {"label":"Backspace", "x":13, "y":1.25, "w":2}, + {"label":"Insert", "x":15.25, "y":1.25}, + {"label":"Home", "x":16.25, "y":1.25}, + {"label":"Page Up", "x":17.25, "y":1.25}, + + {"label":"Tab", "x":0, "y":2.25, "w":1.5}, + {"label":"Q", "x":1.5, "y":2.25}, + {"label":"W", "x":2.5, "y":2.25}, + {"label":"E", "x":3.5, "y":2.25}, + {"label":"R", "x":4.5, "y":2.25}, + {"label":"T", "x":5.5, "y":2.25}, + {"label":"Y", "x":6.5, "y":2.25}, + {"label":"U", "x":7.5, "y":2.25}, + {"label":"I", "x":8.5, "y":2.25}, + {"label":"O", "x":9.5, "y":2.25}, + {"label":"P", "x":10.5, "y":2.25}, + {"label":"[{", "x":11.5, "y":2.25}, + {"label":"]}", "x":12.5, "y":2.25}, + {"label":"Delete", "x":15.25, "y":2.25}, + {"label":"End", "x":16.25, "y":2.25}, + {"label":"Page Down", "x":17.25, "y":2.25}, + + {"label":"Caps Lock", "x":0, "y":3.25, "w":1.75}, + {"label":"A", "x":1.75, "y":3.25}, + {"label":"S", "x":2.75, "y":3.25}, + {"label":"D", "x":3.75, "y":3.25}, + {"label":"F", "x":4.75, "y":3.25}, + {"label":"G", "x":5.75, "y":3.25}, + {"label":"H", "x":6.75, "y":3.25}, + {"label":"J", "x":7.75, "y":3.25}, + {"label":"K", "x":8.75, "y":3.25}, + {"label":"L", "x":9.75, "y":3.25}, + {"label":";:", "x":10.75, "y":3.25}, + {"label":"'\"", "x":11.75, "y":3.25}, + {"label":"#~", "x":12.75, "y":3.25}, + {"label":"Enter", "x":13.75, "y":2.25, "w":1.25, "h":2}, + + {"label":"Left Shift", "x":0, "y":4.25, "w":1.25}, + {"label":"\\|", "x":1.25, "y":4.25}, + {"label":"Z", "x":2.25, "y":4.25}, + {"label":"X", "x":3.25, "y":4.25}, + {"label":"C", "x":4.25, "y":4.25}, + {"label":"V", "x":5.25, "y":4.25}, + {"label":"B", "x":6.25, "y":4.25}, + {"label":"N", "x":7.25, "y":4.25}, + {"label":"M", "x":8.25, "y":4.25}, + {"label":",<", "x":9.25, "y":4.25}, + {"label":".>", "x":10.25, "y":4.25}, + {"label":"/?", "x":11.25, "y":4.25}, + {"label":"Right Shift", "x":12.25, "y":4.25, "w":2.75}, + {"label":"Up", "x":16.25, "y":4.25}, + + {"label":"Left Ctrl", "x":0, "y":5.25, "w":1.25}, + {"label":"Left Win", "x":1.25, "y":5.25, "w":1.25}, + {"label":"Left Alt", "x":2.5, "y":5.25, "w":1.25}, + {"label":"Space", "x":3.75, "y":5.25, "w":6.25}, + {"label":"Right Alt", "x":10, "y":5.25, "w":1.25}, + {"label":"Right Win", "x":11.25, "y":5.25, "w":1.25}, + {"label":"Fn", "x":12.5, "y":5.25, "w":1.25}, + {"label":"Right Ctrl", "x":13.75, "y":5.25, "w":1.25}, + {"label":"Left", "x":15.25, "y":5.25}, + {"label":"Down", "x":16.25, "y":5.25}, + {"label":"Right", "x":17.25, "y":5.25} + ] + } + } +} diff --git a/keyboards/keychron/k8_pro/iso/rgb/config.h b/keyboards/keychron/k8_pro/iso/rgb/config.h new file mode 100644 index 0000000000..e43c591dbe --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/rgb/config.h @@ -0,0 +1,78 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 . + */ + + +#define PRODUCT_ID 0x0281 +#define DEVICE_VER 0x0101 + +#ifdef RGB_MATRIX_ENABLE +/* RGB Matrix driver configuration */ +# define DRIVER_COUNT 2 + +# define DRIVER_ADDR_1 0b1110111 +# define DRIVER_ADDR_2 0b1110100 +# define DRIVER_1_LED_TOTAL 47 +# define DRIVER_2_LED_TOTAL 41 +# define DRIVER_LED_TOTAL (DRIVER_1_LED_TOTAL + DRIVER_2_LED_TOTAL) + +/* Set to infinit, which is use in USB mode by default */ +#define RGB_DISABLE_TIMEOUT RGB_DISABLE_TIME_INFINITE +/* Allow shutdown of led driver to save power */ +# define RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE +/* Turn off backlight on low brightness to save power */ +# define RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL 32 + +# ifdef VIA_ENABLE +# define VIA_QMK_RGBLIGHT_ENABLE +# endif + +/* RGB Matrix Animation modes. Explicitly enabled + * For full list of effects, see: + * https://docs.qmk.fm/#/feature_rgb_matrix?id=rgb-matrix-effects + */ + +# define RGB_MATRIX_KEYPRESSES +# define RGB_MATRIX_FRAMEBUFFER_EFFECTS +# define ENABLE_RGB_MATRIX_BREATHING +# define ENABLE_RGB_MATRIX_BAND_SPIRAL_VAL +# define ENABLE_RGB_MATRIX_CYCLE_ALL +# define ENABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT +# define ENABLE_RGB_MATRIX_CYCLE_UP_DOWN +# define ENABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON +# define ENABLE_RGB_MATRIX_CYCLE_OUT_IN +# define ENABLE_RGB_MATRIX_CYCLE_OUT_IN_DUAL +# define ENABLE_RGB_MATRIX_CYCLE_PINWHEEL +# define ENABLE_RGB_MATRIX_CYCLE_SPIRAL +# define ENABLE_RGB_MATRIX_DUAL_BEACON +# define ENABLE_RGB_MATRIX_RAINBOW_BEACON +# define ENABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS +# define ENABLE_RGB_MATRIX_PIXEL_RAIN + +/* enabled only if RGB_MATRIX_FRAMEBUFFER_EFFECTS is defined */ +# define ENABLE_RGB_MATRIX_TYPING_HEATMAP +# define ENABLE_RGB_MATRIX_DIGITAL_RAIN + +/* enabled only of RGB_MATRIX_KEYPRESSES or RGB_MATRIX_KEYRELEASES is defined*/ +# define ENABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE +# define ENABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE +# define ENABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS +# define ENABLE_RGB_MATRIX_SPLASH +# define ENABLE_RGB_MATRIX_SOLID_SPLASH + +/* Set LED driver current */ +#define CKLED2001_CURRENT_TUNE {0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38, 0x38} + +#endif diff --git a/keyboards/keychron/k8_pro/iso/rgb/keymaps/default/keymap.c b/keyboards/keychron/k8_pro/iso/rgb/keymaps/default/keymap.c new file mode 100644 index 0000000000..379a01d456 --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/rgb/keymaps/default/keymap.c @@ -0,0 +1,59 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 QMK_KEYBOARD_H + +enum layers{ + MAC_BASE, + MAC_FN, + WIN_BASE, + WIN_FN +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +[MAC_BASE] = LAYOUT_iso_88( + KC_ESC, KC_BRID, KC_BRIU, KC_MCTL, KC_LNPD, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_SNAP, KC_SIRI, RGB_MOD, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LOPTN, KC_LCMMD, KC_SPC, KC_RCMMD, KC_ROPTN, MO(MAC_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[MAC_FN] = LAYOUT_iso_88( + KC_TRNS, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_TRNS, KC_TRNS, RGB_TOG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, RGB_RMOD, RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), + +[WIN_BASE] = LAYOUT_iso_88( + KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_CTANA, RGB_MOD, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RGUI, MO(WIN_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[WIN_FN] = LAYOUT_iso_88( + KC_TRNS, KC_BRID, KC_BRIU, KC_TASK, KC_FILE, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_TRNS, KC_TRNS, RGB_TOG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, RGB_RMOD, RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS) + +}; diff --git a/keyboards/keychron/k8_pro/iso/rgb/keymaps/via/keymap.c b/keyboards/keychron/k8_pro/iso/rgb/keymaps/via/keymap.c new file mode 100644 index 0000000000..379a01d456 --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/rgb/keymaps/via/keymap.c @@ -0,0 +1,59 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 QMK_KEYBOARD_H + +enum layers{ + MAC_BASE, + MAC_FN, + WIN_BASE, + WIN_FN +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +[MAC_BASE] = LAYOUT_iso_88( + KC_ESC, KC_BRID, KC_BRIU, KC_MCTL, KC_LNPD, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_SNAP, KC_SIRI, RGB_MOD, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LOPTN, KC_LCMMD, KC_SPC, KC_RCMMD, KC_ROPTN, MO(MAC_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[MAC_FN] = LAYOUT_iso_88( + KC_TRNS, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_TRNS, KC_TRNS, RGB_TOG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, RGB_RMOD, RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), + +[WIN_BASE] = LAYOUT_iso_88( + KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_CTANA, RGB_MOD, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RGUI, MO(WIN_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[WIN_FN] = LAYOUT_iso_88( + KC_TRNS, KC_BRID, KC_BRIU, KC_TASK, KC_FILE, RGB_VAD, RGB_VAI, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_TRNS, KC_TRNS, RGB_TOG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + RGB_TOG, RGB_MOD, RGB_VAI, RGB_HUI, RGB_SAI, RGB_SPI, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, RGB_RMOD, RGB_VAD, RGB_HUD, RGB_SAD, RGB_SPD, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS) + +}; diff --git a/keyboards/keychron/k8_pro/iso/rgb/keymaps/via/rules.mk b/keyboards/keychron/k8_pro/iso/rgb/keymaps/via/rules.mk new file mode 100644 index 0000000000..036bd6d1c3 --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/rgb/keymaps/via/rules.mk @@ -0,0 +1 @@ +VIA_ENABLE = yes \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/iso/rgb/rgb.c b/keyboards/keychron/k8_pro/iso/rgb/rgb.c new file mode 100644 index 0000000000..199cac3317 --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/rgb/rgb.c @@ -0,0 +1,149 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 "quantum.h" + +#ifdef RGB_MATRIX_ENABLE +const ckled2001_led g_ckled2001_leds[DRIVER_LED_TOTAL] = { +/* Refer to IS31 manual for these locations + * driver + * | R location + * | | G location + * | | | B location + * | | | | */ + {0, I_1, G_1, H_1}, + {0, G_2, H_2, I_2}, + {0, H_3, I_3, G_3}, + {0, I_4, G_4, H_4}, + {0, G_5, H_5, I_5}, + {0, H_6, I_6, G_6}, + {0, I_7, G_7, H_7}, + {0, G_8, H_8, I_8}, + {0, H_9, I_9, G_9}, + {0, I_10, G_10, H_10}, + {0, G_11, H_11, I_11}, + {0, H_12, I_12, G_12}, + {0, I_13, G_13, H_13}, + {0, H_15, I_15, G_15}, + {0, I_16, G_16, H_16}, + {1, A_2, B_2, C_2,}, + + {0, C_1, A_1, B_1}, + {0, A_2, B_2, C_2}, + {0, B_3, C_3, A_3}, + {0, C_4, A_4, B_4}, + {0, A_5, B_5, C_5}, + {0, B_6, C_6, A_6}, + {0, C_7, A_7, B_7}, + {0, A_8, B_8, C_8}, + {0, B_9, C_9, A_9}, + {0, C_10, A_10, B_10}, + {0, A_11, B_11, C_11}, + {0, B_12, C_12, A_12}, + {0, C_13, A_13, B_13}, + {0, A_14, B_14, C_14}, + {0, B_15, C_15, A_15}, + {0, C_16, A_16, B_16}, + {1, G_2, H_2, I_2}, + + {0, F_1, D_1, E_1}, + {0, D_2, E_2, F_2}, + {0, E_3, F_3, D_3}, + {0, F_4, D_4, E_4}, + {0, D_5, E_5, F_5}, + {0, E_6, F_6, D_6}, + {0, F_7, D_7, E_7}, + {0, D_8, E_8, F_8}, + {0, E_9, F_9, D_9}, + {0, F_10, D_10, E_10}, + {0, D_11, E_11, F_11}, + {0, E_12, F_12, D_12}, + {0, F_13, D_13, E_13}, + {0, D_14, E_14, F_14}, + {0, E_15, F_15, D_15}, + {0, F_16, D_16, E_16}, + {1, A_1, B_1, C_1}, + + {1, C_16, A_16, B_16}, + {1, A_15, B_15, C_15}, + {1, B_14, C_14, A_14}, + {1, C_13, A_13, B_13}, + {1, A_12, B_12, C_12}, + {1, B_11, C_11, A_11}, + {1, C_10, A_10, B_10}, + {1, A_9, B_9, C_9}, + {1, B_8, C_8, A_8}, + {1, C_7, A_7, B_7}, + {1, A_6, B_6, C_6}, + {1, B_5, C_5, A_5}, + {1, A_3, B_3, C_3}, + + {1, I_16, G_16, H_16}, + {1, G_15, H_15, I_15}, + {1, H_14, I_14, G_14}, + {1, I_13, G_13, H_13}, + {1, G_12, H_12, I_12}, + {1, H_11, I_11, G_11}, + {1, I_10, G_10, H_10}, + {1, G_9, H_9, I_9}, + {1, H_8, I_8, G_8}, + {1, I_7, G_7, H_7}, + {1, G_6, H_6, I_6}, + {1, H_5, I_5, G_5}, + {1, G_3, H_3, I_3}, + {1, I_1, G_1, H_1}, + + {1, F_16, D_16, E_16}, + {1, D_15, E_15, F_15}, + {1, E_14, F_14, D_14}, + {1, F_10, D_10, E_10}, + {1, D_6, E_6, F_6}, + {1, E_5, F_5, D_5}, + {1, F_4, D_4, E_4}, + {1, D_3, E_3, F_3}, + {1, E_2, F_2, D_2}, + {1, F_1, D_1, E_1}, + {1, A_4, B_4, C_4} +}; + + +led_config_t g_led_config = { + { + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, NO_LED, 13, 14, 15 }, + { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 }, + { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 }, + { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, NO_LED, 62, NO_LED, NO_LED, NO_LED }, + { 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, NO_LED, 75, NO_LED, 76, NO_LED }, + { 77, 78, 79, NO_LED, NO_LED, NO_LED, 80, NO_LED, NO_LED, NO_LED, 81, 82, 83, 84, 85, 86, 87} + }, + { + {0, 0}, {25, 0}, {38, 0}, {51, 0}, {64, 0}, {84, 0}, { 97, 0}, {110, 0}, {123, 0}, {142, 0}, {155, 0}, {168, 0}, {181, 0}, {198, 0}, {211, 0}, {224, 0}, + {0,14}, {12,14}, {25,14}, {38,14}, {51,14}, {64,14}, {77,14}, { 90,14}, {103,14}, {116,14}, {129,14}, {142,14}, {155,14}, {175,14}, {198,14}, {211,14}, {224,14}, + {3,26}, {19,26}, {32,26}, {45,26}, {58,26}, {71,26}, {84,26}, { 97,26}, {110,26}, {123,26}, {136,26}, {149,26}, {162,26}, {178,26}, {198,26}, {211,26}, {224,26}, + {4,39}, {22,39}, {35,39}, {48,39}, {61,39}, {74,39}, {87,39}, {100,39}, {113,39}, {126,39}, {139,39}, {152,39}, {173,39}, + {0,51}, {16,51}, {29,51}, {42,51}, {55,51}, {68,51}, {81,51}, {94,51}, {107,51}, {120,51}, {132,51}, {145,51}, {170,51}, {211,51}, + {1,64}, {17,64}, {34,64}, {82,64}, {131,64}, {147,64}, {163,64}, {180,64}, {198,64}, {211,64}, {224,64} + }, + { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 + } +}; +#endif \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/iso/rgb/rules.mk b/keyboards/keychron/k8_pro/iso/rgb/rules.mk new file mode 100644 index 0000000000..5975e7843e --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/rgb/rules.mk @@ -0,0 +1,3 @@ +RGB_MATRIX_ENABLE = yes +RGB_MATRIX_DRIVER = CKLED2001 + diff --git a/keyboards/keychron/k8_pro/iso/white/config.h b/keyboards/keychron/k8_pro/iso/white/config.h new file mode 100644 index 0000000000..99e93b6486 --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/white/config.h @@ -0,0 +1,63 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 . + */ + + +#define PRODUCT_ID 0x0284 +#define DEVICE_VER 0x0101 + +#ifdef LED_MATRIX_ENABLE +/* LED matrix driver configuration */ +# define DRIVER_COUNT 1 +# define DRIVER_ADDR_1 0b1110100 +# define DRIVER_LED_TOTAL 88 + +/* Set to infinit, which is use in USB mode by default */ +#define LED_DISABLE_TIMEOUT LED_DISABLE_TIME_INFINITE +/* Allow shutdown of led driver to save power */ +# define LED_MATRIX_DRIVER_SHUTDOWN_ENABLE +/* Turn off backlight on low brightness to save power */ +# define LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL 32 + +/* LED Matrix Animation modes. Explicitly enabled + * For full list of effects, see: + * https://docs.qmk.fm/#/feature_led_matrix?id=led-matrix-effects + */ +# define LED_MATRIX_KEYPRESSES + +# define ENABLE_LED_MATRIX_NONE +# define ENABLE_LED_MATRIX_SOLID +# define ENABLE_LED_MATRIX_BREATHING +# define ENABLE_LED_MATRIX_BAND_PINWHEEL +# define ENABLE_LED_MATRIX_BAND_SPIRAL +# define ENABLE_LED_MATRIX_CYCLE_LEFT_RIGHT +# define ENABLE_LED_MATRIX_CYCLE_UP_DOWN +# define ENABLE_LED_MATRIX_CYCLE_OUT_IN +# define ENABLE_LED_MATRIX_DUAL_BEACON +# define ENABLE_LED_MATRIX_SOLID_REACTIVE_SIMPLE +# define ENABLE_LED_MATRIX_SOLID_REACTIVE_MULTIWIDE +# define ENABLE_LED_MATRIX_SOLID_REACTIVE_MULTINEXUS +# define ENABLE_LED_MATRIX_SOLID_SPLASH +# define ENABLE_LED_MATRIX_WAVE_LEFT_RIGHT +# define ENABLE_LED_MATRIX_WAVE_UP_DOWN +# define ENABLE_LED_MATRIX_EFFECT_MAX + +/* Use first 6 channels of LED driver */ +#define PHASE_CHANNEL MSKPHASE_6CHANNEL + +/* Set LED driver current */ +#define CKLED2001_CURRENT_TUNE { 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60 } + +#endif diff --git a/keyboards/keychron/k8_pro/iso/white/keymaps/default/keymap.c b/keyboards/keychron/k8_pro/iso/white/keymaps/default/keymap.c new file mode 100644 index 0000000000..b219c5a747 --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/white/keymaps/default/keymap.c @@ -0,0 +1,59 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 QMK_KEYBOARD_H + +enum layers{ + MAC_BASE, + MAC_FN, + WIN_BASE, + WIN_FN +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +[MAC_BASE] = LAYOUT_iso_88( + KC_ESC, KC_BRID, KC_BRIU, KC_MCTL, KC_LNPD, BL_DEC, BL_INC, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_SNAP, KC_SIRI, BL_STEP, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LOPTN, KC_LCMMD, KC_SPC, KC_RCMMD, KC_ROPTN, MO(MAC_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[MAC_FN] = LAYOUT_iso_88( + KC_TRNS, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_TRNS, KC_TRNS, BL_TOGG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + BL_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), + +[WIN_BASE] = LAYOUT_iso_88( + KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_CTANA, BL_STEP, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RGUI, MO(WIN_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[WIN_FN] = LAYOUT_iso_88( + KC_TRNS, KC_BRID, KC_BRIU, KC_TASK, KC_FILE, BL_DEC, BL_INC, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_TRNS, KC_TRNS, BL_TOGG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + BL_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS) + +}; diff --git a/keyboards/keychron/k8_pro/iso/white/keymaps/via/keymap.c b/keyboards/keychron/k8_pro/iso/white/keymaps/via/keymap.c new file mode 100644 index 0000000000..69de3f7d9f --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/white/keymaps/via/keymap.c @@ -0,0 +1,59 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 QMK_KEYBOARD_H + +enum layers{ + MAC_BASE, + MAC_FN, + WIN_BASE, + WIN_FN +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { +[MAC_BASE] = LAYOUT_iso_88( + KC_ESC, KC_BRID, KC_BRIU, KC_MCTL, KC_LNPD, BL_DEC, BL_INC, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_SNAP, KC_SIRI, BL_STEP, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LOPTN, KC_LCMMD, KC_SPC, KC_RCMMD, KC_ROPTN, MO(MAC_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[MAC_FN] = LAYOUT_iso_88( + KC_TRNS, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_TRNS, KC_TRNS, BL_TOGG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + BL_TOGG, BL_STEP, BL_INC, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, BL_DEC, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), + +[WIN_BASE] = LAYOUT_iso_88( + KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_CTANA, BL_STEP, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, + KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RGUI, MO(WIN_FN),KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT), + +[WIN_FN] = LAYOUT_iso_88( + KC_TRNS, KC_BRID, KC_BRIU, KC_TASK, KC_FILE, BL_DEC, BL_INC, KC_MPRV, KC_MPLY, KC_MNXT, KC_MUTE, KC_VOLD, KC_VOLU, KC_TRNS, KC_TRNS, BL_TOGG, + KC_TRNS, BT_HST1, BT_HST2, BT_HST3, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + BL_TOGG, BL_STEP, BL_INC, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, BL_DEC, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, BAT_LVL, NK_TOGG, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, + KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS) + +}; diff --git a/keyboards/keychron/k8_pro/iso/white/keymaps/via/rules.mk b/keyboards/keychron/k8_pro/iso/white/keymaps/via/rules.mk new file mode 100644 index 0000000000..1e5b99807c --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/white/keymaps/via/rules.mk @@ -0,0 +1 @@ +VIA_ENABLE = yes diff --git a/keyboards/keychron/k8_pro/iso/white/rules.mk b/keyboards/keychron/k8_pro/iso/white/rules.mk new file mode 100644 index 0000000000..6b0c2a0769 --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/white/rules.mk @@ -0,0 +1,2 @@ +LED_MATRIX_ENABLE = yes +LED_MATRIX_DRIVER = CKLED2001 \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/iso/white/white.c b/keyboards/keychron/k8_pro/iso/white/white.c new file mode 100644 index 0000000000..33736f8780 --- /dev/null +++ b/keyboards/keychron/k8_pro/iso/white/white.c @@ -0,0 +1,148 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 "quantum.h" + +#ifdef LED_MATRIX_ENABLE +const ckled2001_led g_ckled2001_leds[DRIVER_LED_TOTAL] = { +/* Refer to IS31 manual for these locations + * driver + * | LED address + * | | */ + {0, A_16}, + {0, A_15}, + {0, A_14}, + {0, A_13}, + {0, A_12}, + {0, A_11}, + {0, A_10}, + {0, A_9}, + {0, A_8}, + {0, A_7}, + {0, A_6}, + {0, A_5}, + {0, A_4}, + {0, A_2}, + {0, A_1}, + {0, D_2}, + + {0, B_16}, + {0, B_15}, + {0, B_14}, + {0, B_13}, + {0, B_12}, + {0, B_11}, + {0, B_10}, + {0, B_9}, + {0, B_8}, + {0, B_7}, + {0, B_6}, + {0, B_5}, + {0, B_4}, + {0, B_3}, + {0, B_2}, + {0, B_1}, + {0, E_2}, + + {0, C_16}, + {0, C_15}, + {0, C_14}, + {0, C_13}, + {0, C_12}, + {0, C_11}, + {0, C_10}, + {0, C_9}, + {0, C_8}, + {0, C_7}, + {0, C_6}, + {0, C_5}, + {0, C_4}, + {0, C_3}, + {0, C_2}, + {0, C_1}, + {0, D_1}, + + {0, D_16}, + {0, D_15}, + {0, D_14}, + {0, D_13}, + {0, D_12}, + {0, D_11}, + {0, D_10}, + {0, D_9}, + {0, D_8}, + {0, D_7}, + {0, D_6}, + {0, D_5}, + {0, D_3}, + + {0, E_16}, + {0, E_15}, + {0, E_14}, + {0, E_13}, + {0, E_12}, + {0, E_11}, + {0, E_10}, + {0, E_9}, + {0, E_8}, + {0, E_7}, + {0, E_6}, + {0, E_5}, + {0, E_3}, + {0, E_1}, + + {0, F_16}, + {0, F_15}, + {0, F_14}, + + {0, F_10}, + {0, F_6}, + {0, F_5}, + {0, F_4}, + {0, F_3}, + {0, F_2}, + {0, F_1}, + {0, D_4}, +}; + +led_config_t g_led_config = { + { + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, NO_LED, 13, 14, 15 }, + { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 }, + { 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 }, + { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, NO_LED, 62, NO_LED, NO_LED, NO_LED }, + { 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, NO_LED, 75, NO_LED, 76, NO_LED }, + { 77, 78, 79, NO_LED, NO_LED, NO_LED, 80, NO_LED, NO_LED, NO_LED, 81, 82, 83, 84, 85, 86, 87} + }, + { + {0, 0}, {25, 0}, {38, 0}, {51, 0}, {64, 0}, {84, 0}, { 97, 0}, {110, 0}, {123, 0}, {142, 0}, {155, 0}, {168, 0}, {181, 0}, {198, 0}, {211, 0}, {224, 0}, + {0,14}, {12,14}, {25,14}, {38,14}, {51,14}, {64,14}, {77,14}, { 90,14}, {103,14}, {116,14}, {129,14}, {142,14}, {155,14}, {175,14}, {198,14}, {211,14}, {224,14}, + {3,26}, {19,26}, {32,26}, {45,26}, {58,26}, {71,26}, {84,26}, { 97,26}, {110,26}, {123,26}, {136,26}, {149,26}, {162,26}, {178,26}, {198,26}, {211,26}, {224,26}, + {4,39}, {22,39}, {35,39}, {48,39}, {61,39}, {74,39}, {87,39}, {100,39}, {113,39}, {126,39}, {139,39}, {152,39}, {173,39}, + {0,51}, {16,51}, {29,51}, {42,51}, {55,51}, {68,51}, {81,51}, {94,51}, {107,51}, {120,51}, {132,51}, {145,51}, {170,51}, {211,51}, + {1,64}, {17,64}, {34,64}, {82,64}, {131,64}, {147,64}, {163,64}, {180,64}, {198,64}, {211,64}, {224,64} + }, + { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 + } +}; + +#endif \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/k8_pro.c b/keyboards/keychron/k8_pro/k8_pro.c new file mode 100644 index 0000000000..c466967e32 --- /dev/null +++ b/keyboards/keychron/k8_pro/k8_pro.c @@ -0,0 +1,297 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 "k8_pro.h" +#ifdef BLUETOOTH_ENABLE +# include "ckbt51.h" +# include "bluetooth.h" +# include "indicator.h" +# include "transport.h" +# include "battery.h" +# include "bat_level_animation.h" +# include "lpm.h" +#endif + +#ifdef ENABLE_FACTORY_TEST +# include "factory_test.h" +#endif + +#define POWER_ON_LED_DURATION 3000 + +typedef struct PACKED { + uint8_t len; + uint8_t keycode[3]; +} key_combination_t; + +static uint32_t power_on_indicator_timer_buffer; +static uint32_t siri_timer_buffer = 0; +static uint8_t mac_keycode[4] = {KC_LOPT, KC_ROPT, KC_LCMD, KC_RCMD}; + +key_combination_t key_comb_list[4] = { + {2, {KC_LWIN, KC_TAB}}, // Task (win) + {2, {KC_LWIN, KC_E}}, // Files (win) + {3, {KC_LSFT, KC_LGUI, KC_4}}, // Snapshot (mac) + {2, {KC_LWIN, KC_C}} // Cortana (win) +}; + +#ifdef BLUETOOTH_ENABLE +bool firstDisconnect = true; +bool bt_factory_reset = false; +static virtual_timer_t pairing_key_timer; +extern uint8_t g_pwm_buffer[DRIVER_COUNT][192]; + +static void pairing_key_timer_cb(void *arg) { + bluetooth_pairing_ex(*(uint8_t *)arg, NULL); +} +#endif + +bool dip_switch_update_kb(uint8_t index, bool active) { + if (index == 0) { + default_layer_set(1UL << (active ? 2 : 0)); + } + dip_switch_update_user(index, active); + + return true; +} + +bool process_record_user(uint16_t keycode, keyrecord_t *record) { + static uint8_t host_idx = 0; + + switch (keycode) { + case KC_LOPTN: + case KC_ROPTN: + case KC_LCMMD: + case KC_RCMMD: + if (record->event.pressed) { + register_code(mac_keycode[keycode - KC_LOPTN]); + } else { + unregister_code(mac_keycode[keycode - KC_LOPTN]); + } + return false; // Skip all further processing of this key) + case KC_MCTL: + /* Mission Control */ + host_consumer_send(record->event.pressed ? 0x29F : 0); + return false; // Skip all further processing of this key + case KC_LNPD: + /* Lanuchpad */ + host_consumer_send(record->event.pressed ? 0x2A0 : 0); + return false; // Skip all further processing of this key + case KC_TASK: + case KC_FILE: + case KC_SNAP: + case KC_CTANA: + if (record->event.pressed) { + for (uint8_t i = 0; i < key_comb_list[keycode - KC_TASK].len; i++) + register_code(key_comb_list[keycode - KC_TASK].keycode[i]); + } else { + for (uint8_t i = 0; i < key_comb_list[keycode - KC_TASK].len; i++) + unregister_code(key_comb_list[keycode - KC_TASK].keycode[i]); + } + return false; // Skip all further processing of this key + case KC_SIRI: + if (record->event.pressed && siri_timer_buffer == 0) { + register_code(KC_LGUI); + register_code(KC_SPACE); + siri_timer_buffer = sync_timer_read32() | 1; + } + return false; // Skip all further processing of this key +#ifdef BLUETOOTH_ENABLE + case BT_HST1 ... BT_HST3: + if (record->event.pressed) { + host_idx = keycode + BT_HST1 + 1; + chVTSet(&pairing_key_timer, TIME_MS2I(2000), (vtfunc_t)pairing_key_timer_cb, &host_idx); + bluetooth_connect_ex(host_idx, 0); + } else { + host_idx = 0; + chVTReset(&pairing_key_timer); + } + break; + case BAT_LVL: + if (get_transport() == TRANSPORT_BLUETOOTH && !usb_power_connected()) { + bat_level_animiation_start(battery_get_percentage()); + } + break; +#endif + default: +#ifdef FACTORY_RESET_CHECK + FACTORY_RESET_CHECK(keycode, record); +#endif + break; + } + return true; +} + +void keyboard_post_init_user(void) { + dip_switch_read(true); + +#ifdef BLUETOOTH_ENABLE + /* Currently we don't use this reset pin */ + palSetLineMode(CKBT51_RESET_PIN, PAL_MODE_UNCONNECTED); + + /* IMPORTANT: DO NOT enable internal pull-up resistor + * as there is an external pull-down resistor. + */ + palSetLineMode(USB_BT_MODE_SELECT_PIN, PAL_MODE_INPUT); + + ckbt51_init(false); + bluetooth_init(); +# endif + + power_on_indicator_timer_buffer = sync_timer_read32() | 1; + writePin(BAT_LOW_LED_PIN, BAT_LOW_LED_PIN_ON_STATE); + writePin(LED_CAPS_LOCK_PIN, LED_PIN_ON_STATE); +#ifdef BLUETOOTH_ENABLE + writePin(H3, HOST_LED_PIN_ON_STATE); +# endif +} + +void matrix_scan_user(void) { + if (power_on_indicator_timer_buffer) { + if (sync_timer_elapsed32(power_on_indicator_timer_buffer) > POWER_ON_LED_DURATION) { + power_on_indicator_timer_buffer = 0; + + writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE); + writePin(H3, !HOST_LED_PIN_ON_STATE); + if (!host_keyboard_led_state().caps_lock) writePin(LED_CAPS_LOCK_PIN, !LED_PIN_ON_STATE); + } else { + writePin(BAT_LOW_LED_PIN, BAT_LOW_LED_PIN_ON_STATE); + writePin(H3, HOST_LED_PIN_ON_STATE); + writePin(LED_CAPS_LOCK_PIN, LED_PIN_ON_STATE); + } + } + + if (siri_timer_buffer && sync_timer_elapsed32(siri_timer_buffer) > 500) { + siri_timer_buffer = 0; + unregister_code(KC_LGUI); + unregister_code(KC_SPACE); + } + +#ifdef FACTORY_RESET_TASK + FACTORY_RESET_TASK(); +# endif +} + +#ifdef BLUETOOTH_ENABLE +static void ckbt51_param_init(void) { + /* Set bluetooth device name */ + ckbt51_set_local_name(STR(PRODUCT)); + /* Set bluetooth parameters */ + module_param_t param = {.event_mode = 0x02, + .connected_idle_timeout = 7200, + .pairing_timeout = 180, + .pairing_mode = 0, + .reconnect_timeout = 5, + .report_rate = 90, + .vendor_id_source = 1, + .verndor_id = 0, // Must be 0x3434 + .product_id = PRODUCT_ID}; + ckbt51_set_param(¶m); +} + +void bluetooth_enter_disconnected_kb(uint8_t host_idx) { + if (bt_factory_reset) { + bt_factory_reset = false; + ckbt51_param_init(); + } + /* CKBT51 bluetooth module boot time is slower, it enters disconnected after boot, + so we place initialization here. */ + if (firstDisconnect && sync_timer_read32() < 1000 && get_transport() == TRANSPORT_BLUETOOTH) { + ckbt51_param_init(); + bluetooth_connect(); + firstDisconnect = false; + } +} + +void bluetooth_pre_task(void) { + static uint8_t mode = 1; + + if (readPin(USB_BT_MODE_SELECT_PIN) != mode) { + if (readPin(USB_BT_MODE_SELECT_PIN) != mode) { + mode = readPin(USB_BT_MODE_SELECT_PIN); + set_transport(mode == 0 ? TRANSPORT_BLUETOOTH : TRANSPORT_USB); + } + } +} + +void battery_measure(void) { +# ifdef LED_MATRIX_ENABLE + if (led_matrix_is_enabled()) { + ckbt51_read_state_reg(0x05, 0x02); + return; + } +# endif +# ifdef RGB_MATRIX_ENABLE + if (rgb_matrix_is_enabled()) { + ckbt51_read_state_reg(0x05, 0x02); + return; + } +# endif +} +#endif + +void battery_calculte_voltage(uint16_t value) { + uint16_t voltage = ((uint32_t)value) * 2246 / 1000; + +#ifdef LED_MATRIX_ENABLE + if (led_matrix_is_enabled()) { + uint32_t totalBuf = 0; + + for (uint8_t i = 0; i < DRIVER_COUNT; i++) + for (uint8_t j = 0; j < 192; j++) + totalBuf += g_pwm_buffer[i][j]; + /* We assumpt it is linear relationship*/ + voltage += (30 * totalBuf / DRIVER_LED_TOTAL / 255); + } +#endif +#ifdef RGB_MATRIX_ENABLE + if (rgb_matrix_is_enabled()) { + uint32_t totalBuf = 0; + + for (uint8_t i = 0; i < DRIVER_COUNT; i++) + for (uint8_t j = 0; j < 192; j++) + totalBuf += g_pwm_buffer[i][j]; + /* We assumpt it is linear relationship*/ + uint32_t compensation = 60 * totalBuf / DRIVER_LED_TOTAL / 255 / 3; + voltage += compensation; + } +#endif + battery_set_voltage(voltage); +} + +void raw_hid_receive_kb(uint8_t *data, uint8_t length) { + switch (data[0]) { +#ifdef BLUETOOTH_ENABLE + case 0xAA: + ckbt51_dfu_rx(data, length); + break; +#endif +#ifdef ENABLE_FACTORY_TEST + case 0xAB: + factory_test_rx(data, length); + break; +#endif + } +} + +#if !defined(VIA_ENABLE) +void raw_hid_receive(uint8_t *data, uint8_t length) { + switch (data[0]) { + case RAW_HID_CMD: + raw_hid_receive_kb(data, length); + break; + } +} +#endif diff --git a/keyboards/keychron/k8_pro/k8_pro.h b/keyboards/keychron/k8_pro/k8_pro.h new file mode 100644 index 0000000000..310ea6ab70 --- /dev/null +++ b/keyboards/keychron/k8_pro/k8_pro.h @@ -0,0 +1,99 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 . + */ + +#pragma once + +#include "quantum.h" +#ifdef VIA_ENABLE +# include "via.h" +#endif + +#define ___ KC_NO + +#ifdef VIA_ENABLE +# define USER_START USER00 +#else +# define USER_START SAFE_RANGE +#endif + +enum { + KC_LOPTN = USER_START, + KC_ROPTN, + KC_LCMMD, + KC_RCMMD, + KC_MCTL, + KC_LNPD, + KC_TASK, + KC_FILE, + KC_SNAP, + KC_CTANA, + KC_SIRI, +#ifdef BLUETOOTH_ENABLE + BT_HST1, + BT_HST2, + BT_HST3, +#endif + BAT_LVL, +}; + + +#define LAYOUT_ansi_87( \ + K00, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, K0B, K0C, K0E, K0F, K0G, \ + K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, K1C, K1D, K1E, K1F, K1G, \ + K20, K21, K22, K23, K24, K25, K26, K27, K28, K29, K2A, K2B, K2C, K2D, K2E, K2F, K2G, \ + K30, K31, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, K3D, \ + K40, K42, K43, K44, K45, K46, K47, K48, K49, K4A, K4B, K4D, K4F, \ + K50, K51, K52, K56, K5A, K5B, K5C, K5D, K5E, K5F, K5G \ +) { \ + { K00, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, K0B, K0C, ___, K0E, K0F, K0G }, \ + { K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, K1C, K1D, K1E, K1F, K1G }, \ + { K20, K21, K22, K23, K24, K25, K26, K27, K28, K29, K2A, K2B, K2C, K2D, K2E, K2F, K2G }, \ + { K30, K31, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, ___, K3D, ___, ___, ___ }, \ + { K40, ___, K42, K43, K44, K45, K46, K47, K48, K49, K4A, K4B, ___, K4D, ___, K4F, ___ }, \ + { K50, K51, K52, ___, ___, ___, K56, ___, ___, ___, K5A, K5B, K5C, K5D, K5E, K5F, K5G } \ +} + +#define LAYOUT_iso_88( \ + K00, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, K0B, K0C, K0E, K0F, K0G, \ + K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, K1C, K1D, K1E, K1F, K1G, \ + K20, K21, K22, K23, K24, K25, K26, K27, K28, K29, K2A, K2B, K2C, K2D, K2E, K2F, K2G, \ + K30, K31, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, K3D, \ + K40, K41, K42, K43, K44, K45, K46, K47, K48, K49, K4A, K4B, K4D, K4F, \ + K50, K51, K52, K56, K5A, K5B, K5C, K5D, K5E, K5F, K5G \ +) { \ + { K00, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, K0B, K0C, ___, K0E, K0F, K0G }, \ + { K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, K1C, K1D, K1E, K1F, K1G }, \ + { K20, K21, K22, K23, K24, K25, K26, K27, K28, K29, K2A, K2B, K2C, K3D, K2E, K2F, K2G }, \ + { K30, K31, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, ___, K2D, ___, ___, ___ }, \ + { K40, K41, K42, K43, K44, K45, K46, K47, K48, K49, K4A, K4B, ___, K4D, ___, K4F, ___ }, \ + { K50, K51, K52, ___, ___, ___, K56, ___, ___, ___, K5A, K5B, K5C, K5D, K5E, K5F, K5G } \ +} + +#define LAYOUT_jis_91( \ + K00, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, K0B, K0C, K0E, K0F, K0G, \ + K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, K1C, K1D, K0D, K1E, K1F, K1G, \ + K20, K21, K22, K23, K24, K25, K26, K27, K28, K29, K2A, K2B, K2C, K2D, K2E, K2F, K2G, \ + K30, K31, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, K3D, \ + K40, K42, K43, K44, K45, K46, K47, K48, K49, K4A, K4B, K4C, K4D, K4F, \ + K50, K51, K52, K53, K56, K59, K5A, K5B, K5C, K5D, K5E, K5F, K5G \ +) { \ + { K00, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0A, K0B, K0C, K0D, K0E, K0F, K0G }, \ + { K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, K1C, K1D, K1E, K1F, K1G }, \ + { K20, K21, K22, K23, K24, K25, K26, K27, K28, K29, K2A, K2B, K2C, K3D, K2E, K2F, K2G }, \ + { K30, K31, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, ___, K2D, ___, ___, ___ }, \ + { K40, ___, K42, K43, K44, K45, K46, K47, K48, K49, K4A, K4B, K4C, K4D, ___, K4F, ___ }, \ + { K50, K51, K52, K53, ___, ___, K56, ___, ___, K59, K5A, K5B, K5C, K5D, K5E, K5F, K5G } \ +} diff --git a/keyboards/keychron/k8_pro/matrix.c b/keyboards/keychron/k8_pro/matrix.c new file mode 100644 index 0000000000..6e322ec795 --- /dev/null +++ b/keyboards/keychron/k8_pro/matrix.c @@ -0,0 +1,173 @@ +/* Copyright 2021 @ Keychron (https://www.keychron.com) + * + * 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 "q1_bluetooth.h" +#include "stdint.h" +#include "hal.h" +#include "gpio.h" +#include "quantum.h" + +#define HC595_STCP A0 +#define HC595_SHCP A1 +#define HC595_DS C15 + +pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; +pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; + +static inline void HC595_delay(uint16_t n) { + while (n-- > 0) { + asm volatile("nop" ::: "memory"); + }; +} + +static void HC595_output(uint16_t data) { + uint8_t i; + uint8_t n = 1; + + for (i = 16; i > 0; i--) { + writePinLow(HC595_SHCP); + + if (data & 0x8000) + writePinHigh(HC595_DS); + else + writePinLow(HC595_DS); + + data <<= 1; + + HC595_delay(n); + + writePinHigh(HC595_SHCP); + HC595_delay(n); + } + + HC595_delay(n); + writePinLow(HC595_STCP); + HC595_delay(n); + writePinHigh(HC595_STCP); +} + +static inline void setPinOutput_writeLow(pin_t pin) { + ATOMIC_BLOCK_FORCEON { + setPinOutput(pin); + writePinLow(pin); + } +} + +static inline void setPinInputHigh_atomic(pin_t pin) { + ATOMIC_BLOCK_FORCEON { + setPinInputHigh(pin); + } +} + +static inline uint8_t readMatrixPin(pin_t pin) { + if (pin != NO_PIN) { + return readPin(pin); + } else { + return 1; + } +} + +static bool select_col(uint8_t col) { + pin_t pin = col_pins[col]; + + if (col < 1) { + if (pin != NO_PIN) { + setPinOutput_writeLow(pin); + return true; + } + } else { + HC595_output(~(0x01 << (col - 1))); + return true; + } + return false; +} + +static void unselect_col(uint8_t col) { + pin_t pin = col_pins[col]; + + if (col < 1) { + if (pin != NO_PIN) { + setPinInputHigh_atomic(pin); + } + } else { + // HC595_output(0x01 << (col - 1)); + if (col >= MATRIX_COLS - 1) HC595_output(0xFFFF); + } +} + +static void unselect_cols(void) { + if (col_pins[0] != NO_PIN) setPinInputHigh_atomic(col_pins[0]); + HC595_output(0xFFFF); +} + +void select_all_cols(void) { + if (col_pins[0] != NO_PIN) setPinOutput_writeLow(col_pins[0]); + HC595_output(0x0000); +} + +void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { + + // Select col + if (!select_col(current_col)) { // select col + return; // skip NO_PIN col + } + + HC595_delay(100); + + // For each row... + for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { + // Check row pin state + if (readMatrixPin(row_pins[row_index]) == 0) { + // Pin LO, set col bit + current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); + // key_pressed = true; + } else { + // Pin HI, clear col bit + current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); + } + } + + unselect_col(current_col); + HC595_delay(100); +} + +void matrix_init_custom(void) { + for (uint8_t x = 0; x < MATRIX_ROWS; x++) { + if (row_pins[x] != NO_PIN) { + setPinInputHigh_atomic(row_pins[x]); + } + } + setPinOutput(HC595_DS); + setPinOutput(HC595_STCP); + setPinOutput(HC595_SHCP); + unselect_cols(); +} + +bool matrix_scan_custom(matrix_row_t current_matrix[]) { + bool matrix_has_changed = false; + + matrix_row_t curr_matrix[MATRIX_ROWS] = {0}; + + // Set col, read rows + for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { + matrix_read_rows_on_col(curr_matrix, current_col); + } + + matrix_has_changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0; + if (matrix_has_changed) memcpy(current_matrix, curr_matrix, sizeof(curr_matrix)); + + return matrix_has_changed; +} diff --git a/keyboards/keychron/k8_pro/mcuconf.h b/keyboards/keychron/k8_pro/mcuconf.h new file mode 100644 index 0000000000..7cb0bde3c5 --- /dev/null +++ b/keyboards/keychron/k8_pro/mcuconf.h @@ -0,0 +1,37 @@ +/* Copyright 2020 QMK + * + * 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 . + */ + + +#pragma once + +#include_next + +/* Set HCLK to 48 MHz as tradeoff of USB lowest clockand and + * lower power comsumption for bluetooth. Will use dynamic + * clock when STM32L4 is supported in ChibiOS */ +#undef STM32_PLLM_VALUE +#define STM32_PLLM_VALUE 2 + +#undef STM32_PLLN_VALUE +#define STM32_PLLN_VALUE 12 + +#undef STM32_I2C_USE_I2C1 +#define STM32_I2C_USE_I2C1 TRUE + +#ifdef BLUETOOTH_ENABLE +# undef STM32_SERIAL_USE_USART2 +# define STM32_SERIAL_USE_USART2 TRUE +#endif \ No newline at end of file diff --git a/keyboards/keychron/k8_pro/readme.md b/keyboards/keychron/k8_pro/readme.md new file mode 100644 index 0000000000..e44c406920 --- /dev/null +++ b/keyboards/keychron/k8_pro/readme.md @@ -0,0 +1,21 @@ +# Keychron K8 Pro + +![Keychron K8 Pro](https://cdn.shopify.com/s/files/1/0059/0630/1017/t/5/assets/keychronk8proqmkviawirelessmechanicalkeyboardformacwindowsosaprofilepbtkeycapspcbscrewinstabilizerwithhotswappablegaterongpromechanicalswitchcompatiblewithmxcherrypandakailhwithrgbbacklightaluminumframe-1645094681965.jpg) + +A customizable 87 keys TKL keyboard. + +* Keyboard Maintainer: [Keychron](https://github.com/keychron) +* Hardware Supported: Keychron K8 Pro +* Hardware Availability: [Keychron K8 Pro QMK/VIA Wireless Mechanical Keyboard](https://www.keychron.com/products/keychron-k8-pro-qmk-via-wireless-mechanical-keyboard) + +Make example for this keyboard (after setting up your build environment): + + make keychron/k8_pro/ansi/rgb:default + +Flashing example for this keyboard: + + make keychron/k8_pro/ansi/rgb:default:flash + +**Reset Key**: Connect the USB cable, toggle mode switch to "Off", hold down the *Esc* key or reset button underneath space bar, then toggle then switch to "Cable". + +See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) and the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. Brand new to QMK? Start with our [Complete Newbs Guide](https://docs.qmk.fm/#/newbs). diff --git a/keyboards/keychron/k8_pro/rules.mk b/keyboards/keychron/k8_pro/rules.mk new file mode 100644 index 0000000000..f1d68b2878 --- /dev/null +++ b/keyboards/keychron/k8_pro/rules.mk @@ -0,0 +1,36 @@ +# MCU name +MCU = STM32L432 + +# Bootloader selection +BOOTLOADER = stm32-dfu + +# Build Options +# change yes to no to disable +# +BOOTMAGIC_ENABLE = yes # Enable boot magic +MOUSEKEY_ENABLE = yes # Mouse keys +EXTRAKEY_ENABLE = yes # Audio control and System control +CONSOLE_ENABLE = yes # Console for debug +COMMAND_ENABLE = no # Commands for debug and configuration +# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE +SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend +# if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work +NKRO_ENABLE = yes # USB Nkey Rollover +BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality +RGBLIGHT_ENABLE = no # Enable keyboard RGB underglow +BLUETOOTH_ENABLE = yes # Enable Bluetooth with +BLUETOOTH_DRIVER = custom +AUDIO_ENABLE = no # Audio output on port C6 +DIP_SWITCH_ENABLE = yes +RAW_ENABLE = yes + +# Enter lower-power sleep mode when on the ChibiOS idle thread +OPT_DEFS += -DCORTEX_ENABLE_WFI_IDLE=TRUE +OPT_DEFS += -DNO_USB_STARTUP_CHECK -DENABLE_FACTORY_TEST + +CUSTOM_MATRIX = lite +SRC += matrix.c + +include keyboards/keychron/bluetooth/bluetooth.mk + + diff --git a/platforms/chibios/eeprom_stm32_defs.h b/platforms/chibios/eeprom_stm32_defs.h index 57d0440330..c944e5a30f 100644 --- a/platforms/chibios/eeprom_stm32_defs.h +++ b/platforms/chibios/eeprom_stm32_defs.h @@ -17,6 +17,9 @@ #include +#define __PRINT_MACRO(x) #x +#define PRINT_MARCO(x) #x"=" __PRINT_MACRO(x) + #if !defined(FEE_PAGE_SIZE) || !defined(FEE_PAGE_COUNT) # if defined(STM32F103xB) || defined(STM32F042x6) || defined(GD32VF103C8) || defined(GD32VF103CB) # ifndef FEE_PAGE_SIZE @@ -25,7 +28,7 @@ # ifndef FEE_PAGE_COUNT # define FEE_PAGE_COUNT 2 // How many pages are used # endif -# elif defined(STM32F103xE) || defined(STM32F303xC) || defined(STM32F303xE) || defined(STM32F072xB) || defined(STM32F070xB) +# elif defined(STM32F103xE) || defined(STM32F303xC) || defined(STM32F303xE) || defined(STM32F072xB) || defined(STM32F070xB) || defined(STM32L432xx) # ifndef FEE_PAGE_SIZE # define FEE_PAGE_SIZE 0x800 // Page size = 2KByte # endif @@ -47,7 +50,7 @@ # define FEE_MCU_FLASH_SIZE 32 // Size in Kb # elif defined(GD32VF103C8) # define FEE_MCU_FLASH_SIZE 64 // Size in Kb -# elif defined(STM32F103xB) || defined(STM32F072xB) || defined(STM32F070xB) || defined(GD32VF103CB) +# elif defined(STM32F103xB) || defined(STM32F072xB) || defined(STM32F070xB) || defined(GD32VF103CB) || defined(STM32L432xx) # define FEE_MCU_FLASH_SIZE 128 // Size in Kb # elif defined(STM32F303xC) || defined(STM32F401xC) # define FEE_MCU_FLASH_SIZE 256 // Size in Kb diff --git a/platforms/chibios/eeprom_stm32_l4.c b/platforms/chibios/eeprom_stm32_l4.c new file mode 100644 index 0000000000..33a6d284c8 --- /dev/null +++ b/platforms/chibios/eeprom_stm32_l4.c @@ -0,0 +1,598 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by + * Artur F. + * + * Modifications for QMK and STM32L432 by lalalademaxiya1 & lokher + * + * TODO: Add ECC correction interrupt handler. + */ + +#include +#include +#include +#include "flash_stm32.h" +#include "eeprom_stm32_defs.h" +#include "eeprom_stm32_l4.h" +#include "print.h" +/* + * We emulate eeprom by writing a snapshot compacted view of eeprom contents, + * followed by a write log of any change since that snapshot: + * + * === SIMULATED EEPROM CONTENTS === + * + * ┌─ Compacted─┬─ Write Log ──┐ + * │............│[DWord][DWord]│ + * │FFFF....FFFF│[DWord][DWord]│ + * │FFFFFFFFFFFF│[DWord][DWord]│ + * │....FFFFFFFF│[DWord][DWord]│ + * ├────────────┼──────────────┤ + * └──PAGE_BASE │ │ + * PAGE_LAST─┴─WRITE_BASE │ + * WRITE_LAST ──┘ + * + * Compacted contents are the 1's complement of the actual EEPROM contents. + * e.g. An 'FFFF' represents a '0000' value. + * + * The size of the 'compacted' area is equal to the size of the 'emulated' eeprom. + * The size of the compacted-area and write log are configurable, and the combined + * size of Compacted + WriteLog is a multiple FEE_PAGE_SIZE, which is MCU dependent. + * Simulated Eeprom contents are located at the end of available flash space. + * + * The following configuration defines can be set: + * + * FEE_PAGE_COUNT # Total number of pages to use for eeprom simulation (Compact + Write log) + * FEE_DENSITY_BYTES # Size of simulated eeprom. (Defaults to one pages of FEE_PAGE_COUNT) + * NOTE: The current implementation does not include page swapping, + * and FEE_DENSITY_BYTES will consume that amount of RAM as a cached view of actual EEPROM contents. + * + * The maximum size of FEE_DENSITY_BYTES is currently 8192. The write log size equals + * FEE_PAGE_COUNT * FEE_PAGE_SIZE - FEE_DENSITY_BYTES. + * The larger the write log, the less frequently the compacted area needs to be rewritten. + * + * + * *** General Algorithm *** + * + * During initialization: + * The contents of the Compacted-flash area are loaded and the 1's complement value + * is cached into memory (e.g. 0xFFFF in Flash represents 0x0000 in cache). + * Write log entries are processed until a 0xFFFF is reached. + * Each log entry updates 1/2/4 byte(s) in the cache. + * + * During reads: + * EEPROM contents are given back directly from the cache in memory. + * + * During writes: + * The contents of the cache is updated first. + * If the Compacted-flash area corresponding to the write address is unprogrammed, the 1's complement of the value is written directly into Compacted-flash + * Otherwise: + * If the write log is full, erase both the Compacted-flash area and the Write log, then write cached contents to the Compacted-flash area. + * Otherwise a Write log entry is constructed and appended to the next free position in the Write log. + * + * + * *** Write Log Structure *** + * + * Each log entry compose of double word (2 x 32-bit) due to the minimum program size of STM32L432 flash. + * + * === WRITE LOG ENTRY FORMATS === + * + * ╔══════════ Byte-Entry ═════════╗ + * ║ 00 01 XX XX ║ FF FF FF YY ║ + * ║ └─┬─┘ └─┬─┘ ║ └┘ ║ + * ║ Len Address ║ ~Value ║ + * ╚═══════════════╩═══════════════╝ + * + * ╔══════════ Word-Entry ═════════╗ + * ║ 00 02 XX XX ║ FF FF YY YY ║ + * ║ └─┬─┘ └─┬─┘ ║ └─┬─┘ ║ + * ║ Len Address ║ ~Value ║ + * ╚═══════════════╩═══════════════╝ + * + * ╔══════════ DWord-Entry ═══════╗ + * ║ 00 04 XX XX ║ FF FF FF FF ║ + * ║ └─┬─┘ └─┬─┘ ║ └───┬────┘ ║ + * ║ Len Address ║ ~Value ║ + * ╚═══════════════╩═══════════════╝ + * + */ + +#include "eeprom_stm32_defs.h" +#if !defined(FEE_PAGE_SIZE) || !defined(FEE_PAGE_COUNT) || !defined(FEE_MCU_FLASH_SIZE) || !defined(FEE_PAGE_BASE_ADDRESS) +# error "not implemented." +#endif + +/* These bits indicate that the length of data which was wrote to log space */ +#define FEE_BYTE_FLAG 0x00010000 +#define FEE_WORD_FLAG 0x00020000 +#define FEE_DWORD_FLAG 0x00040000 + +/* Flash byte value after erase */ +#define FEE_EMPTY_BYTE ((uint8_t)0xFF) +/* Flash double byte value after erase */ +#define FEE_EMPTY_DBYTE ((uint16_t)0xFFFF) +/* Flash word value after erase */ +#define FEE_EMPTY_WORD ((uint32_t)0xFFFFFFFF) +/* Flash double word value after erase */ +#define FEE_EMPTY_DWORD ((uint64_t)0xFFFFFFFFFFFFFFFF) + +/* Size of combined compacted eeprom and write log pages */ +#define FEE_DENSITY_MAX_SIZE (FEE_PAGE_COUNT * FEE_PAGE_SIZE) + +#ifndef FEE_MCU_FLASH_SIZE_IGNORE_CHECK /* *TODO: Get rid of this check */ +# if FEE_DENSITY_MAX_SIZE > (FEE_MCU_FLASH_SIZE * 1024) +# pragma message STR(FEE_DENSITY_MAX_SIZE) " > " STR(FEE_MCU_FLASH_SIZE * 1024) +# error emulated eeprom: FEE_DENSITY_MAX_SIZE is greater than available flash size +# endif +#endif + +/* Size of emulated eeprom */ +#ifdef FEE_DENSITY_BYTES +# if (FEE_DENSITY_BYTES > FEE_DENSITY_MAX_SIZE) +# pragma message STR(FEE_DENSITY_BYTES) " > " STR(FEE_DENSITY_MAX_SIZE) +# error emulated eeprom: FEE_DENSITY_BYTES exceeds FEE_DENSITY_MAX_SIZE +# endif +# if (FEE_DENSITY_BYTES == FEE_DENSITY_MAX_SIZE) +# pragma message STR(FEE_DENSITY_BYTES) " == " STR(FEE_DENSITY_MAX_SIZE) +# warning emulated eeprom: FEE_DENSITY_BYTES leaves no room for a write log. This will greatly increase the flash wear rate! +# endif +# if FEE_DENSITY_BYTES > FEE_ADDRESS_MAX_SIZE +# pragma message STR(FEE_DENSITY_BYTES) " > " STR(FEE_ADDRESS_MAX_SIZE) +# error emulated eeprom: FEE_DENSITY_BYTES is greater than FEE_ADDRESS_MAX_SIZE allows +# endif +# if ((FEE_DENSITY_BYTES) % 8) != 0 +# error emulated eeprom: FEE_DENSITY_BYTES must be a multiple of 8 +# endif +#else +/* Default to one page of allocated space used for emulated eeprom, 3 pages for write log */ +# define FEE_DENSITY_BYTES FEE_PAGE_SIZE +#endif + +/* Size of write log */ +#ifdef FEE_WRITE_LOG_BYTES +# if ((FEE_DENSITY_BYTES + FEE_WRITE_LOG_BYTES) > FEE_DENSITY_MAX_SIZE) +# pragma message STR(FEE_DENSITY_BYTES) " + " STR(FEE_WRITE_LOG_BYTES) " > " STR(FEE_DENSITY_MAX_SIZE) +# error emulated eeprom: FEE_WRITE_LOG_BYTES exceeds remaining FEE_DENSITY_MAX_SIZE +# endif +# if ((FEE_WRITE_LOG_BYTES) % 8) != 0 +# error emulated eeprom: FEE_WRITE_LOG_BYTES must be a multiple of 8 +# endif +#else +/* Default to use all remaining space */ +# define FEE_WRITE_LOG_BYTES (FEE_PAGE_COUNT * FEE_PAGE_SIZE - FEE_DENSITY_BYTES) +#endif + +/* In-memory contents of emulated eeprom for faster access */ +/* *TODO: Implement page swapping */ +static uint64_t DWordBuf[FEE_DENSITY_BYTES / 8]; +static uint8_t *DataBuf = (uint8_t *)DWordBuf; + +/* Pointer to the first available slot within the write log */ +static uint32_t *empty_slot; +/* ECC error flag, set in NMI when 2 bits ECC error is detected */ +static bool eccd; + +/* Start of the emulated eeprom compacted flash area */ +#define FEE_COMPACTED_BASE_ADDRESS FEE_PAGE_BASE_ADDRESS +/* End of the emulated eeprom compacted flash area */ +#define FEE_COMPACTED_LAST_ADDRESS (FEE_COMPACTED_BASE_ADDRESS + FEE_DENSITY_BYTES) +/* Start of the emulated eeprom write log */ +#define FEE_WRITE_LOG_BASE_ADDRESS FEE_COMPACTED_LAST_ADDRESS +/* End of the emulated eeprom write log */ +#define FEE_WRITE_LOG_LAST_ADDRESS (FEE_WRITE_LOG_BASE_ADDRESS + FEE_WRITE_LOG_BYTES) + +uint16_t EEPROM_Init(void) { + eccd = false; + + /* Load emulated eeprom contents from compacted flash into memory */ + uint32_t *src = (uint32_t *)FEE_COMPACTED_BASE_ADDRESS; + uint32_t *dest = (uint32_t *)DataBuf; + for (; src < (uint32_t *)FEE_COMPACTED_LAST_ADDRESS; ++src, ++dest) { + *dest = ~*src; + } + + /* Replay write log */ + uint32_t *log_addr; + for (log_addr = (uint32_t *)FEE_WRITE_LOG_BASE_ADDRESS; log_addr < (uint32_t *)FEE_WRITE_LOG_LAST_ADDRESS; log_addr += 2) { + uint32_t address = *log_addr; + + uint32_t data = ~*(log_addr + 1); + + /* Break loop if ECC error is detected */ + if (eccd) break; + + if (address == FEE_EMPTY_WORD) { + break; + } + /* Check if value is in bytes */ + else if ((address & FEE_BYTE_FLAG) == FEE_BYTE_FLAG) { + uint8_t value = (uint8_t)(data & 0xFF); + uint16_t addr = (uint16_t)address; + DataBuf[addr] = value; + } + /* Check if value is in words */ + else if ((address & FEE_WORD_FLAG) == FEE_WORD_FLAG) { + uint16_t value = (uint16_t)(data & 0xFFFF); + uint16_t addr = (uint16_t)address; + *(uint16_t *)(&DataBuf[addr]) = value; + } + /* Check if value is in double words */ + else if ((address & FEE_DWORD_FLAG) == FEE_DWORD_FLAG) { + uint32_t value = data; + uint16_t addr = (uint16_t)address; + *(uint32_t *)(&DataBuf[addr]) = value; + } + } + + empty_slot = log_addr; + + /* Give more chance for NMI interrupt */ + asm("nop"); + + /* Reset eeprom data if ECC error is detected*/ + if (eccd) return 0; + + return FEE_DENSITY_BYTES; +} + +/* Clear flash contents (doesn't touch in-memory DataBuf) */ +static void eeprom_clear(void) { + FLASH_Unlock(); + + for (uint16_t page_num = 0; page_num < FEE_PAGE_COUNT; ++page_num) { + FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE)); + } + + FLASH_Lock(); + + empty_slot = (uint32_t *)FEE_WRITE_LOG_BASE_ADDRESS; +} + +/* Erase emulated eeprom */ +void EEPROM_Erase(void) { + /* Erase compacted pages and write log */ + eeprom_clear(); + /* re-initialize to reset DataBuf */ + EEPROM_Init(); +} + +/* Compact write log */ +static uint8_t eeprom_compact(void) { + /* Erase compacted pages and write log */ + eeprom_clear(); + + FLASH_Unlock(); + + FLASH_Status final_status = FLASH_COMPLETE; + + /* Write emulated eeprom contents from memory to compacted flash */ + uint64_t *src = (uint64_t *)DataBuf; + uint32_t dest = FEE_COMPACTED_BASE_ADDRESS; + uint64_t value; + for (; dest < FEE_COMPACTED_LAST_ADDRESS; ++src, dest += 8) { + value = *src; + if (value) { + FLASH_Status status = FLASH_ProgramDoubleWord(dest, ~value); + if (status != FLASH_COMPLETE) final_status = status; + } + } + + FLASH_Lock(); + + return final_status; +} + +static uint8_t eeprom_write_direct_entry(uint16_t Address) { + /* Check if we can just write this directly to the compacted flash area */ + uint32_t directAddress = FEE_COMPACTED_BASE_ADDRESS + (Address & 0xFFF8); + + /* Write the value directly to the compacted area without a log entry */ + if (*(uint64_t *)directAddress == FEE_EMPTY_DWORD) { + /* Write the value directly to the compacted area without a log entry */ + uint64_t value = ~*(uint64_t *)(&DataBuf[Address & 0xFFF8]); + + /* Early exit if a write isn't needed */ + if (value == FEE_EMPTY_DWORD) return FLASH_COMPLETE; + + FLASH_Unlock(); + + /* write to flash */ + FLASH_Status status = FLASH_ProgramDoubleWord(directAddress, value); + + FLASH_Lock(); + + return status; + } + return 0; +} + +static void blank_check(uint32_t Address) { + /* Align address to 64 bit */ + Address &= (~0x07); + + /* Check if target address is blank */ + if (*(uint64_t *)(Address) != 0xFFFFFFFFFFFFFFFF) { + if ((Address & (FEE_PAGE_SIZE - 1)) == 0) { + /* Erase current page if first byte is not empty */ + FLASH_Unlock(); + FLASH_ErasePage(Address); + FLASH_Lock(); + } else { + /* Compact data if we encounter non empty target address after page head */ + eeprom_compact(); + } + } +} + +static uint8_t eeprom_write_log_byte_entry(uint16_t Address) { + /* if we can't find an empty spot, we must compact emulated eeprom */ + if (empty_slot >= (uint32_t *)FEE_WRITE_LOG_LAST_ADDRESS) { + /* compact the write log into the compacted flash area */ + return eeprom_compact(); + } + + blank_check((uint32_t)empty_slot); + + FLASH_Unlock(); + + /* Pack address and value into the same word */ + uint64_t value = (((uint64_t)(~DataBuf[Address])) << 32) | (FEE_BYTE_FLAG) | Address; + + /* write to flash */ + FLASH_Status status = FLASH_ProgramDoubleWord((uint32_t)empty_slot, value); + + empty_slot += 2; + + FLASH_Lock(); + + return status; +} + +static uint8_t eeprom_write_log_word_entry(uint16_t Address) { + /* if we can't find an empty spot, we must compact emulated eeprom */ + if (empty_slot >= (uint32_t *)FEE_WRITE_LOG_LAST_ADDRESS) { + /* compact the write log into the compacted flash area */ + return eeprom_compact(); + } + + blank_check((uint32_t)empty_slot); + + FLASH_Unlock(); + + /* Pack address and value into the same word */ + uint64_t value = (((uint64_t)(~(*(uint16_t *)&DataBuf[Address]))) << 32) | (FEE_WORD_FLAG) | Address; + + /* write to flash */ + FLASH_Status status = FLASH_ProgramDoubleWord((uint32_t)empty_slot, value); + + empty_slot += 2; + + FLASH_Lock(); + + return status; +} + +static uint8_t eeprom_write_log_dword_entry(uint16_t Address) { + /* if we can't find an empty spot, we must compact emulated eeprom */ + if (empty_slot >= (uint32_t *)FEE_WRITE_LOG_LAST_ADDRESS) { + /* compact the write log into the compacted flash area */ + return eeprom_compact(); + } + + blank_check((uint32_t)empty_slot); + + FLASH_Unlock(); + + /* Pack address and value into the same word */ + uint64_t value = (((uint64_t)(~(*(uint32_t *)&DataBuf[Address]))) << 32) | (FEE_DWORD_FLAG) | Address; + + /* write to flash */ + FLASH_Status status = FLASH_ProgramDoubleWord((uint32_t)empty_slot, value); + + empty_slot += 2; + + FLASH_Lock(); + + return status; +} + +uint8_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte) { + /* if the address is out-of-bounds, do nothing */ + if (Address >= (FEE_DENSITY_BYTES)) { + return FLASH_BAD_ADDRESS; + } + + /* if the value is the same, don't bother writing it */ + if (DataBuf[Address] == DataByte) { + return 0; + } + + /* keep DataBuf cache in sync */ + DataBuf[Address] = DataByte; + + /* perform the write into flash memory */ + /* First, attempt to write directly into the compacted flash area */ + FLASH_Status status = eeprom_write_direct_entry(Address); + + if (!status) { + status = eeprom_write_log_byte_entry(Address); + } + + return status; +} + +uint8_t EEPROM_WriteDataWord(uint16_t Address, uint16_t DataWord) { + /* if the address is out-of-bounds, do nothing */ + if (Address >= (FEE_DENSITY_BYTES - 1)) { + return FLASH_BAD_ADDRESS; + } + + /* if the value is the same, don't bother writing it */ + if (*(uint16_t *)&DataBuf[Address] == DataWord) { + return 0; + } + + /* keep DataBuf cache in sync */ + *(uint16_t *)(&DataBuf[Address]) = DataWord; + + /* perform the write into flash memory */ + /* First, attempt to write directly into the compacted flash area */ + FLASH_Status status = eeprom_write_direct_entry(Address); + + if (!status) { + status = eeprom_write_log_word_entry(Address); + } + + return status; +} + +uint8_t EEPROM_WriteDataDWord(uint16_t Address, uint32_t DataDWord) { + /* if the address is out-of-bounds, do nothing */ + if (Address >= (FEE_DENSITY_BYTES - 3)) { + return FLASH_BAD_ADDRESS; + } + + /* if the value is the same, don't bother writing it */ + if (*(uint32_t *)&DataBuf[Address] == DataDWord) { + return 0; + } + + /* keep DataBuf cache in sync */ + *(uint32_t *)&DataBuf[Address] = DataDWord; + + /* perform the write into flash memory */ + /* First, attempt to write directly into the compacted flash area */ + FLASH_Status status = eeprom_write_direct_entry(Address); + + if (!status) { + status = eeprom_write_log_dword_entry(Address); + } + + return status; +} + +uint8_t EEPROM_ReadDataByte(uint16_t Address) { + uint8_t DataByte = 0xFF; + + if (Address < FEE_DENSITY_BYTES) { + DataByte = DataBuf[Address]; + } + + return DataByte; +} + +uint16_t EEPROM_ReadDataWord(uint16_t Address) { + uint16_t DataWord = 0xFFFF; + + if (Address < FEE_DENSITY_BYTES - 1) { + /* Check word alignment */ + if (Address % 2) { + DataWord = DataBuf[Address] | (DataBuf[Address + 1] << 8); + } else { + DataWord = *(uint16_t *)(&DataBuf[Address]); + } + } + + return DataWord; +} + +/***************************************************************************** + * Bind to eeprom_driver.c + *******************************************************************************/ +uint16_t eeprom_driver_init(void) { + return EEPROM_Init(); +} + +void eeprom_driver_erase(void) { + EEPROM_Erase(); +} + +void eeprom_read_block(void *buf, const void *addr, size_t len) { + const uint8_t *src = (const uint8_t *)addr; + uint8_t * dest = (uint8_t *)buf; + + /* Check word alignment */ + if (len && (uint32_t)src % 2) { + /* Read the unaligned first byte */ + *dest++ = EEPROM_ReadDataByte((const uintptr_t)((uint16_t *)src)); + --len; + } + + uint16_t value; + bool aligned = ((uint32_t)dest % 2 == 0); + while (len > 1) { + value = EEPROM_ReadDataWord((const uintptr_t)((uint16_t *)src)); + if (aligned) { + *(uint16_t *)dest = value; + dest += 2; + } else { + *dest++ = value; + *dest++ = value >> 8; + } + src += 2; + len -= 2; + } + if (len) { + *dest = EEPROM_ReadDataByte((const uintptr_t)src); + } +} + +void eeprom_write_block(const void *buf, void *addr, size_t len) { + uint8_t * dest = (uint8_t *)addr; + const uint8_t *src = (const uint8_t *)buf; + uint8_t write_len; + + while (len > 0) { + /* Check and try to write double word fisrt */ + if ((uintptr_t)dest % 4 == 0 && len >= 4) { + uint32_t dwvalue; + bool dwaligned = ((uint32_t)src % 4 == 0); + + if (dwaligned) { + dwvalue = *(uint32_t *)src; + } else { + dwvalue = *(uint8_t *)src | (*(uint8_t *)(src + 1) << 8) | (*(uint8_t *)(src + 2) << 16) | (*(uint8_t *)(src + 3) << 24); + } + EEPROM_WriteDataDWord((uintptr_t)((uint16_t *)dest), dwvalue); + write_len = 4; + } + /* Check and try to write word */ + else if ((uintptr_t)dest % 2 == 0 && len >= 2) { + uint16_t wvalue; + bool waligned = ((uintptr_t)src % 2 == 0); + + if (waligned) { + wvalue = *(uint16_t *)src; + } else { + wvalue = *(uint8_t *)src | (*(uint8_t *)(src + 1) << 8); + } + EEPROM_WriteDataWord((uintptr_t)((uint16_t *)dest), wvalue); + write_len = 2; + } else { + /* Write the unaligned or single byte */ + EEPROM_WriteDataByte((uintptr_t)dest, *src); + write_len = 1; + } + + dest += write_len; + src += write_len; + len -= write_len; + } +} + +void NMI_Handler(void) { + if (FLASH->ECCR & FLASH_ECCR_ECCD) { + /* Clear ECCD error NMI */ + FLASH->ECCR = FLASH_ECCR_ECCD; + eccd = true; + } +} diff --git a/platforms/chibios/eeprom_stm32_l4.h b/platforms/chibios/eeprom_stm32_l4.h new file mode 100644 index 0000000000..77b36d5cce --- /dev/null +++ b/platforms/chibios/eeprom_stm32_l4.h @@ -0,0 +1,34 @@ +/* + * This software is experimental and a work in progress. + * Under no circumstances should these files be used in relation to any critical system(s). + * Use of these files is at your own risk. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by + * Artur F. + * + * Modifications for QMK and STM32L432 by lalalademaxiya1 & lokher + * + * To add a new MCU, please provide the flash page size and the total flash size in Kb. + * The number of available pages must be at least two. Only one page for the total EEPROM size. + * It is recommend to set the number of log page to 3~5 times of data page for better wear leveling. + * + */ + +#pragma once + +typedef unsigned long long uint64_t; + +uint16_t EEPROM_Init(void); +void EEPROM_Erase(void); +uint8_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte); +uint8_t EEPROM_WriteDataWord(uint16_t Address, uint16_t DataWord); +uint8_t EEPROM_WriteDataDWord(uint16_t Address, uint32_t DataDWord); +uint8_t EEPROM_ReadDataByte(uint16_t Address); +uint16_t EEPROM_ReadDataWord(uint16_t Address); diff --git a/platforms/chibios/flash_stm32.c b/platforms/chibios/flash_stm32.c index 72c41b8b78..59c77e0bc5 100644 --- a/platforms/chibios/flash_stm32.c +++ b/platforms/chibios/flash_stm32.c @@ -51,6 +51,15 @@ static uint8_t ADDR2PAGE(uint32_t Page_Address) { } #endif +#if defined(STM32L4XX) +# define FLASH_SR_PGERR (FLASH_SR_PROGERR | FLASH_SR_PGAERR | FLASH_SR_SIZERR | FLASH_SR_PGSERR) +# define FLASH_OBR_OPTERR FLASH_SR_OPERR +# define FLASH_KEY1 0x45670123U +# define FLASH_KEY2 0xCDEF89ABU + +static uint32_t ADDR2PAGE(uint32_t Page_Address) { return (Page_Address - FLASH_BASE) / 0x800; } +#endif + /* Delay definition */ #define EraseTimeout ((uint32_t)0x00000FFF) #define ProgramTimeout ((uint32_t)0x0000001F) @@ -128,6 +137,9 @@ FLASH_Status FLASH_ErasePage(uint32_t Page_Address) { #if defined(FLASH_CR_SNB) FLASH->CR &= ~FLASH_CR_SNB; FLASH->CR |= FLASH_CR_SER | (ADDR2PAGE(Page_Address) << FLASH_CR_SNB_Pos); +#elif defined(FLASH_CR_PNB) + FLASH->CR &= ~FLASH_CR_PNB; + FLASH->CR |= FLASH_CR_PER | (ADDR2PAGE(Page_Address) << FLASH_CR_PNB_Pos); #else FLASH->CR |= FLASH_CR_PER; FLASH->AR = Page_Address; @@ -140,6 +152,8 @@ FLASH_Status FLASH_ErasePage(uint32_t Page_Address) { /* if the erase operation is completed, disable the configured Bits */ #if defined(FLASH_CR_SNB) FLASH->CR &= ~(FLASH_CR_SER | FLASH_CR_SNB); +#elif defined(FLASH_CR_PNB) + FLASH->CR &= ~(FLASH_CR_PER | FLASH_CR_PNB); #else FLASH->CR &= ~FLASH_CR_PER; #endif @@ -150,6 +164,46 @@ FLASH_Status FLASH_ErasePage(uint32_t Page_Address) { return status; } +#if defined(STM32L4XX) +/** + * @brief Programs double words at a specified address. + * @param Address: specifies the address to be programmed. + * @param Data: specifies the data to be programmed. + * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, + * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. + */ +FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data) { + FLASH_Status status = FLASH_BAD_ADDRESS; + + if (IS_FLASH_ADDRESS(Address)) { + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(ProgramTimeout); + if (status == FLASH_COMPLETE) { + /* if the previous operation is completed, proceed to program the new data */ + /* disable data cache first */ + FLASH->ACR &= ~FLASH_ACR_DCEN; + FLASH->CR |= FLASH_CR_PG; + *(__IO uint32_t*)Address = (uint32_t)Data; + __ISB(); + *(__IO uint32_t*)(Address + 4U) = (uint32_t)(Data >> 32); + /* Wait for last operation to be completed */ + status = FLASH_WaitForLastOperation(ProgramTimeout); + if (status != FLASH_TIMEOUT) { + /* if the program operation is completed, disable the PG Bit */ + FLASH->CR &= ~FLASH_CR_PG; + } + FLASH->SR = (FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPERR); + /* reset data cache */ + FLASH->ACR |= FLASH_ACR_DCRST; + FLASH->ACR &= ~FLASH_ACR_DCRST; + /* enable data cache */ + FLASH->ACR |= FLASH_ACR_DCEN; + } + } + return status; +} + +#else /** * @brief Programs a half word at a specified address. * @param Address: specifies the address to be programmed. @@ -183,6 +237,7 @@ FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data) { } return status; } +#endif /** * @brief Unlocks the FLASH Program Erase Controller. diff --git a/platforms/chibios/flash_stm32.h b/platforms/chibios/flash_stm32.h index 6c66642ec5..97f8ea7cfe 100644 --- a/platforms/chibios/flash_stm32.h +++ b/platforms/chibios/flash_stm32.h @@ -35,6 +35,7 @@ typedef enum { FLASH_BUSY = 1, FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_ERROR_OPT, FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout); FLASH_Status FLASH_ErasePage(uint32_t Page_Address); FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data); +FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data); void FLASH_Unlock(void); void FLASH_Lock(void); diff --git a/platforms/eeprom.h b/platforms/eeprom.h index 091e6e4400..42afd643f6 100644 --- a/platforms/eeprom.h +++ b/platforms/eeprom.h @@ -39,6 +39,9 @@ void eeprom_update_block(const void *__src, void *__dst, size_t __n); #elif defined(EEPROM_STM32_L0_L1) # include "eeprom_stm32_L0_L1.h" # define TOTAL_EEPROM_BYTE_COUNT (STM32_ONBOARD_EEPROM_SIZE) +#elif defined(EEPROM_STM32_L4) +# include "eeprom_stm32_defs.h" +# define TOTAL_EEPROM_BYTE_COUNT (FEE_DENSITY_BYTES) #elif defined(EEPROM_TEENSY) # include "eeprom_teensy.h" # define TOTAL_EEPROM_BYTE_COUNT (EEPROM_SIZE) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index a65f9d6d18..1a89e70e34 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -257,7 +257,10 @@ __attribute__((weak)) void keyboard_post_init_kb(void) { void keyboard_setup(void) { print_set_sendchar(sendchar); #ifdef EEPROM_DRIVER - eeprom_driver_init(); + if (!eeprom_driver_init()) { + eeconfig_init(); + eeprom_driver_init(); + } #endif matrix_setup(); keyboard_pre_init_kb(); diff --git a/quantum/led_matrix/led_matrix.c b/quantum/led_matrix/led_matrix.c index 38ed79bed0..07b3bbca30 100644 --- a/quantum/led_matrix/led_matrix.c +++ b/quantum/led_matrix/led_matrix.c @@ -98,6 +98,10 @@ last_hit_t g_last_hit_tracker; #endif // LED_MATRIX_KEYREACTIVE_ENABLED // internals +#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE +static bool driver_shutdown = false; +#endif +static uint8_t led_enable_eeprom = false; static bool suspend_state = false; static uint8_t led_last_enable = UINT8_MAX; static uint8_t led_last_effect = UINT8_MAX; @@ -105,7 +109,8 @@ static effect_params_t led_effect_params = {0, LED_FLAG_ALL, false}; static led_task_states led_task_state = SYNCING; #if LED_DISABLE_TIMEOUT > 0 static uint32_t led_anykey_timer; -#endif // LED_DISABLE_TIMEOUT > 0 +static uint32_t led_disable_timeout = LED_DISABLE_TIMEOUT; +#endif // LED_DISABLE_TIMEOUT > 0 // double buffers static uint32_t led_timer_buffer; @@ -227,12 +232,22 @@ void process_led_matrix(uint8_t row, uint8_t col, bool pressed) { #endif // defined(LED_MATRIX_FRAMEBUFFER_EFFECTS) && defined(ENABLE_LED_MATRIX_TYPING_HEATMAP) } +void led_matrix_none_indicators(void) { + led_matrix_none_indicators_kb(); + led_matrix_none_indicators_user(); +} + +__attribute__((weak)) void led_matrix_none_indicators_kb(void) {} + +__attribute__((weak)) void led_matrix_none_indicators_user(void) {} + static bool led_matrix_none(effect_params_t *params) { if (!params->init) { return false; } led_matrix_set_value_all(0); + led_matrix_none_indicators(); return false; } @@ -343,10 +358,24 @@ static void led_task_flush(uint8_t effect) { // update last trackers after the first full render so we can init over several frames led_last_effect = effect; led_last_enable = led_matrix_eeconfig.enable; +#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE + // exit from shutdown to if neccesary + if (driver_shutdown) { + led_matrix_driver.exit_shutdown(); + driver_shutdown = false; + } +#endif // update pwm buffers led_matrix_update_pwm_buffers(); +#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE + // shutdown if neccesary + if (effect == LED_MATRIX_NONE && !driver_shutdown && led_matrix_driver_allow_shutdown()) { + led_matrix_driver_shutdown(); + } +#endif + // next task led_task_state = SYNCING; } @@ -358,8 +387,8 @@ void led_matrix_task(void) { // while suspended and just do a software shutdown. This is a cheap hack for now. bool suspend_backlight = suspend_state || #if LED_DISABLE_TIMEOUT > 0 - (led_anykey_timer > (uint32_t)LED_DISABLE_TIMEOUT) || -#endif // LED_DISABLE_TIMEOUT > 0 + (led_anykey_timer > led_disable_timeout) || +#endif // LED_DISABLE_TIMEOUT > 0 false; uint8_t effect = suspend_backlight || !led_matrix_eeconfig.enable ? 0 : led_matrix_eeconfig.mode; @@ -384,6 +413,11 @@ void led_matrix_task(void) { } } +static inline void led_enable_state_backup(void) { + dprintf("led_enable_state_backup\n"); + led_enable_eeprom = led_matrix_eeconfig.enable; +} + void led_matrix_indicators(void) { led_matrix_indicators_kb(); led_matrix_indicators_user(); @@ -417,6 +451,9 @@ __attribute__((weak)) void led_matrix_indicators_advanced_user(uint8_t led_min, void led_matrix_init(void) { led_matrix_driver.init(); +#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE + driver_shutdown = false; +#endif #ifdef LED_MATRIX_KEYREACTIVE_ENABLED g_last_hit_tracker.count = 0; @@ -441,6 +478,7 @@ void led_matrix_init(void) { dprintf("led_matrix_init_drivers led_matrix_eeconfig.mode = 0. Write default values to EEPROM.\n"); eeconfig_update_led_matrix_default(); } + led_enable_state_backup(); eeconfig_debug_led_matrix(); // display current eeprom values } @@ -462,6 +500,7 @@ void led_matrix_toggle_eeprom_helper(bool write_to_eeprom) { led_matrix_eeconfig.enable ^= 1; led_task_state = STARTING; eeconfig_flag_led_matrix(write_to_eeprom); + if (write_to_eeprom) led_enable_state_backup(); dprintf("led matrix toggle [%s]: led_matrix_eeconfig.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", led_matrix_eeconfig.enable); } void led_matrix_toggle_noeeprom(void) { @@ -474,6 +513,7 @@ void led_matrix_toggle(void) { void led_matrix_enable(void) { led_matrix_enable_noeeprom(); eeconfig_flag_led_matrix(true); + led_enable_state_backup(); } void led_matrix_enable_noeeprom(void) { @@ -484,6 +524,7 @@ void led_matrix_enable_noeeprom(void) { void led_matrix_disable(void) { led_matrix_disable_noeeprom(); eeconfig_flag_led_matrix(true); + led_enable_state_backup(); } void led_matrix_disable_noeeprom(void) { @@ -495,6 +536,11 @@ uint8_t led_matrix_is_enabled(void) { return led_matrix_eeconfig.enable; } + +uint8_t led_matrix_is_enabled_eeprom(void) { + return led_enable_eeprom; +} + void led_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) { if (!led_matrix_eeconfig.enable) { return; @@ -563,6 +609,12 @@ uint8_t led_matrix_get_val(void) { } void led_matrix_increase_val_helper(bool write_to_eeprom) { +#ifdef LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL + if (!led_matrix_eeconfig.enable) { + dprintf("increase_val to enable"); + led_matrix_toggle_eeprom_helper(write_to_eeprom); + } +#endif led_matrix_set_val_eeprom_helper(qadd8(led_matrix_eeconfig.val, LED_MATRIX_VAL_STEP), write_to_eeprom); } void led_matrix_increase_val_noeeprom(void) { @@ -580,6 +632,12 @@ void led_matrix_decrease_val_noeeprom(void) { } void led_matrix_decrease_val(void) { led_matrix_decrease_val_helper(true); +#ifdef LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL + if (led_matrix_eeconfig.enable && led_matrix_eeconfig.val <= LED_MATRIX_BRIGHTNESS_TURN_OFF_VAL) { + dprintf("decrease_val to disable\n"); + led_matrix_toggle_eeprom_helper(true); + } +#endif } void led_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) { @@ -625,3 +683,25 @@ led_flags_t led_matrix_get_flags(void) { void led_matrix_set_flags(led_flags_t flags) { led_matrix_eeconfig.flags = flags; } + +#if LED_DISABLE_TIMEOUT > 0 +void led_matrix_disable_timeout_set(uint32_t timeout) { + led_disable_timeout = timeout; +} +void led_matrix_disable_time_reset(void){ + led_anykey_timer = 0; +} +#endif + +#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE +void led_matrix_driver_shutdown(void) { + led_matrix_driver.shutdown(); + driver_shutdown = true; +}; + +bool led_matrix_is_driver_shutdown(void) { + return driver_shutdown; +} + +__attribute__((weak)) bool led_matrix_driver_allow_shutdown(void) { return true; }; +#endif \ No newline at end of file diff --git a/quantum/led_matrix/led_matrix.h b/quantum/led_matrix/led_matrix.h index d21f36e295..963992fb55 100644 --- a/quantum/led_matrix/led_matrix.h +++ b/quantum/led_matrix/led_matrix.h @@ -33,6 +33,9 @@ #ifdef IS31FL3733 # include "is31fl3733-simple.h" #endif +#ifdef CKLED2001 +# include "ckled2001-simple.h" +#endif #ifndef LED_MATRIX_LED_FLUSH_LIMIT # define LED_MATRIX_LED_FLUSH_LIMIT 16 @@ -75,6 +78,8 @@ #define LED_MATRIX_TEST_LED_FLAGS() \ if (!HAS_ANY_FLAGS(g_led_config.flags[i], params->flags)) continue +#define LED_DISABLE_TIME_INFINITE (UINT32_MAX) + enum led_matrix_effects { LED_MATRIX_NONE = 0, @@ -114,6 +119,9 @@ void process_led_matrix(uint8_t row, uint8_t col, bool pressed); void led_matrix_task(void); +void led_matrix_none_indicators_kb(void); +void led_matrix_none_indicators_user(void); + // This runs after another backlight effect and replaces // values already set void led_matrix_indicators(void); @@ -135,6 +143,7 @@ void led_matrix_enable_noeeprom(void); void led_matrix_disable(void); void led_matrix_disable_noeeprom(void); uint8_t led_matrix_is_enabled(void); +uint8_t led_matrix_is_enabled_eeprom(void); void led_matrix_mode(uint8_t mode); void led_matrix_mode_noeeprom(uint8_t mode); uint8_t led_matrix_get_mode(void); @@ -158,6 +167,19 @@ void led_matrix_decrease_speed(void); void led_matrix_decrease_speed_noeeprom(void); led_flags_t led_matrix_get_flags(void); void led_matrix_set_flags(led_flags_t flags); +#ifdef LED_DISABLE_TIMEOUT +# if LED_DISABLE_TIMEOUT > 0 +void led_matrix_disable_timeout_set(uint32_t timeout); +void led_matrix_disable_time_reset(void); +# endif +#endif + +#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE +void led_matrix_driver_shutdown(void); +bool led_matrix_is_driver_shutdown(void); +bool led_matrix_driver_allow_shutdown(void); +#endif + typedef struct { /* Perform any initialisation required for the other driver functions to work. */ @@ -169,6 +191,12 @@ typedef struct { void (*set_value_all)(uint8_t value); /* Flush any buffered changes to the hardware. */ void (*flush)(void); +#ifdef LED_MATRIX_DRIVER_SHUTDOWN_ENABLE + /* Shutdown the driver. */ + void (*shutdown)(void); + /* Exit from shutdown state. */ + void (*exit_shutdown)(void); +#endif } led_matrix_driver_t; static inline bool led_matrix_check_finished_leds(uint8_t led_idx) { diff --git a/quantum/led_matrix/led_matrix_drivers.c b/quantum/led_matrix/led_matrix_drivers.c index 847ca1c310..7a6f3c5767 100644 --- a/quantum/led_matrix/led_matrix_drivers.c +++ b/quantum/led_matrix/led_matrix_drivers.c @@ -25,7 +25,7 @@ * in their own files. */ -#if defined(IS31FL3731) || defined(IS31FL3733) || defined(IS31FLCOMMON) +#if defined(IS31FL3731) || defined(IS31FL3733) || defined(IS31FLCOMMON) || defined(CKLED2001) # include "i2c_master.h" static void init(void) { @@ -78,6 +78,23 @@ static void init(void) { # endif # endif # endif + +# elif defined(CKLED2001) +# if defined(LED_DRIVER_SHUTDOWN_PIN) + setPinOutput(LED_DRIVER_SHUTDOWN_PIN); + writePinHigh(LED_DRIVER_SHUTDOWN_PIN); +# endif + + CKLED2001_init(DRIVER_ADDR_1); +# if defined(DRIVER_ADDR_2) + CKLED2001_init(DRIVER_ADDR_2); +# if defined(DRIVER_ADDR_3) + CKLED2001_init(DRIVER_ADDR_3); +# if defined(DRIVER_ADDR_4) + CKLED2001_init(DRIVER_ADDR_4); +# endif +# endif +# endif # endif for (int index = 0; index < DRIVER_LED_TOTAL; index++) { @@ -87,6 +104,8 @@ static void init(void) { IS31FL3733_set_led_control_register(index, true); # elif defined(IS31FLCOMMON) IS31FL_simple_set_scaling_buffer(index, true); +# elif defined(CKLED2001) + CKLED2001_set_led_control_register(index, true); # endif } @@ -129,6 +148,18 @@ static void init(void) { # endif # endif # endif + +# elif defined(CKLED2001) + CKLED2001_update_led_control_registers(DRIVER_ADDR_1, 0); +# if defined(DRIVER_ADDR_2) + CKLED2001_update_led_control_registers(DRIVER_ADDR_2, 1); +# if defined(DRIVER_ADDR_3) + CKLED2001_update_led_control_registers(DRIVER_ADDR_3, 2); +# if defined(DRIVER_ADDR_4) + CKLED2001_update_led_control_registers(DRIVER_ADDR_4, 3); +# endif +# endif +# endif # endif } @@ -194,5 +225,66 @@ const led_matrix_driver_t led_matrix_driver = { .set_value = IS31FL_simple_set_brightness, .set_value_all = IS31FL_simple_set_brigntness_all, }; + +# elif defined(CKLED2001) +static void flush(void) { + CKLED2001_update_pwm_buffers(DRIVER_ADDR_1, 0); +# if defined(DRIVER_ADDR_2) + CKLED2001_update_pwm_buffers(DRIVER_ADDR_2, 1); +# if defined(DRIVER_ADDR_3) + CKLED2001_update_pwm_buffers(DRIVER_ADDR_3, 2); +# if defined(DRIVER_ADDR_4) + CKLED2001_update_pwm_buffers(DRIVER_ADDR_4, 3); +# endif +# endif +# endif +} + +# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) +static void shutdown(void) { +# if defined(LED_DRIVER_SHUTDOWN_PIN) + writePinLow(LED_DRIVER_SHUTDOWN_PIN); +# else + CKLED2001_sw_shutdown(DRIVER_ADDR_1); +# if defined(DRIVER_ADDR_2) + CKLED2001_sw_shutdown(DRIVER_ADDR_2); +# if defined(DRIVER_ADDR_3) + CKLED2001_sw_shutdown(DRIVER_ADDR_3); +# if defined(DRIVER_ADDR_4) + CKLED2001_sw_shutdown(DRIVER_ADDR_4); +# endif +# endif +# endif +# endif +} + +static void exit_shutdown(void) { +# if defined(LED_DRIVER_SHUTDOWN_PIN) + writePinHigh(LED_DRIVER_SHUTDOWN_PIN); +# else + CKLED2001_sw_return_normal(DRIVER_ADDR_1); +# if defined(DRIVER_ADDR_2) + CKLED2001_sw_return_normal(DRIVER_ADDR_2); +# if defined(DRIVER_ADDR_3) + CKLED2001_sw_return_normal(DRIVER_ADDR_3); +# if defined(DRIVER_ADDR_4) + CKLED2001_sw_return_normal(DRIVER_ADDR_4); +# endif +# endif +# endif +# endif +} +# endif + +const led_matrix_driver_t led_matrix_driver = { + .init = init, + .flush = flush, + .set_value = CKLED2001_set_value, + .set_value_all = CKLED2001_set_value_all, +# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) + .shutdown = shutdown, + .exit_shutdown = exit_shutdown +# endif +}; # endif #endif diff --git a/quantum/rgb_matrix/rgb_matrix.c b/quantum/rgb_matrix/rgb_matrix.c index f721dfc7f2..1f9ad47c81 100644 --- a/quantum/rgb_matrix/rgb_matrix.c +++ b/quantum/rgb_matrix/rgb_matrix.c @@ -121,6 +121,10 @@ last_hit_t g_last_hit_tracker; #endif // RGB_MATRIX_KEYREACTIVE_ENABLED // internals +#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE +static bool driver_shutdown = false; +#endif +static uint8_t rgb_enable_eeprom = false; static bool suspend_state = false; static uint8_t rgb_last_enable = UINT8_MAX; static uint8_t rgb_last_effect = UINT8_MAX; @@ -128,7 +132,8 @@ static effect_params_t rgb_effect_params = {0, LED_FLAG_ALL, false}; static rgb_task_states rgb_task_state = SYNCING; #if RGB_DISABLE_TIMEOUT > 0 static uint32_t rgb_anykey_timer; -#endif // RGB_DISABLE_TIMEOUT > 0 +static uint32_t rgb_disable_timeout = RGB_DISABLE_TIMEOUT; +#endif // RGB_DISABLE_TIMEOUT > 0 // double buffers static uint32_t rgb_timer_buffer; @@ -279,12 +284,22 @@ void rgb_matrix_test(void) { } } +void rgb_matrix_none_indicators(void) { + rgb_matrix_none_indicators_kb(); + rgb_matrix_none_indicators_user(); +} + +__attribute__((weak)) void rgb_matrix_none_indicators_kb(void) {} + +__attribute__((weak)) void rgb_matrix_none_indicators_user(void) {} + static bool rgb_matrix_none(effect_params_t *params) { if (!params->init) { return false; } rgb_matrix_set_color_all(0, 0, 0); + rgb_matrix_none_indicators(); return false; } @@ -398,9 +413,21 @@ static void rgb_task_flush(uint8_t effect) { // update last trackers after the first full render so we can init over several frames rgb_last_effect = effect; rgb_last_enable = rgb_matrix_config.enable; - +#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE + // exit from shutdown to if neccesary + if (driver_shutdown) { + rgb_matrix_driver.exit_shutdown(); + driver_shutdown = false; + } +#endif // update pwm buffers rgb_matrix_update_pwm_buffers(); +#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE + // shutdown to if neccesary + if (effect == RGB_MATRIX_NONE && !driver_shutdown && rgb_matrix_driver_allow_shutdown()) { + rgb_matrix_driver_shutdown(); + } +#endif // next task rgb_task_state = SYNCING; @@ -413,7 +440,7 @@ void rgb_matrix_task(void) { // while suspended and just do a software shutdown. This is a cheap hack for now. bool suspend_backlight = suspend_state || #if RGB_DISABLE_TIMEOUT > 0 - (rgb_anykey_timer > (uint32_t)RGB_DISABLE_TIMEOUT) || + (rgb_anykey_timer > rgb_disable_timeout) || #endif // RGB_DISABLE_TIMEOUT > 0 false; @@ -439,6 +466,11 @@ void rgb_matrix_task(void) { } } +static inline void rgb_enable_state_backup(void) { + dprintf("rgb_enable_state_backup\n"); + rgb_enable_eeprom = rgb_matrix_config.enable; +} + void rgb_matrix_indicators(void) { rgb_matrix_indicators_kb(); rgb_matrix_indicators_user(); @@ -472,6 +504,9 @@ __attribute__((weak)) void rgb_matrix_indicators_advanced_user(uint8_t led_min, void rgb_matrix_init(void) { rgb_matrix_driver.init(); +#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE + driver_shutdown = false; +#endif #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED g_last_hit_tracker.count = 0; @@ -496,6 +531,7 @@ void rgb_matrix_init(void) { dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n"); eeconfig_update_rgb_matrix_default(); } + rgb_enable_state_backup(); eeconfig_debug_rgb_matrix(); // display current eeprom values } @@ -517,6 +553,7 @@ void rgb_matrix_toggle_eeprom_helper(bool write_to_eeprom) { rgb_matrix_config.enable ^= 1; rgb_task_state = STARTING; eeconfig_flag_rgb_matrix(write_to_eeprom); + if (write_to_eeprom) rgb_enable_state_backup(); dprintf("rgb matrix toggle [%s]: rgb_matrix_config.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.enable); } void rgb_matrix_toggle_noeeprom(void) { @@ -529,6 +566,7 @@ void rgb_matrix_toggle(void) { void rgb_matrix_enable(void) { rgb_matrix_enable_noeeprom(); eeconfig_flag_rgb_matrix(true); + rgb_enable_state_backup(); } void rgb_matrix_enable_noeeprom(void) { @@ -539,6 +577,7 @@ void rgb_matrix_enable_noeeprom(void) { void rgb_matrix_disable(void) { rgb_matrix_disable_noeeprom(); eeconfig_flag_rgb_matrix(true); + rgb_enable_state_backup(); } void rgb_matrix_disable_noeeprom(void) { @@ -550,6 +589,10 @@ uint8_t rgb_matrix_is_enabled(void) { return rgb_matrix_config.enable; } +uint8_t rgb_matrix_is_enabled_eeprom(void) { + return rgb_enable_eeprom; +} + void rgb_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) { if (!rgb_matrix_config.enable) { return; @@ -669,6 +712,12 @@ void rgb_matrix_decrease_sat(void) { } void rgb_matrix_increase_val_helper(bool write_to_eeprom) { +#ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL + if (!rgb_matrix_config.enable) { + dprintf("increase_val to enable"); + rgb_matrix_toggle_eeprom_helper(write_to_eeprom); + } +#endif rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP), write_to_eeprom); } void rgb_matrix_increase_val_noeeprom(void) { @@ -680,6 +729,12 @@ void rgb_matrix_increase_val(void) { void rgb_matrix_decrease_val_helper(bool write_to_eeprom) { rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, qsub8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP), write_to_eeprom); +#ifdef RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL + if (rgb_matrix_config.enable && rgb_matrix_config.hsv.v <= RGB_MATRIX_BRIGHTNESS_TURN_OFF_VAL) { + dprintf("decrease_val to disable\n"); + rgb_matrix_toggle_eeprom_helper(write_to_eeprom); + } +#endif } void rgb_matrix_decrease_val_noeeprom(void) { rgb_matrix_decrease_val_helper(false); @@ -731,3 +786,22 @@ led_flags_t rgb_matrix_get_flags(void) { void rgb_matrix_set_flags(led_flags_t flags) { rgb_matrix_config.flags = flags; } + + +#if RGB_DISABLE_TIMEOUT > 0 +void rgb_matrix_disable_timeout_set(uint32_t timeout) { + rgb_disable_timeout = timeout; +} +void rgb_matrix_disable_time_reset(void){ rgb_anykey_timer = 0; } +#endif + +#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE +void rgb_matrix_driver_shutdown(void) { + rgb_matrix_driver.shutdown(); + driver_shutdown = true; +}; + +bool rgb_matrix_is_driver_shutdown(void) { return driver_shutdown; } + +__attribute__((weak)) bool rgb_matrix_driver_allow_shutdown(void) { return true; }; +#endif \ No newline at end of file diff --git a/quantum/rgb_matrix/rgb_matrix.h b/quantum/rgb_matrix/rgb_matrix.h index 359d507a4d..dc3bda53a4 100644 --- a/quantum/rgb_matrix/rgb_matrix.h +++ b/quantum/rgb_matrix/rgb_matrix.h @@ -88,6 +88,8 @@ #define RGB_MATRIX_TEST_LED_FLAGS() \ if (!HAS_ANY_FLAGS(g_led_config.flags[i], params->flags)) continue +#define RGB_DISABLE_TIME_INFINITE (UINT32_MAX) + enum rgb_matrix_effects { RGB_MATRIX_NONE = 0, @@ -126,6 +128,9 @@ void process_rgb_matrix(uint8_t row, uint8_t col, bool pressed); void rgb_matrix_task(void); +void rgb_matrix_none_indicators_kb(void); +void rgb_matrix_none_indicators_user(void); + // This runs after another backlight effect and replaces // colors already set void rgb_matrix_indicators(void); @@ -149,6 +154,7 @@ void rgb_matrix_enable_noeeprom(void); void rgb_matrix_disable(void); void rgb_matrix_disable_noeeprom(void); uint8_t rgb_matrix_is_enabled(void); +uint8_t rgb_matrix_is_enabled_eeprom(void); void rgb_matrix_mode(uint8_t mode); void rgb_matrix_mode_noeeprom(uint8_t mode); uint8_t rgb_matrix_get_mode(void); @@ -183,6 +189,17 @@ void rgb_matrix_decrease_speed(void); void rgb_matrix_decrease_speed_noeeprom(void); led_flags_t rgb_matrix_get_flags(void); void rgb_matrix_set_flags(led_flags_t flags); +#ifdef RGB_DISABLE_TIMEOUT +# if RGB_DISABLE_TIMEOUT > 0 +void rgb_matrix_disable_timeout_set(uint32_t timeout); +void rgb_matrix_disable_time_reset(void); +# endif +#endif +#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE +void rgb_matrix_driver_shutdown(void); +bool rgb_matrix_is_driver_shutdown(void); +bool rgb_matrix_driver_allow_shutdown(void); +#endif #ifndef RGBLIGHT_ENABLE # define eeconfig_update_rgblight_current eeconfig_update_rgb_matrix @@ -237,6 +254,12 @@ typedef struct { void (*set_color_all)(uint8_t r, uint8_t g, uint8_t b); /* Flush any buffered changes to the hardware. */ void (*flush)(void); +#ifdef RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE + /* Shutdown the driver. */ + void (*shutdown)(void); + /* Exit from shutdown state. */ + void (*exit_shutdown)(void); +#endif } rgb_matrix_driver_t; static inline bool rgb_matrix_check_finished_leds(uint8_t led_idx) { diff --git a/quantum/rgb_matrix/rgb_matrix_drivers.c b/quantum/rgb_matrix/rgb_matrix_drivers.c index 27fa7369bf..4b02be98d2 100644 --- a/quantum/rgb_matrix/rgb_matrix_drivers.c +++ b/quantum/rgb_matrix/rgb_matrix_drivers.c @@ -94,6 +94,11 @@ static void init(void) { # endif # elif defined(CKLED2001) +# if defined(LED_DRIVER_SHUTDOWN_PIN) + setPinOutput(LED_DRIVER_SHUTDOWN_PIN); + writePinHigh(LED_DRIVER_SHUTDOWN_PIN); +# endif + CKLED2001_init(DRIVER_ADDR_1); # if defined(DRIVER_ADDR_2) CKLED2001_init(DRIVER_ADDR_2); @@ -295,11 +300,51 @@ static void flush(void) { # endif } +# if defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE) +static void shutdown(void) { +# if defined(LED_DRIVER_SHUTDOWN_PIN) + writePinLow(LED_DRIVER_SHUTDOWN_PIN); +# else + CKLED2001_sw_shutdown(DRIVER_ADDR_1); +# if defined(DRIVER_ADDR_2) + CKLED2001_sw_shutdown(DRIVER_ADDR_2); +# if defined(DRIVER_ADDR_3) + CKLED2001_sw_shutdown(DRIVER_ADDR_3); +# if defined(DRIVER_ADDR_4) + CKLED2001_sw_shutdown(DRIVER_ADDR_4); +# endif +# endif +# endif +# endif +} + +static void exit_shutdown(void) { +# if defined(LED_DRIVER_SHUTDOWN_PIN) + writePinHigh(LED_DRIVER_SHUTDOWN_PIN); +# else + CKLED2001_sw_return_normal(DRIVER_ADDR_1); +# if defined(DRIVER_ADDR_2) + CKLED2001_sw_return_normal(DRIVER_ADDR_2); +# if defined(DRIVER_ADDR_3) + CKLED2001_sw_return_normal(DRIVER_ADDR_3); +# if defined(DRIVER_ADDR_4) + CKLED2001_sw_return_normal(DRIVER_ADDR_4); +# endif +# endif +# endif +# endif +} +# endif + const rgb_matrix_driver_t rgb_matrix_driver = { .init = init, .flush = flush, .set_color = CKLED2001_set_color, .set_color_all = CKLED2001_set_color_all, +# if defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE) + .shutdown = shutdown, + .exit_shutdown = exit_shutdown +# endif }; # endif diff --git a/quantum/via.c b/quantum/via.c index 320bd5546d..55c4cb66cd 100644 --- a/quantum/via.c +++ b/quantum/via.c @@ -396,6 +396,12 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { dynamic_keymap_set_buffer(offset, size, &command_data[3]); break; } +#ifdef RAW_HID_CMD + case RAW_HID_CMD: { + raw_hid_receive_kb(data, length); + return; + } +#endif default: { // The command ID is not known // Return the unhandled state diff --git a/tmk_core/protocol.mk b/tmk_core/protocol.mk index 19fd7d2425..4ebdc7d8fd 100644 --- a/tmk_core/protocol.mk +++ b/tmk_core/protocol.mk @@ -48,8 +48,6 @@ endif ifeq ($(strip $(NKRO_ENABLE)), yes) ifeq ($(PROTOCOL), VUSB) $(info NKRO is not currently supported on V-USB, and has been disabled.) - else ifeq ($(strip $(BLUETOOTH_ENABLE)), yes) - $(info NKRO is not currently supported with Bluetooth, and has been disabled.) else TMK_COMMON_DEFS += -DNKRO_ENABLE SHARED_EP_ENABLE = yes diff --git a/tmk_core/protocol/chibios/chibios.c b/tmk_core/protocol/chibios/chibios.c index c9a480c325..05aa5104ee 100644 --- a/tmk_core/protocol/chibios/chibios.c +++ b/tmk_core/protocol/chibios/chibios.c @@ -192,7 +192,7 @@ void protocol_pre_task(void) { /* Remote wakeup */ if (suspend_wakeup_condition()) { usbWakeupHost(&USB_DRIVER); - restart_usb_driver(&USB_DRIVER); + usb_wakeup(&USB_DRIVER); } } /* Woken up */ diff --git a/tmk_core/protocol/chibios/usb_driver.c b/tmk_core/protocol/chibios/usb_driver.c index ad45f9b1da..0e3f28e3ea 100644 --- a/tmk_core/protocol/chibios/usb_driver.c +++ b/tmk_core/protocol/chibios/usb_driver.c @@ -408,7 +408,7 @@ void qmkusbDataTransmitted(USBDriver *usbp, usbep_t ep) { size. Otherwise the recipient may expect more data coming soon and not return buffered data to app. See section 5.8.3 Bulk Transfer Packet Size Constraints of the USB Specification document.*/ - if (!qmkusbp->config->fixed_size) { + if (!qmkusbp->config->fixed_size && (usbp->epc[ep]->ep_mode & USB_EP_MODE_TYPE) == USB_EP_MODE_TYPE_BULK) { usbStartTransmitI(usbp, ep, usbp->setup, 0); } diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 19e2e858fc..5862e02bf8 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -639,8 +639,7 @@ static bool usb_request_hook_cb(USBDriver *usbp) { if ((usbp->setup[4] == KEYBOARD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */ keyboard_protocol = ((usbp->setup[2]) != 0x00); /* LSB(wValue) */ #ifdef NKRO_ENABLE - keymap_config.nkro = !!keyboard_protocol; - if (!keymap_config.nkro && keyboard_idle) { + if (!keyboard_protocol && keyboard_idle) { #else /* NKRO_ENABLE */ if (keyboard_idle) { #endif /* NKRO_ENABLE */ @@ -745,20 +744,23 @@ void init_usb_driver(USBDriver *usbp) { chVTObjectInit(&keyboard_idle_timer); } -__attribute__((weak)) void restart_usb_driver(USBDriver *usbp) { - usbStop(usbp); - usbDisconnectBus(usbp); +__attribute__((weak)) void usb_wakeup(USBDriver *usbp) { +#if STM32_USB_USE_OTG1 || STM32_USB_USE_OTG1 + stm32_otg_t *otgp = usbp->otg; -#if USB_SUSPEND_WAKEUP_DELAY > 0 - // Some hubs, kvm switches, and monitors do - // weird things, with USB device state bouncing - // around wildly on wakeup, yielding race - // conditions that can corrupt the keyboard state. - // - // Pause for a while to let things settle... - wait_ms(USB_SUSPEND_WAKEUP_DELAY); + osalSysLock(); + /* If clocks are gated off, turn them back on (may be the case if + coming out of suspend mode).*/ + if (otgp->PCGCCTL & (PCGCCTL_STPPCLK | PCGCCTL_GATEHCLK)) { + /* Set to zero to un-gate the USB core clocks.*/ + otgp->PCGCCTL &= ~(PCGCCTL_STPPCLK | PCGCCTL_GATEHCLK); + } + _usb_wakeup(usbp); + osalSysUnlock(); #endif +} +__attribute__((weak)) void usb_start(USBDriver *usbp) { usbStart(usbp, &usbcfg); usbConnectBus(usbp); } diff --git a/tmk_core/protocol/chibios/usb_main.h b/tmk_core/protocol/chibios/usb_main.h index fb33c8cd0f..d1b8cec671 100644 --- a/tmk_core/protocol/chibios/usb_main.h +++ b/tmk_core/protocol/chibios/usb_main.h @@ -34,8 +34,11 @@ /* Initialize the USB driver and bus */ void init_usb_driver(USBDriver *usbp); -/* Restart the USB driver and bus */ -void restart_usb_driver(USBDriver *usbp); +/* Wakeup the USB driver */ +void usb_wakeup(USBDriver *usbp); + +/* Start the USB driver */ +void usb_start(USBDriver *usbp); /* --------------- * USB Event queue diff --git a/tmk_core/protocol/host.h b/tmk_core/protocol/host.h index 6b15f0d0c1..782f3fd70d 100644 --- a/tmk_core/protocol/host.h +++ b/tmk_core/protocol/host.h @@ -35,6 +35,7 @@ extern "C" { extern uint8_t keyboard_idle; extern uint8_t keyboard_protocol; +extern uint8_t bluetooth_report_protocol; /* host driver */ void host_set_driver(host_driver_t *driver); diff --git a/tmk_core/protocol/report.c b/tmk_core/protocol/report.c index 5755098c60..8bf2b8765b 100644 --- a/tmk_core/protocol/report.c +++ b/tmk_core/protocol/report.c @@ -20,6 +20,9 @@ #include "debug.h" #include "util.h" #include +#ifdef BLUETOOTH_ENABLE +#include "transport.h" +#endif #ifdef RING_BUFFERED_6KRO_REPORT_ENABLE # define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS) @@ -40,7 +43,14 @@ uint8_t has_anykey(report_keyboard_t* keyboard_report) { uint8_t* p = keyboard_report->keys; uint8_t lp = sizeof(keyboard_report->keys); #ifdef NKRO_ENABLE +#ifdef BLUETOOTH_ENABLE + if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) || + ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol)) + && keymap_config.nkro) { +#else if (keyboard_protocol && keymap_config.nkro) { +#endif + p = keyboard_report->nkro.bits; lp = sizeof(keyboard_report->nkro.bits); } @@ -57,7 +67,13 @@ uint8_t has_anykey(report_keyboard_t* keyboard_report) { */ uint8_t get_first_key(report_keyboard_t* keyboard_report) { #ifdef NKRO_ENABLE +#ifdef BLUETOOTH_ENABLE + if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) || + ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol)) + && keymap_config.nkro) { +#else if (keyboard_protocol && keymap_config.nkro) { +#endif uint8_t i = 0; for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++) ; @@ -88,7 +104,13 @@ bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key) { return false; } #ifdef NKRO_ENABLE +#ifdef BLUETOOTH_ENABLE + if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) || + ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol)) + && keymap_config.nkro) { +#else if (keyboard_protocol && keymap_config.nkro) { +#endif if ((key >> 3) < KEYBOARD_REPORT_BITS) { return keyboard_report->nkro.bits[key >> 3] & 1 << (key & 7); } else { @@ -242,7 +264,13 @@ void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) { */ void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) { #ifdef NKRO_ENABLE +#ifdef BLUETOOTH_ENABLE + if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) || + ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol)) + && keymap_config.nkro) { +#else if (keyboard_protocol && keymap_config.nkro) { +#endif add_key_bit(keyboard_report, key); return; } @@ -256,7 +284,13 @@ void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) { */ void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) { #ifdef NKRO_ENABLE +#ifdef BLUETOOTH_ENABLE + if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) || + ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol)) + && keymap_config.nkro) { +#else if (keyboard_protocol && keymap_config.nkro) { +#endif del_key_bit(keyboard_report, key); return; } @@ -271,7 +305,13 @@ void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) { void clear_keys_from_report(report_keyboard_t* keyboard_report) { // not clear mods #ifdef NKRO_ENABLE +#ifdef BLUETOOTH_ENABLE + if ((((get_transport() == TRANSPORT_USB) && keyboard_protocol) || + ((get_transport() == TRANSPORT_BLUETOOTH) && bluetooth_report_protocol)) + && keymap_config.nkro) { +#else if (keyboard_protocol && keymap_config.nkro) { +#endif memset(keyboard_report->nkro.bits, 0, sizeof(keyboard_report->nkro.bits)); return; }