2 * at91sam7s USB CDC device implementation
4 * Copyright (c) 2012, Roel Verdult
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the copyright holders nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * based on the "Basic USB Example" from ATMEL (doc6123.pdf)
37 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
38 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
39 #define AT91C_EP_IN_SIZE 0x40
40 #define AT91C_EP_OUT 1
41 #define AT91C_EP_OUT_SIZE 0x40
44 const char devDescriptor
[] = {
45 /* Device descriptor */
47 0x01, // bDescriptorType
48 0x10,0x01, // Complies with USB Spec. Release (0110h = release 1.10)
49 0x02, // bDeviceClass: CDC class code
50 0x00, // bDeviceSubclass: CDC class sub code
51 0x00, // bDeviceProtocol: CDC Device protocol
52 0x08, // bMaxPacketSize0
53 0xeb,0x03, // Vendor ID (random numbers)
54 0x25,0x61, // Product ID (random numbers)
55 0x01,0x00, // Device release number (0001)
56 0x01, // iManufacturer // 0x01
62 const char cfgDescriptor
[] = {
63 /* ============== CONFIGURATION 1 =========== */
64 /* Configuration 1 descriptor */
66 0x02, // CbDescriptorType
67 0x43, // CwTotalLength 2 EP + Control
69 0x02, // CbNumInterfaces
70 0x01, // CbConfigurationValue
71 0x00, // CiConfiguration
72 0xC0, // CbmAttributes 0xA0
75 /* Communication Class Interface Descriptor Requirement */
77 0x04, // bDescriptorType
78 0x00, // bInterfaceNumber
79 0x00, // bAlternateSetting
80 0x01, // bNumEndpoints
81 0x02, // bInterfaceClass
82 0x02, // bInterfaceSubclass
83 0x00, // bInterfaceProtocol
86 /* Header Functional Descriptor */
87 0x05, // bFunction Length
88 0x24, // bDescriptor type: CS_INTERFACE
89 0x00, // bDescriptor subtype: Header Func Desc
93 /* ACM Functional Descriptor */
94 0x04, // bFunctionLength
95 0x24, // bDescriptor Type: CS_INTERFACE
96 0x02, // bDescriptor Subtype: ACM Func Desc
97 0x00, // bmCapabilities
99 /* Union Functional Descriptor */
100 0x05, // bFunctionLength
101 0x24, // bDescriptorType: CS_INTERFACE
102 0x06, // bDescriptor Subtype: Union Func Desc
103 0x00, // bMasterInterface: Communication Class Interface
104 0x01, // bSlaveInterface0: Data Class Interface
106 /* Call Management Functional Descriptor */
107 0x05, // bFunctionLength
108 0x24, // bDescriptor Type: CS_INTERFACE
109 0x01, // bDescriptor Subtype: Call Management Func Desc
110 0x00, // bmCapabilities: D1 + D0
111 0x01, // bDataInterface: Data Class Interface 1
113 /* Endpoint 1 descriptor */
115 0x05, // bDescriptorType
116 0x83, // bEndpointAddress, Endpoint 03 - IN
117 0x03, // bmAttributes INT
118 0x08, // wMaxPacketSize
122 /* Data Class Interface Descriptor Requirement */
124 0x04, // bDescriptorType
125 0x01, // bInterfaceNumber
126 0x00, // bAlternateSetting
127 0x02, // bNumEndpoints
128 0x0A, // bInterfaceClass
129 0x00, // bInterfaceSubclass
130 0x00, // bInterfaceProtocol
133 /* First alternate setting */
134 /* Endpoint 1 descriptor */
136 0x05, // bDescriptorType
137 0x01, // bEndpointAddress, Endpoint 01 - OUT
138 0x02, // bmAttributes BULK
139 AT91C_EP_OUT_SIZE
, // wMaxPacketSize
143 /* Endpoint 2 descriptor */
145 0x05, // bDescriptorType
146 0x82, // bEndpointAddress, Endpoint 02 - IN
147 0x02, // bmAttributes BULK
148 AT91C_EP_IN_SIZE
, // wMaxPacketSize
153 const char strDescriptor
[] = {
155 0x03, // Type is string
171 /* USB standard request code */
172 #define STD_GET_STATUS_ZERO 0x0080
173 #define STD_GET_STATUS_INTERFACE 0x0081
174 #define STD_GET_STATUS_ENDPOINT 0x0082
176 #define STD_CLEAR_FEATURE_ZERO 0x0100
177 #define STD_CLEAR_FEATURE_INTERFACE 0x0101
178 #define STD_CLEAR_FEATURE_ENDPOINT 0x0102
180 #define STD_SET_FEATURE_ZERO 0x0300
181 #define STD_SET_FEATURE_INTERFACE 0x0301
182 #define STD_SET_FEATURE_ENDPOINT 0x0302
184 #define STD_SET_ADDRESS 0x0500
185 #define STD_GET_DESCRIPTOR 0x0680
186 #define STD_SET_DESCRIPTOR 0x0700
187 #define STD_GET_CONFIGURATION 0x0880
188 #define STD_SET_CONFIGURATION 0x0900
189 #define STD_GET_INTERFACE 0x0A81
190 #define STD_SET_INTERFACE 0x0B01
191 #define STD_SYNCH_FRAME 0x0C82
193 /* CDC Class Specific Request Code */
194 #define GET_LINE_CODING 0x21A1
195 #define SET_LINE_CODING 0x2021
196 #define SET_CONTROL_LINE_STATE 0x2221
199 unsigned int dwDTERRate
;
203 } AT91S_CDC_LINE_CODING
, *AT91PS_CDC_LINE_CODING
;
205 AT91S_CDC_LINE_CODING line
= {
211 void AT91F_CDC_Enumerate();
213 AT91PS_UDP pUdp
= AT91C_BASE_UDP
;
214 byte_t btConfiguration
= 0;
215 byte_t btConnection
= 0;
216 byte_t btReceiveBank
= AT91C_UDP_RX_DATA_BK0
;
218 //*----------------------------------------------------------------------------
219 //* \fn AT91F_USB_Enable
220 //* \brief This function Activates the USB device
221 //*----------------------------------------------------------------------------
224 // Set the PLL USB Divider
225 AT91C_BASE_CKGR
->CKGR_PLLR
|= AT91C_CKGR_USBDIV_1
;
227 // Specific Chip USB Initialisation
228 // Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock
229 AT91C_BASE_PMC
->PMC_SCER
= AT91C_PMC_UDP
;
230 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_UDP
);
232 // Enable UDP PullUp (USB_DP_PUP) : enable & Clear of the corresponding PIO
233 // Set in PIO mode and Configure in Output
234 AT91C_BASE_PIOA
->PIO_PER
= AT91C_PIO_PA16
; // Set in PIO mode
235 AT91C_BASE_PIOA
->PIO_OER
= AT91C_PIO_PA16
; // Configure as Output
237 // Clear for set the Pul up resistor
238 AT91C_BASE_PIOA
->PIO_CODR
= AT91C_PIO_PA16
;
241 //*----------------------------------------------------------------------------
242 //* \fn AT91F_UDP_IsConfigured
243 //* \brief Test if the device is configured and handle enumeration
244 //*----------------------------------------------------------------------------
247 AT91_REG isr
= pUdp
->UDP_ISR
;
249 if (isr
& AT91C_UDP_ENDBUSRES
) {
250 pUdp
->UDP_ICR
= AT91C_UDP_ENDBUSRES
;
251 // reset all endpoints
252 pUdp
->UDP_RSTEP
= (unsigned int)-1;
254 // Enable the function
255 pUdp
->UDP_FADDR
= AT91C_UDP_FEN
;
256 // Configure endpoint 0
257 pUdp
->UDP_CSR
[0] = (AT91C_UDP_EPEDS
| AT91C_UDP_EPTYPE_CTRL
);
259 else if (isr
& AT91C_UDP_EPINT0
) {
260 pUdp
->UDP_ICR
= AT91C_UDP_EPINT0
;
261 AT91F_CDC_Enumerate();
263 return (btConfiguration
) ? true : false;
269 if (!usb_check()) return false;
270 return (pUdp
->UDP_CSR
[AT91C_EP_OUT
] & btReceiveBank
);
273 //*----------------------------------------------------------------------------
274 //* \fn AT91F_UDP_Read
275 //* \brief Read available data from Endpoint OUT
276 //*----------------------------------------------------------------------------
277 uint32_t usb_read(byte_t
* data
, size_t len
)
279 byte_t bank
= btReceiveBank
;
280 uint32_t packetSize
, nbBytesRcv
= 0;
281 uint32_t time_out
= 0;
285 if (!usb_check()) break;
287 if ( pUdp
->UDP_CSR
[AT91C_EP_OUT
] & bank
) {
288 packetSize
= MIN(pUdp
->UDP_CSR
[AT91C_EP_OUT
] >> 16, len
);
291 data
[nbBytesRcv
++] = pUdp
->UDP_FDR
[AT91C_EP_OUT
];
292 pUdp
->UDP_CSR
[AT91C_EP_OUT
] &= ~(bank
);
293 if (bank
== AT91C_UDP_RX_DATA_BK0
)
295 bank
= AT91C_UDP_RX_DATA_BK1
;
297 bank
= AT91C_UDP_RX_DATA_BK0
;
300 if (time_out
++ == 0x1fff) break;
303 btReceiveBank
= bank
;
307 //*----------------------------------------------------------------------------
308 //* \fn AT91F_CDC_Write
309 //* \brief Send through endpoint 2
310 //*----------------------------------------------------------------------------
311 uint32_t usb_write(const byte_t
* data
, const size_t len
)
316 if (!length
) return 0;
317 if (!usb_check()) return 0;
319 // Send the first packet
320 cpt
= MIN(length
, AT91C_EP_IN_SIZE
-1);
322 while (cpt
--) pUdp
->UDP_FDR
[AT91C_EP_IN
] = *data
++;
323 pUdp
->UDP_CSR
[AT91C_EP_IN
] |= AT91C_UDP_TXPKTRDY
;
326 // Fill the second bank
327 cpt
= MIN(length
, AT91C_EP_IN_SIZE
-1);
329 while (cpt
--) pUdp
->UDP_FDR
[AT91C_EP_IN
] = *data
++;
330 // Wait for the the first bank to be sent
331 while (!(pUdp
->UDP_CSR
[AT91C_EP_IN
] & AT91C_UDP_TXCOMP
)) {
332 if (!usb_check()) return length
;
334 pUdp
->UDP_CSR
[AT91C_EP_IN
] &= ~(AT91C_UDP_TXCOMP
);
335 while (pUdp
->UDP_CSR
[AT91C_EP_IN
] & AT91C_UDP_TXCOMP
);
336 pUdp
->UDP_CSR
[AT91C_EP_IN
] |= AT91C_UDP_TXPKTRDY
;
339 // Wait for the end of transfer
340 while (!(pUdp
->UDP_CSR
[AT91C_EP_IN
] & AT91C_UDP_TXCOMP
)) {
341 if (!usb_check()) return length
;
344 pUdp
->UDP_CSR
[AT91C_EP_IN
] &= ~(AT91C_UDP_TXCOMP
);
345 while (pUdp
->UDP_CSR
[AT91C_EP_IN
] & AT91C_UDP_TXCOMP
);
350 //*----------------------------------------------------------------------------
351 //* \fn AT91F_USB_SendData
352 //* \brief Send Data through the control endpoint
353 //*----------------------------------------------------------------------------
354 unsigned int csrTab
[100];
355 unsigned char csrIdx
= 0;
357 static void AT91F_USB_SendData(AT91PS_UDP pUdp
, const char *pData
, uint32_t length
)
363 cpt
= MIN(length
, 8);
367 pUdp
->UDP_FDR
[0] = *pData
++;
369 if (pUdp
->UDP_CSR
[0] & AT91C_UDP_TXCOMP
) {
370 pUdp
->UDP_CSR
[0] &= ~(AT91C_UDP_TXCOMP
);
371 while (pUdp
->UDP_CSR
[0] & AT91C_UDP_TXCOMP
);
374 pUdp
->UDP_CSR
[0] |= AT91C_UDP_TXPKTRDY
;
376 csr
= pUdp
->UDP_CSR
[0];
378 // Data IN stage has been stopped by a status OUT
379 if (csr
& AT91C_UDP_RX_DATA_BK0
) {
380 pUdp
->UDP_CSR
[0] &= ~(AT91C_UDP_RX_DATA_BK0
);
383 } while ( !(csr
& AT91C_UDP_TXCOMP
) );
387 if (pUdp
->UDP_CSR
[0] & AT91C_UDP_TXCOMP
) {
388 pUdp
->UDP_CSR
[0] &= ~(AT91C_UDP_TXCOMP
);
389 while (pUdp
->UDP_CSR
[0] & AT91C_UDP_TXCOMP
);
393 //*----------------------------------------------------------------------------
394 //* \fn AT91F_USB_SendZlp
395 //* \brief Send zero length packet through the control endpoint
396 //*----------------------------------------------------------------------------
397 void AT91F_USB_SendZlp(AT91PS_UDP pUdp
)
399 pUdp
->UDP_CSR
[0] |= AT91C_UDP_TXPKTRDY
;
400 while ( !(pUdp
->UDP_CSR
[0] & AT91C_UDP_TXCOMP
) );
401 pUdp
->UDP_CSR
[0] &= ~(AT91C_UDP_TXCOMP
);
402 while (pUdp
->UDP_CSR
[0] & AT91C_UDP_TXCOMP
);
405 //*----------------------------------------------------------------------------
406 //* \fn AT91F_USB_SendStall
407 //* \brief Stall the control endpoint
408 //*----------------------------------------------------------------------------
409 void AT91F_USB_SendStall(AT91PS_UDP pUdp
)
411 pUdp
->UDP_CSR
[0] |= AT91C_UDP_FORCESTALL
;
412 while ( !(pUdp
->UDP_CSR
[0] & AT91C_UDP_ISOERROR
) );
413 pUdp
->UDP_CSR
[0] &= ~(AT91C_UDP_FORCESTALL
| AT91C_UDP_ISOERROR
);
414 while (pUdp
->UDP_CSR
[0] & (AT91C_UDP_FORCESTALL
| AT91C_UDP_ISOERROR
));
417 //*----------------------------------------------------------------------------
418 //* \fn AT91F_CDC_Enumerate
419 //* \brief This function is a callback invoked when a SETUP packet is received
420 //*----------------------------------------------------------------------------
421 void AT91F_CDC_Enumerate()
423 byte_t bmRequestType
, bRequest
;
424 uint16_t wValue
, wIndex
, wLength
, wStatus
;
426 if ( !(pUdp
->UDP_CSR
[0] & AT91C_UDP_RXSETUP
) )
429 bmRequestType
= pUdp
->UDP_FDR
[0];
430 bRequest
= pUdp
->UDP_FDR
[0];
431 wValue
= (pUdp
->UDP_FDR
[0] & 0xFF);
432 wValue
|= (pUdp
->UDP_FDR
[0] << 8);
433 wIndex
= (pUdp
->UDP_FDR
[0] & 0xFF);
434 wIndex
|= (pUdp
->UDP_FDR
[0] << 8);
435 wLength
= (pUdp
->UDP_FDR
[0] & 0xFF);
436 wLength
|= (pUdp
->UDP_FDR
[0] << 8);
438 if (bmRequestType
& 0x80) {
439 pUdp
->UDP_CSR
[0] |= AT91C_UDP_DIR
;
440 while ( !(pUdp
->UDP_CSR
[0] & AT91C_UDP_DIR
) );
442 pUdp
->UDP_CSR
[0] &= ~AT91C_UDP_RXSETUP
;
443 while ( (pUdp
->UDP_CSR
[0] & AT91C_UDP_RXSETUP
) );
445 // Handle supported standard device request Cf Table 9-3 in USB specification Rev 1.1
446 switch ((bRequest
<< 8) | bmRequestType
) {
447 case STD_GET_DESCRIPTOR
:
448 if (wValue
== 0x100) // Return Device Descriptor
449 AT91F_USB_SendData(pUdp
, devDescriptor
, MIN(sizeof(devDescriptor
), wLength
));
450 else if (wValue
== 0x200) // Return Configuration Descriptor
451 AT91F_USB_SendData(pUdp
, cfgDescriptor
, MIN(sizeof(cfgDescriptor
), wLength
));
452 else if ((wValue
& 0x300) == 0x300) // Return String Descriptor
453 AT91F_USB_SendData(pUdp
, strDescriptor
, MIN(sizeof(strDescriptor
), wLength
));
455 AT91F_USB_SendStall(pUdp
);
457 case STD_SET_ADDRESS
:
458 AT91F_USB_SendZlp(pUdp
);
459 pUdp
->UDP_FADDR
= (AT91C_UDP_FEN
| wValue
);
460 pUdp
->UDP_GLBSTATE
= (wValue
) ? AT91C_UDP_FADDEN
: 0;
462 case STD_SET_CONFIGURATION
:
463 btConfiguration
= wValue
;
464 AT91F_USB_SendZlp(pUdp
);
465 pUdp
->UDP_GLBSTATE
= (wValue
) ? AT91C_UDP_CONFG
: AT91C_UDP_FADDEN
;
466 pUdp
->UDP_CSR
[1] = (wValue
) ? (AT91C_UDP_EPEDS
| AT91C_UDP_EPTYPE_BULK_OUT
) : 0;
467 pUdp
->UDP_CSR
[2] = (wValue
) ? (AT91C_UDP_EPEDS
| AT91C_UDP_EPTYPE_BULK_IN
) : 0;
468 pUdp
->UDP_CSR
[3] = (wValue
) ? (AT91C_UDP_EPEDS
| AT91C_UDP_EPTYPE_INT_IN
) : 0;
470 case STD_GET_CONFIGURATION
:
471 AT91F_USB_SendData(pUdp
, (char *) &(btConfiguration
), sizeof(btConfiguration
));
473 case STD_GET_STATUS_ZERO
:
475 AT91F_USB_SendData(pUdp
, (char *) &wStatus
, sizeof(wStatus
));
477 case STD_GET_STATUS_INTERFACE
:
479 AT91F_USB_SendData(pUdp
, (char *) &wStatus
, sizeof(wStatus
));
481 case STD_GET_STATUS_ENDPOINT
:
484 if ((pUdp
->UDP_GLBSTATE
& AT91C_UDP_CONFG
) && (wIndex
<= 3)) {
485 wStatus
= (pUdp
->UDP_CSR
[wIndex
] & AT91C_UDP_EPEDS
) ? 0 : 1;
486 AT91F_USB_SendData(pUdp
, (char *) &wStatus
, sizeof(wStatus
));
488 else if ((pUdp
->UDP_GLBSTATE
& AT91C_UDP_FADDEN
) && (wIndex
== 0)) {
489 wStatus
= (pUdp
->UDP_CSR
[wIndex
] & AT91C_UDP_EPEDS
) ? 0 : 1;
490 AT91F_USB_SendData(pUdp
, (char *) &wStatus
, sizeof(wStatus
));
493 AT91F_USB_SendStall(pUdp
);
495 case STD_SET_FEATURE_ZERO
:
496 AT91F_USB_SendStall(pUdp
);
498 case STD_SET_FEATURE_INTERFACE
:
499 AT91F_USB_SendZlp(pUdp
);
501 case STD_SET_FEATURE_ENDPOINT
:
503 if ((wValue
== 0) && wIndex
&& (wIndex
<= 3)) {
504 pUdp
->UDP_CSR
[wIndex
] = 0;
505 AT91F_USB_SendZlp(pUdp
);
508 AT91F_USB_SendStall(pUdp
);
510 case STD_CLEAR_FEATURE_ZERO
:
511 AT91F_USB_SendStall(pUdp
);
513 case STD_CLEAR_FEATURE_INTERFACE
:
514 AT91F_USB_SendZlp(pUdp
);
516 case STD_CLEAR_FEATURE_ENDPOINT
:
518 if ((wValue
== 0) && wIndex
&& (wIndex
<= 3)) {
520 pUdp
->UDP_CSR
[1] = (AT91C_UDP_EPEDS
| AT91C_UDP_EPTYPE_BULK_OUT
);
521 else if (wIndex
== 2)
522 pUdp
->UDP_CSR
[2] = (AT91C_UDP_EPEDS
| AT91C_UDP_EPTYPE_BULK_IN
);
523 else if (wIndex
== 3)
524 pUdp
->UDP_CSR
[3] = (AT91C_UDP_EPEDS
| AT91C_UDP_EPTYPE_ISO_IN
);
525 AT91F_USB_SendZlp(pUdp
);
528 AT91F_USB_SendStall(pUdp
);
531 // handle CDC class requests
532 case SET_LINE_CODING
:
533 while ( !(pUdp
->UDP_CSR
[0] & AT91C_UDP_RX_DATA_BK0
) );
534 pUdp
->UDP_CSR
[0] &= ~(AT91C_UDP_RX_DATA_BK0
);
535 AT91F_USB_SendZlp(pUdp
);
537 case GET_LINE_CODING
:
538 AT91F_USB_SendData(pUdp
, (char *) &line
, MIN(sizeof(line
), wLength
));
540 case SET_CONTROL_LINE_STATE
:
541 btConnection
= wValue
;
542 AT91F_USB_SendZlp(pUdp
);
545 AT91F_USB_SendStall(pUdp
);