Files
wheel_rfm69_counter/avr_code/m95128.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

57 lines
1.6 KiB
C

/*
* File: spi.h
* Author: thebears
*
* Created on December 6, 2024, 11:02 AM
*/
#include "defines.h"
#include "rfm69.h"
#include "spi.h"
#include "states.h"
#include "uart.h"
#include <avr/io.h>
#include <util/delay.h>
#ifndef M95128__H
#define M95128__H
#define EEPROM_WREN 0b00000110 // 0x06
#define EEPROM_WRDI 0b00000100 // 0x04
#define EEPROM_RDSR 0b00000101 // 0x05
#define EEPROM_WRSR 0b00000001 // 0x01
#define EEPROM_READ 0b00000011 // 0x03
#define EEPROM_WRITE 0b00000010 // 0x02
#define EEPROM_RDID 0b10000011 // 0x83
#define EEPROM_WRID 0b10000010 // 0x82
#define EEPROM_RDLS 0b10000011 // 0x83
#define EEPROM_LID 0b10000010 // 0x82
#define PAGE_SIZE 64
#define EEPROM_STATUS_WIP 0x01
// A page write takes ~5 ms; past this the device is not responding.
#define EEPROM_POLL_TIMEOUT_MS 100U
#ifdef __cplusplus
extern "C" {
#endif
void spi_eeprom_select(bool state);
void eeprom_write(uint8_t page, unsigned const char* msg, uint8_t msg_len);
void eeprom_read(uint8_t page, unsigned char* msg, uint8_t msg_len);
void eeprom_write_tx_data(uint8_t page, tx_rx_data_struct tx_data);
void write_page_address(uint8_t page);
uint8_t get_last_page(void);
tx_rx_data_struct eeprom_read_tx_data(uint8_t page);
void delete_last_page(void);
uint8_t get_last_page(void);
void write_last_page_value(uint8_t page);
void eeprom_clear_page(uint8_t page);
void write_page_address(uint8_t page);
void eeprom_write_tx_data(uint8_t page, tx_rx_data_struct tx_data);
void write_struct_to_last_page(tx_rx_data_struct tx_data);
tx_rx_data_struct read_struct_last_page(void);
#ifdef __cplusplus
}
#endif
#endif /* M95128__H */