#include "common.h"
#include "SL811.H"
#include "HAL.H"
extern XXGFLAGS bdata bXXGFlags;
XXGPKG usbstack;
unsigned char remainder;
unsigned char xdata DBUF[BUFFER_LENGTH];
pUSBDEV idata uDev; // Multiple USB devices attributes, Max 5 devices
//xdata pHUBDEV uHub; // Struct for downstream device on HUB
//pDevDesc idata pDev; // Device descriptor struct
//pCfgDesc idata pCfg; // Configuration descriptor struct
//pIntfDesc idata pIfc; // Interface descriptor struct
//pEPDesc idata pEnp; // Endpoint descriptor struct
//pStrDesc idata pStr; // String descriptor struct
//xdata pHidDesc pHid; // HID class descriptor struct
//xdata pHubDesc pHub; // HUD class descriptor struct
//xdata pPortStatus pStat; // HID ports status
//*****************************************************************************************
// SL811H variables initialization
//*****************************************************************************************
unsigned char SL811_GetRev(void)
{
//SL811Write(SL811_ADDR_PORT, 0x0e);
return SL811Read(0x0e);
}
void USBReset(void)
{
unsigned char temp;
temp=SL811Read(CtrlReg);
SL811Write(CtrlReg,temp|0x08);
DelayMs(25);
SL811Write(CtrlReg,temp);
}
//*****************************************************************************************
// usbXfer:
// successful transfer = return TRUE
// fail transfer = return FALSE
//*****************************************************************************************
unsigned char usbXfer(void)
{
unsigned char xferLen, data0, data1,cmd;
unsigned char intr,result,remainder,dataX,bufLen,addr,timeout;
//------------------------------------------------
// Default setting for usb trasnfer
//------------------------------------------------
dataX=timeout=0;
//result = SL811Read(EP0Status);
data0 = EP0_Buf; // DATA0 buffer address
data1 = data0 + (unsigned char)usbstack.wPayload; // DATA1 buffer address
bXXGFlags.bits.DATA_STOP=FALSE;
bXXGFlags.bits.TIMEOUT_ERR=FALSE;
//------------------------------------------------
// Define data transfer payload
//------------------------------------------------
if (usbstack.wLen >= usbstack.wPayload) // select proper data payload
xferLen = usbstack.wPayload; // limit to wPayload size
else // else take < payload len
xferLen = usbstack.wLen; //
// For IN token
if (usbstack.pid==PID_IN) // for current IN tokens
{ //
cmd = sDATA0_RD; // FS/FS on Hub, sync to sof
}
// For OUT token
else if(usbstack.pid==PID_OUT) // for OUT tokens
{
if(xferLen) // only when there are
{
//intr=usbstack.setup.wLength;
//usbstack.setup.wLength=WordSwap(usbstack.setup.wLength);
SL811BufWrite(data0,usbstack.buffer,xferLen); // data to transfer on USB
//usbstack.setup.wLength=intr;
}
cmd = sDATA0_WR; // FS/FS on Hub, sync to sof
// implement data toggle
bXXGFlags.bits.bData1 = uDev.bData1[usbstack.endpoint];
uDev.bData1[usbstack.endpoint] = (uDev.bData1[usbstack.endpoint] ? 0 : 1); // DataToggle
if(bXXGFlags.bits.bData1)
cmd |= 0x40; // Set Data1 bit in command
}
// For SETUP/OUT token
else // for current SETUP/OUT tokens
{
if(xferLen) // only when there are
{
intr=usbstack.setup.wLength;
usbstack.setup.wValue=WordSwap(usbstack.setup.wValue);
usbstack.setup.wIndex=WordSwap(usbstack.setup.wIndex);
usbstack.setup.wLength=WordSwap(usbstack.setup.wLength);
SL811BufWrite(data0,(unsigned char *)&usbstack.setup,xferLen); // data to transfer on USB
usbstack.setup.wLength=intr;
}
cmd = sDATA0_WR; // FS/FS on Hub, sync to sof
}
//------------------------------------------------
// For EP0's IN/OUT token data, start with DATA1
// Control Endpoint0's status stage.
// For data endpoint, IN/OUT data, start ????
//------------------------------------------------
if (usbstack.endpoint == 0 && usbstack.pid != PID_SETUP) // for Ep0's IN/OUT token
cmd |= 0x40; // always set DATA1
//------------------------------------------------
// Arming of USB data transfer for the first pkt
//------------------------------------------------
SL811Write(EP0Status,((usbstack.endpoint&0x0F)|usbstack.pid)); // PID + EP address
SL811Write(EP0Counter,usbstack.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 a 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(cSOFcnt);
//intr=SL811Read(IntEna);
intr = SL811Read(IntStatus);
// wait for interrupt to be done, and
if((intr & USB_RESET) || (intr & INSERT_REMOVE)) // proceed to parse result from slave
{ // device.
bXXGFlags.bits.DATA_STOP = TRUE; // if device is removed, set DATA_STOP
return FALSE; // flag true, so that main loop will
} // know this 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(usbstack.pid == PID_SETUP) // do nothing for SETUP/OUT token
break; // exit while(1) immediately
// OUT TOKEN
else if(usbstack.pid == PID_OUT)
break;
// IN TOKEN
else if(usbstack.pid == PID_IN)
{ // for IN token only
usbstack.wLen -= (WORD)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's data len is a 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
//------------------------------------------------
// Arm for next data transfer when requested data
// length have not reach zero, i.e. wLen!=0, and
// last xferlen of data was completed, i.e.
// remainder is equal to zero, not a short pkt
//------------------------------------------------
if(!remainder && usbstack.wLen) // remainder==0 when last xferLen
{ // was all completed or wLen!=0
addr = (dataX & 1) ? data1:data0; // select next address for data
xferLen = (BYTE)(usbstack.wLen>=usbstack.wPayload) ? usbstack.wPayload:usbstack.wLen; // get data length required
//if (FULL_SPEED) // sync with SOF transfer
cmd |= 0x20; // always sync SOF when FS, regardless
SL811Write(EP0XferLen, xferLen); // select next xfer length
SL811Write(EP0Address, addr); // data buffer addr
SL811Write(IntStatus
没有合适的资源?快使用搜索试试~ 我知道了~
51单片机读写USB
共43个文件
h:20个
c:14个
uv2:2个
3星 · 超过75%的资源 需积分: 35 15 下载量 113 浏览量
2008-10-22
17:47:05
上传
评论 1
收藏 84KB RAR 举报
温馨提示
该内容是关于51单片机读写USB的程序。有需要的朋友赶快下噢
资源推荐
资源详情
资源评论
收起资源包目录
51读USB.rar (43个子文件)
51读USB
11
usbhost
51+sl811读写U盘的源程序+原理图
SL811.H 8KB
COMMON.H 3KB
HPI.C 22KB
ISR.C 3KB
MAIN.C 3KB
Rbc.h 2B
USBHOST.PLG 227B
USBHOST_Uv2.Bak 2KB
HAL.H 930B
USBHOST_Opt.Bak 1KB
USBHOST.Uv2 2KB
Control.Sch 39KB
MAIN.H 2B
Fat.c 5KB
SL811.C 30KB
HPI.H 7KB
AT89X52.H 6KB
Fat.h 545B
TPBULK.H 1KB
USBHOST.Opt 1KB
51+sl811读写U盘的源程序+原理图
SL811.H 8KB
COMMON.H 3KB
HPI.C 22KB
ISR.C 3KB
MAIN.C 3KB
Rbc.h 2B
USBHOST.PLG 269B
HAL.H 930B
USBHOST.Uv2 2KB
Control.Sch 39KB
MAIN.H 2B
Fat.c 5KB
SL811.C 30KB
HPI.H 7KB
AT89X52.H 6KB
Fat.h 545B
TPBULK.H 1KB
RBCCMD.H 14KB
TPBULK.C 10KB
HAL.C 2KB
RBCCMD.H 14KB
TPBULK.C 10KB
HAL.C 2KB
共 43 条
- 1
资源评论
- mxlhj1682015-04-27没想到下载后,要看懂这个程序得装keil及protel之类的软件;要是能把相应芯片的datasheet附加上就更好了
horsewater
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功