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 1c1f6339a..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中断处理函数 @@ -69,14 +72,10 @@ void KeyboardGenericUpload(uint8_t* packet, uint8_t len) { if (len != 8) return; - 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; - } } /** @@ -87,14 +86,10 @@ void KeyboardGenericUpload(uint8_t* packet, uint8_t len) */ void KeyboardExtraUpload(uint8_t* packet, uint8_t len) { - 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; - } } /** @@ -225,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 4699a382c..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.remote_wake) // 是否连接主机 + 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();