From cda195962907c6e31d52b0588a88e6d7cdb1273c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 17 Feb 2019 22:23:34 +1100 Subject: [PATCH] HAL_ChibiOS: added support for more flexible memory regions this allows for an arbitrary number of memory regions, with each one flagged as DMA safe, fast or normal --- libraries/AP_HAL_ChibiOS/Util.cpp | 12 +- libraries/AP_HAL_ChibiOS/Util.h | 2 - .../AP_HAL_ChibiOS/hwdef/common/malloc.c | 206 +++++++++--------- .../AP_HAL_ChibiOS/hwdef/common/stm32_util.c | 8 - .../AP_HAL_ChibiOS/hwdef/common/stm32_util.h | 2 +- .../hwdef/scripts/STM32F100xB.py | 12 +- .../hwdef/scripts/STM32F405xx.py | 16 +- .../hwdef/scripts/STM32F407xx.py | 16 +- .../hwdef/scripts/STM32F412Rx.py | 11 +- .../hwdef/scripts/STM32F427xx.py | 16 +- .../hwdef/scripts/STM32F469xx.py | 16 +- .../hwdef/scripts/STM32F745xx.py | 18 +- .../hwdef/scripts/STM32F767xx.py | 18 +- .../hwdef/scripts/STM32F777xx.py | 18 +- .../hwdef/scripts/STM32H743xx.py | 21 +- .../hwdef/scripts/chibios_hwdef.py | 32 +-- 16 files changed, 187 insertions(+), 237 deletions(-) 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