📄 usbisr.c
字号:
// Function:
// This firmware use endpoint1-in as a interrupt endpoint to transfer
// current switch status to host.Switch status is refreshed and saved
// at mEp1InBuffer in main program loop.Host software should probe
// endpoint-in use polling interval that endpoint descriptor declared.
void Ep1In(void) //endpoint1 IN transaction process
{
//In main program loop,mEp1InBuffer is stuffed,when switch status has
//changed and host receiced it correctly,this interrupt will happened.
//Here we only need to read last transaction status and has nothing to
//do.
unsigned char data bLastTransactionStatus;
bLastTransactionStatus = D12ReadLastTransactionStatus(D12_READ_LAST_TRANSACTION_STATUS_ENDPOINT1_IN);
}
//
//*************************************************************************
// Paremeter:
// In : None
// Out: None
// Function:
// This firmware use 512 byte inner ram to demonstrate how to implement bulk transfer.
// There are three command operation to ram: loopback, read and write.For loopback,
// we just received data and save it to ram,then transfer those data to host without
// any change.Host software should first issue a vendor defined control transfer to
// tell firmware that the operation command is loopback.For ram read and write,firmware
// save data to ram or transfer ram data to host according to host's request.Every time
// before ram read or write,host software should first issue a vendor defined control
// transfer to tell firmware the operation command is ram read or ram write,the start
// address and the r/w length.
void Ep2Out(void) //endpoint2 OUT transaction process
{
unsigned char data bLastTransactionStatus;
unsigned char data bDataLength;
unsigned char xdata * data pBuffer;
unsigned char data i;
unsigned char data j;
bLastTransactionStatus = D12ReadLastTransactionStatus(D12_READ_LAST_TRANSACTION_STATUS_ENDPOINT2_OUT);
if (sSysInformation.sRamControl.bRamCommand == RAM_COMMAND_LOOPBACK)
{
for (j=0;j<2;j++)
{
bDataLength = D12ReadBuffer(D12_SELECT_ENDPOINT_ENDPOINT2_OUT, 2*D12_EP2_MAX_PACKET_SIZE,&mRamBuffer);
if (bDataLength>0)
{
D12_COMMAND_ADDRESS = D12_SELECT_ENDPOINT_ENDPOINT2_IN; //select endpoint,the optional read one byte is not read
D12_COMMAND_ADDRESS = D12_WRITE_BUFFER_SELECTED_ENDPOINT; //write endpoint buffer
D12_DATA_ADDRESS = 0; //first byte is reserved,should wirte 0x00
D12_DATA_ADDRESS = bDataLength; //the second byte is the data length that will be write to buffer
#ifndef _INT_MODE_
#ifdef _Debug_
printf("Ep2Out Length is 0x%x\n",(unsigned int)(bDataLength));
#endif
#endif
pBuffer = &mRamBuffer;
#ifndef _INT_MODE_
#ifdef _Debug_
printf("Write back to endpoint2_IN; ");
#endif
#endif
for(i=0; i<bDataLength; i++)
{
D12_DATA_ADDRESS = *pBuffer;
#ifndef _INT_MODE_
#ifdef _Debug_
printf("%x,",(unsigned int)(*pBuffer));
#endif
#endif
pBuffer++;
}
#ifndef _INT_MODE_
#ifdef _Debug_
printf("\n");
#endif
#endif
D12_COMMAND_ADDRESS = D12_VALIDATE_BUFFER_SELECTED_ENDPOINT; //validate buffer
}
}
/////////////////////////////////////////////////////////////////////////
}
else
{ //if Host issued a RAM_COMMAND_WRITE command
if (sSysInformation.sRamControl.bRamRwStatus == RECEIVE)
{
for (j=0;j<2;j++)
{
if (sSysInformation.sRamControl.iRamRemaindLength >0)
{
pBuffer = &mRamBuffer + sSysInformation.sRamControl.iRamStartAddress+\
(sSysInformation.sRamControl.iRamRwLength-sSysInformation.sRamControl.iRamRemaindLength);
bDataLength = D12ReadBuffer(D12_SELECT_ENDPOINT_ENDPOINT2_OUT,sSysInformation.sRamControl.iRamRemaindLength, pBuffer);
if (bDataLength == D12_BUFFER_LENGTH_ERROR)
{
//D12_BUFFER_LENGTH_ERROR shows received data length is longer than expected length,
//this is a serious error,so we stall control endpoint
sSysInformation.sRamControl.bRamRwStatus = IDLE;
//D12SetEndpointStatus(D12_SELECT_ENDPOINT_ENDPOINT2_OUT,D12_ENDPOINT_STALL); //set endpoint2-out stall
GENERIC_ENDPOINT_STALL(D12_SELECT_ENDPOINT_ENDPOINT2_OUT)
}
else
{
//get remaind data length
sSysInformation.sRamControl.iRamRemaindLength -= bDataLength;
//If remaind data length is 0, current control-out transfer will be finished.
//We should return zero length data packet as status stage of control-out transfer
//to tell host that the current control-out transfer is completed. see 8.5.2.1
if (sSysInformation.sUsbSetUpDealwith.wRemaindLength<=0)
{
sSysInformation.sRamControl.bRamRwStatus = IDLE;
}
}
}
//If we has already received all data,but host still issue an out token,it is an error.
else
{
sSysInformation.sRamControl.bRamRwStatus = IDLE;
//D12SetEndpointStatus(D12_SELECT_ENDPOINT_ENDPOINT2_OUT,D12_ENDPOINT_STALL); //set endpoint2-out stall
GENERIC_ENDPOINT_STALL(D12_SELECT_ENDPOINT_ENDPOINT2_OUT)
}
}
}
//If Host issued a RAM_COMMAND_READ command,but before complete this command,
//host issued another OUT token,it is an error,and we should stall endpoint2-out.
else
{
sSysInformation.sRamControl.bRamRwStatus = IDLE;
GENERIC_ENDPOINT_STALL(D12_SELECT_ENDPOINT_ENDPOINT2_OUT)
//D12SetEndpointStatus(D12_SELECT_ENDPOINT_ENDPOINT2_OUT,D12_ENDPOINT_STALL); //set endpoint2-out stall
}
}
}
//
//*************************************************************************
// Paremeter:
// In : None
// Out: None
// Function:
// See Ep2Out notes.
void Ep2In(void) //endpoint2 IN transaction process
{
unsigned char data bLastTransactionStatus;
unsigned char data i,j;
unsigned char data bDataLength;
unsigned char xdata * data pBuffer;
bLastTransactionStatus = D12ReadLastTransactionStatus(D12_SET_ENDPOINT_STATUS_ENDPOINT2_IN);
if (sSysInformation.sRamControl.bRamCommand != RAM_COMMAND_LOOPBACK)
{
if (sSysInformation.sRamControl.bRamRwStatus == TRANSMIT)
{
for (j=0;j<2;j++)
{
if (sSysInformation.sRamControl.iRamRemaindLength >0)
{
if (sSysInformation.sRamControl.iRamRemaindLength > D12_EP2_MAX_PACKET_SIZE)
{
//if this time we can not transfer all remaind data to host,
//we still set TRANSMIT flag
sSysInformation.sRamControl.bRamRwStatus = TRANSMIT;
bDataLength = D12_EP2_MAX_PACKET_SIZE;
}
else
{
//if this time we can transfer all remaind data to Host
//reset bRamRwStatus status to IDLE
sSysInformation.sRamControl.bRamRwStatus = IDLE;
bDataLength = (unsigned char)(sSysInformation.sRamControl.iRamRemaindLength);
}
pBuffer = &mRamBuffer + sSysInformation.sRamControl.iRamStartAddress+\
(sSysInformation.sRamControl.iRamRwLength-sSysInformation.sRamControl.iRamRemaindLength);
// if (bDataLength >0)
// {
D12_COMMAND_ADDRESS = D12_SELECT_ENDPOINT_ENDPOINT2_IN; //select endpoint,the optional read one byte is not read
D12_COMMAND_ADDRESS = D12_WRITE_BUFFER_SELECTED_ENDPOINT; //write endpoint buffer
D12_DATA_ADDRESS = 0; //first byte is reserved,should wirte 0x00
D12_DATA_ADDRESS = bDataLength; //the second byte is the data length that will be write to buffer
#ifndef _INT_MODE_
#ifdef _Debug_
printf("Write buffer:");
#endif
#endif
for(i=0; i<bDataLength; i++)
{
D12_DATA_ADDRESS = *pBuffer;
#ifndef _INT_MODE_
#ifdef _Debug_
printf("%x,",(unsigned int)(*pBuffer));
#endif
#endif
pBuffer++;
}
#ifndef _INT_MODE_
#ifdef _Debug_
printf("\n");
#endif
#endif
D12_COMMAND_ADDRESS = D12_VALIDATE_BUFFER_SELECTED_ENDPOINT; //validate buffer
//D12WriteBuffer(D12_SELECT_ENDPOINT_ENDPOINT2_IN,bDataLength,pBuffer);
sSysInformation.sRamControl.iRamRemaindLength -= bDataLength;
// }
}
}
}
else
{
//if Hsot issue a IN transacton,but we are not at transmit state,it is an error
sSysInformation.sRamControl.bRamRwStatus = IDLE;
GENERIC_ENDPOINT_STALL(D12_SELECT_ENDPOINT_ENDPOINT2_IN)
//D12SetEndpointStatus(D12_SELECT_ENDPOINT_ENDPOINT2_IN,D12_ENDPOINT_STALL); //set endpoint2-in stall
}
}
}
//
//*************************************************************************
// Paremeter:
// In : None
// Out: None
// Function:
// According to the interrupt source of PDIUSBD12,call corresponding
// function to implementate interrupt service routine.
void UsbIsr(void)
{
unsigned char data bD12InterruptRegister; //used to save D12 innterrupt register value
bD12InterruptRegister = D12ReadInterruptRegister();
#ifndef _INT_MODE_
#ifdef _Debug_
printf("D12 Int is :0x%x\n",(unsigned int)(bD12InterruptRegister));
#endif
#endif
if(bD12InterruptRegister != 0)
{
if(bD12InterruptRegister & D12_INT_ENDP0_IN) Ep0In();
if(bD12InterruptRegister & D12_INT_ENDP0_OUT) Ep0Out();
if(bD12InterruptRegister & D12_INT_ENDP1_IN) Ep1In();
if(bD12InterruptRegister & D12_INT_ENDP1_OUT) Ep1Out();
if(bD12InterruptRegister & D12_INT_ENDP2_IN) Ep2In();
if(bD12InterruptRegister & D12_INT_ENDP2_OUT) Ep2Out();
if(bD12InterruptRegister & D12_INT_BUS_RESET) sSysInformation.bBusReset = TRUE;
if(bD12InterruptRegister & D12_INT_SUSPEND_CHANGE) sSysInformation.bSuspendChange = TRUE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -