Commit for the 090810 release.
[pub/USBasp.git] / Bootloaders / TeensyHID / TeensyHID.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 TeensyHID bootloader. This file contains the complete bootloader logic.
34 */
35
36 #include "TeensyHID.h"
37
38 /* Global Variables: */
39 /** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run
40 * via a soft reset. When cleared, the bootloader will abort, the USB interface will shut down and the application
41 * started via a forced watchdog reset.
42 */
43 bool RunBootloader = true;
44
45
46 /** Main program entry point. This routine configures the hardware required by the bootloader, then continuously
47 * runs the bootloader processing routine until instructed to soft-exit.
48 */
49 int main(void)
50 {
51 /* Setup hardware required for the bootloader */
52 SetupHardware();
53
54 while (RunBootloader)
55 USB_USBTask();
56
57 /* Wait 100ms to give the host time to register the disconnection */
58 _delay_ms(100);
59
60 /* Enable the watchdog and force a timeout to reset the AVR */
61 wdt_enable(WDTO_250MS);
62
63 for (;;);
64 }
65
66 /** Configures all hardware required for the bootloader. */
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 /* Relocate the interrupt vector table to the bootloader section */
77 MCUCR = (1 << IVCE);
78 MCUCR = (1 << IVSEL);
79
80 /* Initialize USB subsystem */
81 USB_Init();
82 }
83
84 /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready
85 * to relay data to and from the attached USB host.
86 */
87 void EVENT_USB_Device_ConfigurationChanged(void)
88 {
89 /* Setup HID Report Endpoint */
90 Endpoint_ConfigureEndpoint(HID_EPNUM, EP_TYPE_INTERRUPT,
91 ENDPOINT_DIR_IN, HID_EPSIZE,
92 ENDPOINT_BANK_SINGLE);
93 }
94
95 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
96 * control requests that are not handled internally by the USB library (including the HID commands, which are
97 * all issued via the control endpoint), so that they can be handled appropriately for the application.
98 */
99 void EVENT_USB_Device_UnhandledControlRequest(void)
100 {
101 /* Handle HID Class specific requests */
102 switch (USB_ControlRequest.bRequest)
103 {
104 case REQ_SetReport:
105 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
106 {
107 Endpoint_ClearSETUP();
108
109 /* Wait until the command (report) has been sent by the host */
110 while (!(Endpoint_IsOUTReceived()));
111
112 /* Read in the write destination address */
113 uint16_t PageAddress = Endpoint_Read_Word_LE();
114
115 /* Check if the command is a program page command, or a start application command */
116 if (PageAddress == TEENSY_STARTAPPLICATION)
117 {
118 RunBootloader = false;
119 }
120 else
121 {
122 /* Erase the given FLASH page, ready to be programmed */
123 boot_page_erase(PageAddress);
124 boot_spm_busy_wait();
125
126 /* Write each of the FLASH page's bytes in sequence */
127 for (uint8_t PageByte = 0; PageByte < 128; PageByte += 2)
128 {
129 /* Check if endpoint is empty - if so clear it and wait until ready for next packet */
130 if (!(Endpoint_BytesInEndpoint()))
131 {
132 Endpoint_ClearOUT();
133 while (!(Endpoint_IsOUTReceived()));
134 }
135
136 /* Write the next data word to the FLASH page */
137 boot_page_fill(PageAddress + PageByte, Endpoint_Read_Word_LE());
138 }
139
140 /* Write the filled FLASH page to memory */
141 boot_page_write(PageAddress);
142 boot_spm_busy_wait();
143
144 /* Re-enable RWW section */
145 boot_rww_enable();
146 }
147
148 Endpoint_ClearOUT();
149
150 Endpoint_ClearStatusStage();
151 }
152
153 break;
154 }
155 }