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.

VirtualSerial.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 VirtualSerial demo. This file contains the main tasks of the demo and
  29. * is responsible for the initial application hardware configuration.
  30. */
  31. #include "VirtualSerial.h"
  32. /** Contains the current baud rate and other settings of the 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 LineEncoding = { .BaudRateBPS = 0,
  41. .CharFormat = CDC_LINEENCODING_OneStopBit,
  42. .ParityType = CDC_PARITY_None,
  43. .DataBits = 8 };
  44. /** Main program entry point. This routine contains the overall program flow, including initial
  45. * setup of all components and the main program loop.
  46. */
  47. int main(void)
  48. {
  49. SetupHardware();
  50. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  51. GlobalInterruptEnable();
  52. for (;;)
  53. {
  54. CDC_Task();
  55. USB_USBTask();
  56. }
  57. }
  58. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  59. void SetupHardware(void)
  60. {
  61. #if (ARCH == ARCH_AVR8)
  62. /* Disable watchdog if enabled by bootloader/fuses */
  63. MCUSR &= ~(1 << WDRF);
  64. wdt_disable();
  65. /* Disable clock division */
  66. clock_prescale_set(clock_div_1);
  67. #elif (ARCH == ARCH_XMEGA)
  68. /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
  69. XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
  70. XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
  71. /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
  72. XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
  73. XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
  74. PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
  75. #endif
  76. /* Hardware Initialization */
  77. Joystick_Init();
  78. LEDs_Init();
  79. USB_Init();
  80. }
  81. /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
  82. * starts the library USB task to begin the enumeration and USB management process.
  83. */
  84. void EVENT_USB_Device_Connect(void)
  85. {
  86. /* Indicate USB enumerating */
  87. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  88. }
  89. /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
  90. * the status LEDs and stops the USB management and CDC management tasks.
  91. */
  92. void EVENT_USB_Device_Disconnect(void)
  93. {
  94. /* Indicate USB not ready */
  95. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  96. }
  97. /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
  98. * of the USB device after enumeration - the device endpoints are configured and the CDC management task started.
  99. */
  100. void EVENT_USB_Device_ConfigurationChanged(void)
  101. {
  102. bool ConfigSuccess = true;
  103. /* Setup CDC Data Endpoints */
  104. ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1);
  105. ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_TX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
  106. ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_RX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
  107. /* Reset line encoding baud rate so that the host knows to send new values */
  108. LineEncoding.BaudRateBPS = 0;
  109. /* Indicate endpoint configuration success or failure */
  110. LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
  111. }
  112. /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
  113. * the device from the USB host before passing along unhandled control requests to the library for processing
  114. * internally.
  115. */
  116. void EVENT_USB_Device_ControlRequest(void)
  117. {
  118. /* Process CDC specific control requests */
  119. switch (USB_ControlRequest.bRequest)
  120. {
  121. case CDC_REQ_GetLineEncoding:
  122. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  123. {
  124. Endpoint_ClearSETUP();
  125. /* Write the line coding data to the control endpoint */
  126. Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
  127. Endpoint_ClearOUT();
  128. }
  129. break;
  130. case CDC_REQ_SetLineEncoding:
  131. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  132. {
  133. Endpoint_ClearSETUP();
  134. /* Read the line coding data in from the host into the global struct */
  135. Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
  136. Endpoint_ClearIN();
  137. }
  138. break;
  139. case CDC_REQ_SetControlLineState:
  140. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  141. {
  142. Endpoint_ClearSETUP();
  143. Endpoint_ClearStatusStage();
  144. /* NOTE: Here you can read in the line state mask from the host, to get the current state of the output handshake
  145. lines. The mask is read in from the wValue parameter in USB_ControlRequest, and can be masked against the
  146. CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:
  147. */
  148. }
  149. break;
  150. }
  151. }
  152. /** Function to manage CDC data transmission and reception to and from the host. */
  153. void CDC_Task(void)
  154. {
  155. char* ReportString = NULL;
  156. uint8_t JoyStatus_LCL = Joystick_GetStatus();
  157. static bool ActionSent = false;
  158. /* Device must be connected and configured for the task to run */
  159. if (USB_DeviceState != DEVICE_STATE_Configured)
  160. return;
  161. /* Determine if a joystick action has occurred */
  162. if (JoyStatus_LCL & JOY_UP)
  163. ReportString = "Joystick Up\r\n";
  164. else if (JoyStatus_LCL & JOY_DOWN)
  165. ReportString = "Joystick Down\r\n";
  166. else if (JoyStatus_LCL & JOY_LEFT)
  167. ReportString = "Joystick Left\r\n";
  168. else if (JoyStatus_LCL & JOY_RIGHT)
  169. ReportString = "Joystick Right\r\n";
  170. else if (JoyStatus_LCL & JOY_PRESS)
  171. ReportString = "Joystick Pressed\r\n";
  172. else
  173. ActionSent = false;
  174. /* Flag management - Only allow one string to be sent per action */
  175. if ((ReportString != NULL) && (ActionSent == false) && LineEncoding.BaudRateBPS)
  176. {
  177. ActionSent = true;
  178. /* Select the Serial Tx Endpoint */
  179. Endpoint_SelectEndpoint(CDC_TX_EPADDR);
  180. /* Write the String to the Endpoint */
  181. Endpoint_Write_Stream_LE(ReportString, strlen(ReportString), NULL);
  182. /* Remember if the packet to send completely fills the endpoint */
  183. bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);
  184. /* Finalize the stream transfer to send the last packet */
  185. Endpoint_ClearIN();
  186. /* If the last packet filled the endpoint, send an empty packet to release the buffer on
  187. * the receiver (otherwise all data will be cached until a non-full packet is received) */
  188. if (IsFull)
  189. {
  190. /* Wait until the endpoint is ready for another packet */
  191. Endpoint_WaitUntilReady();
  192. /* Send an empty packet to ensure that the host does not buffer data sent to it */
  193. Endpoint_ClearIN();
  194. }
  195. }
  196. /* Select the Serial Rx Endpoint */
  197. Endpoint_SelectEndpoint(CDC_RX_EPADDR);
  198. /* Throw away any received data from the host */
  199. if (Endpoint_IsOUTReceived())
  200. Endpoint_ClearOUT();
  201. }