Skip to content

Commit

Permalink
Clean up some control flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlohr committed Apr 21, 2024
1 parent 15f6149 commit 1dca24b
Show file tree
Hide file tree
Showing 5 changed files with 1,121 additions and 66 deletions.
100 changes: 44 additions & 56 deletions examples_x035/usbdevice.incomplete/fsusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void USBFS_IRQHandler()

// Combined FG + ST flag.
uint16_t intfgst = *(uint16_t*)(&USBFS->INT_FG);
int len = 0, errflag = 0;
int len = 0;
struct _USBState * ctx = &FSUSBCTX;
GPIOA->BSHR = 1;

Expand Down Expand Up @@ -71,7 +71,7 @@ void USBFS_IRQHandler()
{
case USB_GET_DESCRIPTOR:
len = ctx->USBFS_SetupReqLen >= DEF_USBD_UEP0_SIZE ? DEF_USBD_UEP0_SIZE : ctx->USBFS_SetupReqLen;
memcpy( ctx->USBFS_EP0_Buf, ctx->pUSBFS_Descr, len );
memcpy( ctx->USBFS_EP_Buf[0], ctx->pUSBFS_Descr, len );
ctx->USBFS_SetupReqLen -= len;
ctx->pUSBFS_Descr += len;
USBFS->UEP0_TX_LEN = len;
Expand Down Expand Up @@ -160,10 +160,10 @@ void USBFS_IRQHandler()
int USBFS_SetupReqIndex = pUSBFS_SetupReqPak->wIndex;
int USBFS_IndexValue = FSUSBCTX.USBFS_IndexValue = ( pUSBFS_SetupReqPak->wIndex << 16 ) | pUSBFS_SetupReqPak->wValue;
len = 0;
errflag = 0;

if( ( USBFS_SetupReqType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD )
{
#if FUSB_HID_INTERFACES > 0
if( ( USBFS_SetupReqType & USB_REQ_TYP_MASK ) == USB_REQ_TYP_CLASS )
{
/* Class Request */
Expand All @@ -173,43 +173,36 @@ void USBFS_IRQHandler()
break;

case HID_SET_IDLE:
if( USBFS_SetupReqIndex < 0x02 )
if( USBFS_SetupReqIndex < FUSB_HID_INTERFACES )
FSUSBCTX.USBFS_HidIdle[ USBFS_SetupReqIndex ] = (uint8_t)( USBFS_IndexValue >> 8 );
else
errflag = 0xFF;
break;
case HID_SET_PROTOCOL:
if ( USBFS_SetupReqIndex < 0x02 )
if ( USBFS_SetupReqIndex < FUSB_HID_INTERFACES )
FSUSBCTX.USBFS_HidProtocol[USBFS_SetupReqIndex] = (uint8_t)USBFS_IndexValue;
else
errflag = 0xFF;
break;

case HID_GET_IDLE:
if( USBFS_SetupReqIndex < 0x02 )
if( USBFS_SetupReqIndex < FUSB_HID_INTERFACES )
{
FSUSBCTX.USBFS_EP0_Buf[ 0 ] = FSUSBCTX.USBFS_HidIdle[ USBFS_SetupReqIndex ];
FSUSBCTX.USBFS_EP_Buf[0][0] = FSUSBCTX.USBFS_HidIdle[ USBFS_SetupReqIndex ];
len = 1;
}
else
errflag = 0xFF;
break;

case HID_GET_PROTOCOL:
if( USBFS_SetupReqIndex < 0x02 )
if( USBFS_SetupReqIndex < FUSB_HID_INTERFACES )
{
FSUSBCTX.USBFS_EP0_Buf[ 0 ] = FSUSBCTX.USBFS_HidProtocol[ USBFS_SetupReqIndex ];
FSUSBCTX.USBFS_EP_Buf[0][0] = FSUSBCTX.USBFS_HidProtocol[ USBFS_SetupReqIndex ];
len = 1;
}
else
errflag = 0xFF;
break;

default:
errflag = 0xFF;
goto reporterror;
break;
}
}
#endif
}
else
{
Expand All @@ -233,7 +226,7 @@ void USBFS_IRQHandler()
}
if( e == e_end )
{
errflag = 0xFF;
goto reporterror;
}

/* Copy Descriptors to Endp0 DMA buffer */
Expand All @@ -242,7 +235,7 @@ void USBFS_IRQHandler()
USBFS_SetupReqLen = len;
}
len = ( USBFS_SetupReqLen >= DEF_USBD_UEP0_SIZE ) ? DEF_USBD_UEP0_SIZE : USBFS_SetupReqLen;
memcpy( ctx->USBFS_EP0_Buf, ctx->pUSBFS_Descr, len );
memcpy( ctx->USBFS_EP_Buf[0], ctx->pUSBFS_Descr, len );
ctx->pUSBFS_Descr += len;
break;
}
Expand All @@ -254,7 +247,7 @@ void USBFS_IRQHandler()

/* Get usb configuration now set */
case USB_GET_CONFIGURATION:
ctx->USBFS_EP0_Buf[ 0 ] = ctx->USBFS_DevConfig;
ctx->USBFS_EP_Buf[0][0] = ctx->USBFS_DevConfig;
if( ctx->USBFS_SetupReqLen > 1 )
{
ctx->USBFS_SetupReqLen = 1;
Expand All @@ -279,7 +272,7 @@ void USBFS_IRQHandler()
}
else
{
errflag = 0xFF;
goto reporterror;
}
}
else if( ( USBFS_SetupReqType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP )
Expand All @@ -300,18 +293,18 @@ void USBFS_IRQHandler()
break;

default:
errflag = 0xFF;
goto reporterror;
break;
}
}
else
{
errflag = 0xFF;
goto reporterror;
}
}
else
{
errflag = 0xFF;
goto reporterror;
}
break;

Expand All @@ -329,7 +322,7 @@ void USBFS_IRQHandler()
else
#endif
{
errflag = 0xFF;
goto reporterror;
}
}
else if( ( USBFS_SetupReqType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP )
Expand All @@ -348,24 +341,24 @@ void USBFS_IRQHandler()
break;

default:
errflag = 0xFF;
goto reporterror;
break;
}
}
else
{
errflag = 0xFF;
goto reporterror;
}
}
else
{
errflag = 0xFF;
goto reporterror;
}
break;

/* This request allows the host to select another setting for the specified interface */
case USB_GET_INTERFACE:
ctx->USBFS_EP0_Buf[ 0 ] = 0x00;
ctx->USBFS_EP_Buf[0][0] = 0x00;
if( USBFS_SetupReqLen > 1 )
{
USBFS_SetupReqLen = 1;
Expand All @@ -377,17 +370,17 @@ void USBFS_IRQHandler()

/* host get status of specified device/interface/end-points */
case USB_GET_STATUS:
ctx->USBFS_EP0_Buf[ 0 ] = 0x00;
ctx->USBFS_EP0_Buf[ 1 ] = 0x00;
ctx->USBFS_EP_Buf[0][0] = 0x00;
ctx->USBFS_EP_Buf[0][1] = 0x00;
if( ( USBFS_SetupReqType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_DEVICE )
{
if( ctx->USBFS_DevSleepStatus & 0x01 )
{
ctx->USBFS_EP0_Buf[ 0 ] = 0x02;
ctx->USBFS_EP_Buf[0][0] = 0x02;
}
else
{
ctx->USBFS_EP0_Buf[ 0 ] = 0x00;
ctx->USBFS_EP_Buf[0][0] = 0x00;
}
}
else if( ( USBFS_SetupReqType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP )
Expand All @@ -396,24 +389,24 @@ void USBFS_IRQHandler()
{
if( ( USBFS->UEP1_CTRL_H & USBFS_UEP_T_RES_MASK ) == USBFS_UEP_T_RES_STALL )
{
ctx->USBFS_EP0_Buf[ 0 ] = 0x01;
ctx->USBFS_EP_Buf[0][0] = 0x01;
}
}
else if( (uint8_t)( USBFS_SetupReqIndex & 0xFF ) == ( DEF_UEP_IN | DEF_UEP2 ) )
{
if( ( USBFS->UEP2_CTRL_H & USBFS_UEP_T_RES_MASK ) == USBFS_UEP_T_RES_STALL )
{
ctx->USBFS_EP0_Buf[ 0 ] = 0x01;
ctx->USBFS_EP_Buf[0][0] = 0x01;
}
}
else
{
errflag = 0xFF;
goto reporterror;
}
}
else
{
errflag = 0xFF;
goto reporterror;
}
if( USBFS_SetupReqLen > 2 )
{
Expand All @@ -422,18 +415,12 @@ void USBFS_IRQHandler()
break;

default:
errflag = 0xFF;
goto reporterror;
break;
}
}

/* errflag = 0xFF means a request not support or some errors occurred, else correct */
if( errflag == 0xFF )
{
/* if one request not support, return stall */
USBFS->UEP0_CTRL_H = USBFS_UEP_T_TOG | USBFS_UEP_T_RES_STALL|USBFS_UEP_R_TOG | USBFS_UEP_R_RES_STALL;
}
else

{
/* end-point 0 data Tx/Rx */
if( USBFS_SetupReqType & DEF_UEP_IN )
Expand All @@ -458,13 +445,21 @@ void USBFS_IRQHandler()
}
break;

// This might look a little weird, for error handling but it saves a nontrivial amount of storage, and simplifies
// control flow to hard-abort here.
reporterror:
// if one request not support, return stall. Stall means permanent error.
USBFS->UEP0_CTRL_H = USBFS_UEP_T_TOG | USBFS_UEP_T_RES_STALL|USBFS_UEP_R_TOG | USBFS_UEP_R_RES_STALL;

/* Sof pack processing */
case CUIS_TOKEN_SOF:
break;

default :
break;
}


}
else if( intfgst & CRB_UIF_BUS_RST )
{
Expand Down Expand Up @@ -508,9 +503,9 @@ void USBFS_Device_Endp_Init()
USBFS->UEP2_3_MOD = RB_UEP2_TX_EN;
USBFS->UEP567_MOD = 0;

USBFS->UEP0_DMA = (intptr_t)FSUSBCTX.USBFS_EP0_Buf;
USBFS->UEP1_DMA = (intptr_t)FSUSBCTX.USBFS_EP1_Buf;
USBFS->UEP2_DMA = (intptr_t)FSUSBCTX.USBFS_EP2_Buf;
USBFS->UEP0_DMA = (intptr_t)FSUSBCTX.USBFS_EP_Buf[0];
USBFS->UEP1_DMA = (intptr_t)FSUSBCTX.USBFS_EP_Buf[1];
USBFS->UEP2_DMA = (intptr_t)FSUSBCTX.USBFS_EP_Buf[2];

USBFS->UEP0_CTRL_H = USBFS_UEP_R_RES_ACK | USBFS_UEP_T_RES_NAK;
USBFS->UEP1_CTRL_H = USBFS_UEP_T_RES_NAK;
Expand All @@ -523,13 +518,6 @@ void USBFS_Device_Endp_Init()
}
}


void USBFS_Poll()
{
//USBDEBUG2 = USBFS->INT_ST;//EP0_DATA[1];
//USBDEBUG1 = USBFS->MIS_ST;
}

int FSUSBSetup()
{
RCC->APB2PCENR |= RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC;
Expand Down
14 changes: 6 additions & 8 deletions examples_x035/usbdevice.incomplete/fsusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
extern uint32_t USBDEBUG0, USBDEBUG1, USBDEBUG2;

int FSUSBSetup();
void USBFS_Poll();
uint8_t USBFS_Endp_DataUp(uint8_t endp, uint8_t *pbuf, uint16_t len, uint8_t mod);


Expand All @@ -28,19 +27,18 @@ struct _USBState
volatile uint8_t USBFS_DevEnumStatus;

/* Endpoint Buffer */
__attribute__ ((aligned(4))) uint8_t USBFS_EP0_Buf[64];
__attribute__ ((aligned(4))) uint8_t USBFS_EP1_Buf[64];
__attribute__ ((aligned(4))) uint8_t USBFS_EP2_Buf[64];
#define pUSBFS_SetupReqPak ((tusb_control_request_t*)ctx->USBFS_EP0_Buf)
__attribute__ ((aligned(4))) uint8_t USBFS_EP_Buf[FUSB_CONFIG_EPS][64];

#define pUSBFS_SetupReqPak ((tusb_control_request_t*)ctx->USBFS_EP_Buf[0])

int USBFS_HidIdle[2];
int USBFS_HidProtocol[2];

uint8_t USBFS_HidIdle[FUSB_HID_INTERFACES];
uint8_t USBFS_HidProtocol[FUSB_HID_INTERFACES];

const uint8_t *pUSBFS_Descr;

/* USB IN Endpoint Busy Flag */
volatile uint8_t USBFS_Endp_Busy[UNUM_EP];
volatile uint8_t USBFS_Endp_Busy[FUSB_CONFIG_EPS];
};

extern struct _USBState FSUSBCTX;
Expand Down
Loading

0 comments on commit 1dca24b

Please sign in to comment.