Skip to content

Clean up MRAM no-latency unit test after race mitigation redesign#23

Draft
cvinayak with Copilot wants to merge 19 commits into
github_nrf54h20_cpurad_bt_ll_sw_split_supportfrom
copilot/add-unit-test-mram-no-latency
Draft

Clean up MRAM no-latency unit test after race mitigation redesign#23
cvinayak with Copilot wants to merge 19 commits into
github_nrf54h20_cpurad_bt_ll_sw_split_supportfrom
copilot/add-unit-test-mram-no-latency

Conversation

Copilot AI commented Feb 8, 2026

Copy link
Copy Markdown

The unit test was updated to validate a redesigned MRAM no-latency management that handles multiple radio_reset()/radio_stop() calls before async callback completion. This PR cleans up the implementation.

Changes

  • Remove dead code: Deleted #if 0 block containing superseded callback implementation
  • Add missing include: <zephyr/sys/atomic.h> for atomic operations (atomic_inc, atomic_dec, atomic_get)
  • Document race mitigation design: Comprehensive overview of the asynchronous state management strategy

Design Summary

The implementation prevents race conditions through:

  1. Atomic reference counting (mram_refcnt): Only first reset requests MRAM no-latency, only last stop releases
  2. Request/release tracking (start_req/stop_req): Volatile counters accumulate operations between callbacks
  3. Callback batching: Processes accumulated requests/releases atomically, handling scenarios like:
    • reset → stop(pending) → callback → release
    • reset → stop → reset → callback → state_retained
// Callback counts operations since last acknowledgment
uint8_t req = mram_no_latency_start_req - mram_no_latency_start_ack;
uint8_t rel = mram_no_latency_stop_req - mram_no_latency_stop_ack;

// Executes cancel/release if releases exceed requests
if (rel > req) {
    mram_no_latency_cancel_or_release();
}

This mirrors the actual hal_radio_reset()/hal_radio_stop() implementation for nRF54H20 cpurad Bluetooth Controller support.

Original prompt

Context

PR zephyrproject-rtos#95417 adds nRF54H20 cpurad support for the Bluetooth Controller (bt_ll_sw_split). The nRF54H20's hal_radio_reset() and hal_radio_stop() in radio_nrf54hx.h manage hardware resources (currently nrf_lrcconf clock). MRAM no-latency management (mram_no_latency_request() / mram_no_latency_cancel_or_release()) needs to be integrated and its reference-counting behavior validated.

Task

Add a unit test file at tests/bluetooth/controller/radio_nrf54hx_mram/src/main.c (with appropriate testcase.yaml and CMakeLists.txt and prj.conf) that tests the MRAM no-latency state management with stubs for mram_no_latency_request() and mram_no_latency_cancel_or_release().

The test should exercise the state by calling radio_reset() (which calls mram_no_latency_request()) and radio_stop() (which calls mram_no_latency_cancel_or_release()), and then invoking mram_no_latency_callback() to resolve the async state.

Test invariants to verify:

  1. n calls to mram_no_latency_request() and n calls to mram_no_latency_cancel_or_release(), then when mram_no_latency_callback() is called → mram_no_latency is OFF.

  2. (n+1) calls to mram_no_latency_request() and n calls to mram_no_latency_cancel_or_release(), then when mram_no_latency_callback() is called → mram_no_latency is ON.

Implementation details:

  • Stubs: Create stub implementations for mram_no_latency_request() and mram_no_latency_cancel_or_release() that track a pending_requests counter (incremented on request, decremented on release).

  • mram_no_latency_callback(): A test function that resolves state: if pending_requests > 0 → mram_no_latency is ON; if pending_requests == 0 → mram_no_latency is OFF.

  • radio_reset() / radio_stop(): Simplified functions that call the stubs, mirroring what hal_radio_reset() / hal_radio_stop() in radio_nrf54hx.h would do.

Test cases to include:

  • test_n_requests_n_releases_then_callback_off: For n = 1, 2, 3, 5, 10 — verify mram_no_latency is OFF after equal requests and releases + callback.
  • test_n_plus_1_requests_n_releases_then_callback_on: For n = 0, 1, 2, 3, 5, 10 — verify mram_no_latency is ON after (n+1) requests and n releases + callback.
  • test_interleaved_reset_stop: Interleave radio_reset/radio_stop calls, verify state after each callback.
  • test_single_radio_event_cycle: Single reset → stop → callback → OFF.
  • test_single_reset_no_stop: Single reset → callback → ON.
  • test_zero_requests_callback_off: No operations → callback → OFF.
  • test_multiple_callbacks: Multiple callbacks between operations maintain correct state.

Test file structure:

tests/bluetooth/controller/radio_nrf54hx_mram/
├── CMakeLists.txt
├── prj.conf
├── testcase.yaml
└── src/
    └── main.c

Reference files:

  • radio_nrf54hx.h (new in PR): subsys/bluetooth/controller/ll_sw/nordic/hal/nrf5/radio/radio_nrf54hx.h — contains hal_radio_reset() and hal_radio_stop() with nrf_lrcconf clock management at lines 398-412.
  • mram_latency.h: soc/nordic/common/mram_latency.h — declares mram_no_latency_request(), mram_no_latency_cancel_or_release() using struct onoff_client.
  • mram_latency.c: soc/nordic/common/mram_latency.c — real implementation using onoff_manager.
  • Existing MRAM latency tests: tests/boards/nrf/mram_latency/src/main.c — for reference on test patterns.

Use the Zephyr ztest framework. The test should be a native/unit test that can run without hardware (using native_posix or similar).

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

cvinayak and others added 12 commits February 6, 2026 13:15
nrf54h20 cpurad bt_ll_sw_split support.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
FICR compile error fixed
ECB compile error fixed
SWI compile error fixed
draft modification for radio
Update radio.c due to miss some device check

Signed-off-by: Jui-Chou Chung <jui-chou.chung@nordicsemi.no>
Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
Added initial implementation for nrf54hx LF clock control.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
Added initial implementation for nrf54hx GRTC use for Radio.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
Added initial implementation for nrf54hx HF clock control.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
Added initial implementation for nrf54hx MRAM no latency
control.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
Added implementation to mitigate nrf54hx MRAM no latency
control race.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
nRF54H20 cpuapp peripheral_hr quirks.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
nRF54H20 cpuapp hci_usb quirks.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
nRF54H20 cpurad hci_ipc quirks for ISO support

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
nRF54H20 cpurad hci_usb quirks for ISO support

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
Add comprehensive unit test at tests/bluetooth/controller/radio_nrf54hx_mram/
that validates the reference-counting behavior of MRAM no-latency requests
and releases through radio_reset() and radio_stop() operations.

Test cases include:
- n requests and n releases result in OFF state
- (n+1) requests and n releases result in ON state
- Interleaved reset/stop operations
- Single radio event cycle
- Single reset without stop
- Zero requests callback
- Multiple callbacks maintaining state

The test uses stub implementations for mram_no_latency_request() and
mram_no_latency_cancel_or_release() to track pending_requests counter,
and mram_no_latency_callback() to resolve the async state.

Co-authored-by: cvinayak <6350656+cvinayak@users.noreply.github.com>
Copilot AI changed the title [WIP] Add unit test for MRAM no-latency state management tests: Add unit test for nRF54H20 MRAM no-latency state management Feb 8, 2026
…ol race

Added implementation to mitigate nrf54hx MRAM no latency
control race.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
@cvinayak cvinayak force-pushed the copilot/add-unit-test-mram-no-latency branch from 3c1eab9 to 0ff3170 Compare February 9, 2026 08:41
cvinayak added a commit that referenced this pull request Feb 9, 2026
@cvinayak cvinayak force-pushed the github_nrf54h20_cpurad_bt_ll_sw_split_support branch from 520deb1 to 33ce3e3 Compare February 9, 2026 09:17
…ol race

Added implementation to mitigate nrf54hx MRAM no latency
control race.

Signed-off-by: Vinayak Kariappa Chettimada <vich@nordicsemi.no>
Copilot AI and others added 2 commits February 11, 2026 05:21
…header

- Remove #if 0 dead code block from mram_no_latency_callback()
- Add missing <zephyr/sys/atomic.h> include for atomic operations
- Keep the improved implementation that handles multiple requests/cancels

Co-authored-by: cvinayak <6350656+cvinayak@users.noreply.github.com>
Document the sophisticated race condition mitigation design:
- Atomic reference counting strategy (mram_refcnt)
- Request/release tracking mechanism (start_req/stop_req)
- Race condition scenarios and handling
- Callback behavior and state resolution logic

This explains how multiple radio_reset/radio_stop calls are handled
before async callback generates, preventing race conditions.

Co-authored-by: cvinayak <6350656+cvinayak@users.noreply.github.com>
Copilot AI changed the title tests: Add unit test for nRF54H20 MRAM no-latency state management Clean up MRAM no-latency unit test after race mitigation redesign Feb 11, 2026
@cvinayak cvinayak force-pushed the github_nrf54h20_cpurad_bt_ll_sw_split_support branch 2 times, most recently from 0e0746d to 15252bd Compare February 13, 2026 12:28
@cvinayak cvinayak force-pushed the github_nrf54h20_cpurad_bt_ll_sw_split_support branch 3 times, most recently from 2582a3b to 29f739a Compare March 7, 2026 04:58
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.

3 participants