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

[复制链接]
 楼主| ningling_21 发表于 2020-6-29 08:20 | 显示全部楼层 |阅读模式
如题:
有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 编辑
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include "HAL_device.h"
  4. #include "drv_uart.h"
  5. //#include "can.h"
  6. #include "HAL_can.h"

  7. /* defined the LED pin: PA15 */
  8. #define LED_PIN    38
  9. #define LED2_PIN   39
  10. #define LED3_PIN   40
  11. #define LED4_PIN   41
  12. static rt_uint8_t led_1_stack[128];
  13. static struct rt_thread led_1_thread;

  14. static rt_uint8_t led_2_stack[128];
  15. static struct rt_thread led_2_thread;


  16. //--------------------------------------------------------------------------------------------------
  17. static void led_1_thread_entry(void * parameter)//线程1入口
  18. {
  19.         u8  led_stat_led=0;
  20.         //rt_hw_led_init();       
  21.         //rt_hw_usart_init();
  22.         rt_pin_mode(LED4_PIN, PIN_MODE_OUTPUT);
  23.        
  24.         while(1)
  25.         {
  26.                
  27.                 if(led_stat_led==0)
  28.                 {
  29.                         led_stat_led=1;
  30.                        
  31.                         rt_pin_write(LED4_PIN, PIN_LOW);
  32.                 }
  33.                 else
  34.                 {
  35.                         led_stat_led=0;
  36.                        
  37.                         rt_pin_write(LED4_PIN, PIN_HIGH);
  38.                        
  39.                         //mm32_uart_putc(UART1,0x55);
  40.                 }
  41.         rt_thread_delay( 200 );
  42.         }
  43. }
  44. static void led_2_thread_entry(void * parameter)//线程2入口
  45. {
  46.         u8  led_stat_led=0;
  47.         //rt_hw_led_init();       
  48.         //rt_hw_usart_init();
  49.         rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);

  50.         while(1)
  51.         {

  52.                 if(led_stat_led==0)
  53.                 {
  54.                         led_stat_led=1;
  55.                        
  56.                         rt_pin_write(LED3_PIN, PIN_LOW);
  57.                 }
  58.                 else
  59.                 {
  60.                         led_stat_led=0;
  61.                        
  62.                         rt_pin_write(LED3_PIN, PIN_HIGH);
  63.                 }
  64.         rt_thread_delay( 100 );
  65.         }
  66. }
  67. int main(void)
  68. {
  69.         u16  run_cnt=0;
  70.         u8  led_stat_led=0;
  71.         u8  flag_rt_thread=0;
  72.         rt_err_t result;
  73.     int count = 1;
  74.     /* set LED4 pin mode to output */
  75.     rt_pin_mode(LED_PIN,  PIN_MODE_OUTPUT);
  76.         rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
  77.         //rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);

  78.         rt_hw_uart_init();         
  79.     while (count)
  80.     {
  81.         run_cnt++;
  82.                 if(led_stat_led==0)
  83.                 {
  84.                         led_stat_led=1;
  85.                         rt_pin_write(LED_PIN, PIN_HIGH);
  86.                         rt_pin_write(LED2_PIN, PIN_LOW);
  87.                         //rt_pin_write(LED3_PIN, PIN_HIGH);
  88.                         //rt_pin_write(LED4_PIN, PIN_LOW);
  89.                 }
  90.                 else
  91.                 {
  92.                         led_stat_led=0;
  93.                         rt_pin_write(LED_PIN, PIN_LOW);                       
  94.                         rt_pin_write(LED2_PIN, PIN_HIGH);
  95.                         //rt_pin_write(LED3_PIN, PIN_LOW);
  96.                         //rt_pin_write(LED4_PIN, PIN_HIGH);
  97.                 }
  98.                
  99.                 rt_thread_mdelay(100);
  100.                
  101.                 //rt_kprintf("run_count=%d\r\n",run_cnt);
  102.                 if(run_cnt>=20)
  103.                 {
  104.                         run_cnt=0;
  105.                         if(flag_rt_thread==0)
  106.                         {
  107.                                 flag_rt_thread=1;
  108.                                 //rt_thread_create()
  109.                                 result = rt_thread_init(&led_1_thread,
  110.                             "led",
  111.                             led_1_thread_entry,
  112.                             RT_NULL,
  113.                             (rt_uint8_t*)&led_1_stack[0],                //堆栈起始
  114.                             sizeof(led_1_stack),                                //堆栈大小
  115.                             21,                                                                //线程优先级
  116.                             10);                                                                //时间片数
  117.        
  118.                                 if (result == RT_EOK)
  119.                                 {
  120.                                         rt_thread_startup(&led_1_thread);
  121.                                 }
  122.                                 rt_kprintf("led1_thread ok.\n");
  123.                                
  124.                                 result = rt_thread_init(&led_2_thread,
  125.                             "led",
  126.                             led_2_thread_entry,
  127.                             RT_NULL,
  128.                             (rt_uint8_t*)&led_2_stack[0],                //堆栈起始
  129.                             sizeof(led_2_stack),                                //堆栈大小
  130.                             22,                                                                //线程优先级
  131.                             5);                                                                //时间片数
  132.        
  133.                                 if (result == RT_EOK)
  134.                                 {
  135.                                         rt_thread_startup(&led_2_thread);
  136.                                 }
  137.                         }
  138.                        
  139.                        
  140.                 }
  141.     }
  142.     return RT_EOK;
  143. }

zeshoufx 发表于 2020-6-29 20:25 | 显示全部楼层
 楼主| ningling_21 发表于 2020-6-29 21:23 | 显示全部楼层
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函数不放循环的吧

评论

RT Thread main被包装成一个普通任务了.  发表于 2020-7-2 09:19
 楼主| 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 | 显示全部楼层

删除第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个任务时
您需要登录后才可以回帖 登录 | 注册

本版积分规则

5014

主题

17806

帖子

51

粉丝
快速回复 在线客服 返回列表 返回顶部

5014

主题

17806

帖子

51

粉丝
快速回复 在线客服 返回列表 返回顶部