I'd like to use assert statements for debugging my code, but I can't seem to get it to work correctly.
The assert() statement does exist and does work: the execution stops if the condition is false. However, unlike error() there is no debug information printed to stdout, nor are the blue lights of death flashing.
Is it still in there ? or do i implement it myself? something like this:
#define my_assert(b) if (!b) error("ASSERT FAILED at %s, line %d.\n", __FILE__, __LINE__);
thanks,
Daniel
hello,
I'd like to use assert statements for debugging my code, but I can't seem to get it to work correctly.
The assert() statement does exist and does work: the execution stops if the condition is false. However, unlike error() there is no debug information printed to stdout, nor are the blue lights of death flashing.
I noticed there was assert functionality in an earlier revision of the mbed library: http://mbed.org/projects/libraries/svn/mbed/trunk/Debug.h?rev=2
Is it still in there ? or do i implement it myself? something like this:
<<code>>
#define my_assert(b) if (!b) error("ASSERT FAILED at %s, line %d.\n", __FILE__, __LINE__);
<</code>>
thanks,
Daniel
I have a similar question. How do I use assert() macros with mbed? With the following code I see that the flashing led doesn't happen because the assert stopped, but where does the failure get sent to? How can I flush stderr if processing is stopped? I don't see any output in my USB console except for starting.
I have a similar question. How do I use assert() macros with mbed? With the following code I see that the flashing led doesn't happen because the assert stopped, but where does the failure get sent to? How can I flush stderr if processing is stopped? I don't see any output in my USB console except for starting.
<<code>>
#include <mbed.h>
#include <assert.h>
DigitalOut myled(LED1);
int main()
{
printf("starting\n");
assert(false);
printf("finished\n");
while(1)
{
myled = 1;
wait(0.2);
myled = 0;
wait(0.2);
}
}
<</code>>
On revisiting this, the following macro works better for me than the one in assert.h
<<code>>
#ifdef NDEBUG
#define assert(expr)
#else
#define assert(expr) \
{ \
if (!(expr)) \
{ \
printf("Assertion failed: %s, file %s, line %d\n", #expr, __FILE__, __LINE__); \
fflush(stdout); \
exit(-1); \
} \
}
#endif
<</code>>
I like to add a debug message and use a Global.h file that defines the USB debugging serial port. This way you can access it from any of your classes. The blinking red and blue lights you notice the assert in case something is obstructing your view of your terminal. It really helps speed up debugging. Its kind of like you got pulled over. If you have an LCD screen and another microcontroller, you can output the assert statement to the screen and bypass a terminal app all together, which is convenient.
I like to add a debug message and use a Global.h file that defines the USB debugging serial port. This way you can access it from any of your classes. The blinking red and blue lights you notice the assert in case something is obstructing your view of your terminal. It really helps speed up debugging. Its kind of like you got pulled over. If you have an LCD screen and another microcontroller, you can output the assert statement to the screen and bypass a terminal app all together, which is convenient.
<<code>>
#ifndef Included_ProjectName__Global_header
#define Included_ProjectName__Global_header
#include "stdint.h"
#define DebugMode 1
#define Using_mbed 1
#if Using_mbed
#include "mbed.h"
#define nullptr 0
#endif // Using_mbed
#if DebugMode
#define Using_mbed 1
#if Using_mbed
#include "mbed.h"
#define nullptr 0
#endif // Using_mbed
#if DebugMode
#include "mbed.h"
static DigitalOut redLED (LED_RED, 1),
greenLED (LED_GREEN, 1),
blueLED (LED_BLUE, 1);
static Serial debug (USBTX, USBRX); // Tx, Rx
static Ticker assertionBlinker;
static void AssertionBlinkerHandler ()
{
static bool redLEDOn = false;
if (redLEDOn)
{
redLED = 0;
blueLED = 1;
redLEDOn = false;
}
else
{
redLED = 1;
blueLED = 0;
redLEDOn = true;
}
}
#define Assert(statement, message) \
{ \
if (!(statement)) \
{ \
assertionBlinker.attach (&AssertionBlinkerHandler, 0.2f); \
debug.printf("Assert: %s\n\r%s, line %d\n\r", #message, __FILE__, __LINE__); \
while (true); \
} \
}
static void ConsoleLine (char token)
{
debug.putc ('\r');
for (int i = 0; i < 80; ++i)
debug.putc (token);
debug.putc ('\n');
debug.putc ('\r');
}
#endif // DebugMode
#endif // Included_ProjectName__Global_header
<</code>>
Important Information for this Arm website
This site uses cookies to store information on your computer.
By continuing to use our site, you consent to our cookies.
If you are not happy with the use of these cookies, please review our
Cookie Policy
to learn how they can be disabled.
By disabling cookies, some features of the site will not work.
Access Warning
You do not have the correct permissions to perform this operation.
hello,
I'd like to use assert statements for debugging my code, but I can't seem to get it to work correctly.
The assert() statement does exist and does work: the execution stops if the condition is false. However, unlike error() there is no debug information printed to stdout, nor are the blue lights of death flashing.
I noticed there was assert functionality in an earlier revision of the mbed library: http://mbed.org/projects/libraries/svn/mbed/trunk/Debug.h?rev=2
Is it still in there ? or do i implement it myself? something like this:
thanks, Daniel