Add demos/bootloaders/projects overview to the manual.
[pub/USBasp.git] / Bootloaders / TeensyHID / TeensyHID.c
index e81c462..12d5c0d 100644 (file)
@@ -49,6 +49,27 @@ bool RunBootloader = true;
  */\r
 int main(void)\r
 {\r
+       /* Setup hardware required for the bootloader */\r
+       SetupHardware();\r
+       \r
+       while (RunBootloader)\r
+         USB_USBTask();\r
+       \r
+       /* Reset all configured hardware to their default states for the user app */\r
+       ResetHardware();\r
+\r
+       /* Wait 100ms to give the host time to register the disconnection */\r
+       _delay_ms(100);\r
+\r
+       /* Enable the watchdog and force a timeout to reset the AVR */\r
+       wdt_enable(WDTO_250MS);\r
+                                       \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
@@ -62,26 +83,19 @@ int main(void)
 \r
        /* Initialize USB subsystem */\r
        USB_Init();\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
+}\r
 \r
-       /* Enable the watchdog and force a timeout to reset the AVR */\r
-       wdt_enable(WDTO_250MS);\r
-                                       \r
-       for (;;);\r
+/** Resets all configured hardware required for the bootloader back to their original states. */\r
+void ResetHardware(void)\r
+{\r
+       /* Shut down the USB subsystem */\r
+       USB_ShutDown();\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_ConfigurationChanged(void)\r
 {\r
        /* Setup HID Report Endpoint */\r
        Endpoint_ConfigureEndpoint(HID_EPNUM, EP_TYPE_INTERRUPT,\r
@@ -93,13 +107,13 @@ EVENT_HANDLER(USB_ConfigurationChanged)
  *  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_UnhandledControlPacket(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_ClearSETUP();\r
                                \r