Task switch on 8051 - stack-copy overhead?

wigginsbiter wrote on Thursday, March 20, 2008:

I am looking at FreeRTOS for an 8051 device. I was wondering how the task switch handles the stack. As I understand things the 8051 stack is limited to a single 256 byte memory space and so on a task switch everything has to copied out to / in from XDATA. That does not strike me as optimal (but hardly the OS’s fault) and so are there some clever solutions FreeRTOS uses to get around it? Maybe it requires a modified 8051 that can map in XDATA to the stack space or something? Any advice and insight would be greatly appreciated.


woops_ wrote on Thursday, March 20, 2008:

That is how the 8051 works its fixed, and by todays standard its not good. It was never designed to be use in larger applications so it is a shame the otherwise very nice Sil Labs parts use 8051.

wigginsbiter wrote on Friday, March 21, 2008:

Thanks for the response. I kinda expected that would be the case. I guess that sometimes we forget that just adding memory and running at faster clock speeds doesn’t solve everything. It doesn’t make the 8051 a bad processor though - just defines a limit of what it can/should be used for.

adarkar9 wrote on Friday, March 21, 2008:

Some of the more exotic devices have 16-bit stack pointers, but most 8051 variants are locked in to an 8-bit stack pointer.  Not much an OS can do about that.

The 8051 FreeRTOS port copies the used portion of the internal stack to/from XDATA on a context switch.  The stack copy adds a significant amount of overhead to a context switch.  If you expect your application to use most of the processing power of the controller, you probably ought to look elsewhere.  However, if you can handle the overhead, FreeRTOS on an 8051 works pretty nicely.

I use FreeRTOS on several SiLabs 8051 parts.  I run the system clock at 100 MHz with a FreeRTOS tick rate of 1 kHz.  The average context switch time is 42 us (worst case of 62 us with a full stack).  This leaves a task with about 950 us of execution time (~95,000 cycles).  In other words, my configuration has a context switch overhead of about 5%.  My average processor utilization is under 50%, so I can afford the overhead.

Good Luck!

wigginsbiter wrote on Monday, April 14, 2008:

Thanks for your answers - I have a clear picture of the issues now.