uart questions

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
andyturk
Posts: 23
Joined: Tue May 01, 2012 7:22 pm

uart questions

Post by andyturk »

1. Is it possible to invoke uartStartReceive() from within a running rxend_cb callback? Seems like something along those lines should be possible. Perhaps uartStartReceiveI because it'll be handling an interrupt?

2. Is it possible to completely flush all receive buffers (including the hardware) so that an application can start up in a known state? For reasons that I don't understand, my receive callback is always getting run on the first context switch after uartStart. Maybe the hardware thinks it has some data?

3. I tried to search the forums for "uart" and "uart callback" but the @#&&@* forum code says "uart" is too common a word and tells me to try again. :cry:
andyturk
Posts: 23
Joined: Tue May 01, 2012 7:22 pm

Re: uart questions

Post by andyturk »

Hmm, well it looks like neither flavor of uartStartReceive is usable within a receive callback. When I try uartStartReceiveI (with all the debug checks turned on), I get a panic like this:

Code: Select all

(gdb) where
#0  port_halt () at /Users/andy/code/chibios/os/ports/GCC/ARMCMx/chcore.c:50
#1  0x08000c48 in chDbgPanic (msg=0x8007758 "SV#10") at /Users/andy/code/chibios/os/kernel/src/chdebug.c:274
#2  0x08000bfc in chDbgCheckClassI () at /Users/andy/code/chibios/os/kernel/src/chdebug.c:196
#3  0x08002fb0 in uartStartReceiveI (uartp=0x20000aa8, n=2, rxbuf=0x200021a1) at /Users/andy/code/chibios/os/hal/src/uart.c:282
#4  0x08006532 in H4Base::rx_complete (this=0x20000ac8) at h4.cpp:89
#5  0x08006382 in H4Base::rx_complete_cb (d=0x20000aa8) at h4.cpp:43
#6  0x08004444 in uart_lld_serve_rx_end_irq (uartp=0x20000aa8, flags=48) at /Users/andy/code/chibios/os/hal/platforms/STM32/uart_lld.c:226
#7  0x08003144 in Vector70 () at /Users/andy/code/chibios/os/hal/platforms/STM32F4xx/stm32_dma.c:165
#8  0xfffffffc in ?? ()
#9  0xfffffffc in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)


The comment for SV#10 is "misplaced I-class function." That seems odd since it's clearly being called from an interrupt handler (Vector70) and *I routines are made just for that, or so I've been assuming.

When I try the undecorated uartStartReceive, it chokes at a slightly different spot:

Code: Select all

#0  port_halt () at /Users/andy/code/chibios/os/ports/GCC/ARMCMx/chcore.c:50
#1  0x08000c48 in chDbgPanic (msg=0x80076b8 "SV#4") at /Users/andy/code/chibios/os/kernel/src/chdebug.c:274
#2  0x08000a3c in dbg_check_lock () at /Users/andy/code/chibios/os/kernel/src/chdebug.c:117
#3  0x08002f56 in uartStartReceive (uartp=0x20000aa8, n=2, rxbuf=0x200021a1) at /Users/andy/code/chibios/os/hal/src/uart.c:259
#4  0x080064c2 in H4Base::rx_complete (this=0x20000ac8) at h4.cpp:89
#5  0x08006312 in H4Base::rx_complete_cb (d=0x20000aa8) at h4.cpp:43
#6  0x080043d4 in uart_lld_serve_rx_end_irq (uartp=0x20000aa8, flags=48) at /Users/andy/code/chibios/os/hal/platforms/STM32/uart_lld.c:226
#7  0x080030d4 in Vector70 () at /Users/andy/code/chibios/os/hal/platforms/STM32F4xx/stm32_dma.c:165
#8  0xfffffffc in ?? ()
#9  0xfffffffc in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)


How can I kick off another receive operation from a callback?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: uart questions

Post by Giovanni »

SV#10 is triggered because you call an i-class outside a critical zone. You have to use chSysLockFromIsr() and chSysUnlockFromIsr().

2) It is an STM32 small problem, there is always a spurious received character after uartStart() or sdStart(). The workaround is to default the RX pin as input with pull-up in board.h and switch it to alternate AFTER invoking uartStart(). The problem is that when the USART clock is disabled the alternate pin is read as zero and that is interpreted as a spurious start bit when the clock is activated.

3) It is a forum limit that cannot be corrected, use Google with -site:forum.chibios.org

Giovanni
andyturk
Posts: 23
Joined: Tue May 01, 2012 7:22 pm

Re: uart questions

Post by andyturk »

Ah, ok, I misunderstood the I-class and S-class APIs. Using uartStartReceiveI with a chSysLockFromIsr wrapper works fine. Thanks for gently pointing out the mistake.
sntnjrg
Posts: 65
Joined: Mon Jul 22, 2019 2:19 pm
Has thanked: 18 times
Been thanked: 3 times

Re: uart questions

Post by sntnjrg »

Hi,

I have a little problem to use the UART driver. I've looked at the Receiver sub State Machine diagram but I don't know how to invoke the uartStartReceive() function. It gives the impression that it has to be invoked more than once but I don't know where.
Image

In my project, I start the driver and invoke the uartStartReceive() function from this function:

Code: Select all

static void init_uart_driver_(void) {
  if (uart_initializated) {
    return;
  }
  static const UARTConfig c = {NULL,      // End of transmission buffer callback
      NULL,        // Physical end of transmission callback
      rxend_cb_,   // Receive buffer filled callback
      rxchar_cb_,  // Character received while out if the UART_RECEIVE state
      rxerr_cb_,   // Receive error callback
      timeout_cb_, // Receiver timeout callback. Handles both idle and timeout
                   // interrupts depending on configured flags in CR registers and
                   // supported hardware features.
      0,           // Receiver timeout value in terms of number of bit duration.
                   // Set it to 0 when you want to handle idle interrupt instead of
                   // hardware timeout
      UART_DRIVE_SPEED_BAUD_RATE,      // Bit rate
      0,      // Initialization value for the CR1 register.
      USART_CR2_LINEN,      // Initialization value for the CR2 register.
      0      // Initialization value for the CR3 register.
      };

  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);

  reset_char_buffer(rx_block_buffer, RX_BUFFER_SIZE_L4R5ZI);
  uartStartReceive(&UART_DRIVE_L4R5ZI, READING_BLOCK_SIZE_L4R5ZI,
                   rx_block_buffer);
  uart_initializated = true;
}

My buffer(rx_block_buffer) has a size of 80 characters. In the rxend_cb_ callback, I do the following:

Code: Select all

static void rxend_cb_(UARTDriver *uartp) {

  (void)uartp;

  chMtxLock(&mtx_bp_);
  push_buffer_policy(&uart_block_bp, rx_block_buffer);
  chMtxUnlock(&mtx_bp_);
  chEvtSignalI(uart_th_producer, UART_PRODUCER_EVENT_L4R5ZI);
 
}


The content of the rx_block_buffer buffer is stored in a FIFO object and signaled to a thread to tell it to consume what is in the FIFO object.

If I run my program, it only reads once and if I invoke the uartStartReceive() function inside the rxend_cb_ callback, an exception is generated after a reading.

Code: Select all

                _unhandled_exception:
0800029e:   b.n 0x800029e <_unhandled_exception>

In vector.S:

Code: Select all

_unhandled_exception:
.stay:
        b           .stay

Stack;

Code: Select all

Thread [1] (Suspended: Signal 'SIGINT' received. Description: Interrupt.)   
   2 _unhandled_exception() vectors.S:1027 0x0800029e   
   1 VectorFC() vectors.S:1021 0x0800029e   


Where do you have to invoke the uartStartReceive() function?

Thank you.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: uart questions

Post by Giovanni »

You have to call uartStartReceive() at thread level when you want to start receiving bytes, bytes received before calling that function are lost because the driver is unbuffered.

About your code, you cannot use mutexes from callbacks, there you can only call I-class functions, callbacks are called from ISR context.

Please see "kernel concepts" in the RT manual because you are missing some important concepts about API conventions apparently, also make sure to enable assertions and state checker in chconf.h, that would detect most common errors.

Giovanni
Post Reply