📄 usb_main.lst
字号:
C51 COMPILER V7.06 USB_MAIN 05/08/2008 13:49:38 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE USB_MAIN
OBJECT MODULE PLACED IN USB_MAIN.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE USB_MAIN.c BROWSE DEBUG OBJECTEXTEND
stmt level source
1 // File best viewed using Tab Size of 4 Characters
2 // Author DM DATE 4-4-03
3 // Modified CS DATE 8-25-03
4 // This example illustrates usage of the USB_API.lib
5 // DO NOT locate code segments at 0x1400 to 0x4000
6 // These are used for non-voltile storage for transmitted data.
7
8 // Include files
9 #include <c8051f320.h> // Header file for SiLabs c8051f320
10 #include <stddef.h> // Used for NULL pointer definition
11 #include "USB_API.h" // Header file for USB_API.lib
12
13 // Bit Definitions
14 sbit Led1 = P2^2; // LED='1' means ON
15 sbit Led2 = P2^3; // These blink to indicate data transmission
16
17 // Constants Definitions
18 #define NUM_STG_PAGES 20 // Total number of flash pages to be used for file storage
19 #define MAX_BLOCK_SIZE_READ 64 // Use the maximum read block size of 64 bytes
20
21 #define MAX_BLOCK_SIZE_WRITE 4096 // Use the maximum write block size of 4096 bytes
22 #define FLASH_PAGE_SIZE 512 // Size of each flash page
23 #define BLOCKS_PR_PAGE FLASH_PAGE_SIZE/MAX_BLOCK_SIZE_READ // 512/64 = 8
24 #define MAX_NUM_BYTES FLASH_PAGE_SIZE*NUM_STG_PAGES
25 #define MAX_NUM_BLOCKS BLOCKS_PR_PAGE*NUM_STG_PAGES
26
27
28
29 // Message Types
30 #define READ_MSG 0x00 // Message types for communication with host
31 #define WRITE_MSG 0x01
32 #define SIZE_MSG 0x02
33 #define DELAYED_READ_MSG 0x05
34
35 // Machine States
36 #define ST_WAIT_DEV 0x01 // Wait for application to open a device instance
37 #define ST_IDLE_DEV 0x02 // Device is open, wait for Setup Message from host
38 #define ST_RX_SETUP 0x04 // Received Setup Message, decode and wait for data
39 #define ST_RX_FILE 0x08 // Receive file data from host
40 #define ST_TX_FILE 0x10 // Transmit file data to host
41 #define ST_TX_ACK 0x20 // Transmit ACK 0xFF back to host after every 8 packets
42 #define ST_ERROR 0x80 // Error state
43
44 // No such thing as a block of data anymore since it is variable between 1 and 1024
45 // So comment this out
46 typedef struct { // Structure definition of a block of data
47 BYTE Piece[MAX_BLOCK_SIZE_READ];
48 } BLOCK;
49
50 typedef struct { // Structure definition of a flash memory page
51 BYTE FlashPage[FLASH_PAGE_SIZE];
52 } PAGE;
53
54 xdata BLOCK TempStorage[BLOCKS_PR_PAGE]; // Temporary storage of between flash writes
55
C51 COMPILER V7.06 USB_MAIN 05/08/2008 13:49:38 PAGE 2
56 //data BYTE code * PageIndices[20] = {0x1400,0x1600,0x1800,0x1A00,0x1C00,0x1E00,0x2000,0x2200,0x2400,0x260
-0
57 // ,0x2800,0x2A00,0x2C00,0x2E00,0x3000,0x3200,0x3400,0x3600,0x3800,0x3A00};
58 data BYTE code * PageIndices[20] = {0x2200,0x2400,0x2600,0x2800,0x2A00,0x2C00,0x2E00,0x3000,0x3200,0x3400
59 ,0x3600,0x3800,0x3A00,0x3C00,0x3E00,0x4000,0x4200,0x4400,0x4600};
60
61 data UINT BytesToRead; // Total number of bytes to read from host
62 data UINT WriteStageLength; // Current write transfer stage length
63 data UINT ReadStageLength; // Current read transfer stage length
64 data BYTE Buffer[3]; // Buffer for Setup messages
65 data UINT NumBytes; // Number of Blocks for this transfer
66 data BYTE NumBlocks;
67 data UINT BytesRead; // Number of Bytes Read
68 data BYTE M_State; // Current Machine State
69 data UINT BytesWrote; // Number of Bytes Written
70 data BYTE BlockIndex; // Index of Current Block in Page
71 data BYTE PageIndex; // Index of Current Page in File
72 data BYTE BlocksWrote; // Total Number of Blocks Written
73 data BYTE* ReadIndex;
74 data UINT BytesToWrite;
75
76 /*** [BEGIN] USB Descriptor Information [BEGIN] ***/
77 code const UINT USB_VID = 0x10C4;
78 code const UINT USB_PID = 0xEA61;
79 code const BYTE USB_MfrStr[] = {0x1A,0x03,'S',0,'i',0,'l',0,'i',0,'c',0,'o',0,'n',0,' ',0,'L',0,'a',0,'b',
-0,'s',0}; // Manufacturer String
80 code const BYTE USB_ProductStr[] = {0x10,0x03,'U',0,'S',0,'B',0,' ',0,'A',0,'P',0,'I',0}; // Product Desc.
- String
81 code const BYTE USB_SerialStr[] = {0x0A,0x03,'1',0,'2',0,'3',0,'4',0};
82 code const BYTE USB_MaxPower = 15; // Max current = 30 mA (15 * 2)
83 code const BYTE USB_PwAttributes = 0x80; // Bus-powered, remote wakeup not supported
84 code const UINT USB_bcdDevice = 0x0100; // Device release number 1.00
85 /*** [ END ] USB Descriptor Information [ END ] ***/
86
87 code BYTE LengthFile[3] _at_ 0x2000;
88 // {Length(Low Byte), Length(High Byte), Number of Blocks}
89
90 void Port_Init(void); // Initialize Ports Pins and Enable Crossbar
91 void State_Machine(void); // Determine new state and act on current state
92 void Receive_Setup(void); // Receive and decode setup packet from host
93 void Receive_File(void); // Receive file data from host
94 //void Suspend_Device(void); // Place the device in suspend mode
95 void Page_Erase(BYTE*) small; // Erase a flash page
96 void Page_Write(BYTE*) small; // Write a flash page
97
98
99
100 //-----------------------------------------------------------------------------
101 // Main Routine
102 //-----------------------------------------------------------------------------
103 void main(void)
104 {
105 1 PCA0MD &= ~0x40; // Disable Watchdog timer
106 1
107 1 USB_Clock_Start(); // Init USB clock *before* calling USB_Init
108 1 USB_Init(USB_VID,USB_PID,USB_MfrStr,USB_ProductStr,USB_SerialStr,USB_MaxPower,USB_PwAttributes,USB_bcdD
-evice);
109 1
110 1 CLKSEL |= 0x02;
111 1
112 1
113 1 RSTSRC |= 0x02;
C51 COMPILER V7.06 USB_MAIN 05/08/2008 13:49:38 PAGE 3
114 1
115 1 Port_Init(); // Initialize crossbar and GPIO
116 1
117 1 USB_Int_Enable(); // Enable USB_API Interrupts
118 1 while (1);
119 1 }
120
121 void Port_Init(void)
122 {
123 1 P2MDOUT |= 0x0C; // Port 2 pins 0,1 set high impedence
124 1 XBR0 = 0x00;
125 1 XBR1 = 0x40; // Enable Crossbar
126 1 }
127
128 void Page_Erase(BYTE* Page_Address) small
129 {
130 1 BYTE EA_Save; // Used to save state of global interrupt enable
131 1 BYTE xdata *pwrite; // xdata pointer used to generate movx intruction
132 1
133 1 EA_Save = EA; // Save current EA
134 1 EA = 0; // Turn off interrupts
135 1 pwrite = (BYTE xdata *)(Page_Address); // Set write pointer to Page_Address
136 1 PSCTL = 0x03; // Enable flash erase and writes
137 1
138 1 FLKEY = 0xA5; // Write flash key sequence to FLKEY
139 1 FLKEY = 0xF1;
140 1 *pwrite = 0x00; // Erase flash page using a write command
141 1
142 1 PSCTL = 0x00; // Disable flash erase and writes
143 1 EA = EA_Save; // Restore state of EA
144 1 }
145
146 void Page_Write(BYTE* PageAddress) small
147 {
148 1 BYTE EA_Save; // Used to save state of global interrupt enable
149 1 BYTE xdata *pwrite; // Write Pointer
150 1 BYTE xdata *pread; // Read Pointer
151 1 UINT x; // Counter for 0-512 bytes
152 1
153 1 pread = (BYTE xdata *)(TempStorage);
154 1 EA_Save = EA; // Save EA
155 1 EA = 0; // Turn off interrupts
156 1 pwrite = (BYTE xdata *)(PageAddress);
157 1 PSCTL = 0x01; // Enable flash writes
158 1 for(x = 0; x<FLASH_PAGE_SIZE; x++)// Write 512 bytes
159 1 {
160 2 FLKEY = 0xA5; // Write flash key sequence
161 2 FLKEY = 0xF1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -