cancel
Showing results for 
Search instead for 
Did you mean: 

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

RLind.2
Associate II

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");

   }

  }

}

3 REPLIES 3
TDK
Guru

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
Associate II

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
Guru

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".