Page 1 of 2

about uart driver & timeout_cb callback

Posted: Mon Sep 02, 2019 5:57 pm
by sntnjrg
Hi,

I am trying to use the uartReceiveTimeout() function from the UART driver API but I don't know how to use timeout_cb() callback.

My code is:

Code: Select all

static void timeout_cb_(UARTDriver *uartp) {
  (void)uartp;

  uart_error_ = CB_UART_TIMEOUT_DETECTED;
}

static size_t np = READING_BLOCK_SIZE_L4R5ZI;

static void init_uart_driver_(void) {
  static const UARTConfig c = {NULL,
    NULL,
    rxend_cb_,
    NULL,
    rxerr_cb_,
    timeout_cb_,
    0,
    UART_DRIVE_SPEED_BAUD_RATE,
    0,
    USART_CR2_LINEN,
    0
  };
  uartObjectInit(&UART_DRIVE_L4R5ZI);
  uartStart(&UART_DRIVE_L4R5ZI, &c);
  palSetPadMode(
      UART_DRIVE_TX_PORT_L4R5ZI,
      UART_DRIVE_TX_PAD_L4R5ZI,
      PAL_MODE_ALTERNATE(UART_DRIVE_ALTER_FUNC_L4R5ZI) | PAL_STM32_OSPEED_HIGH | PAL_STM32_OTYPE_PUSHPULL);
  palSetPadMode(
      UART_DRIVE_RX_PORT_L4R5ZI,
      UART_DRIVE_RX_PAD_L4R5ZI,
      PAL_MODE_ALTERNATE(UART_DRIVE_ALTER_FUNC_L4R5ZI) | PAL_STM32_OSPEED_HIGH | PAL_STM32_OTYPE_PUSHPULL);
     
  np = READING_BLOCK_SIZE_L4R5ZI;
  uartReceiveTimeout(&UART_DRIVE_L4R5ZI, &np, rx_block_buffer, TIME_MS2I(1));
}


I have verified that np is zero but the callback doesn't run. Shouldn't callback be invoked? When is callback invoked?

When the timeout is completed and I have not received anything, after calling the uartStopReceiveI() function, does the DMA stop?

The driver internally does not have a buffer associated to the DMA, it uses directly what the STM32 provides. Is this so?

Thank you.

Re: about uart driver & timeout_cb callback

Posted: Mon Sep 02, 2019 6:06 pm
by Giovanni
The callback is invoked when the specified number of bytes have been received. The buffer is what you pass to the function. The DMA stops when you call uartStopReceiveI() or when all bytes have been received.

Probably the driver is not receiving bytes, check: GPIO setup, baud rate on both sides, physical connection, etc.

About timeout:

Code: Select all

  /**
   * @brief   Receiver timeout callback.
   * @details Handles both idle and timeout interrupts depending on configured
   *          flags in CR registers and supported hardware features.
   */
  uartcb_t                  timeout_cb;
  /**
   * @brief   Receiver timeout value in terms of number of bit duration.
   * @details Set it to 0 when you want to handle idle interrupt instead of
   *          hardware timeout.
   */
  uint32_t                  timeout;


You are not setting up those fields correctly so no timeout. Details are on the STM32 reference manual, those are register-level settings.

Giovanni

Re: about uart driver & timeout_cb callback

Posted: Tue Sep 03, 2019 10:18 am
by sntnjrg
Hi Giovanni,

I don't understand this field:

Code: Select all

  /**
   * @brief   Receiver timeout value in terms of number of bit duration.
   * @details Set it to 0 when you want to handle idle interrupt instead of
   *          hardware timeout.
   */
  uint32_t                  timeout;


what value do I set here?

Setting a value other than zero invokes the callback.

The configuration is:

Code: Select all

  static const UARTConfig c = {
    NULL,
    NULL,
    rxend_cb_,
    NULL,
    rxerr_cb_,
    timeout_cb_,
    1,
    UART_DRIVE_SPEED_BAUD_RATE,
    USART_CR1_IDLEIE | USART_ISR_IDLE | USART_CR1_RTOIE | USART_ISR_RTOF,
    USART_CR2_LINEN,
    0
  };

Do I get the value from the manual?

Thank you.

Re: about uart driver & timeout_cb callback

Posted: Tue Sep 03, 2019 10:47 am
by Giovanni
Timeout is the initialization value for the RTOR USART register.

Giovanni

Re: about uart driver & timeout_cb callback

Posted: Tue Sep 03, 2019 12:03 pm
by sntnjrg
Hi Giovanni,

What's the advantage between using idle interrupt and hardware timeout?

If ports are used intensively and idle interrupt is used, will the CPU be overhead?

Thank you

Re: about uart driver & timeout_cb callback

Posted: Tue Sep 03, 2019 12:15 pm
by Giovanni
I would not use the HW timeout, there are other ways that are 1) easier 2) more portable.

Giovanni

Re: about uart driver & timeout_cb callback

Posted: Tue Sep 03, 2019 6:26 pm
by sntnjrg
Hi,

A few questions:

1. If my device needs 200 ms to write a complete message and I set a timeout of 250 ms, the idle interrupt will be invoked before the timeout. In this case, what callbacks are executed? rxend_cb_ and timeout_cb_? only timeout_cb_?

2. If the device does not write any more messages on the port, only once will the idle interrupt be executed and the rest of the times will end for the timeout of 250 ms. Is this right?

Thank you.

Re: about uart driver & timeout_cb callback

Posted: Tue Sep 03, 2019 7:25 pm
by Giovanni
Only timeout_cb.

Giovanni

Re: about uart driver & timeout_cb callback

Posted: Thu Oct 31, 2019 12:37 pm
by sntnjrg
Hi,

I have two other questions:

First question.

When I expect to read 80 bytes, I observe the following:

First reading(data: 77 bytes, invoked timeout callback):

Code: Select all

FF 7E 00 43 90 00 7D 33 A2 00 40 66 A3 91 FF FE 02 62 1B 00 06 C8 D3 7D 5D 00 00 F4 3E 38 6E 19 A6 3D 7D 31 27 54 06 00 12 C0 34 26 50 95 59 B8 7D 5E DD 38 30 93 F6 04 7D 5E 38 01 00 00 29 00 1D 80 62 C0 1B 5C AE C7 1B 5C AE C7 F7 00 00 00 

Second reading(data: 5 bytes, invoked timeout callback):

Code: Select all

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7E 00 43 90 00 

Why are the first data written from position 75? This is my rxend_cb callback:

Code: Select all

static void rxend_cb(UARTDriver *uartp) {
  /*
   * This callback is invoked when a receive buffer has been completely written.
   * This callback is called when you call to uartStartReceive()function. In
   * this mode, you only get one interrupt at the end of the whole transfer.
   */
  (void)uartp;
  chSysLockFromISR();
  buffer_push(buffer_ptr_, rx_buffer, BUFFER_PACKET_SIZE);
  if (uartp->rxstate != UART_RX_ACTIVE) {
     uartStartReceiveI(uartp,  sizeof(uint8_t) * BUFFER_PACKET_SIZE,  rx_buffer);
  }
  chSysUnlockFromISR();
}

I start reading after executing this code (rx_buffer is always filled with the value 0 before each reading):

Code: Select all

if (UARTD2.rxstate != UART_RX_ACTIVE) {
   uartStartReceiveI(&UARTD2,  sizeof(uint8_t) * BUFFER_PACKET_SIZE,  rx_buffer);
}

Second question.

When the callback associated with the timeout is invoked, how can I determine if a reading has been made? Is there a record containing the number of items read? How can I distinguish if there is data in the serial port or not?

Thank you

Re: about uart driver & timeout_cb callback

Posted: Thu Oct 31, 2019 1:24 pm
by Giovanni
Hi,

Consider that reception starts when you call uartStartReceiveI(), if some data has already been transmitted then it is lost.

About the first reading: what data did you expect?

About the second: is it possible it received all those zeros then those 5 bytes.

It is possible that the problem is not in the program but something else.

Giovanni