NicoHood / HoodLoader2

16u2 Bootloader to reprogram 16u2 + 328/2560 with Arduino IDE
http://www.nicohood.de
734 stars 186 forks source link

`USE_RAM_DESCRIPTORS` checks incorrectly calculate RAM usage #135

Open hraftery opened 9 months ago

hraftery commented 9 months ago

In LUFAConfig.h, USE_RAM_DESCRIPTORS is set if (RAMEND - RAMSTART) >= 512. Assumedly this is to free up Flash by putting the USB Descriptor structs in RAM if the MCU has at least 512 bytes of SRAM.

But RAMEND is the last address of RAM, so RAMEND - RAMSTART returns one less than the RAM size. For example, for the atmega16u2, RAMEND is defined as (RAMSTART + RAMSIZE - 1) where RAMSIZE is 512.

The USB Descriptors seem to occupy 138 bytes, so assumedly would fit in the 16u2's 512 bytes of SRAM. Alas, there's a second issue - HoodLoader2 reserves the first 386 bytes for the rx/tx buffers! So the check fails anyway, because 386 + 138 > 512.

So I think the fix should be to compensate for RAM_OFFSET in the LUFAConfig check, and maybe for the size of the descriptors if we can get that accurate, but I'm not quite sure where responsibility lies...