cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_SPI_TransmitReceive_IT is not calling HAL_SPI_TxRxCpltCallback

Diego Ballen
Associate II
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.

9 REPLIES 9
Diego Ballen
Associate II
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

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?

PBori
Associate

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.

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

JW

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

AE104
Senior

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

hello, could you please show us your code.

void HAL_SPI_RxCpltCallback (SPI_HandleTypeDef * hspi){ // Set CS pin to high and raise flag // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET); //HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); //memset(rx_buffer,0,sizeof(rx_buffer)); if(rx_buffer[rx_index++]!=9) { //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); HAL_SPI_Receive_IT(&hspi1,rx_buffer+rx_index,1); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); } else { // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET); rx_index=0; HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // HAL_SPI_Receive_IT(&hspi1,rx_buffer+rx_index,1); } } int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); ---THSISIS RECICVER SIDE ABOVE AND BELOW IS TX SIDE--TX SIDE:// Callback function called when SPI transmission is complete void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) { // Set CS pin to high HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); if ( HAL_SPI_Transmit_IT(&hspi1, tx_buffer, 10)!= HAL_OK) { // Handle error, such as retry or logging Error_Handler(); } // Transmit the next byte // transmitNextByte(); } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ //1)---void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) //{ //// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // ////__NOP(); //HAL_SPI_TransmitReceive_IT(&hspi1,tx_buffer,rx_buffer,10); //} //void HAL_SPI_TxCpltCallback (SPI_HandleTypeDef * hspi) //{ // // Set CS pin to high and raise flag // //HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET); // //// HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); //// HAL_SPI_Transmit_IT(&hspi1,tx_buffer,10); // // // // //1byt tx // // // if(tx_index++ < 9) // { // // //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); //cs on // HAL_SPI_Transmit_IT(&hspi1,tx_buffer+tx_index,1); // } // else // { // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET); //cs stop // tx_index=0; // HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); // // HAL_SPI_Transmit_IT(&hspi1,tx_buffer+tx_index,1); // // // // } // //} int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART2_UART_Init(); MX_SPI1_Init(); MX_SPI2_Init(); /* USER CODE BEGIN 2 */ // CS pin should default high HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET); HAL_Delay(100); /* USER CODE END 2 */ // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); // // // Transmit first byte HAL_SPI_Transmit_IT(&hspi1, tx_buffer, 10); //HAL_Delay(200); /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ //1) HAL_SPI_TransmitReceive(&hspi1,tx_buffer,rx_buffer,10,100); // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); //2 HAL_SPI_Transmit(&hspi1,tx_buffer,10,100); // HAL_SPI_Transmit_IT(&hspi1,tx_buffer,10); HAL_Delay(200); // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET); /* USER CODE BEGIN 3 */ // // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); // transmitNextByte(); // if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6) == GPIO_PIN_SET) // { // // Set CS pin low to start transmission // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); // // // Transmit first byte // HAL_SPI_Transmit_IT(&hspi1, tx_buffer, 10); // HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // //HAL_Delay(1000); // } HAL_Delay(500); // // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); // HAL_SPI_Transmit_IT(&hspi1,tx_buffer,1); // HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // HAL_Delay(100); } /* USER CODE END 3 */ } /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART2_UART_Init(); MX_SPI1_Init(); MX_SPI2_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); HAL_SPI_Receive_IT(&hspi1,rx_buffer,1); /* Infinite loop */ ---------------------------MY DOUBT TX ARRAY is ther on op iam lossoing 1byte i.e 4 and iam getin greamianign bytes y pls neglect comenting and just see which lline of code coausing loosing in 1byte and some time other data also coming in debug iamseeing then igot allbytes 1byte lsssing

Use code snippet to show formatted code

 

Code Snippet.jpg