STM32 學習之 RT-Thread 創建進程錯誤

  1. 爲了方便管理,新建了一個函數,將進程的棧,線程名,優先級,還有入口函數,以參數的形式預留了出來

    struct rt_thread tid_thread[4];
rt_uint8_t create_thread(const char       *name,
                        void (*entry)(void *parameter),
                        void             *stack_start,
                        rt_uint8_t        priority)
{
    rt_err_t rt_err;
    rt_thread_t tmp_tid_thread = tid_thread;

    // 創建按鍵線程
    rt_err = rt_thread_init(tmp_tid_thread,            //線程控制塊
                   name,                            //線程名字,在shell裏面可以看到
                   entry,                           //線程入口函數
                   RT_NULL,                         //線程入口函數參數
                   stack_start,                 //線程棧起始地址
                   sizeof(stack_start),             //線程棧大小
                   priority,                        //線程的優先級
                   20);                             //線程時間片
    if(rt_err == RT_EOK)
    {
        rt_kprintf("Thread %s init seccess!", name);
        rt_thread_startup(tmp_tid_thread);             //啓動線程key_thread,開啓調度
        tmp_tid_thread++;
    }
    else
    {
        rt_kprintf("Thread %s init failed!", name);
    }
    return rt_err;
}

2.在新建進程的時候,在進程的棧,線程名,優先級,還有入口函數都聲明好的情況下,調用這個函數,結果每次第一個進程一初始化成功,就會進入Hard Fault 中斷。

create_thread("key", key_thread_entry, &rt_key_thread_stack[0], 3 ); //按鍵進程

報錯信息

 \ | /
- RT -     Thread Operating System
 / | \     2.1.0 build Sep 13 2018
 2006 - 2017 Copyright by rt-thread team
psr: 0x21000000
r00: 0x20001b90
r01: 0x10000000
r02: 0xf0000000
r03: 0x00000020
r04: 0xdeadbf33
r05: 0x00000000
r06: 0xdeadbeef
r07: 0xdeadbeef
r08: 0xdeadbeef
r09: 0xdeadbeef
r10: 0xdeadbeef
r11: 0xdeadbeef
r12: 0x0000000a
 lr: 0x0800291b
 pc: 0x0800291a
hard fault on thread: key

3.後來決定用教程中的方法老老實實的建立靜態線程,雖然成功了,但是由於所有的進程使用的 句柄都是同一個,又出了問題


    // 創建按鍵線程
    rt_err = rt_thread_init(&tid_thread,            //線程控制塊
                   "key",                           //線程名字,在shell裏面可以看到
                   key_thread_entry,                //線程入口函數
                   RT_NULL,                         //線程入口函數參數
                   &rt_key_thread_stack[0],         //線程棧起始地址
                   sizeof(rt_key_thread_stack),     //線程棧大小
                   1,                               //線程的優先級
                   20);                             //線程時間片
    rt_app_stratup("key", &tid_thread, rt_err);
    // 創建按鍵線程
    rt_err = rt_thread_init(&tid_thread,            //線程控制塊
                   "lcd",                           //線程名字,在shell裏面可以看到
                   lcd_thread_entry,                //線程入口函數
                   RT_NULL,                         //線程入口函數參數
                   &rt_lcd_thread_stack[0],         //線程棧起始地址
                   sizeof(rt_lcd_thread_stack),     //線程棧大小
                   2,                               //線程的優先級
                   20);                             //線程時間片
    rt_app_stratup("lcd", &tid_thread, rt_err);

信息

(thread->stat == RT_THREAD_SUSPEND) assertion failed at function:rt_thread_timeout, line number:708

最後爲每個進程都分配一個獨有的 靜態 控制塊,就OK 了

static struct rt_thread tid_thread_key;//線程控制塊
static struct rt_thread tid_thread_iwdg;//線程控制塊
static struct rt_thread tid_thread_lcd;//線程控制塊
static struct rt_thread tid_thread_beep;//線程控制塊

// 創建按鍵線程
rt_err = rt_thread_init(&tid_thread_key,            //線程控制塊
               "key",                           //線程名字,在shell裏面可以看到
               key_thread_entry,                //線程入口函數
               RT_NULL,                         //線程入口函數參數
               &rt_key_thread_stack[0],         //線程棧起始地址
               sizeof(rt_key_thread_stack),     //線程棧大小
               1,                               //線程的優先級
               20);                             //線程時間片
rt_app_stratup("key", &tid_thread_key, rt_err);
// 創建按鍵線程
rt_err = rt_thread_init(&tid_thread_lcd,            //線程控制塊
               "lcd",                           //線程名字,在shell裏面可以看到
               lcd_thread_entry,                //線程入口函數
               RT_NULL,                         //線程入口函數參數
               &rt_lcd_thread_stack[0],         //線程棧起始地址
               sizeof(rt_lcd_thread_stack),     //線程棧大小
               2,                               //線程的優先級
               20);                             //線程時間片
rt_app_stratup("lcd", &tid_thread_lcd, rt_err);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章