]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Projects/SerialToLCD/SerialToLCD.c
Merge commit '22b6e15a179031afb7c3534cf7b109b0668b602c'
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Projects / SerialToLCD / SerialToLCD.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  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 disclaims 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   = INTERFACE_ID_CDC_CCI,
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         GlobalInterruptEnable();
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 EscapePending = 0;
103                         int16_t HD44780Byte = RingBuffer_Remove(&FromHost_Buffer);
104
105                         if (HD44780Byte == COMMAND_ESCAPE)
106                         {
107                                 if (EscapePending)
108                                 {
109                                         HD44780_WriteData(HD44780Byte);
110                                         EscapePending = 0;
111                                 }
112                                 else
113                                 {
114                                         /* Next received character is the command byte */
115                                         EscapePending = 1;
116                                 }
117                         }
118                         else
119                         {
120                                 if (EscapePending)
121                                 {
122                                         HD44780_WriteCommand(HD44780Byte);
123                                         EscapePending = 0;
124                                 }
125                                 else
126                                 {
127                                         HD44780_WriteData(HD44780Byte);
128                                 }
129                         }
130                 }
131
132                 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
133                 USB_USBTask();
134         }
135 }
136
137 /** Configures the board hardware and chip peripherals for the application's functionality. */
138 void SetupHardware(void)
139 {
140 #if (ARCH == ARCH_AVR8)
141         /* Disable watchdog if enabled by bootloader/fuses */
142         MCUSR &= ~(1 << WDRF);
143         wdt_disable();
144
145         /* Disable clock division */
146         clock_prescale_set(clock_div_1);
147 #endif
148
149         /* Hardware Initialization */
150         USB_Init();
151
152         /* Power up the HD44780 Interface */
153         HD44780_Initialize();
154         HD44780_WriteCommand(CMD_DISPLAY_ON);
155
156         /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */
157         TCCR0B = (1 << CS02);
158 }
159
160 /** Event handler for the library USB Configuration Changed event. */
161 void EVENT_USB_Device_ConfigurationChanged(void)
162 {
163         CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
164 }
165
166 /** Event handler for the library USB Control Request reception event. */
167 void EVENT_USB_Device_ControlRequest(void)
168 {
169         CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
170 }