mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-02-02 05:58:30 -04:00
HAL_ChibiOS: added a DMA reserve heap
this ensures we keep some DMA-capable memory aside for when it is needed
This commit is contained in:
parent
d1c271cd13
commit
c249e26a03
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include "hal.h"
|
#include "hal.h"
|
||||||
#include "usbcfg.h"
|
#include "usbcfg.h"
|
||||||
|
#include "stm32_util.h"
|
||||||
|
|
||||||
#if HAL_USE_PAL || defined(__DOXYGEN__)
|
#if HAL_USE_PAL || defined(__DOXYGEN__)
|
||||||
/**
|
/**
|
||||||
@ -64,6 +65,9 @@ void __early_init(void) {
|
|||||||
void __late_init(void) {
|
void __late_init(void) {
|
||||||
halInit();
|
halInit();
|
||||||
chSysInit();
|
chSysInit();
|
||||||
|
#if CH_CFG_USE_HEAP == TRUE
|
||||||
|
malloc_init();
|
||||||
|
#endif
|
||||||
#ifdef HAL_USB_PRODUCT_ID
|
#ifdef HAL_USB_PRODUCT_ID
|
||||||
setup_usb_strings();
|
setup_usb_strings();
|
||||||
#endif
|
#endif
|
||||||
|
@ -41,22 +41,49 @@
|
|||||||
|
|
||||||
#if defined(CCM_RAM_SIZE_KB)
|
#if defined(CCM_RAM_SIZE_KB)
|
||||||
static memory_heap_t ccm_heap;
|
static memory_heap_t ccm_heap;
|
||||||
static bool ccm_heap_initialised = false;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(DTCM_RAM_SIZE_KB)
|
#if defined(DTCM_RAM_SIZE_KB)
|
||||||
static memory_heap_t dtcm_heap;
|
static memory_heap_t dtcm_heap;
|
||||||
static bool dtcm_heap_initialised = false;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// size of memory reserved for dma-capable alloc
|
||||||
|
#ifndef DMA_RESERVE_SIZE
|
||||||
|
#define DMA_RESERVE_SIZE 4096
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static memory_heap_t dma_reserve_heap;
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
/*
|
||||||
|
create a DMA reserve heap, to ensure we keep some memory for DMA
|
||||||
|
safe memory allocations
|
||||||
|
*/
|
||||||
|
void *dma_reserve = malloc_dtcm(DMA_RESERVE_SIZE);
|
||||||
|
if (!dma_reserve) {
|
||||||
|
dma_reserve = chHeapAllocAligned(NULL, DMA_RESERVE_SIZE, MIN_ALIGNMENT);
|
||||||
|
}
|
||||||
|
chHeapObjectInit(&dma_reserve_heap, dma_reserve, DMA_RESERVE_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
void *malloc_ccm(size_t size)
|
void *malloc_ccm(size_t size)
|
||||||
{
|
{
|
||||||
void *p = NULL;
|
void *p = NULL;
|
||||||
#if defined(CCM_RAM_SIZE_KB)
|
#if defined(CCM_RAM_SIZE_KB)
|
||||||
if (!ccm_heap_initialised) {
|
|
||||||
ccm_heap_initialised = true;
|
|
||||||
chHeapObjectInit(&ccm_heap, (void *)CCM_BASE_ADDRESS, CCM_RAM_SIZE_KB*1024);
|
|
||||||
}
|
|
||||||
p = chHeapAllocAligned(&ccm_heap, size, CH_HEAP_ALIGNMENT);
|
p = chHeapAllocAligned(&ccm_heap, size, CH_HEAP_ALIGNMENT);
|
||||||
if (p != NULL) {
|
if (p != NULL) {
|
||||||
memset(p, 0, size);
|
memset(p, 0, size);
|
||||||
@ -67,21 +94,17 @@ void *malloc_ccm(size_t size)
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *malloc_dtcm(size_t size)
|
static void *malloc_dtcm(size_t size)
|
||||||
{
|
{
|
||||||
void *p = NULL;
|
void *p = NULL;
|
||||||
#if defined(DTCM_RAM_SIZE_KB)
|
#if defined(DTCM_RAM_SIZE_KB)
|
||||||
if (!dtcm_heap_initialised) {
|
|
||||||
dtcm_heap_initialised = true;
|
|
||||||
chHeapObjectInit(&dtcm_heap, (void *)DTCM_BASE_ADDRESS, DTCM_RAM_SIZE_KB*1024);
|
|
||||||
}
|
|
||||||
p = chHeapAllocAligned(&dtcm_heap, size, CH_HEAP_ALIGNMENT);
|
p = chHeapAllocAligned(&dtcm_heap, size, CH_HEAP_ALIGNMENT);
|
||||||
if (p != NULL) {
|
|
||||||
memset(p, 0, size);
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
(void)size;
|
(void)size;
|
||||||
#endif
|
#endif
|
||||||
|
if (p != NULL) {
|
||||||
|
memset(p, 0, size);
|
||||||
|
}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,24 +132,36 @@ void *malloc(size_t size)
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fall back to DMA reserve
|
||||||
|
p = chHeapAllocAligned(&dma_reserve_heap, size, MIN_ALIGNMENT);
|
||||||
|
if (p) {
|
||||||
|
memset(p, 0, size);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
allocte DMA-safe memory
|
allocate DMA-safe memory
|
||||||
*/
|
*/
|
||||||
void *malloc_dma(size_t size)
|
void *malloc_dma(size_t size)
|
||||||
{
|
{
|
||||||
|
void *p;
|
||||||
#if defined(DTCM_RAM_SIZE_KB)
|
#if defined(DTCM_RAM_SIZE_KB)
|
||||||
return malloc_dtcm(size);
|
p = malloc_dtcm(size);
|
||||||
#else
|
#else
|
||||||
// if we don't have DTCM memory then assume that main heap is DMA-safe
|
// if we don't have DTCM memory then assume that main heap is DMA-safe
|
||||||
void *p = chHeapAllocAligned(NULL, size, MIN_ALIGNMENT);
|
p = chHeapAllocAligned(NULL, size, MIN_ALIGNMENT);
|
||||||
|
#endif
|
||||||
|
if (p == NULL) {
|
||||||
|
p = chHeapAllocAligned(&dma_reserve_heap, size, MIN_ALIGNMENT);
|
||||||
|
}
|
||||||
if (p) {
|
if (p) {
|
||||||
memset(p, 0, size);
|
memset(p, 0, size);
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *calloc(size_t nmemb, size_t size)
|
void *calloc(size_t nmemb, size_t size)
|
||||||
@ -165,6 +200,10 @@ size_t mem_available(void)
|
|||||||
totalp += dtcm_available;
|
totalp += dtcm_available;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
size_t reserve_available = 0;
|
||||||
|
chHeapStatus(&dma_reserve_heap, &reserve_available, NULL);
|
||||||
|
totalp += reserve_available;
|
||||||
|
|
||||||
return totalp;
|
return totalp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,6 @@ void show_stack_usage(void);
|
|||||||
// allocation functions in malloc.c
|
// allocation functions in malloc.c
|
||||||
size_t mem_available(void);
|
size_t mem_available(void);
|
||||||
void *malloc_ccm(size_t size);
|
void *malloc_ccm(size_t size);
|
||||||
void *malloc_dtcm(size_t size);
|
|
||||||
void *malloc_dma(size_t size);
|
void *malloc_dma(size_t size);
|
||||||
|
|
||||||
// flush all dcache
|
// flush all dcache
|
||||||
@ -68,6 +67,9 @@ void set_fast_reboot(enum rtc_boot_magic v);
|
|||||||
// enable peripheral power if needed
|
// enable peripheral power if needed
|
||||||
void peripheral_power_enable(void);
|
void peripheral_power_enable(void);
|
||||||
|
|
||||||
|
// initialise allocation subsystem
|
||||||
|
void malloc_init(void);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user