2023-06-16 12:12 PM
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)
Tests:
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.
Solved! Go to Solution.
2023-06-22 06:21 AM
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!
2023-06-16 01:11 PM
> 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
2023-06-19 06:31 AM
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.
2023-06-21 09:28 AM
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
2023-06-22 06:21 AM
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!
2023-06-22 07:50 AM
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