Skip to content

Commit de97f1e

Browse files
purdeaandreidrashna
authored andcommitted
Added power tracking api (qmk#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 <[email protected]> * Fix comment * usb_device_state.h: Don't include quantum.h, only the necessary headers. Co-authored-by: Drashna Jaelre <[email protected]>
1 parent b4bb677 commit de97f1e

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed

common_features.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ QUANTUM_SRC += \
2929
$(QUANTUM_DIR)/keyboard.c \
3030
$(QUANTUM_DIR)/keymap_common.c \
3131
$(QUANTUM_DIR)/keycode_config.c \
32+
$(QUANTUM_DIR)/usb_device_state.c \
3233
$(QUANTUM_DIR)/logging/debug.c \
3334
$(QUANTUM_DIR)/logging/sendchar.c \
3435

quantum/usb_device_state.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2021 Andrei Purdea <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include "usb_device_state.h"
19+
20+
enum usb_device_state usb_device_state = USB_DEVICE_STATE_NO_INIT;
21+
22+
__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); }
23+
24+
__attribute__((weak)) void notify_usb_device_state_change_user(enum usb_device_state usb_device_state) {}
25+
26+
static void notify_usb_device_state_change(enum usb_device_state usb_device_state) { notify_usb_device_state_change_kb(usb_device_state); }
27+
28+
void usb_device_state_set_configuration(bool isConfigured, uint8_t configurationNumber) {
29+
usb_device_state = isConfigured ? USB_DEVICE_STATE_CONFIGURED : USB_DEVICE_STATE_INIT;
30+
notify_usb_device_state_change(usb_device_state);
31+
}
32+
33+
void usb_device_state_set_suspend(bool isConfigured, uint8_t configurationNumber) {
34+
usb_device_state = USB_DEVICE_STATE_SUSPEND;
35+
notify_usb_device_state_change(usb_device_state);
36+
}
37+
38+
void usb_device_state_set_resume(bool isConfigured, uint8_t configurationNumber) {
39+
usb_device_state = isConfigured ? USB_DEVICE_STATE_CONFIGURED : USB_DEVICE_STATE_INIT;
40+
notify_usb_device_state_change(usb_device_state);
41+
}
42+
43+
void usb_device_state_set_reset(void) {
44+
usb_device_state = USB_DEVICE_STATE_INIT;
45+
notify_usb_device_state_change(usb_device_state);
46+
}
47+
48+
void usb_device_state_init(void) {
49+
usb_device_state = USB_DEVICE_STATE_INIT;
50+
notify_usb_device_state_change(usb_device_state);
51+
}

quantum/usb_device_state.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2021 Andrei Purdea <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#pragma once
19+
20+
#include <stdbool.h>
21+
#include <stdint.h>
22+
23+
void usb_device_state_set_configuration(bool isConfigured, uint8_t configurationNumber);
24+
void usb_device_state_set_suspend(bool isConfigured, uint8_t configurationNumber);
25+
void usb_device_state_set_resume(bool isConfigured, uint8_t configurationNumber);
26+
void usb_device_state_set_reset(void);
27+
void usb_device_state_init(void);
28+
29+
enum usb_device_state {
30+
USB_DEVICE_STATE_NO_INIT = 0, // We're in this state before calling usb_device_state_init()
31+
USB_DEVICE_STATE_INIT = 1, // Can consume up to 100mA
32+
USB_DEVICE_STATE_CONFIGURED = 2, // Can consume up to what is specified in configuration descriptor, typically 500mA
33+
USB_DEVICE_STATE_SUSPEND = 3 // Can consume only suspend current
34+
};
35+
36+
extern enum usb_device_state usb_device_state;
37+
38+
void notify_usb_device_state_change_kb(enum usb_device_state usb_device_state);
39+
void notify_usb_device_state_change_user(enum usb_device_state usb_device_state);

tmk_core/protocol/chibios/chibios.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "keyboard.h"
2828
#include "action.h"
2929
#include "action_util.h"
30+
#include "usb_device_state.h"
3031
#include "mousekey.h"
3132
#include "led.h"
3233
#include "sendchar.h"
@@ -139,6 +140,8 @@ void boardInit(void) {
139140
}
140141

141142
void protocol_setup(void) {
143+
usb_device_state_init();
144+
142145
// TESTING
143146
// chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
144147

tmk_core/protocol/chibios/usb_main.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
# include "led.h"
4040
#endif
4141
#include "wait.h"
42+
#include "usb_device_state.h"
4243
#include "usb_descriptor.h"
4344
#include "usb_driver.h"
4445

@@ -412,13 +413,15 @@ static inline bool usb_event_queue_dequeue(usbevent_t *event) {
412413
}
413414

414415
static inline void usb_event_suspend_handler(void) {
416+
usb_device_state_set_suspend(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
415417
#ifdef SLEEP_LED_ENABLE
416418
sleep_led_enable();
417419
#endif /* SLEEP_LED_ENABLE */
418420
}
419421

420422
static inline void usb_event_wakeup_handler(void) {
421423
suspend_wakeup_init();
424+
usb_device_state_set_resume(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
422425
#ifdef SLEEP_LED_ENABLE
423426
sleep_led_disable();
424427
// NOTE: converters may not accept this
@@ -440,6 +443,15 @@ void usb_event_queue_task(void) {
440443
last_suspend_state = false;
441444
usb_event_wakeup_handler();
442445
break;
446+
case USB_EVENT_CONFIGURED:
447+
usb_device_state_set_configuration(USB_DRIVER.configuration != 0, USB_DRIVER.configuration);
448+
break;
449+
case USB_EVENT_UNCONFIGURED:
450+
usb_device_state_set_configuration(false, 0);
451+
break;
452+
case USB_EVENT_RESET:
453+
usb_device_state_set_reset();
454+
break;
443455
default:
444456
// Nothing to do, we don't handle it.
445457
break;
@@ -482,13 +494,14 @@ static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
482494
if (last_suspend_state) {
483495
usb_event_queue_enqueue(USB_EVENT_WAKEUP);
484496
}
497+
usb_event_queue_enqueue(USB_EVENT_CONFIGURED);
485498
return;
486499
case USB_EVENT_SUSPEND:
487-
usb_event_queue_enqueue(USB_EVENT_SUSPEND);
488500
/* Falls into.*/
489501
case USB_EVENT_UNCONFIGURED:
490502
/* Falls into.*/
491503
case USB_EVENT_RESET:
504+
usb_event_queue_enqueue(event);
492505
for (int i = 0; i < NUM_USB_DRIVERS; i++) {
493506
chSysLockFromISR();
494507
/* Disconnection event on suspend.*/

tmk_core/protocol/lufa/lufa.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "usb_descriptor.h"
5353
#include "lufa.h"
5454
#include "quantum.h"
55+
#include "usb_device_state.h"
5556
#include <util/atomic.h>
5657

5758
#ifdef NKRO_ENABLE
@@ -414,14 +415,19 @@ void EVENT_USB_Device_Disconnect(void) {
414415
*
415416
* FIXME: Needs doc
416417
*/
417-
void EVENT_USB_Device_Reset(void) { print("[R]"); }
418+
void EVENT_USB_Device_Reset(void) {
419+
print("[R]");
420+
usb_device_state_set_reset();
421+
}
418422

419423
/** \brief Event USB Device Connect
420424
*
421425
* FIXME: Needs doc
422426
*/
423427
void EVENT_USB_Device_Suspend() {
424428
print("[S]");
429+
usb_device_state_set_suspend(USB_Device_ConfigurationNumber != 0, USB_Device_ConfigurationNumber);
430+
425431
#ifdef SLEEP_LED_ENABLE
426432
sleep_led_enable();
427433
#endif
@@ -437,6 +443,8 @@ void EVENT_USB_Device_WakeUp() {
437443
suspend_wakeup_init();
438444
#endif
439445

446+
usb_device_state_set_resume(USB_DeviceState == DEVICE_STATE_Configured, USB_Device_ConfigurationNumber);
447+
440448
#ifdef SLEEP_LED_ENABLE
441449
sleep_led_disable();
442450
// NOTE: converters may not accept this
@@ -529,6 +537,8 @@ void EVENT_USB_Device_ConfigurationChanged(void) {
529537
/* Setup digitizer endpoint */
530538
ConfigSuccess &= Endpoint_ConfigureEndpoint((DIGITIZER_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_INTERRUPT, DIGITIZER_EPSIZE, 1);
531539
#endif
540+
541+
usb_device_state_set_configuration(USB_DeviceState == DEVICE_STATE_Configured, USB_Device_ConfigurationNumber);
532542
}
533543

534544
/* FIXME: Expose this table in the docs somehow
@@ -1059,6 +1069,7 @@ void protocol_setup(void) {
10591069
#endif
10601070

10611071
setup_mcu();
1072+
usb_device_state_init();
10621073
keyboard_setup();
10631074
}
10641075

0 commit comments

Comments
 (0)