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 TeensyHID bootloader. This file contains the complete bootloader logic.
36 #define INCLUDE_FROM_TEENSYHID_C
37 #include "TeensyHID.h"
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.
44 bool RunBootloader
= true;
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.
52 /* Disable watchdog if enabled by bootloader/fuses */
53 MCUSR
&= ~(1 << WDRF
);
56 /* Disable clock division */
57 clock_prescale_set(clock_div_1
);
59 /* Relocate the interrupt vector table to the bootloader section */
63 /* Initialize USB subsystem */
69 /* Shut down the USB interface, so that the host will register the disconnection */
72 /* Wait 100ms to give the host time to register the disconnection */
75 /* Enable the watchdog and force a timeout to reset the AVR */
76 wdt_enable(WDTO_250MS
);
81 /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready
82 * to relay data to and from the attached USB host.
84 EVENT_HANDLER(USB_ConfigurationChanged
)
86 /* Setup HID Report Endpoint */
87 Endpoint_ConfigureEndpoint(HID_EPNUM
, EP_TYPE_INTERRUPT
,
88 ENDPOINT_DIR_IN
, HID_EPSIZE
,
89 ENDPOINT_BANK_SINGLE
);
92 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
93 * control requests that are not handled internally by the USB library (including the HID commands, which are
94 * all issued via the control endpoint), so that they can be handled appropriately for the application.
96 EVENT_HANDLER(USB_UnhandledControlPacket
)
98 /* Handle HID Class specific requests */
102 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
104 Endpoint_ClearSETUP();
106 /* Wait until the command (report) has been sent by the host */
107 while (!(Endpoint_IsOUTReceived()));
109 /* Read in the write destination address */
110 uint16_t PageAddress
= Endpoint_Read_Word_LE();
112 /* Check if the command is a program page command, or a start application command */
113 if (PageAddress
== TEENSY_STARTAPPLICATION
)
115 RunBootloader
= false;
119 /* Erase the given FLASH page, ready to be programmed */
120 boot_page_erase(PageAddress
);
121 boot_spm_busy_wait();
123 /* Write each of the FLASH page's bytes in sequence */
124 for (uint8_t PageByte
= 0; PageByte
< 128; PageByte
+= 2)
126 /* Check if endpoint is empty - if so clear it and wait until ready for next packet */
127 if (!(Endpoint_BytesInEndpoint()))
130 while (!(Endpoint_IsOUTReceived()));
133 /* Write the next data word to the FLASH page */
134 boot_page_fill(PageAddress
+ PageByte
, Endpoint_Read_Word_LE());
137 /* Write the filled FLASH page to memory */
138 boot_page_write(PageAddress
);
139 boot_spm_busy_wait();
141 /* Re-enable RWW section */
147 /* Acknowledge status stage */
148 while (!(Endpoint_IsINReady()));