If you are a beginner to Zephyr and RTOS, This blog is for you. I'm gonna show you basics of how to create thread in Zephyr and show you basics of threads and working.
If you already working with Arduino or PIC micro controller you are working in Super Loop Style.
Round Robin is so easy to implement because it is a natural way of writing software. Developers simply add new code to the super loop and a task is now added. There is no need to create dynamic and memory expensive objects. Just add code!
It runs from to top to bottom by executing the task one by one. If you need any emergency works to be done it can be done by Interrupts.
When some task is waiting for sometime other task also delayed due to the flow. It's not good right ?
Here is the working flow of RTOS.
When it comes to RTOS one of the the main advantage is Multi Threading. It's a ability to do more than one task at a time.For example:
If you write thread with individual Infinite while loop, It will execute parallel.

Refer above image, Each task has its own while loop and it will run Interdependently. If you want to pass values between tasks you may need to implement Queues, Mutex, Semaphore which can be discussed in future Blogs. Now we just look on how to run 3 task individually.
Note: Even it looks like parallel execution, It wont happens like that in single core CPU. It will allocate each task a period of time ex: 1 ms. for every period tasks will get switched from execution to read state and from ready state to execution. Scheduler will save running task current variables and registers before switching to other task. when it move the previous task from ready state to run state it will retrieve the old variables data and allow it to Resume the executions.
Talk is cheap show me the code - Linus Torvalds.
Shall we jump into coding part ??
Here is the simple code to Run 3 task
Get full Code on Click here
/* * * Author: Manu Devappa | manudevappsa@gmail.com * * Implementation of MultiThread in Zephyr RTOS * */ #include <zephyr.h> #include <device.h> #include <drivers/gpio.h> #include <sys/printk.h> #include <sys/__assert.h> #include <string.h> /* size of stack area used by each thread */ #define STACKSIZE 1024 /* Thread 1*/ void thread1(void) { while(1){ // place any other code to run Here printk("1\n"); k_msleep(100); } } /* Thread 2*/ void thread2(void) { while(1){ // place any other code to run Here printk("2\n"); k_msleep(200); } } /* Thread 3*/ void thread3(void) { while (1) { // place any other code to run Here printk("3\n"); k_msleep(300); } } K_THREAD_DEFINE(blink0_id, STACKSIZE, thread1, NULL, NULL, NULL, 7, 0, 0); K_THREAD_DEFINE(blink1_id, STACKSIZE, thread2, NULL, NULL, NULL, 7, 0, 0); K_THREAD_DEFINE(uart_out_id, STACKSIZE, thread3, NULL, NULL, NULL, 7, 0, 0);
K_THREAD_DEFINE
(name, stack_size, entry, p1, p2, p3, prio, options, delay)The ID of the thread can be accessed using:
- Parameters
name – Name of the thread.
stack_size – Stack size in bytes.
entry – Thread entry function.
p1 – 1st entry point parameter.
p2 – 2nd entry point parameter.
p3 – 3rd entry point parameter.
prio – Thread priority.
options – Thread options.
delay – Scheduling delay (in milliseconds), zero for no delay.
west build -p auto -b <yourboardname>
No comments:
Post a Comment