Skip to content
Snippets Groups Projects
Unverified Commit f140c86f authored by Thibaut VIARD's avatar Thibaut VIARD
Browse files

[bl] Fix usb power, move more functions to USB driver

parent d2a2c017
Branches
Tags
No related merge requests found
......@@ -312,3 +312,39 @@ void USB_SendZlp(Usb *pUsb)
while (!( pUsb->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.TRCPT & (1<<1) ));
}
/*----------------------------------------------------------------------------
* \brief Set USB device address obtained from host
*/
void USB_SetAddress(Usb *pUsb, uint16_t wValue)
{
pUsb->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | wValue;
}
/*----------------------------------------------------------------------------
* \brief Configure USB device
*/
void USB_Configure(Usb *pUsb)
{
/* Configure BULK OUT endpoint for CDC Data interface*/
pUsb->DEVICE.DeviceEndpoint[USB_EP_OUT].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(3);
/* Set maximum packet size as 64 bytes */
usb_endpoint_table[USB_EP_OUT].DeviceDescBank[0].PCKSIZE.bit.SIZE = 3;
pUsb->DEVICE.DeviceEndpoint[USB_EP_OUT].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY;
/* Configure the data buffer */
usb_endpoint_table[USB_EP_OUT].DeviceDescBank[0].ADDR.reg = (uint32_t)&udd_ep_out_cache_buffer[1];
/* Configure BULK IN endpoint for CDC Data interface */
pUsb->DEVICE.DeviceEndpoint[USB_EP_IN].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE1(3);
/* Set maximum packet size as 64 bytes */
usb_endpoint_table[USB_EP_IN].DeviceDescBank[1].PCKSIZE.bit.SIZE = 3;
pUsb->DEVICE.DeviceEndpoint[USB_EP_IN].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
/* Configure the data buffer */
usb_endpoint_table[USB_EP_IN].DeviceDescBank[1].ADDR.reg = (uint32_t)&udd_ep_in_cache_buffer[1];
/* Configure INTERRUPT IN endpoint for CDC COMM interface*/
pUsb->DEVICE.DeviceEndpoint[USB_EP_COMM].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE1(4);
/* Set maximum packet size as 64 bytes */
usb_endpoint_table[USB_EP_COMM].DeviceDescBank[1].PCKSIZE.bit.SIZE = 0;
pUsb->DEVICE.DeviceEndpoint[USB_EP_COMM].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
}
......@@ -25,7 +25,6 @@ extern UsbDeviceDescriptor usb_endpoint_table[MAX_EP];
extern uint8_t udd_ep_out_cache_buffer[2][64]; //1 for CTRL, 1 for BULK
extern uint8_t udd_ep_in_cache_buffer[2][64]; //1 for CTRL, 1 for BULK
P_USB_CDC USB_Open(P_USB_CDC pCdc, Usb *pUsb);
void USB_Init(void);
......@@ -39,5 +38,7 @@ uint8_t USB_IsConfigured(P_USB_CDC pCdc);
void USB_SendStall(Usb *pUsb, bool direction_in);
void USB_SendZlp(Usb *pUsb);
void USB_SetAddress(Usb *pUsb, uint16_t wValue);
void USB_Configure(Usb *pUsb);
#endif // _BOARD_DRIVER_USB_H_
......@@ -22,7 +22,10 @@
#include "board_driver_usb.h"
#include "sam_ba_cdc.h"
__attribute__((__aligned__(4)))
/* This data array will be copied into SRAM as its length is inferior to 64 bytes,
* and so can stay in flash.
*/
static __attribute__((__aligned__(4)))
const char devDescriptor[] =
{
/* Device descriptor */
......@@ -46,7 +49,10 @@ const char devDescriptor[] =
0x01 // bNumConfigs
};
__attribute__((__aligned__(4)))
/* This data array will be consumed directly by USB_Write() and must be in SRAM.
* We cannot send data from product internal flash.
*/
static __attribute__((__aligned__(4)))
char cfgDescriptor[] =
{
/* ============== CONFIGURATION 1 =========== */
......@@ -58,7 +64,7 @@ char cfgDescriptor[] =
0x02, // CbNumInterfaces
0x01, // CbConfigurationValue
0x00, // CiConfiguration
0xC0, // CbmAttributes Bus powered without remote wakeup: 0xc0, Self powered with remote wakeup: 0xa0
0x80, // CbmAttributes Bus powered without remote wakeup: 0x80, Self powered without remote wakeup: 0xc0
0x32, // CMaxPower, report using 100mA, enough for a bootloader
/* Communication Class Interface Descriptor Requirement */
......@@ -171,48 +177,42 @@ void sam_ba_usb_CDC_Enumerate(P_USB_CDC pCdc)
switch ((bRequest << 8) | bmRequestType)
{
case STD_GET_DESCRIPTOR:
if (wValue == 0x100)
if (wValue == (STD_GET_DESCRIPTOR_DEVICE<<8))
{
/* Return Device Descriptor */
USB_Write(pCdc->pUsb, devDescriptor, SAM_BA_MIN(sizeof(devDescriptor), wLength), USB_EP_CTRL);
else if (wValue == 0x200)
/* Return Configuration Descriptor */
USB_Write(pCdc->pUsb, cfgDescriptor, SAM_BA_MIN(sizeof(cfgDescriptor), wLength), USB_EP_CTRL);
}
else
/* Stall the request */
USB_SendStall(pUsb, true);
{
if (wValue == (STD_GET_DESCRIPTOR_CONFIGURATION<<8))
{
/* Return Configuration Descriptor */
USB_Write(pCdc->pUsb, cfgDescriptor, SAM_BA_MIN(sizeof(cfgDescriptor), wLength), USB_EP_CTRL);
}
else
{
/* Stall the request */
USB_SendStall(pUsb, true);
}
}
break;
case STD_SET_ADDRESS:
/* Send ZLP */
USB_SendZlp(pUsb);
/* Set device address to the newly received address from host */
pUsb->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | wValue;
USB_SetAddress(pCdc->pUsb, wValue);
break;
case STD_SET_CONFIGURATION:
/* Store configuration */
pCdc->currentConfiguration = (uint8_t)wValue;
/* Send ZLP */
USB_SendZlp(pUsb);
/* Configure BULK OUT endpoint for CDC Data interface*/
pUsb->DEVICE.DeviceEndpoint[USB_EP_OUT].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(3);
/* Set maximum packet size as 64 bytes */
usb_endpoint_table[USB_EP_OUT].DeviceDescBank[0].PCKSIZE.bit.SIZE = 3;
pUsb->DEVICE.DeviceEndpoint[USB_EP_OUT].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY;
/* Configure the data buffer */
usb_endpoint_table[USB_EP_OUT].DeviceDescBank[0].ADDR.reg = (uint32_t)&udd_ep_out_cache_buffer[1];
/* Configure BULK IN endpoint for CDC Data interface */
pUsb->DEVICE.DeviceEndpoint[USB_EP_IN].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE1(3);
/* Set maximum packet size as 64 bytes */
usb_endpoint_table[USB_EP_IN].DeviceDescBank[1].PCKSIZE.bit.SIZE = 3;
pUsb->DEVICE.DeviceEndpoint[USB_EP_IN].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
/* Configure the data buffer */
usb_endpoint_table[USB_EP_IN].DeviceDescBank[1].ADDR.reg = (uint32_t)&udd_ep_in_cache_buffer[1];
/* Configure INTERRUPT IN endpoint for CDC COMM interface*/
pUsb->DEVICE.DeviceEndpoint[USB_EP_COMM].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE1(4);
/* Set maximum packet size as 64 bytes */
usb_endpoint_table[USB_EP_COMM].DeviceDescBank[1].PCKSIZE.bit.SIZE = 0;
pUsb->DEVICE.DeviceEndpoint[USB_EP_COMM].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
/* Configure the 3 needed endpoints */
USB_Configure(pUsb);
break;
case STD_GET_CONFIGURATION:
......@@ -282,6 +282,7 @@ void sam_ba_usb_CDC_Enumerate(P_USB_CDC pCdc)
//pUsb->DEVICE.DeviceEndpoint[wIndex].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_STALLRQ0;
pUsb->DEVICE.DeviceEndpoint[wIndex].EPSTATUSSET.bit.STALLRQ = (1<<0);
}
/* Send ZLP */
USB_SendZlp(pUsb);
}
......
......@@ -22,35 +22,48 @@
#include <sam.h>
#include <stdbool.h>
#define USB_EP_CTRL (0)
#define USB_EP_OUT (2)
#define USB_EP_OUT_SIZE 0x40
#define USB_EP_IN (1)
#define USB_EP_IN_SIZE 0x40
#define USB_EP_COMM (3)
#define MAX_EP (4)
#define USB_EP_CTRL (0u)
#define USB_EP_OUT (2u)
#define USB_EP_OUT_SIZE (0x40u)
#define USB_EP_IN (1u)
#define USB_EP_IN_SIZE (0x40u)
#define USB_EP_COMM (3u)
#define MAX_EP (4u)
/* USB standard request code */
#define STD_GET_STATUS_ZERO (0x0080)
#define STD_GET_STATUS_INTERFACE (0x0081)
#define STD_GET_STATUS_ENDPOINT (0x0082)
#define STD_CLEAR_FEATURE_ZERO (0x0100)
#define STD_CLEAR_FEATURE_INTERFACE (0x0101)
#define STD_CLEAR_FEATURE_ENDPOINT (0x0102)
#define STD_SET_FEATURE_ZERO (0x0300)
#define STD_SET_FEATURE_INTERFACE (0x0301)
#define STD_SET_FEATURE_ENDPOINT (0x0302)
#define STD_SET_ADDRESS (0x0500)
#define STD_GET_DESCRIPTOR (0x0680)
#define STD_SET_DESCRIPTOR (0x0700)
#define STD_GET_CONFIGURATION (0x0880)
#define STD_SET_CONFIGURATION (0x0900)
#define STD_GET_INTERFACE (0x0A81)
#define STD_SET_INTERFACE (0x0B01)
#define STD_SYNCH_FRAME (0x0C82)
#define STD_GET_STATUS_ZERO (0x0080u)
#define STD_GET_STATUS_INTERFACE (0x0081u)
#define STD_GET_STATUS_ENDPOINT (0x0082u)
#define STD_CLEAR_FEATURE_ZERO (0x0100u)
#define STD_CLEAR_FEATURE_INTERFACE (0x0101u)
#define STD_CLEAR_FEATURE_ENDPOINT (0x0102u)
#define STD_SET_FEATURE_ZERO (0x0300u)
#define STD_SET_FEATURE_INTERFACE (0x0301u)
#define STD_SET_FEATURE_ENDPOINT (0x0302u)
#define STD_SET_ADDRESS (0x0500u)
#define STD_GET_DESCRIPTOR (0x0680u)
#define STD_SET_DESCRIPTOR (0x0700u)
#define STD_GET_CONFIGURATION (0x0880u)
#define STD_SET_CONFIGURATION (0x0900u)
#define STD_GET_INTERFACE (0x0A81u)
#define STD_SET_INTERFACE (0x0B01u)
#define STD_SYNCH_FRAME (0x0C82u)
#define STD_GET_DESCRIPTOR_DEVICE (1u)
#define STD_GET_DESCRIPTOR_CONFIGURATION (2u)
#define STD_GET_DESCRIPTOR_STRING (3u)
#define STD_GET_DESCRIPTOR_INTERFACE (4u)
#define STD_GET_DESCRIPTOR_ENDPOINT (5u)
#define STD_GET_DESCRIPTOR_DEVICE_QUALIFIER (6u)
#define STD_GET_DESCRIPTOR_OTHER_SPEED_CONFIGURATION (7u)
#define STD_GET_DESCRIPTOR_INTERFACE_POWER1 (8u)
#define FEATURE_ENDPOINT_HALT (0u)
#define FEATURE_DEVICE_REMOTE_WAKEUP (1u)
#define FEATURE_TEST_MODE (2u)
#define SAM_BA_MIN(a, b) (((a) < (b)) ? (a) : (b))
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment