bbdcc1e623
- 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
78 lines
2.0 KiB
C
78 lines
2.0 KiB
C
|
|
#include "interrupts.h"
|
|
|
|
void init_pins(void)
|
|
{
|
|
|
|
// The reed switch (PD3/INT1) switches to ground and the RTC alarm output
|
|
// (PD2/INT0) is open-drain, so both need the internal pull-up. Leaving them
|
|
// floating makes the inputs self-trigger.
|
|
SET_PIN_IN(DDRD, DDD3);
|
|
SET_PIN_HIGH(PORTD, PD3);
|
|
|
|
SET_PIN_IN(DDRD, DDD2);
|
|
SET_PIN_HIGH(PORTD, PD2);
|
|
}
|
|
|
|
// Both external interrupts are configured low-level triggered (ISCn1:0 = 00).
|
|
// Edge detection needs the I/O clock, which SLEEP_MODE_PWR_DOWN stops, so a
|
|
// falling-edge INT0/INT1 can never wake the MCU. Only level detection is
|
|
// asynchronous. Each handler masks its own interrupt while the source is still
|
|
// asserted, so the low level does not retrigger in a loop.
|
|
|
|
void set_up_reed_interrupt(void)
|
|
{
|
|
EICRA &= ~((1 << ISC11) | (1 << ISC10));
|
|
EIMSK |= (1 << INT1);
|
|
}
|
|
|
|
void set_up_minute_interrupt(void)
|
|
{
|
|
EICRA &= ~((1 << ISC01) | (1 << ISC00));
|
|
EIMSK |= (1 << INT0);
|
|
}
|
|
|
|
void reed_interrupt_enable(void)
|
|
{
|
|
EIFR = (1 << INTF1); // Drop anything latched while we were masked
|
|
EIMSK |= (1 << INT1);
|
|
}
|
|
|
|
void minute_interrupt_enable(void)
|
|
{
|
|
EIFR = (1 << INTF0);
|
|
EIMSK |= (1 << INT0);
|
|
}
|
|
|
|
void wdt_isr_enable(void)
|
|
{
|
|
uint8_t sreg = SREG;
|
|
cli();
|
|
wdt_reset();
|
|
|
|
// WDRF keeps WDE set, which would block the write below, so it must go
|
|
// first. The unlock is a single assignment: a read-modify-write does not
|
|
// open the 4-cycle change window.
|
|
MCUSR &= ~(1 << WDRF);
|
|
WDTCSR = (1 << WDCE) | (1 << WDE);
|
|
|
|
// WDP[3:0] = 0b011 -> 0.125 s. Interrupt mode only (WDE clear), so an
|
|
// expiry wakes us to clear the debounce instead of resetting the part.
|
|
WDTCSR = (1 << WDIE) | (1 << WDP1) | (1 << WDP0);
|
|
|
|
SREG = sreg; // Restore, never blanket-sei(): these run inside an ISR
|
|
}
|
|
|
|
void wdt_isr_disable(void)
|
|
{
|
|
uint8_t sreg = SREG;
|
|
cli();
|
|
wdt_reset();
|
|
|
|
MCUSR &= ~(1 << WDRF);
|
|
WDTCSR = (1 << WDCE) | (1 << WDE);
|
|
WDTCSR = 0x00;
|
|
|
|
SREG = sreg;
|
|
}
|