HALF_DUPLEX deploys a shared line per backend. The hardware USART takes the library's .half_duplex turn-around — RXD and TXD tied off-chip, each reply byte held to transmit-complete before the line can be released (m8 404 B, m328P 440, 1284P 460; the window poll runs through the outlined release-line call at 18 or 22 cycles a poll, measured off the built loops and held per chip by pureboot.window.halfduplex). The software and autobaud links fold onto the RX pin — RX == TX spells the same — and cost nothing: the frame's direction wrap is what the dropped second-pin init paid, and the worst image in the space is unchanged at the 1284s' 502 of 512, now with its one-wire twin proven equal across the exhaustive matrix. The host gains --one-wire, the echo discard a shared line requires: the adapter's echo is matched byte for byte and a reply interleaving a blind write — a loader already in session re-prompts inside the knock — is held for the reader. The device runner models the shared line by direction (drives only while the firmware's DDR reads input, decodes only while the firmware owns it, supplies the host-side echo), extends the USART pin-ownership model to RXEN's hold on RXD, and starts the pty USART from the datasheet's zeroed UCSR#B: simavr's TXEN-set reset plus its clear-UDRE-on-TXEN-drop otherwise wedges the first transmitter after a receiver-only program, which the half-duplex window gate caught as a banner that never came. v7 is tagged at its era's last commit; v8 changes nothing on the wire. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
180 lines
5.5 KiB
C++
180 lines
5.5 KiB
C++
// Test-fixture application for the pureboot protocol tests: prints "APP" on
|
|
// the chip's serial link (the same link the loader uses) — the proof that
|
|
// the loader's hand-over, and on the tinies the host's reset-vector
|
|
// surgery, actually launched it. Linked normally (crt, vectors at 0); on
|
|
// the tinies its reset vector is the rjmp the host re-homes.
|
|
//
|
|
// On the hardware-USART link it then listens, and an 'L' makes it jump into
|
|
// the resident loader — the application-owned loader entry a
|
|
// BOOTRST-unprogrammed mega relies on (reset always boots the application
|
|
// there), exercised by the self-update tests. The software link idles:
|
|
// reset reaches those loaders through the patched vector (or the runner
|
|
// models BOOTRST), so the application owes them nothing.
|
|
//
|
|
// PUREBOOT_HANDOVER drops the listening and jumps straight in, leaving the
|
|
// USART enabled behind it — the hand-over state a loader bit-banging on that
|
|
// USART's own pins has to survive.
|
|
//
|
|
// The fixture speaks the deployment its loader was built for: the same
|
|
// PUREBOOT_* defines configure it, and without them it assumes the stock
|
|
// deployment (the crystal/RC clock table below, the chip's natural link).
|
|
#include <libavr/libavr.hpp>
|
|
|
|
using namespace avr::literals;
|
|
|
|
namespace {
|
|
|
|
consteval avr::hertz_t clock()
|
|
{
|
|
#if defined(PUREBOOT_CLOCK_HZ)
|
|
return avr::hertz_t{PUREBOOT_CLOCK_HZ};
|
|
#else
|
|
auto name = std::string_view{avr::hw::db.name};
|
|
if (name.starts_with("ATtiny13"))
|
|
return 9.6_MHz;
|
|
if (name.starts_with("ATtiny"))
|
|
return 8_MHz;
|
|
return 16_MHz;
|
|
#endif
|
|
}
|
|
|
|
#if !defined(PUREBOOT_TX)
|
|
#define PUREBOOT_TX pb1
|
|
#endif
|
|
#if !defined(PUREBOOT_RX)
|
|
#define PUREBOOT_RX pb0
|
|
#endif
|
|
#if !defined(PUREBOOT_USART)
|
|
#define PUREBOOT_USART 0
|
|
#endif
|
|
|
|
consteval bool use_hardware()
|
|
{
|
|
#if defined(PUREBOOT_SOFT_SERIAL)
|
|
return false;
|
|
#else
|
|
return avr::uart::has_usart<0>();
|
|
#endif
|
|
}
|
|
|
|
using dev = avr::device<{.clock = clock()}>;
|
|
|
|
template <avr::hertz_t C, bool Hardware = use_hardware()>
|
|
struct link {
|
|
#if defined(PUREBOOT_BAUD)
|
|
static constexpr avr::baud_t baud{PUREBOOT_BAUD};
|
|
#else
|
|
static constexpr avr::baud_t baud{115200};
|
|
#endif
|
|
using tx_t = avr::uart::usart<PUREBOOT_USART, C, {.baud = baud, .max_baud_error = 2.5_pct}>;
|
|
static void init()
|
|
{
|
|
avr::init<tx_t>();
|
|
}
|
|
static void tx(char c)
|
|
{
|
|
tx_t::write(static_cast<std::uint8_t>(c));
|
|
}
|
|
// The loader sits in the top slot — 512 bytes on every chip. The jump
|
|
// takes a word address, which is what makes the >64 KiB chips' entry
|
|
// reachable through a 16-bit pointer at all.
|
|
static void enter_loader()
|
|
{
|
|
constexpr std::uint32_t slot = 512;
|
|
reinterpret_cast<void (*)()>(static_cast<std::uint16_t>((avr::hw::db.mem.flash_size - slot) / 2))();
|
|
}
|
|
|
|
[[noreturn]] static void idle()
|
|
{
|
|
#if defined(PUREBOOT_HANDOVER)
|
|
// Hand back at once, with this USART still enabled — the state that
|
|
// leaves a bit-banged loader on its pins mute unless the loader
|
|
// releases it. Unconditional because there is no command wire to
|
|
// wait on: that loader's link is the pins, not this peripheral.
|
|
enter_loader();
|
|
__builtin_unreachable();
|
|
#else
|
|
for (;;) {
|
|
auto command = tx_t::read_blocking();
|
|
if (command == 'L')
|
|
enter_loader();
|
|
// 'D' leaves every word of the SPM page buffer dirty, so that a
|
|
// following 'L' enters the loader with the buffer it never clears.
|
|
if (command == 'D') {
|
|
for (std::uint16_t at = 0; at < avr::spm::page_bytes; at += 2)
|
|
avr::spm::fill(at, 0xdead);
|
|
tx('D');
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
};
|
|
|
|
template <avr::hertz_t C>
|
|
struct link<C, false> {
|
|
#if defined(PUREBOOT_BAUD)
|
|
static constexpr avr::baud_t baud{PUREBOOT_BAUD};
|
|
#else
|
|
static constexpr avr::baud_t baud{57600};
|
|
#endif
|
|
// A shared-pin deployment (RX == TX) banners as a guest on its own line:
|
|
// the pull-up input is the released line, the transmitter takes the pin
|
|
// for exactly one frame per byte — the shape a real one-wire application
|
|
// beside this loader uses.
|
|
static constexpr bool one_wire = avr::PUREBOOT_RX == avr::PUREBOOT_TX;
|
|
using tx_t = avr::uart::software_tx<C, avr::PUREBOOT_TX, baud, one_wire>;
|
|
static void init()
|
|
{
|
|
// The guest transmitter configures no pin; the released line — the
|
|
// pull-up input a receiver would own — is established here.
|
|
if constexpr (one_wire)
|
|
avr::init<avr::io::input<avr::PUREBOOT_TX, avr::io::pull::up>, tx_t>();
|
|
else
|
|
avr::init<tx_t>();
|
|
}
|
|
static void tx(char c)
|
|
{
|
|
tx_t::write(static_cast<std::uint8_t>(c));
|
|
}
|
|
[[noreturn]] static void idle()
|
|
{
|
|
#if defined(PUREBOOT_HEARTBEAT)
|
|
// Repeat the banner forever, which turns the fixture into a fixed
|
|
// cycles-per-bit transmitter: `tools/pbrig.py rate` sweeps the host rate
|
|
// against it to find the part's true bit rate, and from that the clock
|
|
// its RC oscillator is really running at. Only the *bit* timing carries
|
|
// the measurement — the delay merely spaces the lines out, so its own
|
|
// error does not matter. Software link only: the hardware-link idle owes
|
|
// the self-update tests a command loop, and a crystal deployment has
|
|
// nothing to measure.
|
|
while (true) {
|
|
tx('A');
|
|
tx('P');
|
|
tx('P');
|
|
tx('\r');
|
|
tx('\n');
|
|
dev::delay<50_ms>();
|
|
}
|
|
#else
|
|
while (true) {
|
|
}
|
|
#endif
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
link<dev::clock>::init();
|
|
#if !defined(PUREBOOT_HANDOVER)
|
|
link<dev::clock>::tx('A');
|
|
link<dev::clock>::tx('P');
|
|
link<dev::clock>::tx('P');
|
|
#endif
|
|
// The hand-over fixture stays silent: nothing is listening on the USART it
|
|
// brings up — the loader it hands to speaks those pins directly — so its
|
|
// banner would be a write into a peer that does not exist.
|
|
link<dev::clock>::idle();
|
|
}
|