3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * Main source file for the Benito project. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
39 volatile uint8_t ResetPulseMSRemaining
= 0;
40 volatile uint8_t TxPulseMSRemaining
= 0;
41 volatile uint8_t RxPulseMSRemaining
= 0;
43 /** LUFA CDC Class driver interface configuration and state information. This structure is
44 * passed to all CDC Class driver functions, so that multiple instances of the same class
45 * within a device can be differentiated from one another.
47 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface
=
51 .ControlInterfaceNumber
= 0,
53 .DataINEndpointNumber
= CDC_TX_EPNUM
,
54 .DataINEndpointSize
= CDC_TXRX_EPSIZE
,
56 .DataOUTEndpointNumber
= CDC_RX_EPNUM
,
57 .DataOUTEndpointSize
= CDC_TXRX_EPSIZE
,
59 .NotificationEndpointNumber
= CDC_NOTIFICATION_EPNUM
,
60 .NotificationEndpointSize
= CDC_NOTIFICATION_EPSIZE
,
65 // Leave all state values to their defaults
69 /** Main program entry point. This routine contains the overall program flow, including initial
70 * setup of all components and the main program loop.
76 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
80 /* Echo bytes from the host to the target via the hardware USART */
81 if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface
))
83 Serial_TxByte(CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface
));
85 LEDs_TurnOnLEDs(LEDMASK_TX
);
86 TxPulseMSRemaining
= TX_RX_LED_PULSE_MS
;
89 /* Echo bytes from the target to the host via the virtual serial port */
90 if (Serial_IsCharReceived())
92 CDC_Device_SendByte(&VirtualSerial_CDC_Interface
, Serial_RxByte());
94 LEDs_TurnOnLEDs(LEDMASK_RX
);
95 RxPulseMSRemaining
= TX_RX_LED_PULSE_MS
;
98 /* Check if the millisecond timer has elapsed */
99 if (TIFR0
& (1 << OCF0A
))
101 /* Check if the reset pulse period has elapsed, if so tristate the target reset line */
102 if (ResetPulseMSRemaining
&& !(--ResetPulseMSRemaining
))
104 AVR_RESET_LINE_PORT
&= ~AVR_RESET_LINE_MASK
;
105 AVR_RESET_LINE_DDR
&= ~AVR_RESET_LINE_MASK
;
108 /* Turn off TX LED(s) once the TX pulse period has elapsed */
109 if (TxPulseMSRemaining
&& !(--TxPulseMSRemaining
))
110 LEDs_TurnOffLEDs(LEDMASK_TX
);
112 /* Turn off RX LED(s) once the RX pulse period has elapsed */
113 if (RxPulseMSRemaining
&& !(--RxPulseMSRemaining
))
114 LEDs_TurnOffLEDs(LEDMASK_RX
);
116 /* Clear the millisecond timer CTC flag (cleared by writing logic one to the register) */
117 TIFR0
|= (1 << OCF0A
);
120 CDC_Device_USBTask(&VirtualSerial_CDC_Interface
);
125 /** Configures the board hardware and chip peripherals for the demo's functionality. */
126 void SetupHardware(void)
128 /* Disable watchdog if enabled by bootloader/fuses */
129 MCUSR
&= ~(1 << WDRF
);
132 /* Disable clock division */
133 clock_prescale_set(clock_div_1
);
135 /* Hardware Initialization */
139 /* Millisecond Timer Interrupt */
140 OCR0A
= (F_CPU
/ 64 / 1000);
141 TCCR0A
= (1 << WGM01
);
142 TCCR0B
= ((1 << CS01
) | (1 << CS00
));
144 /* Tristate target /RESET Line */
145 AVR_RESET_LINE_PORT
&= ~AVR_RESET_LINE_MASK
;
146 AVR_RESET_LINE_DDR
&= ~AVR_RESET_LINE_MASK
;
149 /** Event handler for the library USB Connection event. */
150 void EVENT_USB_Connect(void)
152 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
155 /** Event handler for the library USB Disconnection event. */
156 void EVENT_USB_Disconnect(void)
158 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
161 /** Event handler for the library USB Configuration Changed event. */
162 void EVENT_USB_ConfigurationChanged(void)
164 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
166 if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface
)))
167 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
170 /** Event handler for the library USB Unhandled Control Packet event. */
171 void EVENT_USB_UnhandledControlPacket(void)
173 CDC_Device_ProcessControlPacket(&VirtualSerial_CDC_Interface
);
176 /** Event handler for the CDC Class driver Line Encoding Changed event.
178 * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
180 void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t
* const CDCInterfaceInfo
)
182 uint8_t ConfigMask
= 0;
184 switch (CDCInterfaceInfo
->State
.LineEncoding
.ParityType
)
187 ConfigMask
= ((1 << UPM11
) | (1 << UPM10
));
189 case CDC_PARITY_Even
:
190 ConfigMask
= (1 << UPM11
);
194 if (CDCInterfaceInfo
->State
.LineEncoding
.CharFormat
== CDC_LINEENCODING_TwoStopBits
)
195 ConfigMask
|= (1 << USBS1
);
197 switch (CDCInterfaceInfo
->State
.LineEncoding
.DataBits
)
200 ConfigMask
|= (1 << UCSZ10
);
203 ConfigMask
|= (1 << UCSZ11
);
206 ConfigMask
|= ((1 << UCSZ11
) | (1 << UCSZ10
));
210 UCSR1A
= (1 << U2X1
);
211 UCSR1B
= ((1 << TXEN1
) | (1 << RXEN1
));
213 UBRR1
= SERIAL_2X_UBBRVAL((uint16_t)CDCInterfaceInfo
->State
.LineEncoding
.BaudRateBPS
);
216 /** Event handler for the CDC Class driver Host-to-Device Line Encoding Changed event.
218 * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
220 void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t
* const CDCInterfaceInfo
)
222 /* Check if the DTR line has been asserted - if so, start the target AVR's reset pulse */
223 if (CDCInterfaceInfo
->State
.ControlLineStates
.HostToDevice
& CDC_CONTROL_LINE_OUT_DTR
)
225 AVR_RESET_LINE_DDR
|= AVR_RESET_LINE_MASK
;
226 AVR_RESET_LINE_PORT
&= ~AVR_RESET_LINE_MASK
;
228 ResetPulseMSRemaining
= AVR_RESET_PULSE_MS
;