Skip to content

Commit fd8650e

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 fd8650e

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

ext/nxp-middleware-usb/device/usb_device_ehci.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,13 @@ usb_status_t USB_DeviceEhciCancel(usb_device_controller_handle ehciHandle, uint8
15551555
return kStatus_USB_Success;
15561556
}
15571557

1558+
static uint32_t ms_to_cycles(const uint32_t val) {
1559+
// 600 cycles at 0.6 cycles/ns == 1μs
1560+
const uint32_t cycles_per_us = 600;
1561+
const uint32_t ms_to_us = 1000;
1562+
return val * ms_to_us * cycles_per_us;
1563+
}
1564+
15581565
/*!
15591566
* @brief Control the status of the selected item.
15601567
*
@@ -1696,8 +1703,11 @@ usb_status_t USB_DeviceEhciControl(usb_device_controller_handle ehciHandle, usb_
16961703
#endif
16971704
ehciState->registerBase->PORTSC1 &= ~USBHS_PORTSC1_PHCD_MASK;
16981705
ehciState->registerBase->PORTSC1 |= USBHS_PORTSC1_FPR_MASK;
1699-
startTick = deviceHandle->hwTick;
1700-
while ((deviceHandle->hwTick - startTick) < 10U)
1706+
// For easier ChibiOS integration, directly query the (already
1707+
// enabled) CYCCNT register instead of the deviceHandle->hwTick
1708+
// variable, which ChibiOS currently does not update.
1709+
startTick = DWT->CYCCNT;
1710+
while ((DWT->CYCCNT - startTick) < ms_to_cycles(10U))
17011711
{
17021712
__NOP();
17031713
}

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
}

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
/*===========================================================================*/

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)