This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Multithread with 8051

Hi there,

Does anyone know if it's possible to rum multiple threads on 8051 mocrocontroller?

Thank you.

  • Certainly. It's just software.

    The 8051 architecture doesn't really lend itself well to preemption, though. It's doable, just as with any processor, but it costs more than you might expect given experience with other processors.

    The compile-time call tree analysis Keil C does becomes far less useful when you can preempt a task at any point in its execution, and thus have to have separate call trees for every task. This tends to increase the amount of space needed for the space, which means you can't fit it into the internal data memory, which means you have to switch to external data for all your temporaries and automatics, which makes all the code bigger and slower. It's not the overhead of a preemptive kernel itself that's a problem, but rather than increase in all the rest of the code.

    Most of these limitations come from the awkwardness of the DPTR and the instruction set, so it's not a matter of finding better tools.

  • The 8051 architecture doesn't really lend itself well to preemption, though.
    No, it's just a awful for that as it is great for what it is intended for

    Most of these limitations come from the awkwardness of the DPTR and the instruction set, so it's not a matter of finding better tools.
    limitations imposed in order to make the stuff it is intended for even better.

    Why are you trying to fit a square peg in a round hole? If you need multitasking (which I doubt) use a processor intended for that such as a pentium.

    Erik

  • Why are you trying to fit a square peg in a round hole? If you need multitasking (which I doubt) use a processor intended for that such as a pentium.
    Perhaps there are cost and time constraints on the project requirements...which is often the case. Assuming this, one doesn't simply opt for a Pentium over a '51 or similar.

    Now, while I'm not a big proponent of preemptive multi-threading in the '51, cooperative multi-threading isn't unreasonable and can be implement fairly efficiently, if done properly.

  • cooperative multi-threading
    is that not just the workloop?

    Erik

  • cooperative multi-threading

    Yes, it's the preemption that's the problem, and that largely because of the secondary effects of the way the stack is (and should be) handled for efficiency's sake.

    See, for example, the Salvo RTOS from Pumpkin.

    http://www.pumpkininc.com/

    Take a close look at the restrictions those OS-looking macros, and you'll see that they're really just calling void (void) functions from the top level -- the workloop, as Erik puts it, but without having to hand-code it all. This allows the compiler to build a call tree and overlay memory usage as usual.

    If you want something preemptive, there's CMX RTX, Keil's RTX, uCOS-II, and others.

  • cooperative multi-threading aka the workloop

    Next, the saucer will be called an "cup spill damage preventer"

    Erik

  • cooperative multi-threading
    is that not just the workloop?


    No, its not just an executive loop. Its an OS that provides functions for relinquishing control back to a task management subsystem, where each "thread"s complete context is saved upon doing so. The functions are effectively $REGUSE fn () ... thus allowing the compiler full optimization across the call.

    Its cooperative in that there is no background preemtion mechanism that switches threads, rather each thread must relinquish control back to the OS on its own accord.

    It avoids all the resource consumption involved with true preemption, but still allows you to write linear threads.

  • Nei San;
    Don't let these guys talk you out of multi-thread system for the 8051. The Keil RTX51 Tiny has run in a chip (old Philips 751)that had only 64 Bytes of RAM and 1K of ROM. The newer RTX51 Tiny 2 allows you to define your minimum set of OS calls and build a stripped down version of the RTX Lib. You will be building some type of scheduler so why not use one already built for the task. I have built a stripped version fully multithreaded with less than 500 bytes of code.
    The RTOS can be time slice round robin or cooperative or both but not preemptive. Timer0 and Register bank 1 by default are used by the RTOS but you can use the timer ticks for other operations within the RTOS. Also, the 8051 has no stack over flow monitor but since RTX 51 Tiny is managing the stack you can add a fail soft macro with your defined stack limits.
    With just a bit of forethought to the defined tasks, you can build a good determinate multithread system.
    The full 8051 interrupt system is still available for parallel operations.
    Download some of the RTX51Tiny examples from the Keil website to see how simple the RTOS can be.
    Bradford

  • Thank you all for the answers. It helped a lot.

  • The Keil RTX51 Tiny has run in a chip (old Philips 751)that had only 64 Bytes of RAM
    OK you have proven it can be done, that does not answer "why do it".
    I am sure that with freezing and some effort it is possible to strike a match on a bar of soap, but would you do that?

    Erik

  • RTX51-Tiny is in use since about 15 years, field-proven and stable. It has been used in many, many applications.

    Why do you re-invent the wheel?

    Reinhard