cancel
Showing results for 
Search instead for 
Did you mean: 

Using 2 UART on STM32?

antonius
Senior

Dear Members,

I want to read a result from my GPS,

it's Neo 6M

I initialized 2 UARTs UART 1 for PC and UART 2 for GPS,

I got this result :

INIT CODE GPS NEO - 6M....

                           

From GPS : 0690X0000087jMhQAI.png

Is it receiving properly ?

The code :

 char in[8];

printf("From GPS : ");

  HAL_UART_Receive(&huart2, (uint8_t *)in, 8, 1000);

HAL_UART_Transmit(&huart1, (uint8_t *)in, 8, 1);

printf("\r\n");

Green LED on GPS module is blinking...

Thanks

35 REPLIES 35

Thanks for the reply Clive,

I had put this function :

/* USER CODE BEGIN PD */
    #define MYUART &huart2 // or whatever
    #define USART_GPS USART2
    #define USART_GPS_IRQn USART2_IRQn
    //#define USART_GPS_IRQHandler USART2_IRQHandler
     
    #ifdef USART_CR1_TXEIE_TXFNFIE // FIFO Support (L4R9)
    #define USART_CR1_TXEIE USART_CR1_TXEIE_TXFNFIE
    #define USART_ISR_TXE USART_ISR_TXE_TXFNF
    #define USART_CR1_RXNEIE USART_CR1_RXNEIE_RXFNEIE
    #define USART_ISR_RXNE USART_ISR_RXNE_RXFNE
    #endif
		#define LINEMAX 200 // Maximal allowed/expected line length
/* USER CODE END PD */
............
void USART_GPS_IRQHandler(void) // Sync and Queue NMEA Sentences
{
 static char rx_buffer[LINEMAX + 1]; // Local holding buffer to build line, w/NUL
 static int rx_index = 0;
 if (USART_GPS->SR & USART_SR_ORE) // Overrun Error
 //USART_GPS->ICR = USART_ICR_ORECF;
 if (USART_GPS->SR & USART_SR_NE) // Noise Error
 //USART_GPS->ICR = USART_ICR_NCF;
 if (USART_GPS->SR & USART_SR_FE) // Framing Error
 //USART_GPS->ICR = USART_ICR_FECF;
 if (USART_GPS->CR1 & USART_CR1_RXNEIE) // Received character?
 {
 char rx = (char)(USART_GPS->DR & 0xFF);
 if ((rx == '\r') || (rx == '\n')) // Is this an end-of-line condition, either will suffice?
 {
 if (rx_index != 0) // Line has some content?
 {
 rx_buffer[rx_index++] = 0; // Add NUL if required down stream
	  //QueueBuffer(rx_buffer, rx_index); // Copy to queue from live dynamic receive buffer
 rx_index = 0; // Reset content pointer
 }
 }
 else
 {
 if ((rx == '$') || (rx_index == LINEMAX)) // If resync or overflows pull back to start
 rx_index = 0;
 rx_buffer[rx_index++] = rx; // Copy to buffer and increment
 }
 }
}
 
 

How does that function related with main() ?

Thanks

STM32L1XX doesn't have ICR Register on USART peripheral,

what's the similar register with STM32L4 XX ?

page 746, 27.6.8 STM32L1 Reference

0690X00000887TmQAI.png

is it related with stm32l1xx_it.c

/**

 * @brief This function handles USART2 global interrupt.

 */

void USART2_IRQHandler(void)

{

 /* USER CODE BEGIN USART2_IRQn 0 */

 /* USER CODE END USART2_IRQn 0 */

 HAL_UART_IRQHandler(&huart2);

 /* USER CODE BEGIN USART2_IRQn 1 */

 /* USER CODE END USART2_IRQn 1 */

}

replace that function with yours ?

Thanks

antonius
Senior

with this code :

 HAL_UART_Receive_IT(&huart2, aRxBuffer, 32); 

  

      printf("From GPS : ");

    HAL_UART_Transmit_IT(&huart1,aRxBuffer,32);

I got Null output, anyone has a clue ?

antonius
Senior

I don't understand there's nothing on USART_DR USART2 register,

but when I checked with PC there are many datas from GPS module,

Any wrong initialization ?

Thanks0690X0000088JW4QAM.png

antonius
Senior

I got it work with UART1, still don't understand, why isn't it working with UART2 ??? anyone ?