# OS running on MCU

#### Pushkar1

Joined Apr 5, 2021
295
I'm looking for an answer to a question that's very confusing to me. How does an MCU control and monitor the devices connected to it when the embedded OS runs on it. For Example, Embedded system have buttons, motor and LCD connected to a micro controller. How embedded OS control and monitor buttons, motor and LCD.

I found that the OS does not communicate directly with the buttons, motor and LCD. Application program communicates with OS program.

I have come to the conclusion that the OS program is a universal program. If an external device is to communicate with the microcontroller, it must write an application program for the device. This application program works with OS program.

How does an MCU control and monitor the devices connected to it when the embedded OS runs on it.

I have expressed my views now I want someone to give their opinion on this

#### KeithWalker

Joined Jul 10, 2017
1,933
This is a very simplified explanation of what happens:
Before an application program is loaded onto a MCU, a program called a bootloader is installed. This is a machine language operating system that is installed in the EPROM that starts running on power-up. It defines the functions of the MCU components and starts up the application program. The compiled application program communicates with the bootloader by reading and writing to registers in the MCU that are controlled by the bootloader.

#### Papabravo

Joined Feb 24, 2006
16,938
Many embedded applications have no OS and ignore completely any peripheral devices that they have no use for. The RESET condition for almost al peripherals is to be inert, as if they were not even there. That way an application does not have to worry about placing them in such a state.

#### Pushkar1

Joined Apr 5, 2021
295
The compiled application program communicates with the bootloader by reading and writing to registers in the MCU that are controlled by the bootloader.
micro-controller have peripherals as SPI, I2C, UART etc. Any peripherals must be configured before use. Which program controls the peripheral, bootloader or application program?

#### MrChips

Joined Oct 2, 2009
24,401
Embedded MCU does not need bootloader. Executable code is already loaded in program memory which is usually flash memory.

Peripherals are initialized by the application program.

#### KeithWalker

Joined Jul 10, 2017
1,933
micro-controller have peripherals as SPI, I2C, UART etc. Any peripherals must be configured before use. Which program controls the peripheral, bootloader or application program?
The communications ports on a MCU may be defined by the hardware configuration or it may be configured by a bootloader.
Usually I/O peripherals have an associated library that compiles with the application to simplify writing communication programs but they can be used without a library if the application program reads and writes directly to the communications registers.

#### Pushkar1

Joined Apr 5, 2021
295
Embedded MCU does not need bootloader. Executable code is already loaded in program memory which is usually flash memory.

Peripherals are initialized by the application program.
I think you are right because MCU will run only one program which will be exe file

The only difference will be that the program will be divided into two phases.

1) Writing programs to control MCU resources like time, memory and I/O etc.

2) Writing a program for the peripheral you want to use as SPI, I2C, UART etc.

#### nsaspook

Joined Aug 27, 2009
8,770
The embedded OS is just a standard method of programming applications that needs to interact with various devices using various methods. It's like a restaurant. Instead of one person controlling and doing all the work we have a division of labor and resources from the manager to the fry cook. The applications program orders (API calls) for action (food, drink, dancing girls, ...) are made, collected, ordered and executed in a hopefully efficent and ordered fashion by trained workers (drivers/tasks/memory functions) overseen by the manager (OS resources to perform tasks). If all you make is hot-dogs (simple tasks, small programs) then you don't really need the OS but if you must serve a wide-range of service in various combinations the OS allows for common interfaces that can be quickly changed to adapt to changing circumstances while keeping a standard plan of operation.

#### MrChips

Joined Oct 2, 2009
24,401
nsa is correct. RTOS is used when you have multiple tasks running in time critical applications that require timely service.

The restaurant situation is a good analogy. Platters of food ready to be served are getting cold, the ice cream dessert is melting, customers are waiting to place their orders, another couple comes through the door while others are waiting at the counter to pay their bills, and then the phone rings!

RTOS does two things, provides services for established procedures such as reading and writing files, handling USB and wifi requests, screen updates, multimedia management, etc. It also launches, schedules and manages multiple tasks in a multi-tasking environment.

In a simple embedded MCU application you don't need OS if you are capable of writing the code for all the services required in the application.

#### BobTPH

Joined Jun 5, 2013
3,769
There is a wide range of "operating systems" used in embedded, from none at all to a full blown Linux. Making any generalization about how devices are controlled in an embedded operating system is bound to be wrong in nearly every example you can find.

Bob

#### click_here

Joined Sep 22, 2020
439
The answer for a Linux port is different to FreeRTOS.

Your question has overlooked so many things: There are 3 classifications to RTOS - Hard (must respond on time), firm (must always try), and soft (missing deadlines is acceptable). https://www.thegeekstuff.com/2012/02/rtos-basics/

If you'd like to use an embedded OS you need to make sure it's the right tool for the job.

FreeRTOS has a lot of IO examples out there, start by looking at "blinking led" examples - Here is an example for an Arduino...

Looking into actual examples might make you narrow your question down.

#### Pushkar1

Joined Apr 5, 2021
295
nsa is correct. RTOS is used when you have multiple tasks running in time critical applications that require timely service.
Let's assume embedded system is doing multitasking and executing the task at scheduled time with their priorities

With 1ms system tick rate It seems to me that OS do multitasking like this

For Example :
T1 runs once for 8ms in every 40ms with Priority 2
T2 runs twice for 5ms in every 40ms with Priority1
T3 runs three times for 10ms in every 40ms with Priority3

Code:
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT.
22222111111113333333222223333333333333
First line indicate time and second line indicate priority

if I'm going on the right direction so what are the T1, T2 and T3 from the practical point of view

#### Papabravo

Joined Feb 24, 2006
16,938
You forgot to allow for the idle task. Every multitasking OS has one of those. Only in trivial cases do tasks have fixed runtimes.

#### Pushkar1

Joined Apr 5, 2021
295
You forgot to allow for the idle task. Every multitasking OS has one of those. Only in trivial cases do tasks have fixed runtimes.
Thanks for showing valid point. I think I now understood the multitasking / scheduling for embedded operating systems. It seems to me that the multitasking schedule is implemented with the timer interrupt and priority setting for MCU

#### nsaspook

Joined Aug 27, 2009
8,770
The answer for a Linux port is different to FreeRTOS.

Your question has overlooked so many things: There are 3 classifications to RTOS - Hard (must respond on time), firm (must always try), and soft (missing deadlines is acceptable). https://www.thegeekstuff.com/2012/02/rtos-basics/

If you'd like to use an embedded OS you need to make sure it's the right tool for the job.

FreeRTOS has a lot of IO examples out there, start by looking at "blinking led" examples - Here is an example for an Arduino...

Looking into actual examples might make you narrow your question down.
It's pretty easy to make the Linux OS (firm) real-time but that's usually not the configuration generated for most devices.

As you see there is a lot of complexity in designing reliable RTOS applications so be sure you 'really' need one on a micro-controller system.

For instance this is a multi-core protocol driver for the RPI-3 with dedicated cores for SPI kernel threads for real-time data acquisition.

Last edited:

#### Pushkar1

Joined Apr 5, 2021
295
So far it understood that the OS program is divided into two parts

1) Kernel code
2) Device Driver code

You can connect any device to a microcontroller if you have a kernel code. For this you need to write the device driver code for the device you want to connect to the MCU

So it will happen that kernel code will directly control MCU and device driver code will communicate with kernel code

I guess I'm getting right concept in my head

Last edited:

#### MrChips

Joined Oct 2, 2009
24,401
So far it understood that the OS program is divided into two parts

1) Kernel code
2) Device Driver code

You can connect any device to a microcontroller if you have a kernel code. For this you need to write the device driver code for the device you want to connect to the MCU

So it will happen that kernel code will directly control MCU and device driver code will communicate with kernel code

I guess I'm getting right concept in my head
Why do you need to know this?
I have written hundreds of MCU applications and did not need to know any of this.
The best way to gain experience with embedded MCU programming is to start designing and writing programs.

#### Pushkar1

Joined Apr 5, 2021
295
Why do you need to know this?
Nothing special just wanted to understand how OS works when it run on MCU. All the members have given good advice to understand this question.

The best way to gain experience with embedded MCU programming is to start designing and writing programs.

#### MrChips

Joined Oct 2, 2009
24,401
And of the hundreds of embedded MCU applications I have written I have never once used an OS.