Skip to content

Commit

Permalink
[example] Update all examples to new assert interface
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Mar 18, 2020
1 parent 0c3681e commit c188ded
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 66 deletions.
62 changes: 28 additions & 34 deletions examples/avr/assert/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,25 @@

#include <modm/board.hpp>
#include <modm/architecture/interface/assert.hpp>
#include <modm/architecture/interface/accessor.hpp>
#include <avr/pgmspace.h>

using modm::accessor::asFlash;

// Flash support on avr-gcc is so horribly broken.
#define IFS(s) asFlash(PSTR(s))

#define MODM_CAN_MODULE_NAME "can"
#define MODM_IOBUFFER_MODULE_NAME "iobuffer"
#define MODM_UART_MODULE_NAME "uart"

extern "C" void
modm_abandon(const char * module,
const char * location,
const char * failure,
uintptr_t context)
modm_abandon(const modm::AssertionInfo &info)
{
serialStream << IFS("Assertion '")
<< asFlash(module) << '.'
<< asFlash(location) << '.'
<< asFlash(failure)
<< IFS("' @ ") << (void *) context
<< IFS(" failed! Abandoning.") << modm::endl;
serialStream << IFS("Assertion '") << asFlash(info.name) << '\'';
if (info.context != uintptr_t(-1))
serialStream << IFS(" @ ") << (void *)info.context << IFS(" (") << (uint32_t)info.context << IFS(")");
#if MODM_ASSERTION_INFO_HAS_DESCRIPTION
serialStream << IFS(" failed!\n ") << asFlash(info.description) << IFS("\nAbandoning...\n");
#else
serialStream << IFS(" failed!\nAbandoning...\n");
#endif

Leds::setOutput();
while (true) {
Expand All @@ -44,57 +40,55 @@ modm_abandon(const char * module,
}
}


static modm::Abandonment
test_assertion_handler(const char * module,
const char * /* location */,
const char * /* failure */,
uintptr_t /* context */)
test_assertion_handler(const modm::AssertionInfo &info)
{
serialStream << IFS("#1: '") << asFlash(module) << IFS("'!") << modm::endl;
serialStream << IFS("#1: '") << asFlash(info.name) << IFS("'!") << modm::endl;
// The strings are located in FLASH!!!
if (strcmp_P(module, PSTR(MODM_IOBUFFER_MODULE_NAME)) == 0)
if (strncmp_P("io.", info.name, 3) == 0) {
serialStream << IFS("Ignoring assertion!") << modm::endl;
return modm::Abandonment::Ignore;
}
return modm::Abandonment::DontCare;
}
MODM_ASSERTION_HANDLER(test_assertion_handler);

static modm::Abandonment
test_assertion_handler2(const char * /* module */,
const char * location,
const char * /* failure */,
uintptr_t /* context */)
test_assertion_handler2(const modm::AssertionInfo &info)
{
serialStream << IFS("#2: '") << asFlash(location) << IFS("'!") << modm::endl;
serialStream << IFS("#2: '") << asFlash(info.name) << IFS("'!") << modm::endl;
return modm::Abandonment::DontCare;
}
MODM_ASSERTION_HANDLER(test_assertion_handler2);

static modm::Abandonment
test_assertion_handler3(const char * /* module */,
const char * /* location */,
const char * failure,
uintptr_t /* context */)
test_assertion_handler3(const modm::AssertionInfo &info)
{
serialStream << IFS("#3: '") << asFlash(failure) << IFS("'!") << modm::endl;
serialStream << IFS("#3: '") << asFlash(info.name) << IFS("'!") << modm::endl;
return modm::Abandonment::DontCare;
}
MODM_ASSERTION_HANDLER(test_assertion_handler3);


// ----------------------------------------------------------------------------
int main()
{
Board::initialize();
Leds::setOutput();
serialStream << IFS("Starting test...\n");

modm_assert(true, MODM_CAN_MODULE_NAME, "init", "timeout");
// only fails for debug builds, but is ignored anyways
modm_assert_continue_fail(false, "io.tx",
"The IO transmit buffer is full!");

modm_assert_debug(false, MODM_IOBUFFER_MODULE_NAME, "tx", "full");
modm_assert_continue_fail_debug(false, "uart.init", "UART init failed!");

modm_assert(false, MODM_UART_MODULE_NAME, "init", "mode");
modm_assert(false, "can.init", "CAN init timed out!");

while (true)
{
Led3::toggle();
LedD13::toggle();
modm::delayMilliseconds(500);
}
}
3 changes: 2 additions & 1 deletion examples/avr/assert/project.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<library>
<extends>modm:al-avreb-can</extends>
<extends>modm:arduino-nano</extends>
<options>
<option name="modm:build:build.path">../../../build/avr/assert</option>
<option name="modm:build:avrdude.port">/dev/tty.usbserial-14120</option>
</options>
<modules>
<module>modm:architecture:delay</module>
Expand Down
28 changes: 16 additions & 12 deletions examples/linux/assert/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,36 @@

#include <modm/platform.hpp>
#include <modm/architecture/interface/assert.hpp>

#define MODM_CAN_MODULE_NAME "can"
#define MODM_IOBUFFER_MODULE_NAME "iobuffer"
#define MODM_UART_MODULE_NAME "uart"
#include <modm/debug.hpp>

static modm::Abandonment
test_assertion_handler(const char * module,
const char * /* location */,
const char * /* failure */,
uintptr_t /* context */)
test_assertion_handler(const modm::AssertionInfo &info)
{
if (strcmp(module, MODM_IOBUFFER_MODULE_NAME) == 0)
if (strncmp(info.name, "io.", 3) == 0) {
MODM_LOG_WARNING << "Ignoring all 'io.*' assertions!" << modm::endl;
return modm::Abandonment::Ignore;
}
return modm::Abandonment::DontCare;
}
MODM_ASSERTION_HANDLER(test_assertion_handler);

static modm::Abandonment
log_assertion_handler(const modm::AssertionInfo &info)
{
MODM_LOG_DEBUG.printf("Assertion '%s' failed!\n", info.name);
return modm::Abandonment::DontCare;
}
MODM_ASSERTION_HANDLER_DEBUG(log_assertion_handler);

// ----------------------------------------------------------------------------
int
main()
{
modm_assert(true, MODM_CAN_MODULE_NAME, "init", "timeout");
modm_assert_continue_fail_debug(false, "io.tx", "IO transmit buffer is full!");

modm_assert_debug(false, MODM_IOBUFFER_MODULE_NAME, "tx", "full");
modm_assert_continue_fail_debug(false, "uart.init", "UART init failed!");

modm_assert(false, MODM_UART_MODULE_NAME, "init", "mode");
modm_assert(false, "can.init", "CAN init timed out!");

return 0;
}
39 changes: 20 additions & 19 deletions examples/stm32f469_discovery/assert/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,31 @@
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <string>

using namespace std::string_literals;
using namespace Board;

static modm::Abandonment
test_assertion_handler(const char * module,
const char * /* location */,
const char * /* failure */,
uintptr_t /* context */)
test_assertion_handler(const modm::AssertionInfo &info)
{
if (not strcmp(module, "iobuffer")) {
MODM_LOG_ERROR << "Ignoring iobuffer full!" << modm::endl;
if (not strncmp(info.name, "io.", 3)) {
MODM_LOG_ERROR << "Ignoring all io.* assertions!" << modm::endl;
return modm::Abandonment::Ignore;
}
return modm::Abandonment::DontCare;
}
MODM_ASSERTION_HANDLER(test_assertion_handler);

static modm::Abandonment
core_assertion_handler(const char * module,
const char * /* location */,
const char * failure,
uintptr_t context)
core_assertion_handler(const modm::AssertionInfo &info)
{
if (not memcmp(module, "core\0nvic\0undefined", 19)) {
MODM_LOG_ERROR.printf("Ignoring undefined IRQ handler %d!\n", context);
if (info.name == "nvic.undef"s) {
MODM_LOG_ERROR.printf("Ignoring undefined IRQ handler %d!\n", info.context);
return modm::Abandonment::Ignore;
}
if (not memcmp(module, "core\0heap", 9)) {
MODM_LOG_ERROR.printf("Ignoring 'core.heap.%s' of size 0x%x!\n", failure, context);
if ((info.name == "new"s) or (info.name == "malloc"s)) {
MODM_LOG_ERROR.printf("Ignoring '%s' of size 0x%x!\n", info.name, info.context);
return modm::Abandonment::Ignore;
}
return modm::Abandonment::DontCare;
Expand All @@ -51,10 +47,12 @@ int
main()
{
Board::initialize();
MODM_LOG_INFO << "\n=== RESTART ===\n" << modm::flush;

// trigger an IRQ with undefined handler
NVIC_EnableIRQ(RTC_Alarm_IRQn);
NVIC_SetPendingIRQ(RTC_Alarm_IRQn);
NVIC_EnableIRQ(OTG_FS_WKUP_IRQn);
NVIC_SetPendingIRQ(OTG_FS_WKUP_IRQn);


// trigger an out of memory
// we definitely don't have 32MB RAM on this board
Expand All @@ -68,13 +66,16 @@ main()

// does not fail, should not be optimized away
volatile bool true_condition = true;
modm_assert(true_condition, "can", "init", "timeout");
modm_assert(true_condition, "can.init",
"Can::init() function has timed out!");

// only fails for debug builds, but is ignored anyways
modm_assert_debug(false, "iobuffer", "tx", "full");
modm_assert_continue_fail_debug(false, "io.tx",
"The IO transmit buffer is full!");

MODM_LOG_ERROR << modm::flush;
// "accidentally" return from main, without even returning properly!
// This should be caught by the debug assert core.main.exit!
// This should be caught by the debug assert main.exit!
// while (true)
// {};
// return 0;
Expand Down

0 comments on commit c188ded

Please sign in to comment.