From 3c0ac106d0ab4759516910e260fbab8c8dbdce61 Mon Sep 17 00:00:00 2001 From: Amar Mahmutbegovic Date: Tue, 2 Jul 2024 14:59:34 +0200 Subject: [PATCH] update Chapter07 --- Chapter07/error_handling/CMakeLists.txt | 4 +- Chapter07/error_handling/app/src/main.cpp | 30 ----------- .../error_handling/app/src/main_assert.cpp | 50 +++++++++++++++++++ .../app/src/main_exceptions.cpp | 48 ++++++++++++++++++ 4 files changed, 101 insertions(+), 31 deletions(-) delete mode 100644 Chapter07/error_handling/app/src/main.cpp create mode 100644 Chapter07/error_handling/app/src/main_assert.cpp create mode 100644 Chapter07/error_handling/app/src/main_exceptions.cpp diff --git a/Chapter07/error_handling/CMakeLists.txt b/Chapter07/error_handling/CMakeLists.txt index d7610b6..d9a4c12 100644 --- a/Chapter07/error_handling/CMakeLists.txt +++ b/Chapter07/error_handling/CMakeLists.txt @@ -83,6 +83,8 @@ include_directories( ${CMAKE_SOURCE_DIR}/cstdlib_support ) +set(MAIN_CPP_PATH "${CMAKE_SOURCE_DIR}/app/src/") +set(MAIN_CPP_FILE_NAME "main_assert.cpp" CACHE STRING "main") set(EXECUTABLE ${PROJECT_NAME}.elf) @@ -98,9 +100,9 @@ add_executable( platform/src/stm32f0xx_hal_msp.c platform/src/stm32f0xx_it.c platform/src/system_stm32f0xx.c - app/src/main.cpp hal/uart/src/uart_stm32.cpp cstdlib_support/retarget.cpp + ${MAIN_CPP_PATH}/${MAIN_CPP_FILE_NAME} ) target_link_libraries(${EXECUTABLE} PUBLIC) diff --git a/Chapter07/error_handling/app/src/main.cpp b/Chapter07/error_handling/app/src/main.cpp deleted file mode 100644 index 4f81498..0000000 --- a/Chapter07/error_handling/app/src/main.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include - -#include - -#include -#include - -#include - - -int main() -{ - hal::init(); - - hal::uart_stm32 uart(USART2); - uart.init(); - - uart.write("Hi from main function!\r\n"); - - retarget::set_stdio_uart(&uart); - - printf("Printing using printf\r\n"); - - printf("PC: 0x%08lX\r\n", hal::get_pc()); - - while(true) - { - } -} diff --git a/Chapter07/error_handling/app/src/main_assert.cpp b/Chapter07/error_handling/app/src/main_assert.cpp new file mode 100644 index 0000000..9ae2827 --- /dev/null +++ b/Chapter07/error_handling/app/src/main_assert.cpp @@ -0,0 +1,50 @@ +#include +#include + +#include + +#include +#include + +#include + +void log_pc_and_halt(std::uint32_t pc) { + printf("Assert at 0x%08lX\r\n", pc); + while(true) {} +} + +#define light_assert(expr) \ + (static_cast (expr) \ + ? void (0) \ + : log_pc_and_halt(hal::get_pc()) \ + ) + +enum class option : std::uint8_t { + Option1 = 0, + Option2, + Option3, + Last +}; + +option uint8_to_option(uint8_t num) { + light_assert(num < static_cast(option::Last)); + return static_cast