📄 host_811.c
字号:
#include "host_811.h"
//All usb variable defined at here
//////////////////////////////////////////////////////////////////////////
#pragma abs_address: 0x8000
UINT8 bBUF[256]; // at 0x2000 for general descriptors data
USBDEV uDev; // Multiple USB devices attributes, Max 5 devices
Control USB_Control; //全局命令控制变量
UINT8 bData1; //乒乓操作标志
UINT8 d1,d2;
UINT8 BuffFile[8*512];
UINT16 FatCache[2][256];
UINT8 buf1[1024],buf2[1024];
UINT8 Cache[512];
UINT8 buf[512];
#pragma end_abs_address
struct _FILE ApiFileControl;
UINT8 UsbEvent;
struct _BPB SimpleBpb;
struct _FILE FileControl;
FAT_PARAMETER FatParameter;
BLOCK BlockCommand;
//-------------------------------------------------------------------------
//* Function Protocol
//-------------------------------------------------------------------------
UINT8 SL811Read(UINT8 a);
void SL811Write(UINT8 a, UINT8 d);
void SL811BufRead(UINT8 addr, UINT8 *s, UINT8 c);
void SL811BufWrite(UINT8 addr, UINT8 *s, UINT8 c);
UINT16 WordSwap(UINT16 input);
UINT32 DWordSwap(UINT32 dData);
void DelayMs(UINT8 nFactor);
void USBReset(void);
UINT8 usbXfer(UINT8 usbaddr, UINT8 endpoint, UINT8 pid,UINT16 wPayload, UINT16 wLen, UINT8 *buffer);
UINT8 ep0Xfer(UINT8 usbaddr, UINT16 payload, pSetupPKG setup, UINT8 *pData);
UINT8 VendorCmd(UINT8 usbaddr,UINT8 bReq, UINT8 bCmd, UINT16 wValue, UINT16 wIndex,UINT16 wLen,UINT8* pData);
UINT8 GetDesc(UINT8 usbaddr, UINT16 wValue, UINT16 wIndex, UINT16 wLen, UINT8 *desc);
UINT8 SetAddress(UINT16 addr);
UINT8 SetConfiguration(UINT8 usbaddr,UINT16 wVal);
UINT8 EnumUsbDev(UINT8 DevAddr);
void sl811h_init(void);
extern void delay_1ms(UINT8 _ms);
//*****************************************************************************************
// Byte Read from SL811H
// Addr = register address
// return = data in register
//*****************************************************************************************
UINT8 SL811Read(UINT8 Addr)
{
OUTPB(USB_RW_ADDR,Addr);
return (INPB(USB_RW_DATA));
}
//*****************************************************************************************
// Byte Write to SL811H
// Addr = register address
// Dat = data to be written to this register address
//*****************************************************************************************
void SL811Write(UINT8 Addr, UINT8 Dat)
{
OUTPB(USB_RW_ADDR,Addr);
OUTPB(USB_RW_DATA,Dat);
}
//*****************************************************************************************
// Buffer Read from SL811H
// addr = buffer start address
// Buf = return buffer address where data are to be save/read
// Num = buffer data length
//*****************************************************************************************
void SL811BufRead(UINT8 Addr, UINT8 *Buf, UINT8 Num)
{
UINT8 i;
UINT8 volatile *p=(UINT8 volatile *)Buf;
i=Num;
OUTPB(USB_RW_ADDR,Addr);
while (i--)
(*p++) = INPB(USB_RW_DATA);
}
//*****************************************************************************************
// Buffer Write to SL811H
// addr = buffer start address
// Buf = buffer address where data are to be written
// Num = buffer data length
//*****************************************************************************************
void SL811BufWrite(UINT8 Addr, UINT8 *Buf, UINT8 Num)
{
UINT8 i;
UINT8 j;
i=Num;
OUTPB(USB_RW_ADDR,Addr);
while (i--)
{
j=*Buf;
OUTPB(USB_RW_DATA,*Buf++);
}
}
void DelayMs(UINT8 nFactor)
{
delay_1ms(nFactor);
}
//*****************************************************************************************
// Swap high and low byte
//*****************************************************************************************
UINT16 WordSwap(UINT16 input)
{
return(((input&0x00FF)<<8)|((input&0xFF00)>>8));
}
UINT32 DWordSwap(UINT32 dData)
{
dData = (dData&0xff)<<24|(dData&0xff00)<<8|(dData&0xff000000)>>24|(dData&0xff0000)>>8;
return dData;
}
//*****************************************************************************************
// UsbReset during enumeration of device attached directly to SL811HS
//*****************************************************************************************
void USBReset()
{
UINT8 temp;
UINT8 i;
i=SL811Read(IntStatus);
if(i&0x80)
{
SL811Write(cDATASet,0xe0);
SL811Write(cSOFcnt,0xae);
SL811Write(CtrlReg,0x05);
}
SL811Write(EP0Status,0x50);
SL811Write(EP0Counter,0x00);
SL811Write(EP0Control,0x01);
DelayMs(150);
temp=SL811Read(CtrlReg);
SL811Write(CtrlReg,temp|0x08);
DelayMs(15);
SL811Write(CtrlReg,temp | 0x18);
DelayMs(15);//Delay(200);
SL811Write(CtrlReg,temp | 0x08);
DelayMs(15);//Delay(200);
SL811Write(CtrlReg,temp);
DelayMs(150);
//DelayMs(400);//Delay(2000);
}
//*****************************************************************************************
// usbXfer:
// successful transfer = return TRUE
// fail transfer = return FALSE
//*****************************************************************************************
UINT8 usbXfer(UINT8 usbaddr, UINT8 endpoint, UINT8 pid,UINT16 wPayload, UINT16 wLen, UINT8 *buffer)
{
UINT8 cmd, result, timeout, intr;
UINT8 xferLen, bufLen, data0, data1, dataX, addr,remainder;
////////////////////////////////////////////////////////////////////////
//------------------------------------------------
// Default setting for usb trasnfer
//------------------------------------------------
bufLen = dataX = timeout = 0; //reset all
data0 = EP0_Buf; // DATA0 buffer address
data1 = data0 + (UINT8)wPayload; // DATA1 buffer address
USB_Control.DATA_STOP = USB_Control.TIMEOUT_ERR = FALSE; // set default conditions
//------------------------------------------------
// Define data transfer payload
//------------------------------------------------
if (wLen >= wPayload) // select proper data payload
xferLen = wPayload; // limit to wPayload size
else // else take < payload len
xferLen = wLen; //
//------------------------------------------------
// For IN token
//------------------------------------------------
if (pid==PID_IN) // for current IN tokens
{
cmd=sDATA0_RD;
}
//------------------------------------------------
// For OUT token
//------------------------------------------------
else if(pid==PID_OUT) // for OUT tokens
{
if(xferLen) // only when there are
SL811BufWrite(data0,buffer,xferLen); // data to transfer on USB
cmd=sDATA0_WR;
bData1 = uDev.bData1[endpoint];
uDev.bData1[endpoint] = (uDev.bData1[endpoint] ? 0 : 1); // DataToggle
if(bData1)
cmd |= 0x40; // Set Data1 bit in command
}
//------------------------------------------------
// For SETUP/OUT token
//------------------------------------------------
else // for current SETUP/OUT tokens
{
if(xferLen) // only when there are
SL811BufWrite(data0,buffer,xferLen); // data to transfer on USB
cmd = sDATA0_WR;
}
//------------------------------------------------
// For EP0'Buf IN/OUT token data, start with DATA1
// Control Endpoint0'Buf status stage.
// For data endpoint, IN/OUT data, start ????
//------------------------------------------------
if (endpoint == 0 && pid != PID_SETUP) // for Ep0'Buf IN/OUT token
cmd |= 0x40; // always set DATA1
//------------------------------------------------
// Arming of USB data transfer for the first pkt
//------------------------------------------------
SL811Write(EP0Status,((endpoint&0x0F)|pid)); // PID + EP address
SL811Write(EP0Counter,usbaddr); // USB address
SL811Write(EP0Address,data0); // buffer address, start with "data0"
SL811Write(EP0XferLen,xferLen); // data transfer length
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status
SL811Write(EP0Control,cmd); // Enable ARM and USB transfer start here
//------------------------------------------------
// Main loop for completing Addr wLen data trasnfer
//---------------------- --------------------------
while(TRUE)
{
//---------------Wait for done interrupt------------------
while(TRUE) // always ensure requested device is
{ // inserted at all time, then you will
intr = SL811Read(IntStatus); // wait for interrupt to be done, and
if((intr & USB_RESET) || (intr & INSERT_REMOVE)) // proceed to parse result from slave
{ // device.
USB_Control.DATA_STOP = TRUE; // if dhevice is removed, set DATA_STOP
return FALSE; // flag true, so that main loop will
} // know tis condition and exit gracefully
if(intr & USB_A_DONE)
break; // interrupt done !!!
}
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status
result = SL811Read(EP0Status); // read EP0status register
remainder = SL811Read(EP0Counter); // remainder value in last pkt xfer
//-------------------------ACK----------------------------
if (result & EP0_ACK) // Transmission ACK
{
// SETUP TOKEN
if(pid == PID_SETUP) // do nothing for SETUP/OUT token
break; // exit while(1) immediately
// OUT TOKEN
else if(pid == PID_OUT)
break;
// IN TOKEN
else if(pid == PID_IN)
{ // for IN token only
wLen -= (UINT16)xferLen; // update remainding wLen value
cmd ^= 0x40; // toggle DATA0/DATA1
dataX++; // point to next dataX
//------------------------------------------------
// If host requested for more data than the slave
// have, and if the slave'Buf data len is Addr multiple
// of its endpoint payload size/last xferLen. Do
// not overwrite data in previous buffer.
//------------------------------------------------
if(remainder==xferLen) // empty data detected
bufLen = 0; // do not overwriten previous data
else // reset bufLen to zero
bufLen = xferLen; // update previous buffer length
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -