# Concurrent processes in MCUs

Is it possible to achieve FPGA-like concurrent functionality on, for example, ARM Cortex-4?

If I have a process which does time-sensitive calculations and at the same time I would like to listen for a button press. Even if I use interrupts for buttons, it will pause time-sensitive calculations for a bit to execute onPress function.

I believe what I am asking is - how to simulate threads on hardware?

• How time-sensitive are they? What is your original problem? Most likely you are asking the wrong question. – starblue Apr 29 '13 at 9:22
• The question is not about the time-sensitive process. It is about concurrency in MCUs. The process may be any... – Arturs Vancans Apr 29 '13 at 14:47

## 7 Answers

FPGAs have concurrent execution not by threads, but by real parallel execution paths. There may be some gates on one part of the die performing an addition while other gates on another part of the die are performing another addition.

Threads are one of many programming abstractions that make it seem as if a CPU can execute multiple tasks when really it can't (multitasking). You wouldn't simulate threads on a Cortex -- you would just implement them, or more likely by selecting an operating system or library that has already implemented the functionality for you.

Regardless of what you do, be it cooperative multitasking, preemptive multitasking, interrupt (event) driven processing, you will have to handle the reality that each CPU can do only one thing. If you need to do something else, the thing you were doing must necessarily be interrupted or it must yield control. If you truly need to do more than one thing, you need more than one CPU, or a CPU with multiple cores.

The problem then becomes balancing the need to do things now with the need to also do other things. Real-time operating systems have been developed to address this problem. Depending on the complexity of your problem and your needs, a RTOS may be overly complex, and you will just have to do what you can to minimize the impact of multitasking and interrupts on your program. Usually this means setting appropriate interrupt priorities if the platform supports it, and keeping interrupt handlers quick to minimize interrupt latency.

• Obviously, a multithreaded core is another alternative. Even Swith-on-Event MultiThreading might be sufficiently fine-grained for some parallelism. – Paul A. Clayton Apr 29 '13 at 16:38

On the Cortex you will have a single core / execution path so what you're asking isn't possible. A seperate thread or or interrupt of any kind will delay execution of other tasks to some degree. You may be able to optimize that to reduce the delay but it will always be present to some degree.

One option to consider is a seperate slave processor such as a cheap microcontroller that can handle button presses and similar time-critical tasks and then be polled from the main processor. That could implement a simple serial protocol for example to retrieve events that have occurred since last time it was checked.

• Another option is to use one of the new dual-core controllers, such as the NXP LPC4300, where you have an M4 core and a separate M0 core. – fm_andreas Apr 28 '13 at 17:30

While PeterJ's answer is almost entirely correct, there are a number of limited cases that fall outside of that generalization: in some very limited ways, your MCU does have ways of doing more than one thing at a time, but only if one of the things you need to do is built into the MCU's hardware:

• The simplest example of this is PWM: most MCUs can produce square wave with certain characteristics while executing your code
• Similarly, if your MCU has hardware-support for SPI protocol, it can be sending a character while executing your code (for example preparing the next character so send)
• Same thing applies to USART usage and other serial transmission or reception

The examples in datasheets usually do not take advantage of this fact. For instance, for AVR, SPI examples look like this (pseudocode)

Loop:
Prepare byte of data
Write data to register
Wait for send to finish
Go to loop


You can take advantage of the weak multiprocessing abilities by rewriting the code as follows:

Loop:
Prepare byte of data
Wait for send to finish
Write data to register
Go to loop


This way the data is prepared while it is being sent: almost like doing two things at once. The same thing can be accomplished by using interrupts, however these generally have a large overhead, and as a result do not provide an advantage over polling code for relatively frequent events, at least on the AVR platform.

In addition to various other answers:

You can disable interrupts during the time critical calculations, then the (button press) interrupt will be serviced once you enable them again. Button presses aren't too time sensitive.

Check out the Parallax Propeller (www.parallax.com) it has 8 independent processors each with a 2kB program space. Any processor can access any of the 32 digital I/O pins and the 32kB of shared Hub memory. Each prodessor (COG in Propeller speak) is capsble of interacting with the other COGs through the shared memory space. It is programmable in it's native languages, Spin or PASM or C or BASIC or Forth.

To the above answers, I would add that the reason microcontrollers are so can do so much with comparatively small amount of circuitry is that in most designs there are relatively few things that truly need to be done "simultaneously". If two tasks need to be performed simultaneously, then separate circuitry will be required for the two tasks. If, however, the tasks can be performed at discrete times, then it may be possible to have a single set of circuitry perform one task and then later perform the other. The reason that even a simple processor can perform so many complicated functions is that circuitry only needs to be dedicated to a task while it is actually being performed. The rest of the time, the circuitry may be used for other purposes.

Check XMOS and their xCORE since they claim that " xCORE multicore microcontroller is able to run multiple real-time tasks simultaneously"

• "Each logical processor core gets a guaranteed slice of processing power". Sounds like pre-emptive multitasking in silicon, or Intel Hyper-Threading. – Phil Frost Apr 29 '13 at 23:08