1 /* Copyright (c) 2010-2011 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 #include "USBBusInterface.h"
23 static uint8_t cdc_line_coding
[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
25 #define DEFAULT_CONFIGURATION (1)
27 #define CDC_SET_LINE_CODING 0x20
28 #define CDC_GET_LINE_CODING 0x21
29 #define CDC_SET_CONTROL_LINE_STATE 0x22
31 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
33 USBCDC::USBCDC(uint16_t vendor_id
, uint16_t product_id
, uint16_t product_release
): USBDevice(vendor_id
, product_id
, product_release
) {
37 bool USBCDC::USBCallback_request(void) {
38 /* Called in ISR context */
41 CONTROL_TRANSFER
* transfer
= getTransferPtr();
43 /* Process class-specific requests */
45 if (transfer
->setup
.bmRequestType
.Type
== CLASS_TYPE
) {
46 switch (transfer
->setup
.bRequest
) {
47 case CDC_GET_LINE_CODING
:
48 transfer
->remaining
= 7;
49 transfer
->ptr
= cdc_line_coding
;
50 transfer
->direction
= DEVICE_TO_HOST
;
53 case CDC_SET_LINE_CODING
:
54 transfer
->remaining
= 7;
57 case CDC_SET_CONTROL_LINE_STATE
:
69 // Called in ISR context
70 // Set configuration. Return false if the
71 // configuration is not supported.
72 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration
) {
73 if (configuration
!= DEFAULT_CONFIGURATION
) {
77 // Configure endpoints > 0
78 addEndpoint(EPINT_IN
, MAX_PACKET_SIZE_EPINT
);
79 addEndpoint(EPBULK_IN
, MAX_PACKET_SIZE_EPBULK
);
80 addEndpoint(EPBULK_OUT
, MAX_PACKET_SIZE_EPBULK
);
82 // We activate the endpoint to be able to recceive data
83 readStart(EPBULK_OUT
, MAX_PACKET_SIZE_EPBULK
);
87 bool USBCDC::send(uint8_t * buffer
, uint16_t size
) {
88 return USBDevice::write(EPBULK_IN
, buffer
, size
, MAX_CDC_REPORT_SIZE
);
91 bool USBCDC::readEP(uint8_t * buffer
, uint16_t * size
) {
92 if (!USBDevice::readEP(EPBULK_OUT
, buffer
, size
, MAX_CDC_REPORT_SIZE
))
94 if (!readStart(EPBULK_OUT
, MAX_CDC_REPORT_SIZE
))
99 bool USBCDC::readEP_NB(uint8_t * buffer
, uint16_t * size
) {
100 if (!USBDevice::readEP_NB(EPBULK_OUT
, buffer
, size
, MAX_CDC_REPORT_SIZE
))
102 if (!readStart(EPBULK_OUT
, MAX_CDC_REPORT_SIZE
))
108 uint8_t * USBCDC::deviceDesc() {
109 static uint8_t deviceDescriptor
[] = {
111 1, // bDescriptorType
112 0x10, 0x01, // bcdUSB
114 0, // bDeviceSubClass
115 0, // bDeviceProtocol
116 MAX_PACKET_SIZE_EP0
, // bMaxPacketSize0
117 LSB(VENDOR_ID
), MSB(VENDOR_ID
), // idVendor
118 LSB(PRODUCT_ID
), MSB(PRODUCT_ID
),// idProduct
119 0x00, 0x01, // bcdDevice
123 1 // bNumConfigurations
125 return deviceDescriptor
;
128 uint8_t * USBCDC::stringIinterfaceDesc() {
129 static uint8_t stringIinterfaceDescriptor
[] = {
134 return stringIinterfaceDescriptor
;
137 uint8_t * USBCDC::stringIproductDesc() {
138 static uint8_t stringIproductDescriptor
[] = {
141 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
143 return stringIproductDescriptor
;
147 #define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)
149 uint8_t * USBCDC::configurationDesc() {
150 static uint8_t configDescriptor
[] = {
152 2, // bDescriptorType;
153 LSB(CONFIG1_DESC_SIZE
), // wTotalLength
154 MSB(CONFIG1_DESC_SIZE
),
156 1, // bConfigurationValue
158 0x80, // bmAttributes
161 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
163 4, // bDescriptorType
164 0, // bInterfaceNumber
165 0, // bAlternateSetting
167 0x02, // bInterfaceClass
168 0x02, // bInterfaceSubClass
169 0x01, // bInterfaceProtocol
172 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
173 5, // bFunctionLength
174 0x24, // bDescriptorType
175 0x00, // bDescriptorSubtype
176 0x10, 0x01, // bcdCDC
178 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
179 5, // bFunctionLength
180 0x24, // bDescriptorType
181 0x01, // bDescriptorSubtype
182 0x03, // bmCapabilities
185 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
186 4, // bFunctionLength
187 0x24, // bDescriptorType
188 0x02, // bDescriptorSubtype
189 0x06, // bmCapabilities
191 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
192 5, // bFunctionLength
193 0x24, // bDescriptorType
194 0x06, // bDescriptorSubtype
195 0, // bMasterInterface
196 1, // bSlaveInterface0
198 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
199 ENDPOINT_DESCRIPTOR_LENGTH
, // bLength
200 ENDPOINT_DESCRIPTOR
, // bDescriptorType
201 PHY_TO_DESC(EPINT_IN
), // bEndpointAddress
202 E_INTERRUPT
, // bmAttributes (0x03=intr)
203 LSB(MAX_PACKET_SIZE_EPINT
), // wMaxPacketSize (LSB)
204 MSB(MAX_PACKET_SIZE_EPINT
), // wMaxPacketSize (MSB)
210 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
212 4, // bDescriptorType
213 1, // bInterfaceNumber
214 0, // bAlternateSetting
216 0x0A, // bInterfaceClass
217 0x00, // bInterfaceSubClass
218 0x00, // bInterfaceProtocol
221 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
223 5, // bDescriptorType
224 PHY_TO_DESC(EPBULK_IN
), // bEndpointAddress
225 0x02, // bmAttributes (0x02=bulk)
226 LSB(MAX_PACKET_SIZE_EPBULK
), // wMaxPacketSize (LSB)
227 MSB(MAX_PACKET_SIZE_EPBULK
), // wMaxPacketSize (MSB)
230 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
232 5, // bDescriptorType
233 PHY_TO_DESC(EPBULK_OUT
),// bEndpointAddress
234 0x02, // bmAttributes (0x02=bulk)
235 LSB(MAX_PACKET_SIZE_EPBULK
), // wMaxPacketSize (LSB)
236 MSB(MAX_PACKET_SIZE_EPBULK
), // wMaxPacketSize (MSB)
239 return configDescriptor
;