RTOS的必要性
当你开始增加你的嵌入式应用的功能时,在单一的主循环和一些中断例程中做所有的事情变得越来越难。通常情况下,下一级的复杂性是某种状态机,你的电子设备的输出会根据这个(内部)状态而改变。如果你需要能够同时操作多个复杂的输入和输出呢?一个很好的例子是TCP/IP连接,通过这个连接,你将接收某种数据,然后用来操作机械臂、控制电动机、发送信号……很快就会发现,需要新的抽象层次,以避免淹没在实现这种东西所需的复杂性中。这就是实时操作系统发挥作用的地方。
Zephyr与其他实时操作系统不同?
- Zephyr是由Linux基金会支持的。
- 通过学习Zephyr,你将自动获得对Linux内核的体验。两者在实现方式上表现出一些重叠,例如: Kconfig和设备树是Zephyr从Linux中借用的概念。
- Zephyr很灵活。
- Zephyr支持各种不同的开发板/SoCs。
更多参考见官网: https://docs.zephyrproject.org/latest/introduction/index.html
实时操作系统是如何工作的?
内核负责为每个特定的任务调度CPU时间,使它们看起来是同时进行的。
每个线程(或任务)在执行时都会使用寄存器和内存。这些处理器寄存器和堆栈(内存)的全部内容都是该特定线程的上下文。一旦RTOS决定切换线程并运行其他东西,它将需要首先将上下文存储起来,然后加载接下来运行的线程的上下文。这个过程被称为上下文切换。
除了线程之外,你还会使用诸如队列、互斥和信号等原语来进行线程间通信。然后,每个RTOS都为不同的协议提供不同程度的支持,如TCP/IP、蓝牙、LoRaWan……这使你的生活更容易,因为现在你不需要深入研究这些协议。你会得到一系列的API调用,这能提高开发速度。
什么是线程?
线程是独立的实例,负责执行一些任务。
一些关键的概念:
- 堆栈区(Stack area):线程堆栈的内存区域。其大小可以根据线程处理的需要来调整。
/* size of stack area used by each thread */
#define STACKSIZE 1024
- 线程控制块(Thread control block):存储线程元数据,是k_thread类型的实例。
K_THREAD_STACK_DEFINE(threadA_stack_area, STACKSIZE);
static struct k_thread threadA_data;
- 入口点函数(Entry point function):线程启动时被调用。最多有3个参数值可以传递给这个函数。
void threadA(void *dummy1, void *dummy2, void *dummy3)
{
ARG_UNUSED(dummy1);
ARG_UNUSED(dummy2);
ARG_UNUSED(dummy3);
ARG_UNUSED来表示这3个参数在我们的线程函数中没有使用。
-
调度策略(Scheduling policy):指示内核的调度器如何分配CPU时间给线程。
-
执行模式Execution mode:可以是监督者(supervisor)模式或用户(user)模式。默认情况下,线程在监督者模式下运行,允许访问特权CPU指令、整个内存地址空间和外围设备。用户模式的线程权限要少一些。
Zephyr如何选择要运行的线程?
“Thread is ready”(就绪) = 有资格被选为下一个运行线程。
以下因素可以使线程不就绪:
- 线程还没有被启动
- 等待内核对象完成一个操作(例如,线程正在占用不可用的信号(semaphore))。
- 等待超时的发生
- 被暂停
- 终止
如何在Zephyr中定义线程?
通过定义它的堆栈区域和线程控制块,然后调用k_thread_create()来生成的。
栈区必须使用K_THREAD_STACK_DEFINE或K_KERNEL_STACK_DEFINE来定义,以确保它在内存中被正确设置。
线程发起函数返回其线程ID,可以用来引用该线程。
#define MY_STACK_SIZE 500
#define MY_PRIORITY 5
extern void my_entry_point(void *, void *, void *);
K_THREAD_STACK_DEFINE(my_stack_area, MY_STACK_SIZE);
struct k_thread my_thread_data;
k_tid_t my_tid = k_thread_create(&my_thread_data, my_stack_area,
K_THREAD_STACK_SIZEOF(my_stack_area),
my_entry_point,
NULL, NULL, NULL,
MY_PRIORITY, 0, K_NO_WAIT);
为了定义线程,你需要初始化参数:
k_tid_t k_thread_create(struct k_thread *new_thread, k_thread_stack_t *stack, size_t stack_size, k_thread_entry_t entry, void *p1, void *p2, void *p3, int prio, uint32_t options, k_timeout_t delay)
参数:
- new_thread – 指向未初始化的k_thread结构的指针
- stack – 指向堆栈空间的指针。
- stack_size – 堆栈大小,字节数。
- entry – 线程入口函数。
- p1 – 第1个入口参数。
- p2 – 第2个入口参数。
- p3 – 第3个入口参数。
- prio – 线程优先级。
- options – 线程选项。
- delay – 调度延迟,或K_NO_WAIT(无延迟)。
返回:
- 新线程的ID。
另外,可以通过调用K_THREAD_DEFINE在编译时声明线程。请注意,该宏自动定义了堆栈区、控制块和线程ID等变量。
下面的代码与上面的代码段有相同的效果。
#define MY_STACK_SIZE 500
#define MY_PRIORITY 5
extern void my_entry_point(void *, void *, void *);
K_THREAD_DEFINE(my_tid, MY_STACK_SIZE,
my_entry_point, NULL, NULL, NULL,
MY_PRIORITY, 0, 0);
参考资料
- 软件测试精品书籍文档下载持续更新 https://github.com/china-testing/python-testing-examples 请点赞,谢谢!
- 本文涉及的python测试开发库 谢谢点赞! https://github.com/china-testing/python_cn_resouce
- python精品书籍下载 https://github.com/china-testing/python_cn_resouce/blob/main/python_good_books.md
线程命令
- k_thread_start() 启动
#include
#include
/* size of stack area used by each thread */
#define STACKSIZE 1024
/* scheduling priority used by each thread */
#define PRIORITY 7
/* delay between greetings (in ms) */
#define SLEEPTIME 200000
K_THREAD_STACK_DEFINE(threadA_stack_area, STACKSIZE);
static struct k_thread threadA_data;
/* threadA is a static thread that is spawned automatically */
void threadA(void *dummy1, void *dummy2, void *dummy3)
{
ARG_UNUSED(dummy1);
ARG_UNUSED(dummy2);
ARG_UNUSED(dummy3);
printk("thread_a: thread started n");
while (1)
{
printk("thread_a: thread loop n");
k_msleep(SLEEPTIME);
}
}
void main(void)
{
k_thread_create(&threadA_data, threadA_stack_area,
K_THREAD_STACK_SIZEOF(threadA_stack_area),
threadA, NULL, NULL, NULL,
PRIORITY, 0, K_FOREVER);
k_thread_name_set(&threadA_data, "thread_a");
k_thread_start(&threadA_data);
}
执行:
$ ninja run
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: qemu32,+nx,+pae
SeaBIOS (version zephyr-v1.0.0-0-g31d4e0e-dirty-20200714_234759-fv-az50-zephyr)
Booting from ROM..
*** Booting Zephyr OS build zephyr-v3.4.0-554-g33b116407b03 ***
thread_a: thread started
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
- k_thread_abort() 终止:线程被从所有的内核队列中移除。
#include
/* size of stack area used by each thread */
#define STACKSIZE 1024
/* scheduling priority used by each thread */
#define PRIORITY 7
/* delay between greetings (in ms) */
#define SLEEPTIME 500
K_THREAD_STACK_DEFINE(threadA_stack_area, STACKSIZE);
static struct k_thread threadA_data;
/* threadA is a static thread that is spawned automatically */
void threadA(void *dummy1, void *dummy2, void *dummy3)
{
ARG_UNUSED(dummy1);
ARG_UNUSED(dummy2);
ARG_UNUSED(dummy3);
int i = 10; // Amount of times to execute while loop
printk("thread_a: thread started n");
while (1)
{
printk("thread_a: thread loop n");
k_msleep(SLEEPTIME);
i -= 1;
if (i == 0)
{
printk("thread_a: thread abort n");
k_thread_abort(&threadA_data);
}
}
}
void main(void)
{
k_thread_create(&threadA_data, threadA_stack_area,
K_THREAD_STACK_SIZEOF(threadA_stack_area),
threadA, NULL, NULL, NULL,
PRIORITY, 0, K_FOREVER);
k_thread_name_set(&threadA_data, "thread_a");
k_thread_start(&threadA_data);
}
执行:
$ ninja run [0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: qemu32,+nx,+pae
SeaBIOS (version zephyr-v1.0.0-0-g31d4e0e-dirty-20200714_234759-fv-az50-zephyr)
Booting from ROM..
*** Booting Zephyr OS build zephyr-v3.4.0-554-g33b116407b03 ***
thread_a: thread started
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread abort
qemu-system-i386: terminating on signal 2
ninja: build stopped: interrupted by user.
- k_sleep()
线程可以阻止自己在指定的时间内执行。一旦达到时间限制就会自动变得可执行。
#include
#include
/* size of stack area used by each thread */
#define STACKSIZE 1024
/* scheduling priority used by each thread */
#define PRIORITY 7
/* delay between greetings (in ms) */
#define SLEEPTIME 500000
K_THREAD_STACK_DEFINE(threadA_stack_area, STACKSIZE);
static struct k_thread threadA_data;
/* threadA is a static thread that is spawned automatically */
void threadA(void *dummy1, void *dummy2, void *dummy3)
{
ARG_UNUSED(dummy1);
ARG_UNUSED(dummy2);
ARG_UNUSED(dummy3);
int i = 2; // Amount of times to execute while loop
k_timeout_t sleep_time = K_MSEC(5000); // length of sleep
printk("thread_a: thread started n");
while (1)
{
printk("thread_a: thread loop n");
k_msleep(SLEEPTIME);
i -= 1;
if (i == 0)
{
printk("thread_a: sleeping for 5000 ms n");
k_sleep(sleep_time);
i = 2;
}
}
}
void main(void)
{
k_thread_create(&threadA_data, threadA_stack_area,
K_THREAD_STACK_SIZEOF(threadA_stack_area),
threadA, NULL, NULL, NULL,
PRIORITY, 0, K_FOREVER);
k_thread_name_set(&threadA_data, "thread_a");
k_thread_start(&threadA_data);
}
执行:
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: qemu32,+nx,+pae
^[[0mSeaBIOS (version zephyr-v1.0.0-0-g31d4e0e-dirty-20200714_234759-fv-az50-zephyr)
Booting from ROM..
*** Booting Zephyr OS build zephyr-v3.4.0-554-g33b116407b03 ***
thread_a: thread started
thread_a: thread loop
thread_a: thread loop
thread_a: sleeping for 5000 ms
thread_a: thread loop
thread_a: thread loop
thread_a: sleeping for 5000 ms
thread_a: thread loop
thread_a: thread loop
thread_a: sleeping for 5000 ms
thread_a: thread loop
thread_a: thread loop
thread_a: sleeping for 5000 ms
thread_a: thread loop
thread_a: thread loop
thread_a: sleeping for 5000 ms
- k_thread_suspend()
挂起,需要使用k_thread_resume()来重新启动。
#include
/* size of stack area used by each thread */
#define STACKSIZE 1024
/* scheduling priority used by each thread */
#define PRIORITY 7
/* delay between greetings (in ms) */
#define SLEEPTIME 500
K_THREAD_STACK_DEFINE(threadA_stack_area, STACKSIZE);
static struct k_thread threadA_data;
K_THREAD_STACK_DEFINE(threadB_stack_area, STACKSIZE);
static struct k_thread threadB_data;
void threadA(void *dummy1, void *dummy2, void *dummy3)
{
ARG_UNUSED(dummy1);
ARG_UNUSED(dummy2);
ARG_UNUSED(dummy3);
int i = 3; //amount of times to loop before suspend
printk("thread_a: thread started n");
while (1)
{
printk("thread_a: thread loop n");
i -= 1;
if (i == 0)
{
printk("thread_a: thread suspended n");
k_thread_suspend(&threadA_data);
i = 3;
}
}
}
void threadB(void *dummy1, void *dummy2, void *dummy3)
{
ARG_UNUSED(dummy1);
ARG_UNUSED(dummy2);
ARG_UNUSED(dummy3);
printk("thread_b: thread started n");
while (1)
{
k_msleep(SLEEPTIME);
printk("thread_b: resuming thread_a n");
k_thread_resume(&threadA_data);
}
}
void main(void)
{
k_thread_create(&threadA_data, threadA_stack_area,
K_THREAD_STACK_SIZEOF(threadA_stack_area),
threadA, NULL, NULL, NULL,
PRIORITY, 0, K_FOREVER);
k_thread_name_set(&threadA_data, "thread_a");
k_thread_create(&threadB_data, threadB_stack_area,
K_THREAD_STACK_SIZEOF(threadB_stack_area),
threadB, NULL, NULL, NULL,
PRIORITY+1, 0, K_FOREVER); // priority of thread_b is lower than thread_a
k_thread_name_set(&threadB_data, "thread_b");
k_thread_start(&threadA_data);
k_thread_start(&threadB_data);
}
执行:
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: qemu32,+nx,+pae
^[[0mSeaBIOS (version zephyr-v1.0.0-0-g31d4e0e-dirty-20200714_234759-fv-az50-zephyr)
Booting from ROM..
*** Booting Zephyr OS build zephyr-v3.4.0-554-g33b116407b03 ***
thread_a: thread started
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread suspended
thread_b: thread started
thread_b: resuming thread_a
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread suspended
thread_b: resuming thread_a
thread_a: thread loop
thread_a: thread loop
- k_thread_join() 睡眠,直到另一线程退出。
比如说:thread_b负责设置硬件接口,线程_a负责处理这个接口的数据。只要线程_b没有退出,线程_a就不能启动,所以在这种情况下我们会使用k_thread_join(thread_b, timeout)。
#include
/* size of stack area used by each thread */
#define STACKSIZE 1024
/* scheduling priority used by each thread */
#define PRIORITY 7
/* delay between greetings (in ms) */
#define SLEEPTIME 500
K_THREAD_STACK_DEFINE(threadA_stack_area, STACKSIZE);
static struct k_thread threadA_data;
K_THREAD_STACK_DEFINE(threadB_stack_area, STACKSIZE);
static struct k_thread threadB_data;
void threadA(void *dummy1, void *dummy2, void *dummy3)
{
ARG_UNUSED(dummy1);
ARG_UNUSED(dummy2);
ARG_UNUSED(dummy3);
printk("thread_a: thread started n");
printk("thread_a: waiting for thread_b to complete n");
k_thread_join(&threadB_data, K_FOREVER); // wait forever until thread_b returns
while (1)
{
printk("thread_a: thread loop n");
k_msleep(SLEEPTIME);
}
}
void threadB(void *dummy1, void *dummy2, void *dummy3)
{
ARG_UNUSED(dummy1);
ARG_UNUSED(dummy2);
ARG_UNUSED(dummy3);
printk("thread_b: thread started n");
k_msleep(SLEEPTIME);
printk("thread_b: returning to thread_a n");
}
void main(void)
{
k_thread_create(&threadA_data, threadA_stack_area,
K_THREAD_STACK_SIZEOF(threadA_stack_area),
threadA, NULL, NULL, NULL,
PRIORITY, 0, K_FOREVER);
k_thread_name_set(&threadA_data, "thread_a");
k_thread_create(&threadB_data, threadB_stack_area,
K_THREAD_STACK_SIZEOF(threadB_stack_area),
threadB, NULL, NULL, NULL,
PRIORITY+1, 0, K_FOREVER); // priority of thread_b is lower than thread_a
k_thread_name_set(&threadB_data, "thread_b");
k_thread_start(&threadA_data);
k_thread_start(&threadB_data);
}
执行:
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: qemu32,+nx,+pae
SeaBIOS (version zephyr-v1.0.0-0-g31d4e0e-dirty-20200714_234759-fv-az50-zephyr)
Booting from ROM..
*** Booting Zephyr OS build zephyr-v3.4.0-554-g33b116407b03 ***
thread_a: thread started
thread_a: waiting for thread_b to complete
thread_b: thread started
thread_b: returning to thread_a
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop
thread_a: thread loop