#ifndef CONFIG_H #define CONFIG_H #pragma once #include // ===================================================== // HARDWARE REVISION FLAGS // ===================================================== // Uncomment each line only when the corresponding signal // is physically routed on the PCB. Undefined flags compile // out all associated code — no floating-pin side effects. // // Current PCB v1 population: // CAN STBY (pin 18) -> NOT routed. Transceiver controlled via XSTBY in IOCON. // CAN nINT/nINT1 -> NOT routed. Interrupt events polled in main loop. // MPS -> 5-pin connector: Tx, Rx, GND, Vin, Vout only. // No NRST, no digital alarm, no switched power. // LEDs -> NOT populated. // #define HW_HAS_CAN_STBY // MCP251863 STBY (pin 18) not routed on v1 // Transceiver standby handled via XSTBY in IOCON // #define HW_HAS_CAN_INT // nINT not routed in v1 — polled in firmware // #define HW_HAS_LEDS // LEDs not populated in v1 // ===================================================== // SYSTEM CLOCK // ===================================================== #define F_CPU_HZ 20000000UL // ATtiny3226 internal 20 MHz // ===================================================== // UART — MPS Sensor (USART0, DEFAULT mux) // ===================================================== // PCB routing: PB3 = TX (pin 11), PB2 = RX (pin 12) // CONFIRMED from ATtiny3226 device header: USART0 DEFAULT = PB[3:0]. // (The ATtiny3226 has only USART0 and USART1 in silicon — no USART2. // USART1 DEFAULT is PA[4:1]; USART1 ALT1 is PC[3:0]. Neither matches // PB2/PB3, so USART0 is the only peripheral that fits this wiring.) // Note: PB3 shares TOSC1 — do NOT fit a 32 kHz crystal. #define MPS_UART_BAUD_RATE 38400UL // From NevadaNano MPS 5.0 User Manual (Table 1) #define MPS_UART_TX_PIN PIN_PB3 // Pin 11 — USART0 TXD (DEFAULT mux) #define MPS_UART_RX_PIN PIN_PB2 // Pin 12 — USART0 RXD (DEFAULT mux) // Ring-buffer sizes (must be powers of 2) #define UART_TX_BUF_SIZE 64 #define UART_RX_BUF_SIZE 128 // ===================================================== // UART — Debug monitor (USART1, ALT1 mux) // ===================================================== // PCB routing: PC2 = TX (pin 17), PC1 = RX (pin 16) // CONFIRMED from ATtiny3226 device header: USART1 ALT1 = PC[3:0]. // Compiled in only when DEBUG_UART is defined (platformio.ini). #define DBG_UART_BAUD_RATE 115200UL #define DBG_UART_TX_PIN PIN_PC2 // Pin 17 — USART1 TXD (ALT1 mux) #define DBG_UART_RX_PIN PIN_PC1 // Pin 16 — USART1 RXD (ALT1 mux) // ===================================================== // SPI — MCP251863 CAN controller (SPI0, DEFAULT MUX) // ===================================================== // PCB routing: // PA1 = MOSI (pin 20) | PA2 = MISO (pin 1) // PA3 = SCK (pin 2) | PA4 = CS (pin 5, software) // No PORTMUX remapping needed — this is the factory default. #define SPI_MOSI_PIN PIN_PA1 // Pin 20 #define SPI_MISO_PIN PIN_PA2 // Pin 1 #define SPI_SCK_PIN PIN_PA3 // Pin 2 #define SPI_CS_CAN_PIN PIN_PA4 // Pin 5 — software CS #define SPI_CLOCK_HZ 4000000UL // 4 MHz — within MCP251863 spec #define SPI_MODE SPI_MODE0 // CPOL=0, CPHA=0 #define SPI_BIT_ORDER MSBFIRST // ===================================================== // MPS ANALOG OUTPUT — backup ADC reading // ===================================================== // PCB routing: PC0 = Vout MPS (pin 15) — analog 0-3.3 V // Represents gas concentration as a voltage. Used as a // cross-check against the primary UART digital interface. #define MPS_VOUT_PIN PIN_PC0 // Pin 15 — AIN14 #define MPS_VOUT_ADC_CH 14 // AIN14 maps to PC0 // ===================================================== // CAN — MCP251863 // ===================================================== #define CAN_NOMINAL_BAUD 500000UL // 500 kbit/s nominal #define CAN_DATA_BAUD 2000000UL // 2 Mbit/s data phase (CAN FD) #define CAN_TX_FIFO_SIZE 8 #define CAN_RX_FIFO_SIZE 16 // 11-bit standard CAN IDs #define CAN_ID_H2_STATUS 0x100 // Periodic concentration + alarm level #define CAN_ID_H2_ALARM 0x101 // Alarm level change event #define CAN_ID_H2_SENSOR_INFO 0x102 // Sensor version / info #define CAN_ID_H2_ENV 0x103 // Temperature, pressure, humidity #define CAN_ID_CMD_REQUEST 0x200 // Incoming command from host // MCP251863 STBY pin — only if routed (v2+ PCB) #ifdef HW_HAS_CAN_STBY #define CAN_STBY_PIN PIN_PB4 // PB4 — drive LOW to enable transceiver #endif // MCP251863 nINT pin — only if routed (v2+ PCB) #ifdef HW_HAS_CAN_INT #define CAN_INT_PIN PIN_PA5 // PA5 — active-low general interrupt #endif // ===================================================== // STATUS LEDs (v2+ PCB only) // ===================================================== #ifdef HW_HAS_LEDS #define LED_STATUS_PIN PIN_PA5 // Green — normal operation #define LED_ALARM_PIN PIN_PA6 // Red — gas alarm #define LED_FAULT_PIN PIN_PA7 // Yellow — sensor / system fault #endif // ===================================================== // TIMING & INTERVALS (milliseconds) // ===================================================== #define MPS_POLL_INTERVAL_MS 2000 // Matches sensor's 0.5 Hz refresh rate (datasheet Sec 2.1.4) #define MPS_STARTUP_TIMEOUT_MS 50000 // Startup (31s) + Initialization (12s) + margin (datasheet Fig 6) #define MPS_RESPONSE_TIMEOUT_MS 200 // Max wait for a single UART reply #define CAN_TX_INTERVAL_MS 500 // Periodic CAN broadcast period #define WATCHDOG_TIMEOUT_MS 2000 // Software watchdog kick interval // ===================================================== // ALARM THRESHOLDS (%LEL) // ===================================================== #define ALARM_LEVEL_1_LEL 10 // Warning #define ALARM_LEVEL_2_LEL 25 // Alarm #define ALARM_LEVEL_3_LEL 50 // High alarm // ===================================================== // MPS PACKET / PROTOCOL // ===================================================== #define MPS_PACKET_MAX_PAYLOAD 64 #define MPS_REPLY_HEADER_SIZE 6 // Reply: cmdID(1)+status(1)+length(2)+checksum(2) #define MPS_REQUEST_HEADER_SIZE 8 // Request: cmdID(2)+length(2)+reserved(2)+checksum(2) #define MPS_PACKET_HEADER_SIZE MPS_REPLY_HEADER_SIZE // kept for backward compatibility #define MPS_PACKET_MAX_TOTAL (MPS_REQUEST_HEADER_SIZE + MPS_PACKET_MAX_PAYLOAD) #define MPS_CRC_POLYNOMIAL 0x1021 // CRC-16/CCITT #define MPS_CRC_INIT 0xFFFF #endif // CONFIG_H