Bladeren bron

Add external memory allocation functions

main
PaulStoffregen 4 jaren geleden
bovenliggende
commit
553538cebb
3 gewijzigde bestanden met toevoegingen van 56 en 0 verwijderingen
  1. +50
    -0
      teensy4/extmem.c
  2. +1
    -0
      teensy4/startup.c
  3. +5
    -0
      teensy4/wiring.h

+ 50
- 0
teensy4/extmem.c Bestand weergeven

@@ -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 Bestand weergeven

@@ -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 Bestand weergeven

@@ -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

Laden…
Annuleren
Opslaan