123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- #include <stdio.h>
- #include <string.h>
- #include <hal.h>
- #include <ch.h>
- #include <stdarg.h>
- #include "stm32_util.h"
- #define MEM_REGION_FLAG_DMA_OK 1
- #define MEM_REGION_FLAG_FAST 2
- #define MEM_REGION_FLAG_SDCARD 4
- static const struct memory_region {
- void *address;
- uint32_t size;
- uint32_t flags;
- } memory_regions[] = { HAL_MEMORY_REGIONS };
- #define NUM_MEMORY_REGIONS (sizeof(memory_regions)/sizeof(memory_regions[0]))
- #if CH_CFG_USE_HEAP == TRUE
- static memory_heap_t heaps[NUM_MEMORY_REGIONS];
- #define MIN_ALIGNMENT 8
- #if defined(STM32H7)
- #define DMA_ALIGNMENT 32
- #else
- #define DMA_ALIGNMENT 8
- #endif
- #ifndef DMA_RESERVE_SIZE
- #define DMA_RESERVE_SIZE 4096
- #endif
- #if DMA_RESERVE_SIZE != 0
- static memory_heap_t dma_reserve_heap;
- #endif
- void malloc_init(void)
- {
- uint8_t i;
- for (i=1; i<NUM_MEMORY_REGIONS; i++) {
- chHeapObjectInit(&heaps[i], memory_regions[i].address, memory_regions[i].size);
- }
- #if DMA_RESERVE_SIZE != 0
-
- void *dma_reserve = malloc_dma(DMA_RESERVE_SIZE);
- osalDbgAssert(dma_reserve != NULL, "DMA reserve");
- chHeapObjectInit(&dma_reserve_heap, dma_reserve, DMA_RESERVE_SIZE);
- #endif
- }
- static void *malloc_flags(size_t size, uint32_t flags)
- {
- if (size == 0) {
- return NULL;
- }
- const uint8_t dma_flags = (MEM_REGION_FLAG_DMA_OK | MEM_REGION_FLAG_SDCARD);
- const uint8_t alignment = (flags&dma_flags?DMA_ALIGNMENT:MIN_ALIGNMENT);
- void *p = NULL;
- uint8_t i;
- if (flags & dma_flags) {
-
- size = (size + (DMA_ALIGNMENT-1)) & ~(DMA_ALIGNMENT-1);
- }
-
-
- if (flags == 0 || (flags == MEM_REGION_FLAG_DMA_OK &&
- (memory_regions[0].flags & MEM_REGION_FLAG_DMA_OK))) {
- p = chHeapAllocAligned(NULL, size, alignment);
- if (p) {
- goto found;
- }
- }
-
- for (i=1; i<NUM_MEMORY_REGIONS; i++) {
- if ((flags & MEM_REGION_FLAG_DMA_OK) &&
- !(memory_regions[i].flags & MEM_REGION_FLAG_DMA_OK)) {
- continue;
- }
- if ((flags & MEM_REGION_FLAG_SDCARD) &&
- !(memory_regions[i].flags & MEM_REGION_FLAG_SDCARD)) {
- continue;
- }
- if ((flags & MEM_REGION_FLAG_FAST) &&
- !(memory_regions[i].flags & MEM_REGION_FLAG_FAST)) {
- continue;
- }
- p = chHeapAllocAligned(&heaps[i], size, alignment);
- if (p) {
- goto found;
- }
- }
-
- if (!(flags & dma_flags)) {
- for (i=1; i<NUM_MEMORY_REGIONS; i++) {
- p = chHeapAllocAligned(&heaps[i], size, alignment);
- if (p) {
- goto found;
- }
- }
-
- p = chHeapAllocAligned(NULL, size, alignment);
- if (p) {
- goto found;
- }
- }
- #if DMA_RESERVE_SIZE != 0
-
- p = chHeapAllocAligned(&dma_reserve_heap, size, alignment);
- if (p) {
- memset(p, 0, size);
- return p;
- }
- #endif
-
- return NULL;
- found:
- memset(p, 0, size);
- return p;
- }
- void *malloc(size_t size)
- {
- return malloc_flags(size, 0);
- }
- void *malloc_dma(size_t size)
- {
- return malloc_flags(size, MEM_REGION_FLAG_DMA_OK);
- }
- void *malloc_sdcard_dma(size_t size)
- {
- #if defined(STM32H7)
- return malloc_flags(size, MEM_REGION_FLAG_SDCARD);
- #else
- return malloc_flags(size, MEM_REGION_FLAG_DMA_OK);
- #endif
- }
- void *malloc_fastmem(size_t size)
- {
- return malloc_flags(size, MEM_REGION_FLAG_FAST);
- }
- void *calloc(size_t nmemb, size_t size)
- {
- return malloc(nmemb * size);
- }
- void free(void *ptr)
- {
- if(ptr != NULL) {
- chHeapFree(ptr);
- }
- }
- size_t mem_available(void)
- {
- size_t totalp = 0;
- uint8_t i;
-
- chHeapStatus(NULL, &totalp, NULL);
-
- totalp += chCoreGetStatusX();
-
- for (i=1; i<NUM_MEMORY_REGIONS; i++) {
- size_t available = 0;
- chHeapStatus(&heaps[i], &available, NULL);
- totalp += available;
- }
- #if DMA_RESERVE_SIZE != 0
-
- size_t available = 0;
- chHeapStatus(&dma_reserve_heap, &available, NULL);
- totalp += available;
- #endif
- return totalp;
- }
- thread_t *thread_create_alloc(size_t size,
- const char *name, tprio_t prio,
- tfunc_t pf, void *arg)
- {
- thread_t *ret;
-
- ret = chThdCreateFromHeap(NULL, size, name, prio, pf, arg);
- if (ret != NULL) {
- return ret;
- }
-
- uint8_t i;
- for (i=1; i<NUM_MEMORY_REGIONS; i++) {
- ret = chThdCreateFromHeap(&heaps[i], size, name, prio, pf, arg);
- if (ret != NULL) {
- return ret;
- }
- }
- return NULL;
- }
- #endif
- void memory_flush_all(void)
- {
- uint8_t i;
- for (i=0; i<NUM_MEMORY_REGIONS; i++) {
- stm32_cacheBufferFlush(memory_regions[i].address, memory_regions[i].size);
- }
- }
|