opt: usb enum types

This commit is contained in:
IOsetting 2022-02-09 01:14:06 +08:00
parent dacf9d3e50
commit f779f040e3
2 changed files with 197 additions and 174 deletions

View File

@ -63,7 +63,7 @@ void USB_Init()
USB_WriteReg(INTROUT1E, 0x3f); USB_WriteReg(INTROUT1E, 0x3f);
USB_WriteReg(INTRUSBE, 0x00); USB_WriteReg(INTRUSBE, 0x00);
USB_WriteReg(POWER, 0x01); USB_WriteReg(POWER, 0x01);
Ep0Stage.bStage = EPIDLE; Ep0Stage.bStage = USB_CtrlState_Idle;
EXTI_USB_SetIntState(HAL_State_ON); EXTI_USB_SetIntState(HAL_State_ON);
} }
@ -85,7 +85,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
USB_WriteReg(INCSR1, INCLRDT); USB_WriteReg(INCSR1, INCLRDT);
USB_SelectEndPoint(1); USB_SelectEndPoint(1);
USB_WriteReg(OUTCSR1, OUTCLRDT); USB_WriteReg(OUTCSR1, OUTCLRDT);
Ep0Stage.bStage = EPIDLE; Ep0Stage.bStage = USB_CtrlState_Idle;
} }
if (intrin & EP0IF) if (intrin & EP0IF)
{ {
@ -94,7 +94,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
if (csr & STSTL) if (csr & STSTL)
{ {
USB_WriteReg(CSR0, csr & ~STSTL); USB_WriteReg(CSR0, csr & ~STSTL);
Ep0Stage.bStage = EPIDLE; Ep0Stage.bStage = USB_CtrlState_Idle;
} }
if (csr & SUEND) if (csr & SUEND)
{ {
@ -102,23 +102,23 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
} }
switch (Ep0Stage.bStage) switch (Ep0Stage.bStage)
{ {
case EPIDLE: case USB_CtrlState_Idle:
if (csr & OPRDY) if (csr & OPRDY)
{ {
Ep0Stage.bStage = EPSTATUS; Ep0Stage.bStage = USB_CtrlState_SettingUp;
USB_ReadFIFO(FIFO0, (uint8_t *)&usb_request); USB_ReadFIFO(FIFO0, (uint8_t *)&usb_request);
((uint8_t *)&Ep0Stage.wResidue)[0] = usb_request.wLength.bb.bh; ((uint8_t *)&Ep0Stage.wResidue)[0] = usb_request.wLength.bb.bh;
((uint8_t *)&Ep0Stage.wResidue)[1] = usb_request.wLength.bb.bl; ((uint8_t *)&Ep0Stage.wResidue)[1] = usb_request.wLength.bb.bl;
switch (usb_request.bmRequestType & REQUEST_MASK) switch (usb_request.bmRequestType & REQUEST_TYPE_MASK)
{ {
case STANDARD_REQUEST: case USB_RequestType_Standard:
switch (usb_request.bRequest) switch (usb_request.bRequest)
{ {
case SET_ADDRESS: case USB_StdReq_SetAddress:
USB_WriteReg(FADDR, usb_request.wValue.bb.bl); USB_WriteReg(FADDR, usb_request.wValue.bb.bl);
break; break;
case SET_CONFIG: case USB_StdReq_SetConfiguration:
USB_SelectEndPoint(1); USB_SelectEndPoint(1);
USB_WriteReg(INCSR2, INMODEIN); USB_WriteReg(INCSR2, INMODEIN);
USB_WriteReg(INMAXP, 8); USB_WriteReg(INMAXP, 8);
@ -128,21 +128,21 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
USB_SelectEndPoint(0); USB_SelectEndPoint(0);
break; break;
case GET_DESCRIPTOR: case USB_StdReq_GetDescriptor:
Ep0Stage.bStage = EPDATAIN; Ep0Stage.bStage = USB_CtrlState_DataIn;
switch (usb_request.wValue.bb.bh) switch (usb_request.wValue.bb.bh)
{ {
case DESC_DEVICE: case USB_DescriptorType_Device:
Ep0Stage.pData = DEVICEDESC; Ep0Stage.pData = DEVICEDESC;
len = sizeof(DEVICEDESC); len = sizeof(DEVICEDESC);
break; break;
case DESC_CONFIG: case USB_DescriptorType_Configuration:
Ep0Stage.pData = CONFIGDESC; Ep0Stage.pData = CONFIGDESC;
len = sizeof(CONFIGDESC); len = sizeof(CONFIGDESC);
break; break;
case DESC_STRING: case USB_DescriptorType_String:
switch (usb_request.wValue.bb.bl) switch (usb_request.wValue.bb.bl)
{ {
case 0: case 0:
@ -161,18 +161,18 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
break; break;
default: default:
Ep0Stage.bStage = EPSTALL; Ep0Stage.bStage = USB_CtrlState_Stalled;
break; break;
} }
break; break;
case DESC_HIDREPORT: case USB_DescriptorType_Report:
Ep0Stage.pData = HIDREPORTDESC; Ep0Stage.pData = HIDREPORTDESC;
len = sizeof(HIDREPORTDESC); len = sizeof(HIDREPORTDESC);
break; break;
default: default:
Ep0Stage.bStage = EPSTALL; Ep0Stage.bStage = USB_CtrlState_Stalled;
break; break;
} }
if (len < Ep0Stage.wResidue) if (len < Ep0Stage.wResidue)
@ -182,66 +182,66 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
break; break;
default: default:
Ep0Stage.bStage = EPSTALL; Ep0Stage.bStage = USB_CtrlState_Stalled;
break; break;
} }
break; break;
case CLASS_REQUEST: case USB_RequestType_Class:
switch (usb_request.bRequest) switch (usb_request.bRequest)
{ {
case GET_REPORT: case USB_HidReq_GetReport:
Ep0Stage.pData = HidFreature; Ep0Stage.pData = HidFreature;
Ep0Stage.bStage = EPDATAIN; Ep0Stage.bStage = USB_CtrlState_DataIn;
break; break;
case SET_REPORT: case USB_HidReq_SetReport:
Ep0Stage.pData = HidFreature; Ep0Stage.pData = HidFreature;
Ep0Stage.bStage = EPDATAOUT; Ep0Stage.bStage = USB_CtrlState_DataOut;
break; break;
case SET_IDLE: case USB_HidReq_SetIdle:
break; break;
case GET_IDLE: case USB_HidReq_GetIdle:
case GET_PROTOCOL: case USB_HidReq_GetProtocol:
case SET_PROTOCOL: case USB_HidReq_SetProtocol:
default: default:
Ep0Stage.bStage = EPSTALL; Ep0Stage.bStage = USB_CtrlState_Stalled;
break; break;
} }
break; break;
default: default:
Ep0Stage.bStage = EPSTALL; Ep0Stage.bStage = USB_CtrlState_Stalled;
break; break;
} }
switch (Ep0Stage.bStage) switch (Ep0Stage.bStage)
{ {
case EPDATAIN: case USB_CtrlState_DataIn:
USB_WriteReg(CSR0, SOPRDY); USB_WriteReg(CSR0, SOPRDY);
goto L_Ep0SendData; goto L_Ep0SendData;
break; break;
case EPDATAOUT: case USB_CtrlState_DataOut:
USB_WriteReg(CSR0, SOPRDY); USB_WriteReg(CSR0, SOPRDY);
break; break;
case EPSTATUS: case USB_CtrlState_SettingUp:
USB_WriteReg(CSR0, SOPRDY | DATEND); USB_WriteReg(CSR0, SOPRDY | DATEND);
Ep0Stage.bStage = EPIDLE; Ep0Stage.bStage = USB_CtrlState_Idle;
break; break;
case EPSTALL: case USB_CtrlState_Stalled:
USB_WriteReg(CSR0, SOPRDY | SDSTL); USB_WriteReg(CSR0, SOPRDY | SDSTL);
Ep0Stage.bStage = EPIDLE; Ep0Stage.bStage = USB_CtrlState_Idle;
break; break;
} }
} }
break; break;
case EPDATAIN: case USB_CtrlState_DataIn:
if (!(csr & IPRDY)) if (!(csr & IPRDY))
{ {
L_Ep0SendData: L_Ep0SendData:
@ -252,7 +252,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
if (Ep0Stage.wResidue == 0) if (Ep0Stage.wResidue == 0)
{ {
USB_WriteReg(CSR0, IPRDY | DATEND); USB_WriteReg(CSR0, IPRDY | DATEND);
Ep0Stage.bStage = EPIDLE; Ep0Stage.bStage = USB_CtrlState_Idle;
} }
else else
{ {
@ -261,7 +261,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
} }
break; break;
case EPDATAOUT: case USB_CtrlState_DataOut:
if (csr & OPRDY) if (csr & OPRDY)
{ {
cnt = USB_ReadFIFO(FIFO0, Ep0Stage.pData); cnt = USB_ReadFIFO(FIFO0, Ep0Stage.pData);
@ -270,7 +270,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
if (Ep0Stage.wResidue == 0) if (Ep0Stage.wResidue == 0)
{ {
USB_WriteReg(CSR0, SOPRDY | DATEND); USB_WriteReg(CSR0, SOPRDY | DATEND);
Ep0Stage.bStage = EPIDLE; Ep0Stage.bStage = USB_CtrlState_Idle;
} }
else else
{ {

View File

@ -19,7 +19,7 @@
#include "fw_types.h" #include "fw_types.h"
/** /**
* STC8H8K64U(LQFP48) (LQFP64) * STC8H8K64U USB SFR
*/ */
#define FADDR 0x00 #define FADDR 0x00
@ -115,37 +115,60 @@
#define UTRKCTL 0x30 #define UTRKCTL 0x30
#define UTRKSTS 0x31 #define UTRKSTS 0x31
#define EPIDLE 0 typedef enum _CONTROL_STATE
#define EPSTATUS 1 {
#define EPDATAIN 2 USB_CtrlState_Idle = 0x00,
#define EPDATAOUT 3 USB_CtrlState_SettingUp = 0x01,
#define EPSTALL -1 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 typedef enum
#define CLEAR_FEATURE 0x01 {
#define SET_FEATURE 0x03 USB_StdReq_GetStatus = 0x00,
#define SET_ADDRESS 0x05 USB_StdReq_ClearFeature = 0x01,
#define GET_DESCRIPTOR 0x06 USB_StdReq_SetFeature = 0x03,
#define SET_DESCRIPTOR 0x07 USB_StdReq_SetAddress = 0x05,
#define GET_CONFIG 0x08 USB_StdReq_GetDescriptor = 0x06,
#define SET_CONFIG 0x09 USB_StdReq_SetDescriptor = 0x07,
#define GET_INTERFACE 0x0A USB_StdReq_GetConfiguration = 0x08,
#define SET_INTERFACE 0x0B USB_StdReq_SetConfiguration = 0x09,
#define SYNCH_FRAME 0x0C USB_StdReq_GetInterface = 0x0A,
#define GET_REPORT 0x01 USB_StdReq_SetInterface = 0x0B,
#define GET_IDLE 0x02 USB_StdReq_SynchFrame = 0x0C,
#define GET_PROTOCOL 0x03 } USB_StdReq_t;
#define SET_REPORT 0x09
#define SET_IDLE 0x0A typedef enum
#define SET_PROTOCOL 0x0B {
#define DESC_DEVICE 0x01 USB_HidReq_GetReport = 0x01,
#define DESC_CONFIG 0x02 USB_HidReq_GetIdle = 0x02,
#define DESC_STRING 0x03 USB_HidReq_GetProtocol = 0x03,
#define DESC_HIDREPORT 0x22 USB_HidReq_SetReport = 0x09,
#define STANDARD_REQUEST 0x00 USB_HidReq_SetIdle = 0x0A,
#define CLASS_REQUEST 0x20 USB_HidReq_SetProtocol = 0x0B,
#define VENDOR_REQUEST 0x40 } USB_HidReq_t;
#define REQUEST_MASK 0x60
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 typedef enum
{ {