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