Skip to main content
antonius
Associate III
March 11, 2019
Question

Using 2 UART on STM32?

  • March 11, 2019
  • 18 replies
  • 9817 views

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

This topic has been closed for replies.

18 replies

Tesla DeLorean
Guru
March 11, 2019

I would expect NMEA sentences at 9600 8N1

Your HAL functions block, so no ideal for forwarding data.​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
AvaTar
Senior III
March 11, 2019

> I would expect NMEA sentences at 9600 8N1

This are the default setting of the module.

Do you have the baudrate of the STM32 MCU set properly ?

And not changed the module's setting to binary UBX messages ?

In addition to the inappropriateness of said HAL functions.

Tesla DeLorean
Guru
March 11, 2019

Seems too consistent and wrong data to be UBX. Would need to know more about specific implementation. NAZA​ UAV modules for example output their own packet format.

One could observe data with a scope, or use USB to CMOS Serial directly from NEO-6M module.​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
March 11, 2019

Does the receive call function or does it return an error? Try clearing the buffer and checking. Post lacks a lot of salient details which would help diagnose situation.

To answer the underlying question, No this doesn't look to be receiving properly/correctly. ​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
antonius
antoniusAuthor
Associate III
March 11, 2019
void MX_USART1_UART_Init(void)
{
 
 huart1.Instance = USART1;
 huart1.Init.BaudRate = 115200;
 huart1.Init.WordLength = UART_WORDLENGTH_8B;
 huart1.Init.StopBits = UART_STOPBITS_1;
 huart1.Init.Parity = UART_PARITY_NONE;
 huart1.Init.Mode = UART_MODE_TX_RX;
 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
 if (HAL_UART_Init(&huart1) != HAL_OK)
 {
 Error_Handler();
 }
 
}
/* USART2 init function */
 
void MX_USART2_UART_Init(void)
{
 
 huart2.Instance = USART2;
 //huart2.Init.BaudRate = 115200;
	huart2.Init.BaudRate = 9600;//NEO6M Baudrate
 huart2.Init.WordLength = UART_WORDLENGTH_8B;
 huart2.Init.StopBits = UART_STOPBITS_1;
 huart2.Init.Parity = UART_PARITY_NONE;
 huart2.Init.Mode = UART_MODE_TX_RX;
 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart2.Init.OverSampling = UART_OVERSAMPLING_16;
 if (HAL_UART_Init(&huart2) != HAL_OK)
 {
 Error_Handler();
 }
 
}

UART Setting from STM32CubeMX

@Clive

Which UART buffer should I clear ?

antonius
antoniusAuthor
Associate III
March 11, 2019

I'm using the module indoor, does it receive signal ?

Or without a signal, at least I can read its ID with UART ?

Is this the right manual ?

https://www.terraelectronica.ru/pdf/show?pdf_file=%2Fz%2FDatasheet%2FU%2FUART+GPS+NEO-6M+User+Manual.pdf

or there's another more comprehensive one ?

Thanks

antonius
antoniusAuthor
Associate III
March 11, 2019
antonius
antoniusAuthor
Associate III
March 11, 2019
antonius
antoniusAuthor
Associate III
March 14, 2019

I got output from

serial to PC

0690X0000087xX0QAI.png

May be with interrupt is better ?

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

display to pc here

}

antonius
antoniusAuthor
Associate III
March 14, 2019

I try with :

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

 /* Prevent unused argument(s) compilation warning */

 UNUSED(huart);

 /* NOTE : This function should not be modified, when the callback is needed,

           the HAL_UART_RxCpltCallback can be implemented in the user file

  */

   if(huart->Instance==USART2)

   {

      while(1)

    HAL_UART_Transmit(&huart1, (uint8_t *)aRxBuffer, 10,0xFFFF);

      printf("interupt rom USART2 GPS");

  }

}

but there's nothing ??

in main()

while (1)

   {

      printf("INIT CODE GPS NEO - 6M....\r\n");

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

      HAL_Delay(260);

   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);

      HAL_Delay(260);

      printf("From GPS : ");

  HAL_UART_Receive_IT(&huart2, (uint8_t *)aRxBuffer, 1000); 

any helps?

thanks

Dean Perry
Visitor II
March 14, 2019

it might take a little while for you to get enough GPS sentences to fill 1000 characters (which is when RxCpltCallback will be called).

BTW, it's not great form to use a infinite loop ('while(1)") in your callbacks (even if you're just playing around).

What you want to do is totally achievable; just last week I wrote code to poll a u-blox M8 on one UART and send data to another simultaneously... getting the hang of concurrent programming can take a little while... 9600 is pretty slow; try receiving just one character at a time, putting that into a queue in your completion routine and reading it out in main... that's (roughly) how I did it.

antonius
antoniusAuthor
Associate III
March 14, 2019

can you share the code / pseudo code , please ?

Thanks

AvaTar
Senior III
March 14, 2019

> void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

> {

> ...

>      while(1)

>    HAL_UART_Transmit(&huart1, (uint8_t *)aRxBuffer, 10,0xFFFF);

>     printf("interupt rom USART2 GPS");

Transmission is most probably blocking, calling it from interrupt context is disruptive. The same goes for the "printf()" call.

And what is the "while (1)" there supposed to do, except for never returning ???