0

i want to connect SPI 2 arduino master and slave through the MUX , to write and read in external memory , when the master write in memory slave read . pls help . code to master

#include <SPI.h>
//-------FM------------------
#define WENB 0x06
#define RDSR 0x05
#define WRSR 0x01
#define PPROGRAM 0x02
#define SERASE 0x20
#define BERASE 0xd8
#define READ 0x03
#define RDSCUR 0x2B
#define CERASE 0x60//or 0xC7

//------------Specify the address-----------
int address = 0x00ff0000;
byte x, Y;
int IN = 5;
int SS1 = 9;
/*char data[]=("don't be afraid ");
char recived[15];*/
//--------------------------
void setup() {
    //------------------------------------------
    // pinMode(EN,OUTPUT);
    pinMode ( SS, OUTPUT ); //ss pin 10
    pinMode ( SS1, OUTPUT );
    digitalWrite ( SS1, HIGH );
    pinMode ( IN, OUTPUT );
    digitalWrite ( IN, LOW ); // MUX connect A
    //digitalWrite(CS,low);
    Serial.begin ( 9600 );
    SPI.begin();//start the SPI connection with the flash memory
    SPI.setClockDivider ( SPI_CLOCK_DIV8 );
    SPI.setBitOrder ( MSBFIRST );
    digitalWrite ( SS, HIGH );
    delay ( 10 );
    //-------erase the sector--//-----------
    digitalWrite ( SS, LOW );
    SPI.transfer ( WENB );
    digitalWrite ( SS, HIGH );
    //-------------------------------------
    digitalWrite ( SS, LOW );
    SPI.transfer ( SERASE );
    SPI.transfer ( ( address >> 32 ) & 0xff );
    SPI.transfer ( ( address >> 16 ) & 0xff );
    SPI.transfer ( ( address >> 8 ) & 0xff );
    SPI.transfer ( address & 0xff );
    digitalWrite ( SS, HIGH );
    delay ( 5000 );
    //------------------------------------------
    //Serial.println("okay fine")
    //-----Program Data Into Memory Array-----.
    //-----------interrupts the slave----------
    /* digitalWrite(SS1,LOW);
     SPI.transfer(0xff);//are you there?
     digitalWrite(SS1,HIGH);
     delay(1000);

     digitalWrite(IN,HIGH);*/
}

void loop()

{
    char c;
    int i = 0;
    char data[] = "Hello!:";
    char recived[7];
    byte x;
    //----------Enable the write in the memory-----.
    digitalWrite ( SS, LOW );
    SPI.transfer ( WENB );
    digitalWrite ( SS, HIGH );
    delay ( 20 );
    //-----Program Data Into Memory Array-----.
    digitalWrite ( SS, LOW );
    SPI.transfer ( PPROGRAM );
    SPI.transfer ( ( address >> 32 ) & 0xff );
    SPI.transfer ( ( address >> 16 ) & 0xff );
    SPI.transfer ( ( address >> 8 ) & 0xff );
    SPI.transfer ( address & 0xff );

    for ( int i = 0; i < sizeof ( data ); i++ ) {
        SPI.transfer ( data[i] );
        // Serial.print(data[i]);
    }

    // disable Slave Select
    digitalWrite ( SS, HIGH );
    //--------------------------------------------
    delay ( 1000 );
    //--------------------------- ----------
    i = 0;
    Serial.print ( "It's okay saved: " );

    while ( data[i] != NULL ) {
        Serial.println ( data[i] );
        i++;
    }

    // delay(100);
    //-----------interrupts the slave----------
    digitalWrite ( SS1, LOW );
    SPI.transfer ( 0xff ); //are you there?
    digitalWrite ( SS1, HIGH );
    delay ( 1000 );
    digitalWrite ( IN, HIGH ); // MUX connect B
}

code to slave

#include <SPI.h>
//-------FM------------------
#define WENB 0x06
#define RDSR 0x05
#define WRSR 0x01
#define PPROGRAM 0x02
#define SERASE 0x20
#define BERASE 0xd8
#define READ 0x03
#define RDSCUR 0x2B
#define CERASE 0x60//or 0xC7

//------------Specify the address-------------
int address = 0x00ff0000;

int SS1 = 9;
char data[] = "Hello!:";
char buff[7];

byte slave;
volatile bool process;
//----------------config slave----------------
void setup() {
    //pinMode(IN,OUTPUT);
    pinMode ( SS, OUTPUT ); //SS pin 10
    pinMode ( MISO, OUTPUT );
    pinMode ( SS1, INPUT );
    pinMode ( MOSI, INPUT );
    //--------------------------------------------------
    Serial.begin ( 9600 );
    ///SPCR |= bit (SPE);
    SPI.begin();//start the SPI connection with the flash memory
    SPI.setClockDivider ( SPI_CLOCK_DIV8 );
    SPI.setBitOrder ( MSBFIRST );
    // digitalWrite(SS,HIGH);
    //-------------master interrupts the slave----------------------------------------------
    // now turn on interrupts
    // SPCR |=bit(SPIE);
    //SPCR |=bit(SPE); //set SPE bit at the SPCR(spi control register) to enable SPI
    SPI.attachInterrupt();
}

ISR ( SPI_STC_vect ) {
    slave = SPDR;

    if ( slave = 0xff ) {
        pinMode ( MISO, INPUT );
        // pinMode(SS1,INPUT);
        pinMode ( MOSI, OUTPUT );
        //digitalWrite(IN,HIGH);
        digitalWrite ( SS, LOW );
        SPI.transfer ( READ );
        SPI.transfer ( ( address >> 32 ) & 0xff );
        SPI.transfer ( ( address >> 16 ) & 0xff );
        SPI.transfer ( ( address >> 8 ) & 0xff );
        SPI.transfer ( address & 0xff );

        for ( int n = 0; n < sizeof ( data ); n++ ) {
            data[n] = SPI.transfer ( 0 );
            Serial.print ( data[n] );
        }

        digitalWrite ( SS, HIGH );
    }
}
  • You mean you want one Arduino to be able to access the memory of another over SPI? If so, what part are you having problems with? You have told us what you want to do, and you have dumped code on us, but you haven't told us what is actually happening (or not happening) and what you need help with. – Majenko Mar 15 at 10:36
  • I'm afraid you have to clarify your question. Do you mean you want to connect two Arduinos to on external memory? And one Arduino drives a MUX to decide which Arduino is allowed to access the ext. memory? Which MUX do you use? How do you plan to negotiate which Arduino can have access to the memory? Wouldn't it be a better idea to let one Arduino access the memory and the other sends its data to that "master" Arduino? – Peter Paul Kiefer Mar 15 at 10:42
  • what my want to do! okay , i have 2 arduino , i program one to be master to control in my project and write in external memory , and anther to be slave and read from external memory , through MUX to which who can connect with memory , and i control in MUX by the master. my problem the slave can't know when exactly read from memory . I used MUX ADG774 and the type of memory MX25L25735E. you can see my code . I hope now be clear – HAask Mar 15 at 11:18
  • Then you need to have some form of communication between the two Arduinos so that the master can tell the slave when it's allowed to read from the external memory. And in that case you may as well just forget sharing the memory and just use that connection to send data from one Arduino to the other. – Majenko Mar 15 at 12:39
  • The master and slave connected by interrupt command . what do you think ? – HAask Mar 15 at 18:17
0

Having a line from Arduino 1 to Arduino 2 to indicate that Arduino 2 should do something is sufficient.

Simple way: a line that is an input on both sides with internal pullups turned on. Both sides monitor it.

When Arduino 1 wants to write, it checks to make sure the line is high, then it makes its pin an output and pulls it low, and when done releases it by going back to an input (using pinMode() command).

When Arduino 2 see the low, it waits for it to go high, then makes its input and output and pulls it low until it is done reading.

Arduino 1 sees the low and holds off on any writing until it goes back high again.

This is known as arbitration, and is an example of one simple method.

| improve this answer | |
  • okay- when or how can arduino 2 know arduino 1 finished write ? – HAask Mar 18 at 6:19
  • Arduino 1 puts the common pin back to an input, which lets it go High. – CrossRoads Mar 18 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.