summaryrefslogtreecommitdiff
path: root/MakefileBasedBuild/Atmel/sam3x/sam3x-ek/libraries/usb_host/example_enum/main.c
blob: 2b0cf28e4181e74eb043813d8a9fac33bbc35ccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* This source file is part of the AVR Software Framework 2.0.0 release */

/*This file is prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
 *
 * \brief Main file of the USB enumeration example.
 *
 * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32
 * - Supported devices:  All AVR32 devices with a USB module can be used.
 * - AppNote:
 *
 * \author               Atmel Corporation: http://www.atmel.com \n
 *                       Support and FAQ: http://support.atmel.no/
 *
 ******************************************************************************/

/*! \mainpage AVR32 USB Software Framework for Dual-Role Devices
 *
 * \section intro License
 * Use of this program is subject to Atmel's End User License Agreement.
 *
 * Please read the \ref license at the bottom of this page.
 *
 * \section install Description
 * This embedded application source code illustrates how to implement a USB interfaced application
 * on the AVR32 microcontroller.
 *
 * As the AVR32 implements a device/host USB controller, the embedded application can operate
 * in one of the following USB operating modes:
 *   - USB device;
 *   - USB reduced-host controller;
 *   - USB dual-role device (depending on the ID pin).
 *
 * To optimize embedded code/RAM size and reduce the number of source modules, the application can be
 * configured to use one and only one of these operating modes.
 *
 * \section sample About the Sample Application
 * By default the sample code is delivered with a simple preconfigured dual-role USB application.
 * It means that the code generated allows to operate as a device or a host depending on the USB ID pin:
 *   - attached to a mini-B plug (ID pin unconnected) the application will be used in the device operating mode;
 *   - attached to a mini-A plug (ID pin tied to ground) the application will operate in reduced-host mode.
 *
 * \image html appli.jpg "Application Overview"
 *
 * Thus two instances of this application can be connected together with a mini-A - mini-B cable.
 *   - The host operating mode of the application (that we call A-device) can manage the connection
 * and the enumeration of a device application connected in device operating mode (that we call B-device).
 * Once the device has been enumerated, the host high-level application can operate USB applicative exchanges
 * with the B-device. Here the sample host application writes 64 bytes through a USB OUT pipe and reads back
 * this data with an IN pipe.
 *   - The device operating mode of the application (B-device) answers to the enumeration requests (setup requests)
 * of the host controller. Once it has been properly enumerated the high-level device application receives 64 bytes
 * through its OUT enpoint and when the host controller request for an IN exchange on its IN endpoints,
 * sends it back (loopback application).
 *
 * \note
 *    - When an EVK1101/UC3C-EK/EVK1104/EVK1105 is the host device, LED2 and LED3 will quickly blink. LED0 & LED1 will slowly blink with
 *    the following pattern: LED0 on/LED1 off, then LED0 off/LED1 on, then LED0 on/LED1 on.
 *    - When an EVK1100 is the host device, LED4(red light of the led tagged as LED5 on the PCB) will quickly blink.
 *    LED0, LED1, LED2 and LED3 will slowly blink in a complex pattern.
 *    - The host device may display the following messages on USART1(EVK1100/EVK1101)/USART0-CDC(EVK1105)/USART1-CDC(EVK1104)/USART2-CDC(UC3C-EK) depending
 *    on the current USB events: "Device connected", "Unsupported device",
 *    "Device enumerated", "USB suspended", "USB resumed". While the slave device
 *    never displays any message on USART.
 *
 * \section arch Architecture
 * As illustrated in the figure below, the application entry point is located in the enum_example.c file.
 * The main function first performs the initialization of services and tasks and then runs them in an infinite loop.
 *
 * The sample dual-role application is based on three different tasks:
 *   - The USB task (usb_task.c associated source file) is the task performing the USB low-level
 * enumeration process in device or host mode. Once this task has detected that the USB connection is fully
 * operational, it updates various status flags that can be checked within the high-level applicative tasks.
 *   - The device template task (device_template_task.c associated source file) performs the high-level device
 * application operation. Once the device is fully enumerated (device SET_CONFIGURATION request received), the task
 * checks for received data on its OUT endpoint and transmits this data on its IN endpoint.
 *   - The host template task (host_template_task.c associated file) performs the high-level host application operation.
 * If the B-device is correctly connected and enumerated, the task sends and receives data over the USB bus.
 *
 * \image html arch_full.jpg "Architecture Overview"
 *
 * \section config Configuration
 * The sample application is configured to implement both host and device functionalities.
 * Of course it can also be configured to be used only in device or reduced-host mode (see the conf_usb.h file).
 * Depending on the USB operating mode selected, the USB task will call either the USB host task (usb_host_task.c),
 * or the USB device task (usb_device_task.c) to manage USB specification chapter 9 requests.
 *
 * \note The B-device descriptors used for this sample application are not directly usable for enumeration
 *       with a standard PC host system. Please see \ref contactinfo for real device application examples
 *       (mass storage, HID mouse, HID keyboard, CDC, etc.).
 *
 * \section contactinfo Contact Information
 * For further information, visit
 * <A href="http://www.atmel.com/products/AVR32/">Atmel AVR32</A>.\n
 * Support and FAQ: http://support.atmel.no/
 *
 * \section license Copyright Notice
 * Copyright (c) 2009 Atmel Corporation. All rights reserved.
 *
 * 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
 * AVR 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
 *
 */

//_____  I N C L U D E S ___________________________________________________
#include "conf_usb.h"

#ifdef FREERTOS_USED
#include <stdio.h>
#endif
#include "compiler.h"
#include "board.h"
//#include "print_funcs.h"
//#include "intc.h"
//#include "power_clocks_lib.h"
#ifdef FREERTOS_USED
#include "libfreertos.h"
#include "FreeRTOSConfig.h"
#include "FreeRTOSTask.h"
#endif

#include "usb_task.h"

#if USB_DEVICE_FEATURE == ENABLED
#include "device_template_task.h"
#endif

#if USB_HOST_FEATURE == ENABLED
#include "host_template_task.h"
#endif

#ifdef FREERTOS_USED
extern void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName );
extern void vApplicationIdleHook( void );
extern void vApplicationTickHook( void );

extern void xPortSysTickHandler(void );
extern void xPortPendSVHandler(void);
extern void vPortSVCHandler(void);
#endif

//_____ D E F I N I T I O N S ______________________________________________
/** PIOs for USB OTGHS */
static const Pin pUOTGHSPins[] = {PIN_UOTGHS_VBOF, PIN_UOTGHS_ID, PIN_UOTGHS_FAULT};
static const Pin pUOTGHS_Fault = PIN_UOTGHS_FAULT;


#ifdef FREERTOS_USED
void SysTick_Handler(void)
{
    xPortSysTickHandler();
    TimeTick_Increment() ;
}

void PendSV_Handler(void)
{
    xPortPendSVHandler();
}
void SVC_Handler(void)
{
    vPortSVCHandler();
}


/**
 * Called if stack overflow during execution
 */
extern void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
{
    printf( "stack overflow %x %s\r\n", pxTask, pcTaskName ) ;
    /* If the parameters have been corrupted then inspect pxCurrentTCB to
       identify which task has overflowed its stack. */
    for( ;; ) ;
}

extern void vApplicationIdleHook( void )
{
}

/**
 * This function is called by FreeRTOS each tick
 */
extern void vApplicationTickHook( void )
{
}
#endif




/**
 *  \brief UOTGHS Library Example Application entry point.
 *
 *  \return 42 Fatal error.
 */
extern int main( void )
{
    /* Disable watchdog */
    WDT_Disable( WDT ) ;

    /* UTMI parallel mode, High/Full/Low Speed */
    /* UOTGCK not used in this configuration (High Speed) */
    PMC->PMC_SCDR = PMC_SCDR_UOTGCK;
    /* USB clock register: USB Clock Input is UTMI PLL */
    PMC->PMC_USB = PMC_USB_USBS;
    /* USBS: USB Input Clock Selection: USB Clock Input is PLLA */
    /* Enable peripheral clock for UOTGHS */
    PMC_EnablePeripheral(UOTGHS_IRQn);
    UOTGHS->UOTGHS_CTRL = 0x0;
    /* Enable PLL 480 MHz */
    PMC->CKGR_UCKR = CKGR_UCKR_UPLLEN | CKGR_UCKR_UPLLCOUNT(0xF);
    /* Wait that PLL is considered locked by the PMC */
    while( !(PMC->PMC_SR & PMC_SR_LOCKU) );
    /* Enable peripheral clock for UOTGHS */
    PMC_EnablePeripheral(UOTGHS_IRQn);

    /* Output example information */
    printf( "-- UOTGHS Library Example %s --\n\r", SOFTPACK_VERSION ) ;
    printf( "-- %s\n\r", BOARD_NAME ) ;
    printf( "-- Compiled: %s %s --\n\r", __DATE__, __TIME__ ) ;

    /* UOTGHS pins */
    PIO_PinConfigure( pUOTGHSPins, PIO_LISTSIZE( pUOTGHSPins ) );
    if ( PIO_PinGet(&pUOTGHS_Fault) == 0 )
    {
        TRACE_OTG("UOTGHS_Fault = 0 (active low  ERROR FLAG !\r\n");
        TRACE_OTG("Undervoltage, Soft Start, Overcurrent, or Overtemperature\r\n");
        while(1);
    }

    /* Enable interrupt */
    NVIC_EnableIRQ(UOTGHS_IRQn);

    /* Initialize USB task */
    usb_task_init();

#if USB_DEVICE_FEATURE == ENABLED
  device_template_task_init();
#endif

#if USB_HOST_FEATURE == ENABLED
  host_template_task_init();
#endif

#ifdef FREERTOS_USED
    /* Start OS scheduler */
  vTaskStartScheduler();
  TRACE_OTG("FreeRTOS returned\n\r");
  return 42;
#else
    /* No OS here. Need to call each task in round-robin mode. */
  while (TRUE)
  {
    usb_task();

  #if USB_DEVICE_FEATURE == ENABLED
    device_template_task();
  #endif

  #if USB_HOST_FEATURE == ENABLED
    host_template_task();
  #endif
  }
#endif  /* FREERTOS_USED */
}