📄 usb_isr.c
字号:
Ep_Status[0] = EP_IDLE; // and return Endpoint 0 to an idle state
}
POLL_WRITE_BYTE(E0CSR, TempReg); // Write mask to E0CSR
}
}
}
}
//-----------------------------------------------------------------------------
// Handle_In1
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This routine loads the current value from In_Packet on the Endpoint 1 fifo,
// after an interrupt is received from the last packet being transmitted
//
//-----------------------------------------------------------------------------
void Handle_In2()
{
BYTE ControlReg;
POLL_WRITE_BYTE(INDEX, 2); // Set index to endpoint 1 registers
POLL_READ_BYTE(EINCSR1, ControlReg); // Read contol register for EP 1
if (Ep_Status[1] == EP_HALT) // If endpoint is currently halted, send a stall
{
POLL_WRITE_BYTE(EINCSR1, rbInSDSTL);
}
else // Otherwise send last updated data to host
{
if (ControlReg & rbInSTSTL) // Clear sent stall if last packet
// returned a stall
{
POLL_WRITE_BYTE(EINCSR1, rbInCLRDT);
}
if (ControlReg & rbInUNDRUN) // Clear underrun bit if it was set
{
POLL_WRITE_BYTE(EINCSR1, 0x00);
}
command_buffer[0] ++;
Fifo_Write(FIFO_EP2, 254, (BYTE *)command_buffer);
POLL_WRITE_BYTE(EINCSR1, rbInINPRDY);
// Set In Packet ready bit, indicating
} // fresh data on Fifo 1
}
//-----------------------------------------------------------------------------
// Handle_Out2
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Take the received packet from the host off the fifo and put it into
// the Out_Packet array
//
//-----------------------------------------------------------------------------
void Handle_Out2()
{
BYTE Count = 0;
BYTE ControlReg;
POLL_WRITE_BYTE(INDEX, 2); // Set index to endpoint 2 registers
POLL_READ_BYTE(EOUTCSR1, ControlReg);
if (Ep_Status[2] == EP_HALT) // If endpoint is halted, send a stall
{
POLL_WRITE_BYTE(EOUTCSR1, rbOutSDSTL);
}
else // Otherwise read packet from host
{
if (ControlReg & rbOutSTSTL) // Clear sent stall bit if last packet
// was a stall
{
POLL_WRITE_BYTE(EOUTCSR1, rbOutCLRDT);
}
POLL_READ_BYTE(EOUTCNTL, Count);
POLL_WRITE_BYTE(EOUTCSR1, 0); // Clear Out Packet ready bit
}
}
//-----------------------------------------------------------------------------
// Handle_In1
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This routine loads the current value from In_Packet on the Endpoint 1 fifo,
// after an interrupt is received from the last packet being transmitted
//
//-----------------------------------------------------------------------------
void Handle_In3()
{
BYTE ControlReg;
POLL_WRITE_BYTE(INDEX, 3); // Set index to endpoint 1 registers
POLL_READ_BYTE(EINCSR1, ControlReg); // Read contol register for EP 1
if (Ep_Status[3] == EP_HALT) // If endpoint is currently halted, send a stall
{
POLL_WRITE_BYTE(EINCSR1, rbInSDSTL);
}
else // Otherwise send last updated data to host
{
if (ControlReg & rbInSTSTL) // Clear sent stall if last packet
// returned a stall
{
POLL_WRITE_BYTE(EINCSR1, rbInCLRDT);
}
if (ControlReg & rbInUNDRUN) // Clear underrun bit if it was set
{
POLL_WRITE_BYTE(EINCSR1, 0x00);
}
Fifo_Write(FIFO_EP3, 512, (BYTE *)buffer);
POLL_WRITE_BYTE(EINCSR1, rbInINPRDY);
pIn = buffer;
// Set In Packet ready bit, indicating
} // fresh data on Fifo 1
}
//-----------------------------------------------------------------------------
// Handle_Out2
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Take the received packet from the host off the fifo and put it into
// the Out_Packet array
//
//-----------------------------------------------------------------------------
void Handle_Out3()
{
BYTE Count = 0;
BYTE ControlReg;
POLL_WRITE_BYTE(INDEX, 3); // Set index to endpoint 2 registers
POLL_READ_BYTE(EOUTCSR1, ControlReg);
if (Ep_Status[4] == EP_HALT) // If endpoint is halted, send a stall
{
POLL_WRITE_BYTE(EOUTCSR1, rbOutSDSTL);
}
else // Otherwise read packet from host
{
if (ControlReg & rbOutSTSTL) // Clear sent stall bit if last packet
// was a stall
{
POLL_WRITE_BYTE(EOUTCSR1, rbOutCLRDT);
}
POLL_READ_BYTE(EOUTCNTL, Count);
POLL_WRITE_BYTE(EOUTCSR1, 0); // Clear Out Packet ready bit
}
}
//-----------------------------------------------------------------------------
// Usb_Suspend
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Enter suspend mode after suspend signalling is present on the bus
//
//-----------------------------------------------------------------------------
void Usb_Suspend(void)
{
// Put the device in a low power configuration
/*
P0MDIN = 0x00; // Port 0 configured as analog input
P1MDIN = 0x00; // Port 1 configured as analog input
P2MDIN = 0x00; // Port 2 configured as analog input
P3MDIN = 0x00; // Port 3 configured as analog input
ADC0CN &= ~0x80; // Disable ADC0
REF0CN = 0x00; // Disable voltage reference
OSCICN |= 0x20; // Put oscillator
// When the device receives a non-idle USB event, it will resume execution
// on the instruction that follows OSCICN |= 0x20.
// Re-enable everything that was disabled when going into Suspend
P0MDIN = 0xFF; // Port 0 configured as analog input
P1MDIN = 0x7F; // Port 1 pin 7 set as analog input
P2MDIN = 0xFF; // Port 2 configured as analog input
P3MDIN = 0xFF; // Port 3 configured as analog input
REF0CN = 0x0E; // Enable voltage reference VREF
ADC0CN |= 0x80; // Re-enable ADC
*/
}
//-----------------------------------------------------------------------------
// Usb_Resume
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Resume normal USB operation
//
//-----------------------------------------------------------------------------
void Usb_Resume(void)
{
volatile int k;
k++;
// Add code for resume
}
//-----------------------------------------------------------------------------
// Fifo_Read
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) BYTE addr : target address
// 2) unsigned int uNumBytes : number of bytes to unload
// 3) BYTE * pData : read data destination
//
// Read from the selected endpoint FIFO
//
//-----------------------------------------------------------------------------
void Fifo_Read(BYTE addr, unsigned int uNumBytes, BYTE * pData){
int i;
if (uNumBytes) // Check if >0 bytes requested,
{
USB0ADR = (addr); // Set address
USB0ADR |= 0xC0; // Set auto-read and initiate
// first read
// Unload <NumBytes> from the selected FIFO
for(i=0;i<uNumBytes-1;i++)
{
while(USB0ADR & 0x80); // Wait for BUSY->'0' (data ready)
pData[i] = USB0DAT; // Copy data byte
}
USB0ADR = 0; // Clear auto-read
while(USB0ADR & 0x80); // Wait for BUSY->'0' (data ready)
pData[i] = USB0DAT; // Copy data byte }}//-----------------------------------------------------------------------------
// Fifo_Write
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) BYTE addr : target address
// 2) unsigned int uNumBytes : number of bytes to unload
// 3) BYTE * pData : location of source data
//
// Write to the selected endpoint FIFO
//
//-----------------------------------------------------------------------------
void Fifo_Write(BYTE addr, unsigned int uNumBytes, BYTE * pData){
int i;
// If >0 bytes requested, if (uNumBytes)
{
while(USB0ADR & 0x80); // Wait for BUSY->'0'
// (register available)
USB0ADR = (addr); // Set address (mask out bits7-6)
// Write <NumBytes> to the selected FIFO
for(i=0;i<uNumBytes;i++)
{
USB0DAT = pData[i];
while(USB0ADR & 0x80); // Wait for BUSY->'0' (data ready)
} }}
//-----------------------------------------------------------------------------
// Force_Stall
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Force a procedural stall to be sent to the host
//
//-----------------------------------------------------------------------------
void Force_Stall(void)
{
POLL_WRITE_BYTE(INDEX, 0);
POLL_WRITE_BYTE(E0CSR, rbSDSTL); // Set the send stall bit
Ep_Status[0] = EP_STALL; // Put the endpoint in stall status
}
void USB_ep0_rx(void)
{
BYTE Count;
POLL_READ_BYTE(EOUTCNTL, Count);
if(Count < usb_ep0_dcntR)
{
Fifo_Read(FIFO_EP0, Count, usb_dptr);
usb_dptr += Count;
usb_ep0_dcntR -= Count;
POLL_WRITE_BYTE(E0CSR, rbSOPRDY); // Set Serviced Setup packet, put endpoint in transmit
DataSent = 0;
}
else
{
Fifo_Read(FIFO_EP0, usb_ep0_dcntR, usb_dptr);
usb_ep0_dcntR = 0;
POLL_WRITE_BYTE(E0CSR, rbSOPRDY|rbDATAEND|rbSSUEND); // Set Serviced Setup packet, put endpoint in transmit
DataSent = 0;
receive_ep0 = 0;
}
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -