Read from UART

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: RoccoMarco, barthess

Amir1980
Posts: 12
Joined: Tue Jan 03, 2023 4:03 pm
Has thanked: 2 times

Read from UART

Postby Amir1980 » Mon Jan 23, 2023 4:07 pm

Hi,

I have two sensors which sends me data in 36 bytes packets, 100Hz, including header bytes and checksums. the baudrates are 115200. and another two sensors with 38400 baudrates and 52 bytes packets, 50Hz. and a uart to receive data from another microcontroller, the baudrate is 115200 and each packet has 84 bytes of data, 10Hz.

my MCU is stm32h743vit6.

what is the best way to read data from these sensors? there are other sensors on my board, I2C and ADC peripherals are used on this board too.

There are two drivers available for UART. Serial Driver and UART driver, which driver do I need to use to read data from interrupts and pass to threads to parse?

Regards,
Amir.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Read from UART

Postby Giovanni » Mon Jan 23, 2023 4:44 pm

Hi,

Use the Serial driver, you just read/write from internal queues, no need to mess with interrupts, it is all done within the driver.

Giovanni

Amir1980
Posts: 12
Joined: Tue Jan 03, 2023 4:03 pm
Has thanked: 2 times

Re: Read from UART

Postby Amir1980 » Mon Jan 23, 2023 4:57 pm

Hi,

Thanks for the reply.

Code: Select all

static THD_WORKING_AREA(waThread3, 256);
static THD_FUNCTION(Thread3, arg) {

  (void)arg;
  chRegSetThreadName("sensor1Receive");
  while (true) {
    sdReadTimeout(&SD2, sensor1Buff, 36, 10);
    chThdSleepMilliseconds(10);
  }
}


Code: Select all

  chThdCreateStatic(waThread3, sizeof(waThread3), NORMALPRIO+1, Thread3, NULL);


is it what you mean? is it the correct and safe way to read continues data?

Regards,
Amir.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Read from UART

Postby Giovanni » Mon Jan 23, 2023 5:38 pm

The function is right, adding intervals is not useful probably, the read function is blocking, also note it handles timeouts (look at the returned value), you need to consider that in your communication protocol.

It is often not so straightforward to get communication done right.

Giovanni

Amir1980
Posts: 12
Joined: Tue Jan 03, 2023 4:03 pm
Has thanked: 2 times

Re: Read from UART

Postby Amir1980 » Tue Jan 24, 2023 8:31 am

Hi,

Is there any none blocking functions to read and write using serial driver?
With blocking functions there is chance to lose data in long time running, especially when there are other sensors to read and write to with high frequency.

Regards,
Amir.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Read from UART

Postby Giovanni » Tue Jan 24, 2023 11:57 am

Using TIME_IMMEDIATE as timeout makes functions non-blocking.

Giovanni


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 6 guests