From 6b19dedafa937122b90221048b1c5656202a08f7 Mon Sep 17 00:00:00 2001 From: Niklas Hauser Date: Sun, 10 Nov 2024 13:48:39 +0100 Subject: [PATCH] [board] Implement modm_abandon for AVR boards --- examples/arduino_nano/printf/main.cpp | 8 ++-- .../basic/analog_read_serial/main.cpp | 2 +- .../basic/digital_read_serial/main.cpp | 2 +- .../basic/read_analog_voltage/main.cpp | 2 +- examples/avr/assert/main.cpp | 35 ++------------ examples/avr/ports/main.cpp | 46 +++++++++---------- examples/avr/qmc5883l/main.cpp | 4 +- src/modm/board/al_avreb_can/board_serial.cpp | 15 ------ src/modm/board/al_avreb_can/module.lb | 5 ++ src/modm/board/arduino_nano/module.lb | 8 +++- src/modm/board/arduino_uno/board_serial.cpp | 22 --------- src/modm/board/arduino_uno/module.lb | 5 ++ src/modm/board/board.cpp.in | 11 +++-- src/modm/board/mega_2560_pro/board_serial.cpp | 22 --------- src/modm/board/mega_2560_pro/module.lb | 5 ++ src/modm/board/module.lb | 2 +- 16 files changed, 66 insertions(+), 128 deletions(-) delete mode 100644 src/modm/board/al_avreb_can/board_serial.cpp delete mode 100644 src/modm/board/arduino_uno/board_serial.cpp delete mode 100644 src/modm/board/mega_2560_pro/board_serial.cpp diff --git a/examples/arduino_nano/printf/main.cpp b/examples/arduino_nano/printf/main.cpp index f78ab2cb36..b66765a0b9 100644 --- a/examples/arduino_nano/printf/main.cpp +++ b/examples/arduino_nano/printf/main.cpp @@ -22,20 +22,20 @@ main() modm::PeriodicTimer heartbeat(1s); // - serialStream << 32ull << modm::endl; + MODM_LOG_INFO << 32ull << modm::endl; // - serialStream << 32.0f << modm::endl; + MODM_LOG_INFO << 32.0f << modm::endl; // - serialStream.printf("hello %lu %03.3f\n", 32ul, 32.23451); + MODM_LOG_INFO.printf("hello %lu %03.3f\n", 32ul, 32.23451); uint8_t counter{0}; while (true) { if (heartbeat.execute()) { - serialStream << counter++ << modm::endl; + MODM_LOG_INFO << counter++ << modm::endl; Board::LedD13::toggle(); } } diff --git a/examples/arduino_uno/basic/analog_read_serial/main.cpp b/examples/arduino_uno/basic/analog_read_serial/main.cpp index 9563a0705a..6c6f332c5a 100644 --- a/examples/arduino_uno/basic/analog_read_serial/main.cpp +++ b/examples/arduino_uno/basic/analog_read_serial/main.cpp @@ -31,6 +31,6 @@ main() // read the input on analog pin 0 int sensorValue = Adc::readChannel(0); // print analog readout - serialStream << sensorValue << modm::endl; + MODM_LOG_INFO << sensorValue << modm::endl; } } diff --git a/examples/arduino_uno/basic/digital_read_serial/main.cpp b/examples/arduino_uno/basic/digital_read_serial/main.cpp index 48b3a574dd..b059280409 100644 --- a/examples/arduino_uno/basic/digital_read_serial/main.cpp +++ b/examples/arduino_uno/basic/digital_read_serial/main.cpp @@ -29,6 +29,6 @@ main() // read the input pin bool buttonState = PushButton::read(); // print button state - serialStream << buttonState << modm::endl; + MODM_LOG_INFO << buttonState << modm::endl; } } diff --git a/examples/arduino_uno/basic/read_analog_voltage/main.cpp b/examples/arduino_uno/basic/read_analog_voltage/main.cpp index 6f9ef12a0b..11bfcdf52c 100644 --- a/examples/arduino_uno/basic/read_analog_voltage/main.cpp +++ b/examples/arduino_uno/basic/read_analog_voltage/main.cpp @@ -33,6 +33,6 @@ main() // convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage = sensorValue * (5.0 / 1023.0); // print voltage - serialStream << voltage << "V" << modm::endl; + MODM_LOG_INFO << voltage << "V" << modm::endl; } } diff --git a/examples/avr/assert/main.cpp b/examples/avr/assert/main.cpp index 3695aa8897..5a623f6e47 100644 --- a/examples/avr/assert/main.cpp +++ b/examples/avr/assert/main.cpp @@ -14,38 +14,13 @@ using modm::accessor::asFlash; -// Flash support on avr-gcc is so horribly broken. -#define IFS(s) asFlash(PSTR(s)) - -extern "C" void -modm_abandon(const modm::AssertionInfo &info) -{ - 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) { - Leds::write(1); - modm::delay(20ms); - Leds::write(0); - modm::delay(180ms); - } -} - - static modm::Abandonment test_assertion_handler(const modm::AssertionInfo &info) { - serialStream << IFS("#1: '") << asFlash(info.name) << IFS("'!") << modm::endl; + MODM_LOG_INFO << IFSS("#1: '") << asFlash(info.name) << IFSS("'!") << modm::endl; // The strings are located in FLASH!!! if (strncmp_P("io.", info.name, 3) == 0) { - serialStream << IFS("Ignoring assertion!") << modm::endl; + MODM_LOG_INFO << IFSS("Ignoring assertion!") << modm::endl; return modm::Abandonment::Ignore; } return modm::Abandonment::DontCare; @@ -55,7 +30,7 @@ MODM_ASSERTION_HANDLER(test_assertion_handler); static modm::Abandonment test_assertion_handler2(const modm::AssertionInfo &info) { - serialStream << IFS("#2: '") << asFlash(info.name) << IFS("'!") << modm::endl; + MODM_LOG_INFO << IFSS("#2: '") << asFlash(info.name) << IFSS("'!") << modm::endl; return modm::Abandonment::DontCare; } MODM_ASSERTION_HANDLER(test_assertion_handler2); @@ -63,7 +38,7 @@ MODM_ASSERTION_HANDLER(test_assertion_handler2); static modm::Abandonment test_assertion_handler3(const modm::AssertionInfo &info) { - serialStream << IFS("#3: '") << asFlash(info.name) << IFS("'!") << modm::endl; + MODM_LOG_INFO << IFSS("#3: '") << asFlash(info.name) << IFSS("'!") << modm::endl; return modm::Abandonment::DontCare; } MODM_ASSERTION_HANDLER(test_assertion_handler3); @@ -74,7 +49,7 @@ int main() { Board::initialize(); Leds::setOutput(); - serialStream << IFS("Starting test...\n"); + MODM_LOG_INFO << IFSS("Starting test...\n"); // only fails for debug builds, but is ignored anyways modm_assert_continue_fail(false, "io.tx", diff --git a/examples/avr/ports/main.cpp b/examples/avr/ports/main.cpp index 302f6b3948..0753c8f3aa 100644 --- a/examples/avr/ports/main.cpp +++ b/examples/avr/ports/main.cpp @@ -19,27 +19,27 @@ struct DebugGpioPort : public Port static void dumpShifts() { for (int ii=0; ii<7; ii++) { - serialStream << char(ii+'A') << " {"; + MODM_LOG_INFO << char(ii+'A') << " {"; for (int jj=0; jj= 0) { serialStream << p; } - else { serialStream << " "; } - serialStream << " "; - if (jj != Port::width - 1) { serialStream << ","; } + MODM_LOG_INFO << " "; + if (p >= 0) { MODM_LOG_INFO << p; } + else { MODM_LOG_INFO << " "; } + MODM_LOG_INFO << " "; + if (jj != Port::width - 1) { MODM_LOG_INFO << ","; } } - serialStream << "}" << modm::endl; + MODM_LOG_INFO << "}" << modm::endl; } } static void dumpMasks() { for (int ii=0; ii<7; ii++) { - serialStream << char(ii+'A') << " " << modm::bin << Port::mask(ii) << modm::endl; + MODM_LOG_INFO << char(ii+'A') << " " << modm::bin << Port::mask(ii) << modm::endl; } - serialStream << modm::endl; + MODM_LOG_INFO << modm::endl; for (int ii=0; ii<7; ii++) { - serialStream << char(ii+'A') << " " << modm::bin << Port::inverted(ii) << modm::endl; + MODM_LOG_INFO << char(ii+'A') << " " << modm::bin << Port::inverted(ii) << modm::endl; } } }; @@ -64,23 +64,23 @@ int main() static_assert(PinGroup3::number_of_ports == 1); static_assert(PinGroup4::number_of_ports == 1); - DebugGpioPort::dumpMasks(); serialStream << modm::endl; - DebugGpioPort::dumpMasks(); serialStream << modm::endl; - DebugGpioPort::dumpMasks(); serialStream << modm::endl; - DebugGpioPort::dumpMasks(); serialStream << modm::endl; + DebugGpioPort::dumpMasks(); MODM_LOG_INFO << modm::endl; + DebugGpioPort::dumpMasks(); MODM_LOG_INFO << modm::endl; + DebugGpioPort::dumpMasks(); MODM_LOG_INFO << modm::endl; + DebugGpioPort::dumpMasks(); MODM_LOG_INFO << modm::endl; - DebugGpioPort::dumpShifts(); serialStream << modm::endl; - DebugGpioPort::dumpShifts(); serialStream << modm::endl; + DebugGpioPort::dumpShifts(); MODM_LOG_INFO << modm::endl; + DebugGpioPort::dumpShifts(); MODM_LOG_INFO << modm::endl; - PinGroup2::setInput(); serialStream << modm::bin << PinGroup2::read() << modm::endl; - PinGroup3::setInput(); serialStream << modm::bin << PinGroup3::read() << modm::endl; - PinGroup4::setInput(); serialStream << modm::bin << PinGroup4::read() << modm::endl; - serialStream << modm::endl; + PinGroup2::setInput(); MODM_LOG_INFO << modm::bin << PinGroup2::read() << modm::endl; + PinGroup3::setInput(); MODM_LOG_INFO << modm::bin << PinGroup3::read() << modm::endl; + PinGroup4::setInput(); MODM_LOG_INFO << modm::bin << PinGroup4::read() << modm::endl; + MODM_LOG_INFO << modm::endl; PinGroup::setOutput(modm::Gpio::High); modm::delay(1s); const auto fn_report = []() { - serialStream << modm::bin << PinGroup::read() << modm::endl; modm::delay(200ms); + MODM_LOG_INFO << modm::bin << PinGroup::read() << modm::endl; modm::delay(200ms); }; while (true) @@ -91,14 +91,14 @@ int main() PinGroup::write(0b00111); fn_report(); PinGroup::write(0b01111); fn_report(); PinGroup::write(0b11111); fn_report(); - serialStream << modm::endl; + MODM_LOG_INFO << modm::endl; PinGroup::reset(); Pin0::set(); fn_report(); Pin1::set(); fn_report(); Pin2::set(); fn_report(); Pin3::set(); fn_report(); - serialStream << modm::endl; + MODM_LOG_INFO << modm::endl; // while (true); } diff --git a/examples/avr/qmc5883l/main.cpp b/examples/avr/qmc5883l/main.cpp index ebfe0bb8bb..124962fcf1 100644 --- a/examples/avr/qmc5883l/main.cpp +++ b/examples/avr/qmc5883l/main.cpp @@ -54,8 +54,8 @@ main() << modm::endl; } else { - serialStream << "readDataBlocking(): Error: " << uint8_t(I2cMaster::getErrorState()) - << modm::endl; + MODM_LOG_INFO << "readDataBlocking(): Error: " << uint8_t(I2cMaster::getErrorState()) + << modm::endl; } } } diff --git a/src/modm/board/al_avreb_can/board_serial.cpp b/src/modm/board/al_avreb_can/board_serial.cpp deleted file mode 100644 index 2ead355220..0000000000 --- a/src/modm/board/al_avreb_can/board_serial.cpp +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2016-2018, Niklas Hauser - * - * This file is part of the modm project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -// ---------------------------------------------------------------------------- - -#include "board.hpp" - -Board::LoggerDevice loggerDevice; -modm::IOStream serialStream(loggerDevice); diff --git a/src/modm/board/al_avreb_can/module.lb b/src/modm/board/al_avreb_can/module.lb index 2cfd86f282..4a563e0f48 100644 --- a/src/modm/board/al_avreb_can/module.lb +++ b/src/modm/board/al_avreb_can/module.lb @@ -34,6 +34,11 @@ def prepare(module, options): def build(env): env.outbasepath = "modm/src/modm/board" + env.substitutions = { + "with_logger": True, + "with_assert": env.has_module(":architecture:assert") + } + env.template("../board.cpp.in", "board.cpp") env.copy('.') env.collect(":build:default.avrdude.port", "usb"); env.collect(":build:default.avrdude.programmer", "avrispmkII"); diff --git a/src/modm/board/arduino_nano/module.lb b/src/modm/board/arduino_nano/module.lb index 188457cf71..77318e372a 100644 --- a/src/modm/board/arduino_nano/module.lb +++ b/src/modm/board/arduino_nano/module.lb @@ -30,7 +30,13 @@ def prepare(module, options): return True def build(env): - env.outbasepath = "modm/src/modm/" + env.outbasepath = "modm/src/modm/board" + env.substitutions = { + "with_logger": True, + "with_assert": env.has_module(":architecture:assert") + } + env.template("../board.cpp.in", "board.cpp") + env.outbasepath = "modm/src/modm" env.copy(localpath("../arduino_uno"), "board") env.collect(":build:default.avrdude.programmer", "arduino"); env.collect(":build:default.avrdude.baudrate", "115200"); diff --git a/src/modm/board/arduino_uno/board_serial.cpp b/src/modm/board/arduino_uno/board_serial.cpp deleted file mode 100644 index ef7c3b9e45..0000000000 --- a/src/modm/board/arduino_uno/board_serial.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2016-2018, Niklas Hauser - * - * This file is part of the modm project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -// ---------------------------------------------------------------------------- - -#include "board.hpp" - -// Create an IODeviceWrapper around the Uart Peripheral we want to use -Board::LoggerDevice loggerDevice; - -// Set all four logger streams to use the UART -modm::IOStream serialStream(loggerDevice); -modm::log::Logger modm::log::debug(loggerDevice); -modm::log::Logger modm::log::info(loggerDevice); -modm::log::Logger modm::log::warning(loggerDevice); -modm::log::Logger modm::log::error(loggerDevice); diff --git a/src/modm/board/arduino_uno/module.lb b/src/modm/board/arduino_uno/module.lb index b54f21189d..54c8febbda 100644 --- a/src/modm/board/arduino_uno/module.lb +++ b/src/modm/board/arduino_uno/module.lb @@ -31,6 +31,11 @@ def prepare(module, options): def build(env): env.outbasepath = "modm/src/modm/board" + env.substitutions = { + "with_logger": True, + "with_assert": env.has_module(":architecture:assert") + } + env.template("../board.cpp.in", "board.cpp") env.copy('.') env.collect(":build:default.avrdude.programmer", "arduino"); env.collect(":build:default.avrdude.baudrate", "115200"); diff --git a/src/modm/board/board.cpp.in b/src/modm/board/board.cpp.in index 890eb10ca8..7f47316808 100644 --- a/src/modm/board/board.cpp.in +++ b/src/modm/board/board.cpp.in @@ -11,6 +11,7 @@ #include "board.hpp" #include +#include %% if with_assert #include %% endif @@ -37,15 +38,15 @@ modm_extern_c void modm_abandon(const modm::AssertionInfo &info) { %% if with_logger - MODM_LOG_ERROR << "Assertion '" << info.name << "'"; + MODM_LOG_ERROR << IFSS("Assertion '") << modm::accessor::asFlash(info.name) << IFSS("'"); if (info.context != uintptr_t(-1)) { - MODM_LOG_ERROR << " @ " << (void *) info.context << - " (" << (uint32_t) info.context << ")"; + MODM_LOG_ERROR << IFSS(" @ ") << (void *) info.context << + IFSS(" (") << (uint32_t) info.context << IFSS(")"); } #if MODM_ASSERTION_INFO_HAS_DESCRIPTION - MODM_LOG_ERROR << " failed!\n " << info.description << "\nAbandoning...\n"; + MODM_LOG_ERROR << IFSS(" failed!\n ") << modm::accessor::asFlash(info.description) << IFSS("\nAbandoning...\n"); #else - MODM_LOG_ERROR << " failed!\nAbandoning...\n"; + MODM_LOG_ERROR << IFSS(" failed!\nAbandoning...\n"); #endif %% else (void)info; diff --git a/src/modm/board/mega_2560_pro/board_serial.cpp b/src/modm/board/mega_2560_pro/board_serial.cpp deleted file mode 100644 index ef7c3b9e45..0000000000 --- a/src/modm/board/mega_2560_pro/board_serial.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2016-2018, Niklas Hauser - * - * This file is part of the modm project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -// ---------------------------------------------------------------------------- - -#include "board.hpp" - -// Create an IODeviceWrapper around the Uart Peripheral we want to use -Board::LoggerDevice loggerDevice; - -// Set all four logger streams to use the UART -modm::IOStream serialStream(loggerDevice); -modm::log::Logger modm::log::debug(loggerDevice); -modm::log::Logger modm::log::info(loggerDevice); -modm::log::Logger modm::log::warning(loggerDevice); -modm::log::Logger modm::log::error(loggerDevice); diff --git a/src/modm/board/mega_2560_pro/module.lb b/src/modm/board/mega_2560_pro/module.lb index b01371e4fa..c1cf13d569 100644 --- a/src/modm/board/mega_2560_pro/module.lb +++ b/src/modm/board/mega_2560_pro/module.lb @@ -38,6 +38,11 @@ def prepare(module, options): def build(env): env.outbasepath = "modm/src/modm/board" + env.substitutions = { + "with_logger": True, + "with_assert": env.has_module(":architecture:assert") + } + env.template("../board.cpp.in", "board.cpp") env.copy('.') env.collect(":build:default.avrdude.programmer", "stk500v2"); env.collect(":build:default.avrdude.baudrate", "115200"); diff --git a/src/modm/board/module.lb b/src/modm/board/module.lb index 2c3876c439..4a20cbf00d 100644 --- a/src/modm/board/module.lb +++ b/src/modm/board/module.lb @@ -16,7 +16,7 @@ def init(module): def prepare(module, options): # The modm_abandon function uses delay functions - module.depends(":architecture:delay", ":architecture:assert") + module.depends(":architecture:delay", ":architecture:assert", ":architecture:accessor") return True