From 0995cec490e62d150913789d3bdb848e8445aba9 Mon Sep 17 00:00:00 2001 From: Tim Vrakas Date: Sat, 8 Aug 2020 15:03:50 -0700 Subject: [PATCH] Allocate descriptors from local buffer, rather than using memalign() --- libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.cpp | 13 +++++++++++-- libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.h | 7 +++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.cpp b/libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.cpp index 0adb78470..f89c71a88 100644 --- a/libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.cpp +++ b/libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.cpp @@ -1,5 +1,4 @@ #include -#include // memalign() function #include "utility/dma.h" static volatile uint32_t _channelMask = 0; // Bitmask of allocated channels @@ -477,7 +476,7 @@ DmacDescriptor *Adafruit_ZeroDMA::addDescriptor( // (aligned_alloc() or posix_memalign()) are not currently // available in the version of ARM GCC in use, but this is, // so here we are. - if(!(desc = (DmacDescriptor *)memalign(16, sizeof(DmacDescriptor)))) { + if(!(desc = alloc_descriptor())) { return NULL; } DmacDescriptor *prev = &_descriptor[channel]; @@ -613,6 +612,16 @@ void Adafruit_ZeroDMA::loop(boolean flag) { } } +//Alocates a descriptor from the local pool +DmacDescriptor * Adafruit_ZeroDMA::alloc_descriptor(){ + if(allocated * sizeof(DmacDescriptor) >= sizeof(desc_heap)){ + return (DmacDescriptor *)(&desc_heap[0] + (allocated++) * sizeof(DmacDescriptor)); + }else + { + return NULL; + } +} + // MISCELLANY -------------------------------------------------------------- void Adafruit_ZeroDMA::printStatus(ZeroDMAstatus s) const { diff --git a/libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.h b/libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.h index fc8461993..d94803c3b 100644 --- a/libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.h +++ b/libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.h @@ -4,6 +4,10 @@ #include "Arduino.h" #include "utility/dma.h" +#ifndef DMAC_DESC_HEAP_SIZE +#define DMAC_DESC_HEAP_SIZE 4 +#endif + // Status codes returned by some DMA functions and/or held in // a channel's jobStatus variable. enum ZeroDMAstatus { @@ -54,6 +58,8 @@ class Adafruit_ZeroDMA { protected: + __attribute__((__aligned__(16))) uint8_t desc_heap[DMAC_DESC_HEAP_SIZE*16]; + DmacDescriptor * alloc_descriptor(); uint8_t channel; volatile enum ZeroDMAstatus jobStatus; bool hasDescriptors; @@ -61,6 +67,7 @@ class Adafruit_ZeroDMA { uint8_t peripheralTrigger; dma_transfer_trigger_action triggerAction; void (*callback[DMA_CALLBACK_N])(Adafruit_ZeroDMA *); + size_t allocated = 0; }; #endif // _ADAFRUIT_ZERODMA_H_