**1.安裝Wifi配置庫(nanoFramework.System.Device.Wifi)** ![](https://img2023.cnblogs.com/blog/2907004/202307/2907004-20230710193559740-846116472.jpg) > 1.1 n ...
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 <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
/* 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 <zephyr/zephyr.h>
/* 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 <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
/* 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 <zephyr/zephyr.h>
/* 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 <zephyr/zephyr.h>
/* 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
釘釘或微信號: pythontesting 微信公眾號:pythontesting