/**
* \file
*
* \brief USB Device Driver for UOTGHS. Compliant with common UDD driver.
*
* Copyright (c) 2012-2015 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#ifdef ARDUINO_ARCH_SAM
#include "compiler.h"
#include "uotghs_device_due.h"
#include "conf_usb.h"
#include "sysclk.h"
#include "udd.h"
#include "uotghs_otg.h"
#include <string.h>
#ifndef UDD_NO_SLEEP_MGR
# include "sleep.h"
# include "sleepmgr.h"
#endif
#if !(SAM3XA)
# error The current UOTGHS Device Driver supports only SAM3X and SAM3A.
#endif
#ifndef UDD_USB_INT_FUN
# define UDD_USB_INT_FUN UOTGHS_Handler
#endif
#ifndef UDD_USB_INT_LEVEL
# define UDD_USB_INT_LEVEL 5 // By default USB interrupt have low priority
#endif
#define UDD_EP_USED(ep) (USB_DEVICE_MAX_EP >= ep)
#if ( (UDD_EP_USED( 1) && Is_udd_endpoint_dma_supported( 1)) \
||(UDD_EP_USED( 2) && Is_udd_endpoint_dma_supported( 2)) \
||(UDD_EP_USED( 3) && Is_udd_endpoint_dma_supported( 3)) \
||(UDD_EP_USED( 4) && Is_udd_endpoint_dma_supported( 4)) \
||(UDD_EP_USED( 5) && Is_udd_endpoint_dma_supported( 5)) \
||(UDD_EP_USED( 6) && Is_udd_endpoint_dma_supported( 6)) \
||(UDD_EP_USED( 7) && Is_udd_endpoint_dma_supported( 7)) \
||(UDD_EP_USED( 8) && Is_udd_endpoint_dma_supported( 8)) \
||(UDD_EP_USED( 9) && Is_udd_endpoint_dma_supported( 9)) \
||(UDD_EP_USED(10) && Is_udd_endpoint_dma_supported(10)) \
||(UDD_EP_USED(11) && Is_udd_endpoint_dma_supported(11)) \
||(UDD_EP_USED(12) && Is_udd_endpoint_dma_supported(12)) \
||(UDD_EP_USED(13) && Is_udd_endpoint_dma_supported(13)) \
||(UDD_EP_USED(14) && Is_udd_endpoint_dma_supported(14)) \
||(UDD_EP_USED(15) && Is_udd_endpoint_dma_supported(15)) \
)
# define UDD_EP_DMA_SUPPORTED
#endif
#if ( (UDD_EP_USED( 1) && !Is_udd_endpoint_dma_supported( 1)) \
||(UDD_EP_USED( 2) && !Is_udd_endpoint_dma_supported( 2)) \
||(UDD_EP_USED( 3) && !Is_udd_endpoint_dma_supported( 3)) \
||(UDD_EP_USED( 4) && !Is_udd_endpoint_dma_supported( 4)) \
||(UDD_EP_USED( 5) && !Is_udd_endpoint_dma_supported( 5)) \
||(UDD_EP_USED( 6) && !Is_udd_endpoint_dma_supported( 6)) \
||(UDD_EP_USED( 7) && !Is_udd_endpoint_dma_supported( 7)) \
||(UDD_EP_USED( 8) && !Is_udd_endpoint_dma_supported( 8)) \
||(UDD_EP_USED( 9) && !Is_udd_endpoint_dma_supported( 9)) \
||(UDD_EP_USED(10) && !Is_udd_endpoint_dma_supported(10)) \
||(UDD_EP_USED(11) && !Is_udd_endpoint_dma_supported(11)) \
||(UDD_EP_USED(12) && !Is_udd_endpoint_dma_supported(12)) \
||(UDD_EP_USED(13) && !Is_udd_endpoint_dma_supported(13)) \
||(UDD_EP_USED(14) && !Is_udd_endpoint_dma_supported(14)) \
||(UDD_EP_USED(15) && !Is_udd_endpoint_dma_supported(15)) \
)
# define UDD_EP_FIFO_SUPPORTED
#endif
// for debug text
//#define dbg_print printf
#define dbg_print(...)
/**
* \ingroup udd_group
* \defgroup udd_udphs_group USB On-The-Go High-Speed Port for device mode (UOTGHS)
*
* \section UOTGHS_CONF UOTGHS Custom configuration
* The following UOTGHS driver configuration must be included in the conf_usb.h
* file of the application.
*
* UDD_USB_INT_LEVEL<br>
* Option to change the interrupt priority (0 to 15) by default 5 (recommended).
*
* UDD_USB_INT_FUN<br>
* Option to fit interrupt function to what defined in exception table.
*
* UDD_ISOCHRONOUS_NB_BANK(ep)<br>
* Feature to reduce or increase isochronous endpoints buffering (1 to 3).
* Default value 2.
*
* UDD_BULK_NB_BANK(ep)<br>
* Feature to reduce or increase bulk endpoints buffering (1 to 2).
* Default value 2.
*
* UDD_INTERRUPT_NB_BANK(ep)<br>
* Feature to reduce or increase interrupt endpoints buffering (1 to 2).
* Default value 1.
*
* \section Callbacks management
* The USB driver is fully managed by interrupt and does not request periodique
* task. Thereby, the USB events use callbacks to transfer the information.
* The callbacks are declared in static during compilation or in variable during
* code execution.
*
* Static declarations defined in conf_usb.h:
* - UDC_VBUS_EVENT(bool b_present)<br>
* To signal Vbus level change
* - UDC_SUSPEND_EVENT()<br>
* Called when USB bus enter in suspend mode
* - UDC_RESUME_EVENT()<br>
* Called when USB bus is wakeup
* - UDC_SOF_EVENT()<br>
* Called for each received SOF, Note: Each 1ms in HS/FS mode only.
*
* Dynamic callbacks, called "endpoint job" , are registered
* in udd_ep_job_t structure via the following functions:
* - udd_ep_run()<br>
* To call it when a transfer is finish
* - udd_ep_wait_stall_clear()<br>
* To call it when a endpoint halt is disabled
*
* \section Power mode management
* The Sleep modes authorized :
* - in USB IDLE state, the UOTGHS needs of USB clock and authorizes up to sleep mode WFI.
* - in USB SUSPEND state, the UOTGHS no needs USB clock and authorizes up to sleep mode WAIT.
* @{
*/
// Check USB Device configuration
#ifndef USB_DEVICE_EP_CTRL_SIZE
# error USB_DEVICE_EP_CTRL_SIZE not defined
#endif
#ifndef USB_DEVICE_MAX_EP
# error USB_DEVICE_MAX_EP not defined
#endif
// Note: USB_DEVICE_MAX_EP does not include control endpoint
#if USB_DEVICE_MAX_EP > (UDD_MAX_PEP_NB-1)
# error USB_DEVICE_MAX_EP is too high and not supported by this part
#endif
#define UDD_EP_ISO_NBANK_ERROR(ep) \
( (UDD_ISOCHRONOUS_NB_BANK(ep) < 1) \
|| (UDD_ISOCHRONOUS_NB_BANK(ep) > 3) )
#define UDD_EP_BULK_NBANK_ERROR(ep) \
( (UDD_BULK_NB_BANK(ep) < 1) || (UDD_BULK_NB_BANK(ep) > 2) )
#define UDD_EP_INT_NBANK_ERROR(ep) \
( (UDD_INTERRUPT_NB_BANK(ep) < 1) || (UDD_INTERRUPT_NB_BANK(ep) > 2) )
#define UDD_EP_ISO_NB_BANK_ERROR(ep) \
(UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
#define UDD_EP_BULK_NB_BANK_ERROR(ep) \
(UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
#define UDD_EP_INT_NB_BANK_ERROR(ep) \
(UDD_EP_USED(ep) && UDD_EP_ISO_NBANK_ERROR(ep))
#define UDD_EP_NB_BANK_ERROR(ep, type) \
(ATPASTE3(UDD_EP_, type, _NB_BANK_ERROR(ep)))
#define UDD_ISO_NB_BANK_ERROR \
( UDD_EP_NB_BANK_ERROR( 1, ISO) \
|| UDD_EP_NB_BANK_ERROR( 2, ISO) \
|| UDD_EP_NB_BANK_ERROR( 3, ISO) \
|| UDD_EP_NB_BANK_ERROR( 4, ISO) \
|| UDD_EP_NB_BANK_ERROR( 5, ISO) \
|| UDD_EP_NB_BANK_ERROR( 6, ISO) \
|| UDD_EP_NB_BANK_ERROR( 7, ISO) \
|| UDD_EP_NB_BANK_ERROR( 8, ISO) \
|| UDD_EP_NB_BANK_ERROR( 9, ISO) \
|