Skip to content
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

BLE causes system sleep before main #41839

Closed
r2r0 opened this issue Jan 15, 2022 · 12 comments · Fixed by #41935
Closed

BLE causes system sleep before main #41839

r2r0 opened this issue Jan 15, 2022 · 12 comments · Fixed by #41935
Assignees
Labels
area: Bluetooth area: Power Management bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug

Comments

@r2r0
Copy link
Member

r2r0 commented Jan 15, 2022

Describe the bug
When the BLE subsystem is enabled (CONFIG_BT=y) then system enters idle thread (sleep) before main function is invoked.

Tested on blinky sample with CONFIG_BT=y and with small code changes as described below.
main.c

...
volatile uint32_t idle_counter = 0;

void main(void)
{
        if (idle_counter > 0) {
                printk("idle_entry: %u", idle_counter);
        }
...

idle.c

...
	while (true) {
	    extern volatile uint32_t idle_counter;
	    idle_counter++;
...

Target (board) is nucleo_wb55rg.

To Reproduce
Steps to reproduce the behavior:

  1. Modify blinky sample and idle.c as described above.
  2. west build --board=nucleo_wb55rg
  3. west flash
  4. See output on console: idle_entry: 2

Expected behavior
System should not sleep before entering main or this behavior should be documented.

Impact
annoyance

Environment (please complete the following information):

  • OS: Linux
  • Toolchain Zephyr SDK 0.13.1
  • Commit SHA: 4446272
@r2r0 r2r0 added the bug The issue is a bug, or the PR is fixing a bug label Jan 15, 2022
@r2r0 r2r0 changed the title Enabling Bluetooth causes CPU idle before main BLE causes system sleep before main Jan 15, 2022
@erwango erwango self-assigned this Jan 17, 2022
@erwango erwango added the platform: STM32 ST Micro STM32 label Jan 17, 2022
@erwango
Copy link
Member

erwango commented Jan 17, 2022

It seems that this problem is not STM32 specific.
I had a try to prevent system to enter PM_STATE_SUSPEND_TO_IDLE during HCI driver init _bt_ipm_init, but this had no effect on the issue.

@jhedberg Would you mind having a look on BLE stack side?

@erwango erwango removed the platform: STM32 ST Micro STM32 label Jan 17, 2022
@erwango erwango assigned jhedberg and ceolin and unassigned erwango Jan 17, 2022
@jhedberg
Copy link
Member

@erwango there is no PM related code on the host side of the Bluetooth stack, so not sure what you’re requesting.

@erwango
Copy link
Member

erwango commented Jan 17, 2022

@erwango there is no PM related code on the host side of the Bluetooth stack, so not sure what you’re requesting.

Sure, but maybe the point is that there are sleep intervals during BLE stack initialization that let the PM subsystem going into PM state while it should prevent it (using pm_constraint_). This could also be a larger system issue, applicable to all stacks and in that case, this should be fixed directly by PM subsystem. I hope @ceolin may give directions.

@ceolin
Copy link
Member

ceolin commented Jan 18, 2022

From the pm subsystem there is nothing preventing it from put the system to sleep. We can ensure that the system does not go to sleep before reach the main, this seems very reasonable. Though, there is a thumb rule where anyone doing an operation that the system shouldn't sleep has to set constraints.

@erwango
Copy link
Member

erwango commented Jan 19, 2022

Though, there is a thumb rule where anyone doing an operation that the system shouldn't sleep has to set constraints.

This seems logical but:

  • would we be able to enforce that ?
  • how can this be technically applied? (how a driver or a subsystem would know that main was entered yet or not. In some cases (init functions), it is simple, but in some subroutines used widely, response is not that easy.

@erwango
Copy link
Member

erwango commented Jan 19, 2022

@r2r0 I just had a debug session in this use case here are my observations:

  • No k_sleep is done anywhere before main is reached
  • When going step by step into STM32 IPM HCI driver, the issue disappear > Root cause is around this

I think what we're seeing is normal here, and not a bug.
According to @andyross:

In a technical sense yes: on a SMP system the extra CPUs will likely reach idle (each has its own idle thread obviously) before system initialization is complete and the application main() function is entered. But that's not your platform.

This is actually our case here. Well, this is not a SMP but we do have a second core (M0), which is initialized during main core boot process (as part of ipm hci driver init). During M0 initialization, main core is waiting (a semaphore to released by a message coming from M0) and then idle thread is entered. This seems actually normal to me and I don't see a specific bug to fix here.

What is strange is that I had a try to prevent to block idle entry (using pm_constraint_set around whole driver init), this had no effect. But this could be expected. @ceolin could you help clarifying this ?

@erwango erwango assigned erwango and unassigned jhedberg Jan 19, 2022
@ceolin
Copy link
Member

ceolin commented Jan 19, 2022

Hi @erwango, constraints don't prevent the system to goes idle, they prevent the system of suspending, aka going to "deep" sleep.

@erwango
Copy link
Member

erwango commented Jan 19, 2022

Hi @erwango, constraints don't prevent the system to goes idle, they prevent the system of suspending, aka going to "deep" sleep.

Ok, so I think there is nothing that can be done and I'd say there is nothing to fix here.
@r2r0 is it fine with you ?

@erwango erwango reopened this Jan 19, 2022
@erwango
Copy link
Member

erwango commented Jan 19, 2022

Reopening as #41935 doesn't solve issue reported.
Though, I think that there is no issue to be solved.
Waiting @r2r0 to confirm.

@ceolin
Copy link
Member

ceolin commented Jan 19, 2022

yep. I had removed the linkage between the pr and the issue, also removed the "fixes" from the commit message, but somehow the issue was automatically closed

@r2r0
Copy link
Member Author

r2r0 commented Jan 20, 2022

Ok, it is clear now to me that system behaior is correct. Thanks a lot for fast proceeding.
I wonder if it should be somehow documented - mainly in context of PM notifiers.
I.e. what is the lowest system initialization level/priority when PM notifiers can be called.
User hooks usually will need some context initialization and it will help users to know when it should be done.

@erwango erwango removed their assignment Jan 20, 2022
@carlescufi
Copy link
Member

I wonder if it should be somehow documented - mainly in context of PM notifiers.

@ceolin @r2r0 please open an Enhancement issue for documenting this behaviour if you want to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Bluetooth area: Power Management bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants