Skip to content

Commit

Permalink
Teensy 4.1: implement USB remote wakeup
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
stapelberg committed Oct 29, 2022
1 parent aa12996 commit 0f47fb7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
7 changes: 5 additions & 2 deletions ext/nxp-middleware-usb/device/usb_device_ehci.c
Original file line number Diff line number Diff line change
Expand Up @@ -1696,8 +1696,11 @@ usb_status_t USB_DeviceEhciControl(usb_device_controller_handle ehciHandle, usb_
#endif
ehciState->registerBase->PORTSC1 &= ~USBHS_PORTSC1_PHCD_MASK;
ehciState->registerBase->PORTSC1 |= USBHS_PORTSC1_FPR_MASK;
startTick = deviceHandle->hwTick;
while ((deviceHandle->hwTick - startTick) < 10U)
// For easier ChibiOS integration, directly query the (already
// enabled) CYCCNT register instead of the deviceHandle->hwTick
// variable, which ChibiOS currently does not update.
startTick = DWT->CYCCNT;
while ((DWT->CYCCNT - startTick) < 10U)
{
__NOP();
}
Expand Down
6 changes: 6 additions & 0 deletions os/hal/boards/PJRC_TEENSY_4_1/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ void __late_init(void) {
*/
void boardInit(void) {
}

void restart_usb_driver(USBDriver *usbp) {
// Do nothing. Restarting the USB driver on the Teensy 4.x breaks it,
// resulting in a keyboard which can wake up a PC from Suspend-to-RAM, but
// does not actually produce any keypresses until you un-plug and re-plug.
}
8 changes: 6 additions & 2 deletions os/hal/ports/MIMXRT1062/LLD/USBHSv1/hal_usb_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,16 @@ static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callb
break;

case kUSB_DeviceEventSuspend:
printf_debug(" suspend");
printf_debug(" suspend--nxp");
// Call USB_DeviceSetStatus() to enable the “detect resume” interrupt.
usb_device_struct_t *dev_handle = (usb_device_struct_t *)handle;
(void)USB_DeviceSetStatus(dev_handle, kUSB_DeviceStatusBusSuspend, NULL);
printf_debug(" suspend--chibi");
_usb_suspend(usbp);
break;

case kUSB_DeviceEventResume:
printf_debug(" resume");
printf_debug(" resume--chibi");
_usb_wakeup(usbp);
break;
}
Expand Down
6 changes: 4 additions & 2 deletions os/hal/ports/MIMXRT1062/LLD/USBHSv1/hal_usb_lld.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,10 @@ struct USBDriver {
* @notapi
*/
#define usb_lld_wakeup_host(usbp) \
do{ \
} while (false)
do{ \
usb_device_struct_t *dev_handle = (usb_device_struct_t *)handle; \
(void)USB_DeviceSetStatus(dev_handle, kUSB_DeviceStatusBusResume, NULL); \
} while (0)


/*===========================================================================*/
Expand Down
4 changes: 2 additions & 2 deletions os/hal/ports/MIMXRT1062/LLD/USBHSv1/usb_device_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@
#define USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE (0U)
#endif
/*! @brief Whether the low power mode is enabled or not. */
#define USB_DEVICE_CONFIG_LOW_POWER_MODE (0U)
#define USB_DEVICE_CONFIG_LOW_POWER_MODE (1U)

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

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

0 comments on commit 0f47fb7

Please sign in to comment.