From f779f040e3204e1f0fd3bdbc5193513e66004fff Mon Sep 17 00:00:00 2001 From: IOsetting Date: Wed, 9 Feb 2022 01:14:06 +0800 Subject: [PATCH] opt: usb enum types --- demo/usb/usb_hid.c | 286 ++++++++++++++++++++++----------------------- include/fw_usb.h | 85 +++++++++----- 2 files changed, 197 insertions(+), 174 deletions(-) diff --git a/demo/usb/usb_hid.c b/demo/usb/usb_hid.c index d73732f..63e00b8 100644 --- a/demo/usb/usb_hid.c +++ b/demo/usb/usb_hid.c @@ -63,7 +63,7 @@ void USB_Init() USB_WriteReg(INTROUT1E, 0x3f); USB_WriteReg(INTRUSBE, 0x00); USB_WriteReg(POWER, 0x01); - Ep0Stage.bStage = EPIDLE; + Ep0Stage.bStage = USB_CtrlState_Idle; EXTI_USB_SetIntState(HAL_State_ON); } @@ -85,7 +85,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB) USB_WriteReg(INCSR1, INCLRDT); USB_SelectEndPoint(1); USB_WriteReg(OUTCSR1, OUTCLRDT); - Ep0Stage.bStage = EPIDLE; + Ep0Stage.bStage = USB_CtrlState_Idle; } if (intrin & EP0IF) { @@ -94,7 +94,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB) if (csr & STSTL) { USB_WriteReg(CSR0, csr & ~STSTL); - Ep0Stage.bStage = EPIDLE; + Ep0Stage.bStage = USB_CtrlState_Idle; } if (csr & SUEND) { @@ -102,146 +102,146 @@ INTERRUPT(USB_Routine, EXTI_VectUSB) } switch (Ep0Stage.bStage) { - case EPIDLE: - if (csr & OPRDY) - { - Ep0Stage.bStage = EPSTATUS; - USB_ReadFIFO(FIFO0, (uint8_t *)&usb_request); - ((uint8_t *)&Ep0Stage.wResidue)[0] = usb_request.wLength.bb.bh; - ((uint8_t *)&Ep0Stage.wResidue)[1] = usb_request.wLength.bb.bl; - switch (usb_request.bmRequestType & REQUEST_MASK) + case USB_CtrlState_Idle: + if (csr & OPRDY) { - case STANDARD_REQUEST: - switch (usb_request.bRequest) + Ep0Stage.bStage = USB_CtrlState_SettingUp; + USB_ReadFIFO(FIFO0, (uint8_t *)&usb_request); + ((uint8_t *)&Ep0Stage.wResidue)[0] = usb_request.wLength.bb.bh; + ((uint8_t *)&Ep0Stage.wResidue)[1] = usb_request.wLength.bb.bl; + switch (usb_request.bmRequestType & REQUEST_TYPE_MASK) { - case SET_ADDRESS: - USB_WriteReg(FADDR, usb_request.wValue.bb.bl); - break; - - case SET_CONFIG: - USB_SelectEndPoint(1); - USB_WriteReg(INCSR2, INMODEIN); - USB_WriteReg(INMAXP, 8); - USB_SelectEndPoint(1); - USB_WriteReg(INCSR2, INMODEOUT); - USB_WriteReg(OUTMAXP, 8); - USB_SelectEndPoint(0); - break; - - case GET_DESCRIPTOR: - Ep0Stage.bStage = EPDATAIN; - switch (usb_request.wValue.bb.bh) + case USB_RequestType_Standard: + switch (usb_request.bRequest) { - case DESC_DEVICE: - Ep0Stage.pData = DEVICEDESC; - len = sizeof(DEVICEDESC); - break; - - case DESC_CONFIG: - Ep0Stage.pData = CONFIGDESC; - len = sizeof(CONFIGDESC); - break; - - case DESC_STRING: - switch (usb_request.wValue.bb.bl) - { - case 0: - Ep0Stage.pData = LANGIDDESC; - len = sizeof(LANGIDDESC); + case USB_StdReq_SetAddress: + USB_WriteReg(FADDR, usb_request.wValue.bb.bl); break; - - case 1: - Ep0Stage.pData = MANUFACTDESC; - len = sizeof(MANUFACTDESC); - break; - - case 2: - Ep0Stage.pData = PRODUCTDESC; - len = sizeof(PRODUCTDESC); - break; - - default: - Ep0Stage.bStage = EPSTALL; - break; - } - break; - case DESC_HIDREPORT: - Ep0Stage.pData = HIDREPORTDESC; - len = sizeof(HIDREPORTDESC); - break; + case USB_StdReq_SetConfiguration: + USB_SelectEndPoint(1); + USB_WriteReg(INCSR2, INMODEIN); + USB_WriteReg(INMAXP, 8); + USB_SelectEndPoint(1); + USB_WriteReg(INCSR2, INMODEOUT); + USB_WriteReg(OUTMAXP, 8); + USB_SelectEndPoint(0); + break; + case USB_StdReq_GetDescriptor: + Ep0Stage.bStage = USB_CtrlState_DataIn; + switch (usb_request.wValue.bb.bh) + { + case USB_DescriptorType_Device: + Ep0Stage.pData = DEVICEDESC; + len = sizeof(DEVICEDESC); + break; + + case USB_DescriptorType_Configuration: + Ep0Stage.pData = CONFIGDESC; + len = sizeof(CONFIGDESC); + break; + + case USB_DescriptorType_String: + switch (usb_request.wValue.bb.bl) + { + case 0: + Ep0Stage.pData = LANGIDDESC; + len = sizeof(LANGIDDESC); + break; + + case 1: + Ep0Stage.pData = MANUFACTDESC; + len = sizeof(MANUFACTDESC); + break; + + case 2: + Ep0Stage.pData = PRODUCTDESC; + len = sizeof(PRODUCTDESC); + break; + + default: + Ep0Stage.bStage = USB_CtrlState_Stalled; + break; + } + break; + + case USB_DescriptorType_Report: + Ep0Stage.pData = HIDREPORTDESC; + len = sizeof(HIDREPORTDESC); + break; + + default: + Ep0Stage.bStage = USB_CtrlState_Stalled; + break; + } + if (len < Ep0Stage.wResidue) + { + Ep0Stage.wResidue = len; + } + break; + default: - Ep0Stage.bStage = EPSTALL; - break; + Ep0Stage.bStage = USB_CtrlState_Stalled; + break; } - if (len < Ep0Stage.wResidue) + break; + + case USB_RequestType_Class: + switch (usb_request.bRequest) { - Ep0Stage.wResidue = len; + case USB_HidReq_GetReport: + Ep0Stage.pData = HidFreature; + Ep0Stage.bStage = USB_CtrlState_DataIn; + break; + + case USB_HidReq_SetReport: + Ep0Stage.pData = HidFreature; + Ep0Stage.bStage = USB_CtrlState_DataOut; + break; + + case USB_HidReq_SetIdle: + break; + + case USB_HidReq_GetIdle: + case USB_HidReq_GetProtocol: + case USB_HidReq_SetProtocol: + default: + Ep0Stage.bStage = USB_CtrlState_Stalled; + break; } - break; + break; default: - Ep0Stage.bStage = EPSTALL; - break; + Ep0Stage.bStage = USB_CtrlState_Stalled; + break; } - break; - - case CLASS_REQUEST: - switch (usb_request.bRequest) - { - case GET_REPORT: - Ep0Stage.pData = HidFreature; - Ep0Stage.bStage = EPDATAIN; + + switch (Ep0Stage.bStage) + { + case USB_CtrlState_DataIn: + USB_WriteReg(CSR0, SOPRDY); + goto L_Ep0SendData; break; - - case SET_REPORT: - Ep0Stage.pData = HidFreature; - Ep0Stage.bStage = EPDATAOUT; + + case USB_CtrlState_DataOut: + USB_WriteReg(CSR0, SOPRDY); break; - - case SET_IDLE: + + case USB_CtrlState_SettingUp: + USB_WriteReg(CSR0, SOPRDY | DATEND); + Ep0Stage.bStage = USB_CtrlState_Idle; break; - - case GET_IDLE: - case GET_PROTOCOL: - case SET_PROTOCOL: - default: - Ep0Stage.bStage = EPSTALL; + + case USB_CtrlState_Stalled: + USB_WriteReg(CSR0, SOPRDY | SDSTL); + Ep0Stage.bStage = USB_CtrlState_Idle; break; - } - break; - - default: - Ep0Stage.bStage = EPSTALL; - break; + } } - - switch (Ep0Stage.bStage) - { - case EPDATAIN: - USB_WriteReg(CSR0, SOPRDY); - goto L_Ep0SendData; - break; - - case EPDATAOUT: - USB_WriteReg(CSR0, SOPRDY); - break; - - case EPSTATUS: - USB_WriteReg(CSR0, SOPRDY | DATEND); - Ep0Stage.bStage = EPIDLE; - break; - - case EPSTALL: - USB_WriteReg(CSR0, SOPRDY | SDSTL); - Ep0Stage.bStage = EPIDLE; - break; - } - } - break; - - case EPDATAIN: + break; + + case USB_CtrlState_DataIn: if (!(csr & IPRDY)) { L_Ep0SendData: @@ -252,32 +252,32 @@ INTERRUPT(USB_Routine, EXTI_VectUSB) if (Ep0Stage.wResidue == 0) { USB_WriteReg(CSR0, IPRDY | DATEND); - Ep0Stage.bStage = EPIDLE; + Ep0Stage.bStage = USB_CtrlState_Idle; } else { USB_WriteReg(CSR0, IPRDY); } } - break; - - case EPDATAOUT: - if (csr & OPRDY) - { - cnt = USB_ReadFIFO(FIFO0, Ep0Stage.pData); - Ep0Stage.wResidue -= cnt; - Ep0Stage.pData += cnt; - if (Ep0Stage.wResidue == 0) + break; + + case USB_CtrlState_DataOut: + if (csr & OPRDY) { - USB_WriteReg(CSR0, SOPRDY | DATEND); - Ep0Stage.bStage = EPIDLE; + cnt = USB_ReadFIFO(FIFO0, Ep0Stage.pData); + Ep0Stage.wResidue -= cnt; + Ep0Stage.pData += cnt; + if (Ep0Stage.wResidue == 0) + { + USB_WriteReg(CSR0, SOPRDY | DATEND); + Ep0Stage.bStage = USB_CtrlState_Idle; + } + else + { + USB_WriteReg(CSR0, SOPRDY); + } } - else - { - USB_WriteReg(CSR0, SOPRDY); - } - } - break; + break; } } diff --git a/include/fw_usb.h b/include/fw_usb.h index 3602f17..4a204b1 100644 --- a/include/fw_usb.h +++ b/include/fw_usb.h @@ -19,7 +19,7 @@ #include "fw_types.h" /** - * STC8H8K64U(LQFP48) (LQFP64) + * STC8H8K64U USB SFR */ #define FADDR 0x00 @@ -115,37 +115,60 @@ #define UTRKCTL 0x30 #define UTRKSTS 0x31 -#define EPIDLE 0 -#define EPSTATUS 1 -#define EPDATAIN 2 -#define EPDATAOUT 3 -#define EPSTALL -1 +typedef enum _CONTROL_STATE +{ + USB_CtrlState_Idle = 0x00, + USB_CtrlState_SettingUp = 0x01, + USB_CtrlState_DataIn = 0x02, + USB_CtrlState_DataOut = 0x03, + USB_CtrlState_Stalled = 0x04, +} USB_CtrlState_t; /* The state machine states of a control pipe */ -#define GET_STATUS 0x00 -#define CLEAR_FEATURE 0x01 -#define SET_FEATURE 0x03 -#define SET_ADDRESS 0x05 -#define GET_DESCRIPTOR 0x06 -#define SET_DESCRIPTOR 0x07 -#define GET_CONFIG 0x08 -#define SET_CONFIG 0x09 -#define GET_INTERFACE 0x0A -#define SET_INTERFACE 0x0B -#define SYNCH_FRAME 0x0C -#define GET_REPORT 0x01 -#define GET_IDLE 0x02 -#define GET_PROTOCOL 0x03 -#define SET_REPORT 0x09 -#define SET_IDLE 0x0A -#define SET_PROTOCOL 0x0B -#define DESC_DEVICE 0x01 -#define DESC_CONFIG 0x02 -#define DESC_STRING 0x03 -#define DESC_HIDREPORT 0x22 -#define STANDARD_REQUEST 0x00 -#define CLASS_REQUEST 0x20 -#define VENDOR_REQUEST 0x40 -#define REQUEST_MASK 0x60 +typedef enum +{ + USB_StdReq_GetStatus = 0x00, + USB_StdReq_ClearFeature = 0x01, + USB_StdReq_SetFeature = 0x03, + USB_StdReq_SetAddress = 0x05, + USB_StdReq_GetDescriptor = 0x06, + USB_StdReq_SetDescriptor = 0x07, + USB_StdReq_GetConfiguration = 0x08, + USB_StdReq_SetConfiguration = 0x09, + USB_StdReq_GetInterface = 0x0A, + USB_StdReq_SetInterface = 0x0B, + USB_StdReq_SynchFrame = 0x0C, +} USB_StdReq_t; + +typedef enum +{ + USB_HidReq_GetReport = 0x01, + USB_HidReq_GetIdle = 0x02, + USB_HidReq_GetProtocol = 0x03, + USB_HidReq_SetReport = 0x09, + USB_HidReq_SetIdle = 0x0A, + USB_HidReq_SetProtocol = 0x0B, +} USB_HidReq_t; + +typedef enum +{ + USB_DescriptorType_Device = 0x01, + USB_DescriptorType_Configuration = 0x02, + USB_DescriptorType_String = 0x03, + USB_DescriptorType_Interface = 0x04, + USB_DescriptorType_Endpoint = 0x05, + USB_DescriptorType_HID = 0x21, + USB_DescriptorType_Report = 0x22, + USB_DescriptorType_Physical = 0x23, +} USB_DescriptorType_t; + +#define REQUEST_TYPE_MASK 0x60 + +typedef enum +{ + USB_RequestType_Standard = 0x00, + USB_RequestType_Class = 0x20, + USB_RequestType_Vendor = 0x40, +} USB_RequestType_t; typedef enum {