Skip to main content
RLind.2
Associate II
October 9, 2020
Question

We are trying to get UART-receiver to work in interrupt mode.

  • October 9, 2020
  • 3 replies
  • 994 views

Here is the code that doesn't work. Any suggestions?

UART_HandleTypeDef UartHandle;

uint8_t MyRxBuf[ 10 ], MyTxBuf[3];

int main( void ) {

  uint32_t t_print;

  int16_t i;

  HAL_Init();

  SystemClock_Config();

  

 /* Put the USART peripheral in the Asynchronous mode (UART Mode) */

 /* UART2 configured as follows:

   - Word Length = 8 Bits

   - Stop Bit = One Stop bit

   - Parity = none

   - BaudRate = 38400 baud

   - Hardware flow control disabled (RTS and CTS signals) */

  UartHandle.Instance     = USART2;

  UartHandle.Init.BaudRate   = 38400;

  UartHandle.Init.WordLength  = UART_WORDLENGTH_8B;

  UartHandle.Init.StopBits   = UART_STOPBITS_1;

  UartHandle.Init.Parity    = UART_PARITY_NONE;

  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;

  UartHandle.Init.Mode     = UART_MODE_TX_RX;

  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;

  UartHandle.pTxBuffPtr    = &MyTxBuf[ 0 ];

  UartHandle.TxXferSize    = 3;

  UartHandle.pRxBuffPtr    = &MyRxBuf[ 0 ];

  UartHandle.RxXferSize    = 10;

  UartHandle.RxXferCount    = 3;

  if(HAL_UART_Init(&UartHandle) != HAL_OK) {

   Error_Handler(); 

  }

  HAL_UART_Receive_IT( &UartHandle, &MyRxBuf[ 0 ], 1 ); // Needed?

  /* Infinite loop */ 

  t_print = get_RTC();

  while (1) {

   if( get_RTC() > t_print ) {

     t_print += 2000UL; // Print buffer every 2 seconds

     printf("RxCnt %d ", UartHandle.RxXferCount );

     for(i=0;i<10;i++) printf("%d ", MyRxBuf[i] );

     printf("\n\r");

   }

  }

}

This topic has been closed for replies.

3 replies

TDK
October 9, 2020

Are you initializing pins?

Does your interrupt handler call HAL_UART_IRQHandler?

"If you feel a post has answered your question, please click ""Accept as Solution""."
RLind.2
RLind.2Author
Associate II
October 14, 2020

Hello,

Sorry for delay in answer.

We don't know how to do that. Could you give example of how to call HAL_UART_IRQHandler?

TDK
October 14, 2020

Generate code from CubeMX and it will (probably) do this for you. Or check out a UART interrupt example in the appropriate cube repository.

"If you feel a post has answered your question, please click ""Accept as Solution""."