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 SPI device(SPI_MOSI, SPI_MISO, SPI_SCK);
00018 
00019 
00020 typedef unsigned char byte;
00021 
00022 typedef union {
00023     float sensor;
00024     byte SPI_Packet[sizeof(float)];
00025 }SPI_Packet_t;
00026 unsigned short tem;
00027 int i;
00028 unsigned int paSize;
00029 SPI_Packet_t spipacket;
00030 
00031 DigitalOut cs(SPI_CS);
00032 
00033 
00034 #define SPI_DUMMY     0X0000
00035 #define SPI_CMD       0XADAD
00036 #define SPI_FAULT     0xDD
00037 
00038 Serial pc(SERIAL_TX, SERIAL_RX);
00039 
00040 short sendToSlave(unsigned short value )
00041 {
00042     short result=0;
00043     cs=1;
00044     wait_us(500);
00045     cs=0;
00046     device.write(value);
00047     wait_us(500);
00048     cs=1;
00049     wait_ms(40);
00050     cs=0;
00051     wait_us(500);
00052     result=device.write(0x0000);
00053     wait_us(500);
00054     cs=1;
00055     wait_ms(40);
00056     return result;
00057 }
00058 
00059 int main()
00060 {
00061     device.format(16,0);
00062     device.frequency(1000000);
00063     tem=0;
00064     while(1) {
00065         
00066         tem=sendToSlave(SPI_CMD);       //RETURNED BYT IS DUMMY
00067         paSize=(tem & 0xFF00)>>8;
00068         if(paSize>0){
00069             spipacket.SPI_Packet[0]=(byte)(tem & 0x00FF);
00070             pc.printf("Packet=%x\n",spipacket.SPI_Packet[0]);
00071             pc.printf("packet size=%i\n",paSize);
00072             for (i=1; i<paSize; i++) {
00073                 cs=1;
00074                 wait_us(500);
00075                 cs=0;
00076                 tem=(unsigned short)device.write(SPI_DUMMY);
00077                 wait_us(500);
00078                 cs=1;
00079                 wait_ms(40);
00080                 if( ((tem & 0xFF00)>>8)!=SPI_FAULT){
00081                     spipacket.SPI_Packet[i]=(byte)(tem & 0x00FF);
00082                     pc.printf("Packet=%x\n",spipacket.SPI_Packet[i]);
00083                 }
00084                     else {
00085                         pc.printf("wrong packet\n");
00086                         break;    //go out from for loop.
00087                           }
00088             }
00089         pc.printf("Temp= %04.2f\n",spipacket.sensor);
00090         pc.printf("------------------------------\n");
00091      }
00092     spipacket.sensor=0.0;// Reset the variable.
00093     wait(1);
00094     }
00095 }