2020-04-16 10:10 PM
This is my first post on this QnA section and i hope i would be getting help from the community.
What i want to implement is a simple communication between 2 STM32 (both are F1 series in this case) using a SPI.
Both the MCU are configured using CubeMX where one of the F1 is set as SPI master using SPI2 and other one set as SPI Slave using SPI2.
Both the MCU are using same pins, PB13,14 and 15 for (SCK, MISO and MOSI respectively) and using PA8 for Slave Select.
There are 2 modes of operation:
Mode 1: Master continuously sends data to slave and slave is receiving without any problem.
Mode 2: this is where Slave has to send data to master. Now i have tried many things but never really got it to work.
Following are the approaches i have tried.
I read somewhere that for slave to send data to master, first master has to send a dummy byte or data to slave and then slave can send data back to master.
Also i read in this community that we have to use
HAL_SPI_TransmitReceive
from master side to receive data from salve. And we have to set size as max (send byte + expected byte from slave) But this also does not work either.
I have tried like so many different approaches but nothing is working as expected. I am literally pulling my hairs off this :(
It would be very kind of you guys to please help me with this.
Here is the config for the SPI in master mode.
And this is config of SPI in slave mode.
I am using HAL codes to implement. Latest is the following codes that i tried last. I have set the SS pin to ground manually.
Master side:
counter = (counter > 4) ? 0 : counter;
HAL_SPI_Transmit(&hspi2, (uint8_t *)&UID[counter], 1, HAL_MAX_DELAY);
HAL_Delay(1000);
HAL_SPI_Receive(&hspi2, (uint8_t *)&txBuf[counter], 1, HAL_MAX_DELAY);
HAL_Delay(1000);
counter++;
Slave Side:
counter = (counter > 4) ? 0 : counter;
HAL_SPI_Receive( &hspi2, (uint8_t *)&rxBuffer[counter], 1, HAL_MAX_DELAY);
HAL_Delay(1000);
HAL_SPI_Transmit( &hspi2, (uint8_t *)&uid[counter], 1, HAL_MAX_DELAY);
HAL_Delay(1000);
counter++;
Its pretty simple, where master is sending 5 bytes (one by one)
and Slave is sending 5 bytes (one by one) but its transmitting after it has received successfully.