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 Nov 12, 2022
1 parent aa12996 commit fd8650e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
14 changes: 12 additions & 2 deletions ext/nxp-middleware-usb/device/usb_device_ehci.c
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,13 @@ usb_status_t USB_DeviceEhciCancel(usb_device_controller_handle ehciHandle, uint8
return kStatus_USB_Success;
}

static uint32_t ms_to_cycles(const uint32_t val) {
// 600 cycles at 0.6 cycles/ns == 1μs
const uint32_t cycles_per_us = 600;
const uint32_t ms_to_us = 1000;
return val * ms_to_us * cycles_per_us;
}

/*!
* @brief Control the status of the selected item.
*
Expand Down Expand Up @@ -1696,8 +1703,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) < ms_to_cycles(10U))
{
__NOP();
}
Expand Down
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 fd8650e

Please sign in to comment.