Skip to content

Commit 7e2d7c2

Browse files
committed
add remote wakeup for HIDKeyboard
1 parent c8d35e3 commit 7e2d7c2

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

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

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

207207
uint8_t Keyboard_press(__data uint8_t k) {
208+
USB_RemoteWakeup(); // Wake up host if it's in suspend mode
209+
208210
__data uint8_t i;
209211
if (k >= 136) { // it's a non-printing key (not a modifier)
210212
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: 23 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,18 @@ 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+
void USB_RemoteWakeup() {
548+
if (usbSuspended && (ConfigurationDescriptor.Config.ConfigAttributes &
549+
USB_CONFIG_ATTR_REMOTEWAKEUP)) {
550+
// Send resume signal: force DP/DM output resume/K state
551+
// The Usb was running at full speed 12M mode
552+
553+
UDEV_CTRL |= bUD_LOW_SPEED;
554+
delayMicroseconds(2000);
555+
UDEV_CTRL &= ~bUD_LOW_SPEED;
556+
557+
usbSuspended = 0; // Mark as no longer suspended
558+
}
559+
}

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

Lines changed: 4 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,6 @@ void USBInterrupt(void);
5659
void USBDeviceCfg();
5760
void USBDeviceIntCfg();
5861
void USBDeviceEndPointCfg();
62+
void USB_RemoteWakeup(); // Trigger remote wakeup
5963

6064
#endif

0 commit comments

Comments
 (0)