ChibiOS/HAL
6.1.0
|
Generic Serial Driver. More...
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).
Macros | |
#define | _serial_driver_methods _base_asynchronous_channel_methods |
SerialDriver specific methods. More... | |
#define | _serial_driver_data |
SerialDriver specific data. More... | |
Serial status flags | |
#define | SD_PARITY_ERROR (eventflags_t)32 |
Parity. More... | |
#define | SD_FRAMING_ERROR (eventflags_t)64 |
Framing. More... | |
#define | SD_OVERRUN_ERROR (eventflags_t)128 |
Overflow. More... | |
#define | SD_NOISE_ERROR (eventflags_t)256 |
Line noise. More... | |
#define | SD_BREAK_DETECTED (eventflags_t)512 |
LIN Break. More... | |
#define | SD_QUEUE_FULL_ERROR (eventflags_t)1024 |
Queue full. More... | |
Serial configuration options | |
#define | SERIAL_DEFAULT_BITRATE 38400 |
Default bit rate. More... | |
#define | SERIAL_BUFFERS_SIZE 16 |
Serial buffers size. More... | |
Macro Functions | |
#define | sdPutI(sdp, b) oqPutI(&(sdp)->oqueue, b) |
Direct write to a SerialDriver . More... | |
#define | sdPut(sdp, b) oqPut(&(sdp)->oqueue, b) |
Direct write to a SerialDriver . More... | |
#define | sdPutTimeout(sdp, b, t) oqPutTimeout(&(sdp)->oqueue, b, t) |
Direct write to a SerialDriver with timeout specification. More... | |
#define | sdGetI(sdp) iqGetI(&(sdp)->iqueue) |
Direct read from a SerialDriver . More... | |
#define | sdGet(sdp) iqGet(&(sdp)->iqueue) |
Direct read from a SerialDriver . More... | |
#define | sdGetTimeout(sdp, t) iqGetTimeout(&(sdp)->iqueue, t) |
Direct read from a SerialDriver with timeout specification. More... | |
#define | sdWriteI(sdp, b, n) oqWriteI(&(sdp)->oqueue, b, n) |
Direct blocking write to a SerialDriver . More... | |
#define | sdWrite(sdp, b, n) oqWriteTimeout(&(sdp)->oqueue, b, n, TIME_INFINITE) |
Direct blocking write to a SerialDriver . More... | |
#define | sdWriteTimeout(sdp, b, n, t) oqWriteTimeout(&(sdp)->oqueue, b, n, t) |
Direct blocking write to a SerialDriver with timeout specification. More... | |
#define | sdAsynchronousWrite(sdp, b, n) oqWriteTimeout(&(sdp)->oqueue, b, n, TIME_IMMEDIATE) |
Direct non-blocking write to a SerialDriver . More... | |
#define | sdReadI(sdp, b, n) iqReadI(&(sdp)->iqueue, b, n, TIME_INFINITE) |
Direct blocking read from a SerialDriver . More... | |
#define | sdRead(sdp, b, n) iqReadTimeout(&(sdp)->iqueue, b, n, TIME_INFINITE) |
Direct blocking read from a SerialDriver . More... | |
#define | sdReadTimeout(sdp, b, n, t) iqReadTimeout(&(sdp)->iqueue, b, n, t) |
Direct blocking read from a SerialDriver with timeout specification. More... | |
#define | sdAsynchronousRead(sdp, b, n) iqReadTimeout(&(sdp)->iqueue, b, n, TIME_IMMEDIATE) |
Direct non-blocking read from a SerialDriver . More... | |
PLATFORM configuration options | |
#define | PLATFORM_SERIAL_USE_USART1 FALSE |
USART1 driver enable switch. More... | |
Typedefs | |
typedef struct SerialDriver | SerialDriver |
Structure representing a serial driver. More... | |
Data Structures | |
struct | SerialDriverVMT |
SerialDriver virtual methods table. More... | |
struct | SerialDriver |
Full duplex serial driver class. More... | |
struct | SerialConfig |
PLATFORM Serial Driver configuration structure. More... | |
Functions | |
void | sdInit (void) |
Serial Driver initialization. More... | |
void | sdObjectInit (SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify) |
Initializes a generic full duplex driver object. More... | |
void | sdStart (SerialDriver *sdp, const SerialConfig *config) |
Configures and starts the driver. More... | |
void | sdStop (SerialDriver *sdp) |
Stops the driver. More... | |
void | sdIncomingDataI (SerialDriver *sdp, uint8_t b) |
Handles incoming data. More... | |
msg_t | sdRequestDataI (SerialDriver *sdp) |
Handles outgoing data. More... | |
bool | sdPutWouldBlock (SerialDriver *sdp) |
Direct output check on a SerialDriver . More... | |
bool | sdGetWouldBlock (SerialDriver *sdp) |
Direct input check on a SerialDriver . More... | |
msg_t | sdControl (SerialDriver *sdp, unsigned int operation, void *arg) |
Control operation on a serial port. More... | |
void | sd_lld_init (void) |
Low level serial driver initialization. More... | |
void | sd_lld_start (SerialDriver *sdp, const SerialConfig *config) |
Low level serial driver configuration and (re)start. More... | |
void | sd_lld_stop (SerialDriver *sdp) |
Low level serial driver stop. More... | |
Enumerations |
Variables | |
SerialDriver | SD1 |
USART1 serial driver identifier. More... | |
static const SerialConfig | default_config |
Driver default configuration. More... | |
#define SD_PARITY_ERROR (eventflags_t)32 |
Parity.
Definition at line 38 of file hal_serial.h.
#define SD_FRAMING_ERROR (eventflags_t)64 |
Framing.
Definition at line 39 of file hal_serial.h.
#define SD_OVERRUN_ERROR (eventflags_t)128 |
Overflow.
Definition at line 40 of file hal_serial.h.
#define SD_NOISE_ERROR (eventflags_t)256 |
Line noise.
Definition at line 41 of file hal_serial.h.
#define SD_BREAK_DETECTED (eventflags_t)512 |
LIN Break.
Definition at line 42 of file hal_serial.h.
#define SD_QUEUE_FULL_ERROR (eventflags_t)1024 |
#define SERIAL_DEFAULT_BITRATE 38400 |
Default bit rate.
Configuration parameter, this is the baud rate selected for the default configuration.
Definition at line 60 of file hal_serial.h.
#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 73 of file hal_serial.h.
Referenced by sdObjectInit().
#define _serial_driver_methods _base_asynchronous_channel_methods |
SerialDriver
specific methods.
Definition at line 104 of file hal_serial.h.
#define sdPutI | ( | sdp, | |
b | |||
) | oqPutI(&(sdp)->oqueue, b) |
Direct write to a SerialDriver
.
Definition at line 145 of file hal_serial.h.
#define sdPut | ( | sdp, | |
b | |||
) | oqPut(&(sdp)->oqueue, b) |
Direct write to a SerialDriver
.
Definition at line 155 of file hal_serial.h.
#define sdPutTimeout | ( | sdp, | |
b, | |||
t | |||
) | oqPutTimeout(&(sdp)->oqueue, b, t) |
Direct write to a SerialDriver
with timeout specification.
Definition at line 165 of file hal_serial.h.
#define sdGetI | ( | sdp | ) | iqGetI(&(sdp)->iqueue) |
Direct read from a SerialDriver
.
Definition at line 175 of file hal_serial.h.
#define sdGet | ( | sdp | ) | iqGet(&(sdp)->iqueue) |
Direct read from a SerialDriver
.
Definition at line 185 of file hal_serial.h.
#define sdGetTimeout | ( | sdp, | |
t | |||
) | iqGetTimeout(&(sdp)->iqueue, t) |
Direct read from a SerialDriver
with timeout specification.
Definition at line 195 of file hal_serial.h.
#define sdWriteI | ( | sdp, | |
b, | |||
n | |||
) | oqWriteI(&(sdp)->oqueue, b, n) |
Direct blocking write to a SerialDriver
.
Definition at line 205 of file hal_serial.h.
#define sdWrite | ( | sdp, | |
b, | |||
n | |||
) | oqWriteTimeout(&(sdp)->oqueue, b, n, TIME_INFINITE) |
Direct blocking write to a SerialDriver
.
Definition at line 215 of file hal_serial.h.
#define sdWriteTimeout | ( | sdp, | |
b, | |||
n, | |||
t | |||
) | oqWriteTimeout(&(sdp)->oqueue, b, n, t) |
Direct blocking write to a SerialDriver
with timeout specification.
Definition at line 226 of file hal_serial.h.
#define sdAsynchronousWrite | ( | sdp, | |
b, | |||
n | |||
) | oqWriteTimeout(&(sdp)->oqueue, b, n, TIME_IMMEDIATE) |
Direct non-blocking write to a SerialDriver
.
Definition at line 237 of file hal_serial.h.
#define sdReadI | ( | sdp, | |
b, | |||
n | |||
) | iqReadI(&(sdp)->iqueue, b, n, TIME_INFINITE) |
Direct blocking read from a SerialDriver
.
Definition at line 248 of file hal_serial.h.
#define sdRead | ( | sdp, | |
b, | |||
n | |||
) | iqReadTimeout(&(sdp)->iqueue, b, n, TIME_INFINITE) |
Direct blocking read from a SerialDriver
.
Definition at line 258 of file hal_serial.h.
#define sdReadTimeout | ( | sdp, | |
b, | |||
n, | |||
t | |||
) | iqReadTimeout(&(sdp)->iqueue, b, n, t) |
Direct blocking read from a SerialDriver
with timeout specification.
Definition at line 269 of file hal_serial.h.
#define sdAsynchronousRead | ( | sdp, | |
b, | |||
n | |||
) | iqReadTimeout(&(sdp)->iqueue, b, n, TIME_IMMEDIATE) |
Direct non-blocking read from a SerialDriver
.
Definition at line 279 of file hal_serial.h.
#define PLATFORM_SERIAL_USE_USART1 FALSE |
USART1 driver enable switch.
If set to TRUE
the support for USART1 is included.
FALSE
. Definition at line 48 of file hal_serial_lld.h.
#define _serial_driver_data |
SerialDriver
specific data.
Definition at line 79 of file hal_serial_lld.h.
typedef struct SerialDriver SerialDriver |
Structure representing a serial driver.
Definition at line 97 of file hal_serial.h.
enum sdstate_t |
Driver state machine possible states.
Enumerator | |
---|---|
SD_UNINIT |
Not initialized. |
SD_STOP |
Stopped. |
SD_READY |
Ready. |
Definition at line 88 of file hal_serial.h.
void sdInit | ( | void | ) |
Serial Driver initialization.
halInit()
, there is no need to explicitly initialize the driver.Definition at line 135 of file hal_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 158 of file hal_serial.c.
References iqObjectInit(), oqObjectInit(), osalEventObjectInit(), 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 185 of file hal_serial.c.
References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), 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 MSG_RESET
.
[in] | sdp | pointer to a SerialDriver object |
Definition at line 206 of file hal_serial.c.
References iqResetI(), oqResetI(), osalDbgAssert, osalDbgCheck, osalOsRescheduleS(), osalSysLock(), osalSysUnlock(), 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 240 of file hal_serial.c.
References CHN_INPUT_AVAILABLE, chnAddFlagsI, iqIsEmptyI, iqPutI(), osalDbgCheck, osalDbgCheckClassI, and SD_QUEUE_FULL_ERROR.
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 |
MSG_TIMEOUT | if the queue is empty (the lower driver usually disables the interrupt source when this happens). |
Definition at line 266 of file hal_serial.c.
References CHN_OUTPUT_EMPTY, chnAddFlagsI, oqGetI(), osalDbgCheck, and osalDbgCheckClassI.
bool sdPutWouldBlock | ( | SerialDriver * | sdp | ) |
Direct output check on a SerialDriver
.
[in] | sdp | pointer to a SerialDriver structure |
false | if the next write operation would not block. |
true | if the next write operation would block. |
Definition at line 293 of file hal_serial.c.
References oqIsFullI, osalSysLock(), and osalSysUnlock().
bool sdGetWouldBlock | ( | SerialDriver * | sdp | ) |
Direct input check on a SerialDriver
.
[in] | sdp | pointer to a SerialDriver structure |
false | if the next write operation would not block. |
true | if the next write operation would block. |
Definition at line 318 of file hal_serial.c.
References iqIsEmptyI, osalSysLock(), and osalSysUnlock().
msg_t sdControl | ( | SerialDriver * | sdp, |
unsigned int | operation, | ||
void * | arg | ||
) |
Control operation on a serial port.
[in] | sdp | pointer to a SerialDriver object |
[in] | operation | control operation code |
[in,out] | arg | operation argument |
MSG_OK | in case of success. |
MSG_TIMEOUT | in case of operation timeout. |
MSG_RESET | in case of operation reset. |
Definition at line 342 of file hal_serial.c.
void sd_lld_init | ( | void | ) |
Low level serial driver initialization.
Definition at line 70 of file hal_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 87 of file hal_serial_lld.c.
References default_config, and SD_STOP.
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 114 of file hal_serial_lld.c.
References SD_READY.
Referenced by sdStop().
SerialDriver SD1 |
USART1 serial driver identifier.
Definition at line 39 of file hal_serial_lld.c.
|
static |
Driver default configuration.
Definition at line 49 of file hal_serial_lld.c.
Referenced by sd_lld_start().