cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407VET USART example

BSang
Associate III

Hi community, I'm searching a USART example , STM32F407VET

First, I create code by using STM32CubeMX.

I has try it but it is too hard to use that.

I felt 'HAL UART' example of length of the array that receive and send , has limited. (or need more buffer size)​

It has a some remain data in USART buffer TX or RX.

when I send data to microcontroller(PC -> MCU)  Repeatedly,

Sometimes the UART doesn't work.

It look like a buffer overload or TX/RX buffer has some remain data​

I want to find a best way to use uart(or USART),

to use efficiently.

under code is my code that use 'HAL UART '

​void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {

//bufferData : char bufferData[128]

//Example of Data Format

//#,100,200,300,!,$,100,200,300,%

if (huart->Instance == USART1) {

int bufferCount = 0;

int isStartFlagOn = 0;

HAL_StatusTypeDef RESULT = HAL_OK;

if(receiveddata == '#'){//wait startflag

bufferData[bufferCount++] = '#';

isStartFlagOn = 1;

}

if(isStartFlagOn == 1){

for (;;)//if get startflag, start loop

{

HAL_UART_Receive(&huart1, &receiveddata, 1, 100);//receive data during end

if (receiveddata == '%') { //if end data received, 

bufferData[bufferCount++] = (char) receiveddata;//fill buffer format

HAL_UART_Transmit(&huart1, bufferData, bufferCount, 10);//and Transmit PC

memset(bufferData, 0, bufferCount);//buffer clear

break;

} else {

bufferData[bufferCount] = (char) receiveddata;//if data is not end, fill data

bufferCount++;//count up buffer

}

}

}

else{

memset(bufferData, 0, bufferCount);//startsignal is not 1 clear buffer,

isStartFlagOn=0;//start signal off

}

RESULT = HAL_UART_Receive_IT(&huart1, &receiveddata, 1);

}

}

is any other problem my code?

and there are best examples?

Thanks you

5 REPLIES 5

HAL_UART_Transmit() will block for multiple byte times, not good for an interrupt/callback

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi, Thanks to reply, So that, is any other function replace the HAL_UART_Transmit()?​

I'm not a proponent of the HAL USART implementation. I've posted alternative buffering code.

You'd need to use the IT or DMA variants of HAL_UART_Transmit, but also manage the buffering and dispatch (one active transfer at a time, and sequentially). You might want to manage that in a different "thread" of execution, or the Tx callback. Perhaps consider queuing.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
BSang
Associate III

Thanks, I will try it...

T J
Lead

you can direct message me, on email, nick.marsh007@gmail.com