Adding assertion failure callback
In this article, we will explore the ‘Q_onAssert’ callback function, which plays a crucial role in handling assertion failures within your codebase. You have to check this file qassert.h, and here you find this Q_onAssert.
Q_onAssert is a callback function invoked in the case of assertion failure. In any of the APIs of the framework, if there is any assertion failure due to your invalid arguments or if there are too many nesting hierarchical state machines, then there could be an assertion failure. So, assertion failure may happen due to many reasons. And if there is any assertion failure, it will be notified to you, I mean, to your application through this callback function. This you have to implement in your main.cpp, as shown in Figure 2.
What is provided here using these parameters?
The first module contains the name of the file or module in which the assertion failed. So, this is a Null terminated C string. You can directly print that string.
We will do one thing. We’ll use our Serial.println to print this module name. So, we will print “Assertion failure” and print the module at which the assertion failure occurred. Just print ‘(String) module.’ And after that, also let’s print the ‘location’ and let’s hang here, as shown below.
Q_NORETURN Q_onAssert ( char_t const Q_ROM *const module,int_t const location ){ Serial.println("Assertion failure!!"); Serial.println((String)module); Serial.println(location); while(1); }
Q_onAssert function in main.cpp
In the upcoming article, let’s explore other APIs, that is QHSM_INIT and QHSM_DISPATCH.
Get the Full Course on Embedded System Design using UML State Machines Here.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1