blob: 5b09ca0973069363ba55cc51e88f6ad1f2858d7e [file] [log] [blame]
Christina Quast6255ccc2015-02-10 20:38:51 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2009, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30/*----------------------------------------------------------------------------
31 * Headers
32 *----------------------------------------------------------------------------*/
33
34#include "board.h"
Christina Quast6255ccc2015-02-10 20:38:51 +010035
36/*----------------------------------------------------------------------------
37 * Internal variables
38 *----------------------------------------------------------------------------*/
39
40/** Standard USB device descriptor for the CDC serial driver */
41const USBDeviceDescriptor deviceDescriptor = {
42
43 sizeof(USBDeviceDescriptor),
44 USBGenericDescriptor_DEVICE,
45 USBDeviceDescriptor_USB2_00,
46 0xff,
47// CDCDeviceDescriptor_CLASS,
48 0xff,
49// CDCDeviceDescriptor_SUBCLASS,
50 0xff,
51// CDCDeviceDescriptor_PROTOCOL,
52 BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
53 ATMEL_VENDOR_ID,
54 SIMTRACE_PRODUCT_ID,
55 1, /* Release number */
56 0, /* No string descriptor for manufacturer */
57 1, /* Index of product string descriptor is #1 */
58 0, /* No string descriptor for serial number */
Christina Quast49ba6bc2015-02-20 14:35:36 +010059 2 /* Device has 2 possible configurations */
Christina Quast6255ccc2015-02-10 20:38:51 +010060};
61
62typedef struct _SIMTraceDriverConfigurationDescriptors {
63
64 /** Standard configuration descriptor. */
65 USBConfigurationDescriptor configuration;
66 /** Data interface descriptor. */
67 USBInterfaceDescriptor data;
68 /** Data OUT endpoint descriptor. */
69 USBEndpointDescriptor dataOut;
70 /** Data IN endpoint descriptor. */
71 USBEndpointDescriptor dataIn;
72
73} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptor;
74
Christina Quast49ba6bc2015-02-20 14:35:36 +010075const SIMTraceDriverConfigurationDescriptor configurationDescriptorsFS1 = {
Christina Quast6255ccc2015-02-10 20:38:51 +010076 /* Standard configuration descriptor */
77 {
78 sizeof(USBConfigurationDescriptor),
79 USBGenericDescriptor_CONFIGURATION,
80 sizeof(SIMTraceDriverConfigurationDescriptor),
81 2, /* There are two interfaces in this configuration */
Christina Quast49ba6bc2015-02-20 14:35:36 +010082 1, /* This is configuration #1 */
Christina Quast6255ccc2015-02-10 20:38:51 +010083 2, /* Second string descriptor for this configuration */
84 USBD_BMATTRIBUTES,
85 USBConfigurationDescriptor_POWER(100)
86 },
87 /* Communication class interface standard descriptor */
88 {
89 sizeof(USBInterfaceDescriptor),
90 USBGenericDescriptor_INTERFACE,
91 0, /* This is interface #0 */
92 0, /* This is alternate setting #0 for this interface */
Christina Quast49ba6bc2015-02-20 14:35:36 +010093 2, /* This interface uses 2 endpoints */
Christina Quast6255ccc2015-02-10 20:38:51 +010094 //CDCCommunicationInterfaceDescriptor_CLASS,
95 0xff,
96// CDCCommunicationInterfaceDescriptor_ABSTRACTCONTROLMODEL,
97 0,
98// CDCCommunicationInterfaceDescriptor_NOPROTOCOL,
99 0,
100 1 /* Second in string descriptor for this interface */
101 },
102 /* Bulk-OUT endpoint standard descriptor */
103#define DATAOUT 1
104 {
105 sizeof(USBEndpointDescriptor),
106 USBGenericDescriptor_ENDPOINT,
107 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
108 DATAOUT),
109 USBEndpointDescriptor_BULK,
110 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT),
111 USBEndpointDescriptor_MAXBULKSIZE_FS),
112 0 /* Must be 0 for full-speed bulk endpoints */
113 },
114 /* Bulk-IN endpoint descriptor */
115#define DATAIN 2
116 {
117 sizeof(USBEndpointDescriptor),
118 USBGenericDescriptor_ENDPOINT,
119 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
120 DATAIN),
121 USBEndpointDescriptor_BULK,
122 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN),
123 USBEndpointDescriptor_MAXBULKSIZE_FS),
124 0 /* Must be 0 for full-speed bulk endpoints */
125 }
126};
127
Christina Quast49ba6bc2015-02-20 14:35:36 +0100128const SIMTraceDriverConfigurationDescriptor configurationDescriptorsFS2 = {
129 /** Second configuration **/
130 {
131 sizeof(USBConfigurationDescriptor),
132 USBGenericDescriptor_CONFIGURATION,
133 sizeof(SIMTraceDriverConfigurationDescriptor),
134 2, /* There are two interfaces in this configuration */
135 2, /* This is configuration #2 */
136 3, /* Third string descriptor for this configuration */
137 USBD_BMATTRIBUTES,
138 USBConfigurationDescriptor_POWER(100)
139 },
140 /* Communication class interface standard descriptor */
141 {
142 sizeof(USBInterfaceDescriptor),
143 USBGenericDescriptor_INTERFACE,
144 0, /* This is interface #0 */
145 0, /* This is alternate setting #0 for this interface */
146 2, /* This interface uses 2 endpoints */
147 //CDCCommunicationInterfaceDescriptor_CLASS,
148 0xff,
149// CDCCommunicationInterfaceDescriptor_ABSTRACTCONTROLMODEL,
150 0,
151// CDCCommunicationInterfaceDescriptor_NOPROTOCOL,
152 0,
153 3 /* Third in string descriptor for this interface */
154 },
155 /* Bulk-OUT endpoint standard descriptor */
156 {
157 sizeof(USBEndpointDescriptor),
158 USBGenericDescriptor_ENDPOINT,
159 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
160 DATAOUT),
161 USBEndpointDescriptor_BULK,
162 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT),
163 USBEndpointDescriptor_MAXBULKSIZE_FS),
164 0 /* Must be 0 for full-speed bulk endpoints */
165 },
166 /* Bulk-IN endpoint descriptor */
167 {
168 sizeof(USBEndpointDescriptor),
169 USBGenericDescriptor_ENDPOINT,
170 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
171 DATAIN),
172 USBEndpointDescriptor_BULK,
173 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN),
174 USBEndpointDescriptor_MAXBULKSIZE_FS),
175 0 /* Must be 0 for full-speed bulk endpoints */
176 }
177};
178
179const SIMTraceDriverConfigurationDescriptor *configurationDescriptorsFSarr[] = {
180 &configurationDescriptorsFS1,
181 &configurationDescriptorsFS2
182};
183
Christina Quast6255ccc2015-02-10 20:38:51 +0100184const unsigned char someString[] = {
185 USBStringDescriptor_LENGTH(4),
186 USBGenericDescriptor_STRING,
187 USBStringDescriptor_UNICODE('S'),
188 USBStringDescriptor_UNICODE('O'),
189 USBStringDescriptor_UNICODE('M'),
190 USBStringDescriptor_UNICODE('E'),
191};
192
193const unsigned char productStringDescriptor[] = {
194
195 USBStringDescriptor_LENGTH(13),
196 USBGenericDescriptor_STRING,
197 USBStringDescriptor_UNICODE('S'),
198 USBStringDescriptor_UNICODE('I'),
199 USBStringDescriptor_UNICODE('M'),
200 USBStringDescriptor_UNICODE('T'),
201 USBStringDescriptor_UNICODE('R'),
202 USBStringDescriptor_UNICODE('A'),
203 USBStringDescriptor_UNICODE('C'),
204 USBStringDescriptor_UNICODE('E'),
205 USBStringDescriptor_UNICODE('~'),
206 USBStringDescriptor_UNICODE('~'),
207 USBStringDescriptor_UNICODE('~'),
208 USBStringDescriptor_UNICODE('~'),
209 USBStringDescriptor_UNICODE('~')
210};
211
Christina Quast49ba6bc2015-02-20 14:35:36 +0100212const unsigned char productStringDescriptor2[] = {
213
214 USBStringDescriptor_LENGTH(13),
215 USBGenericDescriptor_STRING,
216 USBStringDescriptor_UNICODE('S'),
217 USBStringDescriptor_UNICODE('I'),
218 USBStringDescriptor_UNICODE('M'),
219 USBStringDescriptor_UNICODE('T'),
220 USBStringDescriptor_UNICODE('R'),
221 USBStringDescriptor_UNICODE('A'),
222 USBStringDescriptor_UNICODE('C'),
223 USBStringDescriptor_UNICODE('E'),
224 USBStringDescriptor_UNICODE('~'),
225 USBStringDescriptor_UNICODE('2'),
226 USBStringDescriptor_UNICODE('~'),
227 USBStringDescriptor_UNICODE('~'),
228 USBStringDescriptor_UNICODE('~')
229};
230
Christina Quast6255ccc2015-02-10 20:38:51 +0100231/** List of string descriptors used by the device */
232const unsigned char *stringDescriptors[] = {
233/* FIXME: Is it true that I can't use the string desc #0,
Christina Quast49ba6bc2015-02-20 14:35:36 +0100234 * because 0 also stands for "no string desc"?
235 * on the other hand, dmesg output:
236 * "string descriptor 0 malformed (err = -61), defaulting to 0x0409" */
Christina Quast6255ccc2015-02-10 20:38:51 +0100237 0,
238 productStringDescriptor,
239 someString,
Christina Quast49ba6bc2015-02-20 14:35:36 +0100240 productStringDescriptor2,
Christina Quast6255ccc2015-02-10 20:38:51 +0100241};
242
243const USBDDriverDescriptors driverDescriptors = {
244
245 &deviceDescriptor,
Christina Quast49ba6bc2015-02-20 14:35:36 +0100246 (USBConfigurationDescriptor **) &(configurationDescriptorsFSarr), /* full-speed configuration descriptor */
Christina Quast6255ccc2015-02-10 20:38:51 +0100247 0, /* No full-speed device qualifier descriptor */
248 0, /* No full-speed other speed configuration */
249 0, /* No high-speed device descriptor */
250 0, /* No high-speed configuration descriptor */
251 0, /* No high-speed device qualifier descriptor */
252 0, /* No high-speed other speed configuration descriptor */
253 stringDescriptors,
Christina Quast49ba6bc2015-02-20 14:35:36 +0100254 4 /* 4 string descriptors in list */
Christina Quast6255ccc2015-02-10 20:38:51 +0100255};
256
257
258
259/**
260 * \brief Configure 48MHz Clock for USB
261 */
262static void _ConfigureUsbClock(void)
263{
264 /* Enable PLLB for USB */
265// FIXME: are these the dividers I actually need?
266// FIXME: I could just use PLLA, since it has a frequ of 48Mhz anyways?
267 PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5)
268 | CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd*/
269 | CKGR_PLLBR_PLLBCOUNT_Msk;
270 while((PMC->PMC_SR & PMC_SR_LOCKB) == 0);
271 /* USB Clock uses PLLB */
272 PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */
273 | PMC_USB_USBS; /* PLLB */
274}
275
276
277void SIMtraceDriver_Initialize( void )
278{
279 // Get std USB driver
280 USBDDriver *pUsbd = USBD_GetDriver();
281
282 TRACE_DEBUG(".");
283
284 // Initialize standard USB driver
285 USBDDriver_Initialize(pUsbd,
286 &driverDescriptors,
287// FIXME: 2 interface settings supported in MITM mode
288 0); // Multiple interface settings not supported
289
290 USBD_Init();
291 TRACE_DEBUG("%s", "leaving");
292}
293
294/*
295void USBDDriverCallbacks_ConfigurationChanged(unsigned char cfgnum)
296{
297 printf("Configuration change requested: %c\n\r", cfgnum);
298}
299
300void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
301{
302 printf("RequestHandler called %d\n\r", USBGenericRequest_GetType(request));
303}
304*/
305/*----------------------------------------------------------------------------
306 * Main
307 *----------------------------------------------------------------------------*/
308
309/**
310 * \brief usb_cdc_serial Application entry point.
311 *
312 * Initializes drivers and start the USB <-> Serial bridge.
313 */
314int main(void)
315{
316 uint8_t isUsbConnected = 0;
317
318 LED_Configure(LED_NUM_GREEN);
319 LED_Set(LED_NUM_GREEN);
320
321 /* Disable watchdog */
322 WDT_Disable( WDT );
323
324 PIO_InitializeInterrupts(0);
325
326// NVIC_EnableIRQ(UDP_IRQn);
327
328 /* Enable UPLL for USB */
329// _ConfigureUsbClock();
330
331 /* CDC serial driver initialization */
332// CDCDSerialDriver_Initialize(&driverDescriptors);
333 SIMtraceDriver_Initialize();
334// CCIDDriver_Initialize();
335
336 USBD_Connect();
337
338 NVIC_EnableIRQ( UDP_IRQn );
339
340 printf("**** Start");
341
342 while (1) {
343 /* Device is not configured */
344 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
345
346 if (isUsbConnected) {
347 isUsbConnected = 0;
348// TC_Stop(TC0, 0);
349 }
350 }
351 else if (isUsbConnected == 0) {
352 printf("USB is now configured\n\r");
353
354 isUsbConnected = 1;
355// TC_Start(TC0, 0);
356 }
357 }
358}