SerialToLCD.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Copyright 2012 Simon Foster (simon.foster [at] inbox [dot] com)
  10. Permission to use, copy, modify, distribute, and sell this
  11. software and its documentation for any purpose is hereby granted
  12. without fee, provided that the above copyright notice appear in
  13. all copies and that both that the copyright notice and this
  14. permission notice and warranty disclaimer appear in supporting
  15. documentation, and that the name of the author not be used in
  16. advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.
  18. The author disclaims all warranties with regard to this
  19. software, including all implied warranties of merchantability
  20. and fitness. In no event shall the author be liable for any
  21. special, indirect or consequential damages or any damages
  22. whatsoever resulting from loss of use, data or profits, whether
  23. in an action of contract, negligence or other tortious action,
  24. arising out of or in connection with the use or performance of
  25. this software.
  26. */
  27. /** \file
  28. *
  29. * Main source file for the SerialToLCD program. This file contains the main tasks of
  30. * the project and is responsible for the initial application hardware configuration.
  31. */
  32. #include "SerialToLCD.h"
  33. /** Circular buffer to hold data from the host before it is sent to the LCD */
  34. static RingBuffer_t FromHost_Buffer;
  35. /** Underlying data buffer for \ref FromHost_Buffer, where the stored bytes are located. */
  36. static uint8_t FromHost_Buffer_Data[128];
  37. /** LUFA CDC Class driver interface configuration and state information. This structure is
  38. * passed to all CDC Class driver functions, so that multiple instances of the same class
  39. * within a device can be differentiated from one another.
  40. */
  41. USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
  42. {
  43. .Config =
  44. {
  45. .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
  46. .DataINEndpoint =
  47. {
  48. .Address = CDC_TX_EPADDR,
  49. .Size = CDC_TXRX_EPSIZE,
  50. .Banks = 1,
  51. },
  52. .DataOUTEndpoint =
  53. {
  54. .Address = CDC_RX_EPADDR,
  55. .Size = CDC_TXRX_EPSIZE,
  56. .Banks = 1,
  57. },
  58. .NotificationEndpoint =
  59. {
  60. .Address = CDC_NOTIFICATION_EPADDR,
  61. .Size = CDC_NOTIFICATION_EPSIZE,
  62. .Banks = 1,
  63. },
  64. },
  65. };
  66. /** Main program entry point. This routine contains the overall program flow, including initial
  67. * setup of all components and the main program loop.
  68. */
  69. int main(void)
  70. {
  71. SetupHardware();
  72. RingBuffer_InitBuffer(&FromHost_Buffer, FromHost_Buffer_Data, sizeof(FromHost_Buffer_Data));
  73. GlobalInterruptEnable();
  74. for (;;)
  75. {
  76. /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
  77. if (!(RingBuffer_IsFull(&FromHost_Buffer)))
  78. {
  79. int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
  80. /* Read bytes from the USB OUT endpoint into the USART transmit buffer */
  81. if (!(ReceivedByte < 0))
  82. RingBuffer_Insert(&FromHost_Buffer, ReceivedByte);
  83. }
  84. while (RingBuffer_GetCount(&FromHost_Buffer) > 0)
  85. {
  86. static uint8_t EscapePending = 0;
  87. int16_t HD44780Byte = RingBuffer_Remove(&FromHost_Buffer);
  88. if (HD44780Byte == COMMAND_ESCAPE)
  89. {
  90. if (EscapePending)
  91. {
  92. HD44780_WriteData(HD44780Byte);
  93. EscapePending = 0;
  94. }
  95. else
  96. {
  97. /* Next received character is the command byte */
  98. EscapePending = 1;
  99. }
  100. }
  101. else
  102. {
  103. if (EscapePending)
  104. {
  105. HD44780_WriteCommand(HD44780Byte);
  106. EscapePending = 0;
  107. }
  108. else
  109. {
  110. HD44780_WriteData(HD44780Byte);
  111. }
  112. }
  113. }
  114. CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
  115. USB_USBTask();
  116. }
  117. }
  118. /** Configures the board hardware and chip peripherals for the application's functionality. */
  119. void SetupHardware(void)
  120. {
  121. #if (ARCH == ARCH_AVR8)
  122. /* Disable watchdog if enabled by bootloader/fuses */
  123. MCUSR &= ~(1 << WDRF);
  124. wdt_disable();
  125. /* Disable clock division */
  126. clock_prescale_set(clock_div_1);
  127. #endif
  128. /* Hardware Initialization */
  129. USB_Init();
  130. /* Power up the HD44780 Interface */
  131. HD44780_Initialize();
  132. HD44780_WriteCommand(CMD_DISPLAY_ON);
  133. /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */
  134. TCCR0B = (1 << CS02);
  135. }
  136. /** Event handler for the library USB Configuration Changed event. */
  137. void EVENT_USB_Device_ConfigurationChanged(void)
  138. {
  139. CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
  140. }
  141. /** Event handler for the library USB Control Request reception event. */
  142. void EVENT_USB_Device_ControlRequest(void)
  143. {
  144. CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
  145. }