Close

Refactoring the code for multiple MCU targets

A project log for 89C52 clock board

A board designed to mate with the cascadable display boards I designed

ken-yapKen Yap 01/25/2024 at 09:470 Comments

As mentioned in #Adventures with a STC89C52 development board  I bought that development kit because it could accept some AVR MCUs that are almost pin-compatible with the STC89C52. This got me thinking of how I could use the couple of ancient AVR chips that I have. Ideally I would like to reuse firmware that I have written and not have to develop from scratch. So as an exercise in learning more about AVR architecture, I decided to see if I could modify the STC89C52 code so that it could be compiled for more than one type of MCU.

My requirements are:

I went in and hacked at it until I had something acceptable. I was lucky in some respects:

Some constants, like those used for time constants for the button UI can be defined in common include files. Other constants, such as the values loaded into MCU registers, are in MCU specific include files.

The overhead is very acceptable, only increasing the binary size by tens of bytes due to some inline code being turned into functions.

I noted that for avr-gcc it's ok for static functions to be defined in .h files because avr-gcc is smart enough to not emit code for the body if the function is not used later in the .c file. In SDCC the functions need to be in a .c file and global rather than static otherwise if in a .h file there will be a copy for every .c file it's included in.

The changes have been pushed to the Github archive.

Discussions