update Chapter07
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
#include <stm32f072xb.h>
|
||||
|
||||
#include <hal.hpp>
|
||||
#include <uart_stm32.hpp>
|
||||
|
||||
#include <retarget.hpp>
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
}
|
||||
50
Chapter07/error_handling/app/src/main_assert.cpp
Normal file
50
Chapter07/error_handling/app/src/main_assert.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
#include <stm32f072xb.h>
|
||||
|
||||
#include <hal.hpp>
|
||||
#include <uart_stm32.hpp>
|
||||
|
||||
#include <retarget.hpp>
|
||||
|
||||
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<bool> (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<uint8_t>(option::Last));
|
||||
return static_cast<option>(num);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
hal::init();
|
||||
|
||||
hal::uart_stm32 uart(USART2);
|
||||
uart.init();
|
||||
|
||||
retarget::set_stdio_uart(&uart);
|
||||
|
||||
printf("Assert example\r\n");
|
||||
|
||||
const option opt = uint8_to_option(3);
|
||||
|
||||
while(true)
|
||||
{
|
||||
}
|
||||
}
|
||||
48
Chapter07/error_handling/app/src/main_exceptions.cpp
Normal file
48
Chapter07/error_handling/app/src/main_exceptions.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
#include <stm32f072xb.h>
|
||||
|
||||
#include <hal.hpp>
|
||||
#include <uart_stm32.hpp>
|
||||
|
||||
#include <retarget.hpp>
|
||||
|
||||
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<bool> (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<uint8_t>(option::Last));
|
||||
return static_cast<option>(num);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
hal::init();
|
||||
|
||||
hal::uart_stm32 uart(USART2);
|
||||
uart.init();
|
||||
|
||||
retarget::set_stdio_uart(&uart);
|
||||
|
||||
printf("Exceptions example\r\n");
|
||||
|
||||
while(true)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user