diff --git a/usb/endpoints.c b/usb/endpoints.c index bcd330e42..c37041cc9 100644 --- a/usb/endpoints.c +++ b/usb/endpoints.c @@ -23,7 +23,6 @@ along with this program. If not, see . #include "endpoints.h" #define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE -#define REMOTE_WAKE true /** * @brief 端点0/4缓冲区。 @@ -181,6 +180,7 @@ void EP0_SETUP() // USB枚举完毕 usb_state.is_ready = UsbConfig > 0; usb_state.setup_state = SETUP_STATE_IN; + RESET_KEEP = 1; break; case USB_GET_INTERFACE: @@ -225,7 +225,6 @@ void EP0_SETUP() } break; } -#if REMOTE_WAKE case USB_REQ_TO_DEVICE: if (UsbSetupBuf->wValue != 0x01) { // 操作失败 @@ -235,7 +234,6 @@ void EP0_SETUP() // 设置唤醒使能标志 usb_state.remote_wake = false; break; -#endif default: //unsupport SETUP_STALL(); return; @@ -276,7 +274,6 @@ void EP0_SETUP() } break; } -#if REMOTE_WAKE case USB_REQ_TO_DEVICE: { if (UsbSetupBuf->wValue != 0x01) { SETUP_STALL(); @@ -286,7 +283,6 @@ void EP0_SETUP() usb_state.remote_wake = true; break; } -#endif default: SETUP_STALL(); return; diff --git a/usb/main.c b/usb/main.c index fafb5e03c..a768ffd63 100644 --- a/usb/main.c +++ b/usb/main.c @@ -45,11 +45,14 @@ static void CH554SoftReset() /** \brief CH554设备模式唤醒主机,发送K信号 * */ -static void CH554USBDevWakeup() +void CH554USBDevWakeup() { + if (usb_state.is_sleep && usb_state.remote_wake) { + usb_state.is_sleep = false; UDEV_CTRL |= bUD_LOW_SPEED; DelayMs(2); UDEV_CTRL &= ~bUD_LOW_SPEED; + } } /** \brief CH559USB中断处理函数 @@ -59,18 +62,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 上传键盘通常按键数据包 * @@ -81,12 +72,10 @@ 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; + 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; } /** @@ -97,12 +86,10 @@ 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; + 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; } /** @@ -233,14 +220,11 @@ static void main() IE_TKEY = 1; // 运行Timer USBDeviceInit(); //USB设备模式初始化 - DelayMs(10); EA = 1; //允许单片机中断 - DelayMs(10); EnableWatchDog(); #ifdef ONBOARD_CMSIS_DAP Dap_Init(); #endif - DelayMs(10); UEP1_T_LEN = 0; //预使用发送长度一定要清空 UEP2_T_LEN = 0; //预使用发送长度一定要清空 UEP3_T_LEN = 0; diff --git a/usb/uart.c b/usb/uart.c index 7f9d83aa7..f50928bdb 100644 --- a/usb/uart.c +++ b/usb/uart.c @@ -111,13 +111,25 @@ static void uart_data_parser(void) uint8_t index = recv_buff[1]; uint8_t kplen = (command & 0x3F); if (index == 0) { + if (USB_MIS_ST & bUMS_SUSPEND) { + usb_state.is_busy = true; + CH554USBDevWakeup(); + usb_state.is_busy = false; + } else if(usb_state.is_ready) { // 通常键盘数据包 KeyboardGenericUpload(&recv_buff[2], kplen); + } last_success = true; } else { // 附加数据包 // 发过来的包的id和reportID一致,不用处理 + if (USB_MIS_ST & bUMS_SUSPEND) { + usb_state.is_busy = true; + CH554USBDevWakeup(); + usb_state.is_busy = false; + } else if(usb_state.is_ready) { KeyboardExtraUpload(&recv_buff[1], kplen + 1); + } last_success = true; } } @@ -133,8 +145,11 @@ static void uart_send_status() #ifdef PIN_CHARGING if (!IS_CHARGING) // 是否充满 data |= 0x02; +#else + if ((USB_MIS_ST & bUMS_SUSPEND) && RESET_KEEP) //曾经枚举成功,当前连接中断-->已连接但进入休眠 + data |= 0x02; #endif - if (usb_state.is_ready && !usb_state.is_sleep) // 是否连接主机 + if (RESET_KEEP) // 是否连接主机 data |= 0x04; if (usb_state.protocol) data |= 0x08; diff --git a/usb/usb_comm.h b/usb/usb_comm.h index e4a7f6ae9..fc72b941b 100644 --- a/usb/usb_comm.h +++ b/usb/usb_comm.h @@ -6,3 +6,4 @@ void KeyboardGenericUpload(uint8_t * packet, uint8_t len); void KeyboardExtraUpload(uint8_t * packet, uint8_t len); void ResponseConfigurePacket(uint8_t * packet, uint8_t len); +void CH554USBDevWakeup(); diff --git a/usb/usb_descriptor.h b/usb/usb_descriptor.h index 94d85a320..65d3f34a9 100644 --- a/usb/usb_descriptor.h +++ b/usb/usb_descriptor.h @@ -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 @@ -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