update exceptions example
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
{
|
{
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"span": "cpp"
|
"span": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"string_view": "cpp"
|
||||||
},
|
},
|
||||||
"C_Cpp.errorSquiggles": "disabled"
|
"C_Cpp.errorSquiggles": "disabled"
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
#include <stm32f072xb.h>
|
#include <stm32f072xb.h>
|
||||||
|
|
||||||
@@ -8,28 +10,16 @@
|
|||||||
|
|
||||||
#include <retarget.hpp>
|
#include <retarget.hpp>
|
||||||
|
|
||||||
void log_pc_and_halt(std::uint32_t pc) {
|
template <class T, std::size_t N> struct ring_buffer {
|
||||||
printf("Assert at 0x%08lX\r\n", pc);
|
|
||||||
while(true) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
#define light_assert(expr) \
|
std::array<T, N> arr;
|
||||||
(static_cast<bool> (expr) \
|
std::size_t write_idx = 0; // Index of the next element to write (push)
|
||||||
? void (0) \
|
|
||||||
: log_pc_and_halt(hal::get_pc()) \
|
|
||||||
)
|
|
||||||
|
|
||||||
enum class option : std::uint8_t {
|
void push(T t) {
|
||||||
Option1 = 0,
|
arr.at(write_idx++) = t;
|
||||||
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()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -39,7 +29,18 @@ int main()
|
|||||||
uart.init();
|
uart.init();
|
||||||
|
|
||||||
retarget::set_stdio_uart(&uart);
|
retarget::set_stdio_uart(&uart);
|
||||||
|
|
||||||
|
printf("Exceptions example\r\n");
|
||||||
|
|
||||||
|
std::set_terminate([]() {
|
||||||
|
printf("Unhandled exception!\r\n");
|
||||||
|
while(true){}
|
||||||
|
});
|
||||||
|
|
||||||
|
std::array<int, 4> arr;
|
||||||
|
|
||||||
|
arr.at(5) = 6;
|
||||||
|
|
||||||
printf("Exceptions example\r\n");
|
printf("Exceptions example\r\n");
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
|
|||||||
Reference in New Issue
Block a user