Page 1 of 2
USART Code-snippets
Posted: Fri Mar 30, 2012 9:29 pm
by PavloT
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.cEach USART channel available as SDx object.
To use it you need to enable it in the
halconf.hCode: 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?
Re: USART Code-snippets
Posted: Fri Mar 30, 2012 9:40 pm
by Giovanni
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
Re: USART Code-snippets
Posted: Sun Apr 08, 2012 9:11 pm
by PavloT
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:
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);
Re: USART Code-snippets
Posted: Sun Apr 08, 2012 9:52 pm
by Giovanni
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
Re: USART Code-snippets
Posted: Wed Apr 18, 2012 9:45 am
by hoppel
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
Re: USART Code-snippets
Posted: Wed Apr 18, 2012 9:58 am
by Giovanni
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
Re: USART Code-snippets
Posted: Fri Jan 23, 2015 9:02 am
by Chudik
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.
Re: USART Code-snippets
Posted: Fri Jan 23, 2015 9:23 am
by Giovanni
Hi,
You can have Serial and UART drivers active at the same time but not on the same physical USART.
Giovanni
Re: USART Code-snippets
Posted: Mon Feb 15, 2016 12:33 am
by ceremcem
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.
Re: USART Code-snippets
Posted: Tue Oct 04, 2016 2:14 am
by Chudik
Default bitrate set to 38400.
Where can I change it?