cancel
Showing results for 
Search instead for 
Did you mean: 

Failure when Configuring UART1 in the STM32C031C6

hgalindo
Associate II

I'm using the STM32C031C6 configured with USART1 in 115200. When opening communication with the YAT serial terminal, the bytes are sent and received wrong. (I used STMCubeMX for code generation).

Ex: (YAT configured correctly as it works on an older project)

  • sending from YAT to uC 55h I get b5h
  • sending from uC to YAT 55h I get varied values (95h, D5h, B5h together with message XXh [Warning: RX FRAMING ERROR]). When I setup RX and TX in loop, on YAT I always get D5h. uC always puts 0x80 in the 4 most significant bits of the received byte 55h + 80h = D5h (happens for other data) or TX sends data before RX is ready or something like that...

Tests:

  • BaudRate from 115200 to 9600 on both sides (uC and YAT);
  • WordLength 8 and 9 bits;
  • StopBits 1 and 2 on both sides (uC and YAT);
  • Parity None and Odd;
  • ClockPrescaler for multiple values; and
  • I use internal oscillator, so I used PCLK from 3MHz to 12MHz and the error persists.

As commented above, with RX and TX in loop on the board, the transmitted and received signal in debug mode is perfect (55h sending and 55h receiving).

The electrical signal is perfect, very well defined and with no rings and/or noise.

My config (main.c):

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 = 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;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

My config (stm32c0xx_hal_msp.c):

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

  /* USER CODE END USART1_MspInit 0 */

  /** Initializes the peripherals clocks
  */
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
    PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
      Error_Handler();
    }

    /* Peripheral clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PA0     ------> USART1_TX
    PA1     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = uC_USART1_TX_Pin|uC_USART1_RX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART1 interrupt Init */
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }

}

I need help please!

Hernani.

1 ACCEPTED SOLUTION

Accepted Solutions
hgalindo
Associate II

Hello,

Problem solved. I worked from the RCC parameters, on the HSI calibration values. This solved my problem and now the UART works correctly.

Thank you for your cooperation!

View solution in original post

5 REPLIES 5

> I use internal oscillator,

What's that, HSI? It's obviously not stable enough. That can't be remedied by changing baudrate or clock prescalers.

JW

Hello,

Thanks for your answer!

Yes, I'm using HSI and PCLK=12MHz.

I selected autobaud rate with lower HSI and it "worked". With some errors, but communicated.
The internal oscillator is really noisy (very high jitter). Application AN3155/2606 talks about this.

Can I have a robust UART using internal oscillator? The design I'm working on is not external component ready.

Datasheet for STM32C031 gives HSI48 frequency drift as +-1% at 0-85deg.C, that should be good enough for UART.

I'm not sure what's the cause of the problems you experience. Review your connections, especially ground, between the STM32 and whatever you use to connect to PC (an USART-USB converter?) Check waveforms and baudrate using oscilloscope/logic analyzer.

JW

hgalindo
Associate II

Hello,

Problem solved. I worked from the RCC parameters, on the HSI calibration values. This solved my problem and now the UART works correctly.

Thank you for your cooperation!

Thanks for letting us know, but this is unsettling. The HSI should be calibrated within 1% from factory.

@S.Ma, you are the resident 'C0 guru here, have you encountered such problem?

Thanks,

JW