7a289270b465d84b7cd429539903da2727f4232d
[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 * \section Hardware Information
38 *
39 * LCD Datasheet: See http://www.sparkfun.com/datasheets/LCD/HD44780.pdf
40 *
41 * Also see the two articles from EPE which are linked from here:
42 * http://en.wikipedia.org/wiki/HD44780_Character_LCD
43 *
44 * Connections from the Minimus to the HD44780 as shown below.
45 *
46 * ========================================================= \n
47 * Minimus HD44780 Pin \n
48 * ========================================================= \n
49 * PD0 DB4 11 \n
50 * PD1 DB5 12 \n
51 * PD2 DB6 13 \n
52 * PD3 DB7 14 \n
53 * \n
54 * PD4 RS 4 \n
55 * RW 5 GND \n
56 * PD7 EN 6 \n
57 * \n
58 * 1 GND \n
59 * 2 USB +5V \n
60 * 3 2k -> GND \n
61 */
62
63 #include "SerialToLCD.h"
64
65 /** Circular buffer to hold data from the host before it is sent to the LCD */
66 static RingBuffer_t FromHost_Buffer;
67
68 /** Underlying data buffer for \ref FromHost_Buffer, where the stored bytes are located. */
69 static uint8_t FromHost_Buffer_Data[128];
70
71 /** LUFA CDC Class driver interface configuration and state information. This structure is
72 * passed to all CDC Class driver functions, so that multiple instances of the same class
73 * within a device can be differentiated from one another.
74 */
75 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
76 {
77 .Config =
78 {
79 .ControlInterfaceNumber = 0,
80 .DataINEndpoint =
81 {
82 .Address = CDC_TX_EPADDR,
83 .Size = CDC_TXRX_EPSIZE,
84 .Banks = 1,
85 },
86 .DataOUTEndpoint =
87 {
88 .Address = CDC_RX_EPADDR,
89 .Size = CDC_TXRX_EPSIZE,
90 .Banks = 1,
91 },
92 .NotificationEndpoint =
93 {
94 .Address = CDC_NOTIFICATION_EPADDR,
95 .Size = CDC_NOTIFICATION_EPSIZE,
96 .Banks = 1,
97 },
98 },
99 };
100
101
102 /** Main program entry point. This routine contains the overall program flow, including initial
103 * setup of all components and the main program loop.
104 */
105 int main(void)
106 {
107 SetupHardware();
108
109 RingBuffer_InitBuffer(&FromHost_Buffer, FromHost_Buffer_Data, sizeof(FromHost_Buffer_Data));
110
111 sei();
112
113 for (;;)
114 {
115 /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */
116 if (!(RingBuffer_IsFull(&FromHost_Buffer)))
117 {
118 int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
119
120 /* Read bytes from the USB OUT endpoint into the USART transmit buffer */
121 if (!(ReceivedByte < 0))
122 RingBuffer_Insert(&FromHost_Buffer, ReceivedByte);
123 }
124
125 while (RingBuffer_GetCount(&FromHost_Buffer) > 0)
126 {
127 static uint8_t escape_pending = 0;
128 int16_t HD44780Byte = RingBuffer_Remove(&FromHost_Buffer);
129
130 if (HD44780Byte == COMMAND_ESCAPE)
131 {
132 if (escape_pending)
133 {
134 HD44780_WriteData(HD44780Byte);
135 escape_pending = 0;
136 }
137 else
138 {
139 escape_pending = 1;
140 }
141 }
142 else
143 {
144 if (escape_pending)
145 {
146 HD44780_WriteCommand(HD44780Byte);
147 escape_pending = 0;
148 }
149 else
150 {
151 HD44780_WriteData(HD44780Byte);
152 }
153 }
154 }
155
156 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
157 USB_USBTask();
158 }
159 }
160
161 /** Configures the board hardware and chip peripherals for the application's functionality. */
162 void SetupHardware(void)
163 {
164 /* Disable watchdog if enabled by bootloader/fuses */
165 MCUSR &= ~(1 << WDRF);
166 wdt_disable();
167
168 /* Disable clock division */
169 clock_prescale_set(clock_div_1);
170
171 /* Hardware Initialization */
172 USB_Init();
173
174 /* Power up the HD44780 Interface */
175 HD44780_Initialise();
176 HD44780_WriteCommand(CMD_DISPLAY_ON);
177
178 /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */
179 TCCR0B = (1 << CS02);
180 }
181
182 /** Event handler for the library USB Configuration Changed event. */
183 void EVENT_USB_Device_ConfigurationChanged(void)
184 {
185 bool ConfigSuccess = true;
186
187 ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
188 }
189
190 /** Event handler for the library USB Control Request reception event. */
191 void EVENT_USB_Device_ControlRequest(void)
192 {
193 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
194 }