hal_usb.lst
来自「非常全的nrf2401设计资料」· LST 代码 · 共 960 行 · 第 1/3 页
LST
960 行
C51 COMPILER V7.50 HAL_USB 04/09/2009 10:12:51 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE HAL_USB
OBJECT MODULE PLACED IN .\build\hal_usb.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ..\..\..\..\arch\hal\nrf24lu1\hal_usb.c LARGE OMF2 OPTIMIZE(9,SPEED) BROWSE
- INCDIR(..\common;..\..\..\..\comp\protocol\wdp\common\;..\..\..\..\comp\protocol\wdp\host\;..\..\..\..\arch\hal\include
-;..\..\..\..\arch\hal\nrf24lu1;..\..\..\..\arch\nrf24lu1;..\common;..\..\..\..\comp\protocol\fap) DEBUG PRINT(.\lst\hal_
-usb.lst) OBJECT(.\build\hal_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 * Implementaion of the USB HAL
16 * @author Ken A. Redergaard
17 */
18
19 #include <intrins.h>
20 #include <stdint.h>
21
22 #include "nordic_common.h"
23 #include "hal_usb_desc.h"
24 #include "usb.h"
25
26 #include <Nordic\reg24lu1.h>
27
28 // Define formulas for jumping in the usb registry map based upon the endpoint number
29
30 // Calculate control and status register location in USB-controller
31 #define CALCULATE_CS_IN_PTR(ep) (uint8_t*)(&(i_usb.map->in1cs) + 2 * ( ( ep & 0x7f ) - 1 ))
32 #define CALCULATE_CS_OUT_PTR(ep) (uint8_t*)(&(i_usb.map->out1cs) + 2 * ( ( ep & 0x7f ) - 1 ))
33
34 // Calculate byte count register location in USB-controller
35 #define CALCULATE_BC_OUT_PTR(ep) (uint8_t*)(&(i_usb.map->out0bc) + ( ( ep ) * 2 ))
36 #define CALCULATE_BC_IN_PTR(ep) (uint8_t*)(&(i_usb.map->in0bc) + ( ( ep & 0x7f ) * 2 ))
37
38 // Calculate buffer location in USB-controller
39 #define CALCULATE_BUF_IN_PTR(ep) (uint8_t*)(i_usb.map->in0buf - ( ( ep & 0x7f ) * 128 ))
40 #define CALCULATE_BUF_OUT_PTR(ep) (uint8_t*)(i_usb.map->out0buf - ( ep * 128 ))
41
42 static xdata usb_t i_usb;
43 static hal_usb_device_req req;
44 hal_usb_t g_hal_usb;
45
46 static void packetize(uint8_t* data_ptr, uint8_t data_size, uint8_t pkt_size);
47 static void packetizer_isr_ep0_in();
48 static void usb_process_dev_req_cb_response(hal_usb_dev_req_resp_t ret, hal_usb_device_req* req, uint8_t*
-data_ptr, uint16_t size);
49 static void usb_process_get_status(hal_usb_device_req* req);
50 static void usb_process_get_descriptor(hal_usb_device_req* req);
51
C51 COMPILER V7.50 HAL_USB 04/09/2009 10:12:51 PAGE 2
52 static void isr_sudav();
53 static void isr_sof();
54 static void isr_sutok();
55 static void isr_suspend();
56 static void isr_usbreset();
57 static void isr_ep0out();
58
59 static void usb_process_ep_response(uint8_t ret, uint8_t* cs_ptr, uint8_t* bc_ptr);
60 static void delay_ms(uint8_t ms);
61
62 void hal_usb_init(
63 bool usb_disconnect,
64 hal_usb_cb_device_req_t device_req,
65 hal_usb_cb_reset_t reset,
66 hal_usb_cb_resume_t resume,
67 hal_usb_cb_suspend_t suspend)
68 {
69 1 // Setup the USB struct.
70 1 i_usb.map = (usb_map_t xdata*)0;
71 1
72 1 // Setup descriptors
73 1 g_hal_usb.descs.dev = &g_usb_dev_desc;
74 1 g_hal_usb.descs.dev = &g_usb_dev_desc;
75 1 g_hal_usb.descs.conf = &g_usb_conf_desc;
76 1 g_hal_usb.descs.string = &g_usb_string_desc;
77 1
78 1 // This is for setting language American English (String descriptor 0 is an array of supported languag
-es)
79 1 g_hal_usb.descs.string_zero[0] = 0x04;
80 1 g_hal_usb.descs.string_zero[1] = 0x03;
81 1 g_hal_usb.descs.string_zero[2] = 0x09;
82 1 g_hal_usb.descs.string_zero[3] = 0x04;
83 1
84 1 // Setup state information
85 1 g_hal_usb.state = DEFAULT;
86 1 g_hal_usb.bm_state = 0;
87 1
88 1 // Setconfig configuration information
89 1 g_hal_usb.current_config = 0;
90 1 g_hal_usb.current_alt_interface = 0;
91 1
92 1 // Setup callbacks
93 1 g_hal_usb.device_req = device_req;
94 1 g_hal_usb.reset = reset;
95 1 g_hal_usb.resume = resume;
96 1 g_hal_usb.suspend = suspend;
97 1
98 1 // Disconnect from USB-bus if we are in this routine from a power on and not a soft reset
99 1 if(usb_disconnect)
100 1 {
101 2 i_usb.map->usbcs |= 0x08; // disconnect
102 2 delay_ms(50);
103 2 i_usb.map->usbcs &= ~(0x08); // connect
104 2 }
105 1
106 1 // Setup interrupts
107 1 USBWU = 1; // USBWU is mapped to IEN1.3
108 1 USB = 1; // USBIRQ is mapped to IEN1.4
109 1
110 1 i_usb.map->usbien = 0x1d; // ibnie -5 4 - uresir 3 - suspir, 0 - sudavir
111 1
112 1 i_usb.map->in_ien = 0x01;
C51 COMPILER V7.50 HAL_USB 04/09/2009 10:12:51 PAGE 3
113 1 i_usb.map->in_irq = 0x1f;
114 1 i_usb.map->out_ien = 0x01;
115 1 i_usb.map->out_irq = 0x1f;
116 1
117 1 // Setup the USB RAM with some OK default values. Note that isochronos is not set up yet.
118 1 i_usb.map->bout1addr = 16;
119 1 i_usb.map->bout2addr = 32;
120 1 i_usb.map->bout3addr = 48;
121 1 i_usb.map->bout4addr = 64;
122 1 i_usb.map->bout5addr = 80;
123 1
124 1 i_usb.map->binstaddr = 0xc0;
125 1 i_usb.map->bin1addr = 16;
126 1 i_usb.map->bin2addr = 32;
127 1 i_usb.map->bin3addr = 48;
128 1 i_usb.map->bin4addr = 64;
129 1 i_usb.map->bin5addr = 80;
130 1
131 1 // Set all endpoints to not valid (except EP0IN and EP0OUT)
132 1 i_usb.map->inbulkval = 0x01;
133 1 i_usb.map->outbulkval = 0x01;
134 1 i_usb.map->inisoval = 0x00;
135 1 i_usb.map->outisoval = 0x00;
136 1 }
137
138 void hal_usb_endpoint_stall(uint8_t ep_num, bool stall)
139 {
140 1 uint8_t temp;
141 1 uint8_t* cs_ptr;
142 1
143 1 temp = ep_num & 0x7f;
144 1
145 1 // Calculate register address
146 1 if( ( ep_num & 0x80 ) == 0x80 ) // IN endpoints
147 1 {
148 2 // Calculate control and status register for IN endpoint
149 2 cs_ptr = (uint8_t*)(&(i_usb.map->in1cs) + 2 * ( temp - 1 ));
150 2 }
151 1 else // OUT endpoints
152 1 {
153 2 // Calculate control and status register for OUT endpoint
154 2 cs_ptr = (uint8_t*)(&(i_usb.map->out1cs) + 2 * ( temp - 1 ));
155 2 }
156 1
157 1 if( stall == true )
158 1 {
159 2 // Set the stall bit
160 2 *cs_ptr = 0x01;
161 2 }
162 1 else
163 1 {
164 2 // Clear the stall bit
165 2 *cs_ptr = 0x00;
166 2 }
167 1 }
168
169 uint8_t hal_usb_get_address()
170 {
171 1 return i_usb.map->fnaddr;
172 1 }
173
174 void hal_usb_endpoint_config(uint8_t ep_num, uint8_t ep_size, hal_usb_cb_endpoint_t endpoint_isr)
C51 COMPILER V7.50 HAL_USB 04/09/2009 10:12:51 PAGE 4
175 {
176 1 /// @todo Create a function that setup the usbram correctly
177 1 xdata uint8_t *bc_ptr;
178 1 uint8_t temp = ep_num & 0x7f;
179 1
180 1 // Dummy use of variable to get rid of warning
181 1 ep_size = 0;
182 1
183 1 if( ( ep_num & 0x80 ) == 0x80 ) // MSB set indicates IN endpoint
184 1 {
185 2 if( endpoint_isr != NULL )
186 2 {
187 3 // Add the callback, enable the interrupt and validate the endpoint
188 3 i_usb.endpoint_in_isr[temp - 1] = endpoint_isr;
189 3 i_usb.map->in_ien |= ( 1 << temp );
190 3 i_usb.map->inbulkval |= (1 << temp );
191 3 }
192 2 else
193 2 {
194 3 //lint --e{502}
195 3
196 3 // Remove the callback, disable the interrupt and invalidate the endpoint
197 3 i_usb.endpoint_in_isr[temp - 1] = NULL;
198 3 i_usb.map->in_ien &= ~( 1 << temp );
199 3 i_usb.map->inbulkval &= ~(1 << temp );
200 3 }
201 2 }
202 1 else // OUT endpoint
203 1 {
204 2 if( endpoint_isr != NULL )
205 2 {
206 3 // Add the callback, enable the interrupt and validate the endpoint
207 3 i_usb.endpoint_out_isr[temp - 1] = endpoint_isr;
208 3 i_usb.map->out_ien |= ( 1 << temp );
209 3 i_usb.map->outbulkval |= (1 << temp );
210 3
211 3 // Have to write a dummy value to the OUTxBC register to get interrupts
212 3 bc_ptr = CALCULATE_BC_OUT_PTR(ep_num);
213 3 *bc_ptr = 0xff;
214 3 }
215 2 else
216 2 {
217 3 //lint --e{502}
218 3
219 3 // Remove the callback, disable the interrupt and invalidate the endpoint
220 3
221 3 i_usb.endpoint_out_isr[temp - 1] = NULL;
222 3 i_usb.map->out_ien &= ~( 1 << temp );
223 3 i_usb.map->outbulkval &= ~(1 << temp );
224 3 }
225 2 }
226 1 }
227
228 void hal_usb_wakeup()
229 {
230 1 // We can only issue a wakeup if the host has allowed us to do so
231 1 if( ( g_hal_usb.bm_state & USB_BM_STATE_ALLOW_REMOTE_WAKEUP ) == USB_BM_STATE_ALLOW_REMOTE_WAKEUP )
232 1 {
233 2 USBCON = 0x40; // Wakeup the USB controller via remote pin
234 2 delay_ms(1); // Wait until the USB clock starts
235 2 USBCON = 0x00;
236 2 }
C51 COMPILER V7.50 HAL_USB 04/09/2009 10:12:51 PAGE 5
237 1 }
238
239 void hal_usb_reset()
240 {
241 1 SWRST = 1; // Perform a hardware reset of the USB controller
242 1 }
243
244 hal_usb_state_t hal_usb_get_state()
245 {
246 1 return g_hal_usb.state;
247 1 }
248
249 void hal_usb_send_data(uint8_t ep_num, uint8_t* array, uint8_t count)
250 {
251 1 uint8_t i;
252 1
253 1 xdata uint8_t *buf_ptr;
254 1 xdata uint8_t *bc_ptr;
255 1
256 1 // Calculate the buffer pointer and byte count pointer
257 1 buf_ptr = CALCULATE_BUF_IN_PTR(ep_num);
258 1 bc_ptr = CALCULATE_BC_IN_PTR(ep_num);
259 1
260 1 // Copy the data into the USB controller
261 1 for( i = 0; i < count; i++ )
262 1 {
263 2 buf_ptr[i] = array[i];
264 2 }
265 1
266 1 // Set the number of bytes we want to send to USB-host. This also trigger sending of data to USB-host.
267 1 *bc_ptr = count;
268 1 }
269
270 void hal_usb_bus_disconnect()
271 {
272 1 i_usb.map->usbcs |= 0x08; // disconnect
273 1 }
274
275 void hal_usb_bus_connect()
276 {
277 1 i_usb.map->usbcs &= ~(0x08); // connect
278 1 }
279
280 void hal_usb_sleep()
281 {
282 1 USBSLP=1;
283 1 }
284
285 static void packetize(uint8_t* data_ptr, uint8_t data_size, uint8_t pkt_size)
286 {
287 1 i_usb.packetizer.data_ptr = data_ptr;
288 1 i_usb.packetizer.data_size = data_size;
289 1 i_usb.packetizer.pkt_size = pkt_size;
290 1 }
291
292 // This routine is called by functions that shall send their first packet and when the EP0IN interrupt is
-set
293 static void packetizer_isr_ep0_in()
294 {
295 1 uint16_t size, i;
296 1
297 1 // We are getting a ep0in interupt when the host send ACK and do not have any more data to send
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?