//-----------------------------------------------------------------------------
// F32x_USB_USBISR.c
//-----------------------------------------------------------------------------
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// Source file for USB firmware. Includes the following routines:
//
// - USB_ISR(): Top-level USB interrupt handler. All USB handler
// routines are called from here.
// - Endpoint0(): Endpoint0 interrupt handler.
// - BulkOrInterruptOut(): Bulk or Interrupt OUT interrupt
// handler.
// - BulkOrInterruptIn(): Places DataToWrite on the IN FIFO.
// - USBReset (): USB Reset event handler.
// - State_Machine(): USB state machine
// - Receive_Setup(): Determine whether a read or write request
// been received and initializes variables accordingly.
// - Receive_File(): Receives and saves data
// - Page_Erase(): Erases a page of FLASH
// - Page_Write(): Writes to a page of FLASH
//
//
// How To Test: See Readme.txt
//
//
// FID: 32X000013
// Target: C8051F32x
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Silicon Laboratories IDE version 2.6
// Command Line: See Readme.txt
// Project Name: F32x_USB_Bulk
//
//
// Release 1.3
// -All changes by GP
// -21 NOV 2005
// -Changed revision number to match project revision
// No content changes to this file
// -Modified file to fit new formatting guidelines
// -Changed file name from usb_isr.c
//
//
// Release 1.2
// -Initial Revision (JS/CS/JM)
// -XX OCT 2003
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "c8051F320.h"
#include "F32x_USB_Registers.h"
#include "F32x_USB_Structs.h"
#include "F32x_USB_Main.h"
#include "F32x_USB_Descriptors.h"
#include "F32x_USB_Config.h"
#include "F32x_USB_Request.h"
//-----------------------------------------------------------------------------
// Extern Global Variables
//-----------------------------------------------------------------------------
extern DEVICE_STATUS gDeviceStatus;
extern EP0_COMMAND gEp0Command;
extern EP_STATUS gEp0Status;
extern EP_STATUS gEp1InStatus;
extern EP_STATUS gEp2OutStatus;
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
// Constants Definitions
#define NUM_STG_PAGES 20 // Total number of flash pages to
// be used for file storage
#define MAX_BLOCK_SIZE 64 // Use the maximum block size of 64
#define FLASH_PAGE_SIZE 512 // Size of each flash page
#define BLOCKS_PR_PAGE FLASH_PAGE_SIZE/MAX_BLOCK_SIZE
#define MAX_NUM_BLOCKS BLOCKS_PR_PAGE*NUM_STG_PAGES
// UINT type definition
#ifndef _UINT_DEF_
#define _UINT_DEF_
typedef unsigned int UINT;
#endif // _UINT_DEF_
// BYTE type definition
#ifndef _BYTE_DEF_
#define _BYTE_DEF_
typedef unsigned char BYTE;
#endif // _BYTE_DEF_
// Message Types
#define READ_MSG 0x00 // Message types for communication with host
#define WRITE_MSG 0x01
#define SIZE_MSG 0x02
// Machine States
#define ST_WAIT_DEV 0x01 // Wait for application to open a device instance
#define ST_IDLE_DEV 0x02 // Device is open, wait for Setup Message from host
#define ST_RX_SETUP 0x04 // Received Setup Message, decode and wait for data
#define ST_RX_FILE 0x08 // Receive file data from host
#define ST_TX_FILE 0x10 // Transmit file data to host
#define ST_TX_ACK 0x20 // Transmit ACK 0xFF to host after every 8 packets
#define ST_ERROR 0x80 // Error state
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;
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}
sbit Led1 = P2^2; // LED='1' means ON
sbit Led2 = P2^3; // These blink to indicate data transmission
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void State_Machine(void);
void Receive_Setup(void);
void Receive_File(void);
//-----------------------------------------------------------------------------
// 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
USBReset();
M_State = ST_WAIT_DEV;
}
// Check for Endpoint0 interrupt
if (bInInt & rbEP0)
{
// Call Endpoint0 handler
Endpoint0();
}
// Endpoint1 IN
if (bInInt & rbIN1)
{
if (M_State == ST_RX_FILE) // Ack Transmit comp
评论1
最新资源