Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【USB】支持电脑休眠唤醒功能 #117

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 12 additions & 28 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 @@ -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 上传键盘通常按键数据包
*
Expand All @@ -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;
}

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

/**
Expand Down Expand Up @@ -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;
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.is_sleep) // 是否连接主机
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();
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