cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with SPI communication between the two SPI modules of a board STM32L053R8

Diego Ballen
Associate II
Posted on August 23, 2017 at 14:42

Hello everyone,

I am trying to test the communication between the two SPI modules of my board. The master (SPI1) must sent an command to the slave (SPI2). If the command is 0x31 the slave must send back 15 Bytes of data.

My code for the server is:

   uint8_t dataTxMaster[15]={3,3,3,3,3,3,3,3,3,3,3,3,3,3,3};

   uint8_t dataRxMaster[15]={0};

   uint8_t dataTxSlave[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

   uint8_t dataRxSlave[1]={0};

  HAL_SPI_Receive_IT(&hspi2, (uint8_t*) dataRx, 1);//Register event for the first RX

 

  uint8_t commandMaster = 0x31;

  while (1)

  {

      SpiEn; //Macro to enable slave

      HAL_SPI_Transmit(&hspi1, (uint8_t*) &commandMaster, 1,2000); //Send command

      HAL_SPI_TransmitReceive(&hspi1, (uint8_t*) dataTxMaster, (uint8_t*) dataRxMaster, 15,2000); //Send dummy       data

      while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

      SpiDis; //Macro to disable slave

  }

and for the slave I used a call back function after a RX interruption :

void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)

{

    if(hspi->Instance == hspi2.Instance)

    {

        if(dataRxSlave[0] == 0x31)

        {

            //Send data to master

 

            HAL_SPI_Transmit(&hspi2, (uint8_t*) dataTxSlave, 15, 2000);

            while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY);

        }

            //Register event for next RX

 

            HAL_SPI_Receive_IT(&hspi2, (uint8_t*) dataRxSlave, 1);

    }

}

I found that the code crashes when I try to send  data from the slave in the call back function, more exactly because of the line:

HAL_SPI_Transmit(&hspi2, (uint8_t*) dataTxSlave, 15, 2000);

Esentially what I am trying to do is to send the data to the master through an interrupt.

Does anyone know what the problem is? Any help is really appreciated!

Best regards,

Diego

2 REPLIES 2
Posted on August 24, 2017 at 16:43

Hi Diego,

Well, I'm not completely sure about it but I have worked this days with the slave spi mode and I think that the problem is the following:

After you send the spi frame through the spi master:

HAL_SPI_Transmit(&hspi1, (uint8_t*) &commandMaster, 1,2000); //Send command

Suppose that immediately the slave IT Rx callback is called (I said suppose because in fact the IT could be called after the execution of a few instructions of the following function HAL_SPI_TransmitReceive(&hspi1,...)

Then you enter to the callback, check that the data is right and then you call the Tx function of the slave in polling mode with a timeout of 2000ms HAL_SPI_Transmit(&hspi2, (uint8_t*) dataTxSlave, 15, 2000);, the slave will try to send the data but it won't be able because it needs the clock signal from the master, and as the function wasn't be called (HAL_SPI_TransmitReceive(&hspi1,... ) the slave just waits the 2 seconds timeout and finish the function.

How to fix that... it could be a solution:

Put a short delay after HAL_SPI_Transmit(&hspi1, (uint8_t*) &commandMaster, 1,2000); //Send command, the Rx callback be called in the delay so the function HAL_SPI_TransmitReceive(&hspi1,... will be called right, after that, change the spi polling transmit in the callback for a slave interrup transmit, in interrupt mode I suppose (not completely sure) that it set the transmition and waits until the clock signal from the master starts to send the data, as is in interrupt mode it won't wait a time out, the program will exit from the callback function and the following instruction to be executed will be the 

 

HAL_SPI_TransmitReceive(&hspi1,..., as the slave is waiting the clock signal and the master generate it with the function should be works.

Also delete the waits while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY); and better check the return value of the transmit and receive functions, you can see if the function returns an OK or a Timeout error.

Hope that helps to you...

Diego Ballen
Associate II
Posted on August 28, 2017 at 17:58

Hi Elkin,

thank you for your response.

as you mention, the HAL_SPI_Transmit(&hspi1, (uint8_t*) &commandMaster, 1,2000) is locking the program for 2 seconds. During this time there is no clock and the slave cannot send any data. 

I decided to test using two boards and I got no problems at all.

Greetings,

Diego