Christina Quast | 6255ccc | 2015-02-10 20:38:51 +0100 | [diff] [blame^] | 1 | /* ---------------------------------------------------------------------------- |
| 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" |
| 35 | #include "CDCDSerialDriver.h" |
| 36 | |
| 37 | /*---------------------------------------------------------------------------- |
| 38 | * Internal variables |
| 39 | *----------------------------------------------------------------------------*/ |
| 40 | |
| 41 | /** Standard USB device descriptor for the CDC serial driver */ |
| 42 | const USBDeviceDescriptor deviceDescriptor = { |
| 43 | |
| 44 | sizeof(USBDeviceDescriptor), |
| 45 | USBGenericDescriptor_DEVICE, |
| 46 | USBDeviceDescriptor_USB2_00, |
| 47 | 0xff, |
| 48 | // CDCDeviceDescriptor_CLASS, |
| 49 | 0xff, |
| 50 | // CDCDeviceDescriptor_SUBCLASS, |
| 51 | 0xff, |
| 52 | // CDCDeviceDescriptor_PROTOCOL, |
| 53 | BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0), |
| 54 | ATMEL_VENDOR_ID, |
| 55 | SIMTRACE_PRODUCT_ID, |
| 56 | 1, /* Release number */ |
| 57 | 0, /* No string descriptor for manufacturer */ |
| 58 | 1, /* Index of product string descriptor is #1 */ |
| 59 | 0, /* No string descriptor for serial number */ |
| 60 | 1 /* Device has 1 possible configuration */ |
| 61 | }; |
| 62 | |
| 63 | typedef struct _SIMTraceDriverConfigurationDescriptors { |
| 64 | |
| 65 | /** Standard configuration descriptor. */ |
| 66 | USBConfigurationDescriptor configuration; |
| 67 | /** Data interface descriptor. */ |
| 68 | USBInterfaceDescriptor data; |
| 69 | /** Data OUT endpoint descriptor. */ |
| 70 | USBEndpointDescriptor dataOut; |
| 71 | /** Data IN endpoint descriptor. */ |
| 72 | USBEndpointDescriptor dataIn; |
| 73 | |
| 74 | } __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptor; |
| 75 | |
| 76 | /** Standard USB configuration descriptor for the CDC serial driver */ |
| 77 | const SIMTraceDriverConfigurationDescriptor configurationDescriptorsFS = { |
| 78 | |
| 79 | /* Standard configuration descriptor */ |
| 80 | { |
| 81 | sizeof(USBConfigurationDescriptor), |
| 82 | USBGenericDescriptor_CONFIGURATION, |
| 83 | sizeof(SIMTraceDriverConfigurationDescriptor), |
| 84 | 2, /* There are two interfaces in this configuration */ |
| 85 | 2, /* This is configuration #1 */ |
| 86 | 2, /* Second string descriptor for this configuration */ |
| 87 | USBD_BMATTRIBUTES, |
| 88 | USBConfigurationDescriptor_POWER(100) |
| 89 | }, |
| 90 | /* Communication class interface standard descriptor */ |
| 91 | { |
| 92 | sizeof(USBInterfaceDescriptor), |
| 93 | USBGenericDescriptor_INTERFACE, |
| 94 | 0, /* This is interface #0 */ |
| 95 | 0, /* This is alternate setting #0 for this interface */ |
| 96 | 2, /* This interface uses 1 endpoint */ |
| 97 | //CDCCommunicationInterfaceDescriptor_CLASS, |
| 98 | 0xff, |
| 99 | // CDCCommunicationInterfaceDescriptor_ABSTRACTCONTROLMODEL, |
| 100 | 0, |
| 101 | // CDCCommunicationInterfaceDescriptor_NOPROTOCOL, |
| 102 | 0, |
| 103 | 1 /* Second in string descriptor for this interface */ |
| 104 | }, |
| 105 | /* Bulk-OUT endpoint standard descriptor */ |
| 106 | #define DATAOUT 1 |
| 107 | { |
| 108 | sizeof(USBEndpointDescriptor), |
| 109 | USBGenericDescriptor_ENDPOINT, |
| 110 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT, |
| 111 | DATAOUT), |
| 112 | USBEndpointDescriptor_BULK, |
| 113 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT), |
| 114 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 115 | 0 /* Must be 0 for full-speed bulk endpoints */ |
| 116 | }, |
| 117 | /* Bulk-IN endpoint descriptor */ |
| 118 | #define DATAIN 2 |
| 119 | { |
| 120 | sizeof(USBEndpointDescriptor), |
| 121 | USBGenericDescriptor_ENDPOINT, |
| 122 | USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN, |
| 123 | DATAIN), |
| 124 | USBEndpointDescriptor_BULK, |
| 125 | MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN), |
| 126 | USBEndpointDescriptor_MAXBULKSIZE_FS), |
| 127 | 0 /* Must be 0 for full-speed bulk endpoints */ |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | const unsigned char someString[] = { |
| 132 | USBStringDescriptor_LENGTH(4), |
| 133 | USBGenericDescriptor_STRING, |
| 134 | USBStringDescriptor_UNICODE('S'), |
| 135 | USBStringDescriptor_UNICODE('O'), |
| 136 | USBStringDescriptor_UNICODE('M'), |
| 137 | USBStringDescriptor_UNICODE('E'), |
| 138 | }; |
| 139 | |
| 140 | const unsigned char productStringDescriptor[] = { |
| 141 | |
| 142 | USBStringDescriptor_LENGTH(13), |
| 143 | USBGenericDescriptor_STRING, |
| 144 | USBStringDescriptor_UNICODE('S'), |
| 145 | USBStringDescriptor_UNICODE('I'), |
| 146 | USBStringDescriptor_UNICODE('M'), |
| 147 | USBStringDescriptor_UNICODE('T'), |
| 148 | USBStringDescriptor_UNICODE('R'), |
| 149 | USBStringDescriptor_UNICODE('A'), |
| 150 | USBStringDescriptor_UNICODE('C'), |
| 151 | USBStringDescriptor_UNICODE('E'), |
| 152 | USBStringDescriptor_UNICODE('~'), |
| 153 | USBStringDescriptor_UNICODE('~'), |
| 154 | USBStringDescriptor_UNICODE('~'), |
| 155 | USBStringDescriptor_UNICODE('~'), |
| 156 | USBStringDescriptor_UNICODE('~') |
| 157 | }; |
| 158 | |
| 159 | /** List of string descriptors used by the device */ |
| 160 | const unsigned char *stringDescriptors[] = { |
| 161 | /* FIXME: Is it true that I can't use the string desc #0, |
| 162 | * because 0 also stands for "no string desc"? */ |
| 163 | 0, |
| 164 | productStringDescriptor, |
| 165 | someString, |
| 166 | }; |
| 167 | |
| 168 | const USBDDriverDescriptors driverDescriptors = { |
| 169 | |
| 170 | &deviceDescriptor, |
| 171 | (USBConfigurationDescriptor *) &(configurationDescriptorsFS), /* full-speed configuration descriptor */ |
| 172 | 0, /* No full-speed device qualifier descriptor */ |
| 173 | 0, /* No full-speed other speed configuration */ |
| 174 | 0, /* No high-speed device descriptor */ |
| 175 | 0, /* No high-speed configuration descriptor */ |
| 176 | 0, /* No high-speed device qualifier descriptor */ |
| 177 | 0, /* No high-speed other speed configuration descriptor */ |
| 178 | stringDescriptors, |
| 179 | 3 /* 3 string descriptors in list */ |
| 180 | }; |
| 181 | |
| 182 | |
| 183 | |
| 184 | /** |
| 185 | * \brief Configure 48MHz Clock for USB |
| 186 | */ |
| 187 | static void _ConfigureUsbClock(void) |
| 188 | { |
| 189 | /* Enable PLLB for USB */ |
| 190 | // FIXME: are these the dividers I actually need? |
| 191 | // FIXME: I could just use PLLA, since it has a frequ of 48Mhz anyways? |
| 192 | PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5) |
| 193 | | CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd*/ |
| 194 | | CKGR_PLLBR_PLLBCOUNT_Msk; |
| 195 | while((PMC->PMC_SR & PMC_SR_LOCKB) == 0); |
| 196 | /* USB Clock uses PLLB */ |
| 197 | PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */ |
| 198 | | PMC_USB_USBS; /* PLLB */ |
| 199 | } |
| 200 | |
| 201 | |
| 202 | void SIMtraceDriver_Initialize( void ) |
| 203 | { |
| 204 | // Get std USB driver |
| 205 | USBDDriver *pUsbd = USBD_GetDriver(); |
| 206 | |
| 207 | TRACE_DEBUG("."); |
| 208 | |
| 209 | // Initialize standard USB driver |
| 210 | USBDDriver_Initialize(pUsbd, |
| 211 | &driverDescriptors, |
| 212 | // FIXME: 2 interface settings supported in MITM mode |
| 213 | 0); // Multiple interface settings not supported |
| 214 | |
| 215 | USBD_Init(); |
| 216 | TRACE_DEBUG("%s", "leaving"); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | void USBDDriverCallbacks_ConfigurationChanged(unsigned char cfgnum) |
| 221 | { |
| 222 | printf("Configuration change requested: %c\n\r", cfgnum); |
| 223 | } |
| 224 | |
| 225 | void USBDCallbacks_RequestReceived(const USBGenericRequest *request) |
| 226 | { |
| 227 | printf("RequestHandler called %d\n\r", USBGenericRequest_GetType(request)); |
| 228 | } |
| 229 | */ |
| 230 | /*---------------------------------------------------------------------------- |
| 231 | * Main |
| 232 | *----------------------------------------------------------------------------*/ |
| 233 | |
| 234 | /** |
| 235 | * \brief usb_cdc_serial Application entry point. |
| 236 | * |
| 237 | * Initializes drivers and start the USB <-> Serial bridge. |
| 238 | */ |
| 239 | int main(void) |
| 240 | { |
| 241 | uint8_t isUsbConnected = 0; |
| 242 | |
| 243 | LED_Configure(LED_NUM_GREEN); |
| 244 | LED_Set(LED_NUM_GREEN); |
| 245 | |
| 246 | /* Disable watchdog */ |
| 247 | WDT_Disable( WDT ); |
| 248 | |
| 249 | PIO_InitializeInterrupts(0); |
| 250 | |
| 251 | // NVIC_EnableIRQ(UDP_IRQn); |
| 252 | |
| 253 | /* Enable UPLL for USB */ |
| 254 | // _ConfigureUsbClock(); |
| 255 | |
| 256 | /* CDC serial driver initialization */ |
| 257 | // CDCDSerialDriver_Initialize(&driverDescriptors); |
| 258 | SIMtraceDriver_Initialize(); |
| 259 | // CCIDDriver_Initialize(); |
| 260 | |
| 261 | USBD_Connect(); |
| 262 | |
| 263 | NVIC_EnableIRQ( UDP_IRQn ); |
| 264 | |
| 265 | printf("**** Start"); |
| 266 | |
| 267 | while (1) { |
| 268 | /* Device is not configured */ |
| 269 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 270 | |
| 271 | if (isUsbConnected) { |
| 272 | isUsbConnected = 0; |
| 273 | // TC_Stop(TC0, 0); |
| 274 | } |
| 275 | } |
| 276 | else if (isUsbConnected == 0) { |
| 277 | printf("USB is now configured\n\r"); |
| 278 | |
| 279 | isUsbConnected = 1; |
| 280 | // TC_Start(TC0, 0); |
| 281 | } |
| 282 | } |
| 283 | } |