|
ChibiOS/RT
2.5.1 |
|
Generic Serial Driver.
This module implements a generic full duplex serial driver. The driver implements a SerialDriver interface and uses I/O Queues for communication between the upper and the lower driver. Event flags are used to notify the application about incoming data, outgoing data and other I/O events.
The module also contains functions that make the implementation of the interrupt service routines much easier.
HAL_USE_SERIAL option must be enabled in halconf.h.The driver implements a state machine internally, not all the driver functionalities can be used in any moment, any transition not explicitly shown in the following diagram has to be considered an error and shall be captured by an assertion (if enabled).
Data Structures | |
| struct | SerialDriverVMT |
SerialDriver virtual methods table. More... | |
| struct | SerialDriver |
| Full duplex serial driver class. More... | |
| struct | SerialConfig |
| Generic Serial Driver configuration structure. More... | |
Functions | |
| void | sdInit (void) |
| Serial Driver initialization. | |
| void | sdObjectInit (SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify) |
| Initializes a generic full duplex driver object. | |
| void | sdStart (SerialDriver *sdp, const SerialConfig *config) |
| Configures and starts the driver. | |
| void | sdStop (SerialDriver *sdp) |
| Stops the driver. | |
| void | sdIncomingDataI (SerialDriver *sdp, uint8_t b) |
| Handles incoming data. | |
| msg_t | sdRequestDataI (SerialDriver *sdp) |
| Handles outgoing data. | |
| CH_IRQ_HANDLER (17) | |
| IRQ 17 service routine. | |
| CH_IRQ_HANDLER (18) | |
| IRQ 18 service routine. | |
| CH_IRQ_HANDLER (20) | |
| IRQ 20 service routine. | |
| CH_IRQ_HANDLER (21) | |
| IRQ 21 service routine. | |
| void | sd_lld_init (void) |
| Low level serial driver initialization. | |
| void | sd_lld_start (SerialDriver *sdp, const SerialConfig *config) |
| Low level serial driver configuration and (re)start. | |
| void | sd_lld_stop (SerialDriver *sdp) |
| Low level serial driver stop. | |
Variables | |
| SerialDriver | SD1 |
| UART1 serial driver identifier. | |
| SerialDriver | SD2 |
| UART2 serial driver identifier. | |
| SerialDriver | SD3 |
| UART3 serial driver identifier. | |
Serial status flags | |
| #define | SD_PARITY_ERROR 32 |
| Parity error happened. | |
| #define | SD_FRAMING_ERROR 64 |
| Framing error happened. | |
| #define | SD_OVERRUN_ERROR 128 |
| Overflow happened. | |
| #define | SD_NOISE_ERROR 256 |
| Noise on the line. | |
| #define | SD_BREAK_DETECTED 512 |
| Break detected. | |
Serial configuration options | |
| #define | SERIAL_DEFAULT_BITRATE 38400 |
| Default bit rate. | |
| #define | SERIAL_BUFFERS_SIZE 16 |
| Serial buffers size. | |
Macro Functions | |
| #define | sdPutWouldBlock(sdp) chOQIsFullI(&(sdp)->oqueue) |
Direct output check on a SerialDriver. | |
| #define | sdGetWouldBlock(sdp) chIQIsEmptyI(&(sdp)->iqueue) |
Direct input check on a SerialDriver. | |
| #define | sdPut(sdp, b) chOQPut(&(sdp)->oqueue, b) |
Direct write to a SerialDriver. | |
| #define | sdPutTimeout(sdp, b, t) chOQPutTimeout(&(sdp)->oqueue, b, t) |
Direct write to a SerialDriver with timeout specification. | |
| #define | sdGet(sdp) chIQGet(&(sdp)->iqueue) |
Direct read from a SerialDriver. | |
| #define | sdGetTimeout(sdp, t) chIQGetTimeout(&(sdp)->iqueue, t) |
Direct read from a SerialDriver with timeout specification. | |
| #define | sdWrite(sdp, b, n) chOQWriteTimeout(&(sdp)->oqueue, b, n, TIME_INFINITE) |
Direct blocking write to a SerialDriver. | |
| #define | sdWriteTimeout(sdp, b, n, t) chOQWriteTimeout(&(sdp)->oqueue, b, n, t) |
Direct blocking write to a SerialDriver with timeout specification. | |
| #define | sdAsynchronousWrite(sdp, b, n) chOQWriteTimeout(&(sdp)->oqueue, b, n, TIME_IMMEDIATE) |
Direct non-blocking write to a SerialDriver. | |
| #define | sdRead(sdp, b, n) chIQReadTimeout(&(sdp)->iqueue, b, n, TIME_INFINITE) |
Direct blocking read from a SerialDriver. | |
| #define | sdReadTimeout(sdp, b, n, t) chIQReadTimeout(&(sdp)->iqueue, b, n, t) |
Direct blocking read from a SerialDriver with timeout specification. | |
| #define | sdAsynchronousRead(sdp, b, n) chIQReadTimeout(&(sdp)->iqueue, b, n, TIME_IMMEDIATE) |
Direct non-blocking read from a SerialDriver. | |
Defines | |
| #define | _serial_driver_methods _base_asynchronous_channel_methods |
SerialDriver specific methods. | |
| #define | SD_MODE_PARITY 0x07 |
| Parity field mask. | |
| #define | SD_MODE_PARITY_NONE 0x00 |
| No parity. | |
| #define | SD_MODE_PARITY_EVEN 0x05 |
| Even parity. | |
| #define | SD_MODE_PARITY_ODD 0x07 |
| Odd parity. | |
| #define | SD_MODE_STOP 0x30 |
| Stop bits mask. | |
| #define | SD_MODE_STOP_1 0x00 |
| One stop bit. | |
| #define | SD_MODE_STOP_2 0x20 |
| Two stop bits. | |
| #define | SD_MODE_STOP_1P5 0x30 |
| 1.5 stop bits. | |
| #define | STM8S_SERIAL_USE_UART1 TRUE |
| UART1 driver enable switch. | |
| #define | STM8S_SERIAL_USE_UART2 TRUE |
| UART2 driver enable switch. | |
| #define | STM8S_SERIAL_USE_UART3 TRUE |
| UART3 driver enable switch. | |
| #define | _serial_driver_data |
SerialDriver specific data. | |
| #define | BRR(b) (SYSCLK / (b)) |
| Macro for baud rate computation. | |
Typedefs | |
| typedef struct SerialDriver | SerialDriver |
| Structure representing a serial driver. | |
Enumerations | |
| enum | sdstate_t { SD_UNINIT = 0, SD_STOP = 1, SD_READY = 2 } |
| Driver state machine possible states. More... | |
| void sdInit | ( | void | ) |
Serial Driver initialization.
halInit(), there is no need to explicitly initialize the driver.Definition at line 113 of file serial.c.
References sd_lld_init().
Referenced by halInit().

| void sdObjectInit | ( | SerialDriver * | sdp, |
| qnotify_t | inotify, | ||
| qnotify_t | onotify | ||
| ) |
Initializes a generic full duplex driver object.
The HW dependent part of the initialization has to be performed outside, usually in the hardware initialization code.
| [out] | sdp | pointer to a SerialDriver structure |
| [in] | inotify | pointer to a callback function that is invoked when some data is read from the Queue. The value can be NULL. |
| [in] | onotify | pointer to a callback function that is invoked when some data is written in the Queue. The value can be NULL. |
Definition at line 133 of file serial.c.
References SD_STOP, SERIAL_BUFFERS_SIZE, and SerialDriver::vmt.
Referenced by sd_lld_init().
| void sdStart | ( | SerialDriver * | sdp, |
| const SerialConfig * | config | ||
| ) |
Configures and starts the driver.
| [in] | sdp | pointer to a SerialDriver object |
| [in] | config | the architecture-dependent serial driver configuration. If this parameter is set to NULL then a default configuration is used. |
Definition at line 152 of file serial.c.
References sd_lld_start(), SD_READY, and SD_STOP.

| void sdStop | ( | SerialDriver * | sdp | ) |
Stops the driver.
Any thread waiting on the driver's queues will be awakened with the message Q_RESET.
| [in] | sdp | pointer to a SerialDriver object |
Definition at line 174 of file serial.c.
References sd_lld_stop(), SD_READY, and SD_STOP.

| void sdIncomingDataI | ( | SerialDriver * | sdp, |
| uint8_t | b | ||
| ) |
Handles incoming data.
This function must be called from the input interrupt service routine in order to enqueue incoming data and generate the related events.
| [in] | sdp | pointer to a SerialDriver structure |
| [in] | b | the byte to be written in the driver's Input Queue |
Definition at line 206 of file serial.c.
References SD_OVERRUN_ERROR.
Referenced by CH_IRQ_HANDLER().
| msg_t sdRequestDataI | ( | SerialDriver * | sdp | ) |
Handles outgoing data.
Must be called from the output interrupt service routine in order to get the next byte to be transmitted.
| [in] | sdp | pointer to a SerialDriver structure |
| Q_EMPTY | if the queue is empty (the lower driver usually disables the interrupt source when this happens). |
Definition at line 232 of file serial.c.
Referenced by CH_IRQ_HANDLER().
| CH_IRQ_HANDLER | ( | 17 | ) |
IRQ 17 service routine.
Definition at line 229 of file serial_lld.c.
References sdRequestDataI().

| CH_IRQ_HANDLER | ( | 18 | ) |
IRQ 18 service routine.
Definition at line 250 of file serial_lld.c.
References sdIncomingDataI().

| CH_IRQ_HANDLER | ( | 20 | ) |
IRQ 20 service routine.
Definition at line 272 of file serial_lld.c.
References sdRequestDataI().

| CH_IRQ_HANDLER | ( | 21 | ) |
IRQ 21 service routine.
Definition at line 293 of file serial_lld.c.
References sdIncomingDataI().

| void sd_lld_init | ( | void | ) |
Low level serial driver initialization.
Definition at line 361 of file serial_lld.c.
References sdObjectInit().
Referenced by sdInit().

| void sd_lld_start | ( | SerialDriver * | sdp, |
| const SerialConfig * | config | ||
| ) |
Low level serial driver configuration and (re)start.
| [in] | sdp | pointer to a SerialDriver object |
| [in] | config | the architecture-dependent serial driver configuration. If this parameter is set to NULL then a default configuration is used. |
Definition at line 392 of file serial_lld.c.
Referenced by sdStart().
| void sd_lld_stop | ( | SerialDriver * | sdp | ) |
Low level serial driver stop.
De-initializes the USART, stops the associated clock, resets the interrupt vector.
| [in] | sdp | pointer to a SerialDriver object |
Definition at line 426 of file serial_lld.c.
Referenced by sdStop().
UART1 serial driver identifier.
Definition at line 42 of file serial_lld.c.
UART2 serial driver identifier.
Definition at line 49 of file serial_lld.c.
UART3 serial driver identifier.
Definition at line 56 of file serial_lld.c.
| #define SD_OVERRUN_ERROR 128 |
| #define SERIAL_DEFAULT_BITRATE 38400 |
| #define SERIAL_BUFFERS_SIZE 16 |
Serial buffers size.
Configuration parameter, you can change the depth of the queue buffers depending on the requirements of your application.
Definition at line 74 of file serial.h.
Referenced by sdObjectInit().
| #define _serial_driver_methods _base_asynchronous_channel_methods |
SerialDriver specific methods.
| #define sdPutWouldBlock | ( | sdp | ) | chOQIsFullI(&(sdp)->oqueue) |
Direct output check on a SerialDriver.
| #define sdGetWouldBlock | ( | sdp | ) | chIQIsEmptyI(&(sdp)->iqueue) |
Direct input check on a SerialDriver.
| #define sdPut | ( | sdp, | |
| b | |||
| ) | chOQPut(&(sdp)->oqueue, b) |
Direct write to a SerialDriver.
| #define sdPutTimeout | ( | sdp, | |
| b, | |||
| t | |||
| ) | chOQPutTimeout(&(sdp)->oqueue, b, t) |
Direct write to a SerialDriver with timeout specification.
| #define sdGet | ( | sdp | ) | chIQGet(&(sdp)->iqueue) |
Direct read from a SerialDriver.
| #define sdGetTimeout | ( | sdp, | |
| t | |||
| ) | chIQGetTimeout(&(sdp)->iqueue, t) |
Direct read from a SerialDriver with timeout specification.
| #define sdWrite | ( | sdp, | |
| b, | |||
| n | |||
| ) | chOQWriteTimeout(&(sdp)->oqueue, b, n, TIME_INFINITE) |
Direct blocking write to a SerialDriver.
| #define sdWriteTimeout | ( | sdp, | |
| b, | |||
| n, | |||
| t | |||
| ) | chOQWriteTimeout(&(sdp)->oqueue, b, n, t) |
Direct blocking write to a SerialDriver with timeout specification.
| #define sdAsynchronousWrite | ( | sdp, | |
| b, | |||
| n | |||
| ) | chOQWriteTimeout(&(sdp)->oqueue, b, n, TIME_IMMEDIATE) |
Direct non-blocking write to a SerialDriver.
| #define sdRead | ( | sdp, | |
| b, | |||
| n | |||
| ) | chIQReadTimeout(&(sdp)->iqueue, b, n, TIME_INFINITE) |
Direct blocking read from a SerialDriver.
| #define sdReadTimeout | ( | sdp, | |
| b, | |||
| n, | |||
| t | |||
| ) | chIQReadTimeout(&(sdp)->iqueue, b, n, t) |
Direct blocking read from a SerialDriver with timeout specification.
| #define sdAsynchronousRead | ( | sdp, | |
| b, | |||
| n | |||
| ) | chIQReadTimeout(&(sdp)->iqueue, b, n, TIME_IMMEDIATE) |
Direct non-blocking read from a SerialDriver.
| #define SD_MODE_PARITY 0x07 |
Parity field mask.
Definition at line 38 of file serial_lld.h.
| #define SD_MODE_PARITY_NONE 0x00 |
No parity.
Definition at line 39 of file serial_lld.h.
| #define SD_MODE_PARITY_EVEN 0x05 |
Even parity.
Definition at line 40 of file serial_lld.h.
| #define SD_MODE_PARITY_ODD 0x07 |
Odd parity.
Definition at line 41 of file serial_lld.h.
| #define SD_MODE_STOP 0x30 |
Stop bits mask.
Definition at line 43 of file serial_lld.h.
| #define SD_MODE_STOP_1 0x00 |
One stop bit.
Definition at line 44 of file serial_lld.h.
| #define SD_MODE_STOP_2 0x20 |
Two stop bits.
Definition at line 45 of file serial_lld.h.
| #define SD_MODE_STOP_1P5 0x30 |
1.5 stop bits.
Definition at line 46 of file serial_lld.h.
| #define STM8S_SERIAL_USE_UART1 TRUE |
UART1 driver enable switch.
If set to TRUE the support for UART1 is included.
TRUE. Definition at line 58 of file serial_lld.h.
| #define STM8S_SERIAL_USE_UART2 TRUE |
UART2 driver enable switch.
If set to TRUE the support for UART3 is included.
TRUE. Definition at line 67 of file serial_lld.h.
| #define STM8S_SERIAL_USE_UART3 TRUE |
UART3 driver enable switch.
If set to TRUE the support for UART3 is included.
TRUE. Definition at line 76 of file serial_lld.h.
| #define _serial_driver_data |
_base_asynchronous_channel_data \ /* Driver state.*/ \ sdstate_t state; \ /* Input queue.*/ \ InputQueue iqueue; \ /* Output queue.*/ \ OutputQueue oqueue; \ /* Input circular buffer.*/ \ uint8_t ib[SERIAL_BUFFERS_SIZE]; \ /* Output circular buffer.*/ \ uint8_t ob[SERIAL_BUFFERS_SIZE]; \
SerialDriver specific data.
Definition at line 113 of file serial_lld.h.
| #define BRR | ( | b | ) | (SYSCLK / (b)) |
Macro for baud rate computation.
Definition at line 135 of file serial_lld.h.
| typedef struct SerialDriver SerialDriver |