搜索
bottom↓
回复: 5

我写了个程序,却直接出错

[复制链接]

出0入0汤圆

发表于 2013-3-5 23:54:31 | 显示全部楼层 |阅读模式
我用的一个STM32f103RBT6的小板子,试了试RTT
貌似一开始就出错了,我写的任务一个也没有运行。
串口输出的是这样个,全是0,貌似也没有提供任何信息…………
\ | /
- RT -     Thread Operating System
/ | \     1.1.0 build Mar  5 2013
2006 - 2012 Copyright by rt-thread team
psr: 0x00000000
pc: 0x00000000
lr: 0x00000000
r12: 0x00000000
r03: 0x00000000
r02: 0x00000000
r01: 0x00000000
r00: 0x00000000
hard fault on thread: (NULL)
thread  pri  status      sp     stack size max used   left tick  error
-------- ---- ------- ---------- ---------- ---------- ---------- ---

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2013-3-6 09:28:22 | 显示全部楼层
从给出的信息无法确定问题所在。
1.  “我写的任务一个也没有运行”
没写任务时是否正常?

2.  所有寄存器都是0,说明还没有人开始使用,单步一般就可以查出来。

出0入0汤圆

 楼主| 发表于 2013-3-6 13:49:27 | 显示全部楼层
本帖最后由 landings 于 2013-3-6 13:51 编辑
aozima 发表于 2013-3-6 09:28
从给出的信息无法确定问题所在。
1.  “我写的任务一个也没有运行”
没写任务时是否正常?


现在我去掉了自己的任务,还是会出错。代码只有这些:
  1. int rt_application_init()
  2. {
  3.         rt_thread_t init_thread;

  4.         rt_err_t result;

  5. #if (RT_THREAD_PRIORITY_MAX == 32)
  6.         init_thread = rt_thread_create("init",
  7.                                                                 rt_init_thread_entry, RT_NULL,
  8.                                                                 2048, 8, 20);
  9. #else
  10.         init_thread = rt_thread_create("init",
  11.                                                                 rt_init_thread_entry, RT_NULL,
  12.                                                                 2048, 80, 20);
  13. #endif

  14.         if (init_thread != RT_NULL)
  15.                 rt_thread_startup(init_thread);

  16.         return 0;
  17. }
复制代码
我单步运行了一次,跟到rtthread_startup()里面的这里:
  1. rt_system_heap_init((void*)&Image$RW_IRAM1$ZI$Limit, (void*)STM32_SRAM_END);
复制代码
进去以后,跟到这里:

  1.     /* initialize the end of the heap */
  2.     heap_end        = (struct heap_mem *)&heap_ptr[mem->next];
  3.     heap_end->magic = HEAP_MAGIC;
  4.     heap_end->used  = 1;
  5.     heap_end->next  = mem_size_aligned + SIZEOF_STRUCT_MEM;
  6.     heap_end->prev  = mem_size_aligned + SIZEOF_STRUCT_MEM;
复制代码
现在有两种情况:
1. 如果我把唯一的断点打在heap_end->used  = 1;之前,它能运行到断点
如果我把断点打在heap_end->next  = mem_size_aligned + SIZEOF_STRUCT_MEM;之前,它运行不到断点。程序跑到rt_hw_hard_fault_exception里面了。

2. 当我换个步调,以很慢的速度在rt_system_heap_init中一行一行地运行的时候,居然能不出错地运行完毕,直到finsh>>出现。

3. 如果中途停在了其他的断点上耽误了时间,那么有时候会出错,有时候不会,出错的位置也很多变,不过通常都在rt_system_heap_init里面。

综上所述,貌似问题不在主线程?因为跑到rt_hw_hard_fault_exception里面这种情况的重现性并不好,仿佛跟我调试的步调有关似的,每次出错的位置并不一样。我就疑惑啊。

出0入0汤圆

发表于 2013-3-6 15:08:25 | 显示全部楼层
"使用STM32 103不同容量的单片机的时候",需要修改board.h 中的 STM32_SRAM_SIZE 值

出0入0汤圆

 楼主| 发表于 2013-3-6 15:34:17 | 显示全部楼层
aozima 发表于 2013-3-6 15:08
"使用STM32 103不同容量的单片机的时候",需要修改board.h 中的 STM32_SRAM_SIZE 值

果然,是这个问题

出0入0汤圆

 楼主| 发表于 2013-3-6 16:46:51 | 显示全部楼层
本帖最后由 landings 于 2013-3-6 16:48 编辑
aozima 发表于 2013-3-6 15:08
"使用STM32 103不同容量的单片机的时候",需要修改board.h 中的 STM32_SRAM_SIZE 值


还有些问题请教,我现在程序成功启动了,但是出现
rtc is not configured
please configure with set_date and set_time
我暂时并不需要用到RTC,怎么关掉它呢?


二是,请问rt_mq_t是否需要初始化?我在控制台看到了(mq != RT_NULL) assert failed at rt_mq_recv:2085

原因是,我是照抄的RT Thread 1.0 PDF说明中,第十章的串口设备操作例程,它里面的rx_mq在声明以后,未见有初始化,导致在rt_mq_recv()中出错。是否是这个原因?不过,串口调试器还是可以看到接收到的字符被重新发送回了。
如果要避免这个错误提示,该怎么做?
  1. /*
  2. * 程序清单:串口设备操作例程
  3. *
  4. * 在这个例程中,将启动一个devt线程,然后打开串口1和2
  5. * 当串口1和2有输入时,将读取其中的输入数据然后写入到
  6. * 串口1设备中。
  7. *
  8. */
  9. #include <rtthread.h>

  10. /* UART接收消息结构*/
  11. struct rx_msg
  12. {
  13.   rt_device_t dev;
  14.   rt_size_t size;
  15. };
  16. /* 用于接收消息的消息队列*/
  17. static rt_mq_t rx_mq;
  18. /* 接收线程的接收缓冲区*/
  19. static char uart_rx_buffer[64];

  20. /* 数据到达回调函数*/
  21. rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  22. {
  23.   struct rx_msg msg;
  24.   msg.dev = dev;
  25.   msg.size = size;

  26.   /* 发送消息到消息队列中*/
  27.   rt_mq_send(rx_mq, &msg, sizeof(struct rx_msg));

  28.   return RT_EOK;
  29. }

  30. void device_thread_entry(void* parameter)
  31. {
  32.   struct rx_msg msg;
  33.   int count = 0;
  34.   rt_device_t device, write_device;
  35.   rt_err_t result = RT_EOK;

  36.   /* 查找系统中的串口1设备 */
  37.   device = rt_device_find("uart1");
  38.   if (device!= RT_NULL)
  39.   {
  40.     /* 设置回调函数及打开设备*/
  41.     rt_device_set_rx_indicate(device, uart_input);
  42.     rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
  43.   }
  44.   /* 设置写设备为uart1设备 */
  45.   write_device = device;

  46.   /* 查找系统中的串口2设备 */
  47.   device= rt_device_find("uart2");
  48.   if (device_!= RT_NULL)
  49.   {
  50.     /* 设置回调函数及打开设备*/
  51.     rt_device_set_rx_indicate(device, uart_input);
  52.     rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
  53.   }

  54.   while (1)
  55.   {
  56.     /* 从消息队列中读取消息*/
  57.     result = rt_mq_recv(rx_mq, &msg,  
  58.       sizeof(struct rx_msg), 50);
  59.     if (result == -RT_ETIMEOUT)
  60.     {
  61.       /* 接收超时*/
  62.       rt_kprintf("timeout count:%d\n", ++count);
  63.     }

  64.     /* 成功收到消息*/
  65.     if (result == RT_EOK)
  66.     {
  67.       rt_uint32_t rx_length;
  68.       rx_length = (sizeof(uart_rx_buffer) - 1) >
  69. msg.size ? msg.size : sizeof(uart_rx_buffer) - 1;

  70.       /* 读取消息*/
  71.       rx_length = rt_device_read(msg.dev, 0,
  72. &uart_rx_buffer[0], rx_length);
  73.       uart_rx_buffer[rx_length] = '\0';

  74.       /* 写到写设备中*/
  75.       if (write_device != RT_NULL)
  76.         rt_device_write(write_device, 0,
  77. &uart_rx_buffer[0], rx_length);
  78.     }
  79.   }
  80. }

  81. int rt_application_init()
  82. {
  83.   /* 创建devt线程*/
  84.   rt_thread_t thread = rt_thread_create("devt",
  85.     device_thread_entry, RT_NULL,
  86.     1024, 25, 7);
  87.   /* 创建成功则启动线程*/
  88.   if (thread_!= RT_NULL)
  89.     rt_thread_startup(&thread);
  90. }
复制代码
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 09:29

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表