Keyboard firmwares for Atmel AVR and Cortex-M
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RNDISEthernetHost.c 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 RNDISEthernetHost demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "RNDISEthernetHost.h"
  32. /** Buffer to hold incoming and outgoing Ethernet packets. */
  33. static int8_t PacketBuffer[1024];
  34. /** LUFA RNDIS Class driver interface configuration and state information. This structure is
  35. * passed to all RNDIS Class driver functions, so that multiple instances of the same class
  36. * within a device can be differentiated from one another.
  37. */
  38. USB_ClassInfo_RNDIS_Host_t Ethernet_RNDIS_Interface =
  39. {
  40. .Config =
  41. {
  42. .DataINPipe =
  43. {
  44. .Address = (PIPE_DIR_IN | 1),
  45. .Banks = 1,
  46. },
  47. .DataOUTPipe =
  48. {
  49. .Address = (PIPE_DIR_OUT | 2),
  50. .Banks = 1,
  51. },
  52. .NotificationPipe =
  53. {
  54. .Address = (PIPE_DIR_IN | 3),
  55. .Banks = 1,
  56. },
  57. .HostMaxPacketSize = sizeof(PacketBuffer),
  58. },
  59. };
  60. /** Main program entry point. This routine configures the hardware required by the application, then
  61. * enters a loop to run the application tasks in sequence.
  62. */
  63. int main(void)
  64. {
  65. SetupHardware();
  66. puts_P(PSTR(ESC_FG_CYAN "RNDIS Host Demo running.\r\n" ESC_FG_WHITE));
  67. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  68. sei();
  69. for (;;)
  70. {
  71. RNDISHost_Task();
  72. RNDIS_Host_USBTask(&Ethernet_RNDIS_Interface);
  73. USB_USBTask();
  74. }
  75. }
  76. /** Task to manage an enumerated USB RNDIS device once connected, to display device
  77. * received data packets.
  78. */
  79. void RNDISHost_Task(void)
  80. {
  81. if (USB_HostState != HOST_STATE_Configured)
  82. return;
  83. if (RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface))
  84. {
  85. LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
  86. uint16_t PacketLength;
  87. RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface, &PacketBuffer, &PacketLength);
  88. printf_P(PSTR("***PACKET (Size %d)***\r\n"), PacketLength);
  89. for (uint16_t i = 0; i < PacketLength; i++)
  90. printf("0x%02x ", PacketBuffer[i]);
  91. printf_P(PSTR("\r\n\r\n"));
  92. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  93. }
  94. }
  95. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  96. void SetupHardware(void)
  97. {
  98. #if (ARCH == ARCH_AVR8)
  99. /* Disable watchdog if enabled by bootloader/fuses */
  100. MCUSR &= ~(1 << WDRF);
  101. wdt_disable();
  102. /* Disable clock division */
  103. clock_prescale_set(clock_div_1);
  104. #endif
  105. /* Hardware Initialization */
  106. Serial_Init(9600, false);
  107. LEDs_Init();
  108. USB_Init();
  109. /* Create a stdio stream for the serial port for stdin and stdout */
  110. Serial_CreateStream(NULL);
  111. }
  112. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  113. * starts the library USB task to begin the enumeration and USB management process.
  114. */
  115. void EVENT_USB_Host_DeviceAttached(void)
  116. {
  117. puts_P(PSTR("Device Attached.\r\n"));
  118. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  119. }
  120. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  121. * stops the library USB task management process.
  122. */
  123. void EVENT_USB_Host_DeviceUnattached(void)
  124. {
  125. puts_P(PSTR("\r\nDevice Unattached.\r\n"));
  126. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  127. }
  128. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  129. * enumerated by the host and is now ready to be used by the application.
  130. */
  131. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  132. {
  133. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  134. uint16_t ConfigDescriptorSize;
  135. uint8_t ConfigDescriptorData[512];
  136. if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
  137. sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
  138. {
  139. puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
  140. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  141. return;
  142. }
  143. if (RNDIS_Host_ConfigurePipes(&Ethernet_RNDIS_Interface,
  144. ConfigDescriptorSize, ConfigDescriptorData) != RNDIS_ENUMERROR_NoError)
  145. {
  146. puts_P(PSTR("Attached Device Not a Valid RNDIS Class Device.\r\n"));
  147. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  148. return;
  149. }
  150. if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
  151. {
  152. puts_P(PSTR("Error Setting Device Configuration.\r\n"));
  153. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  154. return;
  155. }
  156. if (RNDIS_Host_InitializeDevice(&Ethernet_RNDIS_Interface) != HOST_SENDCONTROL_Successful)
  157. {
  158. puts_P(PSTR("Error Initializing Device.\r\n"));
  159. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  160. USB_Host_SetDeviceConfiguration(0);
  161. return;
  162. }
  163. printf_P(PSTR("Device Max Transfer Size: %lu bytes.\r\n"), Ethernet_RNDIS_Interface.State.DeviceMaxPacketSize);
  164. uint32_t PacketFilter = (REMOTE_NDIS_PACKET_DIRECTED | REMOTE_NDIS_PACKET_BROADCAST | REMOTE_NDIS_PACKET_ALL_MULTICAST);
  165. if (RNDIS_Host_SetRNDISProperty(&Ethernet_RNDIS_Interface, OID_GEN_CURRENT_PACKET_FILTER,
  166. &PacketFilter, sizeof(PacketFilter)) != HOST_SENDCONTROL_Successful)
  167. {
  168. puts_P(PSTR("Error Setting Device Packet Filter.\r\n"));
  169. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  170. USB_Host_SetDeviceConfiguration(0);
  171. return;
  172. }
  173. uint32_t VendorID;
  174. if (RNDIS_Host_QueryRNDISProperty(&Ethernet_RNDIS_Interface, OID_GEN_VENDOR_ID,
  175. &VendorID, sizeof(VendorID)) != HOST_SENDCONTROL_Successful)
  176. {
  177. puts_P(PSTR("Error Getting Vendor ID.\r\n"));
  178. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  179. USB_Host_SetDeviceConfiguration(0);
  180. return;
  181. }
  182. printf_P(PSTR("Device Vendor ID: 0x%08lX\r\n"), VendorID);
  183. puts_P(PSTR("RNDIS Device Enumerated.\r\n"));
  184. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  185. }
  186. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  187. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  188. {
  189. USB_Disable();
  190. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  191. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  192. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  193. for(;;);
  194. }
  195. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  196. * enumerating an attached USB device.
  197. */
  198. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  199. const uint8_t SubErrorCode)
  200. {
  201. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  202. " -- Error Code %d\r\n"
  203. " -- Sub Error Code %d\r\n"
  204. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  205. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  206. }