Description
I am trying to make progress in porting an esp32 arduino sketch to use stm32duino.
In the short term, my progress was impeded by a freertos memory management Issue stm32duino/STM32FreeRTOS#8.
As a temporary workaround because malloc()/realloc() were failing quite badly, I changed my sketch to use heap_4.c along with a statically-allocated memory buffer.
This worked quite well with one exception: my I2C I/O was failing.
After debugging it, I found that the i2c core driver was doing malloc/realloc() for its buffer management.
This is problematic for two reasons:
- it is nonreentrant, in the case where freertos is operating.
- It makes the stm32 arduino core dependent upon dynamically allocated memory, even in an environment in which the developer may wish to just use static
As a workaround, I added the code below to my sketch. I would suggest that the correct workaround, however, is to do something so that the STM32 FreeRTOS code can hook all core allocator requests and reroute them through its own appropriate per-port allocator.
#if configAPPLICATION_ALLOCATED_HEAP && !defined(ESP32DUINO)
// Define the static heap
uint8_t ucHeap[configTOTAL_HEAP_SIZE];
/* Prototypes for our hooks, decorated to make sure they are C bindings */
extern "C" {
extern void *malloc(size_t size);
extern void free(void *p);
extern void *realloc(void *p, size_t size);
}
void *malloc(size_t size) {
return pvPortMalloc(size);
}
void free(void *p) {
vPortFree(p);
}
void *realloc(void *p, size_t size) {
void *pnew = pvPortMalloc(size);
memcpy(pnew, p, size);
vPortFree(p);
return pnew;
}
#endif // configAPPLICATION_ALLOCATED_HEAP