Skip to content

Commit 0f47fb7

Browse files
committed
Teensy 4.1: implement USB remote wakeup
This allows waking up a computer from Suspend-to-RAM by pressing a key on the keyboard (with the QMK keyboard firmware, which uses ChibiOS for the Teensy).
1 parent aa12996 commit 0f47fb7

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

Diff for: ext/nxp-middleware-usb/device/usb_device_ehci.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -1696,8 +1696,11 @@ usb_status_t USB_DeviceEhciControl(usb_device_controller_handle ehciHandle, usb_
16961696
#endif
16971697
ehciState->registerBase->PORTSC1 &= ~USBHS_PORTSC1_PHCD_MASK;
16981698
ehciState->registerBase->PORTSC1 |= USBHS_PORTSC1_FPR_MASK;
1699-
startTick = deviceHandle->hwTick;
1700-
while ((deviceHandle->hwTick - startTick) < 10U)
1699+
// For easier ChibiOS integration, directly query the (already
1700+
// enabled) CYCCNT register instead of the deviceHandle->hwTick
1701+
// variable, which ChibiOS currently does not update.
1702+
startTick = DWT->CYCCNT;
1703+
while ((DWT->CYCCNT - startTick) < 10U)
17011704
{
17021705
__NOP();
17031706
}

Diff for: os/hal/boards/PJRC_TEENSY_4_1/board.c

+6
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ void __late_init(void) {
4545
*/
4646
void boardInit(void) {
4747
}
48+
49+
void restart_usb_driver(USBDriver *usbp) {
50+
// Do nothing. Restarting the USB driver on the Teensy 4.x breaks it,
51+
// resulting in a keyboard which can wake up a PC from Suspend-to-RAM, but
52+
// does not actually produce any keypresses until you un-plug and re-plug.
53+
}

Diff for: os/hal/ports/MIMXRT1062/LLD/USBHSv1/hal_usb_lld.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,16 @@ static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callb
116116
break;
117117

118118
case kUSB_DeviceEventSuspend:
119-
printf_debug(" suspend");
119+
printf_debug(" suspend--nxp");
120+
// Call USB_DeviceSetStatus() to enable the “detect resume” interrupt.
121+
usb_device_struct_t *dev_handle = (usb_device_struct_t *)handle;
122+
(void)USB_DeviceSetStatus(dev_handle, kUSB_DeviceStatusBusSuspend, NULL);
123+
printf_debug(" suspend--chibi");
120124
_usb_suspend(usbp);
121125
break;
122126

123127
case kUSB_DeviceEventResume:
124-
printf_debug(" resume");
128+
printf_debug(" resume--chibi");
125129
_usb_wakeup(usbp);
126130
break;
127131
}

Diff for: os/hal/ports/MIMXRT1062/LLD/USBHSv1/hal_usb_lld.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,10 @@ struct USBDriver {
408408
* @notapi
409409
*/
410410
#define usb_lld_wakeup_host(usbp) \
411-
do{ \
412-
} while (false)
411+
do{ \
412+
usb_device_struct_t *dev_handle = (usb_device_struct_t *)handle; \
413+
(void)USB_DeviceSetStatus(dev_handle, kUSB_DeviceStatusBusResume, NULL); \
414+
} while (0)
413415

414416

415417
/*===========================================================================*/

Diff for: os/hal/ports/MIMXRT1062/LLD/USBHSv1/usb_device_config.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@
154154
#define USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE (0U)
155155
#endif
156156
/*! @brief Whether the low power mode is enabled or not. */
157-
#define USB_DEVICE_CONFIG_LOW_POWER_MODE (0U)
157+
#define USB_DEVICE_CONFIG_LOW_POWER_MODE (1U)
158158

159159
#if ((defined(USB_DEVICE_CONFIG_LOW_POWER_MODE)) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
160160
/*! @brief Whether device remote wakeup supported. 1U supported, 0U not supported */
161-
#define USB_DEVICE_CONFIG_REMOTE_WAKEUP (0U)
161+
#define USB_DEVICE_CONFIG_REMOTE_WAKEUP (1U)
162162

163163
/*! @brief Whether LPM is supported. 1U supported, 0U not supported */
164164
#define USB_DEVICE_CONFIG_LPM_L1 (0U)

0 commit comments

Comments
 (0)