ChibiOS/HAL 9.0.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).
Serial status flags (legacy) | |
#define | SD_PARITY_ERROR CHN_PARITY_ERROR |
#define | SD_FRAMING_ERROR CHN_FRAMING_ERROR |
#define | SD_OVERRUN_ERROR CHN_OVERRUN_ERROR |
#define | SD_NOISE_ERROR CHN_NOISE_ERROR |
#define | SD_BREAK_DETECTED CHN_BREAK_DETECTED |
#define | SD_QUEUE_FULL_ERROR CHN_BUFFER_FULL_ERROR |
Serial configuration options | |
#define | SERIAL_DEFAULT_BITRATE 38400 |
Default bit rate. | |
#define | SERIAL_BUFFERS_SIZE 16 |
Serial buffers size. |
Macro Functions | |
#define | sdPutI(sdp, b) |
Direct write to a SerialDriver . | |
#define | sdPut(sdp, b) |
Direct write to a SerialDriver . | |
#define | sdPutTimeout(sdp, b, t) |
Direct write to a SerialDriver with timeout specification. | |
#define | sdGetI(sdp) |
Direct read from a SerialDriver . | |
#define | sdGet(sdp) |
Direct read from a SerialDriver . | |
#define | sdGetTimeout(sdp, t) |
Direct read from a SerialDriver with timeout specification. | |
#define | sdWriteI(sdp, b, n) |
Direct non-blocking write to a SerialDriver . | |
#define | sdWrite(sdp, b, n) |
Direct blocking write to a SerialDriver . | |
#define | sdWriteTimeout(sdp, b, n, t) |
Direct blocking write to a SerialDriver with timeout specification. | |
#define | sdAsynchronousWrite(sdp, b, n) |
Direct non-blocking write to a SerialDriver . | |
#define | sdReadI(sdp, b, n) |
Direct non-blocking read from a SerialDriver . | |
#define | sdRead(sdp, b, n) |
Direct blocking read from a SerialDriver . | |
#define | sdReadTimeout(sdp, b, n, t) |
Direct blocking read from a SerialDriver with timeout specification. | |
#define | sdAsynchronousRead(sdp, b, n) |
Direct non-blocking read from a SerialDriver . |
PLATFORM configuration options | |
#define | PLATFORM_SERIAL_USE_USART1 FALSE |
USART1 driver enable switch. |
Data Structures | |
struct | SerialDriverVMT |
SerialDriver virtual methods table. More... | |
struct | hal_serial_driver |
Full duplex serial driver class. More... | |
struct | hal_serial_config |
PLATFORM Serial Driver configuration structure. More... |
Macros | |
#define | _serial_driver_methods _base_asynchronous_channel_methods |
SerialDriver specific methods. | |
#define | _serial_driver_data |
SerialDriver specific data. |
Typedefs | |
typedef struct hal_serial_driver | SerialDriver |
Structure representing a serial driver. | |
typedef struct hal_serial_config | SerialConfig |
PLATFORM Serial Driver configuration structure. |
Enumerations | |
enum | sdstate_t { SD_UNINIT = 0 , SD_STOP = 1 , SD_READY = 2 } |
Driver state machine possible states. More... |
Functions | |
static size_t | _write (void *ip, const uint8_t *bp, size_t n) |
static size_t | _read (void *ip, uint8_t *bp, size_t n) |
static msg_t | _put (void *ip, uint8_t b) |
static msg_t | _get (void *ip) |
static msg_t | _putt (void *ip, uint8_t b, sysinterval_t timeout) |
static msg_t | _gett (void *ip, sysinterval_t timeout) |
static size_t | _writet (void *ip, const uint8_t *bp, size_t n, sysinterval_t timeout) |
static size_t | _readt (void *ip, uint8_t *bp, size_t n, sysinterval_t timeout) |
static msg_t | _ctl (void *ip, unsigned int operation, void *arg) |
void | sdInit (void) |
Serial Driver initialization. | |
void | sdObjectInit (SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify) |
Initializes a generic serial driver object. | |
msg_t | 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. | |
bool | sdPutWouldBlock (SerialDriver *sdp) |
Direct output check on a SerialDriver . | |
bool | sdGetWouldBlock (SerialDriver *sdp) |
Direct input check on a SerialDriver . | |
msg_t | sdControl (SerialDriver *sdp, unsigned int operation, void *arg) |
Control operation on a serial port. | |
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 | |
static const struct SerialDriverVMT | vmt |
SerialDriver | SD1 |
USART1 serial driver identifier. | |
static const SerialConfig | default_config |
Driver default configuration. |
#define SD_PARITY_ERROR CHN_PARITY_ERROR |
Definition at line 38 of file hal_serial.h.
#define SD_FRAMING_ERROR CHN_FRAMING_ERROR |
Definition at line 39 of file hal_serial.h.
#define SD_OVERRUN_ERROR CHN_OVERRUN_ERROR |
Definition at line 40 of file hal_serial.h.
#define SD_NOISE_ERROR CHN_NOISE_ERROR |
Definition at line 41 of file hal_serial.h.
#define SD_BREAK_DETECTED CHN_BREAK_DETECTED |
Definition at line 42 of file hal_serial.h.
#define SD_QUEUE_FULL_ERROR CHN_BUFFER_FULL_ERROR |
Definition at line 43 of file hal_serial.h.
Referenced by sdIncomingDataI().
#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 ) |
Direct write to a SerialDriver
.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | the byte value to be written in the output queue |
MSG_OK | if the operation succeeded. |
MSG_TIMEOUT | if the queue is full. |
Definition at line 151 of file hal_serial.h.
#define sdPut | ( | sdp, | |
b ) |
Direct write to a SerialDriver
.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | the byte value to be written in the output queue |
MSG_OK | if the operation succeeded. |
MSG_RESET | if the SerialDriver has been stopped. |
Definition at line 167 of file hal_serial.h.
#define sdPutTimeout | ( | sdp, | |
b, | |||
t ) |
Direct write to a SerialDriver
with timeout specification.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | the byte value to be written in the output queue |
[in] | t | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_OK | if the operation succeeded. |
MSG_TIMEOUT | if the specified time expired. |
MSG_RESET | if the SerialDriver has been stopped. |
Definition at line 188 of file hal_serial.h.
#define sdGetI | ( | sdp | ) |
Direct read from a SerialDriver
.
[in] | sdp | pointer to a SerialDriver object |
MSG_TIMEOUT | if the queue is empty. |
Definition at line 202 of file hal_serial.h.
#define sdGet | ( | sdp | ) |
Direct read from a SerialDriver
.
[in] | sdp | pointer to a SerialDriver object |
MSG_RESET | if the SerialDriver has been stopped. |
Definition at line 216 of file hal_serial.h.
#define sdGetTimeout | ( | sdp, | |
t ) |
Direct read from a SerialDriver
with timeout specification.
[in] | sdp | pointer to a SerialDriver object |
[in] | t | the number of ticks before the operation timeouts, the following special values are allowed:
|
MSG_TIMEOUT | if the specified time expired. |
MSG_RESET | if the SerialDriver has been stopped. |
Definition at line 235 of file hal_serial.h.
#define sdWriteI | ( | sdp, | |
b, | |||
n ) |
Direct non-blocking write to a SerialDriver
.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | pointer to the data buffer |
[in] | n | the maximum amount of data to be transferred, the value 0 is reserved |
Definition at line 251 of file hal_serial.h.
#define sdWrite | ( | sdp, | |
b, | |||
n ) |
Direct blocking write to a SerialDriver
.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | pointer to the data buffer |
[in] | n | the maximum amount of data to be transferred, the value 0 is reserved |
Definition at line 266 of file hal_serial.h.
#define sdWriteTimeout | ( | sdp, | |
b, | |||
n, | |||
t ) |
Direct blocking write to a SerialDriver
with timeout specification.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | pointer to the data buffer |
[in] | n | the maximum amount of data to be transferred, the value 0 is reserved |
[in] | t | the number of ticks before the operation timeouts, the following special values are allowed:
|
Definition at line 287 of file hal_serial.h.
#define sdAsynchronousWrite | ( | sdp, | |
b, | |||
n ) |
Direct non-blocking write to a SerialDriver
.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | pointer to the data buffer |
[in] | n | the maximum amount of data to be transferred, the value 0 is reserved |
Definition at line 303 of file hal_serial.h.
#define sdReadI | ( | sdp, | |
b, | |||
n ) |
Direct non-blocking read from a SerialDriver
.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | pointer to the data buffer |
[in] | n | the maximum amount of data to be transferred, the value 0 is reserved |
Definition at line 320 of file hal_serial.h.
#define sdRead | ( | sdp, | |
b, | |||
n ) |
Direct blocking read from a SerialDriver
.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | pointer to the data buffer |
[in] | n | the maximum amount of data to be transferred, the value 0 is reserved |
Definition at line 335 of file hal_serial.h.
#define sdReadTimeout | ( | sdp, | |
b, | |||
n, | |||
t ) |
Direct blocking read from a SerialDriver
with timeout specification.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | pointer to the data buffer |
[in] | n | the maximum amount of data to be transferred, the value 0 is reserved |
[in] | t | the number of ticks before the operation timeouts, the following special values are allowed:
|
Definition at line 356 of file hal_serial.h.
#define sdAsynchronousRead | ( | sdp, | |
b, | |||
n ) |
Direct non-blocking read from a SerialDriver
.
[in] | sdp | pointer to a SerialDriver object |
[in] | b | pointer to the data buffer |
[in] | n | the maximum amount of data to be transferred, the value 0 is reserved |
Definition at line 372 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 hal_serial_driver SerialDriver |
Structure representing a serial driver.
Definition at line 97 of file hal_serial.h.
typedef struct hal_serial_config SerialConfig |
PLATFORM Serial Driver configuration structure.
An instance of this structure must be passed to sdStart()
in order to configure and start a serial driver operations.
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.
|
static |
Definition at line 50 of file hal_serial.c.
References oqWriteTimeout(), and TIME_INFINITE.
|
static |
Definition at line 56 of file hal_serial.c.
References iqReadTimeout(), and TIME_INFINITE.
|
static |
Definition at line 62 of file hal_serial.c.
References oqPutTimeout(), and TIME_INFINITE.
|
static |
Definition at line 67 of file hal_serial.c.
References iqGetTimeout(), and TIME_INFINITE.
|
static |
Definition at line 72 of file hal_serial.c.
References oqPutTimeout().
|
static |
Definition at line 77 of file hal_serial.c.
References iqGetTimeout().
|
static |
Definition at line 82 of file hal_serial.c.
References oqWriteTimeout().
|
static |
Definition at line 88 of file hal_serial.c.
References iqReadTimeout().
|
static |
Definition at line 94 of file hal_serial.c.
References CHN_CTL_INVALID, CHN_CTL_NOP, HAL_RET_SUCCESS, HAL_RET_UNKNOWN_CTL, and osalDbgCheck.
Referenced by sdControl().
void sdInit | ( | void | ) |
Serial Driver initialization.
halInit()
, there is no need to explicitly initialize the driver.Definition at line 134 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 serial 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 157 of file hal_serial.c.
References iqObjectInit(), oqObjectInit(), osalEventObjectInit(), SD_STOP, SERIAL_BUFFERS_SIZE, hal_serial_driver::vmt, and vmt.
Referenced by sd_lld_init().
msg_t 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 HAL_RET_SUCCESS, 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 222 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 256 of file hal_serial.c.
References CHN_INPUT_AVAILABLE, chnAddFlagsI, iqIsEmptyI, iqPutI(), MSG_OK, 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 282 of file hal_serial.c.
References CHN_OUTPUT_EMPTY, chnAddFlagsI, MSG_OK, 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 309 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 334 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 358 of file hal_serial.c.
References _ctl().
void sd_lld_init | ( | void | ) |
Low level serial driver initialization.
Definition at line 70 of file hal_serial_lld.c.
References SD1, and 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, SD1, 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 113 of file hal_serial_lld.c.
Referenced by sdStop().
|
static |
Definition at line 116 of file hal_serial.c.
SerialDriver SD1 |
USART1 serial driver identifier.
Definition at line 39 of file hal_serial_lld.c.
Referenced by sd_lld_init(), sd_lld_start(), and sd_lld_stop().
|
static |
Driver default configuration.
Definition at line 49 of file hal_serial_lld.c.
Referenced by sd_lld_start().