Skip to content

Progress Log: RTC Time Support Implementation

Task Description

Implemented complete RTC (Real-Time Clock) time support for the OSECHI cosmic ray detector firmware, enabling:

  1. User Story 1: SET_TIME command - Allow researchers to set absolute device time via serial
  2. User Story 2: GET_TIME command - Query current device time
  3. User Story 3: Timestamp field - Add unix_timestamp to all detection event outputs
  4. User Story 4: Feature flag control - All RTC code guarded by #if ENABLE_RTC directives
  5. User Story 5: Power persistence - Time persists across power cycles using ESP32 RTC

Outcome

✅ Successfully Completed

Core Implementation:

  • ✅ T005-T008: SET_TIME command handler with input validation (parses unsigned integers, validates range [0, 4294967295])
  • ✅ T009-T011: GET_TIME command handler returning current unix timestamp in JSONL format
  • ✅ T012-T017: Extended sensor_data_t struct with unix_timestamp field; updated all formatters (SSV/TSV/CSV/JSONL)
  • ✅ T018: Verified all RTC code properly guarded by #if ENABLE_RTC directives
  • ✅ T019: Verified ESP32 RTC API (time.h, sys/time.h) supports persistence across power cycles

Build Verification:

  • ✅ Firmware compiles successfully with ENABLE_RTC=1
  • ✅ RAM usage: 7.7% (added ~8 bytes from g_time_initialized state)
  • ✅ Flash usage: 24.1% (well within budget)

Code Changes:

  • Modified: src/text_command_handlers.cpp - Added SET_TIME/GET_TIME handlers + command table entries
  • Modified: src/rtc_manager.cpp - Fixed includes: added #include <sys/time.h> for settimeofday()
  • Modified: include/sensor_data.h - Added unix_timestamp field (guarded by #if ENABLE_RTC)
  • Modified: src/main.cpp - Populate unix_timestamp during detection event processing
  • Modified: src/sensor_formatter.cpp - Extended send_sv() and send_jsonl() to output unix_timestamp

Commits:

  • d35f664 feat(rtc): add SET_TIME/GET_TIME command handlers with input validation
  • 6660c0f feat(rtc): add unix_timestamp field to sensor data and formatters
  • f29ed0f docs(rtc): mark Phase 3-7 tasks complete in tasks.md

Learnings

  1. ESP32 RTC Headers: The settimeofday() function requires #include <sys/time.h> (not just <time.h>)
  2. Initial build failed without this header despite including time.h
  3. Solution: Added conditional include guarded by #if ENABLE_RTC to text_command_handlers.cpp

  4. Sensor Data Structure Evolution: The sensor_data_t struct has grown gracefully with optional fields:

  5. BME280 fields (temperature, pressure, humidity) → #if ENABLE_BME280
  6. Timestamp fields (uptime_ms, timedelta_us) → #if ENABLE_TIMESTAMP
  7. RTC timestamp (unix_timestamp) → #if ENABLE_RTC
  8. This design scales well and maintains backward compatibility

  9. Compile-Time Feature Flags: All RTC code is properly excluded when ENABLE_RTC=0:

  10. Zero runtime overhead when disabled
  11. Command table entries guarded
  12. sensor_data_t field conditionally compiled
  13. Output formatters handle missing field gracefully

  14. Command Handler Pattern: SET_TIME/GET_TIME handlers follow existing patterns in text_command_handlers.cpp:

  15. Argument count validation
  16. Parse using strtoul() with endptr checking
  17. JSONL response formatting via ArduinoJson StaticJsonDocument
  18. Error responses with error codes (1=invalid_format, 2=out_of_range)

Next Steps

  1. T020: Create detailed RTC documentation in docs/rtc-implementation.md
  2. T021: Add RTC feature description to CLAUDE.md Timestamp Tracking section
  3. T022: Update CHANGELOG.md with RTC feature entry
  4. T023: Document build size impact (ENABLE_RTC=0 vs =1)
  5. T024: Manual serial monitor testing (SET_TIME, GET_TIME, detection event timestamps)
  6. Release: Prepare v1.10.0 release with RTC feature (Phase 3 of Timestamp Tracking)