opt: usb macros

This commit is contained in:
IOsetting 2022-02-08 02:45:04 +08:00
parent 78a88edd0c
commit dacf9d3e50
3 changed files with 35 additions and 31 deletions

View File

@ -13,6 +13,7 @@
// limitations under the License. // limitations under the License.
#include "fw_hal.h" #include "fw_hal.h"
#include <string.h>
__CODE uint8_t DEVICEDESC[18]; __CODE uint8_t DEVICEDESC[18];
__CODE uint8_t CONFIGDESC[41]; __CODE uint8_t CONFIGDESC[41];
@ -31,19 +32,18 @@ uint8_t CalCheckSum(uint8_t *buf, uint8_t len);
void main() void main()
{ {
uint8_t i;
GPIO_P3_SetMode(GPIO_Pin_0|GPIO_Pin_1, GPIO_Mode_Input_HIP); GPIO_P3_SetMode(GPIO_Pin_0|GPIO_Pin_1, GPIO_Mode_Input_HIP);
USB_Init(); USB_Init();
EA = 1; EXTI_Global_SetIntState(HAL_State_ON);
memset(HidOutput, 0x00, sizeof(HidOutput));
HidOutput[0] = 0xaa; HidOutput[0] = 0xaa;
HidOutput[1] = 0x55; HidOutput[1] = 0x55;
HidOutput[2] = 0x02; HidOutput[2] = 0x02;
for(i=3;i<64;i++) HidOutput[i] = 0; HidOutput[3] = 0x01;
HidOutput[3] = (uint8_t)(0x01); HidOutput[4] = 0x02;
HidOutput[4] = (uint8_t)(0x02); HidOutput[5] = 0x01;
HidOutput[5] = (uint8_t)(0x01); HidOutput[6] = 0x02;
HidOutput[6] = (uint8_t)(0x02);
HidOutput[7] = CalCheckSum(HidOutput,7); HidOutput[7] = CalCheckSum(HidOutput,7);
while (1); while (1);
@ -64,7 +64,8 @@ void USB_Init()
USB_WriteReg(INTRUSBE, 0x00); USB_WriteReg(INTRUSBE, 0x00);
USB_WriteReg(POWER, 0x01); USB_WriteReg(POWER, 0x01);
Ep0Stage.bStage = EPIDLE; Ep0Stage.bStage = EPIDLE;
IE2 |= 0x80;
EXTI_USB_SetIntState(HAL_State_ON);
} }
INTERRUPT(USB_Routine, EXTI_VectUSB) INTERRUPT(USB_Routine, EXTI_VectUSB)
@ -80,15 +81,15 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
introut = USB_ReadReg(INTROUT1); introut = USB_ReadReg(INTROUT1);
if (intrusb & RSTIF) if (intrusb & RSTIF)
{ {
USB_WriteReg(INDEX, 1); USB_SelectEndPoint(1);
USB_WriteReg(INCSR1, INCLRDT); USB_WriteReg(INCSR1, INCLRDT);
USB_WriteReg(INDEX, 1); USB_SelectEndPoint(1);
USB_WriteReg(OUTCSR1, OUTCLRDT); USB_WriteReg(OUTCSR1, OUTCLRDT);
Ep0Stage.bStage = EPIDLE; Ep0Stage.bStage = EPIDLE;
} }
if (intrin & EP0IF) if (intrin & EP0IF)
{ {
USB_WriteReg(INDEX, 0); USB_SelectEndPoint(0);
csr = USB_ReadReg(CSR0); csr = USB_ReadReg(CSR0);
if (csr & STSTL) if (csr & STSTL)
{ {
@ -118,13 +119,13 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
break; break;
case SET_CONFIG: case SET_CONFIG:
USB_WriteReg(INDEX, 1); USB_SelectEndPoint(1);
USB_WriteReg(INCSR2, INMODEIN); USB_WriteReg(INCSR2, INMODEIN);
USB_WriteReg(INMAXP, 8); USB_WriteReg(INMAXP, 8);
USB_WriteReg(INDEX, 1); USB_SelectEndPoint(1);
USB_WriteReg(INCSR2, INMODEOUT); USB_WriteReg(INCSR2, INMODEOUT);
USB_WriteReg(OUTMAXP, 8); USB_WriteReg(OUTMAXP, 8);
USB_WriteReg(INDEX, 0); USB_SelectEndPoint(0);
break; break;
case GET_DESCRIPTOR: case GET_DESCRIPTOR:
@ -282,7 +283,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
if (intrin & EP1INIF) if (intrin & EP1INIF)
{ {
USB_WriteReg(INDEX, 1); USB_SelectEndPoint(1);
csr = USB_ReadReg(INCSR1); csr = USB_ReadReg(INCSR1);
if (csr & INSTSTL) if (csr & INSTSTL)
{ {
@ -296,7 +297,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
if (introut & EP1OUTIF) if (introut & EP1OUTIF)
{ {
USB_WriteReg(INDEX, 1); USB_SelectEndPoint(1);
csr = USB_ReadReg(OUTCSR1); csr = USB_ReadReg(OUTCSR1);
if (csr & OUTSTSTL) if (csr & OUTSTSTL)
{ {
@ -309,7 +310,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
if((HidInput[0]==0xaa) && (HidInput[1]==0x55) && (HidInput[2]==0x01)) if((HidInput[0]==0xaa) && (HidInput[1]==0x55) && (HidInput[2]==0x01))
{ {
USB_WriteReg(INDEX, 1); USB_SelectEndPoint(1);
USB_WriteFIFO(FIFO1, HidOutput, 64); USB_WriteFIFO(FIFO1, HidOutput, 64);
USB_WriteReg(INCSR1, INIPRDY); USB_WriteReg(INCSR1, INIPRDY);
} }

View File

@ -200,6 +200,11 @@ typedef enum
*/ */
#define USB_SetDm(__STATE__) SFR_ASSIGN(USBCON, 0, __STATE__) #define USB_SetDm(__STATE__) SFR_ASSIGN(USBCON, 0, __STATE__)
#define USB_IsBusy() (USBADR & 0x80)
#define USB_SetAddrForRead(__ADDR__) (USBADR = (__ADDR__) | 0x80)
#define USB_SetAddrForWrite(__ADDR__) (USBADR = (__ADDR__) & 0x7F)
#define USB_SelectEndPoint(__INDEX__) USB_WriteReg(INDEX, __INDEX__)
typedef union typedef union

View File

@ -3,25 +3,23 @@
uint8_t USB_ReadReg(uint8_t addr) uint8_t USB_ReadReg(uint8_t addr)
{ {
uint8_t dat; while (USB_IsBusy());
while (USBADR & 0x80); USB_SetAddrForRead(addr);
USBADR = addr | 0x80;
while (USBADR & 0x80); while (USB_IsBusy());
dat = USBDAT; return USBDAT;
return dat;
} }
void USB_WriteReg(uint8_t addr, uint8_t dat) void USB_WriteReg(uint8_t addr, uint8_t dat)
{ {
while (USBADR & 0x80); while (USB_IsBusy());
USBADR = addr & 0x7f; USB_SetAddrForWrite(addr);
USBDAT = dat; USBDAT = dat;
} }
uint8_t USB_ReadFIFO(uint8_t fifo, uint8_t *pdat) uint8_t USB_ReadFIFO(uint8_t fifo, uint8_t *pdat)
{ {
uint8_t cnt; uint8_t cnt, ret;
uint8_t ret;
ret = cnt = USB_ReadReg(COUNT0); ret = cnt = USB_ReadReg(COUNT0);
while (cnt--) while (cnt--)
{ {