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