Skip to content

STM32: Reduce HAL_deepsleep stack usage #7241

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

Merged
merged 1 commit into from
Jun 20, 2018

Conversation

LMESTM
Copy link
Contributor

@LMESTM LMESTM commented Jun 18, 2018

Description

There are cases where a call hal_deepsleep would overflow the idle task
stack, especially in developper or debug profile.

In order to avoid this case, we split ForceClockOutofDeepSleep
into two separate functions the two structure RCC_ClkInitStruct and
RCC_OscInitStruct are not allocated at the same time.

Fixes #7235

Pull request type

[x] Fix
[ ] Refactor
[ ] New target
[ ] Feature
[ ] Breaking change

cmonr
cmonr previously approved these changes Jun 18, 2018
Copy link
Contributor

@cmonr cmonr left a comment

Choose a reason for hiding this comment

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

LGTM aside for some whitespace indetation nits, but I'll be waiting to start CI until someone else from ST approves.

@cmonr
Copy link
Contributor

cmonr commented Jun 18, 2018

Kinda surprised that those two structure allocations were overflowing the stack.

}
#endif // TARGET_STM32L4
}

static void ForceClockOutofDeepSleep(void)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like a comment here to see the reason why this was split up; with some hint to test in debug configuration if anything changes in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense - will do.

@janjongboom
Copy link
Contributor

janjongboom commented Jun 19, 2018

This does not fix my issue on mbed-os-example-wifi... Still runs out of stack space immediately (w/ tickless, cpu stats and memory stats enabled).

++ MbedOS Error Info ++
Error Status: 0x80020125 Code: 293 Module: 2
Error Message: CMSIS-RTOS error: Stack overflow
Location: 0x8007e11
Error Value: 0x1
Current Thread: Id: 0x20001c10 Entry: 0x8009a5d StackSize: 0x200 StackMem: 0x20001ca0 SP: 0x2004fed8 
-- MbedOS Error Info --

@LMESTM
Copy link
Contributor Author

LMESTM commented Jun 19, 2018

Just tried on my board (L476 IOT) with all below stats and I could not reproduce the issue
"MBED_HEAP_STATS_ENABLED", "MBED_STACK_STATS_ENABLED", "MBED_CPU_STATS_ENABLED", "MBED_THREAD_STATS_ENABLED"
Will move to DISCO_F413.
Can you just confirm that overflow happens from the IDLE task , looking for Entry: 0x8009a5d in mbed.map ?

To progress in parallel, you can increase MBED_CONF_APP_IDLE_THREAD_STACK_SIZE in your application

@LMESTM
Copy link
Contributor Author

LMESTM commented Jun 19, 2018

I tested my basic test program fine on DISCO-F413 , with all the enabled STATS flags and TICKLESS and I don't see the stack overflow.

Please confirm if the overflow happens from the idle task or not.
In your first report, we could see Entry: 0x8003e69
Now we see : Entry: 0x8009a5d

Maybe this PR solves a first issue, but there may be another one behind.

More generally, in case we activate lots of debug and use a non optimized compilation scheme, we should arrange for larger, non-optimized stack sizes ...


/* Get the Oscillators configuration according to the internal RCC registers */
HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Copy link
Contributor

Choose a reason for hiding this comment

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

is this now misaligned?

@@ -97,19 +77,9 @@ static void ForceClockOutofDeepSleep(void)
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency) != HAL_OK) {
error("clock issue\r\n");
error("clock issue\r\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

8 spaces , not 4 here? Same issue is in the code below as well (copy-paste probably?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Recently changed my PC and have to reconfigure tools correctly - sorry about that ;-)

@janjongboom
Copy link
Contributor

I tested my basic test program fine on DISCO-F413 , with all the enabled STATS flags and TICKLESS and I don't see the stack overflow.

Did you test this on https://github.com/armmbed/mbed-os-example-wifi ? It immediately overflows (on idle thread) for me w/ debug profile, even on your branch.

@LMESTM
Copy link
Contributor Author

LMESTM commented Jun 19, 2018

@janjongboom @0xc0170
Indeed reproduced with mbed-os-example-wifi.

The issues is different though - the stack overflows without a call to hal_deepsleep. It seems we only so sleep, at least during the first seconds of application. Then even if I fully empty hal_sleep(), we still hit the stack overflow, so this seems to be related to the mbed core, not to ST HAL layer.

@0xc0170 could someone help investigating that or provide ideas ... could that be hat calls to default_idle_hook() or some other place uses more than the default IDLE stack size ?

Having MBED_CONF_APP_IDLE_THREAD_STACK_SIZE defined to > 544 (instead of 512) seems to solve the issue.

There are cases where a call hal_deepsleep would overflow the idle task
stack, especially in developper or debug profile.

In order to avoid this case, we split ForceClockOutofDeepSleep
into two separate functions the two structure RCC_ClkInitStruct and
RCC_OscInitStruct are not allocated at the same time.
@LMESTM
Copy link
Contributor Author

LMESTM commented Jun 19, 2018

@0xc0170 @janjongboom I still think this PR would help if not resolving everything. I fixed indentation issues and added a comment as suggested.

@0xc0170
Copy link
Contributor

0xc0170 commented Jun 20, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Jun 20, 2018

Build : SUCCESS

Build number : 2396
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/7241/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Jun 20, 2018

@mbed-ci
Copy link

mbed-ci commented Jun 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants