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