Skip to content
Snippets Groups Projects
Commit 6466e3c5 authored by cnlohr's avatar cnlohr
Browse files

Things are working, but albeit with a bit of a cursed stint.

parent 85a1f95b
No related branches found
No related tags found
No related merge requests found
...@@ -54,20 +54,20 @@ static inline void fastcopy( uint8_t * dest, const uint8_t * src, int len ) ...@@ -54,20 +54,20 @@ static inline void fastcopy( uint8_t * dest, const uint8_t * src, int len )
static inline void fastcopy( uint8_t * dest, const uint8_t * src, int len ) static inline void fastcopy( uint8_t * dest, const uint8_t * src, int len )
{ {
DMA1_Channel7->CFGR = 0; DMA1_Channel7->CFGR = 0;
DMA1_Channel7->MADDR = src; DMA1_Channel7->MADDR = (uintptr_t)src;
DMA1_Channel7->PADDR = dest; DMA1_Channel7->PADDR = (uintptr_t)dest;
DMA1_Channel7->CNTR = (len+3)/4; DMA1_Channel7->CNTR = (len+3)/4;
DMA1_Channel7->CFGR = DMA1_Channel7->CFGR =
DMA_M2M_Enable | DMA_M2M_Enable |
DMA_DIR_PeripheralDST | DMA_DIR_PeripheralDST |
DMA_Priority_High | DMA_Priority_Low |
DMA_MemoryDataSize_Word | DMA_MemoryDataSize_Word |
DMA_PeripheralDataSize_Word | DMA_PeripheralDataSize_Word |
DMA_MemoryInc_Enable | DMA_MemoryInc_Enable |
DMA_PeripheralInc_Enable | DMA_PeripheralInc_Enable |
DMA_Mode_Normal | DMA_CFGR1_EN; DMA_Mode_Normal | DMA_CFGR1_EN;
//XXX TODO: It seems to work (unsafely) without this guard. //XXX TODO: Somehow, it seems to work (unsafely) without this.
while( DMA1_Channel7->CNTR ); //while( DMA1_Channel7->CNTR );
} }
#endif #endif
...@@ -78,7 +78,6 @@ void USBFS_InternalFinishSetup(); ...@@ -78,7 +78,6 @@ void USBFS_InternalFinishSetup();
void USBFS_IRQHandler() void USBFS_IRQHandler()
{ {
// Based on https://github.com/openwch/ch32x035/blob/main/EVT/EXAM/USB/USBFS/DEVICE/CompositeKM/User/ch32x035_usbfs_device.c // Based on https://github.com/openwch/ch32x035/blob/main/EVT/EXAM/USB/USBFS/DEVICE/CompositeKM/User/ch32x035_usbfs_device.c
// Combined FG + ST flag. // Combined FG + ST flag.
uint16_t intfgst = *(uint16_t*)(&USBFS->INT_FG); uint16_t intfgst = *(uint16_t*)(&USBFS->INT_FG);
int len = 0; int len = 0;
...@@ -267,12 +266,10 @@ void USBFS_IRQHandler() ...@@ -267,12 +266,10 @@ void USBFS_IRQHandler()
} }
len = ( USBFS_SetupReqLen >= DEF_USBD_UEP0_SIZE ) ? DEF_USBD_UEP0_SIZE : USBFS_SetupReqLen; len = ( USBFS_SetupReqLen >= DEF_USBD_UEP0_SIZE ) ? DEF_USBD_UEP0_SIZE : USBFS_SetupReqLen;
fastcopy( CTRL0BUFF, ctx->pUSBFS_Descr, len ); //memcpy( CTRL0BUFF, ctx->pUSBFS_Descr, len ); fastcopy( CTRL0BUFF, ctx->pUSBFS_Descr, len ); //memcpy( CTRL0BUFF, ctx->pUSBFS_Descr, len );
//memcpy( CTRL0BUFF, ctx->pUSBFS_Descr, len ); USBFS->UEP0_TX_LEN = len;
USBFS->UEP0_CTRL_H = USBFS_UEP_T_TOG | USBFS_UEP_T_RES_ACK;
ctx->pUSBFS_Descr += len; ctx->pUSBFS_Descr += len;
// USBFS_SetupReqLen -= len; goto replycomplete;
// USBFS->UEP0_TX_LEN = len;
// USBFS->UEP0_CTRL_H = USBFS_UEP_T_TOG | USBFS_UEP_T_RES_ACK;
// goto replycomplete;
break; break;
} }
...@@ -434,8 +431,8 @@ void USBFS_IRQHandler() ...@@ -434,8 +431,8 @@ void USBFS_IRQHandler()
} }
break; break;
// This might look a little weird, for error handling but it saves a nontrivial amount of storage, and simplifies // 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. // control flow to hard-abort here.
sendstall: sendstall:
// if one request not support, return stall. Stall means permanent error. // 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; USBFS->UEP0_CTRL_H = USBFS_UEP_T_TOG | USBFS_UEP_T_RES_STALL|USBFS_UEP_R_TOG | USBFS_UEP_R_RES_STALL;
...@@ -493,7 +490,10 @@ void USBFS_InternalFinishSetup() ...@@ -493,7 +490,10 @@ void USBFS_InternalFinishSetup()
USBFS->UEP4_1_MOD = RB_UEP1_TX_EN; USBFS->UEP4_1_MOD = RB_UEP1_TX_EN;
USBFS->UEP2_3_MOD = RB_UEP2_TX_EN; USBFS->UEP2_3_MOD = RB_UEP2_TX_EN;
USBFS->UEP567_MOD = 0; USBFS->UEP567_MOD = 0;
USBFS->UEP0_DMA = FSUSBCTX.EP0DMABuffer;
// This is really cursed. Somehow it doesn't explode.
// But, normally the USB wants a separate buffer here.
USBFS->UEP0_DMA = (uintptr_t)CTRL0BUFF;
UEP_CTRL_H(0) = USBFS_UEP_R_RES_ACK | USBFS_UEP_T_RES_NAK; UEP_CTRL_H(0) = USBFS_UEP_R_RES_ACK | USBFS_UEP_T_RES_NAK;
int i; int i;
...@@ -527,14 +527,6 @@ int FSUSBSetup() ...@@ -527,14 +527,6 @@ int FSUSBSetup()
USBFS->INT_FG = 0xff; USBFS->INT_FG = 0xff;
USBFS->UDEV_CTRL = RB_UD_PD_DIS | RB_UD_PORT_EN; USBFS->UDEV_CTRL = RB_UD_PD_DIS | RB_UD_PORT_EN;
char abuff[64];
memset( abuff, 0xaa, 64 );
char bbuff[64] = { 0 };
//fastcopy( bbuff, abuff, 35 );
USBDEBUG1 = bbuff[30];
//USBDEBUG0, USBDEBUG1, USBDEBUG2
// Go on-bus. // Go on-bus.
// From the TRM: // From the TRM:
......
...@@ -34,26 +34,18 @@ static inline void USBFS_SendEndpoint( int endp, int len ); ...@@ -34,26 +34,18 @@ static inline void USBFS_SendEndpoint( int endp, int len );
struct _USBState struct _USBState
{ {
/* Setup Request */ // Setup Request
volatile uint8_t USBFS_SetupReqCode; volatile uint8_t USBFS_SetupReqCode;
volatile uint8_t USBFS_SetupReqType; volatile uint8_t USBFS_SetupReqType;
volatile uint16_t USBFS_SetupReqLen; volatile uint16_t USBFS_SetupReqLen;
volatile uint32_t USBFS_IndexValue; volatile uint32_t USBFS_IndexValue;
/* USB Device Status */ // USB Device Status
volatile uint8_t USBFS_DevConfig; volatile uint8_t USBFS_DevConfig;
volatile uint8_t USBFS_DevAddr; volatile uint8_t USBFS_DevAddr;
volatile uint8_t USBFS_DevSleepStatus; volatile uint8_t USBFS_DevSleepStatus;
volatile uint8_t USBFS_DevEnumStatus; volatile uint8_t USBFS_DevEnumStatus;
// Endpoint buffers, only useful if using DMA.
// __attribute__ ((aligned(4))) uint8_t USBFS_EP_Buf[FUSB_CONFIG_EPS][64];
// __attribute__ ((aligned(4))) uint8_t USBFS_EP_Buf[3][64];
// #define pUSBFS_SetupReqPak ((tusb_control_request_t*)ctx->USBFS_EP_Buf[0])
// #define CTRL0BUFF (FSUSBCTX.USBFS_EP_Buf[0])
uint8_t EP0DMABuffer[64];
#define pUSBFS_SetupReqPak ((tusb_control_request_t*)USBFSD_UEP_BUF(0)) #define pUSBFS_SetupReqPak ((tusb_control_request_t*)USBFSD_UEP_BUF(0))
#define CTRL0BUFF USBFSD_UEP_BUF(0) #define CTRL0BUFF USBFSD_UEP_BUF(0)
...@@ -62,8 +54,8 @@ struct _USBState ...@@ -62,8 +54,8 @@ struct _USBState
uint8_t USBFS_HidProtocol[FUSB_HID_INTERFACES]; uint8_t USBFS_HidProtocol[FUSB_HID_INTERFACES];
#endif #endif
const uint8_t *pUSBFS_Descr;
volatile uint8_t USBFS_Endp_Busy[FUSB_CONFIG_EPS]; volatile uint8_t USBFS_Endp_Busy[FUSB_CONFIG_EPS];
const uint8_t *pUSBFS_Descr;
}; };
extern struct _USBState FSUSBCTX; extern struct _USBState FSUSBCTX;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment