Народ помогите, в конец замучался.
На ЛПЦ 2148 пытаюсь реализовать ХИД девайс.
Проблема что не могу отправить усб сообщение через нулевую контрольную точку.
Принимать сообщения принимаю и правильные, но дальше Дескриптора устройтсва и задания адреса не сдвинулся.
Все ведет себя как то не предсказуемо, и уже не знаю на что и думать.
Регулярно получаю ошибку
Unexpected Packet
причем и в еррор статусе и еррор коде регистре.
Есть те кто реализовывал УСБ на ЛПЦ 2148
вот на всякий случай текст функции посылки сообщения, вроде из названия функций можно догадаться что они делают
Рад буду любой помощи
Код
int USBSendData(int EPNum, char *Buffer, int DataLen, int MaxPocketSize)
{
int ErrorCode=0;
int TimeOut=0;
int EPPhysNum=((EPNum<<1)+1); //convert logical to physcal EP num
char EPInfo;
//logical to physycal
ErrorCode=(USBCmd((CMD_USB_SEL_EP+EPPhysNum),NULL,&EPInfo)<<3);
if((EPInfo&0x01)!=0) //not end sending of previous send
{
#ifdef DEBUG
UARTSendText("Prev send not end, EP info :",0);
UARTSendHexStr(&EPInfo,1,0);
#endif
return 4;
}
if(DataLen==0) //need send zero length data
{
USBCTRL=(((EPNum&0x0F)<<2)|0x02);// set write mode, and sellect EP
TPKTLEN=0;
TDATA=0; //just in case, for internal mechanics
ErrorCode+=(USBValidateBuffer(EPPhysNum)<<6); //need use physical ep number;
if(ErrorCode!=0) //error code
return ErrorCode;
TimeOut=0;
EPInfo=0x01;
while(((EPInfo&0x01)!=0)&&(TimeOut++<USB_DEV_TIMEOUT)) //waite end of sending
{
USBCmd((CMD_USB_SEL_EP+EPPhysNum),NULL,&EPInfo);
if((EPInfo&0x02)!=0) //ep was STALLed
{
UARTSendText("EP was STALLed, EPInfo :",0);
UARTSendHexStr(&EPInfo,1,0);
break;
}
}
if(((EPInfo&0x02)!=0)||((EPInfo&0x01)!=0)) //not end of sending interrupt, or EP STALLed
return 3;
USBCmd((CMD_USB_SEL_CLR_INT_EP+EPPhysNum),NULL,&EPInfo); //clear usb clock domain interrupt
#ifdef SPEED_DEBUG
UARTSendText("Zerro EP Info :",0);
UARTSendHexStr(&EPInfo,1,0);
#endif
}
while(DataLen>0) //send data by max pocket size if nedded
{
int CurrentSendDataLen;
if(DataLen>MaxPocketSize)
CurrentSendDataLen=MaxPocketSize;
else
CurrentSendDataLen=DataLen;
USBCTRL=(((EPNum&0x0F)<<2)|0x02);// set write mode, and sellect EP
TPKTLEN=(CurrentSendDataLen&0x3FF);
for(int j=0;j<CurrentSendDataLen;j+=4) //send by word
{
if((CurrentSendDataLen-j)<4)//last part of sending
{
int TempData=0;
char *Pointer=((char *)&TempData);
for(int k=j;k<CurrentSendDataLen;k++)
*Pointer=*Buffer++;
TDATA=TempData;
break;
}
TDATA=*((int *)Buffer);
Buffer+=4; //write by words
}
ErrorCode+=(USBValidateBuffer(EPPhysNum)<<6); //need use physical ep number;
if(ErrorCode!=0) //error code
return ErrorCode;
DataLen-=CurrentSendDataLen;
/*if(DataLen>0) //need send one more data, waite end of last sending
{*/
TimeOut=0;
EPInfo=0x01;
while(((EPInfo&0x01)!=0)&&(TimeOut++<USB_DEV_TIMEOUT)) //waite end of sending
{
USBCmd((CMD_USB_SEL_EP+EPPhysNum),NULL,&EPInfo);
if((EPInfo&0x02)!=0) //ep was STALLed
{
UARTSendText("EP was STALLed, EPInfo :",0);
UARTSendHexStr(&EPInfo,1,0);
break;
}
}
if(((EPInfo&0x02)!=0)||((EPInfo&0x01)!=0)) //not end of sending interrupt, or EP STALLed
return 3;
USBCmd((CMD_USB_SEL_CLR_INT_EP+EPPhysNum),NULL,&EPInfo); //clear usb clock domain interrupt
#ifdef SPEED_DEBUG
UARTSendText("Zerro EP Info :",0);
UARTSendHexStr(&EPInfo,1,0);
#endif
//}
}
return ErrorCode;
}
{
int ErrorCode=0;
int TimeOut=0;
int EPPhysNum=((EPNum<<1)+1); //convert logical to physcal EP num
char EPInfo;
//logical to physycal
ErrorCode=(USBCmd((CMD_USB_SEL_EP+EPPhysNum),NULL,&EPInfo)<<3);
if((EPInfo&0x01)!=0) //not end sending of previous send
{
#ifdef DEBUG
UARTSendText("Prev send not end, EP info :",0);
UARTSendHexStr(&EPInfo,1,0);
#endif
return 4;
}
if(DataLen==0) //need send zero length data
{
USBCTRL=(((EPNum&0x0F)<<2)|0x02);// set write mode, and sellect EP
TPKTLEN=0;
TDATA=0; //just in case, for internal mechanics
ErrorCode+=(USBValidateBuffer(EPPhysNum)<<6); //need use physical ep number;
if(ErrorCode!=0) //error code
return ErrorCode;
TimeOut=0;
EPInfo=0x01;
while(((EPInfo&0x01)!=0)&&(TimeOut++<USB_DEV_TIMEOUT)) //waite end of sending
{
USBCmd((CMD_USB_SEL_EP+EPPhysNum),NULL,&EPInfo);
if((EPInfo&0x02)!=0) //ep was STALLed
{
UARTSendText("EP was STALLed, EPInfo :",0);
UARTSendHexStr(&EPInfo,1,0);
break;
}
}
if(((EPInfo&0x02)!=0)||((EPInfo&0x01)!=0)) //not end of sending interrupt, or EP STALLed
return 3;
USBCmd((CMD_USB_SEL_CLR_INT_EP+EPPhysNum),NULL,&EPInfo); //clear usb clock domain interrupt
#ifdef SPEED_DEBUG
UARTSendText("Zerro EP Info :",0);
UARTSendHexStr(&EPInfo,1,0);
#endif
}
while(DataLen>0) //send data by max pocket size if nedded
{
int CurrentSendDataLen;
if(DataLen>MaxPocketSize)
CurrentSendDataLen=MaxPocketSize;
else
CurrentSendDataLen=DataLen;
USBCTRL=(((EPNum&0x0F)<<2)|0x02);// set write mode, and sellect EP
TPKTLEN=(CurrentSendDataLen&0x3FF);
for(int j=0;j<CurrentSendDataLen;j+=4) //send by word
{
if((CurrentSendDataLen-j)<4)//last part of sending
{
int TempData=0;
char *Pointer=((char *)&TempData);
for(int k=j;k<CurrentSendDataLen;k++)
*Pointer=*Buffer++;
TDATA=TempData;
break;
}
TDATA=*((int *)Buffer);
Buffer+=4; //write by words
}
ErrorCode+=(USBValidateBuffer(EPPhysNum)<<6); //need use physical ep number;
if(ErrorCode!=0) //error code
return ErrorCode;
DataLen-=CurrentSendDataLen;
/*if(DataLen>0) //need send one more data, waite end of last sending
{*/
TimeOut=0;
EPInfo=0x01;
while(((EPInfo&0x01)!=0)&&(TimeOut++<USB_DEV_TIMEOUT)) //waite end of sending
{
USBCmd((CMD_USB_SEL_EP+EPPhysNum),NULL,&EPInfo);
if((EPInfo&0x02)!=0) //ep was STALLed
{
UARTSendText("EP was STALLed, EPInfo :",0);
UARTSendHexStr(&EPInfo,1,0);
break;
}
}
if(((EPInfo&0x02)!=0)||((EPInfo&0x01)!=0)) //not end of sending interrupt, or EP STALLed
return 3;
USBCmd((CMD_USB_SEL_CLR_INT_EP+EPPhysNum),NULL,&EPInfo); //clear usb clock domain interrupt
#ifdef SPEED_DEBUG
UARTSendText("Zerro EP Info :",0);
UARTSendHexStr(&EPInfo,1,0);
#endif
//}
}
return ErrorCode;
}