I'm facing a problem with a Udoo board, which has an embedded Arduino Due. I'm using SPI to get data from 16 push buttons with two shift registers. The code I'm working with works perfectly with an Arduino Uno. Here it is
#include <SPI.h>
const byte buttonLatch = 9;
const byte numOfButChips = 2; // This is the only modification needed if number of button chips used is changed
byte butArray[numOfButChips + 1];
void setup()
{
SPI.begin();
Serial.begin(115200);
pinMode(buttonLatch, OUTPUT);
digitalWrite(buttonLatch, HIGH);
}
void loop ()
{
butArray[0] = 0xc0; // denote the beginning of data stream, necessary for Pd to understand where the data starts
int index = 1;
digitalWrite (buttonLatch, LOW); // pulse the parallel load latch
digitalWrite (buttonLatch, HIGH);
for(int i = 0; i < numOfButChips; i++)
butArray[index++] = SPI.transfer(0);
Serial.write(butArray, numOfButChips + 1);
delay (10); // debounce
} // end of loop
I'm collecting the data in Pure Data as two bytes which are being split into their bits to represent the 16 push buttons.
But with the Due (actually Udoo) the bytes transferred are bit shifted by one, and wrapped up, so that the last push button of the second chip is read in the first bit of the first byte. Plus there seems to be some short circuit connecting the first and last push button of the second chip. I've tested the very same circuit without changing it at all with an Arduino Uno and there was no short circuit, so the wiring is correct.
Looking for answers I found this post Continuous SPI for multiple bytes - Arduino Due - Arduino Forum which mentions and uses the DMA SPI (I've no idea what this is), so I tried to use it. I copied the code and changed the loop function and ended up with this
// Code used to read to shift register chips, each with 8 push buttons, and transfer the data to Pure Data over serial
#define USE_ARDUINO_SPI_LIBRARY 0
#define USE_NATIVE_SAM3X_SPI 1
#define CS 10
#define SPI_RATE 21
#define SPI_BUFF_SIZE 2
uint8_t rx_buffer[SPI_BUFF_SIZE];
uint8_t tx_buffer[SPI_BUFF_SIZE];
byte butArray[3];
void setup() {
Serial.begin(115200);
pinMode(CS,OUTPUT);
digitalWrite(CS,HIGH);
spiBegin();
spiInit(SPI_RATE);
}
void loop() {
butArray[0] = 0xc0; // denote the beginning of data stream, necessary for Pd to understand where the data starts
int index = 1;
digitalWrite(CS,LOW);
digitalWrite(CS,HIGH);
spiRec(rx_buffer, SPI_BUFF_SIZE);
for(int i = 0 ; i < SPI_BUFF_SIZE; i++)
butArray[index++] = rx_buffer[i];
Serial.write(butArray, 3);
}
After this code I've included the SPI functions found here DUEZoo/dmaspi.ino at master · manitou48/DUEZoo · GitHub (taken from the above mentioned post, starting on line 37). The code compiles and uploads, but when I try to open the serial port in Pure Data (all in Udoo) it hangs and I get this message in the terminal
watchdog: signaling pd...
As I don't really understand how to use this DMA SPI, I'm sure I'm doing something (or some things) wrong here. But if there's an issue with Arduino's SPI library and Arduino Due and this DMA SPI is the solution, I'd be grateful if I had some light shed.
P.S. This forum is quite more active the Udoo's, and since it's about Arduino Due, I thought of posting here.