ningling_21 发表于 2020-6-29 08:20

灵动芯片32L373移植了RT-THREAD后,建多个任务为何死机?

如题:
有main任务,又新建2个任务,只是闪灯,运行2秒就进入rt_hw_hard_fault_exception

zhu^zhu 发表于 2020-6-29 08:39

这应该是纯软件的问题。最好是把代码贴出来,应该很容易分析问题所在。

ningling_21 发表于 2020-6-29 10:24

本帖最后由 ningling_21 于 2020-7-1 22:16 编辑



#include <rtthread.h>
#include <rtdevice.h>
#include "HAL_device.h"
#include "drv_uart.h"
//#include "can.h"
#include "HAL_can.h"

/* defined the LED pin: PA15 */
#define LED_PIN    38
#define LED2_PIN   39
#define LED3_PIN   40
#define LED4_PIN   41
static rt_uint8_t led_1_stack;
static struct rt_thread led_1_thread;

static rt_uint8_t led_2_stack;
static struct rt_thread led_2_thread;


//--------------------------------------------------------------------------------------------------
static void led_1_thread_entry(void * parameter)//线程1入口
{
        u8led_stat_led=0;
        //rt_hw_led_init();       
        //rt_hw_usart_init();
        rt_pin_mode(LED4_PIN, PIN_MODE_OUTPUT);
       
        while(1)
        {
               
                if(led_stat_led==0)
                {
                        led_stat_led=1;
                       
                        rt_pin_write(LED4_PIN, PIN_LOW);
                }
                else
                {
                        led_stat_led=0;
                       
                        rt_pin_write(LED4_PIN, PIN_HIGH);
                       
                        //mm32_uart_putc(UART1,0x55);
                }
      rt_thread_delay( 200 );
        }
}
static void led_2_thread_entry(void * parameter)//线程2入口
{
        u8led_stat_led=0;
        //rt_hw_led_init();       
        //rt_hw_usart_init();
        rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);

        while(1)
        {

                if(led_stat_led==0)
                {
                        led_stat_led=1;
                       
                        rt_pin_write(LED3_PIN, PIN_LOW);
                }
                else
                {
                        led_stat_led=0;
                       
                        rt_pin_write(LED3_PIN, PIN_HIGH);
                }
      rt_thread_delay( 100 );
        }
}
int main(void)
{
        u16run_cnt=0;
        u8led_stat_led=0;
        u8flag_rt_thread=0;
        rt_err_t result;
    int count = 1;
    /* set LED4 pin mode to output */
    rt_pin_mode(LED_PIN,PIN_MODE_OUTPUT);
        rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
        //rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);

        rt_hw_uart_init();       
    while (count)
    {
      run_cnt++;
                if(led_stat_led==0)
                {
                        led_stat_led=1;
                        rt_pin_write(LED_PIN, PIN_HIGH);
                        rt_pin_write(LED2_PIN, PIN_LOW);
                        //rt_pin_write(LED3_PIN, PIN_HIGH);
                        //rt_pin_write(LED4_PIN, PIN_LOW);
                }
                else
                {
                        led_stat_led=0;
                        rt_pin_write(LED_PIN, PIN_LOW);                       
                        rt_pin_write(LED2_PIN, PIN_HIGH);
                        //rt_pin_write(LED3_PIN, PIN_LOW);
                        //rt_pin_write(LED4_PIN, PIN_HIGH);
                }
               
                rt_thread_mdelay(100);
               
                //rt_kprintf("run_count=%d\r\n",run_cnt);
                if(run_cnt>=20)
                {
                        run_cnt=0;
                        if(flag_rt_thread==0)
                        {
                                flag_rt_thread=1;
                                //rt_thread_create()
                                result = rt_thread_init(&led_1_thread,
                            "led",
                            led_1_thread_entry,
                            RT_NULL,
                            (rt_uint8_t*)&led_1_stack,                //堆栈起始
                            sizeof(led_1_stack),                                //堆栈大小
                            21,                                                                //线程优先级
                            10);                                                                //时间片数
       
                                if (result == RT_EOK)
                                {
                                        rt_thread_startup(&led_1_thread);
                                }
                                rt_kprintf("led1_thread ok.\n");
                               
                                result = rt_thread_init(&led_2_thread,
                            "led",
                            led_2_thread_entry,
                            RT_NULL,
                            (rt_uint8_t*)&led_2_stack,                //堆栈起始
                            sizeof(led_2_stack),                                //堆栈大小
                            22,                                                                //线程优先级
                            5);                                                                //时间片数
       
                                if (result == RT_EOK)
                                {
                                        rt_thread_startup(&led_2_thread);
                                }
                        }
                       
                       
                }
    }
    return RT_EOK;
}

zeshoufx 发表于 2020-6-29 20:25

ningling_21 发表于 2020-6-29 10:24


解决了吗

ningling_21 发表于 2020-6-29 21:23

zeshoufx 发表于 2020-6-29 20:25
解决了吗

qjp1988113 发表于 2020-6-30 08:21

很混乱,你不觉得你的所谓的MAIN任务很奇怪么。一个程序必须有一个main函数,作为主程序的入口。既然用到了RT-THREAD,最好就把所以动作都做成TASK子程序。main函数的while里面就不要放东西了。

huangzushuimd 发表于 2020-6-30 09:26

有main任务,又新建2个任务,只是闪灯,运行2秒就进入rt_hw_hard_fault_exception
这个建立任务应该在while的外面,确保只运行一次;不然的话,可能会建立任务很多次;
具体使用请参考:
https://www.rt-thread.org/document/site/um4003-rtthread-programming-manual.pdf
https://www.rt-thread.org/document/site/tutorial/iot_board_tutorial.pdf

ningling_21 发表于 2020-6-30 09:51

huangzushuimd 发表于 2020-6-30 09:26
有main任务,又新建2个任务,只是闪灯,运行2秒就进入rt_hw_hard_fault_exception
这个建立任务应该在while ...

确实只建立一次,有标志位控制,在建立的时就异常

zeshoufx 发表于 2020-6-30 18:36

ningling_21 发表于 2020-6-30 09:51
确实只建立一次,有标志位控制,在建立的时就异常

mian函数里面的循环能不能作为一个独立的任务,,,一般系统下的mian函数不放循环的吧

ningling_21 发表于 2020-6-30 19:06

zeshoufx 发表于 2020-6-30 18:36
mian函数里面的循环能不能作为一个独立的任务,,,一般系统下的mian函数不放循环的吧 ...

这里的main 也是一个任务

suncl110 发表于 2020-7-2 09:21

rt_hw_hard_fault_exception 一般是寻址失败,栈溢出的可能性比较大.

晓伍 发表于 2020-7-7 15:53

知道是死在哪里了吗

八层楼 发表于 2020-7-7 15:53

真是看不太好

观海 发表于 2020-7-7 16:00

单个任务不死机吗

guanjiaer 发表于 2020-7-7 16:00

帮楼主顶一下

heimaojingzhang 发表于 2020-7-7 16:00

楼主问题解决了吗

ningling_21 发表于 2020-7-7 20:47

heimaojingzhang 发表于 2020-7-7 16:00
楼主问题解决了吗

删除第3个任务就可以

ningling_21 发表于 2020-7-7 20:47

观海 发表于 2020-7-7 16:00
单个任务不死机吗

单个任务不会

ningling_21 发表于 2020-7-7 20:48

晓伍 发表于 2020-7-7 15:53
知道是死在哪里了吗

创建第3个任务时
页: [1]
查看完整版本: 灵动芯片32L373移植了RT-THREAD后,建多个任务为何死机?