cancel
Showing results for 
Search instead for 
Did you mean: 

UART output is garbled on Tera Term

chachamaru
Associate II

I ran the example project and successfully displayed “Hello World” on Tera Term.

Then I tried to port this project into my own one.
However, the result was garbled characters in the terminal.

The baud rate (115200) and PLL/clock configuration are the same as in the working example.
Do you have any idea what might cause the characters to be corrupted?

 

/**
  ******************************************************************************
  * @file    DCMI/DCMI_CaptureMode/Src/main.c 
  * @author  MCD Application Team
  * @brief   This example describes how to configure the camera in continuous mode.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2017 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private Includes -----------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include <string.h>

/** @addtogroup STM32F4xx_HAL_Examples
  * @{
  */

/** @addtogroup DCMI_CaptureMode
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
TIM_HandleTypeDef htim1;

UART_HandleTypeDef huart1;

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PV */
const char msg[] = "Hello World\r\n";
/* USER CODE END PV */

/* Private variables ---------------------------------------------------------*/
static DMA2D_HandleTypeDef hdma2d_eval;

/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void Error_Handler(void);
static void LCD_LL_ConvertLineToARGB8888(void *pSrc, void *pDst);
static void MX_USART1_UART_Init(void);
void Start_XCLK(void);
/* Private functions ---------------------------------------------------------*/

/**
  * @brief   Main program
  * @PAram  None
  * @retval None
  */
int main(void)
{

	  /* STM32F4xx HAL library initialization:
	       - Configure the Flash prefetch, instruction and Data caches
	       - Configure the Systick to generate an interrupt each 1 msec
	       - Set NVIC Group Priority to 4
	       - Global MSP (MCU Support Package) initialization
	     */
	  HAL_Init();
	  /* Configure the system clock to 180 MHz */
	  SystemClock_Config();

	  /* Configure LED3 and LED4 */
	  BSP_LED_Init(LED3);

	  BSP_LED_Init(LED4);

	  /* UART初期化*/
	  MX_USART1_UART_Init();
	  MX_TIM1_Init(); //PA8設定
	  Start_XCLK(); //PWM

	  /*##-1- Disable SAI1_SDA signal ############################################*/
	  /* Note: In STM324X9I-EVAL RevB, PE6 pin is shared between data_7 of camera
	           and SAI1_SDA of codec WM8994, after power on, SAI1_SDA pin of codec WM8994
	           is in output state, thus preventing MCU from Receiving correct signal
	           from camera, so we need to configure SAI1_SDA pin of codec WM8994
	           in tri-state
	  */

	  /*##-2- Initialize the LCD #################################################*/
	  BSP_LCD_Init();

	  /*##-3- Initialize the LCD Layer ###########################################*/
	  BSP_LCD_LayerDefaultInit(1, LCD_FRAME_BUFFER);

	  /* Enable the LCD */
	  BSP_LCD_DisplayOn();

	  /* Select the LCD Foreground layer */
	  BSP_LCD_SelectLayer(1);

	  /*##-4- Camera Initialization and start capture ############################*/
	  /* Initialize the Camera */
	  //BSP_CAMERA_Init(RESOLUTION_R480x272);

	  /* Start the Camera Capture */
	  //BSP_CAMERA_ContinuousStart((uint8_t *)CAMERA_FRAME_BUFFER);

	  printf("UART test start\r\n");

	  /* Infinite loop */
	  while (1)
	  {
		    HAL_UART_Transmit(&huart1, (uint8_t*)msg, sizeof(msg)-1, HAL_MAX_DELAY);
		    HAL_Delay(100);
	  }
}

/**
  * @brief  Camera line event callback
  * @PAram  None
  * @retval None
  */
void BSP_CAMERA_LineEventCallback(void)
{
  static uint32_t tmp, tmp2, counter;
  
  if(BSP_LCD_GetYSize() > counter)
  {
    LCD_LL_ConvertLineToARGB8888((uint32_t *)(CAMERA_FRAME_BUFFER + tmp), (uint32_t *)(LCD_FRAME_BUFFER + tmp2));
    tmp  = tmp + BSP_LCD_GetXSize()*sizeof(uint16_t); 
    tmp2 = tmp2 + BSP_LCD_GetXSize()*sizeof(uint32_t);
    counter++;
  }
  else
  {
    tmp = 0;
    tmp2 = 0;
    counter = 0;
  }
}

/**
  * @brief  Converts a line to an ARGB8888 pixel format.
  * @PAram  pSrc: Pointer to source buffer
  * @PAram  pDst: Output color
  * @PAram  xSize: Buffer width
  * @PAram  ColorMode: Input color mode   
  * @retval None
  */

void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(huart->Instance==USART1)
  {
    /* USER CODE BEGIN USART1_MspInit 0 */

    /* USER CODE END USART1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10; // TX=PA9, RX=PA10
    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_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USER CODE BEGIN USART1_MspInit 1 */

    /* USER CODE END USART1_MspInit 1 */

  }

}

static 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();
  }
}

static void LCD_LL_ConvertLineToARGB8888(void *pSrc, void *pDst)
{ 
  /* Enable DMA2D clock */
  __HAL_RCC_DMA2D_CLK_ENABLE();
  
  /* Configure the DMA2D Mode, Color Mode and output offset */
  hdma2d_eval.Init.Mode         = DMA2D_M2M_PFC;
  hdma2d_eval.Init.ColorMode    = DMA2D_ARGB8888;
  hdma2d_eval.Init.OutputOffset = 0;     
  
  /* Foreground Configuration */
  hdma2d_eval.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
  hdma2d_eval.LayerCfg[1].InputAlpha = 0xFF;
  hdma2d_eval.LayerCfg[1].InputColorMode = DMA2D_INPUT_RGB565;
  hdma2d_eval.LayerCfg[1].InputOffset = 0;
  
  hdma2d_eval.Instance = DMA2D; 
  
  /* DMA2D Initialization */
  if(HAL_DMA2D_Init(&hdma2d_eval) == HAL_OK) 
  {
    if(HAL_DMA2D_ConfigLayer(&hdma2d_eval, 1) == HAL_OK) 
    {
      if (HAL_DMA2D_Start(&hdma2d_eval, (uint32_t)pSrc, (uint32_t)pDst, BSP_LCD_GetXSize(), 1) == HAL_OK)
      {
        /* Polling For DMA transfer */  
        HAL_DMA2D_PollForTransfer(&hdma2d_eval, 10);
      }
    }
  }
  else
  {
    /* FatFs Initialization Error */
    Error_Handler();
  }
}

/**
  * @brief  This function is executed in case of error occurrence.
  * @PAram  None
  * @retval None
  */
/* ========== Error_Handler ========== */
static void Error_Handler(void)
{
  BSP_LED_On(LED4);
  while (1) {}
}

/* ========== printfをUARTにリダイレクト(任意) ========== */
int _write(int file, char *ptr, int len)
{
  HAL_UART_Transmit(&huart1, (uint8_t *)ptr, len, HAL_MAX_DELAY);
  return len;
}
/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow : 
  *            System Clock source            = PLL (HSE)
  *            SYSCLK(Hz)                     = 180[MHz]
  *            HCLK(Hz)                       = 180[MHz]
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 4
  *            APB2 Prescaler                 = 2
  *            HSE Frequency(Hz)              = 8[MHz]
  *            PLL_M                          = 8
  *            PLL_N                          = 360
  *            PLL_P                          = 2
  *            PLL_Q                          = 7
  *            VDD(V)                         = 3.3
  *            Main regulator output voltage  = Scale1 mode
  *            Flash Latency(WS)              = 5
  * @PAram  None
  * @retval None
  */

/**
 * Duty=Pulse/(Period+1)
 * fPWM=fTIM/(Prescaler+1)(Period+1)
 */

void MX_TIM1_Init(void)
{
	__HAL_RCC_TIM1_CLK_ENABLE(); //TIM1クロックON

	htim1.Instance = TIM1;
	htim1.Init.Prescaler = 0; //Prescaler=0
	htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
	htim1.Init.Period = 7; //Period=7,fPWM=22.5[MHz]
	htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	htim1.Init.RepetitionCounter = 0;
	HAL_TIM_PWM_Init(&htim1);

	TIM_OC_InitTypeDef sConfigOC = {0};
	sConfigOC.OCMode = TIM_OCMODE_PWM1;
	sConfigOC.Pulse = 4; //Pulse=4,PWM=50[%]
	sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
	sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
	HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);

	// GPIO設定:PA8をTIM1_CH1として出力
	__HAL_RCC_GPIOA_CLK_ENABLE();
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pin = GPIO_PIN_8;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void Start_XCLK(void)
{
	HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); //PWM出力開始
}

static void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};

  /* Enable Power Control clock */
  __HAL_RCC_PWR_CLK_ENABLE();

  /* The voltage scaling allows optimizing the power consumption when the device is 
     clocked below the maximum system frequency, to update the voltage scaling value 
     regarding system frequency refer to product datasheet.  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /* Enable HSE Oscillator and activate PLL with HSE as source */
  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 = 4;
  RCC_OscInitStruct.PLL.PLLN = 72;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 3;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  /* Activate the Over-Drive mode */
  HAL_PWREx_EnableOverDrive();
  
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK
		                        | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @PAram  file: pointer to the source file name
  * @PAram  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/**
  * @}
  */

 

chachamaru_0-1762764753839.png

 

Fig.1 Display result

chachamaru_1-1762764754652.png

 

Fig.2 PLL configuration

chachamaru_2-1762764753839.png

 

Fig.3 Baud rate (115200)  on Teraterm

chachamaru_3-1762764753837.png

 

Fig.4 Baundrate on project

1 ACCEPTED SOLUTION

Accepted Solutions
Andrew Neil
Super User

You forgot to link to your previous thread: Unable to display messages on Tera Term via USART1 on STM32F429I-Discovery

 

The fact that you are now seeing anything on your TeraTerm shows that you do now have connectivity from your STM32 all the way through to TeraTerm.

Getting garbled characters (almost) certainly means that the baud rate is wrong.

So check your assumptions carefully: eg, is your clock frequency really what you think it is ?

Use an oscilloscope to see what baud rate is actually happening on the wires.

 

PS:

See this Knowledge Base article for how to output your device's internal clock to the MCO pin - where you can measure & check it:

How can I output a clock signal to the STM32’s MCO pin?

 

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

View solution in original post

4 REPLIES 4
Andrew Neil
Super User

You forgot to link to your previous thread: Unable to display messages on Tera Term via USART1 on STM32F429I-Discovery

 

The fact that you are now seeing anything on your TeraTerm shows that you do now have connectivity from your STM32 all the way through to TeraTerm.

Getting garbled characters (almost) certainly means that the baud rate is wrong.

So check your assumptions carefully: eg, is your clock frequency really what you think it is ?

Use an oscilloscope to see what baud rate is actually happening on the wires.

 

PS:

See this Knowledge Base article for how to output your device's internal clock to the MCO pin - where you can measure & check it:

How can I output a clock signal to the STM32’s MCO pin?

 

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

@Andrew Neil wrote:

Getting garbled characters (almost) certainly means that the baud rate is wrong.


I have noticed with TeraTerm that receiving "junk" data (eg, due to wrong baud rate) can sometimes put it into some weird mode where it will continue to show "junk" even if you do correct the baud rate.

This is fixed by restarting TeraTerm. Therefore, make sure you always start with a fresh session - don't just leave it open.

 

#TeraTermConfused #TeraTermJunk

PS:

Please see How to insert source code - not as screenshots.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
mƎALLEm
ST Employee

Hello,

You've omitted this information:

mALLEm_0-1762769549678.png

How did you port the USART example to your Camera project?

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Andrew Neil
Super User

@chachamaru I see you've marked a solution - good to hear that it's fixed.

 

Again, it would be helpful to say what, exactly, was wrong in your project - this would help future readers who may find this because they are having the same problem ...

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.