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 AVRISP project. This file contains the main tasks of
34 * the project and is responsible for the initial application hardware configuration.
39 /** Main program entry point. This routine contains the overall program flow, including initial
40 * setup of all components and the main program loop.
46 V2Params_LoadNonVolatileParamValues();
48 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
52 Process_AVRISP_Commands();
54 V2Params_UpdateParamValues();
60 /** Configures the board hardware and chip peripherals for the demo's functionality. */
61 void SetupHardware(void)
63 /* Disable watchdog if enabled by bootloader/fuses */
64 MCUSR
&= ~(1 << WDRF
);
67 /* Disable clock division */
68 clock_prescale_set(clock_div_1
);
70 /* Hardware Initialization */
75 /* Initialize the ADC converter for VTARGET level detection on supported AVR models */
76 ADC_Init(ADC_FREE_RUNNING
| ADC_PRESCALE_128
);
77 ADC_SetupChannel(VTARGET_ADC_CHANNEL
);
78 ADC_StartReading(VTARGET_ADC_CHANNEL
| ADC_RIGHT_ADJUSTED
| ADC_REFERENCE_AVCC
);
81 /* Millisecond timer initialization for timeouts and delays */
82 OCR0A
= ((F_CPU
/ 64) / 1000);
83 TCCR0A
= (1 << WGM01
);
84 TCCR0B
= ((1 << CS01
) | (1 << CS00
));
87 /** Event handler for the library USB Connection event. */
88 void EVENT_USB_Device_Connect(void)
90 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
93 /** Event handler for the library USB Disconnection event. */
94 void EVENT_USB_Device_Disconnect(void)
96 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
99 /** Event handler for the library USB Configuration Changed event. */
100 void EVENT_USB_Device_ConfigurationChanged(void)
102 /* Indicate USB connected and ready */
103 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
105 /* Setup AVRISP data Endpoints */
106 if (!(Endpoint_ConfigureEndpoint(AVRISP_DATA_EPNUM
, EP_TYPE_BULK
,
107 ENDPOINT_DIR_OUT
, AVRISP_DATA_EPSIZE
,
108 ENDPOINT_BANK_SINGLE
)))
110 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
114 /** Processes incoming V2 Protocol commands from the host, returning a response when required. */
115 void Process_AVRISP_Commands(void)
117 /* Device must be connected and configured for the task to run */
118 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
121 Endpoint_SelectEndpoint(AVRISP_DATA_EPNUM
);
123 /* Check to see if a V2 Protocol command has been received */
124 if (Endpoint_IsOUTReceived())
126 /* Pass off processing of the V2 Protocol command to the V2 Protocol handler */
127 V2Protocol_ProcessCommand();