forked from mirrors/qmk_userspace
Added wireless support; Added Lemokey L3; Added Keychron V1 Max
This commit is contained in:
parent
9539f135d8
commit
4ae5990fcc
31585 changed files with 99327 additions and 1763186 deletions
142
keyboards/keychron/bluetooth/bat_level_animation.c
Normal file
142
keyboards/keychron/bluetooth/bat_level_animation.c
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
|
||||
#include "quantum.h"
|
||||
#include "bluetooth.h"
|
||||
#include "indicator.h"
|
||||
#include "lpm.h"
|
||||
#if defined(PROTOCOL_CHIBIOS)
|
||||
# include <usb_main.h>
|
||||
#elif if defined(PROTOCOL_LUFA)
|
||||
# include "lufa.h"
|
||||
#endif
|
||||
#include "eeprom.h"
|
||||
|
||||
#ifndef BAT_LEVEL_GROWING_INTERVAL
|
||||
# define BAT_LEVEL_GROWING_INTERVAL 150
|
||||
#endif
|
||||
|
||||
#ifndef BAT_LEVEL_ON_INTERVAL
|
||||
# define BAT_LEVEL_ON_INTERVAL 3000
|
||||
#endif
|
||||
|
||||
#ifdef LED_MATRIX_ENABLE
|
||||
# define LED_DRIVER_IS_ENABLED led_matrix_is_enabled
|
||||
#endif
|
||||
|
||||
#ifdef RGB_MATRIX_ENABLE
|
||||
# define LED_DRIVER_IS_ENABLED rgb_matrix_is_enabled
|
||||
#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 = 0;
|
||||
static uint32_t bat_lvl_ani_timer_buffer = 0;
|
||||
static uint8_t bat_percentage;
|
||||
static uint8_t cur_percentage;
|
||||
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
|
||||
uint8_t bat_lvl_led_list[10] = BAT_LEVEL_LED_LIST;
|
||||
|
||||
for (uint8_t i = 0; i <= LED_MATRIX_LED_COUNT; 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
|
||||
uint8_t bat_lvl_led_list[10] = BAT_LEVEL_LED_LIST;
|
||||
|
||||
for (uint8_t i = 0; i <= RGB_MATRIX_LED_COUNT; 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;
|
||||
indicator_eeconfig_reload();
|
||||
if (indicator_config.value == 0 && !LED_DRIVER_IS_ENABLED()) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
23
keyboards/keychron/bluetooth/bat_level_animation.h
Normal file
23
keyboards/keychron/bluetooth/bat_level_animation.h
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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);
|
||||
140
keyboards/keychron/bluetooth/battery.c
Normal file
140
keyboards/keychron/bluetooth/battery.c
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "quantum.h"
|
||||
#include "bluetooth.h"
|
||||
#include "battery.h"
|
||||
#include "transport.h"
|
||||
#include "ckbt51.h"
|
||||
#include "lpm.h"
|
||||
#include "indicator.h"
|
||||
#include "rtc_timer.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;
|
||||
static uint8_t power_on_sample = 0;
|
||||
|
||||
void battery_init(void) {
|
||||
bat_state = BAT_NOT_CHARGING;
|
||||
}
|
||||
__attribute__((weak)) void battery_measure(void) {
|
||||
ckbt51_read_state_reg(0x05, 0x02);
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
indicator_battery_low_enable(true);
|
||||
#endif
|
||||
#if defined(LOW_BAT_IND_INDEX)
|
||||
indicator_battery_low_backlit_enable(true);
|
||||
#endif
|
||||
power_on_sample = VOLTAGE_POWER_ON_MEASURE_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;
|
||||
}
|
||||
}
|
||||
|
||||
bool battery_power_on_sample(void) {
|
||||
return power_on_sample < VOLTAGE_POWER_ON_MEASURE_COUNT;
|
||||
}
|
||||
|
||||
void battery_task(void) {
|
||||
uint32_t t = rtc_timer_elapsed_ms(bat_monitor_timer_buffer);
|
||||
if (get_transport() == TRANSPORT_BLUETOOTH && bluetooth_get_state() == BLUETOOTH_CONNECTED) {
|
||||
if ((battery_power_on_sample()
|
||||
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||
&& !indicator_is_enabled()
|
||||
#endif
|
||||
&& t > BACKLIGHT_OFF_VOLTAGE_MEASURE_INTERVAL) ||
|
||||
t > VOLTAGE_MEASURE_INTERVAL) {
|
||||
|
||||
battery_check_empty();
|
||||
battery_check_critical_low();
|
||||
|
||||
bat_monitor_timer_buffer = rtc_timer_read_ms();
|
||||
if (bat_monitor_timer_buffer > RTC_MAX_TIME) {
|
||||
bat_monitor_timer_buffer = 0;
|
||||
rtc_timer_clear();
|
||||
}
|
||||
|
||||
battery_measure();
|
||||
power_on_sample++;
|
||||
if (power_on_sample > VOLTAGE_POWER_ON_MEASURE_COUNT) power_on_sample = VOLTAGE_POWER_ON_MEASURE_COUNT;
|
||||
}
|
||||
}
|
||||
|
||||
if ((bat_empty || critical_low) && usb_power_connected()) {
|
||||
bat_empty = false;
|
||||
critical_low = false;
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
indicator_battery_low_enable(false);
|
||||
#endif
|
||||
#if defined(LOW_BAT_IND_INDEX)
|
||||
indicator_battery_low_backlit_enable(false);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
60
keyboards/keychron/bluetooth/battery.h
Normal file
60
keyboards/keychron/bluetooth/battery.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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
|
||||
|
||||
#ifndef VOLTAGE_POWER_ON_MEASURE_COUNT
|
||||
# define VOLTAGE_POWER_ON_MEASURE_COUNT 15
|
||||
#endif
|
||||
|
||||
#ifndef BACKLIGHT_OFF_VOLTAGE_MEASURE_INTERVAL
|
||||
# define BACKLIGHT_OFF_VOLTAGE_MEASURE_INTERVAL 200
|
||||
#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);
|
||||
bool battery_power_on_sample(void);
|
||||
|
||||
void battery_task(void);
|
||||
484
keyboards/keychron/bluetooth/bluetooth.c
Normal file
484
keyboards/keychron/bluetooth/bluetooth.c
Normal file
|
|
@ -0,0 +1,484 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "action.h"
|
||||
#include "quantum.h"
|
||||
#include "bluetooth.h"
|
||||
#include "report_buffer.h"
|
||||
#include "lpm.h"
|
||||
#include "battery.h"
|
||||
#include "indicator.h"
|
||||
#include "transport.h"
|
||||
#include "rtc_timer.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;
|
||||
|
||||
#ifdef NKRO_ENABLE
|
||||
extern nkro_t nkro;
|
||||
#endif
|
||||
|
||||
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_nkro(report_nkro_t *report);
|
||||
void bluetooth_send_mouse(report_mouse_t *report);
|
||||
void bluetooth_send_extra(report_extra_t *report);
|
||||
|
||||
/* host struct */
|
||||
host_driver_t bluetooth_driver = {bluetooth_keyboard_leds, bluetooth_send_keyboard, bluetooth_send_nkro, bluetooth_send_mouse, bluetooth_send_extra};
|
||||
|
||||
#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();
|
||||
rtc_timer_init();
|
||||
|
||||
#ifdef BLUETOOTH_NKRO_ENABLE
|
||||
keymap_config.raw = eeconfig_read_keymap();
|
||||
nkro.bluetooth = keymap_config.nkro;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 = nkro.bluetooth;
|
||||
#else
|
||||
keymap_config.nkro = false;
|
||||
#endif
|
||||
|
||||
bluetooth_enter_connected_kb(host_idx);
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
if (battery_is_empty()) {
|
||||
indicator_battery_low_enable(true);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* 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);
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
indicator_battery_low_enable(false);
|
||||
#endif
|
||||
#if defined(LOW_BAT_IND_INDEX)
|
||||
indicator_battery_low_backlit_enable(false);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* 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 (bluetooth_transport.send_keyboard) {
|
||||
#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_KB;
|
||||
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_keyboard(&report->nkro.mods);
|
||||
#endif
|
||||
}
|
||||
} else if (bt_state != BLUETOOTH_RESET) {
|
||||
bluetooth_connect();
|
||||
}
|
||||
}
|
||||
void bluetooth_send_nkro(report_nkro_t *report) {
|
||||
if (bt_state == BLUETOOTH_PARING && !pincodeEntry) return;
|
||||
|
||||
if (bt_state == BLUETOOTH_CONNECTED || (bt_state == BLUETOOTH_PARING && pincodeEntry)) {
|
||||
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_NKRO;
|
||||
memcpy(&report_buffer.nkro, report, sizeof(report_nkro_t));
|
||||
report_buffer_enqueue(&report_buffer);
|
||||
}
|
||||
#else
|
||||
bluetooth_transport.send_nkro(&report->mods);
|
||||
#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_send_extra(report_extra_t *report) {
|
||||
if (report->report_id == REPORT_ID_SYSTEM) {
|
||||
bluetooth_send_system(report->usage);
|
||||
} else if (report->report_id == REPORT_ID_CONSUMER) {
|
||||
bluetooth_send_consumer(report->usage);
|
||||
}
|
||||
}
|
||||
|
||||
void bluetooth_low_battery_shutdown(void) {
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
indicator_battery_low_enable(false);
|
||||
#endif
|
||||
#if defined(LOW_BAT_IND_INDEX)
|
||||
indicator_battery_low_backlit_enable(false);
|
||||
#endif
|
||||
clear_keyboard();
|
||||
send_keyboard_report();
|
||||
wait_ms(50);
|
||||
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_BT_PINCODE_ENTRY:
|
||||
bluetooth_enter_pin_code_entry();
|
||||
break;
|
||||
case EVT_EXIT_BT_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;
|
||||
};
|
||||
|
||||
__attribute__((weak)) bool process_record_kb_bt(uint16_t keycode, keyrecord_t *record) {
|
||||
return true;
|
||||
};
|
||||
|
||||
bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
|
||||
if (!process_record_user(keycode, record)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (get_transport() == TRANSPORT_BLUETOOTH) {
|
||||
lpm_timer_reset();
|
||||
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(LOW_BAT_IND_INDEX)
|
||||
if (battery_is_empty() && bluetooth_get_state() == BLUETOOTH_CONNECTED && record->event.pressed) {
|
||||
# if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
indicator_battery_low_enable(true);
|
||||
# endif
|
||||
# if defined(LOW_BAT_IND_INDEX)
|
||||
indicator_battery_low_backlit_enable(true);
|
||||
# endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return process_record_kb_bt(keycode, record);
|
||||
// return process_record_user(keycode, record);
|
||||
}
|
||||
87
keyboards/keychron/bluetooth/bluetooth.h
Normal file
87
keyboards/keychron/bluetooth/bluetooth.h
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "bluetooth_event_type.h"
|
||||
#include "action.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);
|
||||
|
||||
bool process_record_kb_bt(uint16_t keycode, keyrecord_t *record);
|
||||
23
keyboards/keychron/bluetooth/bluetooth.mk
Normal file
23
keyboards/keychron/bluetooth/bluetooth.mk
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
|
||||
OPT_DEFS += -DKC_BLUETOOTH_ENABLE
|
||||
|
||||
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 \
|
||||
$(BLUETOOTH_DIR)/rtc_timer.c
|
||||
|
||||
VPATH += $(TOP_DIR)/keyboards/keychron/$(BLUETOOTH_DIR)
|
||||
|
||||
# Work around RTC clock issue without touching chibios, refer to the link for this bug
|
||||
# https://forum.chibios.org/viewtopic.php?f=35&t=6197
|
||||
OPT_DEFS += -DRCC_APBENR1_RTCAPBEN
|
||||
38
keyboards/keychron/bluetooth/bluetooth_config.h
Normal file
38
keyboards/keychron/bluetooth/bluetooth_config.h
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef BLUETOOTH_CONFIG_H
|
||||
#define BLUETOOTH_CONFIG_H
|
||||
|
||||
#include "config.h"
|
||||
|
||||
//
|
||||
#ifndef HOST_DEVICES_COUNT
|
||||
# define HOST_DEVICES_COUNT 3
|
||||
#endif
|
||||
|
||||
// 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
|
||||
|
||||
44
keyboards/keychron/bluetooth/bluetooth_event_type.h
Normal file
44
keyboards/keychron/bluetooth/bluetooth_event_type.h
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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_BT_PINCODE_ENTRY,
|
||||
EVT_EXIT_BT_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;
|
||||
|
||||
37
keyboards/keychron/bluetooth/bluetooth_main.c
Normal file
37
keyboards/keychron/bluetooth/bluetooth_main.c
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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();
|
||||
}
|
||||
606
keyboards/keychron/bluetooth/ckbt51.c
Normal file
606
keyboards/keychron/bluetooth/ckbt51.c
Normal file
|
|
@ -0,0 +1,606 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "quantum.h"
|
||||
#include "ckbt51.h"
|
||||
#include "bluetooth.h"
|
||||
#include "battery.h"
|
||||
#include "raw_hid.h"
|
||||
#include "report_buffer.h"
|
||||
|
||||
#ifndef RAW_EPSIZE
|
||||
# define RAW_EPSIZE 32
|
||||
#endif
|
||||
|
||||
#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,
|
||||
CKBT51_CMD_RADIO_TEST = 0x73,
|
||||
/* 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, ckbt51_send_mouse, 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(&WT_DRIVER, &config);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sdStart(&WT_DRIVER, &config);
|
||||
palSetPadMode(WT_DRIVER_UART_TX_BANK, WT_DRIVER_UART_TX, PAL_MODE_ALTERNATE(WT_DRIVER_UART_TX_PAL_MODE));
|
||||
palSetPadMode(WT_DRIVER_UART_RX_BANK, WT_DRIVER_UART_RX, PAL_MODE_ALTERNATE(WT_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, bool retry) {
|
||||
static uint8_t sn = 0;
|
||||
uint8_t i;
|
||||
uint8_t pkt[PACKET_MAX_LEN] = {0};
|
||||
memset(pkt, 0, PACKET_MAX_LEN);
|
||||
|
||||
if (!retry) ++sn;
|
||||
if (sn == 0) ++sn;
|
||||
|
||||
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(&WT_DRIVER, pkt, i);
|
||||
}
|
||||
|
||||
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, false);
|
||||
}
|
||||
|
||||
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, false);
|
||||
}
|
||||
|
||||
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, false);
|
||||
}
|
||||
|
||||
void ckbt51_send_system(uint16_t report) {
|
||||
/* CKBT51 supports only System Sleep */
|
||||
if ((report & 0xFF) != 0x82) return;
|
||||
|
||||
uint8_t i = 0;
|
||||
memset(payload, 0, PACKET_MAX_LEN);
|
||||
|
||||
payload[i++] = CKBT51_CMD_SEND_SYSTEM;
|
||||
payload[i++] = 0x01 << ((report & 0xFF) - 0x82);
|
||||
|
||||
ckbt51_send_cmd(payload, i, true, false);
|
||||
}
|
||||
|
||||
void ckbt51_send_mouse(uint8_t* report) {
|
||||
uint8_t i = 0;
|
||||
memset(payload, 0, PACKET_MAX_LEN);
|
||||
|
||||
payload[i++] = CKBT51_CMD_SEND_MOUSE; // Cmd type
|
||||
payload[i++] = report[1]; // Button
|
||||
payload[i++] = report[2]; // X
|
||||
payload[i++] = (report[2] & 0x80) ? 0xff : 0x00; // ckbt51 use 16bit report, set high byte
|
||||
payload[i++] = report[3]; // Y
|
||||
payload[i++] = (report[3] & 0x80) ? 0xff : 0x00; // ckbt51 use 16bit report, set high byte
|
||||
payload[i++] = report[4]; // V wheel
|
||||
payload[i++] = report[5]; // H wheel
|
||||
|
||||
ckbt51_send_cmd(payload, i, false, false);
|
||||
}
|
||||
|
||||
/* 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, 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, false);
|
||||
}
|
||||
|
||||
/* 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, false);
|
||||
}
|
||||
|
||||
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, false);
|
||||
}
|
||||
|
||||
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, false);
|
||||
}
|
||||
|
||||
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, 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, 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, true, 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, 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, true, false);
|
||||
}
|
||||
|
||||
void ckbt51_get_local_name(void) {
|
||||
uint8_t i = 0;
|
||||
memset(payload, 0, PACKET_MAX_LEN);
|
||||
|
||||
payload[i++] = CKBT51_CMD_GET_NAME;
|
||||
|
||||
ckbt51_send_cmd(payload, i, false, 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, 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, false);
|
||||
}
|
||||
|
||||
void ckbt51_radio_test(uint8_t channel) {
|
||||
uint8_t i = 0;
|
||||
memset(payload, 0, PACKET_MAX_LEN);
|
||||
payload[i++] = CKBT51_CMD_RADIO_TEST;
|
||||
payload[i++] = channel;
|
||||
payload[i++] = 0;
|
||||
|
||||
ckbt51_send_cmd(payload, i, false, 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;
|
||||
static uint8_t sn = 0;
|
||||
|
||||
bool retry = true;
|
||||
if (sn != data[4]) {
|
||||
sn = data[4];
|
||||
retry = false;
|
||||
}
|
||||
|
||||
if ((payload[0] & 0xF0) == 0x60) {
|
||||
ckbt51_send_cmd(payload, payload_len - 2, data[1] == 0x56, retry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((weak)) void ckbt51_default_ack_handler(uint8_t* data, uint8_t len){};
|
||||
|
||||
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:
|
||||
ckbt51_default_ack_handler(data, len);
|
||||
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_BT_PINCODE_ENTRY;
|
||||
break;
|
||||
case CKBT51_EXIT_PINCODE_ENTRY:
|
||||
event.evt_type = EVT_EXIT_BT_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 && WT_DRIVER.iqueue.q_counter >= PACKECT_HEADER_LEN) {
|
||||
uint8_t buf[32] = {0};
|
||||
|
||||
if (wait_for_new_pkt) {
|
||||
if (sdGet(&WT_DRIVER) == 0xAA && sdGet(&WT_DRIVER) == 0x57) {
|
||||
for (uint8_t i = 0; i < 3; i++) {
|
||||
buf[i] = sdGet(&WT_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 && WT_DRIVER.iqueue.q_counter >= len) {
|
||||
uint8_t buf[32] = {0};
|
||||
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
buf[i] = sdGetTimeout(&WT_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 - 3, sn);
|
||||
} else {
|
||||
// TODO: Error handle
|
||||
}
|
||||
}
|
||||
}
|
||||
157
keyboards/keychron/bluetooth/ckbt51.h
Normal file
157
keyboards/keychron/bluetooth/ckbt51.h
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
#ifdef WT_DRIVER_UART_BANK
|
||||
# define WT_DRIVER_UART_TX_BANK WT_DRIVER_UART_BANK
|
||||
# define WT_DRIVER_UART_RX_BANK WT_DRIVER_UART_BANK
|
||||
#endif
|
||||
|
||||
#ifndef WT_DRIVER_UART_TX_BANK
|
||||
# define WT_DRIVER_UART_TX_BANK GPIOA
|
||||
#endif
|
||||
|
||||
#ifndef WT_DRIVER_UART_RX_BANK
|
||||
# define WT_DRIVER_UART_RX_BANK GPIOA
|
||||
#endif
|
||||
|
||||
#ifndef WT_DRIVER_UART_TX
|
||||
# define WT_DRIVER_UART_TX 2
|
||||
#endif
|
||||
|
||||
#ifndef WT_DRIVER_UART_RX
|
||||
# define WT_DRIVER_UART_RX 3
|
||||
#endif
|
||||
|
||||
#ifndef WT_DRIVER
|
||||
# define WT_DRIVER SD2
|
||||
#endif
|
||||
|
||||
#ifdef USE_GPIOV1
|
||||
# ifndef WT_DRIVER_UART_TX_PAL_MODE
|
||||
# define WT_DRIVER_UART_TX_PAL_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL
|
||||
# endif
|
||||
# ifndef WT_DRIVER_UART_RX_PAL_MODE
|
||||
# define WT_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 WT_DRIVER_UART_TX_PAL_MODE
|
||||
# define WT_DRIVER_UART_TX_PAL_MODE 7
|
||||
# endif
|
||||
# ifndef WT_DRIVER_UART_RX_PAL_MODE
|
||||
# define WT_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, bool retry);
|
||||
|
||||
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_send_mouse(uint8_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(void);
|
||||
|
||||
void ckbt51_factory_reset(void);
|
||||
void ckbt51_int_pin_test(bool enable);
|
||||
void ckbt51_dfu_rx(uint8_t* data, uint8_t length);
|
||||
void ckbt51_radio_test(uint8_t channel);
|
||||
|
||||
void ckbt51_task(void);
|
||||
|
||||
343
keyboards/keychron/bluetooth/factory_test.c
Normal file
343
keyboards/keychron/bluetooth/factory_test.c
Normal file
|
|
@ -0,0 +1,343 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "quantum.h"
|
||||
#include "raw_hid.h"
|
||||
#ifdef KC_BLUETOOTH_ENABLE
|
||||
# include "transport.h"
|
||||
# include "ckbt51.h"
|
||||
#endif
|
||||
|
||||
#ifndef RAW_EPSIZE
|
||||
# define RAW_EPSIZE 32
|
||||
#endif
|
||||
|
||||
#ifndef BL_TEST_KEY1
|
||||
# define BL_TEST_KEY1 KC_RIGHT
|
||||
#endif
|
||||
|
||||
#ifndef BL_TEST_KEY2
|
||||
# define BL_TEST_KEY2 KC_HOME
|
||||
#endif
|
||||
|
||||
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_BL_KEY1 = 0x01 << 3,
|
||||
KEY_PRESS_BL_KEY2 = 0x01 << 4,
|
||||
KEY_PRESS_FACTORY_RESET = KEY_PRESS_FN | KEY_PRESS_J | KEY_PRESS_Z,
|
||||
KEY_PRESS_BACKLIGTH_TEST = KEY_PRESS_FN | KEY_PRESS_BL_KEY1 | KEY_PRESS_BL_KEY2,
|
||||
};
|
||||
|
||||
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,
|
||||
FACTORY_TEST_CMD_RADIO_CARRIER,
|
||||
};
|
||||
|
||||
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 = timer_read32() == 0 ? 1 : timer_read32();
|
||||
}
|
||||
|
||||
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 = timer_read32() == 0 ? 1 : timer_read32();
|
||||
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 KC_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 && 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 = timer_read32() == 0 ? 1 : timer_read32();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void process_record_factory_reset(uint16_t keycode, keyrecord_t *record) {
|
||||
switch (keycode) {
|
||||
#if defined(FN_KEY1) || defined(FN_KEY2)
|
||||
# ifdef FN_KEY1
|
||||
case FN_KEY1: /* fall through */
|
||||
# endif
|
||||
# ifdef FN_KEY2
|
||||
case FN_KEY2:
|
||||
# endif
|
||||
if (record->event.pressed) {
|
||||
factory_reset_state |= KEY_PRESS_FN;
|
||||
} else {
|
||||
factory_reset_state &= ~KEY_PRESS_FN;
|
||||
factory_reset_timer = 0;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
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;
|
||||
#ifdef BL_TEST_KEY1
|
||||
case BL_TEST_KEY1:
|
||||
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_BL_KEY1;
|
||||
if (factory_reset_state == 0x19) factory_timer_start();
|
||||
}
|
||||
} else {
|
||||
factory_reset_state &= ~KEY_PRESS_BL_KEY1;
|
||||
factory_reset_timer = 0;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#ifdef BL_TEST_KEY2
|
||||
case BL_TEST_KEY2:
|
||||
if (record->event.pressed) {
|
||||
if (backlight_test_mode) {
|
||||
backlight_test_mode = BACKLIGHT_TEST_OFF;
|
||||
} else {
|
||||
factory_reset_state |= KEY_PRESS_BL_KEY2;
|
||||
if (factory_reset_state == 0x19) factory_timer_start();
|
||||
}
|
||||
} else {
|
||||
factory_reset_state &= ~KEY_PRESS_BL_KEY2;
|
||||
factory_reset_timer = 0;
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef LED_MATRIX_ENABLE
|
||||
bool led_matrix_indicators_user(void) {
|
||||
if (factory_reset_ind_state) {
|
||||
led_matrix_set_value_all(factory_reset_ind_state % 2 ? 0 : 255);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef RGB_MATRIX_ENABLE
|
||||
bool rgb_matrix_indicators_user(void) {
|
||||
if (factory_reset_ind_state) {
|
||||
backlight_test_mode = BACKLIGHT_TEST_OFF;
|
||||
rgb_matrix_set_color_all(factory_reset_ind_state % 2 ? 0 : 255, 0, 0);
|
||||
} else if (backlight_test_mode) {
|
||||
switch (backlight_test_mode) {
|
||||
case BACKLIGHT_TEST_WHITE:
|
||||
rgb_matrix_set_color_all(255, 255, 255);
|
||||
break;
|
||||
case BACKLIGHT_TEST_RED:
|
||||
rgb_matrix_set_color_all(255, 0, 0);
|
||||
break;
|
||||
case BACKLIGHT_TEST_GREEN:
|
||||
rgb_matrix_set_color_all(0, 255, 0);
|
||||
break;
|
||||
case BACKLIGHT_TEST_BLUE:
|
||||
rgb_matrix_set_color_all(0, 0, 255);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#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;
|
||||
|
||||
#ifdef KC_BLUETOOTH_ENABLE
|
||||
uint8_t payload[32];
|
||||
uint8_t len = 0;
|
||||
#endif
|
||||
|
||||
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;
|
||||
#ifdef KC_BLUETOOTH_ENABLE
|
||||
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;
|
||||
#endif
|
||||
#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
|
||||
case FACTORY_TEST_CMD_RADIO_CARRIER:
|
||||
if (data[2] < 79) ckbt51_radio_test(data[2]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool dip_switch_update_user(uint8_t index, bool active) {
|
||||
if (report_os_sw_state) {
|
||||
#ifdef INVERT_OS_SWITCH_STATE
|
||||
active = !active;
|
||||
#endif
|
||||
uint8_t payload[3] = {FACTORY_TEST_CMD_OS_SWITCH, OS_SWITCH, active};
|
||||
factory_test_send(payload, 3);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
24
keyboards/keychron/bluetooth/factory_test.h
Normal file
24
keyboards/keychron/bluetooth/factory_test.h
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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);
|
||||
607
keyboards/keychron/bluetooth/indicator.c
Normal file
607
keyboards/keychron/bluetooth/indicator.c
Normal file
|
|
@ -0,0 +1,607 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "quantum.h"
|
||||
#include "indicator.h"
|
||||
#include "transport.h"
|
||||
#include "battery.h"
|
||||
#include "eeconfig.h"
|
||||
#include "bluetooth_config.h"
|
||||
#include "config.h"
|
||||
#include "rtc_timer.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"
|
||||
# include "eeprom.h"
|
||||
#endif
|
||||
|
||||
#ifdef LED_MATRIX_ENABLE
|
||||
# define DECIDE_TIME(t, duration) (duration == 0 ? LED_MATRIX_TIMEOUT_INFINITE : ((t > duration) ? t : duration))
|
||||
#endif
|
||||
#ifdef RGB_MATRIX_ENABLE
|
||||
# define DECIDE_TIME(t, duration) (duration == 0 ? RGB_MATRIX_TIMEOUT_INFINITE : ((t > duration) ? t : duration))
|
||||
#endif
|
||||
|
||||
#define LED_ON 0x80
|
||||
#define INDICATOR_SET(s) memcpy(&indicator_config, &s##_config, sizeof(indicator_config_t));
|
||||
|
||||
enum {
|
||||
BACKLIGHT_OFF = 0x00,
|
||||
BACKLIGHT_ON_CONNECTED = 0x01,
|
||||
BACKLIGHT_ON_UNCONNECTED = 0x02,
|
||||
};
|
||||
|
||||
static indicator_config_t pairing_config = INDICATOR_CONFIG_PARING;
|
||||
static indicator_config_t connected_config = INDICATOR_CONFIG_CONNECTD;
|
||||
static indicator_config_t reconnecting_config = INDICATOR_CONFIG_RECONNECTING;
|
||||
static indicator_config_t disconnected_config = INDICATOR_CONFIG_DISCONNECTED;
|
||||
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 = 0;
|
||||
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
static uint32_t bat_low_pin_indicator = 0;
|
||||
static uint32_t bat_low_blink_duration = 0;
|
||||
# ifdef BAT_LOW_LED_PIN_STATE
|
||||
bool bat_low_led_pin_state = false;
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(LOW_BAT_IND_INDEX)
|
||||
static uint32_t bat_low_backlit_indicator = 0;
|
||||
static uint8_t bat_low_ind_state = 0;
|
||||
static uint32_t rtc_time = 0;
|
||||
#endif
|
||||
|
||||
#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
|
||||
|
||||
#ifdef LED_MATRIX_ENABLE
|
||||
# define LED_DRIVER led_matrix_driver
|
||||
# define LED_INDICATORS_KB led_matrix_indicators_kb
|
||||
# define LED_INDICATORS_USER led_matrix_indicators_user
|
||||
# define LED_NONE_INDICATORS_KB led_matrix_none_indicators_kb
|
||||
# define SET_ALL_LED_OFF() led_matrix_set_value_all(0)
|
||||
# define SET_LED_OFF(idx) led_matrix_set_value(idx, 0)
|
||||
# define SET_LED_ON(idx) led_matrix_set_value(idx, 255)
|
||||
# define SET_LED_BT(idx) led_matrix_set_value(idx, 255)
|
||||
# define SET_LED_LOW_BAT(idx) led_matrix_set_value(idx, 255)
|
||||
# define LED_DRIVER_IS_ENABLED led_matrix_is_enabled
|
||||
# define LED_DRIVER_EECONFIG_RELOAD() \
|
||||
eeprom_read_block(&led_matrix_eeconfig, EECONFIG_LED_MATRIX, sizeof(led_matrix_eeconfig)); \
|
||||
if (!led_matrix_eeconfig.mode) { \
|
||||
eeconfig_update_led_matrix_default(); \
|
||||
}
|
||||
# define LED_DRIVER_ALLOW_SHUTDOWN led_matrix_driver_allow_shutdown
|
||||
# define LED_DRIVER_ENABLE_NOEEPROM led_matrix_enable_noeeprom
|
||||
# define LED_DRIVER_DISABLE_NOEEPROM led_matrix_disable_noeeprom
|
||||
# define LED_DRIVER_DISABLE_TIMEOUT_SET led_matrix_disable_timeout_set
|
||||
# define LED_DRIVER_DISABLE_TIME_RESET led_matrix_disable_time_reset
|
||||
#endif
|
||||
|
||||
#ifdef RGB_MATRIX_ENABLE
|
||||
# define LED_DRIVER rgb_matrix_driver
|
||||
# define LED_INDICATORS_KB rgb_matrix_indicators_kb
|
||||
# define LED_INDICATORS_USER rgb_matrix_indicators_user
|
||||
# define LED_NONE_INDICATORS_KB rgb_matrix_none_indicators_kb
|
||||
# define SET_ALL_LED_OFF() rgb_matrix_set_color_all(0, 0, 0)
|
||||
# define SET_LED_OFF(idx) rgb_matrix_set_color(idx, 0, 0, 0)
|
||||
# define SET_LED_ON(idx) rgb_matrix_set_color(idx, 255, 255, 255)
|
||||
# define SET_LED_BT(idx) rgb_matrix_set_color(idx, 0, 0, 255)
|
||||
# define SET_LED_LOW_BAT(idx) rgb_matrix_set_color(idx, 255, 0, 0)
|
||||
# define LED_DRIVER_IS_ENABLED rgb_matrix_is_enabled
|
||||
# define LED_DRIVER_EECONFIG_RELOAD() \
|
||||
eeprom_read_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); \
|
||||
if (!rgb_matrix_config.mode) { \
|
||||
eeconfig_update_rgb_matrix_default(); \
|
||||
}
|
||||
# define LED_DRIVER_ALLOW_SHUTDOWN rgb_matrix_driver_allow_shutdown
|
||||
# define LED_DRIVER_ENABLE_NOEEPROM rgb_matrix_enable_noeeprom
|
||||
# define LED_DRIVER_DISABLE_NOEEPROM rgb_matrix_disable_noeeprom
|
||||
# define LED_DRIVER_DISABLE_TIMEOUT_SET rgb_matrix_disable_timeout_set
|
||||
# define LED_DRIVER_DISABLE_TIME_RESET rgb_matrix_disable_time_reset
|
||||
#endif
|
||||
void indicator_init(void) {
|
||||
memset(&indicator_config, 0, sizeof(indicator_config));
|
||||
|
||||
#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) {
|
||||
if (!LED_DRIVER_IS_ENABLED()) {
|
||||
LED_DRIVER_ENABLE_NOEEPROM();
|
||||
}
|
||||
}
|
||||
|
||||
inline void indicator_disable(void) {
|
||||
LED_DRIVER_DISABLE_NOEEPROM();
|
||||
}
|
||||
|
||||
void indicator_set_backlit_timeout(uint32_t time) {
|
||||
LED_DRIVER_DISABLE_TIMEOUT_SET(time);
|
||||
}
|
||||
|
||||
static inline void indicator_reset_backlit_time(void) {
|
||||
LED_DRIVER_DISABLE_TIME_RESET();
|
||||
}
|
||||
|
||||
bool indicator_is_enabled(void) {
|
||||
return LED_DRIVER_IS_ENABLED();
|
||||
}
|
||||
|
||||
void indicator_eeconfig_reload(void) {
|
||||
LED_DRIVER_EECONFIG_RELOAD();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
bool indicator_is_running(void) {
|
||||
return
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
bat_low_blink_duration ||
|
||||
#endif
|
||||
#if defined(LOW_BAT_IND_INDEX)
|
||||
bat_low_ind_state ||
|
||||
#endif
|
||||
!!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;
|
||||
}
|
||||
|
||||
#ifdef HOST_LED_PIN_LIST
|
||||
if (indicator_config.value) {
|
||||
uint8_t idx = (indicator_config.value & 0x0F) - 1;
|
||||
|
||||
if (idx < HOST_DEVICES_COUNT) {
|
||||
if ((indicator_config.value & 0x80) && !time_up) {
|
||||
writePin(host_led_pin_list[idx], HOST_LED_PIN_ON_STATE);
|
||||
} else {
|
||||
writePin(host_led_pin_list[idx], !HOST_LED_PIN_ON_STATE);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
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;
|
||||
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||
LED_INDICATORS_KB();
|
||||
#endif
|
||||
indicator_config.value = 0;
|
||||
}
|
||||
|
||||
if (indicator_config.value == 0) {
|
||||
indicator_eeconfig_reload();
|
||||
if (!LED_DRIVER_IS_ENABLED()) 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:
|
||||
#ifdef HOST_LED_PIN_LIST
|
||||
writePin(host_led_pin_list[host_index - 1], !HOST_LED_PIN_ON_STATE);
|
||||
#endif
|
||||
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;
|
||||
indicator_eeconfig_reload();
|
||||
|
||||
if (indicator_is_enabled()) {
|
||||
indicator_enable();
|
||||
} else {
|
||||
indicator_disable();
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
void indicator_battery_low_enable(bool enable) {
|
||||
if (enable) {
|
||||
if (bat_low_blink_duration == 0) {
|
||||
bat_low_blink_duration = bat_low_pin_indicator = sync_timer_read32() | 1;
|
||||
} else
|
||||
bat_low_blink_duration = sync_timer_read32() | 1;
|
||||
} else {
|
||||
# if defined(BAT_LOW_LED_PIN)
|
||||
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
|
||||
# else
|
||||
bat_low_led_pin_state = false;
|
||||
# endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(LOW_BAT_IND_INDEX)
|
||||
void indicator_battery_low_backlit_enable(bool enable) {
|
||||
if (enable) {
|
||||
uint32_t t = rtc_timer_read_ms();
|
||||
/* Check overflow */
|
||||
if (rtc_time > t) {
|
||||
if (bat_low_ind_state == 0)
|
||||
rtc_time = t; // Update rtc_time if indicating is not running
|
||||
else {
|
||||
rtc_time += t;
|
||||
}
|
||||
}
|
||||
/* Indicating at first time or after the interval */
|
||||
if ((rtc_time == 0 || t - rtc_time > LOW_BAT_LED_TRIG_INTERVAL) && bat_low_ind_state == 0) {
|
||||
bat_low_backlit_indicator = enable ? (timer_read32() == 0 ? 1 : timer_read32()) : 0;
|
||||
rtc_time = rtc_timer_read_ms();
|
||||
bat_low_ind_state = 1;
|
||||
|
||||
indicator_enable();
|
||||
}
|
||||
} else {
|
||||
rtc_time = 0;
|
||||
bat_low_ind_state = 0;
|
||||
|
||||
indicator_eeconfig_reload();
|
||||
if (!LED_DRIVER_IS_ENABLED()) indicator_disable();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void indicator_battery_low(void) {
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
if (bat_low_pin_indicator && sync_timer_elapsed32(bat_low_pin_indicator) > (LOW_BAT_LED_BLINK_PERIOD)) {
|
||||
# if defined(BAT_LOW_LED_PIN)
|
||||
togglePin(BAT_LOW_LED_PIN);
|
||||
# else
|
||||
bat_low_led_pin_state = !bat_low_led_pin_state;
|
||||
# endif
|
||||
bat_low_pin_indicator = sync_timer_read32() | 1;
|
||||
// Turn off low battery indication if we reach the duration
|
||||
# if defined(BAT_LOW_LED_PIN)
|
||||
if (sync_timer_elapsed32(bat_low_blink_duration) > LOW_BAT_LED_BLINK_DURATION && palReadLine(BAT_LOW_LED_PIN) != BAT_LOW_LED_PIN_ON_STATE) {
|
||||
# elif defined(BAT_LOW_LED_PIN_STATE)
|
||||
if (sync_timer_elapsed32(bat_low_blink_duration) > LOW_BAT_LED_BLINK_DURATION) {
|
||||
# endif
|
||||
bat_low_blink_duration = bat_low_pin_indicator = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if defined(LOW_BAT_IND_INDEX)
|
||||
if (bat_low_ind_state) {
|
||||
if ((bat_low_ind_state & 0x0F) <= (LOW_BAT_LED_BLINK_TIMES) && sync_timer_elapsed32(bat_low_backlit_indicator) > (LOW_BAT_LED_BLINK_PERIOD)) {
|
||||
if (bat_low_ind_state & 0x80) {
|
||||
bat_low_ind_state &= 0x7F;
|
||||
bat_low_ind_state++;
|
||||
} else {
|
||||
bat_low_ind_state |= 0x80;
|
||||
}
|
||||
|
||||
bat_low_backlit_indicator = sync_timer_read32() == 0 ? 1 : sync_timer_read32();
|
||||
|
||||
/* Restore backligth state */
|
||||
if ((bat_low_ind_state & 0x0F) > (LOW_BAT_LED_BLINK_TIMES)) {
|
||||
# if defined(NUM_LOCK_INDEX) || defined(CAPS_LOCK_INDEX) || defined(SCROLL_LOCK_INDEX) || defined(COMPOSE_LOCK_INDEX) || defined(KANA_LOCK_INDEX)
|
||||
if (LED_DRIVER_ALLOW_SHUTDOWN())
|
||||
# endif
|
||||
indicator_disable();
|
||||
}
|
||||
} else if ((bat_low_ind_state & 0x0F) > (LOW_BAT_LED_BLINK_TIMES)) {
|
||||
bat_low_ind_state = 0;
|
||||
}
|
||||
}
|
||||
#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();
|
||||
}
|
||||
|
||||
indicator_battery_low();
|
||||
}
|
||||
|
||||
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
|
||||
__attribute__((weak)) void os_state_indicate(void) {
|
||||
# if defined(NUM_LOCK_INDEX)
|
||||
if (host_keyboard_led_state().num_lock) {
|
||||
SET_LED_ON(NUM_LOCK_INDEX);
|
||||
}
|
||||
# endif
|
||||
# if defined(CAPS_LOCK_INDEX)
|
||||
if (host_keyboard_led_state().caps_lock) {
|
||||
# if defined(DIM_CAPS_LOCK)
|
||||
SET_LED_OFF(CAPS_LOCK_INDEX);
|
||||
# else
|
||||
SET_LED_ON(CAPS_LOCK_INDEX);
|
||||
# endif
|
||||
}
|
||||
# endif
|
||||
# if defined(SCROLL_LOCK_INDEX)
|
||||
if (host_keyboard_led_state().scroll_lock) {
|
||||
SET_LED_ON(SCROLL_LOCK_INDEX);
|
||||
}
|
||||
# endif
|
||||
# if defined(COMPOSE_LOCK_INDEX)
|
||||
if (host_keyboard_led_state().compose) {
|
||||
SET_LED_ON(COMPOSE_LOCK_INDEX);
|
||||
}
|
||||
# endif
|
||||
# if defined(KANA_LOCK_INDEX)
|
||||
if (host_keyboard_led_state().kana) {
|
||||
SET_LED_ON(KANA_LOCK_INDEX);
|
||||
}
|
||||
# endif
|
||||
}
|
||||
|
||||
bool LED_INDICATORS_KB(void) {
|
||||
if (!LED_INDICATORS_USER()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (get_transport() == TRANSPORT_BLUETOOTH) {
|
||||
/* Prevent backlight flash caused by key activities */
|
||||
if (battery_is_critical_low()) {
|
||||
SET_ALL_LED_OFF();
|
||||
return false;
|
||||
}
|
||||
|
||||
# if (defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)) && defined(LOW_BAT_IND_INDEX)
|
||||
if (battery_is_empty()) SET_ALL_LED_OFF();
|
||||
if (bat_low_ind_state && (bat_low_ind_state & 0x0F) <= LOW_BAT_LED_BLINK_TIMES) {
|
||||
if (bat_low_ind_state & 0x80)
|
||||
SET_LED_LOW_BAT(LOW_BAT_IND_INDEX);
|
||||
else
|
||||
SET_LED_OFF(LOW_BAT_IND_INDEX);
|
||||
}
|
||||
# endif
|
||||
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) {
|
||||
SET_ALL_LED_OFF();
|
||||
} else if (last_host_index != host_index) {
|
||||
SET_LED_OFF(host_led_matrix_list[last_host_index - 1]);
|
||||
last_host_index = host_index;
|
||||
}
|
||||
|
||||
if (indicator_config.value & 0x80) {
|
||||
SET_LED_BT(host_led_matrix_list[host_index - 1]);
|
||||
} else {
|
||||
SET_LED_OFF(host_led_matrix_list[host_index - 1]);
|
||||
}
|
||||
} else
|
||||
os_state_indicate();
|
||||
|
||||
} else
|
||||
os_state_indicate();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool led_update_kb(led_t led_state) {
|
||||
bool res = led_update_user(led_state);
|
||||
if (res) {
|
||||
led_update_ports(led_state);
|
||||
|
||||
if (!LED_DRIVER_IS_ENABLED()) {
|
||||
# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE)
|
||||
LED_DRIVER.exit_shutdown();
|
||||
# endif
|
||||
SET_ALL_LED_OFF();
|
||||
os_state_indicate();
|
||||
LED_DRIVER.flush();
|
||||
# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE)
|
||||
if (LED_DRIVER_ALLOW_SHUTDOWN()) LED_DRIVER.shutdown();
|
||||
# endif
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void LED_NONE_INDICATORS_KB(void) {
|
||||
os_state_indicate();
|
||||
}
|
||||
|
||||
# if defined(LED_MATRIX_DRIVER_SHUTDOWN_ENABLE) || defined(RGB_MATRIX_DRIVER_SHUTDOWN_ENABLE)
|
||||
bool LED_DRIVER_ALLOW_SHUTDOWN(void) {
|
||||
# if defined(NUM_LOCK_INDEX)
|
||||
if (host_keyboard_led_state().num_lock) return false;
|
||||
# endif
|
||||
# if defined(CAPS_LOCK_INDEX) && !defined(DIM_CAPS_LOCK)
|
||||
if (host_keyboard_led_state().caps_lock) return false;
|
||||
# endif
|
||||
# if defined(SCROLL_LOCK_INDEX)
|
||||
if (host_keyboard_led_state().scroll_lock) return false;
|
||||
# endif
|
||||
# if defined(COMPOSE_LOCK_INDEX)
|
||||
if (host_keyboard_led_state().compose) return false;
|
||||
# endif
|
||||
# if defined(KANA_LOCK_INDEX)
|
||||
if (host_keyboard_led_state().kana) return false;
|
||||
# endif
|
||||
return true;
|
||||
}
|
||||
# endif
|
||||
|
||||
#endif
|
||||
118
keyboards/keychron/bluetooth/indicator.h
Normal file
118
keyboards/keychron/bluetooth/indicator.h
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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
|
||||
|
||||
#if defined(BAT_LOW_LED_PIN) || defined(BAT_LOW_LED_PIN_STATE)
|
||||
/* Uint: ms */
|
||||
# ifndef LOW_BAT_LED_BLINK_PERIOD
|
||||
# define LOW_BAT_LED_BLINK_PERIOD 1000
|
||||
# endif
|
||||
|
||||
# ifndef LOW_BAT_LED_BLINK_DURATION
|
||||
# define LOW_BAT_LED_BLINK_DURATION 10000
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef LOW_BAT_IND_INDEX
|
||||
/* Uint: ms */
|
||||
# ifndef LOW_BAT_LED_BLINK_PERIOD
|
||||
# define LOW_BAT_LED_BLINK_PERIOD 500
|
||||
# endif
|
||||
|
||||
# ifndef LOW_BAT_LED_BLINK_TIMES
|
||||
# define LOW_BAT_LED_BLINK_TIMES 3
|
||||
# endif
|
||||
|
||||
# ifndef LOW_BAT_LED_TRIG_INTERVAL
|
||||
# define LOW_BAT_LED_TRIG_INTERVAL 30000
|
||||
# endif
|
||||
#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);
|
||||
void indicator_eeconfig_reload(void);
|
||||
bool indicator_is_enabled(void);
|
||||
bool indicator_is_running(void);
|
||||
void os_state_indicate(void);
|
||||
|
||||
#ifdef BAT_LOW_LED_PIN
|
||||
void indicator_battery_low_enable(bool enable);
|
||||
#endif
|
||||
#if defined(LOW_BAT_IND_INDEX)
|
||||
void indicator_battery_low_backlit_enable(bool enable);
|
||||
#endif
|
||||
|
||||
void indicator_task(void);
|
||||
92
keyboards/keychron/bluetooth/lpm.c
Normal file
92
keyboards/keychron/bluetooth/lpm.c
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/******************************************************************************
|
||||
*
|
||||
* Filename: lpm.c
|
||||
*
|
||||
* Description: Contains low power mode implementation
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "quantum.h"
|
||||
#if defined(PROTOCOL_CHIBIOS)
|
||||
# include <usb_main.h>
|
||||
#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
|
||||
# if (USB_POWER_CONNECTED_LEVEL == 0)
|
||||
setPinInputHigh(USB_POWER_SENSE_PIN);
|
||||
# else
|
||||
setPinInputLow(USB_POWER_SENSE_PIN);
|
||||
# endif
|
||||
#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() && !battery_power_on_sample())
|
||||
|
||||
enter_power_mode(LOW_POWER_MODE);
|
||||
}
|
||||
30
keyboards/keychron/bluetooth/lpm.h
Normal file
30
keyboards/keychron/bluetooth/lpm.h
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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);
|
||||
330
keyboards/keychron/bluetooth/lpm_stm32l432.c
Normal file
330
keyboards/keychron/bluetooth/lpm_stm32l432.c
Normal file
|
|
@ -0,0 +1,330 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/******************************************************************************
|
||||
*
|
||||
* Filename: lpm_stm32l432.c
|
||||
*
|
||||
* Description: Contains low power mode implementation
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "quantum.h"
|
||||
#include <usb_main.h>
|
||||
#include "bluetooth.h"
|
||||
#include "indicator.h"
|
||||
#include "lpm.h"
|
||||
#include "transport.h"
|
||||
#include "battery.h"
|
||||
#include "report_buffer.h"
|
||||
#include "stm32_bd.inc"
|
||||
#include "debounce.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 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) {
|
||||
chSysLock();
|
||||
stm32_clock_fast_init();
|
||||
chSysUnlock();
|
||||
|
||||
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;
|
||||
|
||||
/* 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);
|
||||
|
||||
#ifdef USB_POWER_SENSE_PIN
|
||||
palDisableLineEvent(USB_POWER_SENSE_PIN);
|
||||
|
||||
# if defined(KEEP_USB_CONNECTION_IN_BLUETOOTH_MODE)
|
||||
if (usb_power_connected()) {
|
||||
hsi48_init();
|
||||
/* Remove USB isolation.*/
|
||||
// PWR->CR2 |= PWR_CR2_USV; /* PWR_CR2_USV is available on STM32L4x2xx and STM32L4x3xx devices only. */
|
||||
usb_power_connect();
|
||||
usb_start(&USBD1);
|
||||
}
|
||||
# endif
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(DIP_SWITCH_PINS)
|
||||
dip_switch_init();
|
||||
dip_switch_read(true);
|
||||
#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();
|
||||
|
||||
/* Call debounce_free() to avoid memory leak as debounce_init() invoked in matrix_init() allocates
|
||||
* new memory when using per row/key debounce
|
||||
*/
|
||||
debounce_free();
|
||||
matrix_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 data
|
||||
* coming when wakeup pin interrupt of MCU is triggerred.
|
||||
* Here we reduce clock init time to less than 100us.
|
||||
*/
|
||||
void stm32_clock_fast_init(void) {
|
||||
#if !STM32_NO_INIT
|
||||
/* Clocks setup.*/
|
||||
msi_init(); // 6.x us
|
||||
hsi16_init(); // 4.x us
|
||||
|
||||
/* PLLs activation, if required.*/
|
||||
pll_init();
|
||||
pllsai1_init();
|
||||
pllsai2_init();
|
||||
/* clang-format off */
|
||||
/* Other clock-related settings (dividers, MCO etc).*/
|
||||
RCC->CFGR = STM32_MCOPRE | STM32_MCOSEL | STM32_STOPWUCK |
|
||||
STM32_PPRE2 | STM32_PPRE1 | STM32_HPRE;
|
||||
/* CCIPR register initialization, note, must take care of the _OFF
|
||||
pseudo settings.*/
|
||||
{
|
||||
uint32_t ccipr = STM32_DFSDMSEL | STM32_SWPMI1SEL | STM32_ADCSEL |
|
||||
STM32_CLK48SEL | STM32_LPTIM2SEL | STM32_LPTIM1SEL |
|
||||
STM32_I2C3SEL | STM32_I2C2SEL | STM32_I2C1SEL |
|
||||
STM32_UART5SEL | STM32_UART4SEL | STM32_USART3SEL |
|
||||
STM32_USART2SEL | STM32_USART1SEL | STM32_LPUART1SEL;
|
||||
/* clang-format on */
|
||||
# if STM32_SAI2SEL != STM32_SAI2SEL_OFF
|
||||
ccipr |= STM32_SAI2SEL;
|
||||
# endif
|
||||
# if STM32_SAI1SEL != STM32_SAI1SEL_OFF
|
||||
ccipr |= STM32_SAI1SEL;
|
||||
# endif
|
||||
RCC->CCIPR = ccipr;
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
}
|
||||
19
keyboards/keychron/bluetooth/lpm_stm32l432.h
Normal file
19
keyboards/keychron/bluetooth/lpm_stm32l432.h
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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;
|
||||
141
keyboards/keychron/bluetooth/report_buffer.c
Normal file
141
keyboards/keychron/bluetooth/report_buffer.c
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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 512
|
||||
#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];
|
||||
uint16_t report_buffer_queue_head;
|
||||
uint16_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) {
|
||||
uint16_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.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
56
keyboards/keychron/bluetooth/report_buffer.h
Normal file
56
keyboards/keychron/bluetooth/report_buffer.h
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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;
|
||||
report_nkro_t nkro;
|
||||
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);
|
||||
43
keyboards/keychron/bluetooth/rtc_timer.c
Normal file
43
keyboards/keychron/bluetooth/rtc_timer.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* Copyright 2023 @ 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "hal.h"
|
||||
|
||||
#if (HAL_USE_RTC)
|
||||
|
||||
# include "rtc_timer.h"
|
||||
|
||||
void rtc_timer_init(void) {
|
||||
rtc_timer_clear();
|
||||
}
|
||||
|
||||
void rtc_timer_clear(void) {
|
||||
RTCDateTime tm = {0, 0, 0, 0, 0, 0};
|
||||
rtcSetTime(&RTCD1, &tm);
|
||||
}
|
||||
|
||||
uint32_t rtc_timer_read_ms(void) {
|
||||
RTCDateTime tm;
|
||||
rtcGetTime(&RTCD1, &tm);
|
||||
|
||||
return tm.millisecond;
|
||||
}
|
||||
|
||||
uint32_t rtc_timer_elapsed_ms(uint32_t last) {
|
||||
return TIMER_DIFF_32(rtc_timer_read_ms(), last);
|
||||
}
|
||||
|
||||
#endif
|
||||
43
keyboards/keychron/bluetooth/rtc_timer.h
Normal file
43
keyboards/keychron/bluetooth/rtc_timer.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* Copyright 2023 @ 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "timer.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#define RTC_MAX_TIME (24 * 3600 * 1000) // Set to 1 day
|
||||
|
||||
#if 0
|
||||
# define TIMER_DIFF(a, b, max) ((max == UINT8_MAX) ? ((uint8_t)((a) - (b))) : ((max == UINT16_MAX) ? ((uint16_t)((a) - (b))) : ((max == UINT32_MAX) ? ((uint32_t)((a) - (b))) : ((a) >= (b) ? (a) - (b) : (max) + 1 - (b) + (a)))))
|
||||
# define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX)
|
||||
# define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX)
|
||||
# define TIMER_DIFF_32(a, b) TIMER_DIFF(a, b, UINT32_MAX)
|
||||
# define TIMER_DIFF_RAW(a, b) TIMER_DIFF_8(a, b)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void rtc_timer_init(void);
|
||||
void rtc_timer_clear(void);
|
||||
uint32_t rtc_timer_read_ms(void);
|
||||
uint32_t rtc_timer_elapsed_ms(uint32_t last);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
190
keyboards/keychron/bluetooth/transport.c
Normal file
190
keyboards/keychron/bluetooth/transport.c
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "quantum.h"
|
||||
#include "bluetooth.h"
|
||||
#include "indicator.h"
|
||||
#include "lpm.h"
|
||||
#if defined(PROTOCOL_CHIBIOS)
|
||||
# include <usb_main.h>
|
||||
#endif
|
||||
#include "transport.h"
|
||||
|
||||
#ifndef REINIT_LED_DRIVER
|
||||
# define REINIT_LED_DRIVER 1
|
||||
#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
|
||||
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_MATRIX_TIMEOUT)
|
||||
# if (RGB_MATRIX_TIMEOUT > 0)
|
||||
rgb_matrix_disable_timeout_set(RGB_MATRIX_TIMEOUT_INFINITE);
|
||||
rgb_matrix_disable_time_reset();
|
||||
# endif
|
||||
#endif
|
||||
#if defined(LED_MATRIX_ENABLE) && defined(LED_MATRIX_TIMEOUT)
|
||||
# if (LED_MATRIX_TIMEOUT > 0)
|
||||
led_matrix_disable_timeout_set(LED_MATRIX_TIMEOUT_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);
|
||||
}
|
||||
}
|
||||
wait_ms(500);
|
||||
/* Woken up */
|
||||
// variables has been already cleared by the wakeup hook
|
||||
send_keyboard_report();
|
||||
}
|
||||
}
|
||||
39
keyboards/keychron/bluetooth/transport.h
Normal file
39
keyboards/keychron/bluetooth/transport.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
typedef enum {
|
||||
TRANSPORT_NONE,
|
||||
TRANSPORT_USB,
|
||||
TRANSPORT_BLUETOOTH,
|
||||
} transport_t;
|
||||
|
||||
#ifdef NKRO_ENABLE
|
||||
typedef struct {
|
||||
bool usb : 1;
|
||||
bool bluetooth : 1;
|
||||
} nkro_t;
|
||||
#endif
|
||||
|
||||
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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue