Skip to content

Commit 2b89d18

Browse files
committed
add remote wakeup for HIDKeyboard
no key report in wakeup
1 parent c8d35e3 commit 2b89d18

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboard/src/userUsbHidKeyboard/USBHIDKeyboard.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ uint8_t USB_EP1_send() {
205205
}
206206

207207
uint8_t Keyboard_press(__data uint8_t k) {
208+
if (USB_RemoteWakeup()) {
209+
// Don't register this key press - it was used for wakeup
210+
return 0;
211+
}
212+
208213
__data uint8_t i;
209214
if (k >= 136) { // it's a non-printing key (not a modifier)
210215
k = k - 136;

ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboard/src/userUsbHidKeyboard/USBconstant.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ __code USB_Descriptor_Configuration_t ConfigurationDescriptor = {
3838
.ConfigurationNumber = 1,
3939
.ConfigurationStrIndex = NO_DESCRIPTOR,
4040

41-
.ConfigAttributes = (USB_CONFIG_ATTR_RESERVED),
41+
.ConfigAttributes =
42+
(USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_REMOTEWAKEUP),
4243

4344
.MaxPowerConsumption = USB_CONFIG_POWER_MA(200)},
4445

ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboard/src/userUsbHidKeyboard/USBhandler.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ __xdata uint8_t keyboardProtocol = 1;
3333

3434
__xdata uint8_t keyboardLedStatus = 0;
3535

36+
// Track USB suspend state for remote wakeup
37+
volatile __bit usbSuspended = 0;
38+
39+
void delayMicroseconds(__data uint16_t us);
3640
inline void NOP_Process(void) {}
3741

3842
void USB_EP0_SETUP() {
@@ -472,15 +476,16 @@ void USBInterrupt(void) { // inline not really working in multiple files in SDCC
472476
if (UIF_SUSPEND) {
473477
UIF_SUSPEND = 0;
474478
if (USB_MIS_ST & bUMS_SUSPEND) { // Suspend
475-
479+
usbSuspended = 1; // Mark USB as suspended
480+
// Optionally put MCU to sleep here if needed
476481
// while ( XBUS_AUX & bUART0_TX ); // Wait for Tx
477482
// SAFE_MOD = 0x55;
478483
// SAFE_MOD = 0xAA;
479484
// WAKE_CTRL = bWAK_BY_USB | bWAK_RXD0_LO; // Wake up by USB or RxD0
480485
// PCON |= PD; // Chip sleep SAFE_MOD = 0x55; SAFE_MOD = 0xAA; WAKE_CTRL =
481486
// 0x00;
482-
483-
} else { // Unexpected interrupt, not supposed to happen !
487+
} else { // Resume
488+
usbSuspended = 0; // USB is active again
484489
USB_INT_FG = 0xFF; // Clear interrupt flag
485490
}
486491
}
@@ -537,3 +542,21 @@ void USBDeviceEndPointCfg() {
537542
UEP_R_RES_ACK | UEP_T_RES_NAK; // Manual flip, OUT transaction returns
538543
// ACK, IN transaction returns NAK
539544
}
545+
546+
// Trigger USB remote wakeup to wake host from suspend
547+
// Returns 1 if wakeup was performed, 0 if not needed
548+
uint8_t USB_RemoteWakeup() {
549+
if (usbSuspended && (ConfigurationDescriptor.Config.ConfigAttributes &
550+
USB_CONFIG_ATTR_REMOTEWAKEUP)) {
551+
// Send resume signal: force DP/DM output resume/K state
552+
// The Usb was running at full speed 12M mode
553+
554+
UDEV_CTRL |= bUD_LOW_SPEED;
555+
delayMicroseconds(50000);
556+
UDEV_CTRL &= ~bUD_LOW_SPEED;
557+
usbSuspended = 0; // Mark as no longer suspended
558+
delayMicroseconds(50000);
559+
return 1; // Wakeup was performed
560+
}
561+
return 0; // No wakeup needed
562+
}

ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboard/src/userUsbHidKeyboard/USBhandler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ extern __data uint8_t SetupReq;
1818
volatile extern __xdata uint8_t UsbConfig;
1919

2020
extern const __code uint8_t *__data pDescr;
21+
extern __xdata uint8_t keyboardProtocol;
22+
extern __xdata uint8_t keyboardLedStatus;
23+
extern volatile __bit usbSuspended;
2124

2225
void USB_EP1_IN();
2326
void USB_EP1_OUT();
@@ -56,5 +59,7 @@ void USBInterrupt(void);
5659
void USBDeviceCfg();
5760
void USBDeviceIntCfg();
5861
void USBDeviceEndPointCfg();
62+
uint8_t
63+
USB_RemoteWakeup(); // Trigger remote wakeup, returns 1 if wakeup was performed
5964

6065
#endif

0 commit comments

Comments
 (0)