Skip to content

fix(r528s3-mibox-x1): serialise the icm20948 enable/disable transitions - #24

Merged
yanxingyu17 merged 2 commits into
open-vela:dev-ai-contest-2026from
tyx-crazy:icm20948-state-lock
Nov 25, 2026
Merged

yanxingyu17 merged 2 commits into
open-vela:dev-ai-contest-2026from
tyx-crazy:icm20948-state-lock

Conversation

@tyx-crazy

@tyx-crazy tyx-crazy commented Nov 18, 2026 •

Copy link
Copy Markdown

Follow-up to #23, which merged the board support and the SPI adapter fix.
This addresses the concurrency concern left open there.

Summary

work_cancel() does not wait for a worker that is already executing: it can
return -EBUSY. Instead of waiting, the worker performs the final idle and
sleep transition itself after its push path, under the shared state lock.
When the work is not running, the transition is done in the activate path,
and a worker that starts and finds the sensor already disabled does the
same before returning.

The three activate callbacks also had no shared state lock, so an enable
could wake the chip, be preempted before setting its *_enabled flag, and
race with the last disable seeing every flag clear and putting the chip
back to sleep. A mutex_t statelock now serialises those state transitions:
each callback is a thin wrapper that takes the lock and calls the original
logic, moved unchanged into *_activate_locked().

Impact

One file: chips/r528/drivers/rtos-hal/hal/source/sensor/icm20948/icm20948.c.
Only the ICM20948 driver changes; the lock is per device instance, so no
other sensor or board is affected.

Testing

Built from this branch and tested on R528 MIBOX-X1 (NAND image,
20,395,008 bytes, MD5 E644A694DBCBA86C59246B60A5C1344B).

icm20948_uorb was started from nsh and stopped with Ctrl-C, so every cycle
runs activate(true) and activate(false) for accel, gyro and mag. 80 cycles
in total, with scripted close delays of 120 ms / 700 ms / 1500 ms
(30 / 30 / 20):

close delay cycles hangs assert/panic clean exits
120 ms 30 0 0 30
700 ms 30 0 0 30
1500 ms 20 0 0 20

No hang, no assert or panic. Every run printed "Sensor icm20948 read
examples exit." and returned to the nsh prompt. Serial excerpts and the
firmware MD5 are in the test notes attached in the review thread.

Follow-up to open-vela#23, which merged the board support and the SPI adapter fix.
This addresses the concurrency concern that was left open there.

work_cancel() does not wait for a worker that is already executing: it can
return -EBUSY, and the sleep that follows would then land between a worker's
data read and its temperature read. The disable paths now use
work_cancel_sync(), which retries until the running worker has returned.

The three activate callbacks also had no shared state lock, so an enable
could wake the chip, be preempted before setting its *_enabled flag, and race
with the last disable seeing every flag clear and putting the chip back to
sleep. A mutex_t statelock now serialises those state transitions: each
callback is a thin wrapper that takes the lock and calls the original logic,
moved unchanged into *_activate_locked().

Verified on the board with the new image: repeated enable/disable cycles
across all three sensors, no deadlock, the device still sleeps when idle and
wakes on the next enable.

Signed-off-by: tyx <1815108523@qq.com>
@tyx-crazy
tyx-crazy requested review from a team as code owners November 18, 2026 03:52
@github-actions

Copy link
Copy Markdown

❌ CLA Signature Required

@tyx-crazy Some contributors need to sign the CLA:

  • 1815108523@qq.com ❌ Needs to sign CLA

Please:

  1. Sign the CLA at: https://www.openvela.com/#/community/cla
  2. After signing, comment /check-cla to recheck

📋 View detailed check results: Action Run #44233395011


💡 Tip: All contributors must sign the CLA before the PR can be merged.

@tyx-crazy

Copy link
Copy Markdown
Author

/check-cla

@github-actions

Copy link
Copy Markdown

✅ CLA Verification Complete

@tyx-crazy All contributors have signed the CLA!

  • 1815108523@qq.com ✅

📋 View detailed check results: Action Run #44234139497

Your pull request can now proceed with the review process! 🎉

@yanxingyu17 yanxingyu17 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The shared state mutex addresses the cross-topic wake/sleep race, but synchronously cancelling the worker from the sensor activate callback introduces a lock inversion with the sensor upper-half.

@tyx-crazy

Copy link
Copy Markdown
Author

R528_MIBOX-X1_xTS测试报告.docx
Added the full-board xTS self-test report (R528 MIBOX-X1, 34 mandatory items + 11 optional groups).

Note: the report covers the overall board-port validation — kernel, driver BSP,
performance, stability, display/touch/audio/ADC/PWM/buttons/USB, etc. —
not limited to the icm20948 concurrency fix in this PR. It is provided as
validation material for the board-support review; each item lists the commands
used, the serial log excerpt, and the issue analysis.

@tyx-crazy
tyx-crazy force-pushed the icm20948-state-lock branch 2 times, most recently from 56517e1 to ffe4d63 Compare November 22, 2026 15:21

@yanxingyu17 yanxingyu17 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The lock inversion from the previous revision is addressed. Please also update the PR description: it still describes the removed work_cancel_sync() design. The Testing section should include the actual passing self-test log (or a linked log artifact), including the exact commands, 80-cycle count, 120/700/1500 ms close delays, and final zero-hang/zero-failure summary rather than only a prose result.

sensor_close() holds the sensor upper-half lock while it calls
activate(..., false). A worker that is already running may be inside
push_event(), waiting for that same lock, so waiting for the worker
from the activate callback can deadlock.

Keep the cancellation non-blocking: when work_cancel() reports that the
worker is still running, let that worker perform the final idle and
sleep transition itself, after its push path and under the shared state
lock. When the work is not running, do the transition in the activate
path.

Do the same transition when a worker starts, takes the lock and finds
the sensor already disabled, so a close that raced with the scheduler
does not leave the chip awake.

Apply the same pattern to the accel, gyro and mag workers.

@yanxingyu17 yanxingyu17 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The enable/disable state transitions are serialized without blocking on an in-flight worker from the sensor upper-half callback. The worker-side idle transition covers both the running and early-exit paths.

CI, checkpatch, CLA, and the 80-cycle board test pass.

@yanxingyu17
yanxingyu17 merged commit ffb36e1 into open-vela:dev-ai-contest-2026 Nov 25, 2026
12 checks passed
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.

2 participants