Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions .github/scripts/firmware_size_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

Usage: firmware_size_report.py <rp.elf> <rp.elf.map> <title>

Used by the CI workflows to fill the job summary. Section sizes come from
arm-none-eabi-size; the heap, the code copied to RAM and the flash use come
from the linker map, so they follow memmap_rp.ld if it changes.
Used by the CI workflows to fill the job summary and printed after every local
link (EPIC-12 STORY-06). Section sizes come from arm-none-eabi-size; the heap,
the stack, the code copied to RAM and the flash use come from the linker map,
so they follow memmap_rp.ld if it changes.
"""
import re
import subprocess
Expand Down Expand Up @@ -73,8 +74,9 @@ def main():
(".data (copied to RAM)", sizes.get(".data", 0)),
("Code copied to RAM", code_in_ram(map_text, stack_limit)),
(".bss", sizes.get(".bss", 0)),
("Heap (end of .bss to __StackLimit)", stack_limit - map_symbol(map_text, "__bss_end__")),
("Core-0 stack reserved", sizes.get(".stack_dummy", 0)),
("Heap (__end__ to __StackLimit)", stack_limit - map_symbol(map_text, "__end__")),
("Core-0 stack reserved",
map_symbol(map_text, "__StackTop") - map_symbol(map_text, "__StackBottom")),
("Flash used", map_symbol(map_text, "__flash_binary_end") - FLASH_ORIGIN),
]
print(f"### Firmware size: {title}\n")
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Module.symvers
Mkfile.old
dkms.conf

*.pyc
ffconf.h.bak
secrets.sh
upload_s3.sh
Expand Down
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,7 @@ that the firmware recovers from a crash or a hang and reports it.
| `/api/v1/debug/test/panic` | Calls `panic()` from the main loop. The RP reboots with reason `panic`. |
| `/api/v1/debug/test/hardfault` | Reads an unmapped address from the main loop. The RP reboots with reason `hardfault`. |
| `/api/v1/debug/test/hang` | Spins forever in the main loop. After 8 s the watchdog reboots the RP with reason `hang`, phase `main_loop`. |
| `/api/v1/debug/test/stack-overflow` | Recurses until the stack reaches its guard. The MPU faults on the guarded 32 bytes at the stack bottom, so the RP reboots with reason `hardfault` and an `sp` just below `__StackBottom`, instead of writing down through the heap. |
| `/api/v1/debug/test/http-hang` | Spins forever inside the request handler and never answers. Reason `hang`, phase `http_request`. |
| `/api/v1/debug/test/stall` | Blocks the main loop for 500 ms without reading the ROM3 ring. With the ST streaming debug bytes, `rom3_overruns` goes up. |

Expand Down
23 changes: 23 additions & 0 deletions rp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,15 @@ endif()
# Pass the _DEBUG flag to the C compiler
add_definitions(-D_DEBUG=${_DEBUG})

# Guard the bottom of core 0's stack with the MPU (EPIC-12 STORY-05): an
# overflow faults at the guard instead of writing down through the heap and,
# past it, the cartridge window the ST reads. Both build types on purpose: in a
# shipping build the alternative is silent corruption that surfaces later as a
# mystery, while a fault reboots with a breadcrumb (D-03). Costs one of the
# eight MPU regions (nothing else here uses the MPU), the lowest 32 bytes of
# the stack, and a few instructions per core at boot.
add_definitions(-DPICO_USE_STACK_GUARDS=1)

# lwIP statistics are opt-in and debug-only (lwipopts.h). Set
# DEVOPS_LWIP_STATS=1 in the environment of a debug build to report the
# lwIP pool counters in GET /api/v1/system/health.
Expand All @@ -322,6 +331,20 @@ add_definitions(-DPICO_FLASH_ASSUME_CORE0_SAFE=1)
# undefined behaviour.
add_definitions(-DCFG_TUD_CDC_TX_BUFSIZE=1024)

# Print the RAM and flash budget after every link (EPIC-12 STORY-06), with the
# same script that fills the CI job summary, so a local build and a CI build
# report the same numbers. Skipped when no python3 is around.
find_package(Python3 COMPONENTS Interpreter QUIET)
if(Python3_Interpreter_FOUND)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_LIST_DIR}/../../.github/scripts/firmware_size_report.py
$<TARGET_FILE:${PROJECT_NAME}>
$<TARGET_FILE:${PROJECT_NAME}>.map
"$<IF:$<STREQUAL:${_DEBUG},0>,release,debug>"
VERBATIM)
endif()

# Remove unused data
# Symbols stay in rp.elf: they are not part of the flashed image, and crash
# addresses and debug probes need them.
Expand Down
66 changes: 66 additions & 0 deletions rp/src/chandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@
static TransmissionProtocol pendingProtocol;
static bool protocolPending = false;

// Diagnostics for the ST's synchronous handshake, read over SWD by symbol so
// a release build can be measured (EPIC-12 STORY-08). chandlerDropped counts
// commands thrown away because another was still pending; chandlerRepeated
// counts commands carrying the token of the command before them, which is what
// the ST's retry after a timeout looks like.
uint32_t chandlerHandled = 0;
uint32_t chandlerDropped = 0;
uint32_t chandlerRepeated = 0;
// Where the time of a command goes: chandlerBusyUs is what this side spends
// handling one (the SD write included), chandlerGapUs the time from answering
// one command to seeing the next, which is the ST's turnaround.
uint32_t chandlerBusyUs = 0;
uint32_t chandlerGapUs = 0;
uint32_t chandlerMaxGapUs = 0;
uint32_t chandlerMaxBusyUs = 0;
// Of that gap, how long the ST takes to touch the command window at all after
// being answered: its spin-wait polls the shared block ($FA2804), which is not
// this space, so no sample arrives until it starts sending the next command.
uint32_t chandlerQuietUs = 0;
uint32_t chandlerMaxQuietUs = 0;
// How many main-loop passes a frame spans between its first sample and its
// last, and how long this side spends draining the sample ring: tells a slow
// sender apart from slow polling on our part.
uint32_t chandlerFramePolls = 0;
uint32_t chandlerChecksumErrors = 0;
uint32_t chandlerPollUs = 0;
static volatile bool chandlerFrameInFlight = false;
static volatile bool chandlerAwaitingFirstSample = false;
static uint32_t chandlerLastToken = 0;
static uint32_t chandlerAnsweredAtUs = 0;

static uint32_t incrementalCmdCount = 0;

// Address of the random-token reply slot (chandler_loop publishes the
Expand Down Expand Up @@ -126,6 +157,7 @@ static inline void __not_in_flash_func(handle_protocol_command)(
uint16_t size = tprotocol_clamp_payload_size(protocol->payload_size);

if (protocolPending) {
chandlerDropped++;
if (!chandler_protocol_matches_pending(protocol, size)) {
DPRINTF("Ignoring protocol %04x (%u bytes) while %04x is pending\n",
protocol->command_id, protocol->payload_size,
Expand Down Expand Up @@ -159,6 +191,7 @@ bool chandler_injectProtocol(uint16_t commandId, const uint16_t *payload,

static inline void __not_in_flash_func(handle_protocol_checksum_error)(
const TransmissionProtocol *protocol) {
chandlerChecksumErrors++;
DPRINTF(
"Checksum error detected (CommandID=%x, Size=%x, Bytes Read=%x, "
"Chksum=%x, RTOKEN=%x)\n",
Expand All @@ -168,6 +201,13 @@ static inline void __not_in_flash_func(handle_protocol_checksum_error)(

static inline void __not_in_flash_func(chandler_consume_rom3_sample)(
uint16_t sample) {
if (chandlerAwaitingFirstSample) {
chandlerAwaitingFirstSample = false;
chandlerFrameInFlight = true;
uint32_t quiet = (uint32_t)time_us_32() - chandlerAnsweredAtUs;
chandlerQuietUs += quiet;
if (quiet > chandlerMaxQuietUs) chandlerMaxQuietUs = quiet;
}
// Consumer 2: debug-byte filter. External programs
// emit one byte per cartridge cycle by reading at $FBFF00 + c
// (where c is the byte). The captured value's high byte is then
Expand All @@ -194,13 +234,26 @@ static inline void __not_in_flash_func(chandler_consume_rom3_sample)(
// Invoke this function to process the commands from the active loop in the
// main function
void __not_in_flash_func(chandler_loop)() {
uint32_t pollStartUs = (uint32_t)time_us_32();
bool frameWasInFlight = chandlerFrameInFlight;
commemul_poll(chandler_consume_rom3_sample);
chandlerPollUs += (uint32_t)time_us_32() - pollStartUs;
if (frameWasInFlight || chandlerFrameInFlight) {
chandlerFramePolls++;
}

if (!protocolPending) {
// No command to process
return;
}

uint32_t startedAtUs = (uint32_t)time_us_32();
if (chandlerAnsweredAtUs != 0) {
uint32_t gap = startedAtUs - chandlerAnsweredAtUs;
chandlerGapUs += gap;
if (gap > chandlerMaxGapUs) chandlerMaxGapUs = gap;
}

// Shared by all commands
// Read the random token from the command and increment the payload
// pointer to the first parameter available in the payload
Expand All @@ -222,6 +275,19 @@ void __not_in_flash_func(chandler_loop)() {
if (cur->cb) cur->cb(&pendingProtocol, payloadPtr);
}

chandlerFrameInFlight = false;
uint32_t busy = (uint32_t)time_us_32() - startedAtUs;
chandlerBusyUs += busy;
if (busy > chandlerMaxBusyUs) chandlerMaxBusyUs = busy;
chandlerAnsweredAtUs = (uint32_t)time_us_32();
chandlerAwaitingFirstSample = true;

chandlerHandled++;
if (randomToken == chandlerLastToken) {
chandlerRepeated++;
}
chandlerLastToken = randomToken;

incrementalCmdCount++;
TPROTO_SET_RANDOM_TOKEN64(
memoryRandomTokenAddress,
Expand Down
Loading
Loading