You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

DualVirtualSerial.c 11KB

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