Page 1 of 2

Problem with uartSendFullTimeout() function

Posted: Wed Sep 25, 2019 3:49 pm
by sntnjrg
Hi,

I have a function that makes a request and waits for a response. For the request I use the uartSendFullTimeout() function and for the response I use the uartReceiveTimeout()function. In some cases, it is not necessary to wait for a response.

This function sends commands to a device connected to the serial port. The first time, he sent a request but I'm not waiting for a response. The second time, the function waits for a response. In this case, when I try to make the request I get a stack overflow.

The stack is:

Code: Select all

GDB Hardware Debugger (25/9/19 16:22) (Suspended) 
  Thread [1] (Suspended: Signal 'SIGINT' received. Description: Interrupt.)
    13 chSysHalt() chsys.c:206 0x08002570
    12 chSchGoSleepS() chschd.c:313 0x08002570 
    11 chSchGoSleepTimeoutS() chschd.c:383 0x080025da
    10 chThdSuspendTimeoutS() chthreads.c:776 0x08002e94 
    9 osalThreadSuspendTimeoutS() osal.h:760 0x08002e94
    8 uartSendFullTimeout() hal_uart.c:430 0x08002e94
    7 serial_port_send() serial_port.c:76 0x08002e94 
    6 prime_serial_port_send() prime_serial_port.c:130 0x08002e94
    5 prime_send_packet_.part.4() prime.c:229 0x08002e94 
    4 prime_send_packet_() prime.c:239 0x08002f34
    3 prime_set_acq_params_() prime.c:325 0x08002fa2 
    2 prime_prologue() prime.c:430 0x08003f6c
    1 main() main.c:231 0x08003f6c 


It stops at line 385 of the uartReceiveTimeout() function:

Code: Select all

  /* Waiting for result.*/
  msg = osalThreadSuspendTimeoutS(&uartp->threadtx, timeout);
  if (msg != MSG_OK) {
    *np -= uartStopSendI(uartp);
  }
  osalSysUnlock();


This is the function that invokes it:

Code: Select all

msg_t serial_port_send(serial_port_datatype_t *sp, size_t *np,
                       const void *txbuf, sysinterval_t timeout,
                       bool physically) {
  if ((sp == NULL) || (sp->sdp == NULL) || (!sp->is_initialize)) {
    return MSG_RESET ;
  }
  msg_t result;
  if (!physically) {
    result = uartSendTimeout(sp->sdp, np, txbuf, timeout);
  }
  else {
    result = uartSendFullTimeout(sp->sdp, np, txbuf, timeout);
  }
  return result;
}


I don't see what's missing. Can someone help me?
Thank you

Re: Problem with uartSendFullTimeout() function

Posted: Wed Sep 25, 2019 4:00 pm
by Giovanni
You stated it goes in stack overflow, if so then increase the thread stack. It is possible uartSendFullTimeout() takes more stack than the other function.

Giovanni

Re: Problem with uartSendFullTimeout() function

Posted: Thu Sep 26, 2019 9:29 am
by sntnjrg
Hi Giovanni,

In this case, I just need to use the main thread. According to your criteria, what would be the best solution? or What's the best convention to follow?

a) to increase the size of the stack associated with the main thread.
b) always create another thread and provide it with an adequate stack size.

Thank you.

Re: Problem with uartSendFullTimeout() function

Posted: Thu Sep 26, 2019 9:46 am
by Giovanni
You just need to increase the stack of the failing thread.

Giovanni

Re: Problem with uartSendFullTimeout() function

Posted: Sun Sep 29, 2019 5:47 pm
by sntnjrg
Thank you Giovanni,

I have a problem with the uartSendFullTimeout() function. My code is as follows:

Code: Select all

uint8_t bp[15];
size_t n = 15;
uint8_t rbp[5];
size_t rn = 5;
 
// initialize bp ...
 
uartStartReceive(&UARTD2, rn, rbp);                                   //  Start reading
msg_t r =uartSendFullTimeout(&UARTD2, &n, bp, timeout); // Send packet
chThdSleepMilliseconds(100);
rn = uartStopReceive(&UARTD2);                                         // Stop reading

I checked that the n bytes are sent to the serial port but I don't read anything. The same thing happens to me using the uartReceiveTimeout() function. Using the serial driver, I have checked that the device responds when the packet is sent.

In this case, How can I avoid data loss when I read?

Thank you

Re: Problem with uartSendFullTimeout() function

Posted: Sun Sep 29, 2019 5:55 pm
by Giovanni
Is that supposed to be a loopback test?

No clue, it could be anything in the code you didn't post, the timeout for example.

Giovanni

Re: Problem with uartSendFullTimeout() function

Posted: Sun Sep 29, 2019 6:51 pm
by sntnjrg
Hi Giovanni,

I'm using UARTD2. My problem is that I am losing data because the dma does not have the reception line ready.

Imagine you have to send a packet and wait for a response. How would you approach development to avoid the loss of information?

My example is:

Code: Select all


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

static unit8_t test(void) {
  static const UARTConfig c = {
    NULL,
    NULL,
    rxend_cb_,
    NULL,
    NULL,
    timeout_cb_,
    0,
    38400,
    USART_CR1_IDLEIE | USART_ISR_IDLE | USART_CR1_RTOIE | USART_ISR_RTOF,
    USART_CR2_LINEN,
    0
  };

  uartObjectInit(&UARTD2);
  uartStart(&UARTD2, &c);
  palSetPadMode(
      GPIOA,
      2U,
      PAL_MODE_ALTERNATE(7) | PAL_STM32_OSPEED_HIGH | PAL_STM32_OTYPE_PUSHPULL);
  palSetPadMode(
      GPIOA,
      3U,
      PAL_MODE_ALTERNATE(7) | PAL_STM32_OSPEED_HIGH | PAL_STM32_OTYPE_PUSHPULL);

  uint8_t bp[20];
  memset(bp, 0, 20);
  size_t n = 20;
 
  bp[0]  = 0x00;
  bp[1]  = 0x14;
  bp[2]  = 0x18;
  bp[3]  = 0x01;
  bp[4]  = 0x00;
  bp[5]  = 0x00;
  bp[6]  = 0x00;
  bp[7]  = 0x00;
  bp[8]  = 0x00;
  bp[9]  = 0x00;
  bp[10] = 0x00;
  bp[11] = 0x00;
  bp[12] = 0x00;
  bp[13] = 0x49;
  bp[14] = 0x01;
  bp[15] = 0x56;
  bp[16] = 0x00;
  bp[17] = 0x00;
  bp[18] = 0xE2;
  bp[19] = 0x34;

  uint8_t rbp[5];
  memset(rbp, 0, 55);
  size_t rn = 5;
   
  if(uartSendFullTimeout(&UARTD2, &n, bp, TIME_MS2I(1000)) == MSG_RESET) {
     return FALSE;
  }
  if(uartReceiveTimeout(&UARTD2, &rn, rbp, TIME_MS2I(1000))== MSG_RESET) {
     return FALSE;
  }
  if(rn != 5) {
       return FALSE:
  }
  return TRUE;
}


Thank you.

Re: Problem with uartSendFullTimeout() function

Posted: Sun Sep 29, 2019 7:20 pm
by Giovanni
I would implement a state machine for that, sending and receiving fixed size packets will not save you from possible data loss and synchronization problems.

Forget the driver for a moment and design the SM, then implement it.

Giovanni

Re: Problem with uartSendFullTimeout() function

Posted: Mon Sep 30, 2019 11:25 am
by sntnjrg
Hi Giovanni,

I'm using callbacks txend1_cb() and timeout_cb() to implement the state machine. In the first callback I start the reading and in the second I stop it. When I invoke the uartStopReceiveI() function, it returns the value UART_ERR_NOT_ACTIVE because the state is UART_RX_IDLE. However, in the buffer is the response to the request.

Is this behavior due to the buffer size that I specify in the uartStartReceiveI() function? Should the value returned by the uartStopReceiveI() function always be 0 (all bytes have been read)?

Later, my idea is to generate an event when the reading is stopped for the response to be processed. To do this, I need to set a condition that tells me if the bytes I expected have actually been read. My intention is to use the value returned by the uartStopReceiveI() function but I can't because it returns UART_ERR_NOT_ACTIVE.

In a nutshell, this is the code I'm using:

Code: Select all

#define IO_COMMAND_RX_BUFFER_SIZE           5

static uint8_t rx_io_command_buffer[IO_COMMAND_RX_BUFFER_SIZE];
static size_t rx_io_command_n;

static void txend1_cb(UARTDriver *uartp) {
  (void)uartp;
  chSysLockFromISR();
  uartStartReceiveI(&UARTD2, IO_COMMAND_RX_BUFFER_SIZE, rx_io_command_buffer);
  chSysUnlockFromISR();
} // txend1_cb

static void timeout_cb(UARTDriver *uartp) {
  chSysLockFromISR();
   rx_io_command_n = uartStopReceiveI(&UARTD2);
  chSysUnlockFromISR();
} // timeout_cb

static const UARTConfig c = {
    txend1_cb,
    NULL,
    NULL,
    NULL,
    NULL,
    timeout_cb,
    0,
    38400,
    USART_CR1_IDLEIE | USART_ISR_IDLE | USART_CR1_RTOIE | USART_ISR_RTOF,
    USART_CR2_LINEN,
    0
 };
 uartObjectInit(&UARTD2);
 uartStart(&UARTD2, &c);
 palSetPadMode(
      GPIOA, 2U,
      PAL_MODE_ALTERNATE(7) | PAL_STM32_OSPEED_HIGH | PAL_STM32_OTYPE_PUSHPULL);
 palSetPadMode(
      GPIOA, 3U,
      PAL_MODE_ALTERNATE(7) | PAL_STM32_OSPEED_HIGH | PAL_STM32_OTYPE_PUSHPULL);


msg_t result = uartSendFullTimeout(&UARTD2, &n, bp, TIME_MS2I(1000));
chThdSleepMilliseconds(250);
if (rx_io_command_n  == UART_ERR_NOT_ACTIVE) {
    result = MSG_RESET;
 }


Thank you.

Re: Problem with uartSendFullTimeout() function

Posted: Mon Sep 30, 2019 12:17 pm
by Giovanni
Hi,

The returned value should never be zero because the RX callback would already have happened. It returns UART_ERR_NOT_ACTIVE because there is no ongoing operation to stop.

Giovanni