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
main.cpp@1:d154278f2ca1, 2017-12-06 (annotated)
- Committer:
- mjm2016
- Date:
- Wed Dec 06 08:46:12 2017 +0000
- Revision:
- 1:d154278f2ca1
- Parent:
- 0:52f83e5addd7
bug fixed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mjm2016 | 0:52f83e5addd7 | 1 | #include "mbed.h" |
mjm2016 | 0:52f83e5addd7 | 2 | /* |
mjm2016 | 0:52f83e5addd7 | 3 | AUTHOR Mariwan Jalal |
mjm2016 | 0:52f83e5addd7 | 4 | These projects try to make a communications channel between to NUCELO-L476RG |
mjm2016 | 0:52f83e5addd7 | 5 | using SPI protocol (Slave-Master). |
mjm2016 | 0:52f83e5addd7 | 6 | You can send any kind of object and reproduced between the two module |
mjm2016 | 0:52f83e5addd7 | 7 | USE IT AT YOUR OWN RESPONSIBILITY .. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND |
mjm2016 | 0:52f83e5addd7 | 8 | |
mjm2016 | 0:52f83e5addd7 | 9 | */ |
mjm2016 | 0:52f83e5addd7 | 10 | |
mjm2016 | 0:52f83e5addd7 | 11 | /* |
mjm2016 | 0:52f83e5addd7 | 12 | These projects should be used together |
mjm2016 | 0:52f83e5addd7 | 13 | SPI_NUCLEO_MASTER |
mjm2016 | 0:52f83e5addd7 | 14 | SPI_NUCLEO_SLAVE |
mjm2016 | 0:52f83e5addd7 | 15 | */ |
mjm2016 | 0:52f83e5addd7 | 16 | |
mjm2016 | 0:52f83e5addd7 | 17 | /* |
mjm2016 | 0:52f83e5addd7 | 18 | Slave communication is shifted by one byte. To respond for ex for a START byte, |
mjm2016 | 0:52f83e5addd7 | 19 | you need to put the ACK byte prepared on the bus even before having the START |
mjm2016 | 0:52f83e5addd7 | 20 | byte received by the slave. As the Master write the SPI bus the START Command, |
mjm2016 | 0:52f83e5addd7 | 21 | the prepared byte on the slave SPI bus will be transferred to the Master |
mjm2016 | 0:52f83e5addd7 | 22 | immediately. So, you need to prepare the Slave device to shift the bytes by |
mjm2016 | 0:52f83e5addd7 | 23 | one byte as in the following example. |
mjm2016 | 0:52f83e5addd7 | 24 | */ |
mjm2016 | 0:52f83e5addd7 | 25 | |
mjm2016 | 0:52f83e5addd7 | 26 | |
mjm2016 | 0:52f83e5addd7 | 27 | |
mjm2016 | 0:52f83e5addd7 | 28 | Serial pc(USBTX,USBRX); |
mjm2016 | 0:52f83e5addd7 | 29 | |
mjm2016 | 0:52f83e5addd7 | 30 | |
mjm2016 | 0:52f83e5addd7 | 31 | |
mjm2016 | 1:d154278f2ca1 | 32 | #define SPI_DUMMY 0X0000 |
mjm2016 | 0:52f83e5addd7 | 33 | #define SPI_CMD 0XADAD //Anything as a CMD code. |
mjm2016 | 1:d154278f2ca1 | 34 | #define SPI_FAULT 0xDD00 //Anything as a part of 16 bit as control byte |
mjm2016 | 0:52f83e5addd7 | 35 | |
mjm2016 | 0:52f83e5addd7 | 36 | #ifdef _DEBUG |
mjm2016 | 0:52f83e5addd7 | 37 | extern Serial pc; |
mjm2016 | 0:52f83e5addd7 | 38 | #define DEBUG_MESSAGE(...) pc.printf(__VA_ARGS__) |
mjm2016 | 0:52f83e5addd7 | 39 | #else |
mjm2016 | 0:52f83e5addd7 | 40 | #define DEBUG_MESSAGE(...) |
mjm2016 | 0:52f83e5addd7 | 41 | #endif |
mjm2016 | 0:52f83e5addd7 | 42 | |
mjm2016 | 0:52f83e5addd7 | 43 | typedef unsigned char byte; |
mjm2016 | 0:52f83e5addd7 | 44 | |
mjm2016 | 0:52f83e5addd7 | 45 | SPISlave slave(SPI_MOSI, SPI_MISO, SPI_SCK ,PA_4); |
mjm2016 | 0:52f83e5addd7 | 46 | int res=0; |
mjm2016 | 0:52f83e5addd7 | 47 | float d,c,e; |
mjm2016 | 0:52f83e5addd7 | 48 | |
mjm2016 | 0:52f83e5addd7 | 49 | /*This will be the float number we wish to transfer. |
mjm2016 | 0:52f83e5addd7 | 50 | You can make a struct of multiple variable and send it in the same way*/ |
mjm2016 | 0:52f83e5addd7 | 51 | |
mjm2016 | 1:d154278f2ca1 | 52 | typedef union { |
mjm2016 | 0:52f83e5addd7 | 53 | float sensor; |
mjm2016 | 0:52f83e5addd7 | 54 | byte SPI_Packet[sizeof(float)]; |
mjm2016 | 1:d154278f2ca1 | 55 | }SPI_Packet_t; |
mjm2016 | 0:52f83e5addd7 | 56 | |
mjm2016 | 0:52f83e5addd7 | 57 | SPI_Packet_t packet; |
mjm2016 | 0:52f83e5addd7 | 58 | int main() |
mjm2016 | 0:52f83e5addd7 | 59 | { |
mjm2016 | 0:52f83e5addd7 | 60 | pc.printf("Testing is started from slave side\n"); |
mjm2016 | 0:52f83e5addd7 | 61 | slave.format(16,0); |
mjm2016 | 0:52f83e5addd7 | 62 | slave.frequency(1000000); |
mjm2016 | 0:52f83e5addd7 | 63 | unsigned short vs=0; |
mjm2016 | 0:52f83e5addd7 | 64 | slave.reply(SPI_DUMMY); // Prime reply |
mjm2016 | 0:52f83e5addd7 | 65 | unsigned short tempVal=0; |
mjm2016 | 0:52f83e5addd7 | 66 | packet.sensor=1000.02; //Value of the float number we wish to communicate |
mjm2016 | 0:52f83e5addd7 | 67 | while(1) { |
mjm2016 | 0:52f83e5addd7 | 68 | if(slave.receive()) { |
mjm2016 | 0:52f83e5addd7 | 69 | tempVal=(unsigned short) slave.read(); //Real command |
mjm2016 | 0:52f83e5addd7 | 70 | printf("----------------\n%x\n",tempVal); |
mjm2016 | 0:52f83e5addd7 | 71 | /* |
mjm2016 | 0:52f83e5addd7 | 72 | The switch is not necessary if you have only one command between |
mjm2016 | 0:52f83e5addd7 | 73 | master-slave. This is useful if you wish to send different |
mjm2016 | 0:52f83e5addd7 | 74 | object depending on the command. |
mjm2016 | 0:52f83e5addd7 | 75 | */ |
mjm2016 | 0:52f83e5addd7 | 76 | switch(tempVal){ |
mjm2016 | 0:52f83e5addd7 | 77 | case SPI_CMD: { /*Use this if you have different commands |
mjm2016 | 0:52f83e5addd7 | 78 | for sending different objects to the master.*/ |
mjm2016 | 0:52f83e5addd7 | 79 | tempVal=(sizeof(float))<<8| packet.SPI_Packet[0]; |
mjm2016 | 0:52f83e5addd7 | 80 | pc.printf("Packet=%x\n",tempVal); |
mjm2016 | 0:52f83e5addd7 | 81 | slave.reply(tempVal); |
mjm2016 | 0:52f83e5addd7 | 82 | for(unsigned int i=1; i<sizeof(float); i++){ |
mjm2016 | 0:52f83e5addd7 | 83 | while(!(slave.receive())) |
mjm2016 | 0:52f83e5addd7 | 84 | wait_ms(1); |
mjm2016 | 0:52f83e5addd7 | 85 | vs=slave.read(); //Real command |
mjm2016 | 0:52f83e5addd7 | 86 | if (vs==SPI_DUMMY){ |
mjm2016 | 0:52f83e5addd7 | 87 | //Continue sending bytes |
mjm2016 | 0:52f83e5addd7 | 88 | { |
mjm2016 | 0:52f83e5addd7 | 89 | tempVal=0x00FF & packet.SPI_Packet[i]; |
mjm2016 | 0:52f83e5addd7 | 90 | pc.printf("Packet=%x\n",tempVal); |
mjm2016 | 0:52f83e5addd7 | 91 | slave.reply(tempVal); |
mjm2016 | 0:52f83e5addd7 | 92 | } |
mjm2016 | 0:52f83e5addd7 | 93 | } |
mjm2016 | 0:52f83e5addd7 | 94 | else { |
mjm2016 | 0:52f83e5addd7 | 95 | slave.reply(SPI_FAULT); //ERROR CODE |
mjm2016 | 1:d154278f2ca1 | 96 | break; |
mjm2016 | 0:52f83e5addd7 | 97 | } |
mjm2016 | 0:52f83e5addd7 | 98 | } |
mjm2016 | 0:52f83e5addd7 | 99 | } |
mjm2016 | 0:52f83e5addd7 | 100 | |
mjm2016 | 0:52f83e5addd7 | 101 | break; |
mjm2016 | 0:52f83e5addd7 | 102 | } |
mjm2016 | 0:52f83e5addd7 | 103 | |
mjm2016 | 0:52f83e5addd7 | 104 | } |
mjm2016 | 0:52f83e5addd7 | 105 | |
mjm2016 | 0:52f83e5addd7 | 106 | } |
mjm2016 | 0:52f83e5addd7 | 107 | } |