Added Lemokey P1 Pro

This commit is contained in:
lokher 2024-08-21 11:20:44 +08:00
parent b71b83f882
commit 62030ee6bb
72 changed files with 2876 additions and 507 deletions

View file

@ -1,4 +1,4 @@
/* Copyright 2023 @ lokher (https://www.keychron.com)
/* Copyright 2022~2024 @ 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
@ -32,6 +32,7 @@
#include "lpm.h"
#include "transport.h"
#include "battery.h"
#include "bat_level_animation.h"
#include "report_buffer.h"
#include "lemokey_common.h"
@ -46,7 +47,6 @@ static matrix_row_t empty_matrix[MATRIX_ROWS] = {0};
pin_t pins_row[MATRIX_ROWS] = MATRIX_ROW_PINS;
pin_t pins_col[MATRIX_COLS] = MATRIX_COL_PINS;
;
__attribute__((weak)) void select_all_cols(void) {
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
@ -77,44 +77,62 @@ void lpm_timer_stop(void) {
}
static inline bool lpm_any_matrix_action(void) {
#ifdef OPTICAL_SWITCH
bool any_key = false;
for (uint8_t i = 0; i < MATRIX_ROWS; i++)
if (matrix_get_row(i) != 0) {
any_key = true;
}
return any_key;
#else
return memcmp(matrix, empty_matrix, sizeof(empty_matrix));
#endif
}
__attribute__((weak)) void matrix_enter_low_power(void) {
/* Enable key matrix wake up */
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
if (pins_row[x] != NO_PIN) {
palEnableLineEvent(pins_row[x], PAL_EVENT_MODE_BOTH_EDGES);
}
}
select_all_cols();
}
__attribute__((weak)) void matrix_exit_low_power(void) {
/* Disable all wake up pins */
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
if (pins_row[x] != NO_PIN) {
palDisableLineEvent(pins_row[x]);
}
}
}
/* 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;
#else
return true;
__attribute__((weak)) void lpm_pre_enter_low_power(void) {}
__attribute__((weak)) void lpm_enter_low_power_kb(void) {}
__attribute__((weak)) void lpm_enter_low_power(void) {
if (get_transport() == TRANSPORT_USB && !usb_power_connected()) {
#ifdef RGB_MATRIX_ENABLE
rgb_matrix_set_color_all(0, 0, 0);
rgb_matrix_driver.flush();
rgb_matrix_driver_shutdown();
#endif
}
__attribute__((weak)) bool lpm_is_kb_idle(void) {
return true;
}
__attribute__((weak)) bool lpm_set(pm_t mode) {
return false;
}
bool pre_enter_low_power_mode(pm_t mode) {
#if defined(KEEP_USB_CONNECTION_IN_WIRELESS_MODE)
/* Don't enter low power mode if attached to the host */
if (mode > PM_SLEEP && usb_power_connected()) return false;
#ifdef LED_MATRIX_ENABLE
led_matrix_set_value_all(0);
led_matrix_driver.flush();
led_matrix_driver_shutdown();
#endif
if (!lpm_set(mode)) return false;
#ifdef LED_NUM_LOCK_PIN
writePin(LED_NUM_LOCK_PIN, !LED_PIN_ON_STATE);
#endif
#ifdef LED_CAPS_LOCK_PIN
writePin(LED_CAPS_LOCK_PIN, !LED_PIN_ON_STATE);
#endif
#ifdef BAT_LOW_LED_PIN
writePin(BAT_LOW_LED_PIN, !BAT_LOW_LED_PIN_ON_STATE);
#endif
#ifdef BT_INDICATION_LED_PIN_LIST
pin_t bt_led_pins[] = BT_INDICATION_LED_PIN_LIST;
for (uint8_t i = 0; i < sizeof(bt_led_pins) / sizeof(pin_t); i++)
writePin(bt_led_pins[i], !BT_INDICATION_LED_ON_STATE);
#endif
}
#if defined(KEEP_USB_CONNECTION_IN_WIRELESS_MODE)
/* Usb unit is actived and running, stop and disconnect first */
@ -125,6 +143,13 @@ bool pre_enter_low_power_mode(pm_t mode) {
// PWR->CR2 &= ~PWR_CR2_USV; /*PWR_CR2_USV is available on STM32L4x2xx and STM32L4x3xx devices only. */
#endif
#if (HAL_USE_SPI == TRUE)
spiStop(&SPI_DRIVER);
palSetLineMode(SPI_SCK_PIN, PAL_MODE_INPUT_PULLDOWN);
palSetLineMode(SPI_MISO_PIN, PAL_MODE_INPUT_PULLDOWN);
palSetLineMode(SPI_MOSI_PIN, PAL_MODE_INPUT_PULLDOWN);
#endif
palEnableLineEvent(LKBT51_INT_INPUT_PIN, PAL_EVENT_MODE_FALLING_EDGE);
#ifdef USB_POWER_SENSE_PIN
palEnableLineEvent(USB_POWER_SENSE_PIN, PAL_EVENT_MODE_BOTH_EDGES);
@ -135,38 +160,7 @@ bool pre_enter_low_power_mode(pm_t mode) {
#ifdef BT_MODE_SELECT_PIN
palEnableLineEvent(BT_MODE_SELECT_PIN, PAL_EVENT_MODE_BOTH_EDGES);
#endif
#ifdef OPTICAL_SWITCH
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
if (pins_row[x] != NO_PIN) {
writePinLow(pins_row[x]);
}
}
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
if (pins_col[x] != NO_PIN) {
setPinInputLow(pins_col[x]);
}
}
#else
/* Enable key matrix wake up */
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
if (pins_row[x] != NO_PIN) {
palEnableLineEvent(pins_row[x], PAL_EVENT_MODE_BOTH_EDGES);
}
}
#endif
select_all_cols();
#if (HAL_USE_SPI == TRUE)
palSetLineMode(SPI_SCK_PIN, PAL_MODE_INPUT_PULLDOWN);
palSetLineMode(SPI_MISO_PIN, PAL_MODE_INPUT_PULLDOWN);
palSetLineMode(SPI_MOSI_PIN, PAL_MODE_INPUT_PULLDOWN);
#endif
palSetLineMode(A12, PAL_MODE_INPUT_PULLDOWN);
palSetLineMode(A11, PAL_MODE_INPUT_PULLDOWN);
matrix_enter_low_power();
#if defined(DIP_SWITCH_PINS)
# define NUMBER_OF_DIP_SWITCHES (sizeof(dip_switch_pad) / sizeof(pin_t))
@ -177,19 +171,25 @@ bool pre_enter_low_power_mode(pm_t mode) {
}
#endif
battery_stop();
return true;
lpm_enter_low_power_kb();
}
static inline void lpm_wakeup(void) {
palSetLineMode(A11, PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING | PAL_MODE_ALTERNATE(10U));
palSetLineMode(A12, PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING | PAL_MODE_ALTERNATE(10U));
__attribute__((weak)) void lpm_post_enter_low_power(void) {}
#if (HAL_USE_SPI == TRUE)
palSetLineMode(SPI_SCK_PIN, PAL_MODE_ALTERNATE(5));
palSetLineMode(SPI_MISO_PIN, PAL_MODE_ALTERNATE(5));
palSetLineMode(SPI_MOSI_PIN, PAL_MODE_ALTERNATE(5));
#endif
__attribute__((weak)) void lpm_standby(pm_t mode) {}
__attribute__((weak)) void lpm_early_wakeup(void) {
writePinLow(BLUETOOTH_INT_OUTPUT_PIN);
}
__attribute__((weak)) void lpm_wakeup_init(void) {}
__attribute__((weak)) void lpm_pre_wakeup(void) {
writePinHigh(BLUETOOTH_INT_OUTPUT_PIN);
}
__attribute__((weak)) void lpm_wakeup(void) {
matrix_exit_low_power();
halInit();
@ -200,13 +200,6 @@ static inline void lpm_wakeup(void) {
if (wireless_transport.init) wireless_transport.init(true);
battery_init();
/* Disable all wake up pins */
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
if (pins_row[x] != NO_PIN) {
palDisableLineEvent(pins_row[x]);
}
}
palDisableLineEvent(LKBT51_INT_INPUT_PIN);
#ifdef P2P4_MODE_SELECT_PIN
palDisableLineEvent(P2P4_MODE_SELECT_PIN);
@ -218,7 +211,11 @@ static inline void lpm_wakeup(void) {
palDisableLineEvent(USB_POWER_SENSE_PIN);
# if defined(KEEP_USB_CONNECTION_IN_WIRELESS_MODE)
if (usb_power_connected()) {
if (usb_power_connected()
# ifndef BT_MODE_SELECT_PIN
&& (get_transport() == TRANSPORT_USB)
# endif
) {
usb_event_queue_init();
init_usb_driver(&USB_DRIVER);
}
@ -236,8 +233,41 @@ static inline void lpm_wakeup(void) {
debounce_free();
matrix_init();
}
__attribute__((weak)) void lpm_post_wakeup(void) {}
__attribute__((weak)) bool usb_power_connected(void) {
#ifdef USB_POWER_SENSE_PIN
return readPin(USB_POWER_SENSE_PIN) == USB_POWER_CONNECTED_LEVEL;
#else
return true;
#endif
}
__attribute__((weak)) bool lpm_is_kb_idle(void) {
return true;
}
__attribute__((weak)) bool lpm_set(pm_t mode) {
return false;
}
__attribute__((weak)) void lpm_peripheral_enter_low_power(void) {}
__attribute__((weak)) void lpm_peripheral_exit_low_power(void) {}
bool allow_low_power_mode(pm_t mode) {
#if defined(KEEP_USB_CONNECTION_IN_WIRELESS_MODE)
/* Don't enter low power mode if attached to the host */
if (mode > PM_SLEEP && usb_power_connected()) return false;
#endif
if (!lpm_set(mode)) return false;
return true;
}
void lpm_task(void) {
bool lpm = false;
if (!lpm_time_up && sync_timer_elapsed32(lpm_timer_buffer) > RUN_MODE_PROCESS_TIME) {
lpm_time_up = true;
lpm_timer_buffer = 0;
@ -248,30 +278,39 @@ void lpm_task(void) {
init_usb_driver(&USB_DRIVER);
}
if ((get_transport() == TRANSPORT_BLUETOOTH || get_transport() == TRANSPORT_P2P4) && lpm_time_up && !indicator_is_running() && lpm_is_kb_idle()) {
#if defined(LED_MATRIX_ENABLE) || defined(RGB_MATRIX_ENABLE)
if ((get_transport() & TRANSPORT_WIRELESS) && lpm_time_up && !indicator_is_running() && lpm_is_kb_idle()) {
if (
# ifdef LED_MATRIX_ENABLE
!led_matrix_is_enabled() ||
(led_matrix_is_enabled() && led_matrix_is_driver_shutdown())
# endif
# ifdef RGB_MATRIX_ENABLE
!rgb_matrix_is_enabled() ||
(rgb_matrix_is_enabled() && rgb_matrix_is_driver_shutdown())
# endif
)
#ifdef LED_MATRIX_ENABLE
!led_matrix_is_enabled() || (led_matrix_is_enabled() && led_matrix_is_driver_shutdown())
#elif defined(RGB_MATRIX_ENABLE)
!rgb_matrix_is_enabled() || (rgb_matrix_is_enabled() && rgb_matrix_is_driver_shutdown())
#else
!bat_level_animiation_actived()
#endif
{
) {
if (!lpm_any_matrix_action()) {
if (pre_enter_low_power_mode(LOW_POWER_MODE)) {
enter_power_mode(LOW_POWER_MODE);
lpm_wakeup();
lpm_timer_reset();
report_buffer_init();
lpm_set(PM_RUN);
if (allow_low_power_mode(LOW_POWER_MODE)) {
lpm = true;
}
}
}
}
if (lpm) {
lpm_pre_enter_low_power();
lpm_enter_low_power();
lpm_post_enter_low_power();
lpm_standby(LOW_POWER_MODE);
lpm_early_wakeup();
lpm_wakeup_init();
lpm_pre_wakeup();
lpm_wakeup();
lpm_post_wakeup();
lpm_timer_reset();
report_buffer_init();
lpm_set(PM_RUN);
}
}