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