diff --git a/libraries/AP_HAL_ChibiOS/Util.cpp b/libraries/AP_HAL_ChibiOS/Util.cpp index a640e1c1f4..00cccf7f67 100644 --- a/libraries/AP_HAL_ChibiOS/Util.cpp +++ b/libraries/AP_HAL_ChibiOS/Util.cpp @@ -54,7 +54,7 @@ void* Util::malloc_type(size_t size, AP_HAL::Util::Memory_Type mem_type) if (mem_type == AP_HAL::Util::MEM_DMA_SAFE) { return malloc_dma(size); } else if (mem_type == AP_HAL::Util::MEM_FAST) { - return try_alloc_from_ccm_ram(size); + return malloc_fastmem(size); } else { return calloc(1, size); } @@ -68,16 +68,6 @@ void Util::free_type(void *ptr, size_t size, AP_HAL::Util::Memory_Type mem_type) } -void* Util::try_alloc_from_ccm_ram(size_t size) -{ - void *ret = malloc_ccm(size); - if (ret == nullptr) { - //we failed to allocate from CCM so we are going to try common SRAM - ret = calloc(1, size); - } - return ret; -} - #ifdef ENABLE_HEAP void *Util::allocate_heap_memory(size_t size) diff --git a/libraries/AP_HAL_ChibiOS/Util.h b/libraries/AP_HAL_ChibiOS/Util.h index 4ca934a0dd..bf45451108 100644 --- a/libraries/AP_HAL_ChibiOS/Util.h +++ b/libraries/AP_HAL_ChibiOS/Util.h @@ -75,8 +75,6 @@ private: static ToneAlarmPwmGroup _toneAlarm_pwm_group; #endif - void* try_alloc_from_ccm_ram(size_t size); - uint32_t available_memory_in_ccm_ram(void); #if HAL_HAVE_IMU_HEATER struct { diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c b/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c index f986b8e04d..5e9fd46509 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c @@ -31,6 +31,20 @@ #include #include "stm32_util.h" +#define MEM_REGION_FLAG_DMA_OK 1 +#define MEM_REGION_FLAG_FAST 2 + +static struct memory_region { + void *address; + uint32_t size; + uint32_t flags; + memory_heap_t heap; +} memory_regions[] = { HAL_MEMORY_REGIONS }; + +// the first memory region is already setup as the ChibiOS +// default heap, so we will index from 1 in the allocators +#define NUM_MEMORY_REGIONS (sizeof(memory_regions)/sizeof(memory_regions[0])) + #if CH_CFG_USE_HEAP == TRUE #define MIN_ALIGNMENT 8 @@ -41,18 +55,6 @@ #define DMA_ALIGNMENT 8 #endif -#ifdef HAL_NO_CCM -#undef CCM_RAM_SIZE_KB -#endif - -#if defined(CCM_RAM_SIZE_KB) -static memory_heap_t ccm_heap; -#endif - -#if defined(DTCM_RAM_SIZE_KB) -static memory_heap_t dtcm_heap; -#endif - // size of memory reserved for dma-capable alloc #ifndef DMA_RESERVE_SIZE #define DMA_RESERVE_SIZE 4096 @@ -62,96 +64,102 @@ static memory_heap_t dtcm_heap; static memory_heap_t dma_reserve_heap; #endif -static void *malloc_dtcm(size_t size); - /* initialise memory handling */ void malloc_init(void) { -#if defined(CCM_RAM_SIZE_KB) - chHeapObjectInit(&ccm_heap, (void *)CCM_BASE_ADDRESS, CCM_RAM_SIZE_KB*1024); -#endif - -#if defined(DTCM_RAM_SIZE_KB) - chHeapObjectInit(&dtcm_heap, (void *)DTCM_BASE_ADDRESS, DTCM_RAM_SIZE_KB*1024); -#endif + for (uint8_t i=1; i