Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZeroDMA: Allocate descriptors from local buffer #247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <Adafruit_ZeroDMA.h>
#include <malloc.h> // memalign() function

#include "utility/dma.h"
static volatile uint32_t _channelMask = 0; // Bitmask of allocated channels
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions libraries/Adafruit_ZeroDMA/Adafruit_ZeroDMA.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -54,13 +58,16 @@ 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;
bool loopFlag;
uint8_t peripheralTrigger;
dma_transfer_trigger_action triggerAction;
void (*callback[DMA_CALLBACK_N])(Adafruit_ZeroDMA *);
size_t allocated = 0;
};

#endif // _ADAFRUIT_ZERODMA_H_