cancel
Showing results for 
Search instead for 
Did you mean: 

HAL UART Transmit problem

mostafanfs
Associate II
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.h

Then :

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
8 REPLIES 8
mostafanfs
Associate II
Posted on November 09, 2015 at 08:58

You guys have any thoughts why the UART is not working?

I couldn't think of anything else. 

mostafanfs
Associate II
Posted on November 09, 2015 at 12:58

Ok I figured by debug and oscilloscope that UART TX pin is not configured correctly because it's in the low state while we all know that TX pin is supposed to be in high state in idle situations. And comparing with my another program (in which USART2 does work) I find out that __HAL_UART_ENABLE(huart) does not enable UART peripheral in my current program. 

I mean I see in oscilloscope that after running __HAL_UART_ENABLE macro (which is called at the end of HAL_UART_Init function) TX goes into a high state in one program which is correct but it stays in low state in my current program while all the port settings and peripheral settings are the same. 

Could you help me? 

Posted on November 09, 2015 at 13:31

You guys have any thoughts why the UART is not working?

I'd assume there are very few people here providing support. Even less with HAL.

Do you enable the AFIO clock anywhere in your code? You'll need that for the remapping to work on the F1 parts.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mostafanfs
Associate II
Posted on November 09, 2015 at 16:41

Yes there is a macro named __HAL_AFIO_REMAP_USART2_ENABLE in HAL_UART_MspInit()

What's the big deal with HAL anyway? 

Is it not good enough or something? 

I seem to remember there was Standard Peripheral Library. Is it that different from HAL drivers? Because it seems sort of the same to me. 

And ST seems like it has switched over to use HAL and not the old one anymore. 

Although I can also say I'm having problem with HAL anyway. 
Posted on November 09, 2015 at 18:36

What's the big deal with HAL anyway? Is it not good enough or something?

I just don't care for it, it attempts to solve a problem I don't have, and introduces bugs I don't need.

It's not supposed to be this hard getting a simple UART to work.

No it shouldn't, there's too much abstraction and a lot of remedial errors.

I'd expect something like __AFIO_CLK_ENABLE(); prior to remapping

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mostafanfs
Associate II
Posted on November 10, 2015 at 21:26

I just don't care for it, it attempts to solve a problem I don't have, and introduces bugs I don't need.

But seems like STM32F10x standard peripheral libraries are not supported anymore and the last update was in 2011! Seems like we're kind of have to move on sooner or later, don't you think?!

I'd expect something like __AFIO_CLK_ENABLE(); prior to remapping

 

No it's not it. 
Posted on November 11, 2015 at 02:04

So why did you pick a ~10 year old processor for your new design?

The reason the SPL hasn't been updated is because it's pretty bug free and solid, it doesn't need to be updated every week to address the latest batch of errors in the Cube/HAL ecosystem. Solid code can work for decades, it shouldn't need a constant stream of Microsoft/Adobe fixes.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mostafanfs
Associate II
Posted on November 11, 2015 at 22:04

Well I took your advise and have downloaded the last version STM32F10x_StdPeriph_Lib_V3.5.0 and setup a project in System Workbench for STM32 (which by the way took too much of my time getting it to work without errors) , put necessary symbols, added required folders and everything. 

So far it looks good and it seems like I'm having much less problems.