Hello guys.
In my current application i use SPI and saw during analyzing the output with a oscilloscope "gaps" between each byte. I use a shortened form of the DMASpi example which can be found here: https://github.com/crteensy/DmaSpi
My code looks like this
So far its working well but as can be seen in the pictures below, in the green marked areas, there is a gap between every sent byte. Left is withOUT DMA and right is WITH DMA. So its almost the same behavior except that the gap gets smaller with DMA usage.
Where does it come from and can i get rid of it?
Thanks.
In my current application i use SPI and saw during analyzing the output with a oscilloscope "gaps" between each byte. I use a shortened form of the DMASpi example which can be found here: https://github.com/crteensy/DmaSpi
My code looks like this
Code:
//#include <WProgram.h>
#include <SPI.h>
#include <DmaSpi.h>
/** Important
The sketch waits for user input through Serial (USB) before it starts the setup.
After any key was pressed, it should begin to work. It will ask for a second keypress later.
This example cannot simply be modified to use SPI1 or SPI2 instead of SPI0 because ActiveLowChipSelect is
hardcoded to use SPI (SPI0).
If you want to use SPI1 or SPI2, you need to create a new chip select class to reflect this.
**/
/** Hardware setup:
Teensy 3.1, 3.2: DOUT (11) connected to DIN (12)
Teensy LC: DOUT (11) connected to DIN (12)
Teensy 3.5, 3.6: DOUT (11) connected to DIN (12)
**/
/** buffers to send from and to receive to **/
#define DMASIZE 1000
uint8_t src[DMASIZE];
volatile uint8_t dest[DMASIZE];
volatile uint8_t dest1[DMASIZE];
/** Wait for and consume a keypress over USB **/
void waitForKeyPress()
{
Serial.println("\nPress a key to continue\n");
while(!Serial.available());
while(Serial.available())
{
Serial.read();
}
}
//** View received data
void dumpBuffer(const volatile uint8_t* buf, const char* prefix)
{
Serial.print(prefix);
for (size_t i = 0; i < DMASIZE; i++)
{
Serial.printf("0x%02x ", buf[i]);
}
Serial.print('\n');
}
/** Compare the buffers and print the destination contents if there's a mismatch **/
void compareBuffers(const uint8_t* src_, const uint8_t* dest_)
{
int n = memcmp((const void*)src_, (const void*)dest_, DMASIZE);
if (n == 0)
{
Serial.println("src and dest match");
}
else
{
Serial.println("src and dest don't match");
dumpBuffer(src_, " src: " );
dumpBuffer(dest_, "dest: ");
}
}
//** Fill sender buffer with 0..99
void setSrc()
{
for (size_t i = 0; i < DMASIZE; i++)
{
src[i] = i;
}
/*
src[0] = 166;
src[1] = 154;
src[2] = 165;
src[3] = 166;
src[4] = 101;
src[5] = 170;
src[6] = 101;
src[7] = 166;*/
}
//** Clear destination buffer - 100 Zeros
void clrDest(uint8_t* dest_)
{
memset((void*)dest_, 0x00, DMASIZE);
}
void setup()
{
Serial.begin(9600);
waitForKeyPress();
Serial.println("Hi!");
/** Prepare source and destination **/
setSrc();
clrDest((uint8_t*)dest);
Serial.println("Buffers are prepared");Serial.flush(); //what does serial.flush do?
/** set up SPI **/
//SPISettings(5000000, MSBFIRST, SPI_MODE0);
SPISettings spiSettings;
SPI.begin();
// transmit 10 bytes and measure time to get a feel of how long that takes
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
elapsedMicros us;
for (size_t i = 0; i < DMASIZE; i++)
{
dest[i] = SPI.transfer(src[i]);
}
uint32_t t = us;
Serial.print("Time for non-DMA transfer: ");Serial.print(t);Serial.println("us");
SPI.endTransaction();
compareBuffers(src, (const uint8_t*)dest);
// ** ------------------------------------------------------------------------------- ** //
waitForKeyPress();
SPI.setClockDivider(2);
DMASPI0.begin();
DMASPI0.start();
DmaSpi::Transfer trx(nullptr, 0, nullptr);
Serial.println("Testing src -> dest, single transfer");
Serial.println("--------------------------------------------------");
trx = DmaSpi::Transfer(src, DMASIZE, dest);
clrDest((uint8_t*)dest);
DMASPI0.registerTransfer(trx);
Serial.println("Finished DMA transfer");
if (DMASPI0.stopped())
{
Serial.println("DMA SPI stopped.");
}
else
{
Serial.println("DMA SPI is still running");
}
DMASPI0.stop();
if (DMASPI0.stopped())
{
Serial.println("DMA SPI stopped.");
}
else
{
Serial.println("DMA SPI is still running");
}
DMASPI0.end();
SPI.end();
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
digitalWriteFast(LED_BUILTIN, true);
delay(500);
digitalWriteFast(LED_BUILTIN, false);
delay(500);
}
So far its working well but as can be seen in the pictures below, in the green marked areas, there is a gap between every sent byte. Left is withOUT DMA and right is WITH DMA. So its almost the same behavior except that the gap gets smaller with DMA usage.
Where does it come from and can i get rid of it?
Thanks.