SPI Master/Slave communication between two NUCLEO module. You can send objects ( float, text, ..etc) between them. These projects should be used together: SPI_NUCLEO_MASTER SPI_NUCLEO_SLAVE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 /*
00003     AUTHOR  Mariwan Jalal
00004    These projects try to make a communications channel between to NUCELO-L476RG
00005    using SPI protocol (Slave-Master). 
00006    You can send any kind of object and reproduced between the two module 
00007     USE IT AT YOUR OWN RESPONSIBILITY .. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND
00008 
00009 */
00010 
00011 /*
00012     These projects should be used together
00013     SPI_NUCLEO_MASTER
00014     SPI_NUCLEO_SLAVE
00015 */
00016 
00017 /*
00018 Slave communication is shifted by one byte. To respond for ex for a START byte,
00019 you need to put the ACK byte prepared on the bus even before having the START
00020 byte received by the slave. As the Master write the SPI bus the START Command,
00021 the prepared byte on the slave SPI bus will be transferred to the Master
00022 immediately. So, you need to prepare the Slave device to shift the bytes by
00023 one byte  as in the following example.
00024 */
00025 
00026 
00027 
00028 Serial pc(USBTX,USBRX);
00029 
00030 
00031 
00032 #define SPI_DUMMY     0X0000
00033 #define SPI_CMD       0XADAD     //Anything as a CMD code.
00034 #define SPI_FAULT     0xDD00       //Anything as a part of 16 bit as control byte
00035 
00036 #ifdef _DEBUG
00037 extern Serial pc;
00038 #define DEBUG_MESSAGE(...) pc.printf(__VA_ARGS__)
00039 #else
00040 #define DEBUG_MESSAGE(...)
00041 #endif
00042 
00043 typedef unsigned char byte;
00044 
00045 SPISlave slave(SPI_MOSI, SPI_MISO, SPI_SCK ,PA_4);
00046 int res=0;
00047 float d,c,e;
00048 
00049 /*This will be the float number we wish to transfer. 
00050 You can make a struct of multiple variable and send it in the same way*/
00051 
00052 typedef union {
00053 float sensor;
00054 byte SPI_Packet[sizeof(float)];
00055 }SPI_Packet_t;
00056 
00057 SPI_Packet_t packet;
00058 int main()
00059 {
00060     pc.printf("Testing is started from slave side\n");
00061     slave.format(16,0);
00062     slave.frequency(1000000);
00063     unsigned short vs=0;
00064     slave.reply(SPI_DUMMY);   // Prime reply
00065     unsigned short tempVal=0;
00066     packet.sensor=1000.02;    //Value of the float number we wish to communicate
00067     while(1) {
00068         if(slave.receive()) {
00069             tempVal=(unsigned short) slave.read();        //Real command
00070             printf("----------------\n%x\n",tempVal);
00071             /*
00072                 The switch is not necessary if you have only one command between
00073                  master-slave. This is useful if you wish to send different 
00074                  object depending on the command.
00075             */
00076             switch(tempVal){
00077             case SPI_CMD: {    /*Use this if you have different commands 
00078                                 for sending different objects to the master.*/
00079                             tempVal=(sizeof(float))<<8| packet.SPI_Packet[0];
00080                             pc.printf("Packet=%x\n",tempVal);
00081                             slave.reply(tempVal);
00082                             for(unsigned int i=1; i<sizeof(float); i++){
00083                                 while(!(slave.receive()))
00084                                     wait_ms(1);
00085                             vs=slave.read();        //Real command
00086                             if (vs==SPI_DUMMY){
00087                                 //Continue sending bytes
00088                                 {
00089                                   tempVal=0x00FF & packet.SPI_Packet[i];
00090                                   pc.printf("Packet=%x\n",tempVal);
00091                                   slave.reply(tempVal);
00092                                 }
00093                             }
00094                             else {
00095                                 slave.reply(SPI_FAULT); //ERROR CODE
00096                                 break;
00097                             }
00098                         }
00099                     }
00100 
00101                     break;
00102             }
00103 
00104          }
00105 
00106     }
00107 }