Added new Benito project, a simple modified USB-to-Serial bridge for the programming...
[pub/USBasp.git] / Projects / Benito / Benito.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
35 */
36
37 #include "Benito.h"
38
39 volatile uint8_t ResetPulseMSRemaining = 0;
40 volatile uint8_t TxPulseMSRemaining = 0;
41 volatile uint8_t RxPulseMSRemaining = 0;
42
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.
46 */
47 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
48 {
49 .Config =
50 {
51 .ControlInterfaceNumber = 0,
52
53 .DataINEndpointNumber = CDC_TX_EPNUM,
54 .DataINEndpointSize = CDC_TXRX_EPSIZE,
55
56 .DataOUTEndpointNumber = CDC_RX_EPNUM,
57 .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
58
59 .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
60 .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
61 },
62
63 .State =
64 {
65 // Leave all state values to their defaults
66 }
67 };
68
69 /** Main program entry point. This routine contains the overall program flow, including initial
70 * setup of all components and the main program loop.
71 */
72 int main(void)
73 {
74 SetupHardware();
75
76 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
77
78 for (;;)
79 {
80 /* Echo bytes from the host to the target via the hardware USART */
81 if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
82 {
83 Serial_TxByte(CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));
84
85 LEDs_TurnOnLEDs(LEDMASK_TX);
86 TxPulseMSRemaining = TX_RX_LED_PULSE_MS;
87 }
88
89 /* Echo bytes from the target to the host via the virtual serial port */
90 if (Serial_IsCharReceived())
91 {
92 CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Serial_RxByte());
93
94 LEDs_TurnOnLEDs(LEDMASK_RX);
95 RxPulseMSRemaining = TX_RX_LED_PULSE_MS;
96 }
97
98 /* Check if the millisecond timer has elapsed */
99 if (TIFR0 & (1 << OCF0A))
100 {
101 /* Check if the reset pulse period has elapsed, if so tristate the target reset line */
102 if (ResetPulseMSRemaining && !(--ResetPulseMSRemaining))
103 {
104 AVR_RESET_LINE_PORT &= ~AVR_RESET_LINE_MASK;
105 AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK;
106 }
107
108 /* Turn off TX LED(s) once the TX pulse period has elapsed */
109 if (TxPulseMSRemaining && !(--TxPulseMSRemaining))
110 LEDs_TurnOffLEDs(LEDMASK_TX);
111
112 /* Turn off RX LED(s) once the RX pulse period has elapsed */
113 if (RxPulseMSRemaining && !(--RxPulseMSRemaining))
114 LEDs_TurnOffLEDs(LEDMASK_RX);
115
116 /* Clear the millisecond timer CTC flag (cleared by writing logic one to the register) */
117 TIFR0 |= (1 << OCF0A);
118 }
119
120 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
121 USB_USBTask();
122 }
123 }
124
125 /** Configures the board hardware and chip peripherals for the demo's functionality. */
126 void SetupHardware(void)
127 {
128 /* Disable watchdog if enabled by bootloader/fuses */
129 MCUSR &= ~(1 << WDRF);
130 wdt_disable();
131
132 /* Disable clock division */
133 clock_prescale_set(clock_div_1);
134
135 /* Hardware Initialization */
136 LEDs_Init();
137 USB_Init();
138
139 /* Millisecond Timer Interrupt */
140 OCR0A = (F_CPU / 64 / 1000);
141 TCCR0A = (1 << WGM01);
142 TCCR0B = ((1 << CS01) | (1 << CS00));
143
144 /* Tristate target /RESET Line */
145 AVR_RESET_LINE_PORT &= ~AVR_RESET_LINE_MASK;
146 AVR_RESET_LINE_DDR &= ~AVR_RESET_LINE_MASK;
147 }
148
149 /** Event handler for the library USB Connection event. */
150 void EVENT_USB_Connect(void)
151 {
152 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
153 }
154
155 /** Event handler for the library USB Disconnection event. */
156 void EVENT_USB_Disconnect(void)
157 {
158 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
159 }
160
161 /** Event handler for the library USB Configuration Changed event. */
162 void EVENT_USB_ConfigurationChanged(void)
163 {
164 LEDs_SetAllLEDs(LEDMASK_USB_READY);
165
166 if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))
167 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
168 }
169
170 /** Event handler for the library USB Unhandled Control Packet event. */
171 void EVENT_USB_UnhandledControlPacket(void)
172 {
173 CDC_Device_ProcessControlPacket(&VirtualSerial_CDC_Interface);
174 }
175
176 /** Event handler for the CDC Class driver Line Encoding Changed event.
177 *
178 * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
179 */
180 void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
181 {
182 uint8_t ConfigMask = 0;
183
184 switch (CDCInterfaceInfo->State.LineEncoding.ParityType)
185 {
186 case CDC_PARITY_Odd:
187 ConfigMask = ((1 << UPM11) | (1 << UPM10));
188 break;
189 case CDC_PARITY_Even:
190 ConfigMask = (1 << UPM11);
191 break;
192 }
193
194 if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits)
195 ConfigMask |= (1 << USBS1);
196
197 switch (CDCInterfaceInfo->State.LineEncoding.DataBits)
198 {
199 case 6:
200 ConfigMask |= (1 << UCSZ10);
201 break;
202 case 7:
203 ConfigMask |= (1 << UCSZ11);
204 break;
205 case 8:
206 ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));
207 break;
208 }
209
210 UCSR1A = (1 << U2X1);
211 UCSR1B = ((1 << TXEN1) | (1 << RXEN1));
212 UCSR1C = ConfigMask;
213 UBRR1 = SERIAL_2X_UBBRVAL((uint16_t)CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
214 }
215
216 /** Event handler for the CDC Class driver Host-to-Device Line Encoding Changed event.
217 *
218 * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
219 */
220 void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
221 {
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)
224 {
225 AVR_RESET_LINE_DDR |= AVR_RESET_LINE_MASK;
226 AVR_RESET_LINE_PORT &= ~AVR_RESET_LINE_MASK;
227
228 ResetPulseMSRemaining = AVR_RESET_PULSE_MS;
229 }
230 }