cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F072B-Disco HAL Usart problem

cravaour
Associate II
Posted on January 06, 2016 at 17:01

Hi,

I've been trying to make an application which will send and recieve data via USART on STM32F072B-Disco. I managed to generate a project with cubeMX. So far I only achieved transmitting data, but recieving seems a little bit trickier. UsingHAL_USART_Receive() is sending dummy data only. Can I somehow use the function only if the data is being recieved? This is my code:

while (1)
{
if (counter>=100000){
counter=0;
}
if (counter%10000==0){
HAL_USART_Transmit(&husart1,''Hello\n\r'',7,1000);
}
counter++;
HAL_USART_Receive(&husart1,RxBuff,7,1000);
}

USART config is as follows:

void MX_USART1_Init(void)
{
husart1.Instance = USART1;
husart1.Init.BaudRate = 9600;
husart1.Init.WordLength = USART_WORDLENGTH_8B;
husart1.Init.StopBits = USART_STOPBITS_1;
husart1.Init.Parity = USART_PARITY_NONE;
husart1.Init.Mode = USART_MODE_TX_RX;
husart1.Init.CLKPolarity = USART_POLARITY_LOW;
husart1.Init.CLKPhase = USART_PHASE_1EDGE;
husart1.Init.CLKLastBit = USART_LASTBIT_DISABLE;
HAL_USART_Init(&husart1);
}

I just need to send a word to the board, I want it to fiddle with it (like cipher or so), and send it back. Any ideas how to make it work? Any piece of code would be appreciated. #stm32f0-usart-hal-recieve
1 REPLY 1
matthiasboehm9
Associate
Posted on January 08, 2016 at 09:03

Hi Pawel

Probably you already fixed your bug, and I'm not shure if my post is helpful for you. But i had some similar problem some days ago with the same demo-board. I was also able to send via uart, but i could not receive anything. The RDR Register was empty (0x00), and I had some strange RXNE in ISR (it was set mostly everywere, where i placed the first breakpoint in source, and reset one step later). Now, in the end i found the problem: I had some synchronisation problems between my two systems, and because of that, the STM32F0 UART module declared the whole data word as invalid, and does not moved it from the input shift register to the RDR. As I understood, this has to to with the oversampling. The UART interface is able to oversample 8 or 16 times, and because of that, the interface is able to detect, if there is a synchronisation problem which could cause bit-slip. In this case, the received data is declared as invalid, and no data is moved to the RDR. My problem was, that I thought, if there is a sync problem, the input data is probably not correct (for example 0x52 instead of 0x55), but I did not expected, that no data is in the RDR. I managed it to get the data with changing the OneBitSampling to enabled:

huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_ENABLED ;
//huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;

In the end, my working USART init function here:

/* USART1 init function */
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 57600;
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_ONEBIT_SAMPLING_ENABLED ;
//huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
HAL_UART_Init(&huart1);
}

Refer to:

http://codb.coocox.org/component/doc/coocox-master/STM32F030x8_CUBELIB/f72800ad5783981a2f74b13484a08233c5fcf676/structUART__InitTypeDef.html

Probably this problem has nothing to do with your problem, because my baude rate is higher. Anyway, I hope it helps.