浏览代码

Add external memory allocation functions

main
PaulStoffregen 4 年前
父节点
当前提交
553538cebb
共有 3 个文件被更改,包括 56 次插入0 次删除
  1. +50
    -0
      teensy4/extmem.c
  2. +1
    -0
      teensy4/startup.c
  3. +5
    -0
      teensy4/wiring.h

+ 50
- 0
teensy4/extmem.c 查看文件

@@ -0,0 +1,50 @@
// External memory allocation functions. Attempt to use external memory,
// but automatically fall back to internal RAM if external RAM can't be used.

#include <stdlib.h>
#include "smalloc.h"
#include "wiring.h"

#if defined(ARDUINO_TEENSY41)
// Teensy 4.1 external RAM address range is 0x70000000 to 0x7FFFFFFF
#define HAS_EXTRAM
#define IS_EXTMEM(addr) (((uint32_t)ptr >> 28) == 7)
#endif


void *extmem_malloc(size_t size)
{
#ifdef HAS_EXTRAM
void *ptr = sm_malloc_pool(&extmem_smalloc_pool, size);
if (ptr) return ptr;
#endif
return malloc(size);
}

void extmem_free(void *ptr)
{
#ifdef HAS_EXTRAM
if (IS_EXTMEM(ptr)) {
sm_free_pool(&extmem_smalloc_pool, ptr);
return;
}
#endif
free(ptr);
}

void *extmem_calloc(size_t nmemb, size_t size)
{
return extmem_malloc(nmemb * size);
}

void *extmem_realloc(void *ptr, size_t size)
{
#ifdef HAS_EXTRAM
if (IS_EXTMEM(ptr)) {
return sm_realloc_pool(&extmem_smalloc_pool, ptr, size);
}
#endif
return realloc(ptr, size);
}



+ 1
- 0
teensy4/startup.c 查看文件

@@ -3,6 +3,7 @@
#include "usb_dev.h"
#include "avr/pgmspace.h"
#include "smalloc.h"
#include <string.h>

#include "debug/printf.h"


+ 5
- 0
teensy4/wiring.h 查看文件

@@ -208,6 +208,11 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val);
void setup(void);
void loop(void);

void *extmem_malloc(size_t size);
void extmem_free(void *ptr);
void *extmem_calloc(size_t nmemb, size_t size);
void *extmem_realloc(void *ptr, size_t size);

#ifdef __cplusplus
} // extern "C"
#endif

正在加载...
取消
保存