Question
HAL UART Transmit problem
Posted on November 08, 2015 at 21:19
I have a running project with STM32F103 and I wanted to add UART Transmit to do some debugging but HAL_UART_Transmit does not work and does not transmit any data. I have chosen USART2 and PD.5 and PD.6 pins as the interface. The weird thing is that when I create a new project using STM32cubemx the UART does work. But when I implement necessary changes into my first project the UART does not work. I checked a lot of times for something maybe missing but yet have not figured this out.
Here's what I did: First enabled HAL_UART_MODULE_ENABLED in stm32f1xx_hal_conf.hThen :UART_HandleTypeDef huart2;Init function which will be called in main:void UART_Config(void){ /* UARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled *//* Peripheral clock enable */ huart2.Instance = USART2; huart2.Init.BaudRate = 38400; 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; HAL_UART_Init(&huart2);}And HAL_UART_MspInit() to init PD.5 and PD.6 which will be called at the end of above code:void HAL_UART_MspInit(UART_HandleTypeDef* huart){ GPIO_InitTypeDef GPIO_InitStruct; if(huart->Instance==USART2) { /* USER CODE BEGIN USART2_MspInit 0 */ /* USER CODE END USART2_MspInit 0 */ /* Peripheral clock enable */ __USART2_CLK_ENABLE(); /**USART2 GPIO Configuration PD5 ------> USART2_TX PD6 ------> USART2_RX */ GPIO_InitStruct.Pin = GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); __HAL_AFIO_REMAP_USART2_ENABLE(); /* USER CODE BEGIN USART2_MspInit 1 */ /* USER CODE END USART2_MspInit 1 */ }}Then I'm simply calling HAL_UART_Transmit() in the main function after the initialization of all the peripherals like this : HAL_UART_Transmit(&huart2, ''Hello\n'', 6, 10);But I don't see anything in terminal and I assure you the hardware and connections and things like that are fine considering it all work with a new project. But since I have lots of code in this project I figured it would be easier to insert UART in this project instead of doing the other way around. It's not supposed to be this hard getting a simple UART to work. Could you guys help me? #uart