-
Notifications
You must be signed in to change notification settings - Fork 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
STM32L4 Stack and Heap overlap problem #1348
Comments
Thanks for reporting. I just referenced the issue in PR which is now for review. Please review |
The change looks good. However, at present I have no local ARM compiler setup for local testing, I will test it on mbed.org when available. |
I found another problem in the stm32l476xx.sct config files (Nucleo abd Disco) |
The stack problem is now fixed with my previous checkins, the Nucleo L4 board works fine, the stack is correct however there are remaining problems/limitations: ( I am using the mbed.org online compiler) b) The Nucleo L4 board works fine, however the main memory heap/data region be mapped to 0x2000000 and should be mapped to 0x1000000 as defined in the .sct file. c) Allocating memory (in 4k blocks) it crashes after 84k (via malloc). I was expecting a NULL return instead of a crash. I am also to be able to alloc about 124k before I get can malloc error. Could it be that the mbed.org compiler has somewhere a linker file which needs to be updated? |
@helmut64 Still having problems with L4 and allocations? You can test the your example using sources from this repo to compare results. |
@MartinKojtal, the problem is not fully resolved.
The intention is that the data/heap memory gets first used from the SRAM2 at 0x10000000 (32k) and further used from the SRAM1 at 0x20000000, once the SRAM2 is used up until the stack starts. I configured this is the .sct files however data/heap starts at 0x20000000 (SRAM1). So somewhere is a different config which results into problems:
Regards |
Is there still some problem with the STM32L4 memory configuration?
The toolchain used for the offline builds was GCC ARM. |
@0xc0170 could you please have a look into this? |
I have not worked on this for some time. I verifyed today via the online compiler using the STM32L3 Nucleo board which works ok, the STM32L Discovery still crashes before main. My comment above from "Oct 4, 2015" is still valid. My testing belongs to the mbed online compiler. My plan was to debug it using the uvision IDE or gcc/gdb, which I have no done so far. For the gcc/gdb I am still looking for setup options on my Mac. I got uvision running on my Windows box, however I may need to purchase an unlimited copy because the free version has a 32k limit. |
@0xc0170 In the linker script file the memories are defined I changed for heap and stack sections:
and then the stack top definition
|
@melomaa, Below is a little test I wrote to verify the memory locations. Please note that when I call the MaxMemoryBlockAvailable (enable it first) the system hangs at the end of the memory, however it should give a NULL return which works fine on the STM32L1. Second we should get almost 128 kb 32+96 (minus stack) which is not the case. Third starting with the 32kb SRAM 2 and keeping the stack on the top os SRAM 2 has a major advantage we can disable SRAM 1 in deep sleep mode which allows further sleep modes and energy savings. I am available to discuss this further. Regards Helmut #include "mbed.h" DigitalOut myled(LED1); Serial *bs; void SunFunc() { size_t
} int main() {
}
} |
To start with the online compiler needs adjusting, build details report 32K RAM max (unless this has been corrected since I last updated/checked), these targets have 128K, I would start there first and get this corrected. The .sct files are the same for both targets, can't see why there is a problem between them, perhaps check the backend set up on the Mbed compiler. TBH I have been using both boards without problem's, probably lucky so far. The other problem you will run up against is the interface MCU will not always 'connect' correctly to the main MCU for programming. Downloading user code will sometimes 'brick' the MCU and will no longer program the MCU unless you power cycle the board to get the interface MCU led back to 'RED' condition, reload user code and it will be working again. Using an external programmer (SEGGER FLASHER) has no problems at all. So I would check the algo's for the L476 targets in the ST-Link firmware. |
@star297, I have multiple STM32L4 Disco and Nucleo boards, non of them are briked, I can load the original factory sample app using the STLink-utility. The original initial version had a wrong stack location which I fixed a few month ago. We can probably go back to the origianl config an swap the SRAM1 and SRAM2 configs which means the 96k are first. The mbed drive based flashing works, I can flash stuff. Question, you are telling me that the disco baord works with your app, are you using the on-line compiler? |
Yes, working Helmut. I only use Mbed online with the Nucleo board to develop my code then use the Segger Flasher to program my prototype board. I have only used 16K of RAM so far so maybe not hit the Stack/Heap issue. But I do use deepsleep extensively so will need this part retained in this mode. I have clock set up for 80MHz, found that Erik's timer WakeUp library would not work with the 48MHz set up. But that suits me better as I need high speed for a short period of time 100mS or so every minute then deepsleep for the rest of the time. Picture below is using quite a few resources 2 x i2c, SPI, AnalogIn, RTC and InterruptIn. Has been running for a couple of weeks, 22uA deepsleep maintaining the Sharp memory LCD and 15mA wake up. I have been using the disco board as well but not to the same extent. I will try the same code on this board too and see what happens, perhaps there is a problem in the set up there. RW data 32k L4-ECC-SRAM2 retained in standby I have not checked the manual, perhaps this is the only option with these two RAM blocks. But that Mbed build report of 32K RAM max is worrying, maybe the online compiler only counts the first block, not including the second block. In which case perhaps there is no problem there. Paul |
@star297, thanks for your explanation, nice work your project, something similar is also my target. At present I work with the Nucleo and Disco boards, I started using the L1, now L4, next is a own PCB. Regarding the max memory info, here may be the source of the problem. I organized the SCT that the 32k comes first, the stack is on top of the 32k and the 96k comes second. My idea was that we can be enhance the sleep mode where we have an option to turn off the 96k when needed, certainly this needs to be arranged with your project, maybe to reserve the 96k on startup and manage the 96k separately. Here are some of the existing problems:
Somebody who knows the mbed and ARM runtime and linker internals should be able to help. Regards Helmut PS: For programming you should also be able to use the Nucleo ST-Link interface which allows flashing via the USB drive and tty output via USB. |
Helmut, could you run your tests again, the Flash/RAM size settings are now correctly set up on Mbed. |
Dear Paul, the Flash/RAM sizes look correct in mbed, however there are two remaining problems: a) Discovery L4: crashes before main. (same as before). Regards Helmut |
Helmut, we now have a fix for the interrupt problem. |
@paul, at 0x20000000 starts the regular RAM, this is certainly a problem. I updated the stm32l476xx.sct which has the vectors at RW_IRAM1 (0x10000000) and missed the cmsis_nvic.c file. I don't have a local compiler, at present I use only the mbed.org one. I don't have a local ARM compiler, however I can do the changes on git-hub. Thank you, Helmut |
We have also changed the startup file for ARMCC and I can pass helmut's stack/heap example. I mean a NULL pointer is returned now without crash. I need to continue testing it but it looks ok... If you want I can send this startup file for you to test it also. Tell me. |
@bcostm, at present I don't own an ARM local compiler, I have used mbed only. My µVision has a 32kb limit which is good for debugging my code, but it cannot handle the entire mbed size. |
No problem helmut. I have tested your stack/heap example with an update of the ARM_MICRO startup file on Nucleo and Disco L4 boards with uVision. And everything looks ok. For ARM_STD the example works without changing the startup file. The new startup file is here if someone want to test it also before I send a PR: |
@bcostm, I have a question for your change. You specified the heap size of "0x8000 ; 32KB", however there are 98 vectors = 392 bytes (0x188) the the beginning of the RW_IRAM1. My original idea was that the vectors and stack in the in 32kb segment, that the heap also starts in the 32kb segment and continues in the 96k segment once the 32k are filled up. The benefit would be that the 32k supports the higher power saving mode where the 96k are switched off. |
Your idea is great but I don't know how to do it... |
I do not think that it is possible to specify that the heap is build up of more non-consecutive memory regions. As far as I know the uClib of the ARM toolset supports only two variables to specify the heap: |
@betzw, Hallo Wolfgang, I cannot imagine that there are not multiple regions where heap memory is located. There are many cases where an MCU has built-in memory and add-on memory which is all mapped into the main memory area at different locations. Independent of this I believe a working solution for the STM32L4 series has priority because we never had a reliable working mbed version for the L4 series. Regards Helmut |
Regarding multiple regions we need to ask ARM. |
@bcostm, @betzw, I tested today the L4 Disco and Nucleo boards, they booth work with the sample applications, etc. Regarding the memory test I get 96kb as expected, however the MaxMemoryBlockAvailable() hangs when it tries to allocate 97kb. On the L1 Nucleo board it return null as expected when it runs out of memory. I expect to get soon a Keil uVision license which allows me to debug this. The original problems are fixed. Thanks for helping. |
@helmut64 which toolchain did you use? |
@betzw, I used the mbed.org online compiler. |
@betzw Hi Wolfgang, I just know that using the STM32L1 Nucleo board this was working as expected on the L4 board it hangs. In my app I check regularly the available memory via such a loop report how much memory is available and to detect low memory situations. PS: I purchased an ARM Keil MDK µVision compiler today, which means I can do true debugging without any code limits. |
Ciao @helmut64, you might want to take a close look at issue #1561 and the discussion which is or rather has been ongoing there above all regarding to what people in ARM tell about the mbed-classic memory model, as this might mean for you that the way you try to get hold of the currently available amount of heap memory might make crash your application ... |
For the new STM32L4 I found out a major problem, the first 4k of the heap overlap with the stack!
More details here: https://developer.mbed.org/questions/60933/STM32L4-Stack-and-Heap-overlap-Disco/
I believe the problem is in startup_stm32l476xx.s (Disco & Nucleo)
The following line must be inserted/changed: (TOOLCHAIN_ARM_STD + TOOLCHAIN_ARM_MIKRO) initial_sp EQU 0x20018000 ; Top of RAM1 96k
This would at least enable the 96k of RAM1, however there is another 32k SRAM2 at 0x10000000-0x10008000. My preference would be use the SRAM2 for the stack and HEAP, and add the SRAM1 for additional HEAP.
Regards Helmut
The text was updated successfully, but these errors were encountered: