cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot get USART printf working with FreeRTOS + LWIP

Sebastian Gniazdowski
Associate III
Posted on April 04, 2018 at 12:50

Hello. I'm doing CubeMX generation, but earlier I also copied bits of firmware UART printf example. Tried with USART1, USART3, USART2.   HAL_UART_Transmit(&huart2, ''Hello\n'', 6, 0xFFFF); doesn't yield output (i.e. zero reaction on serial console app; running simultaneously openned project derived from firmware-example works, without touching the serial application; UART parameters like parity etc. is the same).

Screenshots of pinout and CLOCK & USART2 configuration:

0690X0000060AR1QAM.png0690X0000060AIpQAM.png0690X0000060AQNQA2.png0690X0000060AQOQA2.png0690X0000060AQSQA2.png0690X0000060AQXQA2.png

USART initialization code is:

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 */

    __HAL_RCC_USART2_CLK_ENABLE();

 

    /**USART2 GPIO Configuration    

    PA3     ------> USART2_RX

    PD5     ------> USART2_TX

    */

    GPIO_InitStruct.Pin = GPIO_PIN_3;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_5;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /* USER CODE BEGIN USART2_MspInit 1 */

  /* USER CODE END USART2_MspInit 1 */

  }

}

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

    /**Configure the main internal regulator output voltage

    */

  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    /**Initializes the CPU, AHB and APB busses clocks

    */

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

  RCC_OscInitStruct.HSEState = RCC_HSE_ON;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

  RCC_OscInitStruct.PLL.PLLM = 25;

  RCC_OscInitStruct.PLL.PLLN = 432;

  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

  RCC_OscInitStruct.PLL.PLLQ = 2;

  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    /**Activate the Over-Drive mode

    */

  if (HAL_PWREx_EnableOverDrive() != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    /**Initializes the CPU, AHB and APB busses clocks

    */

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART2;

  PeriphClkInitStruct.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    /**Configure the Systick interrupt time

    */

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick

    */

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */

  HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);

}

/* USART2 init function */

static void MX_USART2_UART_Init(void)

{

  huart2.Instance = USART2;

  huart2.Init.BaudRate = 9600;

  huart2.Init.WordLength = UART_WORDLENGTH_9B;

  huart2.Init.StopBits = UART_STOPBITS_1;

  huart2.Init.Parity = UART_PARITY_ODD;

  huart2.Init.Mode = UART_MODE_TX_RX;

  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart2.Init.OverSampling = UART_OVERSAMPLING_16;

  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

  if (HAL_UART_Init(&huart2) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

}

/** Configure pins as

        * Analog

        * Input

        * Output

        * EVENT_OUT

        * EXTI

*/

static void MX_GPIO_Init(void)

{

  /* GPIO Ports Clock Enable */

  __HAL_RCC_GPIOH_CLK_ENABLE();

  __HAL_RCC_GPIOC_CLK_ENABLE();

  __HAL_RCC_GPIOA_CLK_ENABLE();

  __HAL_RCC_GPIOB_CLK_ENABLE();

  __HAL_RCC_GPIOG_CLK_ENABLE();

  __HAL_RCC_GPIOD_CLK_ENABLE();

}

My board is Nucleo-144 F767ZIT6. I can run the firmware UART printf example and it works. Can anyone help?

#lwip-stm32f7-nucleo-144 #printf #nucleo-144 #hal-uart #hal_uart_transmit #hal-usart
13 REPLIES 13
Tilen MAJERLE
ST Employee
Posted on April 04, 2018 at 14:16

Hello,

did you intentionally enable ODD parity for USART?

Best regards,

Tilen

AvaTar
Lead
Posted on April 04, 2018 at 14:23

... doesn't yield output.

Means what ?

No characters received on a PC terminal application ?

Have you checked the signal on the USART TX pin ?

As repeatedly stated, I won't wade into Cube code ...

Posted on April 04, 2018 at 14:23

>> I can run the firmware UART printf example and it works.

The example presumably uses the VCP, which is USART3 on PD8/PD9

I would make sure to enable the GPIO clocks at the point where I initialized the pins.

>> RCC_OscInitStruct.PLL.PLLM = 25;

Pretty sure the board is using an 8 MHz clock, not a 25 MHz one, make sure PLLM is set properly, and that HSE_VALUE in stm32f7xx_hal_conf.h is 8000000

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Frank CA
Associate III
Posted on April 04, 2018 at 14:42

@Sebastian - you are showing just CMX auto generated code, why do you think there is an error there? Only if you misconfigured something. And what is not working? Please be more specific if you want meaningful help. Describe what is not happening, what you have done to try different options and show your user code. What  is different between your code and the example? Show also screenshots of your CMX config settings (USART2, RCC).

Regards.

Posted on April 04, 2018 at 15:43

Yes, and selected 9 bits, knowning the HAL-thing that the bits are including parity. I will state also here, that I get zero reaction in terminal app, while clicking 'run' in SW4STM32 on an example-derived project (different project open simultaneously) works.   

Posted on April 04, 2018 at 15:47

I get no reaction in terminal application listening on tty.usbFA123. When I run other project, simultaneously opened in SW4STM32, which is derived from the firmware-example, I get response in the terminal application, without touching anything. I was counting on some insight on the Cube generated code and/or FreeRTOS+LWIP conditions. I was trying earlier to copy firmware-example (UART print) code, but I'm not 100% sure if I was enough strict and careful – it also didn't work. I even found bug there, peripherial clock of USART1 is enabled, while it should be USART3. It still works, though.

Posted on April 04, 2018 at 15:53

Send a continuous stream of U characters and scope the pin so you can see the signal and measure the bit timing.

See earlier message about using the correct PLL settings and HSE_VALUE, compare those to that of the working printf example.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 04, 2018 at 16:51

Thank you, the firmware example uses 8* in both places, I indeed had HSE_VALUE set to 25000000, weird, It didn't help the UART problem. I'm sure I have correct board selected, proved that once in forum, pasting lines from file *.ioc:

Mcu.Name=STM32F767ZITx

Mcu.UserName=STM32F767ZITx

PCC.MCU=STM32F767ZITx

PCC.PartNumber=STM32F767ZITx

ProjectManager.DeviceId=STM32F767ZITx

I have now edited .ioc and changed the fields to:

RCC.HSE_VALUE=8000000

RCC.PLLM=8

I've reloaded the project in MX, I was counting on some clock issue to be reported, but nothing. I do following to test the serial communication:

  HAL_UART_Transmit(&huart2, 'Hello\n', 6, 0xFFFF);

  printf('Starting...\n');

I have __io_putchar overloaded, placed Error_Handler(); there to ensure it is called, and it was OK.

Posted on April 04, 2018 at 17:31

Don't have oscilloscope

:(

But PLLM and HSE_VALUE were wrong, I've changed them to 8*, the example had correct values. No change in UART (lack of) communication, though.