Skip to content

Commit

Permalink
【USB】支持电脑休眠唤醒功能
Browse files Browse the repository at this point in the history
注意:此处修改会导致电脑休眠后,USB指示灯长亮,下一步应当通过Uart发送电脑处于休眠状态的信息,以便蓝牙主控获取信息后关闭USB指示灯。
  • Loading branch information
Geno [格诺] committed Feb 21, 2023
1 parent 2444a46 commit e7d8698
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
40 changes: 16 additions & 24 deletions usb/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ static INTERRUPT_USING(DeviceInterrupt, INT_NO_USB, 1) //USB中断服务程序,
UsbIsr();
}

/**
* @brief 按键发送事件
*
*/
static void UsbOnKeySend()
{
if (usb_state.is_sleep && usb_state.remote_wake) {
usb_state.is_sleep = false;
CH554USBDevWakeup();
}
}

/**
* @brief 上传键盘通常按键数据包
*
Expand All @@ -81,12 +69,14 @@ void KeyboardGenericUpload(uint8_t* packet, uint8_t len)
{
if (len != 8)
return;
UsbOnKeySend();

usb_state.is_busy = true;
memcpy(&Ep1Buffer[64], packet, len);
UEP1_T_LEN = len;
UEP1_CTRL = UEP1_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_ACK;
if ((USB_MIS_ST & bUMS_SUSPEND) && usb_state.remote_wake) {
CH554USBDevWakeup();
} else {
usb_state.is_busy = true;
memcpy(&Ep1Buffer[64], packet, len);
UEP1_T_LEN = len;
UEP1_CTRL = UEP1_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_ACK;
}
}

/**
Expand All @@ -97,12 +87,14 @@ void KeyboardGenericUpload(uint8_t* packet, uint8_t len)
*/
void KeyboardExtraUpload(uint8_t* packet, uint8_t len)
{
UsbOnKeySend();

usb_state.is_busy = true;
memcpy(Ep2Buffer, packet, len);
UEP2_T_LEN = len;
UEP2_CTRL = UEP2_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_ACK;
if ((USB_MIS_ST & bUMS_SUSPEND) && usb_state.remote_wake) {
CH554USBDevWakeup();
} else {
usb_state.is_busy = true;
memcpy(Ep2Buffer, packet, len);
UEP2_T_LEN = len;
UEP2_CTRL = UEP2_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_ACK;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion usb/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static void uart_send_status()
if (!IS_CHARGING) // 是否充满
data |= 0x02;
#endif
if (usb_state.is_ready && !usb_state.is_sleep) // 是否连接主机
if (usb_state.is_ready || usb_state.remote_wake) // 是否连接主机
data |= 0x04;
if (usb_state.protocol)
data |= 0x08;
Expand Down
4 changes: 2 additions & 2 deletions usb/usb_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ enum StringDescriptor {
uint8_t const DeviceDescriptor[] = {
sizeof(DeviceDescriptor), // Length of this descriptor
0x01, // Type code of this descriptor
0x10, 0x01, // Release of USB spec
0x00, 0x02, // Release of USB spec
0x00, // Device's base class code
0x00, // Device's sub class code
0x00, // Device's protocol type code
Expand All @@ -65,7 +65,7 @@ uint8_t const DeviceDescriptor[] = {
#else
#define USB_NUM_INTERFACES 0x03
#endif
#define USB_SUPPORT_REM_WAKE 0x00 // 0x20 support, 0x00 not support
#define USB_SUPPORT_REM_WAKE 0x20 // 0x20 support, 0x00 not support
#define USB_SUPPORT_SELF_POWERED 0x80 // not self-powered
#define USB_MAX_POWER 0xfa // 500 mA

Expand Down

0 comments on commit e7d8698

Please sign in to comment.