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.

PrinterHost.c 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 PrinterHost demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "PrinterHost.h"
  32. /** LUFA Printer Class driver interface configuration and state information. This structure is
  33. * passed to all Printer Class driver functions, so that multiple instances of the same class
  34. * within a device can be differentiated from one another.
  35. */
  36. USB_ClassInfo_PRNT_Host_t Printer_PRNT_Interface =
  37. {
  38. .Config =
  39. {
  40. .DataINPipe =
  41. {
  42. .Address = (PIPE_DIR_IN | 1),
  43. .Banks = 1,
  44. },
  45. .DataOUTPipe =
  46. {
  47. .Address = (PIPE_DIR_OUT | 2),
  48. .Banks = 1,
  49. },
  50. },
  51. };
  52. /** Main program entry point. This routine configures the hardware required by the application, then
  53. * enters a loop to run the application tasks in sequence.
  54. */
  55. int main(void)
  56. {
  57. SetupHardware();
  58. puts_P(PSTR(ESC_FG_CYAN "Printer Host Demo running.\r\n" ESC_FG_WHITE));
  59. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  60. GlobalInterruptEnable();
  61. for (;;)
  62. {
  63. PrinterHost_Task();
  64. PRNT_Host_USBTask(&Printer_PRNT_Interface);
  65. USB_USBTask();
  66. }
  67. }
  68. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  69. void SetupHardware(void)
  70. {
  71. #if (ARCH == ARCH_AVR8)
  72. /* Disable watchdog if enabled by bootloader/fuses */
  73. MCUSR &= ~(1 << WDRF);
  74. wdt_disable();
  75. /* Disable clock division */
  76. clock_prescale_set(clock_div_1);
  77. #endif
  78. /* Hardware Initialization */
  79. Serial_Init(9600, false);
  80. LEDs_Init();
  81. USB_Init();
  82. /* Create a stdio stream for the serial port for stdin and stdout */
  83. Serial_CreateStream(NULL);
  84. }
  85. /** Task to manage an enumerated USB printer once connected, to display device
  86. * information and print a test PCL page.
  87. */
  88. void PrinterHost_Task(void)
  89. {
  90. if (USB_HostState != HOST_STATE_Configured)
  91. return;
  92. LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
  93. puts_P(PSTR("Retrieving Device ID...\r\n"));
  94. char DeviceIDString[300];
  95. if (PRNT_Host_GetDeviceID(&Printer_PRNT_Interface, DeviceIDString,
  96. sizeof(DeviceIDString)) != HOST_SENDCONTROL_Successful)
  97. {
  98. puts_P(PSTR("Error Getting Device ID.\r\n"));
  99. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  100. USB_Host_SetDeviceConfiguration(0);
  101. return;
  102. }
  103. printf_P(PSTR("Device ID: %s.\r\n"), DeviceIDString);
  104. char TestPageData[] = "\033%-12345X\033E" "LUFA PCL Test Page" "\033E\033%-12345X";
  105. uint16_t TestPageLength = strlen(TestPageData);
  106. printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), TestPageLength);
  107. if (PRNT_Host_SendData(&Printer_PRNT_Interface, &TestPageData, TestPageLength) != PIPE_RWSTREAM_NoError)
  108. {
  109. puts_P(PSTR("Error Sending Page Data.\r\n"));
  110. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  111. USB_Host_SetDeviceConfiguration(0);
  112. return;
  113. }
  114. puts_P(PSTR("Test Page Sent.\r\n"));
  115. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  116. USB_Host_SetDeviceConfiguration(0);
  117. }
  118. /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
  119. * starts the library USB task to begin the enumeration and USB management process.
  120. */
  121. void EVENT_USB_Host_DeviceAttached(void)
  122. {
  123. puts_P(PSTR("Device Attached.\r\n"));
  124. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  125. }
  126. /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
  127. * stops the library USB task management process.
  128. */
  129. void EVENT_USB_Host_DeviceUnattached(void)
  130. {
  131. puts_P(PSTR("\r\nDevice Unattached.\r\n"));
  132. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  133. }
  134. /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
  135. * enumerated by the host and is now ready to be used by the application.
  136. */
  137. void EVENT_USB_Host_DeviceEnumerationComplete(void)
  138. {
  139. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  140. uint16_t ConfigDescriptorSize;
  141. uint8_t ConfigDescriptorData[512];
  142. if (USB_Host_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,
  143. sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)
  144. {
  145. puts_P(PSTR("Error Retrieving Configuration Descriptor.\r\n"));
  146. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  147. return;
  148. }
  149. if (PRNT_Host_ConfigurePipes(&Printer_PRNT_Interface,
  150. ConfigDescriptorSize, ConfigDescriptorData) != PRNT_ENUMERROR_NoError)
  151. {
  152. puts_P(PSTR("Attached Device Not a Valid Printer Class Device.\r\n"));
  153. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  154. return;
  155. }
  156. if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
  157. {
  158. puts_P(PSTR("Error Setting Device Configuration.\r\n"));
  159. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  160. return;
  161. }
  162. if (PRNT_Host_SetBidirectionalMode(&Printer_PRNT_Interface) != HOST_SENDCONTROL_Successful)
  163. {
  164. puts_P(PSTR("Error Setting Bidirectional Mode.\r\n"));
  165. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  166. USB_Host_SetDeviceConfiguration(0);
  167. return;
  168. }
  169. puts_P(PSTR("Printer Device Enumerated.\r\n"));
  170. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  171. }
  172. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  173. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  174. {
  175. USB_Disable();
  176. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  177. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  178. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  179. for(;;);
  180. }
  181. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  182. * enumerating an attached USB device.
  183. */
  184. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  185. const uint8_t SubErrorCode)
  186. {
  187. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  188. " -- Error Code %d\r\n"
  189. " -- Sub Error Code %d\r\n"
  190. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  191. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  192. }