I have a code that runs fine on Mega board and recently got Due and started porting it over. Removed all the direct port access commands that are not relevant to Due and the code works fine except when I try to use SPI protocol, it hangs on SPI.transfer command
Here is a sample barebone code snippet that is being used for SPI communication
#include <SPI.h>
#define MAX5424_1CS 52
SPISettings settings(3000000, MSBFIRST, SPI_MODE0);
void setup() {
// put your setup code here, to run once:
// Initiate serial communication
Serial.begin(9600);
//Setup MAX5424 CS pins
pinMode(MAX5424_1CS, OUTPUT);
digitalWrite(MAX5424_1CS, HIGH);
updateDigiPot();
}
void updateDigiPot() {
byte Out[3];
SPI.beginTransaction(settings);
//Pull CS low
digitalWrite(MAX5424_1CS, LOW);
Serial.println("Begin SPI transfer ..");
Out[0] = 0x00;
SPI.transfer(Out[0]);
Out[1] = 0x4F;
SPI.transfer(Out[1]);
//Pull CS High
digitalWrite(MAX5424_1CS, HIGH);
delay(10);
SPI.endTransaction();
Serial.println("SPI endTransaction ..");
}
void loop() {
// put your main code here, to run repeatedly:
}
Serial monitor stops after showing Begin SPI transfer ..
But SPI.transfer works fine using just SPI.begin() command, why is that I am not able to use SPI.beginTransaction with settings command as suggested in the Due SPI page?
Any suggestions where I am missing something here?
Sorry - did not pay attention to that.
The only idea for now is SPI settings.
In beginTransaction you use your own settings.
In SPI.begin MCU uses some default settings.
or maybe a bug in SPI library
Oh, forgot to say:
When you use Transactions you still need to write SPI.begin somewhere
I should have spotted that, no SPI.begin();, no wonder it does not work properly.
Just becuse of some quirk on the Mega you can ommit the required SPI.begin(); it does not mean its going to be OK on other platforms ........................
My only experience with Arduino is with many Mega boards, not familiar with other platforms, so I take it as a miracle quirk of it . Will give this a try again using SPI.begin() and report back
I do not have DUE board under hand - so, cannot try.
But here is what I guess:
beginTransaction at Mega, UNO and other traditional boards probably initiate SPI object automatically.
But DUE has very different processor.
It is possible that SPI object is not initiated properly by beginTransaction . And first call of SPI.transfer crashes everything.
Again - this is just a guess.