tsb: one spelling of the clock and the baud, and the asm's bits by name
The tricks and asm tiers spelled 16_MHz three times each and now name it once; the policy tier's lean bring-up solves the baud from the link config the serial unit is declared with instead of restating 115200 8N1. The asm tier's immediates - RXEN0, RXC0's bit, SPMEN, PGWRT|SPMEN and the three UART addresses - are operands read from the chip database rather than numbers with comments beside them, so avr/io.h is left for SPMCSR's I/O address alone; pure and tricks drop an include nothing used. Headers that described the code as it was are corrected (pure's "only attribute", the policy tier's history), the rule references leave the comments, and locals are const auto. All four images are byte-identical to before, in both modes for tsb_asm. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
20b1485b6f
commit
aafaf25a47
4 files changed
+62
-66
No files matched your search
+24
-20
@@ -32,7 +32,7 @@
|
||||
|
||||
#include <libavr/libavr.hpp>
|
||||
|
||||
#include <avr/io.h> // SP / RAMEND for the crt-free boot entry, SFR addresses for the asm routines
|
||||
#include <avr/io.h> // SPMCSR's I/O address for the page-store routine
|
||||
|
||||
using namespace avr::literals;
|
||||
namespace spm = avr::spm;
|
||||
@@ -56,6 +56,8 @@ constexpr std::uint8_t confirm = '!';
|
||||
constexpr std::uint8_t request = '?';
|
||||
constexpr std::uint8_t knock = '@';
|
||||
|
||||
constexpr auto clock = 16_MHz;
|
||||
|
||||
// Boot geometry for the 512 B boot section (BOOTSZ=11); the page size and the
|
||||
// flash/EEPROM extents are the chip database's to know. app_end is the config
|
||||
// page (TSB's LASTPAGE), one page below the boot section.
|
||||
@@ -65,20 +67,20 @@ constexpr std::uint16_t app_end = spm::flash_bytes - boot_bytes - page;
|
||||
constexpr std::uint16_t eeprom_end = avr::hw::db.mem.eeprom_size - 1;
|
||||
|
||||
// Lockout-proof floor for the activation window: the oracle's F_CPU/1MHz, so
|
||||
// it follows the clock rather than restating it (rule 41).
|
||||
constexpr auto act_min = static_cast<std::uint8_t>((16_MHz).hz / 1'000'000);
|
||||
// it follows the clock rather than restating it.
|
||||
constexpr auto act_min = static_cast<std::uint8_t>(clock.hz / 1'000'000);
|
||||
// Post-activation window: the host gets seconds, not milliseconds, mid-session.
|
||||
constexpr std::uint8_t comm_window = 200;
|
||||
|
||||
constexpr std::uint16_t build_date = 26 * 512 + 7 * 32 + 20;
|
||||
|
||||
// Fixed 115200 8N1; the library solves UBRR + U2X from clock and baud.
|
||||
constexpr auto baud = avr::uart::solve_baud(16_MHz, 115200_Bd, 8, avr::uart::parity::none);
|
||||
constexpr auto baud = avr::uart::solve_baud(clock, 115200_Bd, 8, avr::uart::parity::none);
|
||||
|
||||
// One bit time on the wire: the turn-around a shared-line peer needs to stop
|
||||
// driving before this one starts. Derived from the solved rate, so it follows
|
||||
// the link rather than a count measured against one.
|
||||
constexpr auto guard_cycles = static_cast<std::uint32_t>((16_MHz).hz / baud.actual);
|
||||
constexpr auto guard_cycles = static_cast<std::uint32_t>(clock.hz / baud.actual);
|
||||
|
||||
// The 16-byte device-info block, streamed out on activation.
|
||||
// clang-format off
|
||||
@@ -113,7 +115,7 @@ const std::uint8_t *flash_ptr(std::uint16_t addr)
|
||||
std::uint8_t byte;
|
||||
asm volatile(" tst %[dir] \n\t" // already receiving? keep the line released
|
||||
" brne 1f \n\t"
|
||||
" ldi %[b], 0x10 \n\t" // RXEN0 alone: release the line and listen
|
||||
" ldi %[b], %[rxen] \n\t" // the receiver alone: release the line and listen
|
||||
" sts %[ucsr0b], %[b] \n\t"
|
||||
" ser %[b] \n\t"
|
||||
" mov %[dir], %[b] \n\t"
|
||||
@@ -122,7 +124,7 @@ const std::uint8_t *flash_ptr(std::uint16_t addr)
|
||||
" clr r26 \n\t"
|
||||
"2: ser %[b] \n\t"
|
||||
"3: lds %[b], %[ucsr0a] \n\t"
|
||||
" sbrc %[b], 7 \n\t" // RXC0
|
||||
" sbrc %[b], %[rxc] \n\t"
|
||||
" rjmp 4f \n\t"
|
||||
" dec %[b] \n\t"
|
||||
" brne 3b \n\t"
|
||||
@@ -133,8 +135,9 @@ const std::uint8_t *flash_ptr(std::uint16_t addr)
|
||||
"4: lds %[b], %[udr0] \n\t"
|
||||
"5: \n\t"
|
||||
: [b] "=&d"(byte), [dir] "+r"(g_receiving)
|
||||
: [to] "r"(g_window), [actmin] "M"(act_min), [ucsr0a] "n"(_SFR_MEM_ADDR(UCSR0A)),
|
||||
[ucsr0b] "n"(_SFR_MEM_ADDR(UCSR0B)), [udr0] "n"(_SFR_MEM_ADDR(UDR0))
|
||||
: [to] "r"(g_window), [actmin] "M"(act_min), [rxen] "M"(hw::ucsr0b::rxen0(1).value),
|
||||
[rxc] "I"(std::countr_zero(hw::ucsr0a::rxc0.mask)), [ucsr0a] "n"(hw::ucsr0a::addr),
|
||||
[ucsr0b] "n"(hw::ucsr0b::addr), [udr0] "n"(hw::udr0::addr)
|
||||
: "r26", "r27", "cc");
|
||||
return byte;
|
||||
}
|
||||
@@ -169,7 +172,7 @@ const std::uint8_t *flash_ptr(std::uint16_t addr)
|
||||
// global-register rule, see tsb_tricks.cpp).
|
||||
[[gnu::noinline, gnu::noclone]] std::uint8_t sflash()
|
||||
{
|
||||
std::uint8_t byte = avr::flash_load(flash_ptr(g_addr));
|
||||
const auto byte = avr::flash_load(flash_ptr(g_addr));
|
||||
++g_addr;
|
||||
return byte;
|
||||
}
|
||||
@@ -177,7 +180,7 @@ const std::uint8_t *flash_ptr(std::uint16_t addr)
|
||||
// One EEPROM byte <- [g_addr++].
|
||||
[[gnu::noinline, gnu::noclone]] std::uint8_t eerd()
|
||||
{
|
||||
std::uint8_t byte = ee::read<no_spm>(g_addr);
|
||||
const auto byte = ee::read<no_spm>(g_addr);
|
||||
++g_addr;
|
||||
return byte;
|
||||
}
|
||||
@@ -213,7 +216,7 @@ extern "C" [[noreturn]] void tsb_app(); // the application's reset vector: --def
|
||||
tsb_app();
|
||||
}
|
||||
|
||||
// Step g_addr one page down and erase that page (the decrement lives here -
|
||||
// Step g_addr one page down and erase that page (the decrement lives here -
|
||||
// the global-register rule).
|
||||
[[gnu::noinline, gnu::noclone]] void erase_below()
|
||||
{
|
||||
@@ -233,7 +236,7 @@ extern "C" [[noreturn]] void tsb_app(); // the application's reset vector: --def
|
||||
}
|
||||
|
||||
// Stream one host page into the erased flash page at g_addr (asm 2 of 2): the
|
||||
// word pair stages in r0:r1 straight from rx (whose register set is known -
|
||||
// word pair stages in r0:r1 straight from rx (whose register set is known -
|
||||
// the cross-call liveness C++ cannot express), Z walks the page and PGWRT
|
||||
// programs it. g_addr is left at the next page base.
|
||||
[[gnu::noinline, gnu::noclone]] void store_flash()
|
||||
@@ -243,8 +246,8 @@ extern "C" [[noreturn]] void tsb_app(); // the application's reset vector: --def
|
||||
"1: rcall %x[rx] \n\t"
|
||||
" mov r0, r24 \n\t" // word low byte
|
||||
" rcall %x[rx] \n\t"
|
||||
" mov r1, r24 \n\t" // word high byte
|
||||
" ldi r24, 0x01 \n\t" // SPMEN: buffer the word at Z
|
||||
" mov r1, r24 \n\t" // word high byte
|
||||
" ldi r24, %[fill] \n\t" // buffer the word at Z
|
||||
" out %[spmcsr], r24 \n\t"
|
||||
" spm \n\t"
|
||||
" clr r1 \n\t"
|
||||
@@ -254,12 +257,13 @@ extern "C" [[noreturn]] void tsb_app(); // the application's reset vector: --def
|
||||
" movw %[base], r30 \n\t" // g_addr = the next page base
|
||||
" subi r30, %[pagelo] \n\t" // Z back to this page's base
|
||||
" sbci r31, %[pagehi] \n\t"
|
||||
" ldi r24, 0x05 \n\t" // PGWRT | SPMEN: program the page
|
||||
" ldi r24, %[write] \n\t" // program the page
|
||||
" out %[spmcsr], r24 \n\t"
|
||||
" spm \n\t"
|
||||
: [base] "+r"(g_addr)
|
||||
: [rx] "i"(&rx), [spmcsr] "I"(_SFR_IO_ADDR(SPMCSR)), [words] "M"(page / 2), [pagelo] "M"(page & 0xff),
|
||||
[pagehi] "M"(page >> 8)
|
||||
: [rx] "i"(&rx), [spmcsr] "I"(_SFR_IO_ADDR(SPMCSR)), [fill] "M"(hw::spmcsr::spmen(1).value),
|
||||
[write] "M"(hw::spmcsr::pgwrt(1).value | hw::spmcsr::spmen(1).value), [words] "M"(page / 2),
|
||||
[pagelo] "M"(page & 0xff), [pagehi] "M"(page >> 8)
|
||||
: "r0", "r1", "r20", "r24", "r26", "r27", "r30", "r31", "cc", "memory");
|
||||
settle();
|
||||
}
|
||||
@@ -285,7 +289,7 @@ extern "C" [[noreturn]] void tsb_app(); // the application's reset vector: --def
|
||||
g_receiving = 0;
|
||||
|
||||
// Activation: 3x'@', each inside the config page's timeout window (rx
|
||||
// floors it so a corrupt page cannot lock the loader out); anything else -
|
||||
// floors it so a corrupt page cannot lock the loader out); anything else -
|
||||
// including silence - hands over.
|
||||
g_window = avr::flash_load(flash_ptr(app_end + 2));
|
||||
for (std::uint8_t k = 3; k; --k) {
|
||||
@@ -311,7 +315,7 @@ extern "C" [[noreturn]] void tsb_app(); // the application's reset vector: --def
|
||||
sendf();
|
||||
break;
|
||||
}
|
||||
std::uint8_t got = rx();
|
||||
const auto got = rx();
|
||||
if (got == 0) {
|
||||
if (mask == 0) {
|
||||
continue;
|
||||
|
||||
Reference in new issue
Block a user