📄 usb.lst
字号:
C51 COMPILER V7.50 USB 03/05/2008 17:32:45 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE USB
OBJECT MODULE PLACED IN .\build\usb.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE usb.c OMF2 ROM(COMPACT) OPTIMIZE(9,SPEED) BROWSE MODC2 MDU_R515 DEBUG PRINT
-(.\list\usb.lst) OBJECT(.\build\usb.obj)
line level source
1 /* Copyright (c) 2007 Nordic Semiconductor. All Rights Reserved.
2 *
3 * The information contained herein is property of Nordic Semiconductor ASA.
4 * Terms and conditions of usage are described in detail in NORDIC
5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
6 *
7 * Licensees are granted free, non-transferable use of the information. NO
8 * WARRENTY of ANY KIND is provided. This heading must NOT be removed from
9 * the file.
10 *
11 * $LastChangedRevision: 2290 $
12 */
13
14 /** @file
15 * Minimalistic USB code for the bootloader.
16 *
17 * @author Ken A. Redergaard
18 * @author Ole Saether
19 *
20 */
21 #include <Nordic\reg24lu1.h>
22 #include <intrins.h>
23 #include <stdbool.h>
24
25 #include "config.h"
26 #include "usb.h"
27
28 #ifdef USE_USERCLASS
29 #pragma userclass (code = BOOTLOADER)
30 #pragma userclass (const = BOOTLOADER)
31 #endif
32
33 /** Leaves the minimum of the two arguments */
34 #define MIN(a, b) ((a) < (b) ? (a) : (b))
35
36 // USB map:
37 xdata volatile uint8_t out1buf[USB_EP1_SIZE] _at_ 0xC640;
38 xdata volatile uint8_t in1buf[USB_EP1_SIZE] _at_ 0xC680;
39 xdata volatile uint8_t out0buf[MAX_PACKET_SIZE_EP0] _at_ 0xC6C0;
40 xdata volatile uint8_t in0buf[MAX_PACKET_SIZE_EP0] _at_ 0xC700;
41 xdata volatile uint8_t bout1addr _at_ 0xC781;
42 xdata volatile uint8_t bout2addr _at_ 0xC782;
43 xdata volatile uint8_t bout3addr _at_ 0xC783;
44 xdata volatile uint8_t bout4addr _at_ 0xC784;
45 xdata volatile uint8_t bout5addr _at_ 0xC785;
46 xdata volatile uint8_t binstaddr _at_ 0xC788;
47 xdata volatile uint8_t bin1addr _at_ 0xC789;
48 xdata volatile uint8_t bin2addr _at_ 0xC78A;
49 xdata volatile uint8_t bin3addr _at_ 0xC78B;
50 xdata volatile uint8_t bin4addr _at_ 0xC78C;
51 xdata volatile uint8_t bin5addr _at_ 0xC78D;
52 xdata volatile uint8_t ivec _at_ 0xC7A8;
53 xdata volatile uint8_t in_irq _at_ 0xC7A9;
54 xdata volatile uint8_t out_irq _at_ 0xC7AA;
C51 COMPILER V7.50 USB 03/05/2008 17:32:45 PAGE 2
55 xdata volatile uint8_t usbirq _at_ 0xC7AB;
56 xdata volatile uint8_t in_ien _at_ 0xC7AC;
57 xdata volatile uint8_t out_ien _at_ 0xC7AD;
58 xdata volatile uint8_t usbien _at_ 0xC7AE;
59 xdata volatile uint8_t ep0cs _at_ 0xC7B4;
60 xdata volatile uint8_t in0bc _at_ 0xC7B5;
61 xdata volatile uint8_t in1cs _at_ 0xC7B6;
62 xdata volatile uint8_t in1bc _at_ 0xC7B7;
63 xdata volatile uint8_t out0bc _at_ 0xC7C5;
64 xdata volatile uint8_t out1cs _at_ 0xC7C6;
65 xdata volatile uint8_t out1bc _at_ 0xC7C7;
66 xdata volatile uint8_t usbcs _at_ 0xC7D6;
67 xdata volatile uint8_t inbulkval _at_ 0xC7DE;
68 xdata volatile uint8_t outbulkval _at_ 0xC7DF;
69 xdata volatile uint8_t inisoval _at_ 0xC7E0;
70 xdata volatile uint8_t outisoval _at_ 0xC7E1;
71 xdata volatile uint8_t setupbuf[8] _at_ 0xC7E8;
72
73 static uint8_t usb_bm_state;
74 static uint8_t usb_current_config;
75 static uint8_t usb_current_alt_interface;
76 static usb_state_t usb_state;
77
78 static uint8_t code * packetizer_data_ptr;
79 static uint8_t packetizer_data_size;
80 static uint8_t packetizer_pkt_size;
81 static uint8_t bmRequestType;
82
83 bool packet_received;
84
85 static void packetizer_isr_ep0_in();
86 static void usb_process_get_status();
87 static void usb_process_get_descriptor();
88
89 static void delay_ms(uint16_t ms)
90 {
91 1 uint16_t i, j;
92 1
93 1 for(i = 0; i < ms; i++ )
94 1 {
95 2 for( j = 0; j < 1403; j++)
96 2 {
97 3 _nop_();
98 3 }
99 2 }
100 1 }
101
102 void usb_init(void)
103 {
104 1 // Setup state information
105 1 usb_state = DEFAULT;
106 1 usb_bm_state = 0;
107 1
108 1 // Setconfig configuration information
109 1 usb_current_config = 0;
110 1 usb_current_alt_interface = 0;
111 1
112 1 // Disconnect from USB-bus since we are in this routine from a power on and not a soft reset:
113 1
114 1 usbcs |= 0x08;
115 1 delay_ms(50);
116 1 usbcs &= ~0x08;
C51 COMPILER V7.50 USB 03/05/2008 17:32:45 PAGE 3
117 1
118 1 usbien = 0x1d;
119 1 in_ien = 0x01;
120 1 in_irq = 0x1f;
121 1 out_ien = 0x01;
122 1 out_irq = 0x1f;
123 1
124 1 // Setup the USB RAM with some OK default values:
125 1 bout1addr = MAX_PACKET_SIZE_EP0/2;
126 1 bout2addr = MAX_PACKET_SIZE_EP0/2 + USB_EP1_SIZE/2;
127 1 bout3addr = MAX_PACKET_SIZE_EP0/2 + 2*USB_EP1_SIZE/2;
128 1 bout4addr = MAX_PACKET_SIZE_EP0/2 + 3*USB_EP1_SIZE/2;
129 1 bout5addr = MAX_PACKET_SIZE_EP0/2 + 4*USB_EP1_SIZE/2;
130 1 binstaddr = 0xc0;
131 1 bin1addr = MAX_PACKET_SIZE_EP0/2;
132 1 bin2addr = MAX_PACKET_SIZE_EP0/2 + USB_EP1_SIZE/2;
133 1 bin3addr = MAX_PACKET_SIZE_EP0/2 + 2*USB_EP1_SIZE/2;
134 1 bin4addr = MAX_PACKET_SIZE_EP0/2 + 3*USB_EP1_SIZE/2;
135 1 bin5addr = MAX_PACKET_SIZE_EP0/2 + 4*USB_EP1_SIZE/2;
136 1
137 1 // Set all endpoints to not valid (except EP0IN and EP0OUT)
138 1 inbulkval = 0x01;
139 1 outbulkval = 0x01;
140 1 inisoval = 0x00;
141 1 outisoval = 0x00;
142 1
143 1 in_ien |= 0x02;;
144 1 inbulkval |= 0x02;
145 1 out_ien |= 0x02;
146 1 outbulkval |= 0x02;
147 1 out1bc = 0xff;
148 1 }
149
150 static void packetizer_isr_ep0_in()
151 {
152 1 uint8_t code* data_ptr;
153 1 uint8_t size, i;
154 1 // We are getting a ep0in interupt when the host send ACK and do not have any more data to send
155 1 if(packetizer_data_size == 0)
156 1 {
157 2 in0bc = 0;
158 2 USB_EP0_HSNAK();
159 2 return;
160 2 }
161 1
162 1 size = MIN(packetizer_data_size, packetizer_pkt_size);
163 1
164 1 // Copy data to the USB-controller buffer
165 1 data_ptr = packetizer_data_ptr;
166 1 for(i = 0; i < size;i++)
167 1 {
168 2 in0buf[i] = *data_ptr++;
169 2 }
170 1
171 1 // Tell the USB-controller how many bytes to send
172 1 // If a IN is received from host after this the USB-controller will send the data
173 1 in0bc = size;
174 1
175 1 // Update the packetizer data
176 1 packetizer_data_ptr += size;
177 1 packetizer_data_size -= size;
178 1 }
C51 COMPILER V7.50 USB 03/05/2008 17:32:45 PAGE 4
179
180 static void usb_process_get_status()
181 {
182 1 in0buf[0] = in0buf[1] = 0x00;
183 1 if((usb_state == ADDRESSED) && (setupbuf[4] == 0x00))
184 1 {
185 2 in0bc = 0x02;
186 2 }
187 1 else if(usb_state == CONFIGURED)
188 1 {
189 2 switch(bmRequestType)
190 2 {
191 3 case 0x80: // Device
192 3 if((usb_bm_state & USB_BM_STATE_ALLOW_REMOTE_WAKEUP ) == USB_BM_STATE_ALLOW_REMOTE_WAKEUP)
193 3 {
194 4 in0buf[0] = 0x02;
195 4 }
196 3 in0bc = 0x02;
197 3 break;
198 3
199 3 case 0x81: // Interface
200 3 in0bc = 0x02;
201 3 break;
202 3
203 3 case 0x82: // Endpoint
204 3 if((setupbuf[4] & 0x80) == 0x80) // IN endpoints
205 3 in0buf[0] = in1cs;
206 3 else
207 3 in0buf[0] = out1cs;
208 3 in0bc = 0x02;
209 3 break;
210 3 default:
211 3 USB_EP0_STALL();
212 3 break;
213 3 }
214 2 }
215 1 else
216 1 {
217 2 // We should not be in this state
218 2 USB_EP0_STALL();
219 2 }
220 1 }
221
222 static void usb_process_get_descriptor()
223 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -