greiman / ChRt

ChibiOS/RT for Arduino AVR, SAMD, Due, Teensy 3.x, Teensy 4.0.
88 stars 26 forks source link

heap support #7

Open Vigeant opened 5 years ago

Vigeant commented 5 years ago

Good day Greiman,

Awesome job on this port. Not that I am a fan of using the heap in hard real time applications but do you foresee enabling it. I noticed it was disabled in the config_arm.h (CH_CFG_USE_HEAP). Simply setting that to true and trying to create an object on the heap simply does a hard fault.

Some libraries tend to use the arduino String and it fails silently. I have been converting those to C strings but...

greiman commented 5 years ago

The ChibiOS heap will not fix String or other classes/programs that use the Arduino heap.

The Arduino implementation of malloc, for most architectures, assumes the stack is above the heap and will not work for threads since a thread's stack is below the heap in a work area.

The ChibiOS memory heap uses a static memory area allocated like the thread work areas.

Here is the API.

You init a heap with something like this:

#define ALLOC_SIZE 16
#define HEAP_SIZE (ALLOC_SIZE * 8)

static memory_heap_t test_heap;
static CH_HEAP_AREA(myheap, HEAP_SIZE);

  // init call 
  chHeapObjectInit(&test_heap, myheap, sizeof(myheap));

Then use use these calls.

    p1 = chHeapAlloc(&test_heap, ALLOC_SIZE);
   // ...
    chHeapFree(p1);

I have not tested heap use with ChRt so some pieces may be missing.