-1

I have a program which enables interrupt receive until 250ms passed after message send, and after 250ms it should diable the interrupt and shouldn't receive any message until new message send. I wrote somethnig like that and I think it will work if I can disable Receive_IT but couldn't find anything about it

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
      i = rand() % 250 ;
      CAN_TxData[0] = i ;
      came = 1;
      currTime = HAL_GetTick();
      HAL_CAN_AddTxMessage(&hcan1, &CAN_TxHeader, CAN_TxData, &CAN_TxMailBox); // yollama kodu
      while((HAL_GetTick()-currTime <= 250) && came)
      {
          HAL_UART_Receive_IT(&huart2, TTL_RxData, 8);
      }
      if (HAL_GetTick()-currTime > 250)
      {
          // Here I should disable receive IT 
          timeoutted ++;
      }
  }

Since everyone complained about my solution I wanted to clarify my project.

First stm32 will send 8byte data to the second stm32 by using CAN, and second stm32 will send the same data by using USART to the first stm32.

If first stm32 receives the message until 250ms passed after its own message, it will check if its the same message it send or not then record it (correct ++ and wrong ++). But if 250ms passed and first stm32 didn't receive anyhting it says it is timeout and will start the same process again. This will continue.

ouston
  • 11
  • 1
  • 1
    What's the actual problem you are trying to solve? Why can't you use double buffering and always receive? Does this part have DMA? – Lundin Sep 22 '22 at 11:26
  • @Lundin this doesn't have DMA. I just want to disable Receive_IT if 250ms passed after I sent message with CAN. Then it should start again – ouston Sep 22 '22 at 11:45
  • Asynchronous oprations cannot be done in the loops only in the event handlers – 0___________ Sep 22 '22 at 11:50
  • @0___________ I edited my message to clarify what Im doing – ouston Sep 22 '22 at 12:00

1 Answers1

1

This code makes no sense at all.

You call the HAL_UART_Receive_IT function in the loop (it only sets stuff but not waiting for anything). It is called hundreds (or maybe even many thousands) of times in this loop.

What you can do:

  1. Set the timer interrupt for 250ms.
  2. If IT receive complete callback is fired before it expires disable the timer interrupt.
  3. If the timer interrupt is invoked, simply abort IT receive HAL_UART_AbortReceive_IT (which will also disable this interrupt)
0___________
  • 60,014
  • 4
  • 34
  • 74