Files
wheel_rfm69_counter/avr_code/max31329.h
T
thebears bbdcc1e623 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
2026-08-31 23:04:00 -04:00

59 lines
1.6 KiB
C

/*
* File: max31329.h
* Author: thebears
*
* Created on December 18, 2024, 1:24 PM
*/
#include "defines.h"
#include "i2c.h"
#include "rfm69.h"
#include "st25dv.h"
#include "uart.h"
#ifndef MAX31329_H
#define MAX31329_H
#ifdef __cplusplus
extern "C" {
#endif
#define I2C_ADDR 0x68
// MAX31329 register map (the subset this driver touches)
#define RTC_REG_STATUS 0x00 // Reading clears the alarm flags / releases INTB
#define RTC_REG_INT_EN 0x01
#define RTC_REG_CONFIG2 0x04
#define RTC_REG_SECONDS 0x06 // Start of the 7-byte BCD time block
#define RTC_REG_ALM2_MIN 0x13 // Start of the 3-byte alarm-2 block
#define RTC_INT_EN_A2IE 0b00000010 // Alarm-2 interrupt enable
#define RTC_ALM_MASK_BIT 0x80 // "Don't match this field" bit in each alarm register
#define DEC2BCD(n) ((n) + (6 * ((n) / 10)))
#define BCD2DEC(n) ((n) - (6 * ((n) >> 4)))
// time_struct.Year is years since 2000, which is exactly what the RTC's 2-digit
// year register holds -- no offset. (The old +/-30 round-tripped but stored the
// wrong year in the RTC and overflowed BCD above 2069.)
#define tmYearToY2k(Y) (Y)
#define y2kYearToTm(Y) (Y)
uint8_t rtc_enable_interrupts(void);
uint8_t rtc_set_per_minute_alarm(void);
uint8_t rtc_read_time_array(uint8_t* data);
time_struct rtc_read_time(void);
uint8_t rtc_write_time(time_struct tm);
uint8_t rtc_set_alarm_config(void);
uint8_t rtc_read_status_register(void);
uint8_t rtc_read_interrupt_register(void);
uint8_t rtc_read_register(uint8_t addr);
void uart_print_rtc_time(time_struct td);
RTC_RFM69_STATUS set_time_from_rfm69(identifier_results id_data);
#ifdef __cplusplus
}
#endif
#endif /* MAX31329_H */