Skip to content

Commit f140c86

Browse files
committed
[bl] Fix usb power, move more functions to USB driver
1 parent d2a2c01 commit f140c86

File tree

5 files changed

+108
-57
lines changed

5 files changed

+108
-57
lines changed

bootloaders/zero/board_driver_usb.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,39 @@ void USB_SendZlp(Usb *pUsb)
312312
while (!( pUsb->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.TRCPT & (1<<1) ));
313313
}
314314

315+
/*----------------------------------------------------------------------------
316+
* \brief Set USB device address obtained from host
317+
*/
318+
void USB_SetAddress(Usb *pUsb, uint16_t wValue)
319+
{
320+
pUsb->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | wValue;
321+
}
322+
323+
/*----------------------------------------------------------------------------
324+
* \brief Configure USB device
325+
*/
326+
void USB_Configure(Usb *pUsb)
327+
{
328+
/* Configure BULK OUT endpoint for CDC Data interface*/
329+
pUsb->DEVICE.DeviceEndpoint[USB_EP_OUT].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(3);
330+
/* Set maximum packet size as 64 bytes */
331+
usb_endpoint_table[USB_EP_OUT].DeviceDescBank[0].PCKSIZE.bit.SIZE = 3;
332+
pUsb->DEVICE.DeviceEndpoint[USB_EP_OUT].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY;
333+
/* Configure the data buffer */
334+
usb_endpoint_table[USB_EP_OUT].DeviceDescBank[0].ADDR.reg = (uint32_t)&udd_ep_out_cache_buffer[1];
335+
336+
/* Configure BULK IN endpoint for CDC Data interface */
337+
pUsb->DEVICE.DeviceEndpoint[USB_EP_IN].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE1(3);
338+
/* Set maximum packet size as 64 bytes */
339+
usb_endpoint_table[USB_EP_IN].DeviceDescBank[1].PCKSIZE.bit.SIZE = 3;
340+
pUsb->DEVICE.DeviceEndpoint[USB_EP_IN].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
341+
/* Configure the data buffer */
342+
usb_endpoint_table[USB_EP_IN].DeviceDescBank[1].ADDR.reg = (uint32_t)&udd_ep_in_cache_buffer[1];
343+
344+
/* Configure INTERRUPT IN endpoint for CDC COMM interface*/
345+
pUsb->DEVICE.DeviceEndpoint[USB_EP_COMM].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE1(4);
346+
/* Set maximum packet size as 64 bytes */
347+
usb_endpoint_table[USB_EP_COMM].DeviceDescBank[1].PCKSIZE.bit.SIZE = 0;
348+
pUsb->DEVICE.DeviceEndpoint[USB_EP_COMM].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
349+
}
350+

bootloaders/zero/board_driver_usb.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ extern UsbDeviceDescriptor usb_endpoint_table[MAX_EP];
2525
extern uint8_t udd_ep_out_cache_buffer[2][64]; //1 for CTRL, 1 for BULK
2626
extern uint8_t udd_ep_in_cache_buffer[2][64]; //1 for CTRL, 1 for BULK
2727

28-
2928
P_USB_CDC USB_Open(P_USB_CDC pCdc, Usb *pUsb);
3029

3130
void USB_Init(void);
@@ -39,5 +38,7 @@ uint8_t USB_IsConfigured(P_USB_CDC pCdc);
3938
void USB_SendStall(Usb *pUsb, bool direction_in);
4039
void USB_SendZlp(Usb *pUsb);
4140

41+
void USB_SetAddress(Usb *pUsb, uint16_t wValue);
42+
void USB_Configure(Usb *pUsb);
4243

4344
#endif // _BOARD_DRIVER_USB_H_

bootloaders/zero/sam_ba_usb.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
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)))
2629
const 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)))
5056
char 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
}

bootloaders/zero/sam_ba_usb.h

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,48 @@
2222
#include <sam.h>
2323
#include <stdbool.h>
2424

25-
#define USB_EP_CTRL (0)
26-
#define USB_EP_OUT (2)
27-
#define USB_EP_OUT_SIZE 0x40
28-
#define USB_EP_IN (1)
29-
#define USB_EP_IN_SIZE 0x40
30-
#define USB_EP_COMM (3)
31-
#define MAX_EP (4)
25+
#define USB_EP_CTRL (0u)
26+
#define USB_EP_OUT (2u)
27+
#define USB_EP_OUT_SIZE (0x40u)
28+
#define USB_EP_IN (1u)
29+
#define USB_EP_IN_SIZE (0x40u)
30+
#define USB_EP_COMM (3u)
31+
#define MAX_EP (4u)
3232

3333
/* USB standard request code */
34-
#define STD_GET_STATUS_ZERO (0x0080)
35-
#define STD_GET_STATUS_INTERFACE (0x0081)
36-
#define STD_GET_STATUS_ENDPOINT (0x0082)
37-
38-
#define STD_CLEAR_FEATURE_ZERO (0x0100)
39-
#define STD_CLEAR_FEATURE_INTERFACE (0x0101)
40-
#define STD_CLEAR_FEATURE_ENDPOINT (0x0102)
41-
42-
#define STD_SET_FEATURE_ZERO (0x0300)
43-
#define STD_SET_FEATURE_INTERFACE (0x0301)
44-
#define STD_SET_FEATURE_ENDPOINT (0x0302)
45-
46-
#define STD_SET_ADDRESS (0x0500)
47-
#define STD_GET_DESCRIPTOR (0x0680)
48-
#define STD_SET_DESCRIPTOR (0x0700)
49-
#define STD_GET_CONFIGURATION (0x0880)
50-
#define STD_SET_CONFIGURATION (0x0900)
51-
#define STD_GET_INTERFACE (0x0A81)
52-
#define STD_SET_INTERFACE (0x0B01)
53-
#define STD_SYNCH_FRAME (0x0C82)
34+
#define STD_GET_STATUS_ZERO (0x0080u)
35+
#define STD_GET_STATUS_INTERFACE (0x0081u)
36+
#define STD_GET_STATUS_ENDPOINT (0x0082u)
37+
38+
#define STD_CLEAR_FEATURE_ZERO (0x0100u)
39+
#define STD_CLEAR_FEATURE_INTERFACE (0x0101u)
40+
#define STD_CLEAR_FEATURE_ENDPOINT (0x0102u)
41+
42+
#define STD_SET_FEATURE_ZERO (0x0300u)
43+
#define STD_SET_FEATURE_INTERFACE (0x0301u)
44+
#define STD_SET_FEATURE_ENDPOINT (0x0302u)
45+
46+
#define STD_SET_ADDRESS (0x0500u)
47+
#define STD_GET_DESCRIPTOR (0x0680u)
48+
#define STD_SET_DESCRIPTOR (0x0700u)
49+
#define STD_GET_CONFIGURATION (0x0880u)
50+
#define STD_SET_CONFIGURATION (0x0900u)
51+
#define STD_GET_INTERFACE (0x0A81u)
52+
#define STD_SET_INTERFACE (0x0B01u)
53+
#define STD_SYNCH_FRAME (0x0C82u)
54+
55+
#define STD_GET_DESCRIPTOR_DEVICE (1u)
56+
#define STD_GET_DESCRIPTOR_CONFIGURATION (2u)
57+
#define STD_GET_DESCRIPTOR_STRING (3u)
58+
#define STD_GET_DESCRIPTOR_INTERFACE (4u)
59+
#define STD_GET_DESCRIPTOR_ENDPOINT (5u)
60+
#define STD_GET_DESCRIPTOR_DEVICE_QUALIFIER (6u)
61+
#define STD_GET_DESCRIPTOR_OTHER_SPEED_CONFIGURATION (7u)
62+
#define STD_GET_DESCRIPTOR_INTERFACE_POWER1 (8u)
63+
64+
#define FEATURE_ENDPOINT_HALT (0u)
65+
#define FEATURE_DEVICE_REMOTE_WAKEUP (1u)
66+
#define FEATURE_TEST_MODE (2u)
5467

5568
#define SAM_BA_MIN(a, b) (((a) < (b)) ? (a) : (b))
5669

bootloaders/zero/samd21_sam_ba.bin

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)