Electronic – How to reduce the size of an AVR ethernet bootloader

avrbootloader

I am writing an ethernet capable bootloader for an AVR ATmega328 MCU. I am currently running over the 4k size limit and need some design advice.

Bootloader requirements:

  • Needs to be able to update the application via a simple UDP based messaging protocol (similar to TFTP).
  • As the device operates in a variety of LANs, it should support DHCP.

With my current code, I need about 4k for the Ethernet driver, DHCP, ARP and UDP support.
Plus 1.5k for the actual messaging and app programming.

Compiler optimizations are turned on.

My current considerations to proceed are:

  1. The code is not particularily optimized, so I guess I could save some bytes by following AVR035. I doubt I can save enough code size going this route though.
  2. I could simply extend the bootloader area by reserving 2k from the top app memory area for the bootloader. I need to be extra careful then to not overwrite that area when programming though. And, I would end up with a large, complex bootloader which probably has bugs, which I cannot update in the field.
  3. I could move some functions from the bootloader to the app. DHCP could be a good candidate: The app obtains a IP address via DHCP and stores it into EEPROM. The bootloader then can simply use the stored IP address and does not need to do DHCP itself. This could be dangerous though if the app fails e.g. by software bug or random corruption.

Which route would you suggest? Are there other ideas I should consider?

Best Answer

I managed to reduce the size of the bootloader to around 3k. Here is what I did, maybe it helps someone with a similar task:

I removed DHCP and ARP support from the bootloader. I make the assumption that I can always reach the device via UDP broadcast, so I don't need it to obtain an IP address when running the bootloader.

I also did a lot of code tweaking, which helped reduce the size some more. The removal of DHCP/ARP was the biggest chunk, however.