Corrected CDC class bootloader to fix a few bugs, changed address counter to store...
[pub/USBasp.git] / Bootloaders / CDC / BootloaderCDC.c
index 0add977..a1394bd 100644 (file)
@@ -52,7 +52,7 @@ CDC_Line_Coding_t LineCoding = { BaudRateBPS: 9600,
  *  and is used when reading or writing to the AVRs memory (either FLASH or EEPROM depending on the issued\r
  *  command.)\r
  */\r
-uint16_t CurrAddress;\r
+uint32_t CurrAddress;\r
 \r
 /** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run\r
  *  via a soft reset. When cleared, the bootloader will abort, the USB interface will shut down and the application\r
@@ -223,101 +223,101 @@ static void ReadWriteMemoryBlock(const uint8_t Command)
        \r
        MemoryType =  FetchNextCommandByte();\r
 \r
-       if ((MemoryType == 'E') || (MemoryType == 'F'))\r
+       if ((MemoryType != 'E') && (MemoryType != 'F'))\r
        {\r
-               /* Check if command is to read memory */\r
-               if (Command == 'g')\r
-               {\r
-                       /* Re-enable RWW section */\r
-                       boot_rww_enable();\r
+               /* Send error byte back to the host */\r
+               WriteNextResponseByte('?');\r
+               \r
+               return;\r
+       }\r
 \r
-                       while (BlockSize--)\r
+       /* Check if command is to read memory */\r
+       if (Command == 'g')\r
+       {\r
+               /* Re-enable RWW section */\r
+               boot_rww_enable();\r
+\r
+               while (BlockSize--)\r
+               {\r
+                       if (MemoryType == 'E')\r
                        {\r
-                               if (MemoryType == 'E')\r
-                               {\r
-                                       /* Read the next EEPROM byte into the endpoint */\r
-                                       WriteNextResponseByte(eeprom_read_byte((uint8_t*)CurrAddress));\r
+                               /* Read the next EEPROM byte into the endpoint */\r
+                               WriteNextResponseByte(eeprom_read_byte((uint8_t*)(uint16_t)(CurrAddress >> 1)));\r
 \r
-                                       /* Increment the address counter after use */\r
-                                       CurrAddress++;\r
-                               }\r
-                               else\r
-                               {\r
-                                       /* Read the next FLASH byte from the current FLASH page */\r
-                                       #if defined(RAMPZ)\r
-                                       WriteNextResponseByte(pgm_read_byte_far(((uint32_t)CurrAddress << 1) + HighByte));\r
-                                       #else\r
-                                       WriteNextResponseByte(pgm_read_byte((CurrAddress << 1) + HighByte));                                    \r
-                                       #endif\r
-                                       \r
-                                       /* If both bytes in current word have been read, increment the address counter */\r
-                                       if (HighByte)\r
-                                         CurrAddress++;\r
-                                       \r
-                                       HighByte ^= 1;\r
-                               }\r
+                               /* Increment the address counter after use */\r
+                               CurrAddress += 2;\r
+                       }\r
+                       else\r
+                       {\r
+                               /* Read the next FLASH byte from the current FLASH page */\r
+                               #if defined(RAMPZ)\r
+                               WriteNextResponseByte(pgm_read_byte_far(CurrAddress | HighByte));\r
+                               #else\r
+                               WriteNextResponseByte(pgm_read_byte(CurrAddress | HighByte));                                   \r
+                               #endif\r
+                               \r
+                               /* If both bytes in current word have been read, increment the address counter */\r
+                               if (HighByte)\r
+                                 CurrAddress += 2;\r
+                               \r
+                               HighByte = !HighByte;\r
                        }\r
                }\r
-               else\r
+       }\r
+       else\r
+       {\r
+               uint32_t PageStartAddress = CurrAddress;\r
+\r
+               if (MemoryType == 'F')\r
+               {\r
+                       boot_page_erase(PageStartAddress);\r
+                       boot_spm_busy_wait();\r
+               }\r
+               \r
+               while (BlockSize--)\r
                {\r
-                       uint32_t PageStartAddress = ((uint32_t)CurrAddress << 1);\r
-       \r
                        if (MemoryType == 'F')\r
-                       {\r
-                               boot_page_erase(PageStartAddress);\r
-                               boot_spm_busy_wait();\r
-                       }\r
-                       \r
-                       while (BlockSize--)\r
-                       {\r
-                               if (MemoryType == 'E')\r
+                       {       \r
+                               /* If both bytes in current word have been written, increment the address counter */\r
+                               if (HighByte)\r
                                {\r
-                                       /* Write the next EEPROM byte from the endpoint */\r
-                                       eeprom_write_byte((uint8_t*)CurrAddress, FetchNextCommandByte());                                       \r
+                                       /* Write the next FLASH word to the current FLASH page */\r
+                                       boot_page_fill(CurrAddress, ((FetchNextCommandByte() << 8) | LowByte));\r
 \r
                                        /* Increment the address counter after use */\r
-                                       CurrAddress++;\r
+                                       CurrAddress += 2;\r
+\r
+                                       HighByte = false;\r
                                }\r
                                else\r
-                               {       \r
-                                       /* If both bytes in current word have been written, increment the address counter */\r
-                                       if (HighByte)\r
-                                       {\r
-                                               /* Write the next FLASH word to the current FLASH page */\r
-                                               boot_page_fill(((uint32_t)CurrAddress << 1), ((FetchNextCommandByte() << 8) | LowByte));\r
-\r
-                                               HighByte = false;\r
-                                               \r
-                                               /* Increment the address counter after use */\r
-                                               CurrAddress++;\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               LowByte = FetchNextCommandByte();\r
-                                       \r
-                                               HighByte = true;\r
-                                       }\r
+                               {\r
+                                       LowByte = FetchNextCommandByte();\r
+                               \r
+                                       HighByte = true;\r
                                }\r
                        }\r
-\r
-                       /* If in FLASH programming mode, commit the page after writing */\r
-                       if (MemoryType == 'F')\r
+                       else\r
                        {\r
-                               /* Commit the flash page to memory */\r
-                               boot_page_write(PageStartAddress);\r
-                               \r
-                               /* Wait until write operation has completed */\r
-                               boot_spm_busy_wait();\r
+                               /* Write the next EEPROM byte from the endpoint */\r
+                               eeprom_write_byte((uint8_t*)(uint16_t)(CurrAddress >> 1), FetchNextCommandByte());                                      \r
+\r
+                               /* Increment the address counter after use */\r
+                               CurrAddress += 2;\r
                        }\r
-               \r
-                       /* Send response byte back to the host */\r
-                       WriteNextResponseByte('\r');            \r
                }\r
-       }\r
-       else\r
-       {\r
-               /* Send error byte back to the host */\r
-               WriteNextResponseByte('?');\r
+\r
+               /* If in FLASH programming mode, commit the page after writing */\r
+               if (MemoryType == 'F')\r
+               {\r
+                       /* Commit the flash page to memory */\r
+                       boot_page_write(PageStartAddress);\r
+                       \r
+                       /* Wait until write operation has completed */\r
+                       boot_spm_busy_wait();\r
+               }\r
+       \r
+               /* Send response byte back to the host */\r
+               WriteNextResponseByte('\r');            \r
        }\r
 }\r
 \r
@@ -402,8 +402,8 @@ TASK(CDC_Task)
                else if (Command == 'A')\r
                {\r
                        /* Set the current address to that given by the host */\r
-                       CurrAddress  = (FetchNextCommandByte() << 8);\r
-                       CurrAddress |=  FetchNextCommandByte();\r
+                       CurrAddress   = (FetchNextCommandByte() << 9);\r
+                       CurrAddress  |= (FetchNextCommandByte() << 1);\r
 \r
                        /* Send confirmation byte back to the host */\r
                        WriteNextResponseByte('\r');\r
@@ -426,9 +426,9 @@ TASK(CDC_Task)
                }\r
                else if (Command == 's')\r
                {\r
-                       WriteNextResponseByte(boot_signature_byte_get(4));\r
-                       WriteNextResponseByte(boot_signature_byte_get(2));\r
-                       WriteNextResponseByte(boot_signature_byte_get(0));              \r
+                       WriteNextResponseByte(SIGNATURE_0);\r
+                       WriteNextResponseByte(SIGNATURE_1);\r
+                       WriteNextResponseByte(SIGNATURE_2);             \r
                }\r
                else if (Command == 'b')\r
                {\r
@@ -478,24 +478,29 @@ TASK(CDC_Task)
                {\r
                        WriteNextResponseByte(boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS));         \r
                }\r
-               else if ((Command == 'C') || (Command == 'c'))\r
-               {\r
-                       if (Command == 'c')\r
-                       {\r
-                               /* Increment the address if the second byte is being written */\r
-                               CurrAddress++;\r
-                       }\r
-                       \r
+               else if (Command == 'C')\r
+               {                       \r
                        /* Write the high byte to the current flash page */\r
-                       boot_page_fill(((uint32_t)CurrAddress << 1), FetchNextCommandByte());\r
+                       boot_page_fill(CurrAddress, FetchNextCommandByte());\r
+\r
+                       /* Send confirmation byte back to the host */\r
+                       WriteNextResponseByte('\r');            \r
+               }\r
+               else if (Command == 'c')\r
+               {                       \r
+                       /* Write the low byte to the current flash page */\r
+                       boot_page_fill(CurrAddress | 1, FetchNextCommandByte());\r
                        \r
+                       /* Increment the address */\r
+                       CurrAddress += 2;\r
+\r
                        /* Send confirmation byte back to the host */\r
                        WriteNextResponseByte('\r');            \r
                }\r
                else if (Command == 'm')\r
                {\r
                        /* Commit the flash page to memory */\r
-                       boot_page_write((uint32_t)CurrAddress << 1);\r
+                       boot_page_write(CurrAddress);\r
                        \r
                        /* Wait until write operation has completed */\r
                        boot_spm_busy_wait();\r
@@ -511,9 +516,9 @@ TASK(CDC_Task)
                else if (Command == 'R')\r
                {\r
                        #if defined(RAMPZ)\r
-                       uint16_t ProgramWord = pgm_read_word_far(((uint32_t)CurrAddress << 1));\r
+                       uint16_t ProgramWord = pgm_read_word_far(CurrAddress);\r
                        #else\r
-                       uint16_t ProgramWord = pgm_read_word(CurrAddress << 1);                 \r
+                       uint16_t ProgramWord = pgm_read_word(CurrAddress);                      \r
                        #endif\r
                        \r
                        WriteNextResponseByte(ProgramWord >> 8);\r
@@ -522,10 +527,10 @@ TASK(CDC_Task)
                else if (Command == 'D')\r
                {\r
                        /* Read the byte from the endpoint and write it to the EEPROM */\r
-                       eeprom_write_byte((uint8_t*)CurrAddress, FetchNextCommandByte());\r
+                       eeprom_write_byte((uint8_t*)(uint16_t)(CurrAddress >> 1), FetchNextCommandByte());\r
                        \r
                        /* Increment the address after use */                   \r
-                       CurrAddress++;\r
+                       CurrAddress += 2;\r
        \r
                        /* Send confirmation byte back to the host */\r
                        WriteNextResponseByte('\r');            \r
@@ -533,10 +538,10 @@ TASK(CDC_Task)
                else if (Command == 'd')\r
                {\r
                        /* Read the EEPROM byte and write it to the endpoint */\r
-                       WriteNextResponseByte(eeprom_read_byte((uint8_t*)CurrAddress));\r
+                       WriteNextResponseByte(eeprom_read_byte((uint8_t*)(uint16_t)(CurrAddress >> 1)));\r
 \r
                        /* Increment the address after use */\r
-                       CurrAddress++;\r
+                       CurrAddress += 2;\r
                }\r
                else if (Command == 27)\r
                {\r