Refactor firmware for clarity; no functional changes

- main.c: file-header comment describing the hardware and operation, logic
  split into named phases (sleep_until_interrupt, wake_peripheral_rails,
  take_counts_snapshot, send_wheel_counts_report, handle_minute_alarm,
  init_all_hardware); shared state renamed to say what it is and made static.
- rfm69.c: reorganized into six labeled sections; cond_1/2/3 and hash scratch
  globals replaced by a reply_acknowledges() helper with clear locals; packet
  layout and every init register write documented.
- LOG() macro (compiled out when DO_UART is off) replaces the #if DO_UART
  blocks that obscured the logic.
- Drivers: file-header comments; named RTC_REG_*/RTC_ALM_MASK_BIT constants;
  EEPROM spool scheme documented; ADC_CHANNEL_BANDGAP named; repeated pin
  if/else helpers collapsed to SET_PIN_TO().
- Removed unused globals/buffers and commented-out code; ran clang-format
  with the project style.

Register writes and radio protocol are byte-identical. Builds clean under
-Wall -Wextra on gnu17 and c23; flash 13028 -> 12830 B, static RAM
1038 -> 999 B.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YVJKatfeMJjAmuH9KYiLuv
This commit is contained in:
2026-08-31 23:04:00 -04:00
parent ed476473b6
commit bbdcc1e623
25 changed files with 825 additions and 1798 deletions
+14 -10
View File
@@ -1,16 +1,21 @@
// M95128 SPI EEPROM used as a LIFO spool for unacknowledged radio packets.
//
// Page 0, byte 0: depth of the spool (the highest page currently in use;
// 0 means empty)
// Pages 1..N: one 64-byte packet each -- 60 msg bytes followed by
// flags, from, to, dtype
//
// write_struct_to_last_page() pushes, read_struct_last_page() peeks, and
// delete_last_page() pops.
#include "m95128.h"
uint8_t read_value;
uint8_t old_last_page;
uint8_t new_last_page;
// EEPROM chip select (PB0, active low)
void spi_eeprom_select(bool state)
{
SET_PIN_OUT(DDRB, DDB0);
if (!state) {
SET_PIN_HIGH(PORTB, PB0);
} else {
SET_PIN_LOW(PORTB, PB0);
}
SET_PIN_TO(PORTB, PB0, !state);
}
void eeprom_write(uint8_t page, unsigned const char* msg, uint8_t msg_len)
@@ -68,13 +73,12 @@ void eeprom_read(uint8_t page, unsigned char* msg, uint8_t msg_len)
void delete_last_page(void)
{
old_last_page = get_last_page();
uint8_t old_last_page = get_last_page();
if (old_last_page == 0) // If we have nothing, no need to delete anything
{
return;
}
new_last_page = old_last_page - 1;
write_last_page_value(new_last_page);
write_last_page_value(old_last_page - 1);
eeprom_clear_page(old_last_page);
}