From 8203a24f33a4034d68d8dca94053eb4a23b03514 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sun, 19 Jul 2026 13:12:56 +0200 Subject: [PATCH] tsb: slim the port branch to the libavr reimplementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit main carried the whole pre-libavr tree beside the port: the other-bootloader directories (blink, stk500v2), the Atmel Studio solution/project, and — dead in the tsb dir itself — four submodule links to the superseded io/flash/uart/type libraries the libavr sources never include. None are build inputs; CMake drives the three variants through FetchContent. master keeps the full legacy tree untouched. Co-Authored-By: Claude Opus 4.8 --- .gitmodules | 27 -- blink/blink.cppproj | 239 --------------- blink/bootloader.cpp | 46 --- blink/bootloader.hpp | 24 -- blink/clock.hpp | 5 - blink/io | 1 - blink/main.cpp | 30 -- bootloader.atsln | 34 --- stk500v2/clock.hpp | 5 - stk500v2/command.hpp | 113 ------- stk500v2/flash | 1 - stk500v2/io | 1 - stk500v2/main.cpp | 611 -------------------------------------- stk500v2/stk500v2.cppproj | 275 ----------------- stk500v2/type | 1 - stk500v2/uart | 1 - tsb/clock.hpp | 4 - tsb/flash | 1 - tsb/io | 1 - tsb/tsb.cppproj | 263 ---------------- tsb/type | 1 - tsb/uart | 1 - 22 files changed, 1685 deletions(-) delete mode 100644 .gitmodules delete mode 100644 blink/blink.cppproj delete mode 100644 blink/bootloader.cpp delete mode 100644 blink/bootloader.hpp delete mode 100644 blink/clock.hpp delete mode 160000 blink/io delete mode 100644 blink/main.cpp delete mode 100644 bootloader.atsln delete mode 100644 stk500v2/clock.hpp delete mode 100644 stk500v2/command.hpp delete mode 160000 stk500v2/flash delete mode 160000 stk500v2/io delete mode 100644 stk500v2/main.cpp delete mode 100644 stk500v2/stk500v2.cppproj delete mode 160000 stk500v2/type delete mode 160000 stk500v2/uart delete mode 100644 tsb/clock.hpp delete mode 160000 tsb/flash delete mode 160000 tsb/io delete mode 100644 tsb/tsb.cppproj delete mode 160000 tsb/type delete mode 160000 tsb/uart diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 0264bc5..0000000 --- a/.gitmodules +++ /dev/null @@ -1,27 +0,0 @@ -[submodule "tsb/io"] - path = tsb/io - url = git@git.blackmark.me:avr/io.git -[submodule "tsb/flash"] - path = tsb/flash - url = git@git.blackmark.me:avr/flash.git -[submodule "tsb/uart"] - path = tsb/uart - url = git@git.blackmark.me:avr/uart.git -[submodule "tsb/type"] - path = tsb/type - url = git@git.blackmark.me:avr/type.git -[submodule "stk500v2/type"] - path = stk500v2/type - url = git@git.blackmark.me:avr/type.git -[submodule "stk500v2/io"] - path = stk500v2/io - url = git@git.blackmark.me:avr/io.git -[submodule "stk500v2/uart"] - path = stk500v2/uart - url = git@git.blackmark.me:avr/uart.git -[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 deleted file mode 100644 index dfbf1b4..0000000 --- a/blink/blink.cppproj +++ /dev/null @@ -1,239 +0,0 @@ - - - - 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 - - - - - - - - - - -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 deleted file mode 100644 index f34ad74..0000000 --- a/blink/bootloader.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#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 deleted file mode 100644 index 8f7574d..0000000 --- a/blink/bootloader.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#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 deleted file mode 100644 index 0eaf2c2..0000000 --- a/blink/clock.hpp +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -//#define F_CPU 18'432'000 -#define F_CPU 16'000'000 -#include diff --git a/blink/io b/blink/io deleted file mode 160000 index 80de36e..0000000 --- a/blink/io +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 80de36ee7ee3e6b0842d5eaee81d54062cb496b2 diff --git a/blink/main.cpp b/blink/main.cpp deleted file mode 100644 index 0bedfa3..0000000 --- a/blink/main.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#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 deleted file mode 100644 index bd1171f..0000000 --- a/bootloader.atsln +++ /dev/null @@ -1,34 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Atmel Studio Solution File, Format Version 11.00 -VisualStudioVersion = 14.0.23107.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "tsb", "tsb\tsb.cppproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}" -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 - Release|AVR = Release|AVR - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.ActiveCfg = Debug|AVR - {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.Build.0 = Debug|AVR - {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.ActiveCfg = Release|AVR - {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.Build.0 = Release|AVR - {19798CCE-5D96-40E9-B769-D209715DCE0C}.Debug|AVR.ActiveCfg = Debug|AVR - {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 - EndGlobalSection -EndGlobal diff --git a/stk500v2/clock.hpp b/stk500v2/clock.hpp deleted file mode 100644 index 0eaf2c2..0000000 --- a/stk500v2/clock.hpp +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -//#define F_CPU 18'432'000 -#define F_CPU 16'000'000 -#include diff --git a/stk500v2/command.hpp b/stk500v2/command.hpp deleted file mode 100644 index 68742a1..0000000 --- a/stk500v2/command.hpp +++ /dev/null @@ -1,113 +0,0 @@ -#pragma once - -#include - -////////////////////////////////////////////////////////////////////////// -// STK message constants - -static constexpr uint8_t MESSAGE_START = 0x1B; // ASCII ESC -static constexpr uint8_t TOKEN = 0x0E; - -////////////////////////////////////////////////////////////////////////// -// STK general command constants - -static constexpr uint8_t CMD_SIGN_ON = 0x01; -static constexpr uint8_t CMD_SET_PARAMETER = 0x02; -static constexpr uint8_t CMD_GET_PARAMETER = 0x03; -static constexpr uint8_t CMD_SET_DEVICE_PARAMETERS = 0x04; -static constexpr uint8_t CMD_OSCCAL = 0x05; -static constexpr uint8_t CMD_LOAD_ADDRESS = 0x06; -static constexpr uint8_t CMD_FIRMWARE_UPGRADE = 0x07; - -////////////////////////////////////////////////////////////////////////// -// STK ISP command constants - -static constexpr uint8_t CMD_ENTER_PROGMODE_ISP = 0x10; -static constexpr uint8_t CMD_LEAVE_PROGMODE_ISP = 0x11; -static constexpr uint8_t CMD_CHIP_ERASE_ISP = 0x12; -static constexpr uint8_t CMD_PROGRAM_FLASH_ISP = 0x13; -static constexpr uint8_t CMD_READ_FLASH_ISP = 0x14; -static constexpr uint8_t CMD_PROGRAM_EEPROM_ISP = 0x15; -static constexpr uint8_t CMD_READ_EEPROM_ISP = 0x16; -static constexpr uint8_t CMD_PROGRAM_FUSE_ISP = 0x17; -static constexpr uint8_t CMD_READ_FUSE_ISP = 0x18; -static constexpr uint8_t CMD_PROGRAM_LOCK_ISP = 0x19; -static constexpr uint8_t CMD_READ_LOCK_ISP = 0x1A; -static constexpr uint8_t CMD_READ_SIGNATURE_ISP = 0x1B; -static constexpr uint8_t CMD_READ_OSCCAL_ISP = 0x1C; -static constexpr uint8_t CMD_SPI_MULTI = 0x1D; - -////////////////////////////////////////////////////////////////////////// -// STK PP command constants - -static constexpr uint8_t CMD_ENTER_PROGMODE_PP = 0x20; -static constexpr uint8_t CMD_LEAVE_PROGMODE_PP = 0x21; -static constexpr uint8_t CMD_CHIP_ERASE_PP = 0x22; -static constexpr uint8_t CMD_PROGRAM_FLASH_PP = 0x23; -static constexpr uint8_t CMD_READ_FLASH_PP = 0x24; -static constexpr uint8_t CMD_PROGRAM_EEPROM_PP = 0x25; -static constexpr uint8_t CMD_READ_EEPROM_PP = 0x26; -static constexpr uint8_t CMD_PROGRAM_FUSE_PP = 0x27; -static constexpr uint8_t CMD_READ_FUSE_PP = 0x28; -static constexpr uint8_t CMD_PROGRAM_LOCK_PP = 0x29; -static constexpr uint8_t CMD_READ_LOCK_PP = 0x2A; -static constexpr uint8_t CMD_READ_SIGNATURE_PP = 0x2B; -static constexpr uint8_t CMD_READ_OSCCAL_PP = 0x2C; - -static constexpr uint8_t CMD_SET_CONTROL_STACK = 0x2D; - -////////////////////////////////////////////////////////////////////////// -// STK HVSP command constants - -static constexpr uint8_t CMD_ENTER_PROGMODE_HVSP = 0x30; -static constexpr uint8_t CMD_LEAVE_PROGMODE_HVSP = 0x31; -static constexpr uint8_t CMD_CHIP_ERASE_HVSP = 0x32; -static constexpr uint8_t CMD_PROGRAM_FLASH_HVSP = 0x33; -static constexpr uint8_t CMD_READ_FLASH_HVSP = 0x34; -static constexpr uint8_t CMD_PROGRAM_EEPROM_HVSP = 0x35; -static constexpr uint8_t CMD_READ_EEPROM_HVSP = 0x36; -static constexpr uint8_t CMD_PROGRAM_FUSE_HVSP = 0x37; -static constexpr uint8_t CMD_READ_FUSE_HVSP = 0x38; -static constexpr uint8_t CMD_PROGRAM_LOCK_HVSP = 0x39; -static constexpr uint8_t CMD_READ_LOCK_HVSP = 0x3A; -static constexpr uint8_t CMD_READ_SIGNATURE_HVSP = 0x3B; -static constexpr uint8_t CMD_READ_OSCCAL_HVSP = 0x3C; - -////////////////////////////////////////////////////////////////////////// -// STK status constants - -// Success -static constexpr uint8_t STATUS_CMD_OK = 0x00; - -// Warnings -static constexpr uint8_t STATUS_CMD_TOUT = 0x80; -static constexpr uint8_t STATUS_RDY_BSY_TOUT = 0x81; -static constexpr uint8_t STATUS_SET_PARAM_MISSING = 0x82; - -// Errors -static constexpr uint8_t STATUS_CMD_FAILED = 0xC0; -static constexpr uint8_t STATUS_CKSUM_ERROR = 0xC1; -static constexpr uint8_t STATUS_CMD_UNKNOWN = 0xC9; - -////////////////////////////////////////////////////////////////////////// -// STK parameter constants -static constexpr uint8_t PARAM_BUILD_NUMBER_LOW = 0x80; -static constexpr uint8_t PARAM_BUILD_NUMBER_HIGH = 0x81; -static constexpr uint8_t PARAM_HW_VER = 0x90; -static constexpr uint8_t PARAM_SW_MAJOR = 0x91; -static constexpr uint8_t PARAM_SW_MINOR = 0x92; -static constexpr uint8_t PARAM_VTARGET = 0x94; -static constexpr uint8_t PARAM_VADJUST = 0x95; -static constexpr uint8_t PARAM_OSC_PSCALE = 0x96; -static constexpr uint8_t PARAM_OSC_CMATCH = 0x97; -static constexpr uint8_t PARAM_SCK_DURATION = 0x98; -static constexpr uint8_t PARAM_TOPCARD_DETECT = 0x9A; -static constexpr uint8_t PARAM_STATUS = 0x9C; -static constexpr uint8_t PARAM_DATA = 0x9D; -static constexpr uint8_t PARAM_RESET_POLARITY = 0x9E; -static constexpr uint8_t PARAM_CONTROLLER_INIT = 0x9F; - -////////////////////////////////////////////////////////////////////////// -// STK answer constants - -static constexpr uint8_t ANSWER_CKSUM_ERROR = 0xB0; diff --git a/stk500v2/flash b/stk500v2/flash deleted file mode 160000 index 6edb2e5..0000000 --- a/stk500v2/flash +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6edb2e5a21e0ce58ff2df936caee8b84e240a46b diff --git a/stk500v2/io b/stk500v2/io deleted file mode 160000 index 80de36e..0000000 --- a/stk500v2/io +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 80de36ee7ee3e6b0842d5eaee81d54062cb496b2 diff --git a/stk500v2/main.cpp b/stk500v2/main.cpp deleted file mode 100644 index 36049f8..0000000 --- a/stk500v2/main.cpp +++ /dev/null @@ -1,611 +0,0 @@ -#include "clock.hpp" - -#include "uart/uart.hpp" - -#include - -#include -#include -#include -#include - -#include "command.hpp" - -static constexpr auto TIMEOUT = 5000; -static constexpr auto BAUD_RATE = 115200; - -using uart_interface = uart::Hardware0, uart::Driven::BLOCKING>; -uart::Uart serial; - -struct Message { - uint8_t start; - uint8_t number; - uint16_t size; - uint8_t token; - uint8_t body[275]; - uint8_t checksum; -}; - -static inline bool receiveByte(uint8_t &data, uint16_t &timeout) -{ - constexpr auto MICROSECOND = 1000.0 * 1000; - constexpr auto SYMBOL_SIZE = 9; - constexpr auto BYTE_DELAY_US = (SYMBOL_SIZE * MICROSECOND) / BAUD_RATE; - constexpr auto NUM_MS_DELAY_STEPS = static_cast(round(1000 / BYTE_DELAY_US)); - uint16_t msDelay = NUM_MS_DELAY_STEPS; - - while (timeout) { - if (serial.rxByte(data)) { - timeout = TIMEOUT; - return true; - } - - _delay_us(BYTE_DELAY_US); - - if (--msDelay == 0) { - msDelay = NUM_MS_DELAY_STEPS; - --timeout; - } - } - - return false; -} - -static inline uint8_t calcChecksum(const Message &msg) -{ - uint8_t checksum = msg.start; - - for (uint16_t i = 1; i < 5 + msg.size; ++i) { - checksum ^= *(reinterpret_cast(&msg) + i); - } - - return checksum; -} - -static inline bool receiveMessage(Message &msg, uint16_t &timeout) -{ - if (!receiveByte(msg.start, timeout) || msg.start != MESSAGE_START) - return false; - if (!receiveByte(msg.number, timeout)) - return false; - if (!receiveByte(*(reinterpret_cast(&msg.size) + 1), timeout)) - return false; - if (!receiveByte(*reinterpret_cast(&msg.size), timeout) || msg.size > sizeof(msg.body)) - return false; - if (!receiveByte(msg.token, timeout) || msg.token != TOKEN) - return false; - for (uint16_t i = 0; i < msg.size; ++i) { - if (!receiveByte(msg.body[i], timeout)) - return false; - } - if (!receiveByte(msg.checksum, timeout) || msg.checksum != calcChecksum(msg)) - return false; - - return true; -} - -static inline void transmitMessage(const Message &msg) -{ - serial.txByte(msg.start); - serial.txByte(msg.number); - serial.txByte(msg.size >> 8); - serial.txByte(msg.size & 0xFF); - serial.txByte(msg.token); - for (uint16_t i = 0; i < msg.size; ++i) - serial.txByte(msg.body[i]); - serial.txByte(msg.checksum); -} - -static inline bool isSignOn(const Message &msg) -{ - if (msg.size == 1 && msg.body[0] == CMD_SIGN_ON) - return true; - return false; -} - -static inline bool isGetParameter(const Message &msg) -{ - if (msg.size == 2 && msg.body[0] == CMD_GET_PARAMETER) - return true; - return false; -} - -static inline bool isSetParameter(const Message &msg) -{ - if (msg.size == 3 && msg.body[0] == CMD_SET_PARAMETER) - return true; - return false; -} - -static inline bool isEnterProgmodeIsp(const Message &msg) -{ - if (msg.size == 12 && msg.body[0] == CMD_ENTER_PROGMODE_ISP) - return true; - return false; -} - -static inline bool isReadSignatureIsp(const Message &msg) -{ - if (msg.size == 6 && msg.body[0] == CMD_READ_SIGNATURE_ISP) - return true; - return false; -} - -static inline bool isReadFuseIsp(const Message &msg) -{ - if (msg.size == 6 && msg.body[0] == CMD_READ_FUSE_ISP) - return true; - return false; -} - -static inline bool isReadLockIsp(const Message &msg) -{ - if (msg.size == 6 && msg.body[0] == CMD_READ_LOCK_ISP) - return true; - return false; -} - -static inline bool isLoadAddress(const Message &msg) -{ - if (msg.size == 5 && msg.body[0] == CMD_LOAD_ADDRESS) - return true; - return false; -} - -static inline bool isReadFlashIsp(const Message &msg) -{ - if (msg.size == 4 && msg.body[0] == CMD_READ_FLASH_ISP) - return true; - return false; -} - -static inline bool isReadEepromIsp(const Message &msg) -{ - if (msg.size == 4 && msg.body[0] == CMD_READ_EEPROM_ISP) - return true; - return false; -} - -static inline bool isChipEraseIsp(const Message &msg) -{ - if (msg.size == 7 && msg.body[0] == CMD_CHIP_ERASE_ISP) - return true; - return false; -} - -static inline bool isProgramFlashIsp(const Message &msg) -{ - if (msg.body[0] == CMD_PROGRAM_FLASH_ISP) { - const auto dataSize = static_cast(msg.body[1]) << 8 | msg.body[2]; - if (msg.size == (dataSize + 10) && dataSize == SPM_PAGESIZE) - return true; - } - return false; -} - -static inline bool isProgramEepromIsp(const Message &msg) -{ - if (msg.body[0] == CMD_PROGRAM_EEPROM_ISP) { - if (msg.size == (static_cast(msg.body[1]) << 8 | msg.body[2]) + 10) - return true; - } - - return false; -} - -static inline bool isLeaveProgmodeIsp(const Message &msg) -{ - if (msg.size == 3 && msg.body[0] == CMD_LEAVE_PROGMODE_ISP) - return true; - return false; -} - -static inline void formatSignOnAnswer(Message &msg) -{ - msg.size = 3 + 8; - msg.body[1] = STATUS_CMD_OK; - msg.body[2] = 8; - msg.body[3] = 'S'; - msg.body[4] = 'T'; - msg.body[5] = 'K'; - msg.body[6] = '5'; - msg.body[7] = '0'; - msg.body[8] = '0'; - msg.body[9] = '_'; - msg.body[10] = '2'; - msg.checksum = calcChecksum(msg); -} - -static inline void formatGetParameterAnswer(Message &msg) -{ - msg.size = 3; - - if (msg.body[1] == PARAM_HW_VER) { - msg.body[2] = 1; - } else if (msg.body[1] == PARAM_SW_MAJOR) { - msg.body[2] = 0x02; - } else if (msg.body[1] == PARAM_SW_MINOR) { - msg.body[2] = 0x0a; - } else if (msg.body[1] == PARAM_SCK_DURATION) { - msg.body[2] = 2; - } else if (msg.body[1] == PARAM_VADJUST) { - msg.body[2] = 25; - } else if (msg.body[1] == PARAM_VTARGET) { - msg.body[2] = 49; - } else if (msg.body[1] == PARAM_OSC_PSCALE) { - msg.body[2] = 2; - } else if (msg.body[1] == PARAM_OSC_CMATCH) { - msg.body[2] = 127; - } else if (msg.body[1] == PARAM_TOPCARD_DETECT) { - msg.body[2] = 0xFF; - } else { - msg.size = 2; - } - - if (msg.size == 2) { - msg.body[1] = STATUS_CMD_FAILED; - } else { - msg.body[1] = STATUS_CMD_OK; - } - - msg.checksum = calcChecksum(msg); -} - -static inline void formatSetParameterAnswer(Message &msg) -{ - msg.size = 2; - msg.body[1] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatEnterProgmodeIspAnswer(Message &msg) -{ - msg.size = 2; - msg.body[1] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatReadSignatureIspAnswer(Message &msg) -{ - msg.size = 4; - msg.body[2] = boot_signature_byte_get(msg.body[4] * 2); - msg.body[1] = STATUS_CMD_OK; - msg.body[3] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatReadFuseIspAnswer(Message &msg) -{ - constexpr auto READ_LOW_FUSE_BITS = 0x0050; - constexpr auto READ_HIGH_FUSE_BITS = 0x0858; - constexpr auto READ_EXTENDED_FUSE_BITS = 0x0850; - - msg.size = 4; - - if (*reinterpret_cast(msg.body + 2) == READ_EXTENDED_FUSE_BITS) { - msg.body[2] = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS); - } - if (*reinterpret_cast(msg.body + 2) == READ_HIGH_FUSE_BITS) { - msg.body[2] = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); - } - if (*reinterpret_cast(msg.body + 2) == READ_LOW_FUSE_BITS) { - msg.body[2] = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS); - } - - msg.body[1] = STATUS_CMD_OK; - msg.body[3] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatReadLockIspAnswer(Message &msg) -{ - msg.size = 4; - msg.body[2] = boot_lock_fuse_bits_get(GET_LOCK_BITS); - msg.body[1] = STATUS_CMD_OK; - msg.body[3] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatLoadAddressAnswer(Message &msg) -{ - msg.size = 2; - msg.body[1] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatReadFlashIspAnswer(Message &msg, uint32_t &addr) -{ - const uint16_t byteAddress = 2 * addr; - const uint16_t numBytes = static_cast(msg.body[1]) << 8 | msg.body[2]; - msg.size = 3 + numBytes; - msg.body[1] = STATUS_CMD_OK; - for (uint16_t i = 0; i < numBytes; ++i) { - msg.body[i + 2] = pgm_read_byte(static_cast(byteAddress + i)); - } - const auto numWords = numBytes / 2; - addr += numWords; - msg.body[numBytes + 2] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -namespace { - -bool isEepromReady() -{ - return (EECR & (1 << EEPE)) ? false : true; -} - -void waitEepromReady() -{ - while (!isEepromReady()) - ; -} - -uint8_t readEepromByte(const uint8_t *addr) -{ - EEAR = reinterpret_cast(addr); - EECR |= (1 << EERE); - return EEDR; -} - -void writeEepromByte(uint8_t *addr, uint8_t value) -{ - EECR = 0; - EEAR = reinterpret_cast(addr); - EEDR = value; - EECR |= (1 << EEMPE); - EECR |= (1 << EEPE); -} - -////////////////////////////////////////////////////////////////////////// - -void writeFlashPage(uint32_t pageAddress, const uint8_t *data) -{ - boot_page_erase(pageAddress); - boot_spm_busy_wait(); - - for (uint16_t i = 0; i < SPM_PAGESIZE; i += 2) { - uint16_t dataWord = *data++; - dataWord |= (*data++) << 8; - boot_page_fill(pageAddress + i, dataWord); - } - - boot_page_write(pageAddress); - boot_spm_busy_wait(); - boot_rww_enable(); -} - -} // namespace - -static inline uint16_t getBootloaderSize() -{ - const auto highFuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); - constexpr auto BOOTSZ0 = 1; - constexpr auto BOOTSZ1 = 2; - - if (highFuse & (1 << BOOTSZ1) && highFuse & (1 << BOOTSZ0)) - return 256 * 2; - else if (highFuse & (1 << BOOTSZ1)) - return 512 * 2; - else if (highFuse & (1 << BOOTSZ0)) - return 1024 * 2; - return 2048 * 2; -} - -static inline uint32_t getFlashSize() -{ - const auto bootloaderSize = getBootloaderSize(); - return (FLASHEND - bootloaderSize + 1); -} - -static inline void performChipErase(uint16_t flashStartAddress = 0x0000) -{ - constexpr auto getEepromEraseFuseBit = []() -> bool { - constexpr auto EESAVE = 3; - return boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS) & (1 << EESAVE); - }; - - constexpr auto eraseFlash = [](const uint16_t &flashStartAddress) { - const auto flashSize = getFlashSize(); - const auto byteAddress = 2 * flashStartAddress; - for (uint16_t i = byteAddress; i < flashSize; i += SPM_PAGESIZE) { - boot_page_erase(i); - boot_spm_busy_wait(); - } - boot_rww_enable(); - }; - - constexpr auto eraseEeprom = [getEepromEraseFuseBit]() { - const auto eraseEeprom = getEepromEraseFuseBit(); - if (eraseEeprom) { - constexpr auto EEPROM_SIZE = E2END + 1; - for (uint16_t i = 0; i < EEPROM_SIZE; ++i) { - writeEepromByte(reinterpret_cast(i), 0xFF); - waitEepromReady(); - } - } - }; - - eraseFlash(flashStartAddress); - eraseEeprom(); -} - -static inline void formatChipEraseIspAnswer(Message &msg) -{ - msg.size = 2; - msg.body[1] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatReadEepromIspAnswer(Message &msg, uint32_t &addr) -{ - const uint16_t numBytes = static_cast(msg.body[1]) << 8 | msg.body[2]; - msg.size = 3 + numBytes; - msg.body[1] = STATUS_CMD_OK; - for (uint16_t i = 0; i < numBytes; ++i) { - msg.body[i + 2] = readEepromByte(reinterpret_cast(addr + i)); - } - addr += numBytes; - msg.body[numBytes + 2] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatProgramFlashIspAnswer(Message &msg, uint32_t &addr) -{ - const auto byteAddress = 2 * addr; - if (byteAddress < getFlashSize()) - writeFlashPage(byteAddress, msg.body + 10); - const uint16_t numBytes = static_cast(msg.body[1]) << 8 | msg.body[2]; - const auto numWords = numBytes / 2; - addr += numWords; - - msg.size = 2; - msg.body[1] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatProgramEepromIspAnswer(Message &msg, uint32_t &addr) -{ - const uint16_t numBytes = static_cast(msg.body[1]) << 8 | msg.body[2]; - for (uint16_t i = 0; i < numBytes; ++i) { - writeEepromByte(reinterpret_cast(addr + i), msg.body[10 + i]); - waitEepromReady(); - } - addr += numBytes; - msg.size = 2; - msg.body[1] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatLeaveProgmodeIspAnswer(Message &msg) -{ - msg.size = 2; - msg.body[1] = STATUS_CMD_OK; - - msg.checksum = calcChecksum(msg); -} - -static inline void formatErrorAnswer(Message &msg) -{ - msg.start = MESSAGE_START; - msg.size = 1; - msg.token = TOKEN; - msg.body[0] = STATUS_CMD_UNKNOWN; - msg.checksum = calcChecksum(msg); -} - -enum class ChipEraseState { - NONE = 0, - REQUEST = (1 << 1), - RESPONSE = (1 << 2), - PERFORM = REQUEST | RESPONSE, - PROGRAM = (1 << 3), - FINISH = REQUEST | RESPONSE | PROGRAM, -}; - -constexpr ChipEraseState operator|(const ChipEraseState &self, const ChipEraseState &other) -{ - return static_cast(static_cast(self) | static_cast(other)); -} - -constexpr ChipEraseState &operator|=(ChipEraseState &self, const ChipEraseState &other) -{ - self = self | other; - return self; -} - -static inline void handleMessage(Message &msg, uint32_t &addr, uint16_t &finishEraseAddress, - ChipEraseState &chipEraseFlag) -{ - if (isSignOn(msg)) - formatSignOnAnswer(msg); - else if (isGetParameter(msg)) - formatGetParameterAnswer(msg); - else if (isSetParameter(msg)) - formatSetParameterAnswer(msg); - else if (isEnterProgmodeIsp(msg)) - formatEnterProgmodeIspAnswer(msg); - else if (isReadSignatureIsp(msg)) - formatReadSignatureIspAnswer(msg); - else if (isReadFuseIsp(msg)) - formatReadFuseIspAnswer(msg); - else if (isReadLockIsp(msg)) - formatReadLockIspAnswer(msg); - else if (isLoadAddress(msg)) { - addr = msg.body[1]; - addr = (addr << 8) | msg.body[2]; - addr = (addr << 8) | msg.body[3]; - addr = (addr << 8) | msg.body[4]; - formatLoadAddressAnswer(msg); - } else if (isReadFlashIsp(msg)) - formatReadFlashIspAnswer(msg, addr); - else if (isReadEepromIsp(msg)) - formatReadEepromIspAnswer(msg, addr); - else if (isChipEraseIsp(msg)) { - chipEraseFlag |= ChipEraseState::REQUEST; - formatChipEraseIspAnswer(msg); - } else if (isProgramFlashIsp(msg)) { - chipEraseFlag |= ChipEraseState::PROGRAM; - formatProgramFlashIspAnswer(msg, addr); - finishEraseAddress = addr; - } else if (isProgramEepromIsp(msg)) - formatProgramEepromIspAnswer(msg, addr); - else if (isLeaveProgmodeIsp(msg)) { - chipEraseFlag |= ChipEraseState::RESPONSE; - formatLeaveProgmodeIspAnswer(msg); - } else - formatErrorAnswer(msg); - - transmitMessage(msg); -} - -int main() -{ - serial.init(); - - Message msg; - uint32_t addr = 0x0000; - uint16_t finishEraseAddress = 0x0000; - ChipEraseState chipEraseFlag = ChipEraseState::NONE; - uint16_t timeout = TIMEOUT; - - while (true) { - if (receiveMessage(msg, timeout)) { - handleMessage(msg, addr, finishEraseAddress, chipEraseFlag); - } - - if (timeout == 0) { - if (chipEraseFlag == ChipEraseState::PERFORM) { - performChipErase(); - chipEraseFlag = ChipEraseState::NONE; - } else if (chipEraseFlag == ChipEraseState::FINISH) { - performChipErase(finishEraseAddress); - chipEraseFlag = ChipEraseState::NONE; - } - - asm volatile("jmp 0x0000"); - } - } - - return 0; -} - -void startup() __attribute__((naked, section(".vectors"))); -void startup() -{ - asm volatile("clr __zero_reg__"); - SP = RAMEND; - SREG = 0; - asm volatile("jmp main"); -} diff --git a/stk500v2/stk500v2.cppproj b/stk500v2/stk500v2.cppproj deleted file mode 100644 index d870eb8..0000000 --- a/stk500v2/stk500v2.cppproj +++ /dev/null @@ -1,275 +0,0 @@ - - - - 2.0 - 7.0 - com.Atmel.AVRGCC8.CPP - {19798cce-5d96-40e9-b769-d209715dce0c} - ATmega328P - none - Executable - CPP - $(MSBuildProjectName) - .elf - $(MSBuildProjectDirectory)\$(Configuration) - stk500v2 - stk500v2 - stk500v2 - 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 - - - - - - - - - - -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) - - - - - - compile - - - compile - - - compile - - - compile - - - compile - - - compile - - - compile - - - compile - - - compile - - - compile - - - compile - - - compile - - - - - - - - - - \ No newline at end of file diff --git a/stk500v2/type b/stk500v2/type deleted file mode 160000 index ce31ef0..0000000 --- a/stk500v2/type +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ce31ef017f738baaca6f0cbf99dac9d59b5e6811 diff --git a/stk500v2/uart b/stk500v2/uart deleted file mode 160000 index 8f88cdc..0000000 --- a/stk500v2/uart +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8f88cdccea6b8f1f8521bcad6724cacb1e6acbcf diff --git a/tsb/clock.hpp b/tsb/clock.hpp deleted file mode 100644 index 9d22bb6..0000000 --- a/tsb/clock.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#define F_CPU 18'432'000 -#include diff --git a/tsb/flash b/tsb/flash deleted file mode 160000 index 6edb2e5..0000000 --- a/tsb/flash +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6edb2e5a21e0ce58ff2df936caee8b84e240a46b diff --git a/tsb/io b/tsb/io deleted file mode 160000 index 80de36e..0000000 --- a/tsb/io +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 80de36ee7ee3e6b0842d5eaee81d54062cb496b2 diff --git a/tsb/tsb.cppproj b/tsb/tsb.cppproj deleted file mode 100644 index a124e53..0000000 --- a/tsb/tsb.cppproj +++ /dev/null @@ -1,263 +0,0 @@ - - - - 2.0 - 7.0 - com.Atmel.AVRGCC8.CPP - dce6c7e3-ee26-4d79-826b-08594b9ad897 - ATmega328P - none - Executable - CPP - $(MSBuildProjectName) - .elf - $(MSBuildProjectDirectory)\$(Configuration) - tsb - tsb - tsb - avr-g++-9.1.0 - true - false - true - true - 0x20000000 - - true - exception_table - 2 - 0 - 0 - - - - J41800099437 - 0x1E9705 - - - - 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 - - - - - - - - - - -mmcu=atmega1284p - True - 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=atmega1284p - True - 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 - - - compile - - - compile - - - compile - - - compile - - - compile - - - compile - - - - - - - - - - \ No newline at end of file diff --git a/tsb/type b/tsb/type deleted file mode 160000 index ce31ef0..0000000 --- a/tsb/type +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ce31ef017f738baaca6f0cbf99dac9d59b5e6811 diff --git a/tsb/uart b/tsb/uart deleted file mode 160000 index 8f88cdc..0000000 --- a/tsb/uart +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8f88cdccea6b8f1f8521bcad6724cacb1e6acbcf