Skip to content

4 - SD Subsystem#14

Open
KshitijKapoor8 wants to merge 5 commits intomasterfrom
4-sd
Open

4 - SD Subsystem#14
KshitijKapoor8 wants to merge 5 commits intomasterfrom
4-sd

Conversation

@KshitijKapoor8
Copy link
Copy Markdown
Collaborator

@KshitijKapoor8 KshitijKapoor8 commented Feb 22, 2026

Overview

Closes #4

This PR introduces a proper SD Card logging system to ESP-DAQ. This system writes to SD Card over SDIO and integrates with the existing TWAI and CAN system. This PR also brings certain fixes to the CAN system, especially regarding memory management.

Design

daqdesign

Diagram note: red represents data flow

The SD Card is mounted via the ESP_VFS module, allowing for normal POSIX calls to the file system. FAT32 (ESP's FatFS) is used.

The SD Card system is a double buffer, two task, non-blocking system. Buffer A is written to until full, then swapped out with buffer B, which is written to. Meanwhile, a background task flushes the buffer to the SD card.

Timestamping is also introduced. Session files are created on SD card init. The behavior changes depending on whether we have real time from GPS or not. If so, a file in the format /sdcard/LOGS/YYYY/MM/DD/SESSION_N is created. Otherwise, a file at /sdcard/NOTIME/SESSION_RANDOM is created.

Data is logged in the format:

SSSSSSSSS.UUUUUU,CANID,DDDDDDDDDDDDDDDD\n

Which is seconds.micro,can id,data.

/sdcard/META is also created. This is where any metadata for the DAQ system may live, such as CAN ID Filtration lists.

Summary of Changes

  • Implemented SD card subsystem
  • SD writes
  • SD reads
  • SD tests
  • SD double buffering
  • SD multiple tasks
  • SD write batching
  • SD card data / metadata structure
  • CAN fanout changes
  • TWAI documentation fixes
  • Buffer size changes
  • General code quality and documentation
  • Added python scripts for minor data parsing and testing

TODOs

  • Handle late-acquisition of real time
  • Add CAN filtration
  • Add SDIO Card Detect

Notes

  • This code has been tested with the following setup:
    Laptop -> (via socketcan) UCAN device -> (via CAN transceiver) ESP -> (via code, TWAI, SDIO) SD Card.
    Currently, the two python scripts in the PR are used for testing. 400kbps data has been handled with minimal data drop.

@KshitijKapoor8 KshitijKapoor8 changed the title 4 sd 4 - SD Subsystem Feb 22, 2026
Copy link
Copy Markdown

@SamrutGadde SamrutGadde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly looks really good, only critique could be using some C++ features to protect those raw pointers, but ngl I think this is fine. We tested so this is good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement SD Card logging

2 participants