Arduino Forum

Products => Arduino Due => Topic started by: magnuspub on Aug 10, 2017, 11:21 am

Title: Arduino DUE and the code that does not work.
Post by: magnuspub on Aug 10, 2017, 11:21 am
Hello,
I wrote this code to read an eeprom ST M95160 but I do not understand why it run perfectly on Arduino NANO, and not on a Arduino DUE.

I had to write this code because Arduino DUE SPI libraries (which operate with the dedicated SPI connector) do not work with this eeprom (also using Extended SPI functions).

Does anyone know why this simple code should not work on Arduino DUE?

Code: [Select]
#define SS_PIN   10
#define MOSI_PIN 11
#define MISO_PIN 12
#define CLK_PIN  13

void setup() {
  pinMode(SS_PIN, OUTPUT);
  pinMode(MOSI_PIN, OUTPUT);
  pinMode(MISO_PIN, INPUT);
  pinMode(CLK_PIN, OUTPUT);
  
  digitalWrite(SS_PIN, HIGH);

  Serial.begin(115200);
}

void SS_HIGH(){
  delayMicroseconds(4);
  digitalWrite(SS_PIN,HIGH);
}

void CLK_HIGH(){
  digitalWrite(CLK_PIN,HIGH);
}

void MOSI_HIGH(){
  digitalWrite(MOSI_PIN, HIGH);
}

void sendByte(byte data){
  CLK_LOW();
  for (int i=7; i>=0; i--){
    CLK_LOW();
    delay(1);
    if (bitRead(data,i)){
      MOSI_HIGH();
    }
    else {
      MOSI_LOW();
    }
    CLK_HIGH();
    delay(1);
  }
  CLK_LOW();
}

byte receiveByte(){
  byte data = 0;
  SS_LOW();
  for (int i=7; i>=0; i--){
    CLK_LOW();
    delay(1);
    bitWrite(data,i,(digitalRead(MISO_PIN)));
    CLK_HIGH();
    delay(1);
  }
  SS_LOW();
  Serial.print(data,BIN); //for debug
  return data;
}

void SS_LOW(){
  digitalWrite(SS_PIN,LOW);
  delayMicroseconds(1);
}

void CLK_LOW(){
  digitalWrite(CLK_PIN,LOW);
}

void MOSI_LOW(){
  digitalWrite(MOSI_PIN, LOW);
}

void loop() {
  SS_LOW();
  sendByte(B00000011);
  sendByte(B00000000);
  sendByte(B00000001);

  for (int i=0; i<=2047; i++){
  byte data = receiveByte();
  
  }
  Serial.println(" "); //for debug
  SS_HIGH();
  delay(40);
}


To be more precise, if I connect the oscilloscope to pins 10 11 12 13, on Arduino DUE the only thing I can see is pin 13 (CLK_PIN), the others do not "move".
Title: Re: Arduino DUE and the code that does not work.
Post by: rockwallaby on Aug 10, 2017, 12:44 pm
You might need to take a closer look at the DUE I/O diagram nicely done by Rob, http://www.robgray.com/temp/Due-pinout-A4.png (http://www.robgray.com/temp/Due-pinout-A4.png)
From sticky in DUE section; https://forum.arduino.cc/index.php?topic=132130.0 (https://forum.arduino.cc/index.php?topic=132130.0)
There you will notice your mistake with the SPI configuration I am sure.
____________
Paul - VK7KPA
Title: Re: Arduino DUE and the code that does not work.
Post by: magnuspub on Aug 10, 2017, 01:22 pm
In this code I did not use the SPI libraries (SPI.h), and arbitrarily I chose pins that could fit well for other Arduino boards
Title: Re: Arduino DUE and the code that does not work.
Post by: rockwallaby on Aug 10, 2017, 01:39 pm
In your test setup, have you looked at the results if you do not connect the EEPROM ?
Have you confirmed that device will work at 3.3V okay ?

My thinking is that if you have code that will operate output pins and they don't, then start looking closer at what is connected to those outputs.
____________
Paul - VK7KPA
Title: Re: Arduino DUE and the code that does not work.
Post by: ard_newbie on Aug 10, 2017, 01:54 pm
If you don't use the SPI dedicated pins, then you might be interested by this thread, reply #1:


https://forum.arduino.cc/index.php?topic=491672.0 (https://forum.arduino.cc/index.php?topic=491672.0)

Usually when an SPI communication doesn't work, this is due to a wrong CPOL/NCPHA configuration.