Skip to content

NRF5x: rationalise sleep+critical #9358

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

Closed
wants to merge 1 commit into from
Closed

Conversation

kjbracey
Copy link
Contributor

Description

This follows up from previous closed PRs #8366 and #9211, in an attempt to fix the issue originally reported as #5647.

As discussed in those PRs, the sleep behaviour of the NRF5x family is apparently unsafe, at least for the cases where the SoftDevice is disabled.

This commit fixes it up by:

  • Collapsing down to 1 implementation, from 3.
  • Using standard PRIMASK disable when SoftDevice is disabled.
  • Using standard WFI when SoftDevice is disabled.

This commit shouldn't change the functionality for when it is enabled - it retains the "SEVONPEND" and "FPU pending" fudges.

If SoftDevice is enabled, we continue to hope that its sd_app_evt_wait() call does the Right Thing(TM). However, this seems unlikely, as documentation and the "nosd" stubs visible suggests they just do a
"WFE", when we are looking for a "WFI" equivalent. Or it's possible their real implementation does the unsafe and racy "SEV; WFE; WFE;" sequence.

Comments added to clarify what we are hoping for from SoftDevice.

Pull request type

[X] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

Reviewers

@TacoGrandeTX, @deepikabhavnani, @pan-, @marcuschangarm, @scartmell-arm

@kjbracey
Copy link
Contributor Author

This effectively completely replaces the sleep and critical code, and is kind of a "best-of" the various implementations around, with some of my own special sauce.

First draft, untested, but I want feedback.

I'm not clear how the SDK and glue stuff in FEATURE_BLE relates to the rest of the tree - I can't see they co-exist, so I've just deleted the "hal_patch" directory from there for now, and I'm assuming we're using either SDK11 or SDK14 from the targets directory.

One small "adaptation" header put in to cover up API differences between the SDKs.

I think we might have a problem in that the SoftDevice event wait API is unclear on whether it's a "WFE" or "WFI", and implementation may be too. I've added notes on how it might work for both purposes. But this PR doesn't change that mode - only the SoftDevice-disabled mode.

@ciarmcom
Copy link
Member

@kjbracey-arm, thank you for your changes.
@scartmell-arm @marcuschangarm @deepikabhavnani @pan- @ARMmbed/mbed-os-pan @ARMmbed/mbed-os-maintainers please review.

@marcuschangarm
Copy link
Contributor

// Will likely recurse, as assert failure will try to take critical region
MBED_ASSERT(sd_result == NRF_SUCCESS);
#else
(void) sd_nvic_critical_region_enter(&nested);
Copy link
Member

Choose a reason for hiding this comment

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

This function is not defined when softdevice is included.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When it is not included, you mean?

Copy link
Contributor Author

@kjbracey kjbracey Jan 14, 2019

Choose a reason for hiding this comment

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

Can it ever not be included? Is there not SOFTDEVICE_NONE, at minimum, which gives us a stubbed implementation of that? (Not that it should ever be used, as mbed_softdevice_is_enabled will be a constant false in that case).

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I made more investigation and the definition is not included on NRF52840_DK. When the soft device is on, the definition is in nrf_nvic.h

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So could we turn on SDK14_2, NORDIC_SOFTDEVICE and SOFTDEVICE_NONE for that? Would that make sense?

uint32_t sd_result = sd_nvic_critical_region_exit(!critical_interrupts_enabled);
MBED_ASSERT(sd_result == NRF_SUCCESS);
#else
(void) sd_nvic_critical_region_exit(!critical_interrupts_enabled);
Copy link
Member

Choose a reason for hiding this comment

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

This function is not defined when softdevice is included.

@cmonr
Copy link
Contributor

cmonr commented Jan 14, 2019

Please rebase your PR.

A fix for the travis-ci/tools-py2.7 was brought in today (#9371) due to an update to the hypothesis module used in CI: https://github.com/HypothesisWorks/hypothesis/releases/tag/hypothesis-python-4.0.0

@0xc0170
Copy link
Contributor

0xc0170 commented Jan 28, 2019

Besides rebase to resolve travis failures (there was another tool breakage), needs reviews !

This follows up from previous closed PRs ARMmbed#8366 and ARMmbed#9211, in an attempt
to fix the issue originally reported as ARMmbed#5647.

As discussed in those PRs, the sleep behaviour of the NRF5x family is
apparently unsafe, at least for the cases where the SoftDevice is
disabled.

This commit fixes it up by:

* Collapsing down to 1 implementation, from 3.
* Using standard PRIMASK disable when SoftDevice is disabled.
* Using standard WFI when SoftDevice is disabled.

This commit shouldn't change the functionality for when it is enabled -
it retains the "SEVONPEND" and "FPU pending" fudges.

If SoftDevice is enabled, we continue to hope that its sd_app_evt_wait()
call does the Right Thing(TM). However, this seems unlikely, as
documentation and the "nosd" stubs visible suggests they just do a
"WFE", when we are looking for a "WFI" equivalent. Or it's possible
their real implementation does the unsafe and racy "SEV; WFE; WFE;"
sequence.

Comments added to clarify what we are hoping for from SoftDevice.
@kjbracey
Copy link
Contributor Author

Will also need to coordinate with #9019 - that no doubt will affect this. Seems like it might be increasing the number of HW/SDK/how-to-drive-it permutations further :/

@cmonr cmonr mentioned this pull request Jan 30, 2019
@0xc0170
Copy link
Contributor

0xc0170 commented Feb 13, 2019

Will also need to coordinate with #9019 - that no doubt will affect this. Seems like it might be increasing the number of HW/SDK/how-to-drive-it permutations further :/

The update went in to the feature branch , anything to do there?

This still needs reviews

@TacoGrandeTX
Copy link
Contributor

@0xc0170 As this was still under review it was not brought into the nrf52-sdk15 feature branch. What level of testing has been performed on this?

@kjbracey
Copy link
Contributor Author

This is still at discussion stage - this is a proposal, and I was awaiting feedback and answers to some questions above. Particularly:

  • can we use "SOFTDEVICE_NONE" to get stubbed functions, rather than kill the entire SDK,
  • why does the BLE code have its own "hal patches"?

I will no doubt need to update this for SDK15, but not sure how to co-ordinate that if you're on a feature branch. Haven't looked at it yet, so I don't know how it changes anything.

There should be a follow-up discussion about how sd_app_evt_wait really works for when SoftDevice is on, as I'm not yet convinced from documentation that it's race-free the way we're using it, but for now this only changes the cases where we're not using it. (I hope).

@cmonr
Copy link
Contributor

cmonr commented Feb 20, 2019

@kjbracey-arm Any progress?

@TacoGrandeTX
Copy link
Contributor

TacoGrandeTX commented Feb 20, 2019

@kjbracey-arm

  • can we use "SOFTDEVICE_NONE" to get stubbed functions, rather than kill the entire SDK,

I'm not sure what functions you are referring to, but yes building for SOFTDEVICE_NONE doesn't kill the SDK - it prevents the SoftDevice and MBR from being built into the final image. Some helper utility functions are located here for SOFTDEVICE_NONE:
https://github.com/ARMmbed/mbed-os/tree/master/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_14_2/TARGET_SOFTDEVICE_NONE/nrf_soc_nosd

I just checked your branch and it doesn't yet build for SOFTDEVICE_NONE.

  • why does the BLE code have its own "hal patches"?

Not sure - perhaps @desmond-blue knows?

@desmond-blue
Copy link
Contributor

@TacoGrandeTX
I have no idea.
The hal_patch folder is only under target nRF51822, so my guess is that those two files are there because they include some SoftDevice functions and SoftDevice was bonded with BLE, and we just haven't updated them.

@cmonr
Copy link
Contributor

cmonr commented Mar 19, 2019

@kjbracey-arm @ARMmbed/mbed-os-pan Any thoughts on how to progress this PR?

@cmonr
Copy link
Contributor

cmonr commented Mar 27, 2019

@kjbracey-arm @ARMmbed/mbed-os-pan ^^^

@0xc0170 0xc0170 removed the request for review from a user April 15, 2019 13:48
@mbed-ci
Copy link

mbed-ci commented Apr 17, 2019

Test run: FAILED

Summary: 3 of 7 test jobs failed
Build number : 1
Build artifacts

Failed test jobs:

  • jenkins-ci/mbed-os-ci_build-IAR
  • jenkins-ci/mbed-os-ci_build-GCC_ARM
  • jenkins-ci/mbed-os-ci_build-ARM

@adbridge
Copy link
Contributor

@kjbracey-arm as this has seen no update for 29 days I am closing this. Please re-open once there are any new changes.

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.

10 participants