From 9390f7830bb3b622fdcb6f1ecb941f6ec13b91fc Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sun, 12 Apr 2020 18:45:33 +0200 Subject: [PATCH] Add small blink example as bootloader payload test --- .gitmodules | 3 + blink/blink.cppproj | 250 ++++++++++++++++++++++++++++++++++++++ blink/bootloader.cpp | 46 +++++++ blink/bootloader.hpp | 24 ++++ blink/clock.hpp | 4 + blink/io | 1 + blink/main.cpp | 30 +++++ bootloader.atsln | 6 + stk500v2/stk500v2.cppproj | 238 ++++++++++++++++++------------------ tsb/tsb.cppproj | 15 +++ 10 files changed, 498 insertions(+), 119 deletions(-) create mode 100644 blink/blink.cppproj create mode 100644 blink/bootloader.cpp create mode 100644 blink/bootloader.hpp create mode 100644 blink/clock.hpp create mode 160000 blink/io create mode 100644 blink/main.cpp diff --git a/.gitmodules b/.gitmodules index c350145..0264bc5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -22,3 +22,6 @@ [submodule "stk500v2/flash"] path = stk500v2/flash url = git@git.blackmark.me:avr/flash.git +[submodule "blink/io"] + path = blink/io + url = git@git.blackmark.me:avr/io.git diff --git a/blink/blink.cppproj b/blink/blink.cppproj new file mode 100644 index 0000000..0502e00 --- /dev/null +++ b/blink/blink.cppproj @@ -0,0 +1,250 @@ + + + + 2.0 + 7.0 + com.Atmel.AVRGCC8.CPP + {d887fc8e-ee68-4248-8382-92dbc9a54145} + ATmega328P + none + Executable + CPP + $(MSBuildProjectName) + .elf + $(MSBuildProjectDirectory)\$(Configuration) + blink + blink + blink + avr-g++-9.1.0 + true + false + true + true + 0x20000000 + + true + exception_table + 2 + 0 + 0 + + com.atmel.avrdbg.tool.atmelice + J41800099437 + 0x1E950F + + + + 125000 + + ISP + + com.atmel.avrdbg.tool.stk500 + + + STK500 + + ISP + 125000 + + + + + + + + + + + + + + + + 125000 + + ISP + + com.atmel.avrdbg.tool.atmelice + J41800099437 + Atmel-ICE + + + + + 125000 + + + + + custom + + + Custom Programming Tool + + + + + + + + + com.atmel.avrdbg.tool.simulator + + + Simulator + + + + + \Debug\blink.lss + + + .lss + ^\s*(?<address>[a-f0-9]*):\s*.*$ + true + address + $pc + + + + + + + + + -mmcu=atmega328p + True + True + True + True + False + True + True + + + NDEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize for size (-Os) + True + True + True + True + -fno-threadsafe-statics -std=c11 + True + True + + + NDEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize for size (-Os) + True + True + True + -fno-threadsafe-statics -Wextra -std=c++17 + + + libm + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + + + + + + + -mmcu=atmega328p + True + True + True + True + False + True + True + + + DEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize (-O1) + True + Maximum (-g3) + True + True + True + -fno-threadsafe-statics -std=c11 + True + True + + + DEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize (-O1) + True + Maximum (-g3) + True + True + -fno-threadsafe-statics -Wextra -std=c++17 + + + libm + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Default (-Wa,-g) + + + + + + compile + + + compile + + + compile + + + compile + + + compile + + + + + + + \ No newline at end of file diff --git a/blink/bootloader.cpp b/blink/bootloader.cpp new file mode 100644 index 0000000..f34ad74 --- /dev/null +++ b/blink/bootloader.cpp @@ -0,0 +1,46 @@ +#include "bootloader.hpp" + +#include +#include +#include + +namespace { + +typedef void (*jmp_fn)() __attribute__((noreturn)); + +jmp_fn boot = reinterpret_cast(0x0000); +jmp_fn bootloader = reinterpret_cast(0x7800 / 2); + +} // namespace + +bool Bootloader::handleReset() +{ + wdt_reset(); + uint8_t mcuStatus = MCUSR; + MCUSR &= ~(1 << WDRF); + wdt_disable(); + return (mcuStatus & (1 << WDRF)); +} + +void Bootloader::reset() +{ + wdt_enable(WDTO_15MS); + while (true) + ; +} + +bool Bootloader::check() +{ + if (pgm_read_byte(reinterpret_cast(bootloader) * 2) != 0xFF) + return true; + + return false; +} + +void Bootloader::call() +{ + if (check()) + bootloader(); + else + boot(); +} diff --git a/blink/bootloader.hpp b/blink/bootloader.hpp new file mode 100644 index 0000000..8f7574d --- /dev/null +++ b/blink/bootloader.hpp @@ -0,0 +1,24 @@ +#pragma once + +class Bootloader { + public: + template + static inline void init(Fn callback) + { + if (handleReset()) { + callback(); + call(); + } + } + + static inline void enter() + { + reset(); + } + + private: + static bool handleReset(); + static void reset(); + static bool check(); + static void call(); +}; diff --git a/blink/clock.hpp b/blink/clock.hpp new file mode 100644 index 0000000..9d22bb6 --- /dev/null +++ b/blink/clock.hpp @@ -0,0 +1,4 @@ +#pragma once + +#define F_CPU 18'432'000 +#include diff --git a/blink/io b/blink/io new file mode 160000 index 0000000..80de36e --- /dev/null +++ b/blink/io @@ -0,0 +1 @@ +Subproject commit 80de36ee7ee3e6b0842d5eaee81d54062cb496b2 diff --git a/blink/main.cpp b/blink/main.cpp new file mode 100644 index 0000000..0bedfa3 --- /dev/null +++ b/blink/main.cpp @@ -0,0 +1,30 @@ +#include "clock.hpp" + +#include "io/io.hpp" + +#include "bootloader.hpp" + +int main() +{ + io::Pin ledPin; + ledPin.dir(io::Dir::OUT); + ledPin = false; + + Bootloader::init([&ledPin]() { + for (uint8_t i = 0; i < 10; ++i) { + ledPin = true; + _delay_ms(50); + ledPin = false; + _delay_ms(50); + } + }); + + for (uint8_t i = 0; i < 10; ++i) { + ledPin.toggle(); + _delay_ms(1000); + } + + Bootloader::enter(); + + return 0; +} diff --git a/bootloader.atsln b/bootloader.atsln index 5e98c26..bd1171f 100644 --- a/bootloader.atsln +++ b/bootloader.atsln @@ -7,6 +7,8 @@ Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "tsb", "tsb\tsb.cppproj", "{ EndProject Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "stk500v2", "stk500v2\stk500v2.cppproj", "{19798CCE-5D96-40E9-B769-D209715DCE0C}" EndProject +Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "blink", "blink\blink.cppproj", "{D887FC8E-EE68-4248-8382-92DBC9A54145}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|AVR = Debug|AVR @@ -21,6 +23,10 @@ Global {19798CCE-5D96-40E9-B769-D209715DCE0C}.Debug|AVR.Build.0 = Debug|AVR {19798CCE-5D96-40E9-B769-D209715DCE0C}.Release|AVR.ActiveCfg = Release|AVR {19798CCE-5D96-40E9-B769-D209715DCE0C}.Release|AVR.Build.0 = Release|AVR + {D887FC8E-EE68-4248-8382-92DBC9A54145}.Debug|AVR.ActiveCfg = Debug|AVR + {D887FC8E-EE68-4248-8382-92DBC9A54145}.Debug|AVR.Build.0 = Debug|AVR + {D887FC8E-EE68-4248-8382-92DBC9A54145}.Release|AVR.ActiveCfg = Release|AVR + {D887FC8E-EE68-4248-8382-92DBC9A54145}.Release|AVR.Build.0 = Release|AVR EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/stk500v2/stk500v2.cppproj b/stk500v2/stk500v2.cppproj index 5f90607..da607e9 100644 --- a/stk500v2/stk500v2.cppproj +++ b/stk500v2/stk500v2.cppproj @@ -112,130 +112,130 @@ - -mmcu=atmega328p - True - True - True - True - False - True - True - - - NDEBUG - - - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - Optimize for size (-Os) - True - True - True - True - -fno-threadsafe-statics -std=c11 - True - True - - - NDEBUG - - - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - Optimize for size (-Os) - True - True - True - -fno-threadsafe-statics -Wextra -std=c++17 - True - - - libm - - - - - .text=0x3C00 - - - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - + -mmcu=atmega328p + True + True + True + True + False + True + True + + + NDEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize for size (-Os) + True + True + True + True + -fno-threadsafe-statics -std=c11 + True + True + + + NDEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize for size (-Os) + True + True + True + -fno-threadsafe-statics -Wextra -std=c++17 + True + + + libm + + + + + .text=0x3C00 + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + - -mmcu=atmega328p - True - True - True - True - False - True - True - - - DEBUG - - - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - Optimize (-O1) - True - Maximum (-g3) - True - True - True - -fno-threadsafe-statics -std=c11 - True - True - - - DEBUG - - - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - Optimize debugging experience (-Og) - True - Maximum (-g3) - True - True - -fno-threadsafe-statics -Wextra -std=c++17 - True - - - libm - - - - - .text=0x3800 - - - - - %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include - - - Default (-Wa,-g) - + -mmcu=atmega328p + True + True + True + True + False + True + True + + + DEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize (-O1) + True + Maximum (-g3) + True + True + True + -fno-threadsafe-statics -std=c11 + True + True + + + DEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Optimize debugging experience (-Og) + True + Maximum (-g3) + True + True + -fno-threadsafe-statics -Wextra -std=c++17 + True + + + libm + + + + + .text=0x3800 + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + Default (-Wa,-g) + diff --git a/tsb/tsb.cppproj b/tsb/tsb.cppproj index fa2d942..96b66b0 100644 --- a/tsb/tsb.cppproj +++ b/tsb/tsb.cppproj @@ -93,6 +93,21 @@ Simulator + + + + \Debug\tsb.lss + + + .lss + ^\s*(?<address>[a-f0-9]*):\s*.*$ + true + address + $pc + + + +