* Main source file for the TeensyHID bootloader. This file contains the complete bootloader logic.\r
*/\r
\r
-#define INCLUDE_FROM_TEENSYHID_C\r
#include "TeensyHID.h"\r
\r
/* Global Variables: */\r
*/\r
int main(void)\r
{\r
- /* Disable watchdog if enabled by bootloader/fuses */\r
- MCUSR &= ~(1 << WDRF);\r
- wdt_disable();\r
-\r
- /* Disable Clock Division */\r
- SetSystemClockPrescaler(0);\r
-\r
- /* Relocate the interrupt vector table to the bootloader section */\r
- MCUCR = (1 << IVCE);\r
- MCUCR = (1 << IVSEL);\r
-\r
- /* Initialize USB subsystem */\r
- USB_Init();\r
+ /* Setup hardware required for the bootloader */\r
+ SetupHardware();\r
\r
while (RunBootloader)\r
USB_USBTask();\r
- \r
- /* Shut down the USB interface, so that the host will register the disconnection */\r
- USB_ShutDown();\r
\r
/* Wait 100ms to give the host time to register the disconnection */\r
_delay_ms(100);\r
for (;;);\r
}\r
\r
+/** Configures all hardware required for the bootloader. */\r
+void SetupHardware(void)\r
+{\r
+ /* Disable watchdog if enabled by bootloader/fuses */\r
+ MCUSR &= ~(1 << WDRF);\r
+ wdt_disable();\r
+\r
+ /* Disable clock division */\r
+ clock_prescale_set(clock_div_1);\r
+\r
+ /* Relocate the interrupt vector table to the bootloader section */\r
+ MCUCR = (1 << IVCE);\r
+ MCUCR = (1 << IVSEL);\r
+\r
+ /* Initialize USB subsystem */\r
+ USB_Init();\r
+}\r
+\r
/** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready\r
* to relay data to and from the attached USB host.\r
*/\r
-EVENT_HANDLER(USB_ConfigurationChanged)\r
+void EVENT_USB_Device_ConfigurationChanged(void)\r
{\r
/* Setup HID Report Endpoint */\r
Endpoint_ConfigureEndpoint(HID_EPNUM, EP_TYPE_INTERRUPT,\r
ENDPOINT_BANK_SINGLE);\r
}\r
\r
-/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific\r
+/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific\r
* control requests that are not handled internally by the USB library (including the HID commands, which are\r
* all issued via the control endpoint), so that they can be handled appropriately for the application.\r
*/\r
-EVENT_HANDLER(USB_UnhandledControlPacket)\r
+void EVENT_USB_Device_UnhandledControlRequest(void)\r
{\r
/* Handle HID Class specific requests */\r
- switch (bRequest)\r
+ switch (USB_ControlRequest.bRequest)\r
{\r
case REQ_SetReport:\r
- if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
+ if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
{\r
- Endpoint_ClearSetupReceived();\r
+ Endpoint_ClearSETUP();\r
\r
/* Wait until the command (report) has been sent by the host */\r
- while (!(Endpoint_IsSetupOUTReceived()));\r
-\r
+ while (!(Endpoint_IsOUTReceived()));\r
+ \r
/* Read in the write destination address */\r
uint16_t PageAddress = Endpoint_Read_Word_LE();\r
\r
/* Check if endpoint is empty - if so clear it and wait until ready for next packet */\r
if (!(Endpoint_BytesInEndpoint()))\r
{\r
- Endpoint_ClearSetupOUT();\r
- while (!(Endpoint_IsSetupOUTReceived()));\r
+ Endpoint_ClearOUT();\r
+ while (!(Endpoint_IsOUTReceived()));\r
}\r
\r
/* Write the next data word to the FLASH page */\r
boot_rww_enable();\r
}\r
\r
- Endpoint_ClearSetupOUT();\r
+ Endpoint_ClearOUT();\r
\r
- /* Wait until the host is ready to receive the request confirmation */\r
- while (!(Endpoint_IsSetupINReady()));\r
- \r
- /* Handshake the request by sending an empty IN packet */\r
- Endpoint_ClearSetupIN();\r
+ Endpoint_ClearStatusStage();\r
}\r
\r
break;\r