|
| 1 | +// Double-buffered DMA on auxiliary SPI peripheral on pins 11/12/13. |
| 2 | +// Continuously alternates between two data buffers...one is filled |
| 3 | +// with new data as the other is being transmitted. |
| 4 | + |
| 5 | +#include <SPI.h> |
| 6 | +#include <Adafruit_ZeroDMA.h> |
| 7 | +#include "utility/dma.h" |
| 8 | +#include "wiring_private.h" // pinPeripheral() function |
| 9 | + |
| 10 | +// Declare our own SPI peripheral 'mySPI' on pins 11/12/13: |
| 11 | +// (Do not call this SPI1; Arduino Zero and Metro M0 already |
| 12 | +// have an SPI1 (the EDBG interface) and it won't compile.) |
| 13 | +SPIClass mySPI( |
| 14 | + &sercom1, // -> Sercom peripheral |
| 15 | + 34, // MISO pin (also digital pin 12) |
| 16 | + 37, // SCK pin (also digital pin 13) |
| 17 | + 35, // MOSI pin (also digital pin 11) |
| 18 | + SPI_PAD_0_SCK_1, // TX pad (MOSI, SCK pads) |
| 19 | + SERCOM_RX_PAD_3); // RX pad (MISO pad) |
| 20 | + |
| 21 | +Adafruit_ZeroDMA myDMA; |
| 22 | +ZeroDMAstatus stat; // DMA status codes returned by some functions |
| 23 | + |
| 24 | +// Data we'll issue to mySPI. There are TWO buffers; one being |
| 25 | +// filled with new data while the other's being transmitted in |
| 26 | +// the background. |
| 27 | +#define DATA_LENGTH 512 |
| 28 | +uint8_t source_memory[2][DATA_LENGTH], |
| 29 | + buffer_being_filled = 0, // Index of 'filling' buffer |
| 30 | + buffer_value = 0; // Value of fill |
| 31 | + |
| 32 | +volatile bool transfer_is_done = true; // Done yet? |
| 33 | + |
| 34 | +// Callback for end-of-DMA-transfer |
| 35 | +void dma_callback(Adafruit_ZeroDMA *dma) { |
| 36 | + transfer_is_done = true; |
| 37 | +} |
| 38 | + |
| 39 | +DmacDescriptor *desc; // DMA descriptor address (so we can change contents) |
| 40 | + |
| 41 | +void setup() { |
| 42 | + Serial.begin(115200); |
| 43 | + while(!Serial); // Wait for Serial monitor before continuing |
| 44 | + |
| 45 | + Serial.println("DMA test: SPI data out"); |
| 46 | + |
| 47 | + mySPI.begin(); |
| 48 | + // Assign pins 11, 12, 13 to SERCOM functionality |
| 49 | + pinPeripheral(11, PIO_SERCOM); |
| 50 | + pinPeripheral(12, PIO_SERCOM); |
| 51 | + pinPeripheral(13, PIO_SERCOM); |
| 52 | + |
| 53 | + // Configure DMA for SERCOM1 (our 'mySPI' port on 11/12/13) |
| 54 | + Serial.println("Configuring DMA trigger"); |
| 55 | + myDMA.setTrigger(SERCOM1_DMAC_ID_TX); |
| 56 | + myDMA.setAction(DMA_TRIGGER_ACTON_BEAT); |
| 57 | + |
| 58 | + Serial.print("Allocating DMA channel..."); |
| 59 | + stat = myDMA.allocate(); |
| 60 | + myDMA.printStatus(stat); |
| 61 | + |
| 62 | + desc = myDMA.addDescriptor( |
| 63 | + source_memory[buffer_being_filled], // move data from here |
| 64 | + (void *)(&SERCOM1->SPI.DATA.reg), // to here |
| 65 | + DATA_LENGTH, // this many... |
| 66 | + DMA_BEAT_SIZE_BYTE, // bytes/hword/words |
| 67 | + true, // increment source addr? |
| 68 | + false); // increment dest addr? |
| 69 | + |
| 70 | + Serial.println("Adding callback"); |
| 71 | + // register_callback() can optionally take a second argument |
| 72 | + // (callback type), default is DMA_CALLBACK_TRANSFER_DONE |
| 73 | + myDMA.setCallback(dma_callback); |
| 74 | +} |
| 75 | + |
| 76 | +void loop() { |
| 77 | + // Fill buffer with new data. The other buffer might |
| 78 | + // still be transmitting in the background via DMA. |
| 79 | + memset(source_memory[buffer_being_filled], buffer_value, DATA_LENGTH); |
| 80 | + |
| 81 | + // Wait for prior transfer to complete before starting new one... |
| 82 | + Serial.print("Waiting on prior transfer..."); |
| 83 | + while(!transfer_is_done) Serial.write('.'); |
| 84 | + mySPI.endTransaction(); |
| 85 | + Serial.println("Done!"); |
| 86 | + |
| 87 | + // Modify the DMA descriptor using the newly-filled buffer as source... |
| 88 | + myDMA.changeDescriptor(desc, // DMA descriptor address |
| 89 | + source_memory[buffer_being_filled]); // New src; dst & count don't change |
| 90 | + |
| 91 | + // Begin new transfer... |
| 92 | + Serial.println("Starting new transfer job"); |
| 93 | + mySPI.beginTransaction(SPISettings(12000000, MSBFIRST, SPI_MODE0)); |
| 94 | + transfer_is_done = false; // Reset 'done' flag |
| 95 | + stat = myDMA.startJob(); // Go! |
| 96 | + myDMA.printStatus(stat); |
| 97 | + |
| 98 | + // Switch buffer indices so the alternate buffer is filled/xfer'd |
| 99 | + // on the next pass. |
| 100 | + buffer_being_filled = 1 - buffer_being_filled; |
| 101 | + buffer_value++; |
| 102 | +} |
| 103 | + |
0 commit comments