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

Committer:
mjm2016
Date:
Wed Dec 06 08:44:47 2017 +0000
Revision:
1:39accdbe3208
Parent:
0:65e85b7b26c1
Child:
2:06274adb4f7a
bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjm2016 0:65e85b7b26c1 1 #include <mbed.h>
mjm2016 0:65e85b7b26c1 2 /*
mjm2016 0:65e85b7b26c1 3 AUTHOR Mariwan Jalal
mjm2016 0:65e85b7b26c1 4 These projects try to make a communications channel between to NUCELO-L476RG
mjm2016 0:65e85b7b26c1 5 using SPI protocol (Slave-Master).
mjm2016 0:65e85b7b26c1 6 You can send any kind of object and reproduced between the two module
mjm2016 0:65e85b7b26c1 7 USE IT AT YOUR OWN RESPONSIBILITY .. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND
mjm2016 0:65e85b7b26c1 8
mjm2016 0:65e85b7b26c1 9 */
mjm2016 0:65e85b7b26c1 10
mjm2016 0:65e85b7b26c1 11 /*
mjm2016 0:65e85b7b26c1 12 These projects should be used together
mjm2016 0:65e85b7b26c1 13 SPI_NUCLEO_MASTER
mjm2016 0:65e85b7b26c1 14 SPI_NUCLEO_SLAVE
mjm2016 0:65e85b7b26c1 15 */
mjm2016 0:65e85b7b26c1 16
mjm2016 0:65e85b7b26c1 17 SPI device(SPI_MOSI, SPI_MISO, SPI_SCK);
mjm2016 0:65e85b7b26c1 18
mjm2016 0:65e85b7b26c1 19
mjm2016 0:65e85b7b26c1 20 typedef unsigned char byte;
mjm2016 0:65e85b7b26c1 21
mjm2016 1:39accdbe3208 22 typedef union {
mjm2016 0:65e85b7b26c1 23 float sensor;
mjm2016 0:65e85b7b26c1 24 byte SPI_Packet[sizeof(float)];
mjm2016 1:39accdbe3208 25 }SPI_Packet_t;
mjm2016 0:65e85b7b26c1 26 unsigned short tem;
mjm2016 0:65e85b7b26c1 27 int i;
mjm2016 0:65e85b7b26c1 28 unsigned int paSize;
mjm2016 0:65e85b7b26c1 29 SPI_Packet_t spipacket;
mjm2016 0:65e85b7b26c1 30
mjm2016 0:65e85b7b26c1 31 DigitalOut cs(SPI_CS);
mjm2016 0:65e85b7b26c1 32
mjm2016 0:65e85b7b26c1 33
mjm2016 0:65e85b7b26c1 34 #define SPI_DUMMY 0X0000
mjm2016 0:65e85b7b26c1 35 #define SPI_CMD 0XADAD
mjm2016 0:65e85b7b26c1 36 #define SPI_FAULT 0xDD
mjm2016 0:65e85b7b26c1 37
mjm2016 0:65e85b7b26c1 38 Serial pc(SERIAL_TX, SERIAL_RX);
mjm2016 0:65e85b7b26c1 39
mjm2016 0:65e85b7b26c1 40 short sendToSlave(unsigned short value )
mjm2016 0:65e85b7b26c1 41 {
mjm2016 0:65e85b7b26c1 42 short result=0;
mjm2016 0:65e85b7b26c1 43 cs=1;
mjm2016 0:65e85b7b26c1 44 wait_us(500);
mjm2016 0:65e85b7b26c1 45 cs=0;
mjm2016 0:65e85b7b26c1 46 device.write(value);
mjm2016 0:65e85b7b26c1 47 wait_us(500);
mjm2016 0:65e85b7b26c1 48 cs=1;
mjm2016 0:65e85b7b26c1 49 wait_ms(40);
mjm2016 0:65e85b7b26c1 50 cs=0;
mjm2016 0:65e85b7b26c1 51 wait_us(500);
mjm2016 0:65e85b7b26c1 52 result=device.write(0x0000);
mjm2016 0:65e85b7b26c1 53 wait_us(500);
mjm2016 0:65e85b7b26c1 54 cs=1;
mjm2016 0:65e85b7b26c1 55 wait_ms(40);
mjm2016 0:65e85b7b26c1 56 return result;
mjm2016 0:65e85b7b26c1 57 }
mjm2016 0:65e85b7b26c1 58
mjm2016 0:65e85b7b26c1 59 int main()
mjm2016 0:65e85b7b26c1 60 {
mjm2016 0:65e85b7b26c1 61 device.format(16,0);
mjm2016 0:65e85b7b26c1 62 device.frequency(1000000);
mjm2016 0:65e85b7b26c1 63 tem=0;
mjm2016 0:65e85b7b26c1 64 while(1) {
mjm2016 0:65e85b7b26c1 65
mjm2016 0:65e85b7b26c1 66 tem=sendToSlave(SPI_CMD); //RETURNED BYT IS DUMMY
mjm2016 0:65e85b7b26c1 67 paSize=(tem & 0xFF00)>>8;
mjm2016 0:65e85b7b26c1 68 if(paSize>0){
mjm2016 0:65e85b7b26c1 69 spipacket.SPI_Packet[0]=(byte)(tem & 0x00FF);
mjm2016 0:65e85b7b26c1 70 pc.printf("Packet=%x\n",spipacket.SPI_Packet[0]);
mjm2016 0:65e85b7b26c1 71 pc.printf("packet size=%i\n",paSize);
mjm2016 0:65e85b7b26c1 72 for (i=1; i<paSize; i++) {
mjm2016 0:65e85b7b26c1 73 cs=1;
mjm2016 0:65e85b7b26c1 74 wait_us(500);
mjm2016 0:65e85b7b26c1 75 cs=0;
mjm2016 0:65e85b7b26c1 76 tem=(unsigned short)device.write(SPI_DUMMY);
mjm2016 0:65e85b7b26c1 77 wait_us(500);
mjm2016 0:65e85b7b26c1 78 cs=1;
mjm2016 0:65e85b7b26c1 79 wait_ms(40);
mjm2016 0:65e85b7b26c1 80 if( ((tem & 0xFF00)>>8)!=SPI_FAULT){
mjm2016 0:65e85b7b26c1 81 spipacket.SPI_Packet[i]=(byte)(tem & 0x00FF);
mjm2016 0:65e85b7b26c1 82 pc.printf("Packet=%x\n",spipacket.SPI_Packet[i]);
mjm2016 0:65e85b7b26c1 83 }
mjm2016 0:65e85b7b26c1 84 else {
mjm2016 0:65e85b7b26c1 85 pc.printf("wrong packet\n");
mjm2016 0:65e85b7b26c1 86 break; //go out from for loop.
mjm2016 0:65e85b7b26c1 87 }
mjm2016 0:65e85b7b26c1 88 }
mjm2016 0:65e85b7b26c1 89 pc.printf("Temp= %04.2f\n",spipacket.sensor);
mjm2016 0:65e85b7b26c1 90 pc.printf("------------------------------\n");
mjm2016 0:65e85b7b26c1 91 }
mjm2016 0:65e85b7b26c1 92 spipacket.sensor=0.0;// Reset the variable.
mjm2016 0:65e85b7b26c1 93 wait(1);
mjm2016 0:65e85b7b26c1 94 }
mjm2016 0:65e85b7b26c1 95 }