Switch to hardware delays and timeouts via a hardware timer in the V2 Protocol handler.
[pub/USBasp.git] / Projects / Incomplete / AVRISP / AVRISP.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 AVRISP project. This file contains the main tasks of
34 * the project and is responsible for the initial application hardware configuration.
35 */
36
37 // TODO: Add reversed target connector checks
38 // TODO: Add in software SPI for lower programming speeds below 125KHz
39 // TODO: Add in VTARGET detection
40 // TODO: Add in software SPI for lower programming speeds
41
42 #include "AVRISP.h"
43
44 /** Main program entry point. This routine contains the overall program flow, including initial
45 * setup of all components and the main program loop.
46 */
47 int main(void)
48 {
49 SetupHardware();
50
51 V2Params_LoadEEPROMParamValues();
52
53 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
54
55 for (;;)
56 {
57 Process_AVRISP_Commands();
58
59 USB_USBTask();
60 }
61 }
62
63 /** Configures the board hardware and chip peripherals for the demo's functionality. */
64 void SetupHardware(void)
65 {
66 /* Disable watchdog if enabled by bootloader/fuses */
67 MCUSR &= ~(1 << WDRF);
68 wdt_disable();
69
70 /* Disable clock division */
71 clock_prescale_set(clock_div_1);
72
73 /* Hardware Initialization */
74 LEDs_Init();
75 USB_Init();
76
77 /* Millisecond timer initialization for timeout checking */
78 OCR0A = ((F_CPU / 64) / 1000);
79 TCCR0A = (1 << WGM01);
80 TCCR0B = ((1 << CS01) | (1 << CS00));
81 }
82
83 /** Event handler for the library USB Connection event. */
84 void EVENT_USB_Device_Connect(void)
85 {
86 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
87 }
88
89 /** Event handler for the library USB Disconnection event. */
90 void EVENT_USB_Device_Disconnect(void)
91 {
92 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
93 }
94
95 /** Event handler for the library USB Configuration Changed event. */
96 void EVENT_USB_Device_ConfigurationChanged(void)
97 {
98 /* Indicate USB connected and ready */
99 LEDs_SetAllLEDs(LEDMASK_USB_READY);
100
101 /* Setup AVRISP data Endpoints */
102 if (!(Endpoint_ConfigureEndpoint(AVRISP_DATA_EPNUM, EP_TYPE_BULK,
103 ENDPOINT_DIR_OUT, AVRISP_DATA_EPSIZE,
104 ENDPOINT_BANK_SINGLE)))
105 {
106 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
107 }
108 }
109
110 /** Processes incomming V2 Protocol commands from the host, returning a response when required. */
111 void Process_AVRISP_Commands(void)
112 {
113 /* Device must be connected and configured for the task to run */
114 if (USB_DeviceState != DEVICE_STATE_Configured)
115 return;
116
117 Endpoint_SelectEndpoint(AVRISP_DATA_EPNUM);
118
119 /* Check to see if a V2 Protocol command has been received - if not, abort */
120 if (!(Endpoint_IsOUTReceived()))
121 return;
122
123 /* Pass off processing of the V2 Protocol command to the V2 Protocol handler */
124 V2Protocol_ProcessCommand();
125 }
126