Skip to main content
yldzmuhammed
Associate III
October 28, 2021
Question

STM32L151CBU UART 8E1 (Parity) Problem

  • October 28, 2021
  • 3 replies
  • 3283 views

Hello everyone,

I have created a project with STM32L151CBU and used UART2 (PA2, PA3) for communication with some other transceiver IC.

I have configured my UART as follows for (1-bit start, 8 bits data, 1 bit EVEN parity, and 1 bit stop) communication.

	/** USART2 GPIO Configuration
		* PA1 ------> USART2_RTS
		* PA2 ------> USART2_TX
		* PA3 ------> USART2_RX
	*/
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pin 	 = IOLink_TxEn_Pin;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
	GPIO_InitStruct.Speed	 = GPIO_SPEED_FREQ_VERY_HIGH;
	GPIO_InitStruct.Pin = IOLink_Tx_Pin | IOLink_Rx_Pin;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
	/* USART2 interrupt Init */
	HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
	HAL_NVIC_EnableIRQ(USART2_IRQn);
	
 huart2.Init.BaudRate 	= 38400;
 huart2.Instance 			= USART2;
 huart2.Init.Mode 		= UART_MODE_TX_RX;
 huart2.Init.StopBits 		= UART_STOPBITS_1;
 huart2.Init.Parity 		= UART_PARITY_EVEN;
 huart2.Init.WordLength 	= UART_WORDLENGTH_9B;
 huart2.Init.HwFlowCtl 	= UART_HWCONTROL_NONE;
 huart2.Init.OverSampling = UART_OVERSAMPLING_16;
 HAL_UART_Init(&huart2);
	

I have also enabled the receive interrupt and I am getting some data but that is the problem.

Normally I should receive 0xA2, but I am getting sometimes 0x1FD, and mostly 0x69.

The 0x1FD is ok and normal. Because my transmitter device is tries to send 3 different baud rate. When transmits first baud rate data 0xA2, it means in 38400 baud 0x1FD (including parity). But I don't know about the 0x69.

When try to receive or transmit same data from F0 mcu, there is no problem. But I could not successfully receive with this mcu.

By the way, I am checking the data from mcu's rx pin with logic analyzer and everything is as it is.

What is wrong here? What am I missing?

This topic has been closed for replies.

3 replies

TDK
October 28, 2021

The clock rate is likely off. Since you have a logic analyzer, output MCO or some other known clock source and verify it for accuracy.

> The 0x1FD is ok and normal. 

How are you receiving 9 bits when it's configured for 8 bits? I don't see how transmitting 0xA2 allows you to receive 0x1FD even if the clock rate is off.

> By the way, I am checking the data from mcu's rx pin with logic analyzer and everything is as it is.

Show it.

"If you feel a post has answered your question, please click ""Accept as Solution""."
yldzmuhammed
Associate III
November 1, 2021

Hello,

What do you mean by "The clock rate is likely off."? Do you mean there is something wrong with systick or internal/external clock source?

I am going to share related screenshots here.

TDK
November 1, 2021

Why are you changing the baud rate mid stream here? The logic analyzer says there are issues, but that's because you're changing the baud rate.

You should be able to send a known signal at a known baud rate and view that in the logic analyzer to determine if the clock speed is as you expect. That would be step 1 in analyzing the problem. It looks like you're also sending 0x00 in addition to 0xA2.

> I configured my USART2 to 9 bits. Why did you say that?

You said you received 0x1FD. That's 9 bits.

"If you feel a post has answered your question, please click ""Accept as Solution""."
yldzmuhammed
Associate III
November 2, 2021

I am not sure why did you thinking that I am changing the baud but I am not. At least not on purpose.

I found the problem and what was causing it and I fixed it.

The problem was, somehow baud rate changed after my UART init function. That was why I could not be able to receive the correct value/data.

I found this after doing this but before you said :)

> You should be able to send a known signal at a known baud rate and view that in the logic analyzer to determine if the clock speed is as you expect. 

No, I am not actually. That was about the baud rate issue.

> It looks like you're also sending 0x00 in addition to 0xA2.

Now everything is working fine.

Thank you for your support.

Tesla DeLorean
Guru
November 1, 2021

With the 9-bit/Parity mode you need to read the data register as 16-bit, and then mask the low-order 8-bit, otherwise you will also see the parity bit when it is set.

I've got no reason to believe the L151 implementation is broken.

Perhaps try making a complete/concise demo of your issue rather than these piece-meal snippets which don't convey the complete context of the variables, etc.

Check the divisors and register settings. Check signal polarity expectations.

Check HSE_VALUE define.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
yldzmuhammed
Associate III
November 2, 2021

It was all about a library mistakes. Thank you anyway.