This thread discusses problems with the Sloeber IDE, which does not fully support the STM32.
The problems described in this post do not occur if you use the Arduino IDE
See this post http://www.stm32duino.com/viewtopic.php … =70#p33060
Hello, first post here.
I am working with the Sloeber, but I am adding Roger Clark’s github libraries.
I am trying to port the KNX library from Franck Marini and so far I am having problems with malloc.
When I remove the malloc call, the program does not crash, when I leave it in there, it crashes.
I think it has to do with the Sloeber configuration.
In the menu->preferences, in Arduino:
Private Library path: C:\Arduino\hardware\Arduino_STM32\STM32F1\libraries
Private hardware path: C:\Arduino\hardware\Arduino_STM32\STM32F1
The above are, as I mentioned, from Roger Clark’s github.
When I right click->go to definition on the malloc function, the choices I have, are not from these paths, instead they are from some Sloeber paths.
Do you also think that this may cause the problem?
How can I change that?
Apart from that, the ILI9341 test code works great when the program does not crash.
Thanks everyone!
Bill.
Malloc is not recommended for devices with such small memories.
This should not be the case, it should work, right? Is there any known bug for malloc?
I think what you are saying is that you tried to use a specific 3rd party library, which did not work.
The library you vaguely referred to seems to be
https://github.com/franckmarini/KnxDevice
I suspect the bug has nothing to do with malloc, unless its the size its malloc’ing is effected by the size native types on this 32 bit device vs size of objects on the 8 bit AVR device e.g. you could end up attempting to malloc 4 times as much memory as on the AVR
However the problem is more likely to be something to do with global constructor instantiation where the constructor attemps to use the hardware before parts of the core have been initialised
Yes this is the library.
I think you are correct, the problem is caused in the construction of the KNX objects.
Since the constructor of the KNX objects is using malloc, I imagine that malloc tries to allocate non-existing memory since the init isn’t fully done yet.
I don’t think I would ever think of that (well, from now on I hope I will
)
What is your suggestion on solving this? How do I tell the software to construct the objects after the full init of the core?
Is there an attribute that can specify this?
Or is it a good approach to first KNXObject pointers and init them in setup() and never destruct them?
Thanks for the insight,
Bill.
In the constructor, if you change the code to return just prior to the malloc, I presume its OK, and if you return just after the malloc, it never returns ?
You also need to make a really simple example, that just has the global instance of that library object and don’t try to use the object.
PS.
Looking at the code, it looks like one of the constructors calls
https://github.com/franckmarini/KnxDevi … ct.cpp#L39
calls a function lengthCalculation
https://github.com/franckmarini/KnxDevi … pp#L28-L30
which uses program memory reads to load from this array
https://github.com/franckmarini/KnxDevi … #L109-L171
I think its far more likely that these program memory arrays have not been setup when the code attempts to use them, and therefore tries to malloc a huge amount of memory
Basically the code is written for the AVR processor which does not use the same system architecture (flat memory addressing),
e.g. AVR uses Harvard architecture https://en.wikipedia.org/wiki/Atmel_AVR
where as ARM devices Von Neumann architecture use https://en.wikipedia.org/wiki/Von_Neumann_architecture
I’d suggest you get rid of the progmem stuff first
[RogerClark – Wed Aug 09, 2017 7:55 am] –
Are you sure its malloc that is crashing ?In the constructor, if you change the code to return just prior to the malloc, I presume its OK, and if you return just after the malloc, it never returns ?
You also need to make a really simple example, that just has the global instance of that library object and don’t try to use the object.
I have done that, I commented out just the malloc part, I constructed the objects and didn’t use it, and it didn’t crash.
PS.
Looking at the code, it looks like one of the constructors calls
https://github.com/franckmarini/KnxDevi … ct.cpp#L39
calls a function lengthCalculation
https://github.com/franckmarini/KnxDevi … pp#L28-L30
which uses program memory reads to load from this array
https://github.com/franckmarini/KnxDevi … #L109-L171
I think its far more likely that these program memory arrays have not been setup when the code attempts to use them, and therefore tries to malloc a huge amount of memory
Basically the code is written for the AVR processor which does not use the same system architecture (flat memory addressing),
e.g. AVR uses Harvard architecture https://en.wikipedia.org/wiki/Atmel_AVR
where as ARM devices Von Neumann architecture use https://en.wikipedia.org/wiki/Von_Neumann_architecture
I’d suggest you get rid of the progmem stuff first
I was reading about progmem in a post here on how to import libraries and I got rid of the PROGMEM word at the first stages of compiling. I also removed pgm_read_byte:
pgm_read_byte(&KnxDPTIdToFormat[dptId]) becomes —–> KnxDPTIdToFormat[dptId]
However, it still crashes. I feel very bad about this, I have no idea what to do.
Thanks,
Bill.
chance the code so that it just uses a length of 100, and see if that still crashes
e.g.
_length(100)
#ifdef KNX_COM_OBJ_SUPPORT_ALL_PRIORITIES
KnxComObject::KnxComObject(word addr, e_KnxDPT_ID dptId, e_KnxPriority prio, byte indicator )
: _addr(addr), _dptId(dptId), _indicator(indicator), _length(100), _prio(prio)
#else
KnxComObject::KnxComObject(word addr, e_KnxDPT_ID dptId, byte indicator )
: _addr(addr), _dptId(dptId), _indicator(indicator), _length(100)
#endif
{
if (_length <= 2) _longValue = NULL; // short value case
else { // long value case
_longValue = (byte *) malloc(_length-1);
for (byte i=0; i <_length-1 ; i++) _longValue[i] = 0;
}
if (_indicator & KNX_COM_OBJ_I_INDICATOR) _validity = false; // case of object with "InitRead" indicator
else _validity = true; // case of object without "InitRead" indicator
}
Bill.
Perhaps I didn’t notice yesterday.
When I add the library in the project (Sloeber menu->Arduino->Add a library to the selected project) it crashes (or it doesn’t even run, cannot tell).
I am doing nothing else, I am not calling anything or even creating Objects the start of the file, not even including files from the library. I am just adding the library.
If I remove the library, it works.
Has anyone seen something like that?
If I add another library, for example the Wire library, it doesn’t have any problems.
Thanks,
Bill.
I went to the STM32Duino and I just included the library and it didn’t crash.
Not it is crashing in another line, but at least this seems to be a matter of code.
So, for now I am just changing the libraries in Sloeber and for the last compilation, I compile in the STM32duino and upload from there.
Thanks,
Bill.
Try using the normal Arduino IDE, instead …
It worked in the Arduino IDE.
So, as I wrote, I am using the Sloeber for development (code navigation and all the goodies) and I just compile and upload from the IDE.
Thanks!
Bill.
The core is only written to work with the Arduino IDE.
I don’t use other IDEs for Arduino development, as generally there are multiple incompatibilities.
You should contact the Sleober team about this problem
Sloeber does not have any “own” libraries or cores.
It uses libmaple or stm32generic or any other core you select for compilation (called “Platform” in Sloeber – “The Platform you want to use – Platform Folder” selector).
I have 11 cores (Platform folders) in Sloeber and I can select and compile a source in any of those cores.
You have to spend some time to learn how to work with Sloeber. Sometimes (for example when you mess with libraries manually or when you copy/paste projects and you changing includes afterwards) you have to clean up the list in “Include Folders” to get rid of those you do not need – most probably your case (sometimes it happens you get there two lib includes from different locations for the same lib). There are 3 “Include Folders” lists you may edit (all 3 lists must be with the same content).
In Project -> Properties (an Example):

- Include list Sloeber.JPG (124.38 KiB) Viewed 396 times
Roger I changed the first post, do you need me to change the replies as well?
That is our standard compiler, not Sloeber related..
.

- KNX malloc issue.JPG (79.19 KiB) Viewed 365 times
Pito, the fix you suggested is exactly what the code is at the first place.
While I was debugging, I saw it really crashed when it was getting into malloc().
I went in there with assembly step into (I really don’t know ARM asm) and I saw that it crashed at a certain point and then my PC is going in __stm32reservedexception9. This is exactly after the instruction: “sub.w r3, r2, r6”.
I am shooting in the dark here, but I found this: http://infocenter.arm.com/help/index.js … HBIDD.html, is there a case where the processor is confused about 32 bit ARM and 16 bit mode Thumb instruction set?
I am asking because I also found this: https://stackoverflow.com/questions/265 … init-array
Any help is welcome!
Thanks,
Bill.
Multiple errors reported.
1) Failed to execute MI command:
-var-create - * _longValue
Error message from debugger back end:
-var-create: unable to create variable object
2) Unable to create variable object
3) Failed to execute MI command:
-data-evaluate-expression _longValue
Error message from debugger back end:
No symbol "_longValue" in current context.
4) Failed to execute MI command:
-var-create - * _longValue
Error message from debugger back end:
-var-create: unable to create variable object
Pointers in AVR are 16bits, while in ARM are 32bits, so malloc may not be allocating enough memony to hold a pointer to a byte, which is what I understand it needs to allocate.
Try
union {
// field used in case of short value (1 byte max width, i.e. length <= 2)
struct{
byte _value;
byte _notUSed;
byte _notUSed; // added Pito
byte _notUSed; // added Pito
};
// field used in case of long value (2 bytes width or more, i.e. length > 2)
// The data space is allocated dynamically by the constructor
byte *_longValue;
};However when I removed the malloc and placed a statically allocated buffer it didn’t crash at that point, but it did somewhere else.
I will look at it later.
I am going to try just a simple program with one malloc and see if it crashes.
Pito have you ever used malloc/new in any Sloeber projects of yours?
Thanks,
Bill.
Serial.println("Allocating EXRAM32 memory..");
/* EXRAM32 Initial memory allocation */
uint32_t* EXRAM32 = (uint32_t*) malloc(n * sizeof(uint32_t));
if (NULL == EXRAM32) {
Serial.println(" ############### EXRAM32 MALLOC FAILED..");
return -1;
}
..
/* EXRAM8 Initial memory allocation */
uint8_t* EXRAM8 = (uint8_t*) malloc(n * sizeof(uint8_t));
if (NULL == EXRAM8) {
Serial.println(" ############### EXRAM8 MALLOC FAILED..");
return -1;
}
http://www.stm32duino.com/viewtopic.php … 839#p27839
adjust the array size “n” such it fits (via the malloc) into your internal sram (like n = 1000..4000 -> 4kB..16kB).
It crashed.
Therefore my Sloeber is not correctly configured to handle malloc.
Perhaps some library is referenced from a wrong point.
Any idea how to proceed here?
[Pito – Tue Aug 15, 2017 2:28 pm] –
You may try this demo
http://www.stm32duino.com/viewtopic.php … 839#p27839
adjust the array size “n” such it fits (via the malloc) into your internal sram (like n = 1000..4000 -> 4kB..16kB).
I just saw this post after I posted my previous answer.
I tried it and it crashed on malloc.
It may happen you do not have any Heap defined.. Malloc and friends allocate spaces in Heap..
I am going to try in my Sloeber with the demo to doublecheck..
Is this the selection in C/C++ Build -> settings where there is a processor selection?
Or is this in a library path somewhere?
Where do I define the Heap? I thought this is taken care of in some already made startup file.
Roger’s Libmaple latest: Crashes here – PROBLEM WITH HEAP !!!
STM32Generic: works fine:
Allocating EXRAM32 memory..
********
Generating 4000 32bit uints:
Checksum of generated random numbers: 3737197177307
Checksum of unsorted EXRAM32 content: 3737197177307
BubbleSorting 32bit uints:
Wait while BubbleSorting.. Loops 0 out of 4000
Wait while BubbleSorting.. Loops 1000 out of 4000
Wait while BubbleSorting.. Loops 2000 out of 4000
Wait while BubbleSorting.. Loops 3000 out of 4000
Elapsed: 2456 msecs
Checksum of sorted numbers: 3737197177307
Sorted last 10 in ascending order:
3990 1994165868
3991 1994355317
3992 1994787653
3993 1994815262
3994 1994870290
3995 1996675596
3996 1996946895
3997 1997629299
3998 1998548949
3999 1999650861I only have this:

- selections.png (20.81 KiB) Viewed 242 times
The HEAP is defined in the ld file located in ..STM32\STM32F1\variants\maple_mini\ld\common.inc
_lm_heap_start = DEFINED(_lm_heap_start) ? _lm_heap_start : _end;
_lm_heap_end = DEFINED(_lm_heap_end) ? _lm_heap_end : __msp_init;
} > REGION_RODATA
I will try to revert to a previous core from Roger’s repository to see if it works.
So I cannot go back. I will wait for Roger’s fix.
[kostbill – Tue Aug 15, 2017 8:33 pm] –
So I cannot go back. I will wait for Roger’s fix.
You seem to misunderstand how community / open software development works
The process is that you submit a fix via a PR to github, this is then reviewed by the community via a thread on the forum, and if people using the Arduino IDE find no problems with your fix, it may eventually be merged back into the core.
[RogerClark – Tue Aug 15, 2017 10:58 pm] –
You seem to misunderstand how community / open software development worksThe process is that you submit a fix via a PR to github, this is then reviewed by the community via a thread on the forum, and if people using the Arduino IDE find no problems with your fix, it may eventually be merged back into the core.
Sorry for that Roger. Of course you are right. Let me explain:
1. I don’t mean that I will wait and I want it tomorrow. I mean that perhaps it will be fixed and perhaps it won’t.
I know it is not your burden. I also understand that it is very low priority since not a lot of people are using malloc.
2. I have not the slightest idea on how to fix this. I mean, if I try, I will have to spend a lot of time for that and I think it is counter productive, especially if people with far more superior knowledge than mine can solve this in just a couple of hours.
Having said that, I am willing to go through this if I can get some guidance, I don’t even know how to start debugging this.
I don’t know what the problem is, except from what Pito said, that it is a heap problem.
Just hit me with an ARM startup page about this, it will save me a couple of hours.
Thanks and I apologize if I got into your nerves!
Bill.
There have been many changes recently, I think quite a few large ones related to LTO.
If the December 2016 version does not work, try this, this PR was before most or all the LTO changes and changes in the menu options:
https://github.com/rogerclarkmelbourne/ … febd20dc0c
You can download that point in time with the green “clone or download” button, then Download Zip.
Please post back if that resolves the issue with a basic sketch with malloc only, and in that case, if it also resolves it with your sketch.
I’m busy with a bunch of other things and can test that out myself.
So this is something unique to Sleober
I’m not saying there is no bug in LibMaple, but malloc appears to be totally usable.
Re: Changes for LTO
The default is for LTO to be off.
However…
If I try to malloc more memory than is available on the heap, like this.
(it keeps malloc’ing 8 bytes prints out the pointer address and then loops back again to malloc another 8 bytes, repeatedly)
void setup() {
}
void loop() {
uint32_t *p = (uint32_t *)malloc(8);
Serial.println((int)p);
delay(10);
}
What I see from your repository is that you are acquiring the _lm_heap_start and _lm_heap_end from the linker.
I didn’t find a hardcoded definition, so I assume it is embedded in the toolchain, in some binary file.
https://github.com/rogerclarkmelbourne/ … common.inc:
/* Heap boundaries, for libmaple */
EXTERN(_lm_heap_start);
EXTERN(_lm_heap_end);
I don’t know the details of this, as I didnt write the original code. It was written my Leaflabs but they abandoned it 3 or 4 years ago.
Unfortunately Leaflabs don’t answer questions e.g. as issues on their github repo which still contains the original libmaple code.
So we can only speculate on these sorts of design decisions
The process is that you submit a fix via a PR to github, this is then reviewed by the community via a thread on the forum
This may work in a community of Experts who deal with the code at almost expert level.
“Arduino” community is not such a community.
This is not a community of STM32 core software developers.
The vast majority of people here are the USERS of MapleMinis and BluePills.
It would be great if this would be understood by our Experts as well.
For example:
When User Bill has got some issues “around” malloc(), and an another User e.g. Pito will try him to help (best how he can do), for example he will try to run a test and publish a result to compare ie. the versions, environments. Pito even asked a third person to verify.
When the problem is well understood and replicated by the Users, the Experts may dig into and issue a PR, and the Experts may discuss at github how it should be implemented in very detail in their cores or libraries.
Again, try to understand this community.
Also you cannot expect an User will identify the issues/bugs in their sketches/libraries or cores immediately.
In 80% of cases the Name of a thread is not precise and reveals the issue is different. That is normal in this community. The name of the Bill’s thread changed 3x. And it could be it changes itself a fourth time. That is normal in this community.
Pito created 2 threads:
[STM32GENERIC] – Latest Issues
[Libmaple] – Latest Issues
which may work as aggregates for issues close to this 2 popular cores used. Thus the Experts may react faster as they usually do not mess too much with threads which Names are weird.
Also mind the USERS will always ask help and questions which duplicates another threads and posts. You cannot avoid that if you understand this community.
Look at Arduino forum, similar to this. There are hundreds of thousands duplicated posts with the same (few) questions. And another User or Expert is answering them again and again. No problem.
That is how the community of Users usually work..
[kostbill – Wed Aug 16, 2017 8:04 am] –
Roger,What I see from your repository is that you are acquiring the _lm_heap_start and _lm_heap_end from the linker.
I didn’t find a hardcoded definition, so I assume it is embedded in the toolchain, in some binary file.
https://github.com/rogerclarkmelbourne/ … common.inc:
/* Heap boundaries, for libmaple */
EXTERN(_lm_heap_start);
EXTERN(_lm_heap_end);
[Pito – Wed Aug 16, 2017 10:40 am] –
Pito, with all due respect, I think Roger’s comment was correct for that Bill had suggested (I will wait until Roger resolves it).
Doesn’t work like that here, there is no waiting for anyone else to resolve it no one here should think someone else is going to resolve anything. Roger may as well take his repo offline tomorrow if he wishes so and stop working on this altogether.
This is a community to discuss and help each other, but no expectation should ever be had that someone is going to do something. It’s all volunteer in our spare time. When I have time I help what I can, when I dont have time I have not help on something. Doesn’t mean anything other than I have time or I don’t have time, and is the same for Roger.
With the size of the forum Roger spends already quite a lot of time policying it and answering the questions he can. He also check almost every PR that is submitted in github that makes any sense (and many that do not), so I think is right for him to ask that people submit the PRs, and he will help review, test and approve them, with the help of the community.
I believe the right expectation is that when people finds issues they try to work toward resolution and not wait on someone else to resolve them, be it Roger or anyone else.
If Bill will rather wait than test things like he has been suggested, then he may wait for a long time. Perhaps someone resolves it, but there is a chance that doesn’t happen or not for a long time, so for everyone’s benefit, specially him, it is better to put some effort on testing what’s suggested, and investigate etc.
I believe from Bill’s reply afterwards that he understand that, and his comment about waiting for the solution was taken too literally and he is in the same page that this a joint effort with no expectations of what someone else will be able to do.
[victor_pv – Wed Aug 16, 2017 1:36 pm] –
That script has been unchanged for the longest time, and malloc() worked before, so the problem is most likely not related to the value of the heap start and end. But since there is a copy of that in each board folder, I advise you to check the version for the specific board you are using, just in case there was an unintentioanl change to common.inc / jtag.ld / flash.ld
I am looking at these files but I don’t know if they are good. What am I searching for?
If you don’t find any problem with the linker script of the ram/flash definitions being changed in those files for the board you use, then I think as suggested before that you should try a version of the repo from 1 or 2 months ago at least.
I tested a simple malloc program:
void setup() {
uint32_t n = 100, * vec;
Serial.begin(9600);
vec = (uint32_t*) malloc(n * sizeof(uint32_t));
if (vec == NULL) {
Serial.println("Not enough space.");
}
else {
Serial.print("malloc was successful.");
}
}
void loop(){}
Here is the assembly code block:
08002b4e: ldr r2, [r3, #0]
08002b50: cmp r1, r2
08002b52: it hi
08002b54: strhi r1, [r3, #0]
08002b56: ldr r3, [pc, #108] ; (0x8002bc4 <_malloc_r+824>)
08002b58: ldr r2, [r3, #0]
08002b5a: cmp r1, r2
08002b5c: ldr r2, [r4, #4]
08002b5e: it hi
08002b60: strhi r1, [r3, #0]
08002b62: bic.w r2, r2, #3
08002b66: cmp r6, r2
08002b68: sub.w r3, r2, r6
08002b6c: bhi.n 0x8002b72 <_malloc_r+742>
08002b6e: cmp r3, #15
08002b70: bgt.n 0x8002b7c <_malloc_r+752>
08002b72: mov r0, r5
08002b74: bl 0x8002ddc <__malloc_unlock>
http://www.stm32duino.com/viewtopic.php … 839#p27839
on BlackF407 and the malloc Allocates the memory “successfully” and returns the address of the start of the array
/* EXRAM32 Initial memory allocation */
uint32_t* EXRAM32 = (uint32_t*) malloc(n * sizeof(uint32_t));
if (NULL == EXRAM32) {
Serial.println(" ############### EXRAM32 MALLOC FAILED..");
//return -1;
}
Serial.println((uint32_t)EXRAM32); // We print here the address of the EXRAM32[0]
and can you post the simplest sketch that causes the crash for you and the board you select?
About what to look at the ld files, just check if for the board you are using the flash and ram start addresses and size are correct, and then check in the repo the history for those files for that board to see when was the last change and what was it.
I have used that sbrk function recently to get the top of the heap and my code did not crash, but the compiler optimizes a bunch of things away and I don’t think I was getting runtime heap, but rather a value calculated at compile time.
Pito you said it crashed for you too in a previous post, was that with the F1 libmaple core?
The Libmaple F1 crashes such I cannot get the address printed out. ( I do not have debugger handy these days).
Try to run the above sketch on MMini/BluePill or any other board (sketch prepared for you already, just compile and run) what you get.
UPDATE:
I downloaded 5mins back the latest Roger’s Libmaple repo – result as above, address 8.
I read back through all the posts by kostbill. He seems to be using a bluepill. He seems to be using Sloeber. He seems to be using all different cores so trying to answer a question about which one has issues is just an exercise in futility.
Before I delete this whole thread. Please answer this question.
Have you tried Roger’s core (stm32duino the libmaple one) with your blue pill running in the Arduino IDE? This core:
https://github.com/rogerclarkmelbourne/Arduino_STM32
I just tried the lastest core with a malloc test program and it works fine.
@kostbill Please explain your current setup and the problem with it or this thread will be deleted.
Allocating EXRAM32 memory..class dumbclass {
int foo;
int bar;
int counter;
public:
dumbclass(void) : foo(0),bar(0), counter(100) {
}
int getDelay() {
counter = (counter == 100 ) ? 900 : 100;
return counter;
}
};
char *buffer;
dumbclass *dumb;
void setup() {
pinMode(LED_BUILTIN,OUTPUT);
buffer = (char *)malloc(1024);
buffer[0] = 'a';
buffer[1023] = 'z';
buffer[4] = 100;
dumb = new dumbclass;
}
void loop() {
digitalWrite(LED_BUILTIN,LOW);
delay(dumb->getDelay());
digitalWrite(LED_BUILTIN,HIGH);
delay(900);
buffer[1] = (buffer[1] == 10) ? 255 : 10;
}
We asking our colleagues since ever tho run the above malloc test called BubbleSort.
Just run it and give us your results pls.
We appreciate your effort.
There is all information required you would need to practically help Bill with his issue.
I found the thread where you posted a link to a demo bubble sort. I tested it and it works fine:
Allocating EXRAM32 memory..
536874888
********
Generating 4000 32bit uints:
Checksum of generated random numbers: 3737197177307
Checksum of unsorted EXRAM32 content: 3737197177307
BubbleSorting 32bit uints:
Wait while BubbleSorting.. Loops 0 out of 4000
Wait while BubbleSorting.. Loops 1000 out of 4000
Wait while BubbleSorting.. Loops 2000 out of 4000
Wait while BubbleSorting.. Loops 3000 out of 4000
Elapsed: 2173 msecs
Checksum of sorted numbers: 3737197177307
Sorted last 10 in ascending order:
3990 1994165868
3991 1994355317
3992 1994787653
3993 1994815262
3994 1994870290
3995 1996675596
3996 1996946895
3997 1997629299
3998 1998548949
3999 1999650861
What repository did you use? Roger’s or Daniel’s?
Also, the latest versions?
Also, in what IDE are you working on?
[Pito – Wed Aug 16, 2017 6:25 pm] –
We asking our colleagues since ever tho run the above malloc test called BubbleSort.
BTW: There is nothing special about your BubbleSort test compared to what I tested. Both allocate memory and verify that it gets allocated.
If I force it:
extern "C" void *_sbrk(int incr);
void setup() {
_sbrk(0); // If I comment this out, no blinking
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(1000);
Serial.println((uint32_t)malloc(1));
}
[kostbill – Wed Aug 16, 2017 6:56 pm] –
Rick,
What repository did you use? Roger’s or Daniel’s?
Also, the latest versions?
Also, in what IDE are you working on?
I ask again @kostbill:
Have you tried Roger’s core (stm32duino the libmaple one) with your blue pill running in the Arduino IDE? This core:
https://github.com/rogerclarkmelbourne/Arduino_STM32
I just tried the lastest core with a malloc test program and it works fine.
@kostbill Please explain your current setup and the problem with it or this thread will be deleted.
Also, I just went through all the posts in this thread and I don’t see anywhere that you have posted the changes you made to the KNX_Device or the CLI libraries. Please post those if you want help.
[Rick Kimball – Wed Aug 16, 2017 7:01 pm] –[kostbill – Wed Aug 16, 2017 6:56 pm] –
Rick,
What repository did you use? Roger’s or Daniel’s?
Also, the latest versions?
Also, in what IDE are you working on?I ask again @kostbill:
Have you tried Roger’s core (stm32duino the libmaple one) with your blue pill running in the Arduino IDE? This core:
https://github.com/rogerclarkmelbourne/Arduino_STM32I just tried the lastest core with a malloc test program and it works fine.
@kostbill Please explain your current setup and the problem with it or this thread will be deleted.
Rick, all your questions are answered here but I will write them again.
– I am working on a BluePill.
– I am using Sloeber, 4.01 and 4.10.
– Malloc does not work. I am working with Pito’s bubblesort.
– When I am using Roger’s latest. It doesn’t work on Sloeber. IT DOES WORK on Arduino IDE.
– When I am using Daniel’s repository, it works on Sloeber.
– When I am using one of Roger’s older repositories, it still doesn’t work on Sloeber.
– In all the cases that it doesn’t work, I am programming with JTAG or Serial upload.
– Also, in all the cases that it doesn’t work, I am running the program normally and I am debugging it.
If I omitted something let me know, I don’t think it is necessary to delete this thread.
You can’t expect me to summarize everything I wrote in the last 6 pages in every new post I am writing.
[victor_pv – Wed Aug 16, 2017 5:23 pm] –
What version of the core is the oldest you have tried? (like the date, or link on github)
I don’t know how to find the date. How can I find the oldest one?
and can you post the simplest sketch that causes the crash for you and the board you select?
Any sketch that uses a malloc will do, you may use Pito’s bubblesort:
http://www.stm32duino.com/viewtopic.php … 839#p27839
About what to look at the ld files, just check if for the board you are using the flash and ram start addresses and size are correct, and then check in the repo the history for those files for that board to see when was the last change and what was it.
OK, so per your advice I checked some things and I found something, perhaps it is something useful:
BluePill has 20KB of SRAM.
In some of these scripts, we find that ram(rwx) provides only 17K:
https://github.com/rogerclarkmelbourne/ … /ram_c8.ld,
https://github.com/rogerclarkmelbourne/ … em-ram.inc,
https://github.com/rogerclarkmelbourne/ … -flash.inc
MEMORY
{
ram (rwx) : ORIGIN = 0x20000C00, LENGTH = 17K
rom (rx) : ORIGIN = 0x08005000, LENGTH = 0
}
[kostbill – Wed Aug 16, 2017 7:19 pm] –
– When I am using Roger’s latest. It doesn’t work on Sloeber. IT DOES WORK on Arduino IDE.
So malloc works fine with the Arduino IDE.
If you use Roger’s latest repository and the Arduino IDE and compile the example code with your KnxDevice and CLI modified libraries does that work?
[Rick Kimball – Wed Aug 16, 2017 7:01 pm] –
Also, I just went through all the posts in this thread and I don’t see anywhere that you have posted the changes you made to the KNX_Device or the CLI libraries. Please post those if you want help.
It really doesn’t matter about the KNX changes. The problem is the malloc, which crashes even without the KNX library, in a fresh, clean repository without me even adding the KNX library.
The KNX library works nice when I compile with the Arduino IDE.
[Rick Kimball – Wed Aug 16, 2017 7:31 pm] –
Do the people at Sloeber provide support for all the various arduino cores it uses?
I don’t know, I didn’t ask anyone else, this forum is the first I am asking.
[kostbill – Wed Aug 16, 2017 7:28 pm] – The problem is the malloc, which crashes even without the KNX library, in a fresh, clean repository without me even adding the KNX library.
The KNX library works nice when I compile with the Arduino IDE.
Have you read my post? viewtopic.php?f=41&t=2434&start=50#p33027
I guess this is a library order issue in the gcc linker step http://www.network-theory.co.uk/docs/gc … ro_18.html , and _sbrk is defined somewhere weakly that crashes which is why no compiler error.
[danieleff – Wed Aug 16, 2017 7:43 pm] –[kostbill – Wed Aug 16, 2017 7:28 pm] – The problem is the malloc, which crashes even without the KNX library, in a fresh, clean repository without me even adding the KNX library.
The KNX library works nice when I compile with the Arduino IDE.Have you read my post? viewtopic.php?f=41&t=2434&start=50#p33027
I guess this is a library order issue in the gcc linker step http://www.network-theory.co.uk/docs/gc … ro_18.html , and _sbrk is defined somewhere weakly that crashes which is why no compiler error.
No Daniel I didn’t, the truth is that I was a bit stressed because Rick was going to delete the thread and then I was not going to be able to read it and then I forgot.
But now that I read it and tested it, it seems to be working.
The first try was not successful, but I cleaned the project and it works!
Thank you very much Daniel!
Thanks everyone for their help.
Bill.
Starting combiner
“arm-none-eabi-g++” -Og -Wl,–gc-sections -mcpu=cortex-m3 “-T/home/kimballr/Arduino/hardware/stm32duino/STM32F1/variants/generic_stm32f103c/ld/jtag.ld” “-Wl,-Map,/home/kimballr/workspace_neon3/malloc_test/Release/malloc_test.map” “-L/home/kimballr/Arduino/hardware/stm32duino/STM32F1/variants/generic_stm32f103c/ld” -o “/home/kimballr/workspace_neon3/malloc_test/Release/malloc_test.elf” “-L/home/kimballr/workspace_neon3/malloc_test/Release” -lm -lgcc -mthumb -Wl,–cref -Wl,–check-sections -Wl,–gc-sections -Wl,–unresolved-symbols=report-all -Wl,–warn-common -Wl,–warn-section-align -Wl,–warn-unresolved-symbols -Wl,–start-group ./.ino.cpp.o /home/kimballr/workspace_neon3/malloc_test/Release/arduino.ar /home/kimballr/workspace_neon3/malloc_test/Release/arduino.ar -Wl,–end-group
/usr/bin/../lib/gcc/arm-none-eabi/6.3.1/../../../../arm-none-eabi/lib/thumb/v7-m/libnosys.a(sbrk.o): In function `_sbrk’:
sbrk.c:(.text._sbrk+0x18): warning: undefined reference to `end’
Finished building: malloc_test.
However it just goes along its merry way even though end is used by _sbrk
If I used gdb to see what happens …
(gdb) finish
Run till exit from #0 0x0800311e in _malloc_r ()
0x0800016a in setup () at ../malloc_test.ino:31
31 buffer = (char *)malloc(1024);
(gdb) n
33 buffer[0] = 'a';
(gdb) p buffer
$1 = 0x20000010 <usblib+4> ""
(gdb) p &usblib
usblib usblib_dev
(gdb) p &usblib
$2 = (usblib_dev *) 0x2000000c <usblib>
(gdb) p &buffer
$3 = (char **) 0x20000c14 <buffer>
(gdb) p *buffer
$4 = 0 '\000'
(gdb) p buffer
$5 = 0x20000010 <usblib+4> ""
(gdb)
$ grep ‘end =’ /home/kimballr/Arduino/hardware/stm32duino/STM32F1/variants/generic_stm32f103c/ld/common.inc
_end = __bss_end__;
end = _end; <<<<<< I added this line
After you compile with this change the linker should be happy and not complain about a missing ‘end’ symbol.
If you use the nm command you should find the ‘end’ symbol as some address in your RAM address that is greater than the highest data and bss address
$ arm-none-eabi-nm -CS malloc_test.elf | grep ‘B end’
20000f88 B end
This is not a problem with Roger’s repository nor is a problem with malloc. This problem is being caused by the way Sloeber is working. It is also a problem in that a major error happened and Sloeber doesn’t report it.
Please change the title of this thread to indicate that it is not an libmaple F1 malloc problem.
‘Sloeber is dumb and has a problem’ might be apt
You may be able to change the thread title.
Just edit the initial post. This does not change the titles on the replies but it does change what is seen in the listing in the container section.
If you can’t change it, let me know and I will see what permissions need to be changed to allow moderators to do that.
Thanks @kostbill
I often have to put [SOLVED] into titles as people can’t be bothered to do it themselves
[Rick Kimball – Wed Aug 16, 2017 9:48 pm] –
Dudes, let’s not hide the answer to this problem …
Rick, I do have a question about the end symbol, related to this problem.
From what I see _sbrk() declared in syscalls.c does not use “end”, in what file is the sbrk function that uses it?
I’m suspecting syscalls.c may not be included in the build, and I don’t see any other declaration for sbrk in libmaple F1, so is it pulling an sbrk function from the standard library?
From your output seems like it’s pulling it from libnosys.
In that case, when compiling in Arduino, is not pulling that and using the syscalls.c version?
I’m not at home and can’t compile to confirm the above.
[victor_pv – Wed Aug 16, 2017 9:53 pm] –
I’m suspecting syscalls.c may not be included in the build, and I don’t see any other declaration for sbrk in libmaple F1, so is it pulling an sbrk function from the standard library?
Exactly right. As I mentioned above, Sloeber is using a different command line to link than what the Arduino IDE produces. So you end up using the libc _sbrk(). I’d show you the command line except I already uninstalled Sloeber from my eclipse setup.
For the record this is the one produced by the Arduino IDE:
“arm-none-eabi-g++” -Os -Wl,–gc-sections -mcpu=cortex-m3 “-T/home/kimballr/Arduino/hardware/stm32duino/STM32F1/variants/generic_stm32f103c/ld/jtag_c8.ld” “-Wl,-Map,/tmp/arduino_build_6110/malloc_test.ino.map” “-L/home/kimballr/Arduino/hardware/stm32duino/STM32F1/variants/generic_stm32f103c/ld” -o “/tmp/arduino_build_6110/malloc_test.ino.elf” “-L/tmp/arduino_build_6110” -lm -lgcc -mthumb -Wl,–cref -Wl,–check-sections -Wl,–gc-sections -Wl,–unresolved-symbols=report-all -Wl,–warn-common -Wl,–warn-section-align -Wl,–warn-unresolved-symbols -Wl,–start-group “/tmp/arduino_build_6110/sketch/malloc_test.ino.cpp.o” “/tmp/arduino_build_6110/core/wirish/start.S.o” “/tmp/arduino_build_6110/core/wirish/start_c.c.o” “/tmp/arduino_build_6110/core/wirish/syscalls.c.o” “/tmp/arduino_build_6110/core/board.cpp.o” “/tmp/arduino_build_6110/core/wirish/boards.cpp.o” “/tmp/arduino_build_6110/core/wirish/boards_setup.cpp.o” “/tmp/arduino_build_6110/../arduino_cache_912348/core/core_stm32duino_STM32F1_genericSTM32F103C_device_variant_STM32F103C8,upload_method_STLinkMethod,cpu_speed_speed_72mhz,opt_osstd_755122cd08a3b2bdd89b2d83715e70db.a” -Wl,–end-group
Note that it uses the syscalls.c from variant/wirish not libc.
I looked at the differences, and esides the syscalls.c file having small differences, in the F4 is placed in a different folder, so I moved it over in the F1 to the same folder, but that did not make a difference.
Then I looked at the linker options, and the F4 is still using our old friend “–whole-archive”, so I added that to the combiner recipe in the F1 and BAM! now it’s linking syscalls correctly.
I wonder why the problem went away in the Arduino IDE, but is showing it’s ugly face in Sloeber ![]()
So I suspect that several of the other functions in syscalls.c may not be included either.
I need to run the compile in Arduino IDE and see if the .text size is different. I’m pretty sure it is much larger with the whole-archive flag, but I wonder if it is smaller when that flag is not present and compiled in sloeber, since it’s supposedly leaving things out.
https://github.com/Sloeber/arduino-ecli … .java#L650
It doesn’t look like they are doing anything special for the stm32 architecture and they don’t really care:
jantje wrote:“As I stated before: Feel free to add STM32 base support. In other words I’m not going to do it.
I’m wiling to support you in getting it done but you’ll have to do it.”
I think if someone is using an IDE other than the Arduino IDE, then we need to say that they need to get support from the forum for the IDE in question.
After all, the strap line I’ve had on the forum, from the beginning is “Everything relating to using STM32 boards with the Arduino IDE”
Anything else is just adding confusion and is Off Topic..
Edit. I”m going to edit the first post to link to what you found about Jantje’s plugin
It’s sad Sloeber has this problem, Eclipse is so much better than the Arduino IDE when working in the code.
From what I see Arduino IDE had the same problem not including syscalls_sam3.c for the SAM cores, instead of using whole-archive like we did for some time, they were forcing syscalls_sam3.c.o in the parameters to the linker.
Then eventually they found another way around, and is what’s implemented in their platform.txt file.
This thread shows how and why they were doing it first (forcing the file to be included by the linker)
https://github.com/arduino/Arduino/comm … e22a101e7e
This show when they found a different way around, using a new set of flags for the linker:
https://github.com/arduino/Arduino/pull/4810
We did not have those flags, but someway in Arduino IDE we are lucky enough that syscalls get linked while for the SAM it was not linking.
I tested adding the same flags, and it works. I get these 2 addresses for buffer and dumb in Rick’s example, which match with where my heap is:
20000C0C
20000C10
Whether we want to include this in the main repo, I don’t care, but I wanted to post here for everyone that uses Sloeber, and in case any future change to the Arduino ide, the source or anything results in syscalls.c not being linked with Arduino either, so hopefully we can find this post.
I will update the first post with this additional solution for anyone using Eclipse or another IDE facing the issue.
[victor_pv – Thu Aug 17, 2017 1:44 pm] –
We did not have those flags, but someway in Arduino IDE we are lucky enough that syscalls get linked while for the SAM it was not linking.
It works in Arduino for our core because syscalls.c is in the variant subdirectory. Any .c files in the variant sub directory are linked as objects not as a library. Those objects are placed in the linker command line before the library references. See the {objects_files} in the combine recipe
recipe.c.combine.pattern=”{compiler.path}{compiler.c.elf.cmd}” {compiler.c.elf.flags} -mcpu={build.mcu} “-T{build.variant.path}/{build.ldscript}” “-Wl,-Map,{build.path}/{build.project_name}.map” {compiler.c.elf.extra_flags} -o “{build.path}/{build.project_name}.elf” “-L{build.path}” -lm -lgcc -mthumb -Wl,–cref -Wl,–check-sections -Wl,–gc-sections -Wl,–unresolved-symbols=report-all -Wl,–warn-common -Wl,–warn-section-align -Wl,–warn-unresolved-symbols -Wl,–start-group {object_files} “{build.path}/{archive_file}” -Wl,–end-group
Because of this we don’t need to resort to doing the -u trick or any other tricks.
$ find variants/ -iname 'syscalls.c'
...
variants/generic_stm32f103c/wirish/syscalls.c
...
recipe.c.combine.pattern=”{compiler.path}{compiler.c.elf.cmd}” -mcpu={build.mcu} -mthumb {compiler.c.elf.flags} “-T{build.variant.path}/{build.ldscript}” “-Wl,-Map,{build.path}/{build.project_name}.map” {compiler.c.elf.extra_flags} -o “{build.path}/{build.project_name}.elf” “-L{build.path}” -Wl,–cref -Wl,–check-sections -Wl,–gc-sections -Wl,–entry=Reset_Handler -Wl,–unresolved-symbols=report-all -Wl,–warn-common -Wl,–warn-section-align -Wl,–start-group {compiler.combine.flags} {object_files} “{build.variant.path}/{build.variant_system_lib}” “{build.path}/{archive_file}” -Wl,–end-group -lm -gcc
… {build.variant.path}/{build.variant_system_lib} …
I’m going to guess that they take the files in the variant subdirectory and put them into a library instead of adding each .o file to the the object_files variable.
I’ll have to check what do they have as library, but I know there is a syscall_sam3.c file with the functions in one of the folders.
One other difference with our code, they have a syscalls.h header file, while we don’t, but even without that file if I add the “-u…” flags, syscall was being included correctly and the sketch running without crash.
With adding the {compiler.combine.flags} -u flags into platform.txt the malloc() now works with latest Libmaple and F1 (ie. F103ZE) under Sloeber.
I’m using Bluepill and Sloeber, but when I load the scketch on STM32, the micro crashes (with Arduino IDE the schetck works).
I found the problem: ringbuffer function use new function:
RingBuffer::RingBuffer(unsigned int size)
{
_size = size;
// add one char to terminate the string
ringBuf = new char[size+1]; <---- !
ringBufEnd = &ringBuf[size];
init();
}

