Two Nucleo boards (NUCLEO-F746ZG to NUCLEO-F746ZG) to communicate with each other using SPI

Hello,
I’m trying to make two boards (NUCLEO-F746ZG and NUCLEO-F103RB) to communicate with each other using SPI. However, my slave code does not seem to work on neither of the boards.
In my board master to slave connection as per below

MASTER ( Nulceo F746ZG)
SCK -PE_2
MISO-PE_5
MOSI-PE_6
CS-PE_4

SLAVE( Nulceo F746ZG)
SCK -PE_2
MISO-PE_5
MOSI-PE_6
CS-PE_4

IN Slave Nucleo its Showing Errorbelow
pinmap not found for peripheral

Here i have Attach My Master and Slave Code
This one is Master Code Below

// master Nucleo(F746ZG)
#include "mbed.h"
#include"string.h"

Serial pc(USBTX, USBRX);                    // tx, rx (serial Communication Tera Term)
SPI Master_To_Slave(PE_6,PE_5,PE_2); // mosi, miso, sclk
DigitalOut Slave1_Selection(PE_4);               //master NUCLEO-F746ZG slave selection pin
DigitalOut Master_Led(PG_3);
char Send_String[20] = "KALAIVANAN#";
char Received_String[30]="\0";

int main()
{
    pc.baud(9600);
    int i=0;
    int j=0;
    int ch;

    while (1) 
    {
        pc.printf("\n Select The case :\n"); 
        pc.scanf("%d",&ch); 
        switch(ch)
        {
            case 1:
            {
                pc.printf("nucleo to nucleo SPI Communication\n");
                Slave1_Selection = 1; // De_Select the 1st Slave
                Master_To_Slave.format(8,3);        // Setup: 8bit mode 3
                Master_To_Slave.frequency(1000000); // Starting Frequency 1 MHZ
                while(Send_String[i] != '\0' )  // Send the String Upto Null
                    {
                        Slave1_Selection = 0; // Select the 1st Slave (Activate)
                        Received_String[j++] =  Master_To_Slave.write(Send_String[i++]); //Data Send to Master and Data Receiver From Master
                        Master_Led=!Master_Led;//LEd Blinking While Data Transfer
                        Slave1_Selection = 1; // De_Select the 1st Slave
                        wait_ms(200); // Wait for readability only(Ready to wait for Next Data)
                    }
                pc.printf("Received String=%s",Received_String);    
                NVIC_SystemReset();//system reset
            }
            break;
        }
    }
}

  paste code here

This one is Slave Code Below

// Slave Nucleo (F746ZG)
#include "mbed.h"
#include"string.h"
#include"SPISlave.h"

Serial pc(USBTX, USBRX);    // tx, rx
SPISlave Master_To_Slave(PE_6,PE_5,PE_2,PE_2); //mosi, miso, sclk,SSEL - (Slave Select its activate by master in this pin low mean this Slave Selected )
DigitalOut Slave_Led(PG_3);
char Send_String[25] = "DONE#";
char Received_String[20]="\0";

int main()
{
    pc.baud(9600);
    Master_To_Slave.format(8,3);                 // Setup: 8bit mode 3
    Master_To_Slave.frequency(1000000);          // Starting Frequency 1MHZ
    pc.printf("\n Ready");
    int Responce =2024;
    int i=0;
    int j=0;
    Master_To_Slave.reply(++Responce);              // SPI with 1st reply
    Master_To_Slave.reply(++Responce);              // SPI with 2nd reply its just Responces
    printf(" \n Replay Last Responce and before while(1)=%d",Responce);
    while(1)
    {
        if(Master_To_Slave.receive());  // start Reading from Master
        {
            Received_String[i++] = Master_To_Slave.read();   // Read Data from master and Stored to Received String
            pc.printf("\n Slave1 Received String %s",Received_String); //print the Receiving Char
            Slave_Led = !Slave_Led;// LEd Blinking While Data Transfer
            Master_To_Slave.reply(Send_String[j++]); //Data Send to Master (Done Message)
            pc.printf("\n Sending String %s",Send_String[j++]); //print the sending Char
            wait_ms(200);//waiting 
        }         
    }
    wait_ms(10);
    NVIC_SystemReset(); //System Reset
}  

Hello,

In the slave code you have typo in the constructor, two times same pin

SPISlave Master_To_Slave(PE_6,PE_5,PE_2,PE_2); 

BR, Jan

thanks johnny
now i removed JP6 of Both nucleo ,
in master side only Led Blinking ,Slave side not received any Data from master.
can you give your valuable Suggestions.

The JP6 is for cut out conection between SPI6 and Etherneth PHY. But you use SPI4 so what effect this have?

I will look to the code later.

BR, Jan

ok thanks johnny.

In the master code does not make sense to set the part below in every iteration of the loop and should be before loop.

Slave1_Selection = 1; // De_Select the 1st Slave
Master_To_Slave.format(8,3);        // Setup: 8bit mode 3
Master_To_Slave.frequency(1000000); // Starting Frequency 1 MHz

Also 200ms wait between every byte is not what you want in SPI communication.

In the Slave code it is similar about the wait and especially delay between Read and Reply should be minimal. So try to place both printfs after both functions for minimal delay.
You also need modified the end

 pc.printf("Received String=%s\n",Received_String);    // add \n
 wait(2); // add w8 for print 
 NVIC_SystemReset();//system reset

BB, Jan

hi johnny i tried this one with all baud rate ,after i removed Replay
here Attached my New code with output also can you please check

New Master Code


//Master Nucleo(F746ZG)
#include"mbed.h"
#include"string.h"
#include"SPI.h"

Serial pc(USBTX, USBRX);                    // tx, rx (serial Communication Tera Term)
SPI Master_To_Slave(PE_6,PE_5,PE_2); // mosi, miso, sclk
DigitalOut Slave1_Selection(PE_4);               //master NUCLEO-F746ZG slave selection pin
DigitalOut Master_Led(PG_3);
char Send_String[20] = "KALAIVANAN#";

int main()
{
    pc.baud(115200);
    int i=0;
    int j=0;
    int ch;
    Slave1_Selection = 1; // De_Select the 1st Slave
    Master_To_Slave.format(8,3);        // Setup: 8bit mode 3
    Master_To_Slave.frequency(1000000); // Starting Frequency 1 MHZ

    while(1) 
    {
        pc.printf("\n Select The case :\n"); 
        pc.scanf("%d",&ch); 
        switch(ch)
        {
            case 1:
            {
                pc.printf("nucleo to nucleo SPI Communication\n");
 
                while(Send_String[i] != '#' )  // Send the String Upto Null
                    {
                        Slave1_Selection = 0; // Select the 1st Slave (Activate)
                        pc.printf(" \n Send_String = %s ",Send_String[i]);
                        Master_To_Slave.write(Send_String[i]);
                        Master_Led=!Master_Led;//LEd Blinking While Data Transfer
                        Slave1_Selection = 1; // De_Select the 1st Slave
                        wait_ms(200); // Wait for readability only(Ready to wait for Next Data)
                        i++;
                    }
            }
            break;
        }
    }
}

 

New Slave Code

// Sub Nucleo
#include"mbed.h"
#include"string.h"
#include"SPISlave.h"

Serial pc(USBTX, USBRX);    // tx, rx
SPISlave Master_To_Slave(PE_6,PE_5,PE_2,PE_4); //mosi, miso, sclk,SSEL - (Slave Select its activate by master in this pin low mean this Slave Selected )
DigitalOut Slave_Led(PG_3);
char Received_String[20]="\0";

int main()
{
    pc.baud(115200);
    Master_To_Slave.format(8,3);                 // Setup: 8bit mode 3
    Master_To_Slave.frequency(1000000);          // Starting Frequency 1MHZ
    pc.printf("\n Ready");
    int i=0;

    while(1)
    {
        pc.printf(" Inside of while Loop waiting for Data from Master");
        if(Master_To_Slave.receive());  // start Reading from Master
        {  
            Received_String[i] = Master_To_Slave.read();
            pc.printf("\n Slave1 Received String %s",Received_String[i]); //print the Receiving Char
            Slave_Led = !Slave_Led;// LEd Blinking While Data Transfer
            wait_ms(200);//waiting 
            i++;
        }         
    }
    wait_ms(10);
}  

master Output of Tera term

![image|690x123](upload://qtSvwbeuuV3KGtBAOo1yfxEyOzV.png)

slave Output of Tera term

![Screenshot 2024-03-15 124023 (1)|690x366](upload://m6LGv66XHv1VEsRXKOfvn12ZqoL.png)


can you give your valuable Suggestions.

One issue I can spot is that in the following lines:

pc.printf(" \n Send_String = %s ",Send_String[i]);

pc.printf("\n Slave1 Received String %s",Received_String[i]); //print the Receiving Char

“%s” should be “%c” instead. %s is for when you pass a pointer to a zero terminated string in memory. Using %s instead of %c here means you’re printing random memory to the console.

very thanks jamie smith.

thanks now its working ,how i will Add the reply also in my above code ,
i want reply from slave after one character received ,