3      Copyright (C) Dean Camera, 2013. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2013  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  11   Copyright 2012  Simon Foster (simon.foster [at] inbox [dot] com) 
  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. 
  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 
  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. 
  38 #include "SerialToLCD.h" 
  40 /** Circular buffer to hold data from the host before it is sent to the LCD */ 
  41 static RingBuffer_t FromHost_Buffer
; 
  43 /** Underlying data buffer for \ref FromHost_Buffer, where the stored bytes are located. */ 
  44 static uint8_t      FromHost_Buffer_Data
[128]; 
  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. 
  50 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface 
= 
  54                                 .ControlInterfaceNumber   
= INTERFACE_ID_CDC_CCI
, 
  57                                                 .Address          
= CDC_TX_EPADDR
, 
  58                                                 .Size             
= CDC_TXRX_EPSIZE
, 
  63                                                 .Address          
= CDC_RX_EPADDR
, 
  64                                                 .Size             
= CDC_TXRX_EPSIZE
, 
  67                                 .NotificationEndpoint 
= 
  69                                                 .Address          
= CDC_NOTIFICATION_EPADDR
, 
  70                                                 .Size             
= CDC_NOTIFICATION_EPSIZE
, 
  77 /** Main program entry point. This routine contains the overall program flow, including initial 
  78  *  setup of all components and the main program loop. 
  84         RingBuffer_InitBuffer(&FromHost_Buffer
, FromHost_Buffer_Data
, sizeof(FromHost_Buffer_Data
)); 
  86         GlobalInterruptEnable(); 
  90                 /* Only try to read in bytes from the CDC interface if the transmit buffer is not full */ 
  91                 if (!(RingBuffer_IsFull(&FromHost_Buffer
))) 
  93                         int16_t ReceivedByte 
= CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface
); 
  95                         /* Read bytes from the USB OUT endpoint into the USART transmit buffer */ 
  96                         if (!(ReceivedByte 
< 0)) 
  97                           RingBuffer_Insert(&FromHost_Buffer
, ReceivedByte
); 
 100                 while (RingBuffer_GetCount(&FromHost_Buffer
) > 0) 
 102                         static uint8_t EscapePending 
= 0; 
 103                         int16_t HD44780Byte 
= RingBuffer_Remove(&FromHost_Buffer
); 
 105                         if (HD44780Byte 
== COMMAND_ESCAPE
) 
 109                                         HD44780_WriteData(HD44780Byte
); 
 114                                         /* Next received character is the command byte */ 
 122                                         HD44780_WriteCommand(HD44780Byte
); 
 127                                         HD44780_WriteData(HD44780Byte
); 
 132                 CDC_Device_USBTask(&VirtualSerial_CDC_Interface
); 
 137 /** Configures the board hardware and chip peripherals for the application's functionality. */ 
 138 void SetupHardware(void) 
 140 #if (ARCH == ARCH_AVR8) 
 141         /* Disable watchdog if enabled by bootloader/fuses */ 
 142         MCUSR 
&= ~(1 << WDRF
); 
 145         /* Disable clock division */ 
 146         clock_prescale_set(clock_div_1
); 
 149         /* Hardware Initialization */ 
 152         /* Power up the HD44780 Interface */ 
 153         HD44780_Initialize(); 
 154         HD44780_WriteCommand(CMD_DISPLAY_ON
); 
 156         /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */ 
 157         TCCR0B 
= (1 << CS02
); 
 160 /** Event handler for the library USB Configuration Changed event. */ 
 161 void EVENT_USB_Device_ConfigurationChanged(void) 
 163         CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface
); 
 166 /** Event handler for the library USB Control Request reception event. */ 
 167 void EVENT_USB_Device_ControlRequest(void) 
 169         CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface
);