🛠 Log CAN FD Data on XIAO STM32C5
Objectives
This tutorial aims to configure the integrated CAN FD controller (FDCAN2) of the XIAO STM32C5 to receive data frames and log them to the 16 MB external flash memory. You will learn how to initialize the CAN bus, manage interrupts, and use the file system for persistent storage.
Required Hardware
- Seeed Studio XIAO STM32C5 board.
- USB-C cable for power and debugging.
- External CAN transceiver or CAN FD module to generate traffic (optional for testing, as the TCAN3403-Q1 is embedded on FDCAN2).
- Computer with STM32CubeIDE or Zephyr RTOS installed.
Steps
- Project Initialization: Create a new project in STM32CubeIDE by selecting the STM32C5A3CG microcontroller. Enable the 48 MHz external clock to ensure the timing accuracy required by CAN FD.
- FDCAN2 Configuration: In the device configurator, enable FDCAN2. Note that the TCAN3403-Q1 transceiver is already connected with a 120 Ω termination resistor. Configure the bus speed (e.g., 500 kbps for arbitration, 2 Mbps for data) and enable receive interrupts.
- External Memory Management: The board features 16 MB of external flash. Configure the corresponding QSPI or SPI driver in the code to access this memory. Initialize a simple file system (like LittleFS) to structure data into files.
- Reception and Storage Code: Implement the CAN interrupt callback function. Upon each received frame, read the ID and data. Write this information to a text or binary file on the external flash. Ensure you add a timestamp for each entry.
- Compilation and Upload: Compile the project and upload it to the board via the USB-C port. Open the serial monitor to verify debug messages indicating successful CAN and file system initialization.
Verification
Connect a CAN device to the FDCAN2 bus. Send data frames. Check on the serial monitor that frames are detected. Use a file read command to confirm that data is persisted in external memory after a board restart.
Going Further
Explore using the LSM6DS3TR-C inertial sensor to enrich CAN data with motion measurements. Activate the PWM-programmable heating circuit to stabilize measurements before logging them, creating a high-precision logbook.