diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6366408 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,12 @@ +[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 diff --git a/tsb/flash b/tsb/flash new file mode 160000 index 0000000..6edb2e5 --- /dev/null +++ b/tsb/flash @@ -0,0 +1 @@ +Subproject commit 6edb2e5a21e0ce58ff2df936caee8b84e240a46b diff --git a/tsb/io b/tsb/io new file mode 160000 index 0000000..80de36e --- /dev/null +++ b/tsb/io @@ -0,0 +1 @@ +Subproject commit 80de36ee7ee3e6b0842d5eaee81d54062cb496b2 diff --git a/tsb/main.cpp b/tsb/main.cpp index ef7c54e..9543bdd 100644 --- a/tsb/main.cpp +++ b/tsb/main.cpp @@ -1,6 +1,102 @@ #include "clock.hpp" +#include "uart/uart.hpp" + +constexpr auto READ_FLASH_CMD = 'f'; +constexpr auto WRITE_FLASH_CMD = 'F'; +constexpr auto READ_EEPROM_CMD = 'e'; +constexpr auto WRITE_EEPROM_CMD = 'E'; +constexpr auto READ_USERDATA_CMD = 'c'; +constexpr auto WRITE_USERDATA_CMD = 'C'; +constexpr auto REQUEST_CMD = '?'; +constexpr auto CONFIRM_CMD = '!'; +constexpr auto AUTO_BAUDING_CMD = '@'; + +using uart_interface = uart::Hardware0, uart::Driven::BLOCKING>; + +enum class State { + WAITING, + ACTIVE, +}; + +struct DeviceInfo { + char name[3] = {'T', 'S', 'B'}; + uint8_t date[2] = {0x1a, 0x1f}; + uint8_t status = 0xf0; + uint8_t signature[3] = {0x1e, 0x95, 0x0f}; + uint8_t pageSize = 0x40; + uint16_t flashSize = 0x3ec0; + uint16_t eepromSize = 0x03ff; +}; + +struct UserData { + uint16_t jumpAddress = 0xAAAA; + uint8_t timeout = 0x21; +}; + +static inline void sendDeviceInfo() +{ + uart::Uart serial; + + constexpr DeviceInfo deviceInfo; + constexpr UserData userData; + + for (uint8_t i = 0; i < sizeof(deviceInfo); ++i) { + serial.txByte(*(reinterpret_cast(&deviceInfo) + i)); + } + + for (uint8_t i = 0; i < sizeof(userData); ++i) { + serial.txByte(*(reinterpret_cast(&userData) + i)); + } +} + +static uint8_t g_lastPage[128] = {}; + +static inline void sendUserData() +{ + uart::Uart serial; + + for (uint8_t i = 0; i < sizeof(g_lastPage); ++i) { + serial.txByte(*(reinterpret_cast(&g_lastPage) + i)); + } +} + +static inline void sendConfirm() +{ + uart::Uart serial; + serial.txByte(CONFIRM_CMD); +} + int main() { + uart::Uart serial; + serial.init(); + + State state = State::WAITING; + + uint8_t receivedByte = 0; + uint8_t autoBaudingCounter = 0; + + while (true) { + if (serial.rxByte(receivedByte)) { + if (state == State::WAITING) { + if (receivedByte == AUTO_BAUDING_CMD) { + ++autoBaudingCounter; + } + if (autoBaudingCounter == 3) { + autoBaudingCounter = 0; + state = State::ACTIVE; + sendDeviceInfo(); + } + } else if (state == State::ACTIVE) { + if (receivedByte == READ_USERDATA_CMD) { + sendUserData(); + sendConfirm(); + state = State::WAITING; + } + } + } + } + return 0; } diff --git a/tsb/tsb.cppproj b/tsb/tsb.cppproj index e72d63a..fa2d942 100644 --- a/tsb/tsb.cppproj +++ b/tsb/tsb.cppproj @@ -215,9 +215,42 @@ 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 new file mode 160000 index 0000000..ce31ef0 --- /dev/null +++ b/tsb/type @@ -0,0 +1 @@ +Subproject commit ce31ef017f738baaca6f0cbf99dac9d59b5e6811 diff --git a/tsb/uart b/tsb/uart new file mode 160000 index 0000000..ae03c8d --- /dev/null +++ b/tsb/uart @@ -0,0 +1 @@ +Subproject commit ae03c8d43e46dfbd396a052f71670727b293ac76