AnsweredAssumed Answered

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

Question asked by Diego Ballen on Aug 23, 2017
Latest reply on Aug 28, 2017 by Diego Ballen

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

Outcomes