2222#include "board_driver_usb.h"
2323#include "sam_ba_cdc.h"
2424
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 )))
2629const char devDescriptor [] =
2730{
2831 /* Device descriptor */
@@ -46,7 +49,10 @@ const char devDescriptor[] =
4649 0x01 // bNumConfigs
4750};
4851
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 )))
5056char cfgDescriptor [] =
5157{
5258 /* ============== CONFIGURATION 1 =========== */
@@ -58,7 +64,7 @@ char cfgDescriptor[] =
5864 0x02 , // CbNumInterfaces
5965 0x01 , // CbConfigurationValue
6066 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
6268 0x32 , // CMaxPower, report using 100mA, enough for a bootloader
6369
6470 /* Communication Class Interface Descriptor Requirement */
@@ -171,48 +177,42 @@ void sam_ba_usb_CDC_Enumerate(P_USB_CDC pCdc)
171177 switch ((bRequest << 8 ) | bmRequestType )
172178 {
173179 case STD_GET_DESCRIPTOR :
174- if (wValue == 0x100 )
180+ if (wValue == (STD_GET_DESCRIPTOR_DEVICE <<8 ))
181+ {
175182 /* Return Device Descriptor */
176183 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+ }
180185 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+ }
183198 break ;
184199
185200 case STD_SET_ADDRESS :
186201 /* Send ZLP */
187202 USB_SendZlp (pUsb );
188203 /* 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 ) ;
190205 break ;
191206
192207 case STD_SET_CONFIGURATION :
193208 /* Store configuration */
194209 pCdc -> currentConfiguration = (uint8_t )wValue ;
210+
195211 /* Send ZLP */
196212 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 );
216216 break ;
217217
218218 case STD_GET_CONFIGURATION :
@@ -282,6 +282,7 @@ void sam_ba_usb_CDC_Enumerate(P_USB_CDC pCdc)
282282 //pUsb->DEVICE.DeviceEndpoint[wIndex].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0;
283283 pUsb -> DEVICE .DeviceEndpoint [wIndex ].EPSTATUSSET .bit .STALLRQ = (1 <<0 );
284284 }
285+
285286 /* Send ZLP */
286287 USB_SendZlp (pUsb );
287288 }
0 commit comments