cancel
Showing results for 
Search instead for 
Did you mean: 

Cann't parse large string on STM32F746VGTX with cJSON

Tu Nguyen Ngoc
Associate II

Hi all,

I'm trying parse a large string stored on internal flash (up to 100 KB). Error is occurred around 60 KB.

I tested the same string with cJSON on Visual Studio, and it work well.

I think the problem is mcu doesn't have enough heap ram for cJSON.

I have tried to modify Minimum Heap Size in Linker Settings, but nothing changed.

jsonstr length 112393 [Bytes]

Error before: 60760

Error at: 60760

0693W000000Tc0RQAS.png

Can anyone help me please. Thank all in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Piranha
Chief II

Look at the code - that library relies heavily on malloc/free/realloc. Probably you are running into fragmentation. As MCUs doesn't have MMU and virtual memory, even if you will get it to work somehow, you'll never be able to guarantee stability.

And large JSON in internal flash... Why? Sounds like a bad design.

View solution in original post

4 REPLIES 4
matte
Associate II

Maybe you could verify your assumption by looking into how your RAM memory is consumed by the parser.

To do that you could have the Linker fill the RAM memory that is not used with a known pattern.

Then you could set some breakpoint in your parser to see how the RAM memory usage increases for each iteration the parser runs?!

If you want to insert a pattern in the RAM-memory using the Linker then try inserting Line 11 - 13 this: 

  /* User_heap_stack section, used to check that there is enough "RAM" Ram  type memory left */
  ._user_heap_stack :
  {
    . = ALIGN(8);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    . = . + _Min_Heap_Size;
    . = . + _Min_Stack_Size;
    . = ALIGN(8);
    
    FILL(0xFFFFFFFF);
    . = ORIGIN(RAM) + LENGTH(RAM) - 1;
    BYTE(0xAA)
 
  } >RAM

The example above is copied from a default MX-generated STM32L475VG linker script, where I just insert line 11 - 13... I imagine you have the same structure.

In this case I set the magic pattern to 0xFFFFFFFF. Should be OK in RAM. But maybe not a good idea in FLASH, since reset state is 0xFFFFFFFF in FLASH.

Adding these commands to the user_heap_stack section means that the pattern is only written when binary is loaded to target memory. If you pull the plug to the device the pattern is gone since it is not written by the startup code from flash to RAM.

Another side-effect is that the build analyzer will now report RAM memory usage ~100%.

Now step through your code and look in the memory browser view to see how RAM is consumed... Do you have a stack/heap collision?

Piranha
Chief II

Look at the code - that library relies heavily on malloc/free/realloc. Probably you are running into fragmentation. As MCUs doesn't have MMU and virtual memory, even if you will get it to work somehow, you'll never be able to guarantee stability.

And large JSON in internal flash... Why? Sounds like a bad design.

Thank for reply. I'm testing now.

Thank for your reply. Now I'm using internal flash just for testing. Actually , I send file via http to mcu and store on external flash.