Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /******************************************************************************** |
2 | File Information: |
||
3 | FileName: usb_function_cdc.c |
||
4 | Dependencies: See INCLUDES section |
||
5 | Processor: PIC18 or PIC24 USB Microcontrollers |
||
6 | Hardware: The code is natively intended to be used on the following |
||
7 | hardware platforms: PICDEM FS USB Demo Board, |
||
8 | PIC18F87J50 FS USB Plug-In Module, or |
||
9 | Explorer 16 + PIC24 USB PIM. The firmware may be |
||
10 | modified for use on other USB platforms by editing the |
||
11 | HardwareProfile.h file. |
||
12 | Complier: Microchip C18 (for PIC18) or C30 (for PIC24) |
||
13 | Company: Microchip Technology, Inc. |
||
14 | |||
15 | Software License Agreement: |
||
16 | |||
17 | The software supplied herewith by Microchip Technology Incorporated |
||
18 | (the Company) for its PIC® Microcontroller is intended and |
||
19 | supplied to you, the Companys customer, for use solely and |
||
20 | exclusively on Microchip PIC Microcontroller products. The |
||
21 | software is owned by the Company and/or its supplier, and is |
||
22 | protected under applicable copyright laws. All rights are reserved. |
||
23 | Any use in violation of the foregoing restrictions may subject the |
||
24 | user to criminal sanctions under applicable laws, as well as to |
||
25 | civil liability for the breach of the terms and conditions of this |
||
26 | license. |
||
27 | |||
28 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
29 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
30 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
31 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
32 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
33 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
34 | |||
35 | Summary: |
||
36 | This file contains all of functions, macros, definitions, variables, |
||
37 | datatypes, etc. that are required for usage with the CDC function |
||
38 | driver. This file should be included in projects that use the CDC |
||
39 | \function driver. |
||
40 | |||
41 | |||
42 | |||
43 | This file is located in the "\<Install Directory\>\\Microchip\\USB\\CDC |
||
44 | Device Driver" directory. |
||
45 | Description: |
||
46 | USB CDC Function Driver File |
||
47 | |||
48 | This file contains all of functions, macros, definitions, variables, |
||
49 | datatypes, etc. that are required for usage with the CDC function |
||
50 | driver. This file should be included in projects that use the CDC |
||
51 | \function driver. |
||
52 | |||
53 | This file is located in the "\<Install Directory\>\\Microchip\\USB\\CDC |
||
54 | Device Driver" directory. |
||
55 | |||
56 | When including this file in a new project, this file can either be |
||
57 | referenced from the directory in which it was installed or copied |
||
58 | directly into the user application folder. If the first method is |
||
59 | chosen to keep the file located in the folder in which it is installed |
||
60 | then include paths need to be added so that the library and the |
||
61 | application both know where to reference each others files. If the |
||
62 | application folder is located in the same folder as the Microchip |
||
63 | folder (like the current demo folders), then the following include |
||
64 | paths need to be added to the application's project: |
||
65 | |||
66 | ..\\..\\Include |
||
67 | |||
68 | . |
||
69 | |||
70 | If a different directory structure is used, modify the paths as |
||
71 | required. An example using absolute paths instead of relative paths |
||
72 | would be the following: |
||
73 | |||
74 | C:\\Microchip Solutions\\Microchip\\Include |
||
75 | |||
76 | C:\\Microchip Solutions\\My Demo Application |
||
77 | ********************************************************************************/ |
||
78 | |||
79 | /******************************************************************** |
||
80 | Change History: |
||
81 | Rev Description |
||
82 | ---- ----------- |
||
83 | 2.3 Decricated the mUSBUSARTIsTxTrfReady() macro. It is |
||
84 | replaced by the USBUSARTIsTxTrfReady() function. |
||
85 | |||
86 | 2.6 Minor definition changes |
||
87 | |||
88 | 2.6a No Changes |
||
89 | |||
90 | 2.7 Fixed error in the part support list of the variables section |
||
91 | where the address of the CDC variables are defined. The |
||
92 | PIC18F2553 was incorrectly named PIC18F2453 and the PIC18F4558 |
||
93 | was incorrectly named PIC18F4458. |
||
94 | |||
95 | http://www.microchip.com/forums/fb.aspx?m=487397 |
||
96 | |||
97 | 2.7a No change |
||
98 | |||
99 | ********************************************************************/ |
||
100 | |||
101 | /** I N C L U D E S **********************************************************/ |
||
102 | #include "USB\usb.h" |
||
103 | #include "USB\usb_function_cdc.h" |
||
104 | //#include "HardwareProfile.h" |
||
105 | |||
106 | #ifdef USB_USE_CDC |
||
107 | |||
108 | /** V A R I A B L E S ********************************************************/ |
||
109 | #if defined(__18F14K50) || defined(__18F13K50) || defined(__18LF14K50) || defined(__18LF13K50) |
||
110 | #pragma udata usbram2 |
||
111 | #elif defined(__18F2455) || defined(__18F2550) || defined(__18F4455) || defined(__18F4550)\ |
||
112 | || defined(__18F2458) || defined(__18F2553) || defined(__18F4458) || defined(__18F4553) |
||
113 | #pragma udata USB_VARIABLES=0x500 |
||
114 | #elif defined(__18F4450) || defined(__18F2450) |
||
115 | #pragma udata USB_VARIABLES=0x480 |
||
116 | #else |
||
117 | #pragma udata |
||
118 | #endif |
||
119 | |||
120 | volatile FAR CDC_NOTICE cdc_notice; |
||
121 | volatile FAR unsigned char cdc_data_rx[CDC_DATA_OUT_EP_SIZE]; |
||
122 | volatile FAR unsigned char cdc_data_tx[CDC_DATA_IN_EP_SIZE]; |
||
123 | LINE_CODING line_coding; // Buffer to store line coding information |
||
124 | |||
125 | #pragma udata |
||
126 | BYTE cdc_rx_len; // total rx length |
||
127 | |||
128 | BYTE cdc_trf_state; // States are defined cdc.h |
||
129 | POINTER pCDCSrc; // Dedicated source pointer |
||
130 | POINTER pCDCDst; // Dedicated destination pointer |
||
131 | BYTE cdc_tx_len; // total tx length |
||
132 | BYTE cdc_mem_type; // _ROM, _RAM |
||
133 | |||
134 | USB_HANDLE CDCDataOutHandle; |
||
135 | USB_HANDLE CDCDataInHandle; |
||
136 | |||
137 | |||
138 | CONTROL_SIGNAL_BITMAP control_signal_bitmap; |
||
139 | DWORD BaudRateGen; // BRG value calculated from baudrate |
||
140 | extern BYTE i; |
||
141 | extern BYTE_VAL *pDst; |
||
142 | |||
143 | /************************************************************************** |
||
144 | SEND_ENCAPSULATED_COMMAND and GET_ENCAPSULATED_RESPONSE are required |
||
145 | requests according to the CDC specification. |
||
146 | However, it is not really being used here, therefore a dummy buffer is |
||
147 | used for conformance. |
||
148 | **************************************************************************/ |
||
149 | #define dummy_length 0x08 |
||
150 | BYTE_VAL dummy_encapsulated_cmd_response[dummy_length]; |
||
151 | |||
152 | #if defined(USB_CDC_SET_LINE_CODING_HANDLER) |
||
153 | CTRL_TRF_RETURN USB_CDC_SET_LINE_CODING_HANDLER(CTRL_TRF_PARAMS); |
||
154 | #endif |
||
155 | |||
156 | /** P R I V A T E P R O T O T Y P E S ***************************************/ |
||
157 | void USBCDCSetLineCoding(void); |
||
158 | |||
159 | /** D E C L A R A T I O N S **************************************************/ |
||
160 | //#pragma code |
||
161 | |||
162 | /** C L A S S S P E C I F I C R E Q ****************************************/ |
||
163 | /****************************************************************************** |
||
164 | Function: |
||
165 | void USBCheckCDCRequest(void) |
||
166 | |||
167 | Description: |
||
168 | This routine checks the setup data packet to see if it |
||
169 | knows how to handle it |
||
170 | |||
171 | PreCondition: |
||
172 | None |
||
173 | |||
174 | Parameters: |
||
175 | None |
||
176 | |||
177 | Return Values: |
||
178 | None |
||
179 | |||
180 | Remarks: |
||
181 | None |
||
182 | |||
183 | *****************************************************************************/ |
||
184 | void USBCheckCDCRequest(void) |
||
185 | { |
||
186 | /* |
||
187 | * If request recipient is not an interface then return |
||
188 | */ |
||
189 | if(SetupPkt.Recipient != USB_SETUP_RECIPIENT_INTERFACE_BITFIELD) return; |
||
190 | |||
191 | /* |
||
192 | * If request type is not class-specific then return |
||
193 | */ |
||
194 | if(SetupPkt.RequestType != USB_SETUP_TYPE_CLASS_BITFIELD) return; |
||
195 | |||
196 | /* |
||
197 | * Interface ID must match interface numbers associated with |
||
198 | * CDC class, else return |
||
199 | */ |
||
200 | if((SetupPkt.bIntfID != CDC_COMM_INTF_ID)&& |
||
201 | (SetupPkt.bIntfID != CDC_DATA_INTF_ID)) return; |
||
202 | |||
203 | switch(SetupPkt.bRequest) |
||
204 | { |
||
205 | //****** These commands are required ******// |
||
206 | case SEND_ENCAPSULATED_COMMAND: |
||
207 | //send the packet |
||
208 | inPipes[0].pSrc.bRam = (BYTE*)&dummy_encapsulated_cmd_response; |
||
209 | inPipes[0].wCount.Val = dummy_length; |
||
210 | inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; |
||
211 | inPipes[0].info.bits.busy = 1; |
||
212 | break; |
||
213 | case GET_ENCAPSULATED_RESPONSE: |
||
214 | // Populate dummy_encapsulated_cmd_response first. |
||
215 | inPipes[0].pSrc.bRam = (BYTE*)&dummy_encapsulated_cmd_response; |
||
216 | inPipes[0].info.bits.busy = 1; |
||
217 | break; |
||
218 | //****** End of required commands ******// |
||
219 | |||
220 | #if defined(USB_CDC_SUPPORT_ABSTRACT_CONTROL_MANAGEMENT_CAPABILITIES_D1) |
||
221 | case SET_LINE_CODING: |
||
222 | outPipes[0].wCount.Val = SetupPkt.wLength; |
||
223 | outPipes[0].pDst.bRam = (BYTE*)LINE_CODING_TARGET; |
||
224 | outPipes[0].pFunc = LINE_CODING_PFUNC; |
||
225 | outPipes[0].info.bits.busy = 1; |
||
226 | break; |
||
227 | |||
228 | case GET_LINE_CODING: |
||
229 | USBEP0SendRAMPtr( |
||
230 | (BYTE*)&line_coding, |
||
231 | LINE_CODING_LENGTH, |
||
232 | USB_EP0_INCLUDE_ZERO); |
||
233 | break; |
||
234 | |||
235 | case SET_CONTROL_LINE_STATE: |
||
236 | control_signal_bitmap._byte = (BYTE)SetupPkt.W_Value.v[0]; |
||
237 | CONFIGURE_RTS(control_signal_bitmap.CARRIER_CONTROL); |
||
238 | CONFIGURE_DTR(control_signal_bitmap.DTE_PRESENT); |
||
239 | inPipes[0].info.bits.busy = 1; |
||
240 | break; |
||
241 | #endif |
||
242 | |||
243 | #if defined(USB_CDC_SUPPORT_ABSTRACT_CONTROL_MANAGEMENT_CAPABILITIES_D2) |
||
244 | case SEND_BREAK: // Optional |
||
245 | inPipes[0].info.bits.busy = 1; |
||
246 | if (SetupPkt.wValue == 0xFFFF) |
||
247 | { |
||
248 | UART_ENABLE = 0; // turn off USART |
||
249 | UART_TRISTx = 0; // Make TX pin an output |
||
250 | UART_Tx = 0; // make it low |
||
251 | } |
||
252 | else if (SetupPkt.wValue == 0x0000) |
||
253 | { |
||
254 | UART_ENABLE = 1; // turn on USART |
||
255 | UART_TRISTx = 1; // Make TX pin an input |
||
256 | } |
||
257 | else |
||
258 | { |
||
259 | UART_SEND_BREAK(); |
||
260 | } |
||
261 | break; |
||
262 | #endif |
||
263 | default: |
||
264 | break; |
||
265 | }//end switch(SetupPkt.bRequest) |
||
266 | |||
267 | }//end USBCheckCDCRequest |
||
268 | |||
269 | /** U S E R A P I ***********************************************************/ |
||
270 | |||
271 | /************************************************************************** |
||
272 | Function: |
||
273 | void CDCInitEP(void) |
||
274 | |||
275 | Summary: |
||
276 | This function initializes the CDC function driver. This function should |
||
277 | be called after the SET_CONFIGURATION command. |
||
278 | Description: |
||
279 | This function initializes the CDC function driver. This function sets |
||
280 | the default line coding (baud rate, bit parity, number of data bits, |
||
281 | and format). This function also enables the endpoints and prepares for |
||
282 | the first transfer from the host. |
||
283 | |||
284 | This function should be called after the SET_CONFIGURATION command. |
||
285 | This is most simply done by calling this function from the |
||
286 | USBCBInitEP() function. |
||
287 | |||
288 | Typical Usage: |
||
289 | <code> |
||
290 | void USBCBInitEP(void) |
||
291 | { |
||
292 | CDCInitEP(); |
||
293 | } |
||
294 | </code> |
||
295 | Conditions: |
||
296 | None |
||
297 | Remarks: |
||
298 | None |
||
299 | **************************************************************************/ |
||
300 | void CDCInitEP(void) |
||
301 | { |
||
302 | //Abstract line coding information |
||
303 | line_coding.dwDTERate.Val = 19200; // baud rate |
||
304 | line_coding.bCharFormat = 0x00; // 1 stop bit |
||
305 | line_coding.bParityType = 0x00; // None |
||
306 | line_coding.bDataBits = 0x08; // 5,6,7,8, or 16 |
||
307 | |||
308 | cdc_trf_state = CDC_TX_READY; |
||
309 | cdc_rx_len = 0; |
||
310 | |||
311 | /* |
||
312 | * Do not have to init Cnt of IN pipes here. |
||
313 | * Reason: Number of BYTEs to send to the host |
||
314 | * varies from one transaction to |
||
315 | * another. Cnt should equal the exact |
||
316 | * number of BYTEs to transmit for |
||
317 | * a given IN transaction. |
||
318 | * This number of BYTEs will only |
||
319 | * be known right before the data is |
||
320 | * sent. |
||
321 | */ |
||
322 | USBEnableEndpoint(CDC_COMM_EP,USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP); |
||
323 | USBEnableEndpoint(CDC_DATA_EP,USB_IN_ENABLED|USB_OUT_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP); |
||
324 | |||
325 | CDCDataOutHandle = USBRxOnePacket(CDC_DATA_EP,(BYTE*)&cdc_data_rx,sizeof(cdc_data_rx)); |
||
326 | CDCDataInHandle = NULL; |
||
327 | }//end CDCInitEP |
||
328 | |||
329 | /********************************************************************************** |
||
330 | Function: |
||
331 | BYTE getsUSBUSART(char *buffer, BYTE len) |
||
332 | |||
333 | Summary: |
||
334 | getsUSBUSART copies a string of BYTEs received through USB CDC Bulk OUT |
||
335 | endpoint to a user's specified location. It is a non-blocking function. |
||
336 | It does not wait for data if there is no data available. Instead it |
||
337 | returns '0' to notify the caller that there is no data available. |
||
338 | |||
339 | Description: |
||
340 | getsUSBUSART copies a string of BYTEs received through USB CDC Bulk OUT |
||
341 | endpoint to a user's specified location. It is a non-blocking function. |
||
342 | It does not wait for data if there is no data available. Instead it |
||
343 | returns '0' to notify the caller that there is no data available. |
||
344 | |||
345 | Typical Usage: |
||
346 | <code> |
||
347 | BYTE numBytes; |
||
348 | BYTE buffer[64] |
||
349 | |||
350 | numBytes = getsUSBUSART(buffer,sizeof(buffer)); //until the buffer is free. |
||
351 | if(numBytes \> 0) |
||
352 | { |
||
353 | //we received numBytes bytes of data and they are copied into |
||
354 | // the "buffer" variable. We can do something with the data |
||
355 | // here. |
||
356 | } |
||
357 | </code> |
||
358 | Conditions: |
||
359 | Value of input argument 'len' should be smaller than the maximum |
||
360 | endpoint size responsible for receiving bulk data from USB host for CDC |
||
361 | class. Input argument 'buffer' should point to a buffer area that is |
||
362 | bigger or equal to the size specified by 'len'. |
||
363 | Input: |
||
364 | buffer - Pointer to where received BYTEs are to be stored |
||
365 | len - The number of BYTEs expected. |
||
366 | |||
367 | **********************************************************************************/ |
||
368 | BYTE getsUSBUSART(char *buffer, BYTE len) |
||
369 | { |
||
370 | cdc_rx_len = 0; |
||
371 | |||
372 | if(!USBHandleBusy(CDCDataOutHandle)) |
||
373 | { |
||
374 | /* |
||
375 | * Adjust the expected number of BYTEs to equal |
||
376 | * the actual number of BYTEs received. |
||
377 | */ |
||
378 | if(len > USBHandleGetLength(CDCDataOutHandle)) |
||
379 | len = USBHandleGetLength(CDCDataOutHandle); |
||
380 | |||
381 | /* |
||
382 | * Copy data from dual-ram buffer to user's buffer |
||
383 | */ |
||
384 | for(cdc_rx_len = 0; cdc_rx_len < len; cdc_rx_len++) |
||
385 | buffer[cdc_rx_len] = cdc_data_rx[cdc_rx_len]; |
||
386 | |||
387 | /* |
||
388 | * Prepare dual-ram buffer for next OUT transaction |
||
389 | */ |
||
390 | |||
391 | CDCDataOutHandle = USBRxOnePacket(CDC_DATA_EP,(BYTE*)&cdc_data_rx,sizeof(cdc_data_rx)); |
||
392 | |||
393 | }//end if |
||
394 | |||
395 | return cdc_rx_len; |
||
396 | |||
397 | }//end getsUSBUSART |
||
398 | |||
399 | /****************************************************************************** |
||
400 | Function: |
||
401 | void putUSBUSART(char *data, BYTE length) |
||
402 | |||
403 | Summary: |
||
404 | putUSBUSART writes an array of data to the USB. Use this version, is |
||
405 | capable of transfering 0x00 (what is typically a NULL character in any of |
||
406 | the string transfer functions). |
||
407 | |||
408 | Description: |
||
409 | putUSBUSART writes an array of data to the USB. Use this version, is |
||
410 | capable of transfering 0x00 (what is typically a NULL character in any of |
||
411 | the string transfer functions). |
||
412 | |||
413 | Typical Usage: |
||
414 | <code> |
||
415 | if(USBUSARTIsTxTrfReady()) |
||
416 | { |
||
417 | char data[] = {0x00, 0x01, 0x02, 0x03, 0x04}; |
||
418 | putUSBUSART(data,5); |
||
419 | } |
||
420 | </code> |
||
421 | |||
422 | The transfer mechanism for device-to-host(put) is more flexible than |
||
423 | host-to-device(get). It can handle a string of data larger than the |
||
424 | maximum size of bulk IN endpoint. A state machine is used to transfer a |
||
425 | \long string of data over multiple USB transactions. CDCTxService() |
||
426 | must be called periodically to keep sending blocks of data to the host. |
||
427 | |||
428 | Conditions: |
||
429 | USBUSARTIsTxTrfReady() must return TRUE. This indicates that the last |
||
430 | transfer is complete and is ready to receive a new block of data. The |
||
431 | string of characters pointed to by 'data' must equal to or smaller than |
||
432 | 255 BYTEs. |
||
433 | |||
434 | Input: |
||
435 | char *data - pointer to a RAM array of data to be transfered to the host |
||
436 | BYTE length - the number of bytes to be transfered (must be less than 255). |
||
437 | |||
438 | *****************************************************************************/ |
||
439 | void putUSBUSART(char *data, BYTE length) |
||
440 | { |
||
441 | /* |
||
442 | * User should have checked that cdc_trf_state is in CDC_TX_READY state |
||
443 | * before calling this function. |
||
444 | * As a safety precaution, this fuction checks the state one more time |
||
445 | * to make sure it does not override any pending transactions. |
||
446 | * |
||
447 | * Currently it just quits the routine without reporting any errors back |
||
448 | * to the user. |
||
449 | * |
||
450 | * Bottomline: User MUST make sure that USBUSARTIsTxTrfReady()==1 |
||
451 | * before calling this function! |
||
452 | * Example: |
||
453 | * if(USBUSARTIsTxTrfReady()) |
||
454 | * putUSBUSART(pData, Length); |
||
455 | * |
||
456 | * IMPORTANT: Never use the following blocking while loop to wait: |
||
457 | * while(!USBUSARTIsTxTrfReady()) |
||
458 | * putUSBUSART(pData, Length); |
||
459 | * |
||
460 | * The whole firmware framework is written based on cooperative |
||
461 | * multi-tasking and a blocking code is not acceptable. |
||
462 | * Use a state machine instead. |
||
463 | */ |
||
464 | USBMaskInterrupts(); |
||
465 | if(cdc_trf_state == CDC_TX_READY) |
||
466 | { |
||
467 | mUSBUSARTTxRam((BYTE*)data, length); // See cdc.h |
||
468 | } |
||
469 | USBUnmaskInterrupts(); |
||
470 | }//end putUSBUSART |
||
471 | |||
472 | /****************************************************************************** |
||
473 | Function: |
||
474 | void putsUSBUSART(char *data) |
||
475 | |||
476 | Summary: |
||
477 | putsUSBUSART writes a string of data to the USB including the null |
||
478 | character. Use this version, 'puts', to transfer data from a RAM buffer. |
||
479 | |||
480 | Description: |
||
481 | putsUSBUSART writes a string of data to the USB including the null |
||
482 | character. Use this version, 'puts', to transfer data from a RAM buffer. |
||
483 | |||
484 | Typical Usage: |
||
485 | <code> |
||
486 | if(USBUSARTIsTxTrfReady()) |
||
487 | { |
||
488 | char data[] = "Hello World"; |
||
489 | putsUSBUSART(data); |
||
490 | } |
||
491 | </code> |
||
492 | |||
493 | The transfer mechanism for device-to-host(put) is more flexible than |
||
494 | host-to-device(get). It can handle a string of data larger than the |
||
495 | maximum size of bulk IN endpoint. A state machine is used to transfer a |
||
496 | \long string of data over multiple USB transactions. CDCTxService() |
||
497 | must be called periodically to keep sending blocks of data to the host. |
||
498 | |||
499 | Conditions: |
||
500 | USBUSARTIsTxTrfReady() must return TRUE. This indicates that the last |
||
501 | transfer is complete and is ready to receive a new block of data. The |
||
502 | string of characters pointed to by 'data' must equal to or smaller than |
||
503 | 255 BYTEs. |
||
504 | |||
505 | Input: |
||
506 | char *data - null\-terminated string of constant data. If a |
||
507 | null character is not found, 255 BYTEs of data |
||
508 | will be transferred to the host. |
||
509 | |||
510 | *****************************************************************************/ |
||
511 | |||
512 | void putsUSBUSART(char *data) |
||
513 | { |
||
514 | BYTE len; |
||
515 | char *pData; |
||
516 | |||
517 | /* |
||
518 | * User should have checked that cdc_trf_state is in CDC_TX_READY state |
||
519 | * before calling this function. |
||
520 | * As a safety precaution, this fuction checks the state one more time |
||
521 | * to make sure it does not override any pending transactions. |
||
522 | * |
||
523 | * Currently it just quits the routine without reporting any errors back |
||
524 | * to the user. |
||
525 | * |
||
526 | * Bottomline: User MUST make sure that USBUSARTIsTxTrfReady()==1 |
||
527 | * before calling this function! |
||
528 | * Example: |
||
529 | * if(USBUSARTIsTxTrfReady()) |
||
530 | * putsUSBUSART(pData, Length); |
||
531 | * |
||
532 | * IMPORTANT: Never use the following blocking while loop to wait: |
||
533 | * while(!USBUSARTIsTxTrfReady()) |
||
534 | * putsUSBUSART(pData); |
||
535 | * |
||
536 | * The whole firmware framework is written based on cooperative |
||
537 | * multi-tasking and a blocking code is not acceptable. |
||
538 | * Use a state machine instead. |
||
539 | */ |
||
540 | USBMaskInterrupts(); |
||
541 | if(cdc_trf_state != CDC_TX_READY) |
||
542 | { |
||
543 | USBUnmaskInterrupts(); |
||
544 | return; |
||
545 | } |
||
546 | |||
547 | /* |
||
548 | * While loop counts the number of BYTEs to send including the |
||
549 | * null character. |
||
550 | */ |
||
551 | len = 0; |
||
552 | pData = data; |
||
553 | do |
||
554 | { |
||
555 | len++; |
||
556 | if(len == 255) break; // Break loop once max len is reached. |
||
557 | }while(*pData++); |
||
558 | |||
559 | /* |
||
560 | * Second piece of information (length of data to send) is ready. |
||
561 | * Call mUSBUSARTTxRam to setup the transfer. |
||
562 | * The actual transfer process will be handled by CDCTxService(), |
||
563 | * which should be called once per Main Program loop. |
||
564 | */ |
||
565 | mUSBUSARTTxRam((BYTE*)data, len); // See cdc.h |
||
566 | USBUnmaskInterrupts(); |
||
567 | }//end putsUSBUSART |
||
568 | |||
569 | /************************************************************************** |
||
570 | Function: |
||
571 | void putrsUSBUSART(const ROM char *data) |
||
572 | |||
573 | Summary: |
||
574 | putrsUSBUSART writes a string of data to the USB including the null |
||
575 | character. Use this version, 'putrs', to transfer data literals and |
||
576 | data located in program memory. |
||
577 | |||
578 | Description: |
||
579 | putrsUSBUSART writes a string of data to the USB including the null |
||
580 | character. Use this version, 'putrs', to transfer data literals and |
||
581 | data located in program memory. |
||
582 | |||
583 | Typical Usage: |
||
584 | <code> |
||
585 | if(USBUSARTIsTxTrfReady()) |
||
586 | { |
||
587 | putrsUSBUSART("Hello World"); |
||
588 | } |
||
589 | </code> |
||
590 | |||
591 | The transfer mechanism for device-to-host(put) is more flexible than |
||
592 | host-to-device(get). It can handle a string of data larger than the |
||
593 | maximum size of bulk IN endpoint. A state machine is used to transfer a |
||
594 | \long string of data over multiple USB transactions. CDCTxService() |
||
595 | must be called periodically to keep sending blocks of data to the host. |
||
596 | |||
597 | Conditions: |
||
598 | USBUSARTIsTxTrfReady() must return TRUE. This indicates that the last |
||
599 | transfer is complete and is ready to receive a new block of data. The |
||
600 | string of characters pointed to by 'data' must equal to or smaller than |
||
601 | 255 BYTEs. |
||
602 | |||
603 | Input: |
||
604 | const ROM char *data - null\-terminated string of constant data. If a |
||
605 | null character is not found, 255 BYTEs of data |
||
606 | will be transferred to the host. |
||
607 | |||
608 | **************************************************************************/ |
||
609 | void putrsUSBUSART(const ROM char *data) |
||
610 | { |
||
611 | BYTE len; |
||
612 | const ROM char *pData; |
||
613 | |||
614 | /* |
||
615 | * User should have checked that cdc_trf_state is in CDC_TX_READY state |
||
616 | * before calling this function. |
||
617 | * As a safety precaution, this fuction checks the state one more time |
||
618 | * to make sure it does not override any pending transactions. |
||
619 | * |
||
620 | * Currently it just quits the routine without reporting any errors back |
||
621 | * to the user. |
||
622 | * |
||
623 | * Bottomline: User MUST make sure that USBUSARTIsTxTrfReady() |
||
624 | * before calling this function! |
||
625 | * Example: |
||
626 | * if(USBUSARTIsTxTrfReady()) |
||
627 | * putsUSBUSART(pData); |
||
628 | * |
||
629 | * IMPORTANT: Never use the following blocking while loop to wait: |
||
630 | * while(cdc_trf_state != CDC_TX_READY) |
||
631 | * putsUSBUSART(pData); |
||
632 | * |
||
633 | * The whole firmware framework is written based on cooperative |
||
634 | * multi-tasking and a blocking code is not acceptable. |
||
635 | * Use a state machine instead. |
||
636 | */ |
||
637 | USBMaskInterrupts(); |
||
638 | if(cdc_trf_state != CDC_TX_READY) |
||
639 | { |
||
640 | USBUnmaskInterrupts(); |
||
641 | return; |
||
642 | } |
||
643 | |||
644 | /* |
||
645 | * While loop counts the number of BYTEs to send including the |
||
646 | * null character. |
||
647 | */ |
||
648 | len = 0; |
||
649 | pData = data; |
||
650 | do |
||
651 | { |
||
652 | len++; |
||
653 | if(len == 255) break; // Break loop once max len is reached. |
||
654 | }while(*pData++); |
||
655 | |||
656 | /* |
||
657 | * Second piece of information (length of data to send) is ready. |
||
658 | * Call mUSBUSARTTxRom to setup the transfer. |
||
659 | * The actual transfer process will be handled by CDCTxService(), |
||
660 | * which should be called once per Main Program loop. |
||
661 | */ |
||
662 | |||
663 | mUSBUSARTTxRom((ROM BYTE*)data,len); // See cdc.h |
||
664 | USBUnmaskInterrupts(); |
||
665 | |||
666 | }//end putrsUSBUSART |
||
667 | |||
668 | /************************************************************************ |
||
669 | Function: |
||
670 | void CDCTxService(void) |
||
671 | |||
672 | Summary: |
||
673 | CDCTxService handles device-to-host transaction(s). This function |
||
674 | should be called once per Main Program loop after the device reaches |
||
675 | the configured state. |
||
676 | Description: |
||
677 | CDCTxService handles device-to-host transaction(s). This function |
||
678 | should be called once per Main Program loop after the device reaches |
||
679 | the configured state. |
||
680 | |||
681 | Typical Usage: |
||
682 | <code> |
||
683 | void main(void) |
||
684 | { |
||
685 | USBDeviceInit(); |
||
686 | while(1) |
||
687 | { |
||
688 | USBDeviceTasks(); |
||
689 | if((USBGetDeviceState() \< CONFIGURED_STATE) || |
||
690 | (USBIsDeviceSuspended() == TRUE)) |
||
691 | { |
||
692 | //Either the device is not configured or we are suspended |
||
693 | // so we don't want to do execute any application code |
||
694 | continue; //go back to the top of the while loop |
||
695 | } |
||
696 | else |
||
697 | { |
||
698 | //Keep trying to send data to the PC as required |
||
699 | CDCTxService(); |
||
700 | |||
701 | //Run application code. |
||
702 | UserApplication(); |
||
703 | } |
||
704 | } |
||
705 | } |
||
706 | </code> |
||
707 | Conditions: |
||
708 | None |
||
709 | Remarks: |
||
710 | None |
||
711 | ************************************************************************/ |
||
712 | |||
713 | void CDCTxService(void) |
||
714 | { |
||
715 | BYTE byte_to_send; |
||
716 | BYTE i; |
||
717 | |||
718 | USBMaskInterrupts(); |
||
719 | if(USBHandleBusy(CDCDataInHandle)) |
||
720 | { |
||
721 | USBUnmaskInterrupts(); |
||
722 | return; |
||
723 | } |
||
724 | |||
725 | /* |
||
726 | * Completing stage is necessary while [ mCDCUSartTxIsBusy()==1 ]. |
||
727 | * By having this stage, user can always check cdc_trf_state, |
||
728 | * and not having to call mCDCUsartTxIsBusy() directly. |
||
729 | */ |
||
730 | if(cdc_trf_state == CDC_TX_COMPLETING) |
||
731 | cdc_trf_state = CDC_TX_READY; |
||
732 | |||
733 | /* |
||
734 | * If CDC_TX_READY state, nothing to do, just return. |
||
735 | */ |
||
736 | if(cdc_trf_state == CDC_TX_READY) |
||
737 | { |
||
738 | USBUnmaskInterrupts(); |
||
739 | return; |
||
740 | } |
||
741 | |||
742 | /* |
||
743 | * If CDC_TX_BUSY_ZLP state, send zero length packet |
||
744 | */ |
||
745 | if(cdc_trf_state == CDC_TX_BUSY_ZLP) |
||
746 | { |
||
747 | CDCDataInHandle = USBTxOnePacket(CDC_DATA_EP,NULL,0); |
||
748 | //CDC_DATA_BD_IN.CNT = 0; |
||
749 | cdc_trf_state = CDC_TX_COMPLETING; |
||
750 | } |
||
751 | else if(cdc_trf_state == CDC_TX_BUSY) |
||
752 | { |
||
753 | /* |
||
754 | * First, have to figure out how many byte of data to send. |
||
755 | */ |
||
756 | if(cdc_tx_len > sizeof(cdc_data_tx)) |
||
757 | byte_to_send = sizeof(cdc_data_tx); |
||
758 | else |
||
759 | byte_to_send = cdc_tx_len; |
||
760 | |||
761 | /* |
||
762 | * Subtract the number of bytes just about to be sent from the total. |
||
763 | */ |
||
764 | cdc_tx_len = cdc_tx_len - byte_to_send; |
||
765 | |||
766 | pCDCDst.bRam = (BYTE*)&cdc_data_tx; // Set destination pointer |
||
767 | |||
768 | i = byte_to_send; |
||
769 | if(cdc_mem_type == USB_EP0_ROM) // Determine type of memory source |
||
770 | { |
||
771 | while(i) |
||
772 | { |
||
773 | *pCDCDst.bRam = *pCDCSrc.bRom; |
||
774 | pCDCDst.bRam++; |
||
775 | pCDCSrc.bRom++; |
||
776 | i--; |
||
777 | }//end while(byte_to_send) |
||
778 | } |
||
779 | else // _RAM |
||
780 | { |
||
781 | while(i) |
||
782 | { |
||
783 | *pCDCDst.bRam = *pCDCSrc.bRam; |
||
784 | pCDCDst.bRam++; |
||
785 | pCDCSrc.bRam++; |
||
786 | i--; |
||
787 | }//end while(byte_to_send._word) |
||
788 | }//end if(cdc_mem_type...) |
||
789 | |||
790 | /* |
||
791 | * Lastly, determine if a zero length packet state is necessary. |
||
792 | * See explanation in USB Specification 2.0: Section 5.8.3 |
||
793 | */ |
||
794 | if(cdc_tx_len == 0) |
||
795 | { |
||
796 | if(byte_to_send == CDC_DATA_IN_EP_SIZE) |
||
797 | cdc_trf_state = CDC_TX_BUSY_ZLP; |
||
798 | else |
||
799 | cdc_trf_state = CDC_TX_COMPLETING; |
||
800 | }//end if(cdc_tx_len...) |
||
801 | CDCDataInHandle = USBTxOnePacket(CDC_DATA_EP,(BYTE*)&cdc_data_tx,byte_to_send); |
||
802 | |||
803 | }//end if(cdc_tx_sate == CDC_TX_BUSY) |
||
804 | USBUnmaskInterrupts(); |
||
805 | }//end CDCTxService |
||
806 | |||
807 | #endif //USB_USE_CDC |
||
808 | |||
809 | /** EOF cdc.c ****************************************************************/ |
Powered by WebSVN v2.8.3