-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mpsl: Add integration layer for MPSL external clock driver #19498
base: main
Are you sure you want to change the base?
mpsl: Add integration layer for MPSL external clock driver #19498
Conversation
The following west manifest projects have changed revision in this Pull Request:
✅ All manifest checks OK Note: This message is automatically posted and updated by the Manifest GitHub Action. |
CI InformationTo view the history of this post, clich the 'edited' button above Inputs:Sources:sdk-nrf: PR head: 59a8322ea6fe6a93299b80bb66854074e8738e16 more detailssdk-nrf:
Github labels
List of changed files detected by CI (11)
Outputs:ToolchainVersion: 342151af73 Test Spec & Results: ✅ Success; ❌ Failure; 🟠 Queued; 🟡 Progress; ◻️ Skipped;
|
9ae3f8c
to
a5d7313
Compare
You can find the documentation preview for this PR at this link. It will be updated about 10 minutes after the documentation build succeeds. Note: This comment is automatically posted by the Documentation Publish GitHub Action. |
a5d7313
to
f712fd7
Compare
/* In case of use of nrf clock control MPSL is stared on POST_KERNEL level, hence NRF_RTC0 is | ||
* started after system clock which is NRF_RTC1. In case MPLS is configured as clock control driver | ||
* then NRF_RTC0 is started on PRE_KERNEL_1 init level. | ||
*/ | ||
#if defined(CONFIG_MPSL_USE_ZEPHYR_CLOCK_CONTROL) | ||
#define REF_RTC NRF_RTC1 | ||
#define CMP_RTC NRF_RTC0 | ||
#else | ||
#define REF_RTC NRF_RTC0 | ||
#define CMP_RTC NRF_RTC1 | ||
#endif /* CONFIG_MPSL_USE_ZEPHYR_CLOCK_CONTROL */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code change is unnecessary if set_sys_clock_start_to_bt_clk_start_us()
is updated to set sys_clock_start_to_bt_clk_start_us
as a signed value. I suggest to change that instead to avoid extra complexity in this module.
That change can be separated out to a separate PR and be merged independently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted a PR: #20166
include/mpsl/mpsl_clock_ctrl.h
Outdated
* @retval -NRF_EPERM Clock control is already initialized. | ||
* @retval -NRF_EINVAL Invalid parameters supplied. | ||
*/ | ||
int32_t mpsl_clock_ctrl_init(void); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The APIs mpsl_clock_ctrl_init()
and mpsl_clock_ctrl_uninit()
are not supposed to be called from application layer, only from MPSL-internal components. We should therefore move this header file from include/mpsl
to subsys/mpsl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then mpsl_pm.h
should also be moved to mpsl/pm
, it is also used internally by the mpsl subsystem.
File moved.
subsys/bluetooth/controller/Kconfig
Outdated
This option configures LL_SOFTDEVICE system init priority level. The priority must be lower | ||
than the CONFIG_MPLS_SYS_INIT_PRIO_LEVEL due to dependency on the MPSL library. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This option configures LL_SOFTDEVICE system init priority level. The priority must be lower | |
than the CONFIG_MPLS_SYS_INIT_PRIO_LEVEL due to dependency on the MPSL library. | |
This option configures the LL_SOFTDEVICE initialization priority level. The priority must be lower | |
than the CONFIG_MPSL_SYS_INIT_PRIO_LEVEL due to dependency on the MPSL library. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed
subsys/mpsl/init/Kconfig
Outdated
config MPSL_SYS_INIT_LEVEL_POST_KERNEL | ||
bool | ||
default y if MPSL_USE_ZEPHYR_CLOCK_CONTROL | ||
help | ||
Change MPSL system init level to POST_KERNEL. Default init level is PRE_KERNEL_1. | ||
The POST_KERNEL init level must be used in case of use of external clock driver. | ||
That gives integration layer possibilty to use kernel object for clocks handling. | ||
The option shall not be set in case of use of MPLS internal clock driver. | ||
Then the MPSL system init level must be set to PRE_KERNEL_1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need MPSL_SYS_INIT_LEVEL_POST_KERNEL` or should we always initialize mpsl post-kernel when MPSL_USE_ZEPHYR_CLOCK_CONTROL is set? Otherwise we need to add tests for both configurations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, use of MPSL_USE_ZEPHYR_CLOCK_CONTROL
should be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed and used MPSL_USE_EXTERNAL_CLOCK_CONTROL
instead
subsys/mpsl/init/Kconfig
Outdated
The option shall not be set in case of use of MPLS internal clock driver. | ||
Then the MPSL system init level must be set to PRE_KERNEL_1. | ||
|
||
config MPSL_SYS_INIT_PRIO_LEVEL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would consider naming this MPSL_INIT_PRIORITY
. Other drivers tend to use the convention <drvier>_INIT_PRIO_LEVEL
or <driver>_INIT_PRIORITY
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to: MPSL_INIT_LEVEL
subsys/mpsl/init/mpsl_init.c
Outdated
@@ -390,7 +395,14 @@ static int32_t mpsl_lib_init_internal(void) | |||
* 500ppm or better regardless of the value passed in clock_cfg. | |||
*/ | |||
memset(&clock_cfg, 0, sizeof(clock_cfg)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would consider passing in NULL when CONFIG_MPSL_USE_ZEPHYR_CLOCK_CONTROL is used. That makes it more clear the configuration is not used and also saves some code size
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change done, the mpsl_init
is called with NULL
if CONFIG_MPSL_USE_EXTERNAL_CLOCK_CONTROL
is set.
subsys/mpsl/init/mpsl_init.c
Outdated
#endif /* CONFIG_MPLS_SYS_INIT_LEVEL_POST_KERNEL */ | ||
|
||
#if defined(CONFIG_MPSL_USE_ZEPHYR_CLOCK_CONTROL) && defined(CONFIG_SOC_SERIES_NRF54HX) | ||
BUILD_ASSERT(CONFIG_MPLS_SYS_INIT_PRIO_LEVEL > CONFIG_NRFS_BACKEND_IPC_SERVICE_INIT_PRIO, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some typos here: MPLS -> MPSL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also fix the commit message and title of this PR :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
f712fd7
to
06d2cc1
Compare
10bb5d8
to
379cb4d
Compare
#if !defined(CONFIG_MPSL_USE_EXTERNAL_CLOCK_CONTROL) | ||
__ASSERT_NO_MSG(ticks_difference >= 0); | ||
#endif /* CONFIG_MPSL_USE_EXTERNAL_CLOCK_CONTROL */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assertion is no longer needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
e0be461
to
9d9adf0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, definately like the introduction of MPSL_USE_EXTERNAL_CLOCK_CONTROL
9d9adf0
to
3e8750d
Compare
/* In case of use of nrf clock control MPSL is stared on POST_KERNEL level, hence NRF_RTC0 is | ||
* started after system clock which is NRF_RTC1. In case MPLS is configured as clock control driver | ||
* then NRF_RTC0 is started on PRE_KERNEL_1 init level. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/* In case of use of nrf clock control MPSL is stared on POST_KERNEL level, hence NRF_RTC0 is | |
* started after system clock which is NRF_RTC1. In case MPLS is configured as clock control driver | |
* then NRF_RTC0 is started on PRE_KERNEL_1 init level. | |
/* In case of use of nrf clock control MPSL is started on POST_KERNEL level, hence NRF_RTC0 is | |
* started after system clock which is NRF_RTC1. In case MPSL is configured as clock control driver | |
* then NRF_RTC0 is started on PRE_KERNEL_1 init level. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for spotting these
5d077d7
to
2bb2cd2
Compare
6280e4b
to
8e499b7
Compare
8e499b7
to
2d80bbe
Compare
d122c36
to
6532f9a
Compare
@@ -18,4 +18,8 @@ if (CONFIG_MPSL_USE_ZEPHYR_PM) | |||
add_subdirectory(pm) | |||
endif() | |||
|
|||
if (CONFIG_MPSL_USE_EXTERNAL_CLOCK_CONTROL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (CONFIG_MPSL_USE_EXTERNAL_CLOCK_CONTROL) | |
if(CONFIG_MPSL_USE_EXTERNAL_CLOCK_CONTROL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nordicjm please see:
sdk-nrf/subsys/mpsl/CMakeLists.txt
Line 17 in 7815f14
if (CONFIG_MPSL_USE_ZEPHYR_PM) |
5dc3a46
to
734971a
Compare
/** @brief Uninitialize MPSL integration with NRF clock control | ||
* | ||
* @retval 0 MPSL clock was uninitialized successfully. | ||
* @retval -NRF_EPERM MPSL was not uninitialized before the call. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @retval -NRF_EPERM MPSL was not uninitialized before the call. | |
* @retval -NRF_EPERM MPSL was not initialized before the call. |
@@ -0,0 +1,33 @@ | |||
/* | |||
* Copyright (c) 2024 Nordic Semiconductor ASA |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking: Code is added in 2025. There are some other occurrences in this PR as well
* Copyright (c) 2024 Nordic Semiconductor ASA | |
* Copyright (c) 2025 Nordic Semiconductor ASA |
#if defined(CONFIG_CLOCK_CONTROL_NRF_ACCURACY) | ||
m_nrf_lfclk_ctrl_data.accuracy_ppm = CONFIG_CLOCK_CONTROL_NRF_ACCURACY; | ||
#else | ||
m_nrf_lfclk_ctrl_data.accuracy_ppm = MPSL_LFCLK_ACCURACY_PPM; | ||
#endif /* CONFIG_CLOCK_CONTROL_NRF_ACCURACY */ | ||
m_nrf_lfclk_ctrl_data.skip_wait_lfclk_started = IS_ENABLED(CONFIG_SYSTEM_CLOCK_NO_WAIT); | ||
m_nrf_hfclk_ctrl_data.startup_time_us = CONFIG_MPSL_HFCLK_LATENCY; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we setting some m_nrf_lfclk_ctrl_data
and m_nrf_hfclk_ctrl_data
at run-time and others at compile time?
I would expect it is more space efficient to set everything at compile time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change that. As of now all can be initialized during build.
The accuracy_ppm for 54h may be initialized in future in runtime.
if (m_hfclk_refcnt < 1) { | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it intentional that we here allow a mismatch between the number of calls to m_hfclk_release()
and m_hfclk_request()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only thing here is that there are allowed more releases than requests. That is not a problem from functional stand point of view I think. Do you see any risk?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering why we have added it here in the first place. It shouldn't be needed. It could indicate we have a bug somewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is added for following purposes:
- We do not release the clock if there is more than one request.
- To avoid calling
z_nrf_clock_bt_ctlr_hf_release
more times than needed. - Gives some insight how many times did we request clock e.g. if there is more requests in runtime than releases you can check if immediately why HFXO is left requested.
|
||
static bool m_hfclk_is_running(void) | ||
{ | ||
if (m_hfclk_refcnt < 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason for not using atomic_get()
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I didn't find the ARM Cotrex-M specific implementation in sdk-zphyr. The one available does plain load: https://github.com/nrfconnect/sdk-zephyr/blob/693769a5c7354efce42af62b6c7f68f77c6678b7/kernel/atomic_c.c#L220-L223.
Anyway I'll change it to make it complete and safe in case someone adds custom implementation.
/* The function is executed in POST_KERNEL hence sleep is allowed */ | ||
k_msleep(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we sleep when the operation didn't complete instead of retrying immediately?
Is there a reason for choosing 1 ms?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed offline. Since the mpsl_init()
happens in POST_KERNEL
it is possible to use a callback notification from clock control. The decision was to switch from wait in a loop to use callback and semaphore.
static void m_hfclk_release(void) | ||
{ | ||
/* The z_nrf_clock_bt_ctlr_hf_request doesn't count references to HFCLK, | ||
* it is caller responsibility to do not release the clock if there is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* it is caller responsibility to do not release the clock if there is | |
* it is caller responsibility to not release the clock if there is |
|
||
static bool m_hfclk_is_running(void) | ||
{ | ||
/* As of now assume the HFCLK is runnig after the request was put */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to add a comment here describing that this is not always true.
Maybe something like
/* As of now assume the HFCLK is runnig after the request was put */ | |
/* As of now assume the HFCLK is running after the request was put. | |
* This puts the responsibility to the user to check if the time since last | |
* request is larger than the HFXO rampup time. | |
*/ |
Another approach would be to disallow calling this API (__ASSERT_NO_MSG(false)
) to make it clear that the result of this function call cannot be trusted. The user would anyways have check the time since the last request.
static bool m_lfclk_calibration_is_enabled(void) | ||
{ | ||
if (IS_ENABLED(CONFIG_CLOCK_CONTROL_NRF_DRIVER_CALIBRATION)) { | ||
return z_nrf_clock_calibration_is_in_progress(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calibration "is enabled" is something else than "is in progress", So I would assume the correct thing to do is to change this to
return z_nrf_clock_calibration_is_in_progress(); | |
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, there is a difference in naming convention between nrf clock control
and former mpsl clock control
. What is enabled
for mpsl clock control
refers to: https://github.com/nrfconnect/sdk-zephyr/blob/693769a5c7354efce42af62b6c7f68f77c6678b7/drivers/clock_control/nrf_clock_calibration.c#L23-L24.
The nrf clock control
uses this global to store state: https://github.com/nrfconnect/sdk-zephyr/blob/693769a5c7354efce42af62b6c7f68f77c6678b7/drivers/clock_control/nrf_clock_calibration.c#L34 and
the function used returns its value: https://github.com/nrfconnect/sdk-zephyr/blob/693769a5c7354efce42af62b6c7f68f77c6678b7/drivers/clock_control/nrf_clock_calibration.c#L298-L301
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused here. cal_process_in_progress
in nrf_clock_calibration.c seems to be cleared in start_cycle
. That is, is looks like the function only returns true when calibration is actually running.
I would expect m_lfclk_calibration_is_enabled
to return true
statically when calibration is enabled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed offline, the correct approach is to return true;
if CONFIG_CLOCK_CONTROL_NRF_DRIVER_CALIBRATION
is enabled.
|
||
static void m_lfclk_calibration_start(void) | ||
{ | ||
/* This function should not be called from MPSL if CONFIG_CLOCK_CONTROL_NRF2 is set */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can clarify a bit here and for m_lfclk_calibration_is_enabled()
/* This function should not be called from MPSL if CONFIG_CLOCK_CONTROL_NRF2 is set */ | |
/* This function is not supported when CONFIG_CLOCK_CONTROL_NRF2 is set. | |
* Currently MPSL will never use this API for this configuration. | |
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to
/* This function is not supported when CONFIG_CLOCK_CONTROL_NRF2 is set.
* As of now MPSL does not use this API in this configuration.
*/
734971a
to
b149f68
Compare
In the past MPSL used its own implementation of clock control. The approach changes due to lack of acces to clocks from Radio core in nRF54h20 SoC. Thaking this opportunity we provide experimental support for external clock control to nRF52 and nRF53 SoC Series. The new module is an integration layer between new MPSL API that allows for registration of external clock driver and nRF clock control driver. The implementation in this commit provides integration with MPSL for nrf clock control for nRF52 and nRF53 SoC series. Note: The support for nRF52 and nRF53 SoC series is experimental and is not enabled by default. Use of nrf clock control with MPSL allows to initialize the library in POST_KERNEL stage. Thanks to that we can use kernel synchronization primitives and non blocking waits. The change in MPSL init level and priority affects BT_LL_SOFTDEVIDE- _HCI_SYS_INIT_PRIORITY. The HCI driver for SDC depends on MPSL so it must be initialized after the MPSL. Signed-off-by: Piotr Pryga <piotr.pryga@nordicsemi.no>
Add integration layer for MPSL external clock driver and nrf2 clock control for nRF5420. This is mandatory for the nRF54H20. Note: The nrf2 clock control requires the MPSL initialization to be done later. The nrf2 clock control depends on nRFS that is initialized at POST_KENREL init level. Its init priority is CONFIG_NRFS_BACKEND_IPC_SERVICE_INIT_PRIO that is lower than former MPSL init level. To make sure the mpsl lfclk request and response is handled corrently we must make the MPSL is initialized after it. Signed-off-by: Piotr Pryga <piotr.pryga@nordicsemi.no>
The nrf clock control driver doesn't enable HFXO when LFSYNTH is selected as a source of LFCLK. That causes the accuracy of LFCLK to be not within the expected by BT Core Specification range up to 500 ppm. To fix the problem the mpsl clock control integration layer has to request the hfxo in case the lfclk is requested with LFSYNTH as a source. Use of z_nrf_clock_bt_ctlr_hf_release makes the call to be faster. That unfortunately requires reference counting do avoid release of HFXO later in runtime by MPSL. For that reason the m_hfclk_request and m_hfclk_release are used. Signed-off-by: Piotr Pryga <piotr.pryga@nordicsemi.no>
…issue Add temporary workaround that allows the LFCLK to timeout on nRF54H20 (if CLOCK_CONTROL_NRF2 is enabled). The potential timeout is not an issue for now because the integration layer request the lowest accuracy of LFCLK and such or better LFCLK should be runing from boot of the radio core. Signed-off-by: Piotr Pryga <piotr.pryga@nordicsemi.no>
b149f68
to
59a8322
Compare
@rugeGerritsen @nordicjm @knutel-nordic your comments were addressed. It would be nice if you re-review the PR. |
The MPLS is enhanced with possibility to use it external clock driver.
That feature requires implementation of integration layer between nrf-clock-control for nRF52, nRF53 SoC series and nrf2-clock-control for nRF54H SoC series (the nRF54L SoC series is not available yet).
The integration with nrf-clock-control is experimental.
The integration with nrf2-clock-control is supported and enabled by default for nRF54H SoC series.
nrf2-clock-control required some changes to MSPL and SDC HCI driver initialization. The nrf2-clock-control depends on nRFS
so MPLS and SDC HCI driver that depend on MPLS need to be initialized in
POST_KERNEL
system initialization level and with priority lower than nRFS initialization priority.Besides that there were a change in radio-notification-cb host extension that was dependent on RTC0 and RTC1 start order. With changed initialization level of MPSL the RTC1 (system timer) is started before the RTC0 used by MPSL.