A
A
AVerzin2022-04-11 23:59:30
UART
AVerzin, 2022-04-11 23:59:30

What is the problem with connecting via UART GPS module to STM32?

Hi all! I am learning programming on STM32, I am trying to connect a GPS sensor via UART, but unfortunately everything does not work as expected. Data from GPS comes via UART1 and via UART2 goes to the terminal on the PC, or rather they should.

It all works in the following way, if you do the following algorithm of actions: turn off the power at the STM, break the TX / RX UART line, apply power to the STM, restore the line (I have it connected by breadboard wiring). Then all the information from the GPS module begins to fully come to the terminal.

What could be the problem?

It seems to me that the problem here is not so much in the code as in the connection ... But I use ready-made blocks, the stm board is
NUCLEO-F103RB, GPS troyka v2 from Amperka. I take the ground and 3.3V from the nucleo board, and the TX module is plugged into the RX (PB7 this leg is in input mode with a pull-up to + 3.3V) nucleo.

spoiler
char str[1024] = { 0 };
int main(void) {
   MX_USART1_UART_Init();
   MX_USART2_UART_Init();
   HAL_UARTEx_ReceiveToIdle_IT(&huart1, str, 1024);
   while (1) {
   }
}
static void MX_USART1_UART_Init(void) {

   /* USER CODE BEGIN USART1_Init 0 */

   /* USER CODE END USART1_Init 0 */

   /* USER CODE BEGIN USART1_Init 1 */

   /* USER CODE END USART1_Init 1 */
   huart1.Instance = USART1;
   huart1.Init.BaudRate = 9600;
   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();
   }
   /* USER CODE BEGIN USART1_Init 2 */

   /* USER CODE END USART1_Init 2 */

}

/**
 * @brief USART2 Initialization Function
 * @param None
 * @retval None
 */
static void MX_USART2_UART_Init(void) {

   /* USER CODE BEGIN USART2_Init 0 */

   /* USER CODE END USART2_Init 0 */

   /* USER CODE BEGIN USART2_Init 1 */

   /* USER CODE END USART2_Init 1 */
   huart2.Instance = USART2;
   huart2.Init.BaudRate = 115200;
   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();
   }
   /* USER CODE BEGIN USART2_Init 2 */

   /* USER CODE END USART2_Init 2 */

}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
   if (huart->Instance == USART1) {
      HAL_UART_Transmit_IT(&huart2, str, Size);
      HAL_UARTEx_ReceiveToIdle_IT(&huart1, str, 1024);
   }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AVerzin, 2022-04-12
@AVerzin

There was a bug in the display initialization code and the UART was malfunctioning because of it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question