cancel
Showing results for 
Search instead for 
Did you mean: 

I want to implement a single functions that continuously controls two or more output peripheral all at once. Which is a proper way to achieve that?

GSaw
Associate II

I'm working on G030C6 MCU.

I have written a program that takes input from multiple sensors and based on the input (true/false) give output to other peripherals. Input is taken from polling therefore all the functions are written in main while loop.

I've read that above requirement can be achieved using RTOS but not sure if I can integrate RTOS scheduler with main while loop so that other functions are also executed.

I'm new to embedded programming and STM. Please Help.

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions

Your question is very vague, that's hard to answer helpfully. You seem to ask about multitasking, i.e. making the processor appear as if executing several processes simultaneously. Generally there are two approaches.

One is preemtive multitasking. A piece of code (in embedded environment usually called RTOS) uses periodic (timer) interrupt to rapidly switch execution between mutually relatively independent pieces of program (processes, threads, whatever terminology used here). The primary advantage is that individual processes don't have to be aware of other processes and can be written as blocking, which also means that preexisting pieces of code can be relatively easily included as a separate process. Drawback is higher memory consumption (as state of individual processes is mostly kept in the stack, which has to be separately allocated to each process). The relatively complex inter-process interaction/communication and timing issues can be argued both positive and negative ways. There is also some learning to do.

The other is cooperative multitasking. Here, the individual processes are written manually so that they are non-blocking (i.e. they execute only some portion of code at a time, and don't wait for any stimulus with unknown/lengthy timing), and they keep their state explicitly in global or static variables (either as an explicit state machine, or in form of "natural" variables, e.g. a data transmitting process keeps it state typically as an index to the buffer holding data and a counter indicating number of remaining data to transmit). This concept is sometimes also called as "superloop", as individual processes are usually written as functions, and the "main" program simply calls all these functions in a loop.

In reality, things are not black and white and there are also various blends of these approaches in various forms, but these are the general outlines.

Try to go through some general embedded programming education material/books/videos/courses.

JW

View solution in original post

2 REPLIES 2

Your question is very vague, that's hard to answer helpfully. You seem to ask about multitasking, i.e. making the processor appear as if executing several processes simultaneously. Generally there are two approaches.

One is preemtive multitasking. A piece of code (in embedded environment usually called RTOS) uses periodic (timer) interrupt to rapidly switch execution between mutually relatively independent pieces of program (processes, threads, whatever terminology used here). The primary advantage is that individual processes don't have to be aware of other processes and can be written as blocking, which also means that preexisting pieces of code can be relatively easily included as a separate process. Drawback is higher memory consumption (as state of individual processes is mostly kept in the stack, which has to be separately allocated to each process). The relatively complex inter-process interaction/communication and timing issues can be argued both positive and negative ways. There is also some learning to do.

The other is cooperative multitasking. Here, the individual processes are written manually so that they are non-blocking (i.e. they execute only some portion of code at a time, and don't wait for any stimulus with unknown/lengthy timing), and they keep their state explicitly in global or static variables (either as an explicit state machine, or in form of "natural" variables, e.g. a data transmitting process keeps it state typically as an index to the buffer holding data and a counter indicating number of remaining data to transmit). This concept is sometimes also called as "superloop", as individual processes are usually written as functions, and the "main" program simply calls all these functions in a loop.

In reality, things are not black and white and there are also various blends of these approaches in various forms, but these are the general outlines.

Try to go through some general embedded programming education material/books/videos/courses.

JW

Thank you this was v helpful for further research/knowledge gain :smiling_face_with_halo: