Make CDC class bootloader hard-reset the AVR when exited instead of a soft-reset...
[pub/USBasp.git] / Bootloaders / TeensyHID / TeensyHID.c
index b93be6d..ff28927 100644 (file)
@@ -105,18 +105,24 @@ void EVENT_USB_Device_UnhandledControlRequest(void)
                        /* Wait until the command has been sent by the host */\r
                        while (!(Endpoint_IsOUTReceived()));\r
                \r
-                       /* Read in the write destination address */\r
-                       uint16_t PageAddress = Endpoint_Read_Word_LE();\r
+                       /* Read in the write destination index */\r
+                       uint16_t PageIndex = Endpoint_Read_Word_LE();\r
                        \r
                        /* Check if the command is a program page command, or a start application command */\r
-                       if (PageAddress == TEENSY_STARTAPPLICATION)\r
+                       if (PageIndex == TEENSY_STARTAPPLICATION)\r
                        {\r
                                RunBootloader = false;\r
                        }\r
                        else\r
                        {\r
+                               #if (SPM_PAGESIZE == 128)\r
+                               uint16_t PageByteAddress = PageIndex;\r
+                               #else\r
+                               uint32_t PageByteAddress = ((uint32_t)PageIndex << 8);\r
+                               #endif\r
+                       \r
                                /* Erase the given FLASH page, ready to be programmed */\r
-                               boot_page_erase(PageAddress);\r
+                               boot_page_erase(PageByteAddress);\r
                                boot_spm_busy_wait();\r
                                \r
                                /* Write each of the FLASH page's bytes in sequence */\r
@@ -134,15 +140,12 @@ void EVENT_USB_Device_UnhandledControlRequest(void)
                                        }\r
 \r
                                        /* Write the next data word to the FLASH page */\r
-                                       boot_page_fill(PageAddress + PageByte, Endpoint_Read_Word_LE());\r
+                                       boot_page_fill(PageByteAddress + PageByte, Endpoint_Read_Word_LE());\r
                                }\r
 \r
                                /* Write the filled FLASH page to memory */\r
-                               boot_page_write(PageAddress);\r
+                               boot_page_write(PageByteAddress);\r
                                boot_spm_busy_wait();\r
-\r
-                               /* Re-enable RWW section */\r
-                               boot_rww_enable();\r
                        }\r
 \r
                        Endpoint_ClearOUT();\r