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