Explicitly cast FetchNextCommandByte to u32 to ensure correct handling
authorPaul R <exp@users.noreply.github.com>
Mon, 22 Mar 2021 12:19:25 +0000 (12:19 +0000)
committerPaul R <exp@users.noreply.github.com>
Mon, 22 Mar 2021 12:19:25 +0000 (12:19 +0000)
Without the explicit cast, avr-gcc generates incorrect asm which sets
only the lower bytes of the u32, leading the upper bytes to be set to
0xFFFF when the input value has MSB set. This results in flashing past
32k bytes failing. Explicit casting corrects this behaviour in testing.

Bootloaders/CDC/BootloaderCDC.c

index 2c8748a..6a0740a 100644 (file)
@@ -475,8 +475,8 @@ static void CDC_Task(void)
        else if (Command == AVR109_COMMAND_SetCurrentAddress)
        {
                /* Set the current address to that given by the host (translate 16-bit word address to byte address) */
        else if (Command == AVR109_COMMAND_SetCurrentAddress)
        {
                /* Set the current address to that given by the host (translate 16-bit word address to byte address) */
-               CurrAddress   = (FetchNextCommandByte() << 9);
-               CurrAddress  |= (FetchNextCommandByte() << 1);
+               CurrAddress   = ((uint32_t)FetchNextCommandByte() << 9);
+               CurrAddress  |= ((uint32_t)FetchNextCommandByte() << 1);
 
                /* Send confirmation byte back to the host */
                WriteNextResponseByte('\r');
 
                /* Send confirmation byte back to the host */
                WriteNextResponseByte('\r');