Added power tracking api (#12691)

* Add power tracking API to lufa and chibios targets

* power.c: Pass through power state to the notify function

* power: added notify_power_state_change_user too.

* making it pass the PR linter

* Add a POWER_STATE_NO_INIT state, that we start in before calling power_init();

* Rename *power* to *usb_power*

* removing stray newline

* Rename usb_power* to usb_device_state*

* Update quantum/usb_device_state.h

Co-authored-by: Drashna Jaelre <drashna@live.com>

* Fix comment

* usb_device_state.h: Don't include quantum.h, only the necessary headers.

Co-authored-by: Drashna Jaelre <drashna@live.com>
This commit is contained in:
Purdea Andrei 2021-09-29 23:31:39 +03:00 committed by GitHub
parent 552c126bea
commit b02a539625
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 120 additions and 2 deletions

View file

@ -29,6 +29,7 @@ QUANTUM_SRC += \
$(QUANTUM_DIR)/keyboard.c \ $(QUANTUM_DIR)/keyboard.c \
$(QUANTUM_DIR)/keymap_common.c \ $(QUANTUM_DIR)/keymap_common.c \
$(QUANTUM_DIR)/keycode_config.c \ $(QUANTUM_DIR)/keycode_config.c \
$(QUANTUM_DIR)/usb_device_state.c \
$(QUANTUM_DIR)/logging/debug.c \ $(QUANTUM_DIR)/logging/debug.c \
$(QUANTUM_DIR)/logging/sendchar.c \ $(QUANTUM_DIR)/logging/sendchar.c \

View file

@ -0,0 +1,51 @@
/*
* Copyright 2021 Andrei Purdea <andrei@purdea.ro>
*
* 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 "usb_device_state.h"
enum usb_device_state usb_device_state = USB_DEVICE_STATE_NO_INIT;
__attribute__((weak)) void notify_usb_device_state_change_kb(enum usb_device_state usb_device_state) { notify_usb_device_state_change_user(usb_device_state); }
__attribute__((weak)) void notify_usb_device_state_change_user(enum usb_device_state usb_device_state) {}
static void notify_usb_device_state_change(enum usb_device_state usb_device_state) { notify_usb_device_state_change_kb(usb_device_state); }
void usb_device_state_set_configuration(bool isConfigured, uint8_t configurationNumber) {
usb_device_state = isConfigured ? USB_DEVICE_STATE_CONFIGURED : USB_DEVICE_STATE_INIT;
notify_usb_device_state_change(usb_device_state);
}
void usb_device_state_set_suspend(bool isConfigured, uint8_t configurationNumber) {
usb_device_state = USB_DEVICE_STATE_SUSPEND;
notify_usb_device_state_change(usb_device_state);
}
void usb_device_state_set_resume(bool isConfigured, uint8_t configurationNumber) {
usb_device_state = isConfigured ? USB_DEVICE_STATE_CONFIGURED : USB_DEVICE_STATE_INIT;
notify_usb_device_state_change(usb_device_state);
}
void usb_device_state_set_reset(void) {
usb_device_state = USB_DEVICE_STATE_INIT;
notify_usb_device_state_change(usb_device_state);
}
void usb_device_state_init(void) {
usb_device_state = USB_DEVICE_STATE_INIT;
notify_usb_device_state_change(usb_device_state);
}

View file

@ -0,0 +1,39 @@
/*
* Copyright 2021 Andrei Purdea <andrei@purdea.ro>
*
* 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 <stdbool.h>
#include <stdint.h>
void usb_device_state_set_configuration(bool isConfigured, uint8_t configurationNumber);
void usb_device_state_set_suspend(bool isConfigured, uint8_t configurationNumber);
void usb_device_state_set_resume(bool isConfigured, uint8_t configurationNumber);
void usb_device_state_set_reset(void);
void usb_device_state_init(void);
enum usb_device_state {
USB_DEVICE_STATE_NO_INIT = 0, // We're in this state before calling usb_device_state_init()
USB_DEVICE_STATE_INIT = 1, // Can consume up to 100mA
USB_DEVICE_STATE_CONFIGURED = 2, // Can consume up to what is specified in configuration descriptor, typically 500mA
USB_DEVICE_STATE_SUSPEND = 3 // Can consume only suspend current
};
extern enum usb_device_state usb_device_state;
void notify_usb_device_state_change_kb(enum usb_device_state usb_device_state);
void notify_usb_device_state_change_user(enum usb_device_state usb_device_state);

View file

@ -27,6 +27,7 @@
#include "keyboard.h" #include "keyboard.h"
#include "action.h" #include "action.h"
#include "action_util.h" #include "action_util.h"
#include "usb_device_state.h"
#include "mousekey.h" #include "mousekey.h"
#include "led.h" #include "led.h"
#include "sendchar.h" #include "sendchar.h"
@ -139,6 +140,8 @@ void boardInit(void) {
} }
void protocol_setup(void) { void protocol_setup(void) {
usb_device_state_init();
// TESTING // TESTING
// chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); // chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

View file

@ -39,6 +39,7 @@
# include "led.h" # include "led.h"
#endif #endif
#include "wait.h" #include "wait.h"
#include "usb_device_state.h"
#include "usb_descriptor.h" #include "usb_descriptor.h"
#include "usb_driver.h" #include "usb_driver.h"
@ -412,6 +413,7 @@ static inline bool usb_event_queue_dequeue(usbevent_t *event) {
} }
static inline void usb_event_suspend_handler(void) { static inline void usb_event_suspend_handler(void) {
usb_device_state_set_suspend(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
#ifdef SLEEP_LED_ENABLE #ifdef SLEEP_LED_ENABLE
sleep_led_enable(); sleep_led_enable();
#endif /* SLEEP_LED_ENABLE */ #endif /* SLEEP_LED_ENABLE */
@ -419,6 +421,7 @@ static inline void usb_event_suspend_handler(void) {
static inline void usb_event_wakeup_handler(void) { static inline void usb_event_wakeup_handler(void) {
suspend_wakeup_init(); suspend_wakeup_init();
usb_device_state_set_resume(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
#ifdef SLEEP_LED_ENABLE #ifdef SLEEP_LED_ENABLE
sleep_led_disable(); sleep_led_disable();
// NOTE: converters may not accept this // NOTE: converters may not accept this
@ -440,6 +443,15 @@ void usb_event_queue_task(void) {
last_suspend_state = false; last_suspend_state = false;
usb_event_wakeup_handler(); usb_event_wakeup_handler();
break; break;
case USB_EVENT_CONFIGURED:
usb_device_state_set_configuration(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
break;
case USB_EVENT_UNCONFIGURED:
usb_device_state_set_configuration(false, 0);
break;
case USB_EVENT_RESET:
usb_device_state_set_reset();
break;
default: default:
// Nothing to do, we don't handle it. // Nothing to do, we don't handle it.
break; break;
@ -482,13 +494,14 @@ static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
if (last_suspend_state) { if (last_suspend_state) {
usb_event_queue_enqueue(USB_EVENT_WAKEUP); usb_event_queue_enqueue(USB_EVENT_WAKEUP);
} }
usb_event_queue_enqueue(USB_EVENT_CONFIGURED);
return; return;
case USB_EVENT_SUSPEND: case USB_EVENT_SUSPEND:
usb_event_queue_enqueue(USB_EVENT_SUSPEND);
/* Falls into.*/ /* Falls into.*/
case USB_EVENT_UNCONFIGURED: case USB_EVENT_UNCONFIGURED:
/* Falls into.*/ /* Falls into.*/
case USB_EVENT_RESET: case USB_EVENT_RESET:
usb_event_queue_enqueue(event);
for (int i = 0; i < NUM_USB_DRIVERS; i++) { for (int i = 0; i < NUM_USB_DRIVERS; i++) {
chSysLockFromISR(); chSysLockFromISR();
/* Disconnection event on suspend.*/ /* Disconnection event on suspend.*/

View file

@ -52,6 +52,7 @@
#include "usb_descriptor.h" #include "usb_descriptor.h"
#include "lufa.h" #include "lufa.h"
#include "quantum.h" #include "quantum.h"
#include "usb_device_state.h"
#include <util/atomic.h> #include <util/atomic.h>
#ifdef NKRO_ENABLE #ifdef NKRO_ENABLE
@ -414,7 +415,10 @@ void EVENT_USB_Device_Disconnect(void) {
* *
* FIXME: Needs doc * FIXME: Needs doc
*/ */
void EVENT_USB_Device_Reset(void) { print("[R]"); } void EVENT_USB_Device_Reset(void) {
print("[R]");
usb_device_state_set_reset();
}
/** \brief Event USB Device Connect /** \brief Event USB Device Connect
* *
@ -422,6 +426,8 @@ void EVENT_USB_Device_Reset(void) { print("[R]"); }
*/ */
void EVENT_USB_Device_Suspend() { void EVENT_USB_Device_Suspend() {
print("[S]"); print("[S]");
usb_device_state_set_suspend(USB_Device_ConfigurationNumber != 0, USB_Device_ConfigurationNumber);
#ifdef SLEEP_LED_ENABLE #ifdef SLEEP_LED_ENABLE
sleep_led_enable(); sleep_led_enable();
#endif #endif
@ -437,6 +443,8 @@ void EVENT_USB_Device_WakeUp() {
suspend_wakeup_init(); suspend_wakeup_init();
#endif #endif
usb_device_state_set_resume(USB_DeviceState == DEVICE_STATE_Configured, USB_Device_ConfigurationNumber);
#ifdef SLEEP_LED_ENABLE #ifdef SLEEP_LED_ENABLE
sleep_led_disable(); sleep_led_disable();
// NOTE: converters may not accept this // NOTE: converters may not accept this
@ -529,6 +537,8 @@ void EVENT_USB_Device_ConfigurationChanged(void) {
/* Setup digitizer endpoint */ /* Setup digitizer endpoint */
ConfigSuccess &= Endpoint_ConfigureEndpoint((DIGITIZER_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, DIGITIZER_EPSIZE, 1); ConfigSuccess &= Endpoint_ConfigureEndpoint((DIGITIZER_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, DIGITIZER_EPSIZE, 1);
#endif #endif
usb_device_state_set_configuration(USB_DeviceState == DEVICE_STATE_Configured, USB_Device_ConfigurationNumber);
} }
/* FIXME: Expose this table in the docs somehow /* FIXME: Expose this table in the docs somehow
@ -1059,6 +1069,7 @@ void protocol_setup(void) {
#endif #endif
setup_mcu(); setup_mcu();
usb_device_state_init();
keyboard_setup(); keyboard_setup();
} }