Skip to content

Commit

Permalink
【USB】完善休眠唤醒支持
Browse files Browse the repository at this point in the history
1. 利用RESET_KEEP寄存器判断主机状态
USB枚举完成代表接入了正确的主机,此时将RESET_KEEP寄存器写入1,在重新断电上电RESET_KEEP清零之前,可认为CH552一直正确接入主机上
2. 未枚举完成不发送按键信息,以修正休眠唤醒过程中,卡按键的问题
3. 将休眠状态发送给主机(兼容老版本的PIN_CHARGING检测),主机可通过此信息关闭指示灯
  • Loading branch information
genokolar committed Oct 26, 2024
1 parent 86b271b commit 2d9bc42
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
6 changes: 1 addition & 5 deletions usb/endpoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "endpoints.h"

#define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE
#define REMOTE_WAKE true

/**
* @brief 端点0/4缓冲区。
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -225,7 +225,6 @@ void EP0_SETUP()
}
break;
}
#if REMOTE_WAKE
case USB_REQ_TO_DEVICE:
if (UsbSetupBuf->wValue != 0x01) {
// 操作失败
Expand All @@ -235,7 +234,6 @@ void EP0_SETUP()
// 设置唤醒使能标志
usb_state.remote_wake = false;
break;
#endif
default: //unsupport
SETUP_STALL();
return;
Expand Down Expand Up @@ -276,7 +274,6 @@ void EP0_SETUP()
}
break;
}
#if REMOTE_WAKE
case USB_REQ_TO_DEVICE: {
if (UsbSetupBuf->wValue != 0x01) {
SETUP_STALL();
Expand All @@ -286,7 +283,6 @@ void EP0_SETUP()
usb_state.remote_wake = true;
break;
}
#endif
default:
SETUP_STALL();
return;
Expand Down
16 changes: 4 additions & 12 deletions usb/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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中断处理函数
Expand All @@ -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;
}
}

/**
Expand All @@ -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;
}
}

/**
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion usb/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions usb/usb_comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit 2d9bc42

Please sign in to comment.