[PIC32/SAM] PIC32 Harmony V2.06 如何使assert函数输出错误信息

[复制链接]
7525|14
 楼主| dfli 发表于 2023-6-8 12:20 | 显示全部楼层 |阅读模式
PIC32 Harmony V2.06 assert.h
#define assert(expr)     \
  ((void)((expr) ? 0 : (__assert (__LINE__, __FILE__, #expr, __ASSERT_FUNC), 0)))
设置错误条件,使断言起作用:
me->cellSize = 0;
assert(0 == me->cellSize);
程序停止运行,没有异常信息输出。

应如何配置,才能看到输出的信息?
 楼主| dfli 发表于 2023-6-8 12:22 | 显示全部楼层
extern void __attribute__((noreturn)) _fassert(int, const char *, const char *, const char*);
#define __assert(line,file,expression,func)  \
  _fassert(line,file,expression,func)
#define assert(expr)     \
  ((void)((expr) ? 0 : (__assert (__LINE__, __FILE__, #expr, __ASSERT_FUNC), 0)))

#else /* NDEBUG */
#define assert(exp) ((void)0)
#endif /* NDEBUG */

#if defined(NDEBUG) || !defined(__DEBUG)
#  define __conditional_software_breakpoint(X) ((void)0)
#else
#  define __conditional_software_breakpoint(X) \
     ((X) ?                                    \
       (void) 0 :                              \
       __builtin_software_breakpoint())
#endif
 楼主| dfli 发表于 2023-6-8 12:45 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| dfli 发表于 2023-6-8 12:46 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| dfli 发表于 2023-6-8 12:49 | 显示全部楼层
参考:help_harmony,Asserts Under the Debugger,
7:
The function _fassert is the built-in assert handler provided by the compiler. Modify the file to allow the debugger to fire a breakpoint when running the debugger (when defined(__DEBUG) is true) by adding the code shown in red in the following example.
#if defined(NDEBUG) || !defined(__DEBUG)

# define __conditional_software_breakpoint(X) ((void)0)

#else

# define __conditional_software_breakpoint(X) \

((X) ? \

(void) 0 : \

__builtin_software_breakpoint())

// Added to support debugger:

# undef assert

# define assert(expr) __conditional_software_breakpoint(expr)

// End of addition

#endif

保存,编译,调试:运行,停在:assert()语句处
一秒落纱 发表于 2023-9-17 18:22 | 显示全部楼层
打开您的PIC32 Harmony工程,并找到与您要使用assert函数的源文件相关的头文件(通常是"app.h"或"app.c"文件)
冰春彩落下 发表于 2023-9-17 21:52 | 显示全部楼层
在该头文件中添加以下代码行来定义自定义的_assert宏,并将错误信息输出到合适的调试接口(例如UART或调试串口)
夜阑风雨 发表于 2023-9-18 00:23 | 显示全部楼层
你可以试试这段代码看看
#include <stdio.h> // 需要包含stdio.h头文件

#define _assert(test, message) \
    do { \
        if (!(test)) { \
            printf("Assertion failed: %s\n", message); \
            while(1); // 或者您可以自己定义其他的错误处理方式,例如重启系统或其他操作 \
        } \
    } while (0)
别乱了阵脚 发表于 2023-9-18 03:59 | 显示全部楼层
这个我觉得可以找个技术支持来帮您
远山寻你 发表于 2023-9-18 06:51 | 显示全部楼层
没用过assert函数,这个是为了做log的么
江河千里 发表于 2023-9-18 09:22 | 显示全部楼层
程序停止但没有异常输出?
光辉梦境 发表于 2023-9-18 12:59 | 显示全部楼层
你可以用仿真的方式实现的啊,没必要用这个吧
暖了夏天蓝了海 发表于 2023-9-18 15:45 | 显示全部楼层
这种操作是不是需要连接上位机,并且连接个数据显示端才行啊
三生万物 发表于 2023-9-18 18:27 | 显示全部楼层
最后您这个参考,好用不?
淡漠安然 发表于 2023-9-18 23:00 | 显示全部楼层
正常还是找个例程跑一下比较好,移植一下也行
您需要登录后才可以回帖 登录 | 注册

本版积分规则

5

主题

17

帖子

0

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