cancel
Showing results for 
Search instead for 
Did you mean: 

Using the calloc function on an STM32F4

rajpaged
Associate II
Posted on July 15, 2014 at 17:12

I'm trying to understand how this function should be used on an F405 chip. The code that calls calloc is all generated from a simulink fuzzy controller model so I'm confident it's correct in terms of code.

The program seems to be ending up in the default handler as soon as it hits a calloc function. Do I need to sort out a heap for this or something? Not really sure how it works

#calloc-stm32f4
3 REPLIES 3
Posted on July 15, 2014 at 17:52

Yes, you'd need to address the size of the heap, however your particular tool chain deals with it. RTM

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rajpaged
Associate II
Posted on July 15, 2014 at 19:00

I think I got past that issue, I added this into the arm-gcc-link.ld

/* heap section */

    .co_heap (NOLOAD):

    {

      __cs3_heap_start = . ; 

        . = ALIGN(8);

        *(.co_heap .co_heap.*)

     __cs3_heap_end = . ; 

    } > ram

This into startup_stm324fxx.c

#define HEAP_SIZE       0x00000200

__attribute__ ((section(''.co_heap'')))

unsigned long pulHeap[HEAP_SIZE];

extern unsigned long __cs3_heap_start;

extern unsigned long __cs3_heap_end;

and this into my syscalls.c

__attribute__ ((used))

caddr_t _sbrk (int size)

{

   extern char __cs3_heap_start;

   extern char __cs3_heap_end ;

   static char *current_heap_end = &__cs3_heap_start;

   char *previous_heap_end;

   previous_heap_end = current_heap_end;

   if (current_heap_end + size > &__cs3_heap_end )

   {

      errno = ENOMEM;   // don't forget to include <errno.h>

      return (caddr_t) -1;

   }

   current_heap_end += size;

   return (caddr_t) previous_heap_end;

}

Now my code ends up in the default handler later on when assigning a value to one of the matlab generated structures. Is there a way I can determine how much memory it's all using from within GDB?
Posted on July 15, 2014 at 19:51

Can you not gauge the size and number of allocations the code might be expecting to use?

512 bytes won't go far if you have a 1024 array of 64-bit doubles

You could instrument calloc(), or a proxy, and quantify what's going on.

Is the machine generated code not looking for NULL returns from calloc()/malloc() ?

You could supply a Hard Fault handler, or other handlers, to more specifically identify the cause of the errors/faults.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..