Skip to content

fix(sbgecom): fix stack overflow when sending frames on NuttX, fix serial port misconfiguration (B0 in SITL), fix timestamps and validity flags on mag data in both directions#27831

Open
tolesam wants to merge 7 commits into
PX4:mainfrom
SBG-Systems:bugfix/sbgecom-fixes

Conversation

@tolesam

@tolesam tolesam commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Solved Problem

The sbgECom driver had several defects breaking both directions of the link:

  • Every transmitted frame overflowed the TTY work queue stack on NuttX (the
    sbgECom library puts a 4096-byte buffer on a 1728-byte stack), corrupting
    heap memory.
  • A broken termios override (c_cflag &= CSIZE) cleared CREAD and set the
    port to B0 in SITL.
  • Transmitted mag aiding logs carried status = 0 (failing BIT, out of
    range, uncalibrated) and a bogus timestamp, so the INS rejected them.
  • Received mag data was published with the INS timestamp instead of PX4 hrt
    time.

Solution

  • Run the driver on the INS0 work queue (6000-byte stack).
  • Remove the termios block — sbgInterfaceSerialCreate() already configures
    raw 8N1.
  • Outgoing mag logs: set BIT/in-range/calibration-ok flags, send timestamp 0
    and let the INS stamp on arrival.
  • Incoming mag data: publish with hrt_absolute_time(), like accel/gyro.
  • Cleanup: cancel perf counters on error paths, drop duplicate send-failure
    warnings.

Changelog Entry

For release notes:

Bugfix: sbgECom driver - fix stack overflow when sending frames on NuttX, fix serial port misconfiguration (B0 in SITL), fix timestamps and validity flags on mag data in both directions
No new parameters.
No documentation changes.

Alternatives

N/A

Test coverage

  • SITL with a recorded sbgECom stream replayed over a pty: received topics
    publish with coherent PX4 timestamps; outgoing mag frames captured and
    checked for status flags and zero timestamp.
  • Bench test against an SBG INS on serial: no more send-side reception
    errors; INS accepts the mag aiding logs.

Context

N/A

Samuel TOLEDANO added 7 commits July 7, 2026 11:32
The sbgECom library allocates SBG_ECOM_MAX_BUFFER_SIZE (4096) bytes on
the stack in sbgEComProtocolSendStandardFrame() whenever a frame is
sent (mag/air data aiding logs, configuration commands at init). The
TTY work queues only have a 1728 byte stack by default
(CONFIG_WQ_TTY_STACKSIZE), so every send overflowed the stack by more
than 2 KB, corrupting adjacent heap memory and causing spurious
reception errors.

Run on the INS0 work queue instead (6000 bytes by default).

Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
The custom termios block applied 'c_cflag &= CSIZE', which keeps only
the character size bits and clears everything else: CREAD (receiver
enable) and, on Linux where the baud rate lives in c_cflag, the CBAUD
bits, so the port would be reconfigured to B0 in SITL. The intent was
most likely '&= ~CSIZE' followed by '|= CS8'.

The whole block is redundant anyway: sbgInterfaceSerialCreate()
already configures the port in raw 8N1 mode with software/hardware
flow control disabled, OPOST cleared and the buffers flushed, so
remove it entirely.

Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
The mag log was sent with status = 0, which reports the magnetometers
as failing built-in test, out of operating range and not calibrated,
so the INS is expected to reject the measurements even when the frame
is received correctly.

vehicle_magnetometer contains calibrated data, so set the BIT,
in-range and calibration-ok flags. The accelerometer bits are left
cleared as no accelerometer data is sent with this log.

Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
The mag log timeStamp field is defined as time since the sensor
powered up, but it was filled with PX4 hrt time truncated to 32 bits,
which is meaningless in the INS timebase and wraps every ~71 minutes.

Unlike SbgEComLogAirData there is no time-is-delay convention for this
log, so send 0 and let the INS timestamp the measurement on arrival.

Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
sensor_mag was published with the INS timestamp, which is the time
since the INS powered up and not in the PX4 hrt timebase, so
downstream consumers received wildly wrong timestamps. Use
hrt_absolute_time() like the accel and gyro handlers do.

Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
The sample and write elapsed counters were only ended on the success
paths, leaving dangling perf_begin() calls on reception or send
failures. Cancel them explicitly so the counters only account for
completed operations.

Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
sendAirDataLog() and sendMagLog() already log the specific failure
(payload serialization vs protocol send), so the additional generic
warnings in Run() only duplicated every error message.

Signed-off-by: Samuel TOLEDANO <samuel.toledano@sbg-systems.com>
@github-actions github-actions Bot added scope:drivers Device drivers and hardware interfaces. scope:sensors Sensor pipeline, calibration, voting, or sensor validation. labels Jul 7, 2026
@tolesam tolesam changed the title Bugfix/sbgecom fixes Bugfix: sbgECom driver - fix stack overflow when sending frames on NuttX, fix serial port misconfiguration (B0 in SITL), fix timestamps and validity flags on mag data in both directions Jul 7, 2026
@tolesam tolesam changed the title Bugfix: sbgECom driver - fix stack overflow when sending frames on NuttX, fix serial port misconfiguration (B0 in SITL), fix timestamps and validity flags on mag data in both directions fix(sbgecom): fix stack overflow when sending frames on NuttX, fix serial port misconfiguration (B0 in SITL), fix timestamps and validity flags on mag data in both directions Jul 7, 2026
@github-actions github-actions Bot added the kind:bug Something is broken or behaving incorrectly. label Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:bug Something is broken or behaving incorrectly. scope:drivers Device drivers and hardware interfaces. scope:sensors Sensor pipeline, calibration, voting, or sensor validation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant