ed476473b6
Three defects prevented the board from working at all: - INT0/INT1 were falling-edge triggered. Edge detection needs the I/O clock, which SLEEP_MODE_PWR_DOWN stops, so neither the reed switch nor the RTC alarm could wake the MCU. Both are now low-level triggered (the only asynchronous mode), and each handler masks its own interrupt while the source is still asserted so the low level cannot retrigger. The reed and RTC pins also get their pull-ups; they were explicitly driven low. - Statics were 1440 B of 2048 with a 538 B main frame, so the first NFC read ran the stack into .data. Shrank the oversized buffers and made the NFC scratch buffer static: statics 1440 -> 1038 B, main frame -> 204 B. - The FIFO was filled after entering TX mode with TxStart = FifoNotEmpty, so transmission began before the payload was loaded. Load in standby. Memory safety: clamp the unvalidated RX length (len - 4 underflowed to >=252 into a 60-byte buffer), fix writes one byte past DATA_BUFFER_65, fix the diameter copy length in st25dv.c, NUL-terminate remove_spaces, and bounds-check the NDEF parser (dropping its tag-sized VLA and its unchecked payload_length decrements). Hangs: add bail-outs to every peripheral poll loop - RFM69 mode/TX/RX waits, the EEPROM WIP poll, all six I2C TWINT spins, and the ADC. The LDO is cut before sleeping, so a slow peripheral hung the firmware with no watchdog armed. Correctness: boot no longer wipes the EEPROM spool; the replayed packet is sent once and deleted only on success; short ATOMIC_BLOCK sections replace the blanket cli() that lost reed pulses during the radio window; the I2C rail comes up before the RTC is touched; sleep_bod_disable() moves into the timed sequence with the sleep race closed; sei() no longer runs inside ISRs; REG_FDEV_MSB was 0x06 twice so deviation was 0; ADC uses return ADC and a /64 prescaler; SS1 is an output before SPE is set. VAL_DATA_MODUL_OOK was misnamed rather than wrong - 0x01 lands in ModulationShaping, not ModulationType - so the register value is unchanged and on-air behavior still matches the base station. Verified: builds clean under -Wall -Wextra on both gnu17 and c23. Not yet run on hardware. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YVJKatfeMJjAmuH9KYiLuv
114 lines
2.2 KiB
C
114 lines
2.2 KiB
C
/*
|
|
* File: defines.h
|
|
* Author: thebears
|
|
*
|
|
* Created on December 18, 2024, 11:41 AM
|
|
*/
|
|
|
|
#ifndef DEFINES_H
|
|
#define DEFINES_H
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include "stdint.h"
|
|
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#ifndef F_CPU
|
|
#define F_CPU 8000000UL // 8 MHz clock speed; prefer -DF_CPU=8000000UL in the build flags
|
|
#endif
|
|
#define BAUD 38400
|
|
#define F_SCL 200000UL
|
|
|
|
#define DO_UART true
|
|
#define ITERATING false
|
|
|
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
|
#define MAX(a,b) (((a)>(b))?(a):(b))
|
|
|
|
extern unsigned char DATA_BUFFER_65[65];
|
|
extern uint8_t DATA_BUFFER_7[7];
|
|
//extern uint8_t DATA_BUFFER_254[255];
|
|
extern unsigned char DATA_BUFFER_20[20];
|
|
|
|
typedef struct {
|
|
uint8_t payload_len;
|
|
char payload[48];
|
|
uint8_t success;
|
|
} ndef_message;
|
|
extern ndef_message NDEF_MSG;
|
|
|
|
typedef struct {
|
|
char str[21]; // Pointer to the modified string
|
|
size_t length; // Length of the modified string
|
|
} trimmed_string_struct;
|
|
extern trimmed_string_struct TRIMMED_STRING;
|
|
|
|
typedef struct {
|
|
uint8_t name_len;
|
|
char name_str[16];
|
|
char diameter_str[16];
|
|
uint8_t diameter_len;
|
|
uint8_t hashed;
|
|
} identifier_results;
|
|
extern identifier_results IDENTIFIER;
|
|
|
|
|
|
typedef struct {
|
|
uint8_t len;
|
|
uint8_t to;
|
|
uint8_t from;
|
|
uint8_t dtype;
|
|
uint8_t flags;
|
|
unsigned char msg[60];
|
|
} tx_rx_data_struct;
|
|
extern tx_rx_data_struct TX_DATA;
|
|
extern tx_rx_data_struct RX_DATA;
|
|
|
|
typedef struct {
|
|
uint8_t Second; // 0-59
|
|
uint8_t Minute; // 0-59
|
|
uint8_t Hour; // 0-23
|
|
uint8_t Wday; // Day of week, 1-7 (1 = Sunday)
|
|
uint8_t Day; // 1-31
|
|
uint8_t Month; // 1-12
|
|
uint8_t Year; // Full year (e.g., 2024)
|
|
} time_struct;
|
|
extern time_struct TIME;
|
|
|
|
|
|
typedef enum {
|
|
RTC_RFM69_SET_TIME_SUCCESS = 1,
|
|
RTC_RFM69_SET_TIME_FAILED = 2,
|
|
} RTC_RFM69_STATUS;
|
|
|
|
|
|
typedef enum // Goes into ID
|
|
{ DATA_SEND_SUCCESS = 1,
|
|
DATA_NOT_SENT = 2 } DATA_SEND_STATUS;
|
|
|
|
|
|
typedef enum // Goes into ID
|
|
{ MSG_TYPE_STRING = 1,
|
|
MSG_TYPE_BINARY = 2 } MSG_DATA_TYPE;
|
|
|
|
typedef enum // Goes into flags
|
|
{ MSG_REQ_CTIME = 1,
|
|
MSG_RESP_CTIME = 2,
|
|
MSG_SEND_COUNTS = 31,
|
|
MSG_RECV_COUNTS_SUCCESS = 32,
|
|
MSG_RECV_COUNTS_FAIL = 33,
|
|
MSG_RESENT_COUNTS = 34} MSG_REQUEST_TYPE_FLAG;
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* DEFINES_H */
|
|
|