USB_ISR.c
上传用户:gxz1972
上传日期:2019-09-13
资源大小:323k
文件大小:17k
- //-----------------------------------------------------------------------------
- // USB_ISR.c
- //-----------------------------------------------------------------------------
- // Copyright 2007 Vson Technology, Inc.
- // http://www.usbmcu.com
- //
- // Program Description:
- //
- //
- //
- //
- //
- // MCU: C8051F347
- // Tool chain: Keil C51 7.50 / Keil EVAL C51
- // Silicon Laboratories IDE version 2.6
- // Command Line:
- // Project Name: TR1000
- //
- //
- // Release 1.0
- // -All changes by Brin Cai
- // -24 JUL 2007
- //
- //
- //-----------------------------------------------------------------------------
- // Includes
- //-----------------------------------------------------------------------------
- #include "Include_H.h"
- typedef struct { // Structure definition of a block of data
- BYTE Piece[MAX_BLOCK_SIZE];
- } BLOCK;
- typedef struct { // Structure definition of a flash memory page
- BYTE FlashPage[FLASH_PAGE_SIZE];
- } PAGE;
- //xdata BLOCK TempStorage[BLOCKS_PR_PAGE]; // Temporary storage of between
- // flash writes
- code BYTE Pg0 _at_ 0x1400;
- code BYTE Pg1 _at_ 0x1600;
- code BYTE Pg2 _at_ 0x1800;
- code BYTE Pg3 _at_ 0x1A00;
- code BYTE Pg4 _at_ 0x1C00;
- code BYTE Pg5 _at_ 0x1E00;
- code BYTE Pg6 _at_ 0x2000;
- code BYTE Pg7 _at_ 0x2200;
- code BYTE Pg8 _at_ 0x2400;
- code BYTE Pg9 _at_ 0x2600;
- code BYTE Pg10 _at_ 0x2800;
- code BYTE Pg11 _at_ 0x2A00;
- code BYTE Pg12 _at_ 0x2C00;
- code BYTE Pg13 _at_ 0x2E00;
- code BYTE Pg14 _at_ 0x3000;
- code BYTE Pg15 _at_ 0x3200;
- code BYTE Pg16 _at_ 0x3400;
- code BYTE Pg17 _at_ 0x3600;
- code BYTE Pg18 _at_ 0x3800;
- code BYTE Pg19 _at_ 0x3A00;
- data bit bitInterruptFlag = 0;
- //idata unsigned char gaucTxData2HostBuffer[64];
- /*
- idata BYTE* PageIndices[20] = {&Pg0, &Pg1, &Pg2, &Pg3, &Pg4,
- &Pg5, &Pg6, &Pg7, &Pg8, &Pg9,
- &Pg10, &Pg11, &Pg12, &Pg13, &Pg14,
- &Pg15, &Pg16, &Pg17, &Pg18, &Pg19};
- */
- data UINT BytesToWrite; // Total number of bytes to write to the host
- //data UINT BytesToRead; // Total number of bytes to read from host
- data BYTE Buffer[3]; // Buffer for Setup messages
- data BYTE NumBlocks; // Number of Blocks for this transfer
- //data BYTE M_State; // Current Machine State
- data BYTE BlockIndex; // Index of Current Block in Page
- data BYTE PageIndex; // Index of Current Page in File
- data BYTE BlocksRead; // Total Number of Blocks Read
- data BYTE BlocksWrote; // Total Number of Blocks Written
- data BYTE* ReadIndex;
- // code const BYTE Serial1[0x0A] = {0x0A,0x03,'A',0,'B',0,'C',0,'D',0};
- // Serial Number Defintion
- code BYTE LengthFile[3] _at_ 0x1200;
- // {Length(Low Byte), Length(High Byte), Number of Blocks}
-
- //-----------------------------------------------------------------------------
- // Interrupt Service Routines
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- // USB_ISR
- //-----------------------------------------------------------------------------
- //
- //
- // This is the top level USB ISR. All endpoint interrupt/request
- // handlers are called from this function.
- //
- // Handler routines for any configured interrupts should be
- // added in the appropriate endpoint handler call slots.
- //
- //-----------------------------------------------------------------------------
- void USB_ISR () interrupt 8
- {
- BYTE bCommonInt, bInInt, bOutInt;
- // Read interrupt registers
- UREAD_BYTE(CMINT, bCommonInt);
- UREAD_BYTE(IN1INT, bInInt);
- UREAD_BYTE(OUT1INT, bOutInt);
-
- // Check for reset interrupt
- if (bCommonInt & rbRSTINT)
- {
- // Call reset handler
- M_State = USB_RESET_STATUS;
- //USBReset();
- //M_State = WAIT_OPEN_DEVICE_STATUS;
-
- }
- // Check for Endpoint0 interrupt
- if (bInInt & rbEP0)
- {
- // Call Endpoint0 handler
- M_State = EP0_HANDLER_STATUS;
- //Endpoint0();
- }
- // Endpoint1 IN
- if (bInInt & rbIN1)
- {
- bitInterruptFlag = 1;
- //sbitLED = 1;
- /*
- switch (M_State)
- {
- case
- TX_CONFIG_RESPONSE_TO_HOST_STATUS: // Stay in Wait State
- M_State = TX_KEY_DATA_TO_HOST_STATUS;
- break;
- case
- TX_KEY_DATA_TO_HOST_STATUS: // Stay in Idle State
- M_State = TX_TOUCHPAD_DATA_TO_HOST_STATUS;
- break;
- case
- TX_TOUCHPAD_DATA_TO_HOST_STATUS: // Response Host After receive the Config Data
- M_State = TX_CONFIG_RESPONSE_TO_HOST_STATUS;//ST_TX_KEY_DATA_TO_HOST;
- break;
-
- default:
- M_State = DEVICE_IDLE_STATUS; // Unknown State, stay in Error State
- break;
- }//switch
- */
- }//if
- // Endpoint2 OUT
- if (bOutInt & rbOUT2)
- {
- bitInterruptFlag = 1;
- M_State = RX_CONFIG_DATA_FROM_HOST_STATUS;
- }
- //State_Machine();
- }
- //-----------------------------------------------------------------------------
- // Support Subroutines
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- // USBReset
- //-----------------------------------------------------------------------------
- //
- // Return Value : None
- // Parameters : None
- //
- // - Initialize the global Device Status structure (all zeros)
- // - Resets all endpoints
- //-----------------------------------------------------------------------------
- void USBReset ()
- {
- BYTE i, bPower = 0;
- BYTE * pDevStatus;
- // Reset device status structure to all zeros (undefined)
- pDevStatus = (BYTE *)&gDeviceStatus;
- for (i=0;i<sizeof(DEVICE_STATUS);i++)
- {
- *pDevStatus++ = 0x00;
- }
- // Set device state to default
- gDeviceStatus.bDevState = DEV_DEFAULT;
- // REMOTE_WAKEUP_SUPPORT and SELF_POWERED_SUPPORT
- // defined in file "usb_desc.h"
- gDeviceStatus.bRemoteWakeupSupport = REMOTE_WAKEUP_SUPPORT;
- gDeviceStatus.bSelfPoweredStatus = SELF_POWERED_SUPPORT;
- // Reset all endpoints
- // Reset Endpoint0
- gEp0Status.bEpState = EP_IDLE; // Reset Endpoint0 state
- gEp0Status.bEp = 0; // Set endpoint number
- gEp0Status.uMaxP = EP0_MAXP; // Set maximum packet size
- // Reset Endpoint1 IN
- gEp1InStatus.bEpState = EP_HALTED; // Reset state
- gEp1InStatus.uNumBytes = 0; // Reset byte counter
- // Reset Endpoint2 OUT
- gEp2OutStatus.bEpState = EP_HALTED; // Reset state
- gEp2OutStatus.uNumBytes = 0; // Reset byte counter
- // Get Suspend enable/disable status. If enabled, prepare temporary
- // variable bPower.
- if (SUSPEND_ENABLE)
- {
- bPower = 0x01; // Set bit0 (Suspend Enable)
- }
- // Get ISO Update enable/disable status. If enabled, prepare temporary
- // variable bPower.
- if (ISO_UPDATE_ENABLE)
- {
- bPower |= 0x80; // Set bit7 (ISO Update Enable)
- }
- UWRITE_BYTE(POWER, bPower);
- }
- //-----------------------------------------------------------------------------
- // Endpoint0
- //-----------------------------------------------------------------------------
- //
- // Return Value : None
- // Parameters : None
- //
- //-----------------------------------------------------------------------------
- void Endpoint0 ()
- {
- BYTE bTemp = 0;
- BYTE bCsr1, uTxBytes;
- UWRITE_BYTE(INDEX, 0); // Target ep0
- UREAD_BYTE(E0CSR, bCsr1);
- // Handle Setup End
- if (bCsr1 & rbSUEND) // Check for setup end
- { // Indicate setup end serviced
- UWRITE_BYTE(E0CSR, rbSSUEND);
- gEp0Status.bEpState = EP_IDLE; // ep0 state to idle
- M_State = DEVICE_IDLE_STATUS; // ported from usb_file.c
- }
- // Handle sent stall
- if (bCsr1 & rbSTSTL) // If last state requested a stall
- { // Clear Sent Stall bit (STSTL)
- UWRITE_BYTE(E0CSR, 0);
- gEp0Status.bEpState = EP_IDLE; // ep0 state to idle
- M_State = DEVICE_IDLE_STATUS; // ported from usb_file.c
- }
- // Handle incoming packet
- if (bCsr1 & rbOPRDY)
- {
- // Read the 8-byte command from Endpoint0 FIFO
- FIFORead(0, 8, (BYTE*)&gEp0Command);
- // Byte-swap the wIndex field
- bTemp = gEp0Command.wIndex.c[1];
- gEp0Command.wIndex.c[1] = gEp0Command.wIndex.c[0];
- gEp0Command.wIndex.c[0] = bTemp;
- // Byte-swap the wValue field
- bTemp = gEp0Command.wValue.c[1];
- gEp0Command.wValue.c[1] = gEp0Command.wValue.c[0];
- gEp0Command.wValue.c[0] = bTemp;
- // Byte-swap the wLength field
- bTemp = gEp0Command.wLength.c[1];
- gEp0Command.wLength.c[1] = gEp0Command.wLength.c[0];
- gEp0Command.wLength.c[0] = bTemp;
- // Decode received command
- switch (gEp0Command.bmRequestType & CMD_MASK_COMMON)
- {
- case CMD_STD_DEV_OUT: // Standard device requests
- // Decode standard OUT request
- switch (gEp0Command.bRequest)
- {
- case SET_ADDRESS:
- SetAddressRequest();
- break;
- case SET_FEATURE:
- SetFeatureRequest();
- break;
- case CLEAR_FEATURE:
- ClearFeatureRequest();
- break;
- case SET_CONFIGURATION:
- SetConfigurationRequest();
- break;
- case SET_INTERFACE:
- SetInterfaceRequest();
- break;
- // All other OUT requests not supported
- case SET_DESCRIPTOR:
- default:
- gEp0Status.bEpState = EP_ERROR;
- break;
- }
- break;
- // Decode standard IN request
- case CMD_STD_DEV_IN:
- switch (gEp0Command.bRequest)
- {
- case GET_STATUS:
- GetStatusRequest();
- break;
- case GET_DESCRIPTOR:
- GetDescriptorRequest();
- break;
- case GET_CONFIGURATION:
- GetConfigurationRequest();
- break;
- case GET_INTERFACE:
- GetInterfaceRequest();
- break;
- // All other IN requests not supported
- case SYNCH_FRAME:
- break;//add at Aug.3, 2007
- default:
- gEp0Status.bEpState = EP_ERROR;
- break;
- }
- break;
- // All other requests not supported
- default:
- gEp0Status.bEpState = EP_ERROR;
- }
- // Write E0CSR according to the result of the serviced out packet
- bTemp = rbSOPRDY;
- if (gEp0Status.bEpState == EP_ERROR)
- {
- bTemp |= rbSDSTL; // Error condition handled
- // with STALL
- gEp0Status.bEpState = EP_IDLE; // Reset state to idle
- }
- UWRITE_BYTE(E0CSR, bTemp);
- }
- bTemp = 0; // Reset temporary variable
- // If state is transmit, call transmit routine
- if (gEp0Status.bEpState == EP_TX)
- {
- // Check the number of bytes ready for transmit
- // If less than the maximum packet size, packet will
- // not be of the maximum size
- if (gEp0Status.uNumBytes <= EP0_MAXP)
- {
- uTxBytes = gEp0Status.uNumBytes;
- gEp0Status.uNumBytes = 0; // update byte counter
- bTemp |= rbDATAEND; // This will be the last
- // packet for this transfer
- gEp0Status.bEpState = EP_IDLE; // Reset endpoint state
- }
- // Otherwise, transmit maximum-length packet
- else
- {
- uTxBytes = EP0_MAXP;
- gEp0Status.uNumBytes -= EP0_MAXP;// update byte counter
- }
- // Load FIFO
- FIFOWrite(0, uTxBytes, (BYTE*)gEp0Status.pData);
- // Update data pointer
- gEp0Status.pData = (BYTE*)gEp0Status.pData + uTxBytes;
- // Update Endpoint0 Control/Status register
- bTemp |= rbINPRDY; // Always transmit a packet
- // when this routine is called
- // (may be zero-length)
- UWRITE_BYTE(E0CSR, bTemp); // Write to Endpoint0 Control/Status
- }
- }
- //-----------------------------------------------------------------------------
- // BulkOrInterruptOut
- //-----------------------------------------------------------------------------
- //
- // Return Value : None
- // Parameters :
- // 1) PEP_STATUS pEpOutStatus
- //
- //-----------------------------------------------------------------------------
- void BulkOrInterruptOut(PEP_STATUS pEpOutStatus)
- {
- UINT uBytes;
- BYTE bTemp = 0;
- BYTE bCsrL, bCsrH;
- UWRITE_BYTE(INDEX, pEpOutStatus->bEp); // Index to current endpoint
- UREAD_BYTE(EOUTCSRL, bCsrL);
- UREAD_BYTE(EOUTCSRH, bCsrH);
- // Make sure this endpoint is not halted
- if (pEpOutStatus->bEpState != EP_HALTED)
- {
- // Handle STALL condition sent
- if (bCsrL & rbOutSTSTL)
- {
- // Clear Send Stall, Sent Stall, and data toggle
- UWRITE_BYTE(EOUTCSRL, rbOutCLRDT);
- }
- // Read received packet
- if(bCsrL & rbOutOPRDY)
- {
- // Get packet length
- UREAD_BYTE(EOUTCNTL, bTemp); // Low byte
- uBytes = (UINT)bTemp & 0x00FF;
- UREAD_BYTE(EOUTCNTH, bTemp); // High byte
- uBytes |= (UINT)bTemp << 8;
- if (M_State == DEVICE_IDLE_STATUS)
- {
- FIFORead(0x02, uBytes, &Buffer);
- }
- else
- {
- FIFORead(0x02, uBytes, (BYTE*)(&gaucTempStorage[BlockIndex]));
- }
- // Clear out-packet-ready
- UWRITE_BYTE(INDEX, pEpOutStatus->bEp);
- UWRITE_BYTE(EOUTCSRL, 0);
- // Read updated status register
- //UWRITE_BYTE(INDEX, pEpOutStatus->bEp); // Index to current endpoint
- //UREAD_BYTE(EOUTCSRL, bCsrL);
- }
- }
- }
- //-----------------------------------------------------------------------------
- // BulkOrInterruptIn
- //-----------------------------------------------------------------------------
- //
- // Return Value : None
- // Parameters :
- // 1) PEP_STATUS pEpOutStatus
- // 2) BYTE * DataToWrite
- // 3) UINT NumBytes
- //
- // - Places DataToWrite on the IN FIFO
- // - Sets Packet Ready Bit
- //-----------------------------------------------------------------------------
- void BulkOrInterruptIn (PEP_STATUS pEpInStatus, BYTE * DataToWrite,
- UINT NumBytes)
- {
- BYTE bCsrL, bCsrH;
- UWRITE_BYTE(INDEX, pEpInStatus->bEp); // Index to current endpoint
- UREAD_BYTE(EINCSRL, bCsrL);
- UREAD_BYTE(EINCSRH, bCsrH);
- // Make sure this endpoint is not halted
- if (pEpInStatus->bEpState != EP_HALTED)
- {
- // Handle STALL condition sent
- if (bCsrL & rbInSTSTL)
- {
- UWRITE_BYTE(EINCSRL, rbInCLRDT); // Clear Send Stall and Sent Stall,
- // and clear data toggle
- }
- // If a FIFO slot is open, write a new packet to the IN FIFO
- if (!(bCsrL & rbInINPRDY))
- {
- pEpInStatus->uNumBytes = NumBytes;
- pEpInStatus->pData = (BYTE*)DataToWrite;
- // Write <uNumBytes> bytes to the <bEp> FIFO
- FIFOWrite(pEpInStatus->bEp, pEpInStatus->uNumBytes,
- (BYTE*)pEpInStatus->pData);
- BytesToWrite -= NumBytes;
- ReadIndex += NumBytes;
- BlocksWrote++;
- // Set Packet Ready bit (INPRDY)
- UWRITE_BYTE(EINCSRL, rbInINPRDY);
- // Check updated endopint status
- //UREAD_BYTE(EINCSRL, bCsrL);
- }
- }
- }
- //-----------------------------------------------------------------------------
- // End Of File
- //-----------------------------------------------------------------------------