Skip to content

Codex/sd mass storage hotplug - #267

Open
mkuettner97 wants to merge 10 commits into
2.3.4from
codex/sd-mass-storage-hotplug
Open

mkuettner97 wants to merge 10 commits into
2.3.4from
codex/sd-mass-storage-hotplug

Conversation

@mkuettner97

Copy link
Copy Markdown
Collaborator

SD card can be remounted after removal (unmount) and can be mounted as a USB device.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves SD-card hotplug robustness (safe teardown + remount) and enables exposing the SD card over USB mass storage by switching the FOTA build to Zephyr’s next-generation USB device stack.

Changes:

  • Add dedicated, preemptible work queues for SD-card lifecycle operations and sensor configuration to avoid blocking cooperative/system workqueues during long SD operations.
  • Refactor SD logger teardown coordination (park writer thread, abort recording on removal) and make unmount resilient when media disappears mid-write.
  • Introduce an MSC helper module and update build/Kconfig/CMake + prj configs to support next-gen USB MSC and reduce SD command retry/timeout delays after removal.

Reviewed changes

Copilot reviewed 16 out of 17 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
unicast_server/main.cpp Switch USB init to next-gen stack path and add MSC LUN + SD MSC init hook.
src/SensorManager/SensorManager.cpp Move sensor config application work onto a dedicated preemptible workqueue.
src/SensorManager/Kconfig Add Kconfig knobs for sensor config workqueue prio/stack.
src/SD_Card/SDLogger/SDLogger.h Add abort_recording() API for card-removal handling.
src/SD_Card/SDLogger/SDLogger.cpp Rework writer/teardown coordination (park handshake), removal abort path, and flush semantics.
src/SD_Card/SD_Card_Manager/SD_Card_Manager.h Minor formatting-only change.
src/SD_Card/SD_Card_Manager/SD_Card_Manager.cpp Add dedicated SD lifecycle workqueue; robust unmount/remount sequencing; disk deinit + load-switch release changes.
src/SD_Card/MassStorage/SDMassStorage.h New interface for syncing USB MSC media with SD presence.
src/SD_Card/MassStorage/SDMassStorage.cpp New implementation: init/deinit disk ref for MSC, shell status command.
src/SD_Card/MassStorage/CMakeLists.txt Build MSC helper only when next-gen USB + MSC class enabled.
src/Battery/PowerManager.cpp Keep SD + level shifter powered for USB MSC sessions (runtime PM gets).
prj.conf Reduce SD data timeout / retry count to bound card-removal teardown latency.
prj_fota.conf Switch to USB_DEVICE_STACK_NEXT + enable MSC class; adjust SD timeouts/retries.
Kconfig Pull in Zephyr sample-usbd Kconfig for next-gen USB init helpers.
include/openearable_common.h Document sd_msg semantics/order for removal vs unmount.
CMakeLists.txt Add MassStorage include/subdir; include Zephyr USB sample common.cmake when next-gen USB enabled.
CLAUDE.md New repository guidance doc (architecture + build/testing notes).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread unicast_server/main.cpp
Comment on lines +55 to +66
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
#include <zephyr/storage/disk_access.h>
#include <zephyr/usb/usbd.h>
#include <zephyr/usb/class/usbd_msc.h>
extern "C" {
#include <sample_usbd.h>
}

USBD_DEFINE_MSC_LUN(sd, "SD", "OpenEarable", "SD Card", "1.00");
#else
#include <zephyr/usb/usb_device.h>
#endif
Comment thread unicast_server/main.cpp
Comment on lines 81 to +85
/*sdcard_manager.init();

sdcard_manager.mount();*/

/* STEP 5.5 - Enable USB */
if (IS_ENABLED(CONFIG_USB_DEVICE_STACK)) {
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
Co-authored-by: DennisMoschina <45356478+DennisMoschina@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

Build output available:
openearable_v2_firmware.elf.zip
openearable_v2_fota.zip

@o-bagge

o-bagge commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Should we also migrate the non-FOTA prj.conf build to the new USB stack, or is USB MSC intended to be FOTA-only for now?

@TobiasRoeddiger
TobiasRoeddiger changed the base branch from main to 2.2.9 September 21, 2026 10:03
@TobiasRoeddiger TobiasRoeddiger mentioned this pull request Sep 28, 2026
@TobiasRoeddiger TobiasRoeddiger mentioned this pull request Oct 16, 2026
@TobiasRoeddiger
TobiasRoeddiger changed the base branch from 2.3.0 to 2.3.3 November 4, 2026 10:30
@TobiasRoeddiger TobiasRoeddiger mentioned this pull request Nov 4, 2026
@TobiasRoeddiger
TobiasRoeddiger changed the base branch from 2.3.3 to 2.3.4 November 16, 2026 10:17
@github-actions

Copy link
Copy Markdown

✅ Unit tests passed

9 passed, 0 failed/error, 0 skipped — view workflow run

Test scenario Platform Result
openearable.unit.battery_profile native_sim/native/64 ✅ passed
openearable.unit.bond_cleanup native_sim/native/64 ✅ passed
openearable.unit.audio_startup native_sim/native/64 ✅ passed
openearable.unit.csip_identity native_sim/native/64 ✅ passed
openearable.unit.audio_sync_math native_sim/native/64 ✅ passed
openearable.unit.sensor_component native_sim/native/64 ✅ passed
openearable.unit.sensor_transport native_sim/native/64 ✅ passed
openearable.unit.audio_encoder native_sim/native/64 ✅ passed
openearable.unit.ring_buffer native_sim/native/64 ✅ passed

Download the unit-test-results artifact for full Twister reports and logs.

@github-actions

Copy link
Copy Markdown

Compiler warnings

The extended-warning build completed successfully.

Application compiler warnings

Show warnings
src/SD_Card/SD_Card_Manager/SD_Card_Manager.cpp:507:33: warning: comparison of integer expressions of different signedness: 'ssize_t' {aka 'int'} and 'size_t' {aka 'unsigned int'} [-Wsign-compare]

View this workflow run

@github-actions

Copy link
Copy Markdown

CodeChecker static analysis (advisory)

Note

This report is informational. It does not block merging and does not change code.

At a glance

Scope Result
Findings on lines changed by this PR 0
Findings in the complete codebase 1
Reviewed intentional hardware accesses 13
Analyzer invocations completed 188
Analyzer invocations failed 0

Findings on changed lines

No non-style findings were reported on lines changed by this PR.

Analyzer coverage

Analyzer Completed Failed
clangsa 94 0
cppcheck 94 0

Complete-codebase summary

Severity: 1 high
Reported by analyzer: clangsa: 1

Complete-codebase findings

Each finding includes source context. The > line and carets identify the expression reported by the analyzer.

Show detailed findings with source context (1 of 1)
HIGH — boards/teco/openearable_v2/boot_charger_status.h:16 — Dereference of a fixed address (loaded from variable 'reg')
  15 | {
> 16 |     uint32_t value = *reg;
     |                       ^^^
  17 |     if ((value & ~BOOT_CHARGER_EVENTS) != BOOT_CHARGER_MARKER) return 0;

Open the workflow run and download the complete CodeChecker report for a detailed view. Check out codechecker.html/index.html.

13 known nRF5340 hardware-register accesses are reviewed as intentional using exact checker/path/hash rules in .codechecker-review.json. Other fixed-address and array-bound findings remain enabled. The artifact includes reviewed-hardware-accesses.json with the findings and reviewed-hardware-rules.json with the matching rules and reasons.

@github-actions

Copy link
Copy Markdown

Build output available:
openearable_v2_firmware.zip
openearable_v2_fota.zip

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.

5 participants