Keyboard firmwares for Atmel AVR and Cortex-M
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.

VirtualSerialHost.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 VirtualSerialHost demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "VirtualSerialHost.h"
  32. /** Main program entry point. This routine configures the hardware required by the application, then
  33. * enters a loop to run the application tasks in sequence.
  34. */
  35. int main(void)
  36. {
  37. SetupHardware();
  38. puts_P(PSTR(ESC_FG_CYAN "CDC Host Demo running.\r\n" ESC_FG_WHITE));
  39. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  40. GlobalInterruptEnable();
  41. for (;;)
  42. {
  43. CDCHost_Task();
  44. USB_USBTask();
  45. }
  46. }
  47. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  48. void SetupHardware(void)
  49. {
  50. #if (ARCH == ARCH_AVR8)
  51. /* Disable watchdog if enabled by bootloader/fuses */
  52. MCUSR &= ~(1 << WDRF);
  53. wdt_disable();
  54. /* Disable clock division */
  55. clock_prescale_set(clock_div_1);
  56. #endif
  57. /* Hardware Initialization */
  58. Serial_Init(9600, false);
  59. LEDs_Init();
  60. USB_Init();
  61. /* Create a stdio stream for the serial port for stdin and stdout */
  62. Serial_CreateStream(NULL);
  63. }
  64. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  65. * starts the library USB task to begin the enumeration and USB management process.
  66. */
  67. void EVENT_USB_Host_DeviceAttached(void)
  68. {
  69. puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
  70. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  71. }
  72. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  73. * stops the library USB task management process.
  74. */
  75. void EVENT_USB_Host_DeviceUnattached(void)
  76. {
  77. puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE));
  78. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  79. }
  80. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  81. * enumerated by the host and is now ready to be used by the application.
  82. */
  83. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  84. {
  85. puts_P(PSTR("Getting Config Data.\r\n"));
  86. uint8_t ErrorCode;
  87. /* Get and process the configuration descriptor data */
  88. if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
  89. {
  90. if (ErrorCode == ControlError)
  91. puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
  92. else
  93. puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
  94. printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  95. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  96. return;
  97. }
  98. /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
  99. if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
  100. {
  101. printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"
  102. " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  103. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  104. return;
  105. }
  106. CDC_LineEncoding_t LineEncoding = { .BaudRateBPS = 9600,
  107. .CharFormat = CDC_LINEENCODING_OneStopBit,
  108. .ParityType = CDC_PARITY_None,
  109. .DataBits = 8 };
  110. USB_ControlRequest = (USB_Request_Header_t)
  111. {
  112. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  113. .bRequest = CDC_REQ_SetLineEncoding,
  114. .wValue = 0,
  115. .wIndex = 0,
  116. .wLength = sizeof(LineEncoding),
  117. };
  118. /* Set the Line Encoding of the CDC interface within the device, so that it is ready to accept data */
  119. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  120. if (USB_Host_SendControlRequest(&LineEncoding) != HOST_SENDCONTROL_Successful)
  121. {
  122. printf_P(PSTR(ESC_FG_RED "Control Error (Set Line Encoding).\r\n"
  123. " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  124. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  125. return;
  126. }
  127. puts_P(PSTR("CDC Device Enumerated.\r\n"));
  128. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  129. }
  130. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  131. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  132. {
  133. USB_Disable();
  134. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  135. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  136. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  137. for(;;);
  138. }
  139. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  140. * enumerating an attached USB device.
  141. */
  142. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  143. const uint8_t SubErrorCode)
  144. {
  145. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  146. " -- Error Code %d\r\n"
  147. " -- Sub Error Code %d\r\n"
  148. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  149. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  150. }
  151. /** Task to read in data received from the attached CDC device and print it to the serial port.
  152. */
  153. void CDCHost_Task(void)
  154. {
  155. if (USB_HostState != HOST_STATE_Configured)
  156. return;
  157. /* Select the data IN pipe */
  158. Pipe_SelectPipe(CDC_DATA_IN_PIPE);
  159. Pipe_Unfreeze();
  160. /* Check to see if a packet has been received */
  161. if (Pipe_IsINReceived())
  162. {
  163. /* Re-freeze IN pipe after the packet has been received */
  164. Pipe_Freeze();
  165. /* Check if data is in the pipe */
  166. if (Pipe_IsReadWriteAllowed())
  167. {
  168. /* Get the length of the pipe data, and create a new buffer to hold it */
  169. uint16_t BufferLength = Pipe_BytesInPipe();
  170. uint8_t Buffer[BufferLength];
  171. /* Read in the pipe data to the temporary buffer */
  172. Pipe_Read_Stream_LE(Buffer, BufferLength, NULL);
  173. /* Print out the buffer contents to the USART */
  174. for (uint16_t BufferByte = 0; BufferByte < BufferLength; BufferByte++)
  175. putchar(Buffer[BufferByte]);
  176. }
  177. /* Clear the pipe after it is read, ready for the next packet */
  178. Pipe_ClearIN();
  179. }
  180. /* Re-freeze IN pipe after use */
  181. Pipe_Freeze();
  182. /* Select and unfreeze the notification pipe */
  183. Pipe_SelectPipe(CDC_NOTIFICATION_PIPE);
  184. Pipe_Unfreeze();
  185. /* Check if a packet has been received */
  186. if (Pipe_IsINReceived())
  187. {
  188. /* Discard the unused event notification */
  189. Pipe_ClearIN();
  190. }
  191. /* Freeze notification IN pipe after use */
  192. Pipe_Freeze();
  193. }