Author Topic: UART error with Kinetis  (Read 2662 times)

0 Members and 1 Guest are viewing this topic.

Offline luiHSTopic starter

  • Frequent Contributor
  • **
  • Posts: 612
  • Country: es
UART error with Kinetis
« on: November 16, 2020, 01:46:59 am »
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[1] == 7)
   {
       printf("\nData received is OK... %d %d %d\r\n", rxbuff[0], rxbuff[1], leo_uart);
          break;
   }
   else {
   delay(15);
        printf("\nError... %d %d %d\r\n", rxbuff[0], rxbuff[1], 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 UART
   UART_Deinit(DEMO_UART);
   delay(25); // Espera 25ms, para evitar problemas en la UART en el Raspberry
   
// Config UART
   uart_config_t config;
   UART_GetDefaultConfig(&config);
   config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
   config.enableTx = true;
   config.enableRx = true;

// Start UART   
UART_Init(DEMO_UART, &config, DEMO_UART_CLK_FREQ);

 

Offline dietert1

  • Super Contributor
  • ***
  • Posts: 3053
  • Country: br
    • CADT Homepage
Re: UART error with Kinetis
« Reply #1 on: November 16, 2020, 07:09:03 am »
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
 

Offline TomS_

  • Frequent Contributor
  • **
  • Posts: 861
  • Country: gb
Re: UART error with Kinetis
« Reply #2 on: November 16, 2020, 07:37:36 am »
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.
 

Offline luiHSTopic starter

  • Frequent Contributor
  • **
  • Posts: 612
  • Country: es
Re: UART error with Kinetis
« Reply #3 on: November 16, 2020, 10:12:45 am »
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.


Do 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.



 

Offline luiHSTopic starter

  • Frequent Contributor
  • **
  • Posts: 612
  • Country: es
Re: UART error with Kinetis
« Reply #4 on: November 16, 2020, 10:17:24 am »
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

The 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.
« Last Edit: November 16, 2020, 10:24:24 am by luiHS »
 

Offline dietert1

  • Super Contributor
  • ***
  • Posts: 3053
  • Country: br
    • CADT Homepage
Re: UART error with Kinetis
« Reply #5 on: November 16, 2020, 11:06:14 am »
No, you receive a stream of characters. You can't just pick two consecutive characters and think they will make a message.

Regards, Dieter
 
The following users thanked this post: newbrain

Offline luiHSTopic starter

  • Frequent Contributor
  • **
  • Posts: 612
  • Country: es
Re: UART error with Kinetis
« Reply #6 on: November 16, 2020, 11:11:30 am »
No, you receive a stream of characters. You can't just pick two consecutive characters and think they will make a message.

Regards, Dieter

Yes 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.
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1920
  • Country: se
Re: UART error with Kinetis
« Reply #7 on: November 16, 2020, 11:46:17 am »
Yes 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
Until 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.
« Last Edit: November 16, 2020, 11:58:46 am by newbrain »
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline voltsandjolts

  • Supporter
  • ****
  • Posts: 3946
  • Country: gb
Re: UART error with Kinetis
« Reply #8 on: November 16, 2020, 12:01:28 pm »
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)/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->