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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /** 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 "Printer Host Demo running.\r\n" ESC_FG_WHITE));
  39. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  40. GlobalInterruptEnable();
  41. for (;;)
  42. {
  43. PrinterHost_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"), 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. /* Some printers use alternate settings to determine the communication protocol used - if so, send a SetInterface
  107. * request to switch to the interface alternate setting with the Bidirectional protocol */
  108. if (PrinterAltSetting)
  109. {
  110. if ((ErrorCode = USB_Host_SetInterfaceAltSetting(PrinterInterfaceNumber, PrinterAltSetting)) != HOST_SENDCONTROL_Successful)
  111. {
  112. printf_P(PSTR(ESC_FG_RED "Control Error (Set Interface).\r\n"
  113. " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  114. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  115. USB_Host_SetDeviceConfiguration(0);
  116. return;
  117. }
  118. }
  119. puts_P(PSTR("Retrieving Device ID...\r\n"));
  120. char DeviceIDString[300];
  121. if ((ErrorCode = Printer_GetDeviceID(DeviceIDString, sizeof(DeviceIDString))) != HOST_SENDCONTROL_Successful)
  122. {
  123. printf_P(PSTR(ESC_FG_RED "Control Error (Get Device ID).\r\n"
  124. " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  125. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  126. USB_Host_SetDeviceConfiguration(0);
  127. return;
  128. }
  129. printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString);
  130. puts_P(PSTR("Printer Enumerated.\r\n"));
  131. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  132. }
  133. /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
  134. void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
  135. {
  136. USB_Disable();
  137. printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
  138. " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
  139. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  140. for(;;);
  141. }
  142. /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
  143. * enumerating an attached USB device.
  144. */
  145. void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
  146. const uint8_t SubErrorCode)
  147. {
  148. printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
  149. " -- Error Code %d\r\n"
  150. " -- Sub Error Code %d\r\n"
  151. " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
  152. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  153. }
  154. /** Task to manage an enumerated USB printer once connected, to display device
  155. * information and print a test PCL page.
  156. */
  157. void PrinterHost_Task(void)
  158. {
  159. if (USB_HostState != HOST_STATE_Configured)
  160. return;
  161. uint8_t ErrorCode;
  162. /* Indicate device busy via the status LEDs */
  163. LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
  164. char TestPageData[] = "\033%-12345X\033E" "LUFA PCL Test Page" "\033E\033%-12345X";
  165. uint16_t TestPageLength = strlen(TestPageData);
  166. printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), TestPageLength);
  167. /* Send the test page to the attached printer */
  168. if ((ErrorCode = Printer_SendData(&TestPageData, TestPageLength)) != PIPE_RWSTREAM_NoError)
  169. {
  170. printf_P(PSTR(ESC_FG_RED "Error Sending Test Page.\r\n"
  171. " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
  172. LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
  173. USB_Host_SetDeviceConfiguration(0);
  174. return;
  175. }
  176. puts_P(PSTR("Test Page Sent.\r\n"));
  177. /* Indicate device no longer busy */
  178. LEDs_SetAllLEDs(LEDMASK_USB_READY);
  179. USB_Host_SetDeviceConfiguration(0);
  180. }