Skip to main content
Visitor II
August 28, 2017
Question

HAL_SPI_TransmitReceive_IT is not calling HAL_SPI_TxRxCpltCallback

  • August 28, 2017
  • 4 replies
  • 7581 views
Posted on August 28, 2017 at 18:08

Hello everyone,

I am having a bit of trouble with the SPI communication between two STM32L053R8 boards.

I have a master who is sending 15 bytes every second to the slave.  The slave must also return 15 Bytes.

Tha master works properly and I confirmed it with a logic analyzer.

In the slave I  enabled the SPI global interrupt (via Cube) and I have the following code:

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

uint8_t dataRxSlave[15]={0};

int main(void)

{

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();

  MX_USART2_UART_Init();

  MX_SPI1_Init();

  while (1)

  {

      HAL_SPI_TransmitReceive_IT(&hspi1, (uint8_t*) dataTxSlave, (uint8_t*) dataRxSlave, 15);

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

      HAL_GPIO_TogglePin(GPIOA,LD2_Pin);

      HAL_Delay(500);

  }

}

I can confirm that the callback:

void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)

{

    printf('End InterrupTXRX\r\n');

}

Is never called. Neverthelees the HAL_SPI_TransmitReceive_IT is working since the Master is receiving the data from the slave.

 Does anyone knows what I missing here?

Your help is very much appreciated.

Greetings,

Diego.

    This topic has been closed for replies.

    4 replies

    Visitor II
    August 28, 2017
    Posted on August 28, 2017 at 18:34

    Hello again,

    I just realize about something interesting:

    the HAL_SPI_TransmitReceive_IT(&hspi1, (uint8_t*) dataTxSlave, (uint8_t*) dataRxSlave, 15) function is missing the last two bytes of data comming from the master.

    If I use the function HAL_SPI_Receive_IT(&hspi1, (uint8_t*) dataRxSlave, 15) then I get all the bytes and the callback HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) is executed. This is really strange.

    Is there anyway to confirm that I have the right HAL Drivers installed in my IDE (AC6 Open STM32 - Eclipse)?

    Thanks in advanced for the help!

    Greetings,

    Diego

    Visitor II
    January 29, 2018
    Posted on January 29, 2018 at 21:40

    I am having this exact same issue with a STM32F103. I am currently speaking with customer support but getting nowhere. Did you ever get a solution to this?

    Visitor II
    November 18, 2018

    Have the same issue.

    When I use blocking mode (HAL_SPI_TransmitReceive(...)) everything is working fine.

    I have buffer for send

    uint8_t request[3] = {1,2,3};

    And buffer for receive.

    uint8_t result[3] = {0,0,0};

    I have connected MISO to MOSI to make a loopback and confirmed that everything works fine in blocking mode (receiving {1,2,3}).

    Then I start transfer using function HAL_SPI_TransmitReceive_IT(...)

    SPI driver hangs and when I stopped processor I saw that it is in BUSY_TX_RX state.

    Result buffer is {1,3,0}

    So we can see that it is omit second byte.

    Then I add some dynamic printf in eclipse. Type char 't' when TXE interrupt is occures and type char 'r' when RXNE interrupt is occured.

    After I start debugging I can see

    "ttrtr" in debugger console. Should be "trtrtr"

    Have no idea how to fix. Only throw HAL code into garbage can together with processor and migrate to another manufacturer.

    Super User
    November 18, 2018

    What's your SPI clock frequency? Try lowering it.

    JW

    Visitor II
    November 18, 2018

    I used prescaler parameters from table provided in HAL SPI driver header file: 32. In this case I have 1MHz SCK frequency.

    By the way. I've solved the problem. I have customised TXRX function and SPI IT Handler.

    I push first byte of data before enable SPI, and I enable only RXNE interrupt.

    Then when RXNE interupt is occured I read income data first, and then in the same handler push next byte. After last byte is received (when txcount == 0) I disable RXNE IT and SPI peripheral and call my callback.

    Now everything works fine even on 2MHz

    Visitor II
    June 16, 2020

    hello, could you please show us your code.

    Visitor II
    December 17, 2019

    Hello,

    I use same code for the external ADC cmmunication with SPI interface. I get the correct digitized values. But the CS signal looks a little strange. For example during the reset state, there is a dirac signal. Does anyone have an idea to fix the problem? Also, How can I control CS sampling rate more precisely? Because I want to get 1MSPS rate.

    In CubeMX,

    SPI4 set as Full-duplex Master

    SPI4 NSS as Hardware output

    RCC as High Crystal Clock

    In clock settings, 8MHz, HSE, PLLCLK, and SYSCLK as 160MHz.

    In SPI4 configuration, Motorola, 16 bits, MSB, 4, 20Mbits/s, Low, 1 edge, sequentially.

    DMA is not enabled.

    SPI4 global interrupt enabled. 

    In Keil IDE,

    Variables:

    uint8_t i; // The index number of the testdata_in array

    uint16_t Sample;

    float volt=0; // Decimal value of the conversion

    uint8_t ADC_Buf[2]; // Output of ADC

    float testdata_in[60]; // 16 different ADC values in this array

    Main code calls in main.c file,

     while (1)

     {

     /* USER CODE END WHILE */

     /* USER CODE BEGIN 3 */

         HAL_SPI_Receive_IT(&hspi4,ADC_Buf, 2);

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

            Sample = (((uint16_t) ADC_Buf[1]) << 8 | ADC_Buf[0]) & 0x0FFF;

            volt = (float)(Sample * (3.3 / 4096.0)); //

            testdata_in[i++]=volt;

            i %= 60;

     }

    void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)

    {

       printf("End InterrupTXRX\r\n");

    }

    Thank you

    0690X00000Bujk0QAB.png0690X00000Bujk5QAB.png