diff options
Diffstat (limited to 'lib/lufa/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.c')
| -rw-r--r-- | lib/lufa/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.c | 299 |
1 files changed, 0 insertions, 299 deletions
diff --git a/lib/lufa/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.c b/lib/lufa/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.c deleted file mode 100644 index b94a67dab..000000000 --- a/lib/lufa/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.c +++ /dev/null | |||
| @@ -1,299 +0,0 @@ | |||
| 1 | /* | ||
| 2 | LUFA Library | ||
| 3 | Copyright (C) Dean Camera, 2017. | ||
| 4 | |||
| 5 | dean [at] fourwalledcubicle [dot] com | ||
| 6 | www.lufa-lib.org | ||
| 7 | */ | ||
| 8 | |||
| 9 | /* | ||
| 10 | Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) | ||
| 11 | |||
| 12 | Permission to use, copy, modify, distribute, and sell this | ||
| 13 | software and its documentation for any purpose is hereby granted | ||
| 14 | without fee, provided that the above copyright notice appear in | ||
| 15 | all copies and that both that the copyright notice and this | ||
| 16 | permission notice and warranty disclaimer appear in supporting | ||
| 17 | documentation, and that the name of the author not be used in | ||
| 18 | advertising or publicity pertaining to distribution of the | ||
| 19 | software without specific, written prior permission. | ||
| 20 | |||
| 21 | The author disclaims all warranties with regard to this | ||
| 22 | software, including all implied warranties of merchantability | ||
| 23 | and fitness. In no event shall the author be liable for any | ||
| 24 | special, indirect or consequential damages or any damages | ||
| 25 | whatsoever resulting from loss of use, data or profits, whether | ||
| 26 | in an action of contract, negligence or other tortious action, | ||
| 27 | arising out of or in connection with the use or performance of | ||
| 28 | this software. | ||
| 29 | */ | ||
| 30 | |||
| 31 | /** \file | ||
| 32 | * | ||
| 33 | * Main source file for the DualVirtualSerial demo. This file contains the main tasks of the demo and | ||
| 34 | * is responsible for the initial application hardware configuration. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #include "DualVirtualSerial.h" | ||
| 38 | |||
| 39 | /** Contains the current baud rate and other settings of the first virtual serial port. While this demo does not use | ||
| 40 | * the physical USART and thus does not use these settings, they must still be retained and returned to the host | ||
| 41 | * upon request or the host will assume the device is non-functional. | ||
| 42 | * | ||
| 43 | * These values are set by the host via a class-specific request, however they are not required to be used accurately. | ||
| 44 | * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical | ||
| 45 | * serial link characteristics and instead sends and receives data in endpoint streams. | ||
| 46 | */ | ||
| 47 | static CDC_LineEncoding_t LineEncoding1 = { .BaudRateBPS = 0, | ||
| 48 | .CharFormat = CDC_LINEENCODING_OneStopBit, | ||
| 49 | .ParityType = CDC_PARITY_None, | ||
| 50 | .DataBits = 8 }; | ||
| 51 | |||
| 52 | /** Contains the current baud rate and other settings of the second virtual serial port. While this demo does not use | ||
| 53 | * the physical USART and thus does not use these settings, they must still be retained and returned to the host | ||
| 54 | * upon request or the host will assume the device is non-functional. | ||
| 55 | * | ||
| 56 | * These values are set by the host via a class-specific request, however they are not required to be used accurately. | ||
| 57 | * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical | ||
| 58 | * serial link characteristics and instead sends and receives data in endpoint streams. | ||
| 59 | */ | ||
| 60 | static CDC_LineEncoding_t LineEncoding2 = { .BaudRateBPS = 0, | ||
| 61 | .CharFormat = CDC_LINEENCODING_OneStopBit, | ||
| 62 | .ParityType = CDC_PARITY_None, | ||
| 63 | .DataBits = 8 }; | ||
| 64 | |||
| 65 | |||
| 66 | /** Main program entry point. This routine configures the hardware required by the application, then | ||
| 67 | * enters a loop to run the application tasks in sequence. | ||
| 68 | */ | ||
| 69 | int main(void) | ||
| 70 | { | ||
| 71 | SetupHardware(); | ||
| 72 | |||
| 73 | LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); | ||
| 74 | GlobalInterruptEnable(); | ||
| 75 | |||
| 76 | for (;;) | ||
| 77 | { | ||
| 78 | CDC1_Task(); | ||
| 79 | CDC2_Task(); | ||
| 80 | USB_USBTask(); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | /** Configures the board hardware and chip peripherals for the demo's functionality. */ | ||
| 85 | void SetupHardware(void) | ||
| 86 | { | ||
| 87 | #if (ARCH == ARCH_AVR8) | ||
| 88 | /* Disable watchdog if enabled by bootloader/fuses */ | ||
| 89 | MCUSR &= ~(1 << WDRF); | ||
| 90 | wdt_disable(); | ||
| 91 | |||
| 92 | /* Disable clock division */ | ||
| 93 | clock_prescale_set(clock_div_1); | ||
| 94 | #elif (ARCH == ARCH_XMEGA) | ||
| 95 | /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ | ||
| 96 | XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU); | ||
| 97 | XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL); | ||
| 98 | |||
| 99 | /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ | ||
| 100 | XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ); | ||
| 101 | XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB); | ||
| 102 | |||
| 103 | PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm; | ||
| 104 | #endif | ||
| 105 | |||
| 106 | /* Hardware Initialization */ | ||
| 107 | Joystick_Init(); | ||
| 108 | LEDs_Init(); | ||
| 109 | USB_Init(); | ||
| 110 | } | ||
| 111 | |||
| 112 | /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and | ||
| 113 | * starts the library USB task to begin the enumeration and USB management process. | ||
| 114 | */ | ||
| 115 | void EVENT_USB_Device_Connect(void) | ||
| 116 | { | ||
| 117 | /* Indicate USB enumerating */ | ||
| 118 | LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); | ||
| 119 | } | ||
| 120 | |||
| 121 | /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via | ||
| 122 | * the status LEDs and stops the USB management and CDC management tasks. | ||
| 123 | */ | ||
| 124 | void EVENT_USB_Device_Disconnect(void) | ||
| 125 | { | ||
| 126 | /* Indicate USB not ready */ | ||
| 127 | LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); | ||
| 128 | } | ||
| 129 | |||
| 130 | /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration | ||
| 131 | * of the USB device after enumeration - the device endpoints are configured and the CDC management tasks are started. | ||
| 132 | */ | ||
| 133 | void EVENT_USB_Device_ConfigurationChanged(void) | ||
| 134 | { | ||
| 135 | bool ConfigSuccess = true; | ||
| 136 | |||
| 137 | /* Setup first CDC Interface's Endpoints */ | ||
| 138 | ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC1_TX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1); | ||
| 139 | ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC1_RX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1); | ||
| 140 | ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC1_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1); | ||
| 141 | |||
| 142 | /* Setup second CDC Interface's Endpoints */ | ||
| 143 | ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC2_TX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1); | ||
| 144 | ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC2_RX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1); | ||
| 145 | ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC2_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1); | ||
| 146 | |||
| 147 | /* Reset line encoding baud rates so that the host knows to send new values */ | ||
| 148 | LineEncoding1.BaudRateBPS = 0; | ||
| 149 | LineEncoding2.BaudRateBPS = 0; | ||
| 150 | |||
| 151 | /* Indicate endpoint configuration success or failure */ | ||
| 152 | LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); | ||
| 153 | } | ||
| 154 | |||
| 155 | /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to | ||
| 156 | * the device from the USB host before passing along unhandled control requests to the library for processing | ||
| 157 | * internally. | ||
| 158 | */ | ||
| 159 | void EVENT_USB_Device_ControlRequest(void) | ||
| 160 | { | ||
| 161 | /* Determine which interface's Line Coding data is being set from the wIndex parameter */ | ||
| 162 | void* LineEncodingData = (USB_ControlRequest.wIndex == 0) ? &LineEncoding1 : &LineEncoding2; | ||
| 163 | |||
| 164 | /* Process CDC specific control requests */ | ||
| 165 | switch (USB_ControlRequest.bRequest) | ||
| 166 | { | ||
| 167 | case CDC_REQ_GetLineEncoding: | ||
| 168 | if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) | ||
| 169 | { | ||
| 170 | Endpoint_ClearSETUP(); | ||
| 171 | |||
| 172 | /* Write the line coding data to the control endpoint */ | ||
| 173 | Endpoint_Write_Control_Stream_LE(LineEncodingData, sizeof(CDC_LineEncoding_t)); | ||
| 174 | Endpoint_ClearOUT(); | ||
| 175 | } | ||
| 176 | |||
| 177 | break; | ||
| 178 | case CDC_REQ_SetLineEncoding: | ||
| 179 | if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) | ||
| 180 | { | ||
| 181 | Endpoint_ClearSETUP(); | ||
| 182 | |||
| 183 | /* Read the line coding data in from the host into the global struct */ | ||
| 184 | Endpoint_Read_Control_Stream_LE(LineEncodingData, sizeof(CDC_LineEncoding_t)); | ||
| 185 | Endpoint_ClearIN(); | ||
| 186 | } | ||
| 187 | |||
| 188 | break; | ||
| 189 | case CDC_REQ_SetControlLineState: | ||
| 190 | if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) | ||
| 191 | { | ||
| 192 | Endpoint_ClearSETUP(); | ||
| 193 | Endpoint_ClearStatusStage(); | ||
| 194 | } | ||
| 195 | |||
| 196 | break; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | /** Function to manage CDC data transmission and reception to and from the host for the first CDC interface, which sends joystick | ||
| 201 | * movements to the host as ASCII strings. | ||
| 202 | */ | ||
| 203 | void CDC1_Task(void) | ||
| 204 | { | ||
| 205 | char* ReportString = NULL; | ||
| 206 | uint8_t JoyStatus_LCL = Joystick_GetStatus(); | ||
| 207 | static bool ActionSent = false; | ||
| 208 | |||
| 209 | /* Device must be connected and configured for the task to run */ | ||
| 210 | if (USB_DeviceState != DEVICE_STATE_Configured) | ||
| 211 | return; | ||
| 212 | |||
| 213 | /* Determine if a joystick action has occurred */ | ||
| 214 | if (JoyStatus_LCL & JOY_UP) | ||
| 215 | ReportString = "Joystick Up\r\n"; | ||
| 216 | else if (JoyStatus_LCL & JOY_DOWN) | ||
| 217 | ReportString = "Joystick Down\r\n"; | ||
| 218 | else if (JoyStatus_LCL & JOY_LEFT) | ||
| 219 | ReportString = "Joystick Left\r\n"; | ||
| 220 | else if (JoyStatus_LCL & JOY_RIGHT) | ||
| 221 | ReportString = "Joystick Right\r\n"; | ||
| 222 | else if (JoyStatus_LCL & JOY_PRESS) | ||
| 223 | ReportString = "Joystick Pressed\r\n"; | ||
| 224 | else | ||
| 225 | ActionSent = false; | ||
| 226 | |||
| 227 | /* Flag management - Only allow one string to be sent per action */ | ||
| 228 | if ((ReportString != NULL) && (ActionSent == false) && LineEncoding1.BaudRateBPS) | ||
| 229 | { | ||
| 230 | ActionSent = true; | ||
| 231 | |||
| 232 | /* Select the Serial Tx Endpoint */ | ||
| 233 | Endpoint_SelectEndpoint(CDC1_TX_EPADDR); | ||
| 234 | |||
| 235 | /* Write the String to the Endpoint */ | ||
| 236 | Endpoint_Write_Stream_LE(ReportString, strlen(ReportString), NULL); | ||
| 237 | |||
| 238 | /* Finalize the stream transfer to send the last packet */ | ||
| 239 | Endpoint_ClearIN(); | ||
| 240 | |||
| 241 | /* Wait until the endpoint is ready for another packet */ | ||
| 242 | Endpoint_WaitUntilReady(); | ||
| 243 | |||
| 244 | /* Send an empty packet to ensure that the host does not buffer data sent to it */ | ||
| 245 | Endpoint_ClearIN(); | ||
| 246 | } | ||
| 247 | |||
| 248 | /* Select the Serial Rx Endpoint */ | ||
| 249 | Endpoint_SelectEndpoint(CDC1_RX_EPADDR); | ||
| 250 | |||
| 251 | /* Throw away any received data from the host */ | ||
| 252 | if (Endpoint_IsOUTReceived()) | ||
| 253 | Endpoint_ClearOUT(); | ||
| 254 | } | ||
| 255 | |||
| 256 | /** Function to manage CDC data transmission and reception to and from the host for the second CDC interface, which echoes back | ||
| 257 | * all data sent to it from the host. | ||
| 258 | */ | ||
| 259 | void CDC2_Task(void) | ||
| 260 | { | ||
| 261 | /* Device must be connected and configured for the task to run */ | ||
| 262 | if (USB_DeviceState != DEVICE_STATE_Configured) | ||
| 263 | return; | ||
| 264 | |||
| 265 | /* Select the Serial Rx Endpoint */ | ||
| 266 | Endpoint_SelectEndpoint(CDC2_RX_EPADDR); | ||
| 267 | |||
| 268 | /* Check to see if any data has been received */ | ||
| 269 | if (Endpoint_IsOUTReceived()) | ||
| 270 | { | ||
| 271 | /* Create a temp buffer big enough to hold the incoming endpoint packet */ | ||
| 272 | uint8_t Buffer[Endpoint_BytesInEndpoint()]; | ||
| 273 | |||
| 274 | /* Remember how large the incoming packet is */ | ||
| 275 | uint16_t DataLength = Endpoint_BytesInEndpoint(); | ||
| 276 | |||
| 277 | /* Read in the incoming packet into the buffer */ | ||
| 278 | Endpoint_Read_Stream_LE(&Buffer, DataLength, NULL); | ||
| 279 | |||
| 280 | /* Finalize the stream transfer to send the last packet */ | ||
| 281 | Endpoint_ClearOUT(); | ||
| 282 | |||
| 283 | /* Select the Serial Tx Endpoint */ | ||
| 284 | Endpoint_SelectEndpoint(CDC2_TX_EPADDR); | ||
| 285 | |||
| 286 | /* Write the received data to the endpoint */ | ||
| 287 | Endpoint_Write_Stream_LE(&Buffer, DataLength, NULL); | ||
| 288 | |||
| 289 | /* Finalize the stream transfer to send the last packet */ | ||
| 290 | Endpoint_ClearIN(); | ||
| 291 | |||
| 292 | /* Wait until the endpoint is ready for the next packet */ | ||
| 293 | Endpoint_WaitUntilReady(); | ||
| 294 | |||
| 295 | /* Send an empty packet to prevent host buffering */ | ||
| 296 | Endpoint_ClearIN(); | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
