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
+23 -19
View File
@@ -1,9 +1,13 @@
// ST25DV NFC tag driver. The tag holds a single NDEF text record of the form
// "<name>,<wheel diameter>", which get_nugget_data() parses into IDENTIFIER.
#include "st25dv.h"
#define IDENT_NAME_MAX (sizeof(IDENTIFIER.name_str) - 1)
#define IDENT_DIAM_MAX (sizeof(IDENTIFIER.diameter_str) - 1)
static void set_identifier(const char* name, uint8_t name_len, const char* diam, uint8_t diam_len) {
static void set_identifier(const char* name, uint8_t name_len, const char* diam, uint8_t diam_len)
{
if (name_len > IDENT_NAME_MAX) {
name_len = IDENT_NAME_MAX;
}
@@ -21,7 +25,8 @@ static void set_identifier(const char* name, uint8_t name_len, const char* diam,
IDENTIFIER.hashed = hash(IDENTIFIER.name_str, 0, 59);
}
identifier_results get_nugget_data(void) {
identifier_results get_nugget_data(void)
{
NDEF_MSG = rfid_read_first_ndef_entry();
@@ -52,7 +57,8 @@ identifier_results get_nugget_data(void) {
return IDENTIFIER;
}
trimmed_string_struct remove_spaces(char* str, uint8_t len_str) {
trimmed_string_struct remove_spaces(char* str, uint8_t len_str)
{
const uint8_t max_len = sizeof(TRIMMED_STRING.str) - 1;
uint8_t j = 0;
@@ -69,16 +75,15 @@ trimmed_string_struct remove_spaces(char* str, uint8_t len_str) {
return TRIMMED_STRING;
}
void rfid_set_low_power_down(bool state) {
// LPD pin: high puts the tag's I2C interface into low-power mode
void rfid_set_low_power_down(bool state)
{
SET_PIN_OUT(DDRD, DDD5);
if (state) {
SET_PIN_HIGH(PORTD, PD5);
} else {
SET_PIN_LOW(PORTD, PD5);
}
SET_PIN_TO(PORTD, PD5, state);
}
ndef_message rfid_read_first_ndef_entry(void) {
ndef_message rfid_read_first_ndef_entry(void)
{
rfid_set_low_power_down(false);
rfid_set_i2c_power(true);
_delay_ms(1);
@@ -91,26 +96,25 @@ ndef_message rfid_read_first_ndef_entry(void) {
NDEF_MSG = readNDEFText(DATA_BUFFER_INTERNAL, NDEF_READ_LEN);
rfid_set_low_power_down(true);
rfid_set_i2c_power(false);
return NDEF_MSG;
}
uint8_t rfid_read_system_register(void) {
uint8_t rfid_read_system_register(void)
{
return read_one_byte_16bit_addr_no_err_register(I2C_SYSTEM_ADDR, 0x0000);
}
void rfid_set_i2c_power(bool state) {
// Switched supply for the tag's I2C side
void rfid_set_i2c_power(bool state)
{
SET_PIN_OUT(DDRE, DDE0);
if (state) {
SET_PIN_HIGH(PORTE, PE0);
} else {
SET_PIN_LOW(PORTE, PE0);
}
SET_PIN_TO(PORTE, PE0, state);
}
uint8_t rfid_read_memory(uint8_t* data, uint8_t num_bytes, uint16_t address) {
uint8_t rfid_read_memory(uint8_t* data, uint8_t num_bytes, uint16_t address)
{
return read_n_bytes_16bit_addr(I2C_USER_ADDR, address, data, num_bytes);
}