UART error with Kinetis - Page 1
Forum section: electronics / general

author: luiHS
author_rank: Frequent Contributor
timestamp: November 16, 2020, 01:46:59 am
content: Hi.I have a problem using the UART with a Kinetis microcontroller using MCUXpresso.I use this function to receive data in an array of 2 elements:UART_ReadBlocking (DEMO_UART, & rxbuff, 2);The problem is that the Kinetis communicates with a Raspberry by serial port, and when starting both at the same time there is always a little interference that causes incorrect data to be read. In fact this is detected well by the function and returns error code 1011.The point is that if when detecting the error, I run the same function again, it no longer receives correct data, only zeros. I have tried to restart the UART but then incomprehensibly the Rasbperry does not start.This is the source code:while(1){int32_t leo_uart = UART_ReadBlocking(DEMO_UART, &rxbuff, 2);if (rxbuff == 7){printf("\nData received is OK... %d %d %d\r\n", rxbuff, rxbuff, leo_uart);break;}else {delay(15);printf("\nError... %d %d %d\r\n", rxbuff, rxbuff, leo_uart);}In the first data reception that is the result of interference when starting the Raspberry, it returns data to 0 and error code 1011. And in the second data reception that is already with correct data, it returns data to 0 and error code to 0. The correct data to receive would be 42 and 7, and as an error code 0 that indicates that the received data is correct, but it is not.I tried to restart the UART with this source code, but then the Raspberry won't start, this I don't understand why it happens.// Stop UARTUART_Deinit(DEMO_UART);delay(25); // Espera 25ms, para evitar problemas en la UART en el Raspberry// Config UARTuart_config_t config;UART_GetDefaultConfig(&config);config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;config.enableTx = true;config.enableRx = true;// Start UARTUART_Init(DEMO_UART, &config, DEMO_UART_CLK_FREQ);

author: dietert1
author_rank: Super Contributor
timestamp: November 16, 2020, 07:09:03 am
content: Likely there are API calls for error handling, so you can find out what kind of error happened and act to recover. Also after power up and opening a serial interface i remember using a flush() API to force a clean start.Also your understanding of UART communication may be incomplete in the sense that there is nothing like packets. It's just characters and if you want packets you will have to invent something called "protocol", like marking the end of a message with a reserved character, e.g. carriage return.Regards, Dieter

author: TomS_
author_rank: Frequent Contributor
timestamp: November 16, 2020, 07:37:36 am
content: Are you pulling the two tx/rx pins up? Since most devices are CMOS these days you will need to stabilise them or you could end up with junk before both ends are fully operational.The UART will look for a low going pulse and consider that the start of a byte being transmitted to it, and that's entirely possible with a floating CMOS input.

author: luiHS
author_rank: Frequent Contributor
timestamp: November 16, 2020, 10:12:45 am
content: Quote from: TomS_ on November 16, 2020, 07:37:36 amDo you mean add two external pullup resistors to the TX and RX lines ?.Or may be enable internal pullup resistors in MCUXpresso ?.Currently I do not have pullup resistors on the TX or RX lines, I can try to activate the internal pullups in MCUXpresso to see if this way I can avoid the interferences that occur when starting the Raspberry.

author: luiHS
author_rank: Frequent Contributor
timestamp: November 16, 2020, 10:17:24 am
content: Quote from: dietert1 on November 16, 2020, 07:09:03 amThe error is indicated by the function returning the code 1011 which means communication error, corrupt data. The problem is that once the error is detected, I don't know how to restart the UART so that it can receive the following data well, which is already correct, but is not received well.I don't know how I can clear the UART buffer or reset it so that it can receive well after the error. I have looked in the sources of the UART functions and I can't find how to solve it.On the other hand, I don't need to set any control character, I know that I will always receive 2-byte packets. The problem is the interference that occurs when the Raspberry starts up that causes the UART to receive wrong data, that is detected by the function, and then I no longer know how to restart or clean the UART so that it can receive the data that comes later, which already are right.

author: dietert1
author_rank: Super Contributor
timestamp: November 16, 2020, 11:06:14 am
content: No, you receive a stream of characters. You can't just pick two consecutive characters and think they will make a message.Regards, Dieter

author: luiHS
author_rank: Frequent Contributor
timestamp: November 16, 2020, 11:11:30 am
content: Quote from: dietert1 on November 16, 2020, 11:06:14 amYes I can when I know what I expect to receive, which is this case.In addition, the Raspberry sends data when the Kinetis requests it, and it always does so by replying with 2 bytes, so everything is predictable, so I don't need to set any control character.And in fact the system is working perfectly after avoiding the initial interference that is now done simply with a delay before starting the UART.

author: newbrain
author_rank: Super Contributor
timestamp: November 16, 2020, 11:46:17 am
content: Quote from: luiHS (trimmed, emphasis mine) on November 16, 2020, 11:11:30 amUntil it isn't.If you have a disturbance on the line between the two chars, or the Pi SW crashes in the middle of a transmission or whatever, you'll be stuck forever unless you enable the timeout version of the fsl drivers (which, BTW, is quite badly implemented, even ST's HAL makes a better job with timeouts).It might not be important in your case, but (generally speaking) that's quite lazy, unless there are other recovery mechanism (watchdog?).That said, I see another problem in your 'while' above:you check one of the received chars and assume success if it's correct; remember that the blocking read will not update it if it errors out before receiving the new data (e.g. for an overrun for some kinetis UARTs), so you might be checking stale data.

author: voltsandjolts
author_rank: Unknown
timestamp: November 16, 2020, 12:01:28 pm
content: If you are looking for a good structure for uart messages then I would recommend starting with COBS:https://www.eevblog.com/forum/microcontrollers/implementing-uart-data-packets-with-consistent-overhead-byte-stuffing-(cobs)/