USART Code-snippets

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general. This forum is NOT for support.
PavloT
Posts: 4
Joined: Wed Mar 28, 2012 6:32 pm

USART Code-snippets

Postby PavloT » Fri Mar 30, 2012 9:29 pm

ChibiOS is a new platform for me, so while I'm studying it I'd like to make some notes about it usecases.
First problem I faced: there is no clean examples on USART usage, so here is my assumptions.
The easiest way to start with it - use SerialDriver. Each platform has it's own implementation for it with default settings.
E.g. os/hal/platforms/STM32/serial_lld.c
Each USART channel available as SDx object.
To use it you need to enable it in the halconf.h

Code: Select all

#if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__)
#define HAL_USE_SERIAL              TRUE
#endif


After this it can be used in the main thread (this example is a simple echo):

Code: Select all

/*
   * Activates the serial driver 1 using the driver default configuration.
   */
  sdStart(&SD1, NULL);

  u8 i = 0;
  while (TRUE) {
      i = chIOGet(&SD1);
      if(i!=0)
        chIOPut(&SD1, i);
  }


This is the most basic example, but now I have some questions:
1. What pitfalls we have here?
2. How multithreading and blocking works? I mean what user must expect when working with USART over SerialDriver objects.
3. Also I propose to discuss about lowlevel USART objects, when they should be used?

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

Re: USART Code-snippets

Postby Giovanni » Fri Mar 30, 2012 9:40 pm

Few more notes:
- In mcuconf.h you can individually enable and disable the single USARTs, this way you can save space disabling those you don't need.
- The driver implements circular buffers so you the timing of reads/writes is often not critical.

Try to answer to your question #2, it involves an important mechanism in ChibiOS.

Giovanni

PavloT
Posts: 4
Joined: Wed Mar 28, 2012 6:32 pm

Re: USART Code-snippets

Postby PavloT » Sun Apr 08, 2012 9:11 pm

chIO* functions are blocking. This means that thread which call it will be frozen until this function successfully ended.
chIOPutWouldBlock could be used to verify whether call to chIO* will block thread or not.

It is possible to use chIO*Timeout() this function has second paramether @time which declare tick count for blocking.
chIO(Put\Get) are the macroses to call chIO(Put\Get)Timeout() with second parameter set to TIME_INFINITE
If it will be called with value TIME_IMMEDIATE then no blocking occurs, e.g.:

Code: Select all

i=chIOGetTimeout(&SD1, TIME_IMMEDIATE);

will try to read one byte from queue.
If no data available Q_TIMEOUT (-1) will be returned. Such code could be used in case if stream doesn't contain data equal Q_TIMEOUT.

chIO* data exchange is buffered. Buffer size defined as following:

Code: Select all

#define SERIAL_BUFFERS_SIZE 16

Application must be quick enough to obtain data from input queue.
If data is not read, it will be overwritten by new data from stream, am I right?

I still don't know how to work with channel events, I saw in the forum code like this:

Code: Select all

chEvtRegister(chIOGetEventSource(&SD4), &s4EventListener, 1);

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

Re: USART Code-snippets

Postby Giovanni » Sun Apr 08, 2012 9:52 pm

Hi,

If incoming data is not removed from the input queue fast enough then the queue is not overwritten but incoming data is lost, also, an overflow flag is added to the errors mask and the event source is signaled.

Giovanni

hoppel
Posts: 3
Joined: Wed Apr 18, 2012 9:09 am

Re: USART Code-snippets

Postby hoppel » Wed Apr 18, 2012 9:45 am

Hello @ all,

i use Chibi since one month on Cortex M3/M4. So i have a question about using UART. The serial device is no problem, that works fine.

What i want: i want ust the RX only. And i have to measure the time between two bytes which arrives. My Problem is to understand,
which Callback funktion i have to use.

Here is my current code, where i try to figure out, how to use the uart.

Code: Select all

UARTConfig uart_cfg = {
   NULL,                    /* End of Transmission buffer callback               */
   NULL,                    /* Physical end of transmission callback             */
   NULL,                    /* Receive buffer filled callback                    */   
   RcvCallback,             /* Char received while out of the UART_RECEIVE state */
   NULL,                    /* Receive error callback                            */
   115200,                  /* Baudrate                                          */
   0,                       /* cr1 register values                               */
   0,                       /* cr2 register values                               */
   0                        /* cr3 register values                               */
};

static void RcvSpektrumCallback(void)
{
   uint32_t buf=0;
   uartStartReceive(&UARTD2, 1, &buf);
}


How i could measure the time between two bytes arriving?

Regards,
Andre

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

Re: USART Code-snippets

Postby Giovanni » Wed Apr 18, 2012 9:58 am

Hi Andre,

You choose the correct callback, that one is invoked each time a frame is received (do not call uartStartReceive(), it is invoked anyway, see the driver state diagram).

If the resolution of 1mS is sufficient then you just need to read the system time using chTimeNow(), if you need a more accurate measurement then you can read the RT counter implemented in the STM32 HAL using halGetCounterValue() which is clock cycle-accurate.

Consider that both counters are 32 bits wide so there is a limit to the intervals they can measure.

Giovanni

User avatar
Chudik
Posts: 152
Joined: Fri Jan 16, 2015 7:51 am
Location: California
Has thanked: 7 times
Been thanked: 1 time

Re: USART Code-snippets

Postby Chudik » Fri Jan 23, 2015 9:02 am

What is a relationship between Serial driver finctions and UART driver functions? Is there possible to open SD device and use it somehow with uart functions?
The reason is - Serial driver operates with 32 bit queue data and UART drivers operate with more familiar byte data that is more convenient for sending text.

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

Re: USART Code-snippets

Postby Giovanni » Fri Jan 23, 2015 9:23 am

Hi,

You can have Serial and UART drivers active at the same time but not on the same physical USART.

Giovanni

ceremcem
Posts: 67
Joined: Mon Aug 10, 2015 6:57 am
Has thanked: 7 times
Been thanked: 6 times

Re: USART Code-snippets

Postby ceremcem » Mon Feb 15, 2016 12:33 am

I wonder why there is no sample code other than `sdStart(&SD1, NULL);` on anywhere. I can not figure out how to setup serial port other "8N1" configuration.

User avatar
Chudik
Posts: 152
Joined: Fri Jan 16, 2015 7:51 am
Location: California
Has thanked: 7 times
Been thanked: 1 time

Re: USART Code-snippets

Postby Chudik » Tue Oct 04, 2016 2:14 am

Default bitrate set to 38400.
Where can I change it?


Return to “User Projects”

Who is online

Users browsing this forum: No registered users and 7 guests