22
22
#include "board_driver_usb.h"
23
23
#include "sam_ba_cdc.h"
24
24
25
- __attribute__((__aligned__ (4 )))
25
+ /* This data array will be copied into SRAM as its length is inferior to 64 bytes,
26
+ * and so can stay in flash.
27
+ */
28
+ static __attribute__((__aligned__ (4 )))
26
29
const char devDescriptor [] =
27
30
{
28
31
/* Device descriptor */
@@ -46,7 +49,10 @@ const char devDescriptor[] =
46
49
0x01 // bNumConfigs
47
50
};
48
51
49
- __attribute__((__aligned__ (4 )))
52
+ /* This data array will be consumed directly by USB_Write() and must be in SRAM.
53
+ * We cannot send data from product internal flash.
54
+ */
55
+ static __attribute__((__aligned__ (4 )))
50
56
char cfgDescriptor [] =
51
57
{
52
58
/* ============== CONFIGURATION 1 =========== */
@@ -58,7 +64,7 @@ char cfgDescriptor[] =
58
64
0x02 , // CbNumInterfaces
59
65
0x01 , // CbConfigurationValue
60
66
0x00 , // CiConfiguration
61
- 0xC0 , // CbmAttributes Bus powered without remote wakeup: 0xc0 , Self powered with remote wakeup: 0xa0
67
+ 0x80 , // CbmAttributes Bus powered without remote wakeup: 0x80 , Self powered without remote wakeup: 0xc0
62
68
0x32 , // CMaxPower, report using 100mA, enough for a bootloader
63
69
64
70
/* Communication Class Interface Descriptor Requirement */
@@ -171,48 +177,42 @@ void sam_ba_usb_CDC_Enumerate(P_USB_CDC pCdc)
171
177
switch ((bRequest << 8 ) | bmRequestType )
172
178
{
173
179
case STD_GET_DESCRIPTOR :
174
- if (wValue == 0x100 )
180
+ if (wValue == (STD_GET_DESCRIPTOR_DEVICE <<8 ))
181
+ {
175
182
/* Return Device Descriptor */
176
183
USB_Write (pCdc -> pUsb , devDescriptor , SAM_BA_MIN (sizeof (devDescriptor ), wLength ), USB_EP_CTRL );
177
- else if (wValue == 0x200 )
178
- /* Return Configuration Descriptor */
179
- USB_Write (pCdc -> pUsb , cfgDescriptor , SAM_BA_MIN (sizeof (cfgDescriptor ), wLength ), USB_EP_CTRL );
184
+ }
180
185
else
181
- /* Stall the request */
182
- USB_SendStall (pUsb , true);
186
+ {
187
+ if (wValue == (STD_GET_DESCRIPTOR_CONFIGURATION <<8 ))
188
+ {
189
+ /* Return Configuration Descriptor */
190
+ USB_Write (pCdc -> pUsb , cfgDescriptor , SAM_BA_MIN (sizeof (cfgDescriptor ), wLength ), USB_EP_CTRL );
191
+ }
192
+ else
193
+ {
194
+ /* Stall the request */
195
+ USB_SendStall (pUsb , true);
196
+ }
197
+ }
183
198
break ;
184
199
185
200
case STD_SET_ADDRESS :
186
201
/* Send ZLP */
187
202
USB_SendZlp (pUsb );
188
203
/* Set device address to the newly received address from host */
189
- pUsb -> DEVICE . DADD . reg = USB_DEVICE_DADD_ADDEN | wValue ;
204
+ USB_SetAddress ( pCdc -> pUsb , wValue ) ;
190
205
break ;
191
206
192
207
case STD_SET_CONFIGURATION :
193
208
/* Store configuration */
194
209
pCdc -> currentConfiguration = (uint8_t )wValue ;
210
+
195
211
/* Send ZLP */
196
212
USB_SendZlp (pUsb );
197
- /* Configure BULK OUT endpoint for CDC Data interface*/
198
- pUsb -> DEVICE .DeviceEndpoint [USB_EP_OUT ].EPCFG .reg = USB_DEVICE_EPCFG_EPTYPE0 (3 );
199
- /* Set maximum packet size as 64 bytes */
200
- usb_endpoint_table [USB_EP_OUT ].DeviceDescBank [0 ].PCKSIZE .bit .SIZE = 3 ;
201
- pUsb -> DEVICE .DeviceEndpoint [USB_EP_OUT ].EPSTATUSSET .reg = USB_DEVICE_EPSTATUSSET_BK0RDY ;
202
- /* Configure the data buffer */
203
- usb_endpoint_table [USB_EP_OUT ].DeviceDescBank [0 ].ADDR .reg = (uint32_t )& udd_ep_out_cache_buffer [1 ];
204
- /* Configure BULK IN endpoint for CDC Data interface */
205
- pUsb -> DEVICE .DeviceEndpoint [USB_EP_IN ].EPCFG .reg = USB_DEVICE_EPCFG_EPTYPE1 (3 );
206
- /* Set maximum packet size as 64 bytes */
207
- usb_endpoint_table [USB_EP_IN ].DeviceDescBank [1 ].PCKSIZE .bit .SIZE = 3 ;
208
- pUsb -> DEVICE .DeviceEndpoint [USB_EP_IN ].EPSTATUSCLR .reg = USB_DEVICE_EPSTATUSCLR_BK1RDY ;
209
- /* Configure the data buffer */
210
- usb_endpoint_table [USB_EP_IN ].DeviceDescBank [1 ].ADDR .reg = (uint32_t )& udd_ep_in_cache_buffer [1 ];
211
- /* Configure INTERRUPT IN endpoint for CDC COMM interface*/
212
- pUsb -> DEVICE .DeviceEndpoint [USB_EP_COMM ].EPCFG .reg = USB_DEVICE_EPCFG_EPTYPE1 (4 );
213
- /* Set maximum packet size as 64 bytes */
214
- usb_endpoint_table [USB_EP_COMM ].DeviceDescBank [1 ].PCKSIZE .bit .SIZE = 0 ;
215
- pUsb -> DEVICE .DeviceEndpoint [USB_EP_COMM ].EPSTATUSCLR .reg = USB_DEVICE_EPSTATUSCLR_BK1RDY ;
213
+
214
+ /* Configure the 3 needed endpoints */
215
+ USB_Configure (pUsb );
216
216
break ;
217
217
218
218
case STD_GET_CONFIGURATION :
@@ -282,6 +282,7 @@ void sam_ba_usb_CDC_Enumerate(P_USB_CDC pCdc)
282
282
//pUsb->DEVICE.DeviceEndpoint[wIndex].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0;
283
283
pUsb -> DEVICE .DeviceEndpoint [wIndex ].EPSTATUSSET .bit .STALLRQ = (1 <<0 );
284
284
}
285
+
285
286
/* Send ZLP */
286
287
USB_SendZlp (pUsb );
287
288
}
0 commit comments