ChibiOS/RT
2.5.1
serial.h
Go to the documentation of this file.
00001 /*
00002     ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
00003                  2011,2012 Giovanni Di Sirio.
00004 
00005     This file is part of ChibiOS/RT.
00006 
00007     ChibiOS/RT is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     ChibiOS/RT is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 /**
00022  * @file    serial.h
00023  * @brief   Serial Driver macros and structures.
00024  *
00025  * @addtogroup SERIAL
00026  * @{
00027  */
00028 
00029 #ifndef _SERIAL_H_
00030 #define _SERIAL_H_
00031 
00032 #if HAL_USE_SERIAL || defined(__DOXYGEN__)
00033 
00034 /*===========================================================================*/
00035 /* Driver constants.                                                         */
00036 /*===========================================================================*/
00037 
00038 /**
00039  * @name    Serial status flags
00040  * @{
00041  */
00042 #define SD_PARITY_ERROR         32  /**< @brief Parity error happened.      */
00043 #define SD_FRAMING_ERROR        64  /**< @brief Framing error happened.     */
00044 #define SD_OVERRUN_ERROR        128 /**< @brief Overflow happened.          */
00045 #define SD_NOISE_ERROR          256 /**< @brief Noise on the line.          */
00046 #define SD_BREAK_DETECTED       512 /**< @brief Break detected.             */
00047 /** @} */
00048 
00049 /*===========================================================================*/
00050 /* Driver pre-compile time settings.                                         */
00051 /*===========================================================================*/
00052 
00053 /**
00054  * @name    Serial configuration options
00055  * @{
00056  */
00057 /**
00058  * @brief   Default bit rate.
00059  * @details Configuration parameter, this is the baud rate selected for the
00060  *          default configuration.
00061  */
00062 #if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__)
00063 #define SERIAL_DEFAULT_BITRATE      38400
00064 #endif
00065 
00066 /**
00067  * @brief   Serial buffers size.
00068  * @details Configuration parameter, you can change the depth of the queue
00069  *          buffers depending on the requirements of your application.
00070  * @note    The default is 16 bytes for both the transmission and receive
00071  *          buffers.
00072  */
00073 #if !defined(SERIAL_BUFFERS_SIZE) || defined(__DOXYGEN__)
00074 #define SERIAL_BUFFERS_SIZE         16
00075 #endif
00076 /** @} */
00077 
00078 /*===========================================================================*/
00079 /* Derived constants and error checks.                                       */
00080 /*===========================================================================*/
00081 
00082 #if !CH_USE_QUEUES && !CH_USE_EVENTS
00083 #error "Serial Driver requires CH_USE_QUEUES and CH_USE_EVENTS"
00084 #endif
00085 
00086 /*===========================================================================*/
00087 /* Driver data structures and types.                                         */
00088 /*===========================================================================*/
00089 
00090 /**
00091  * @brief Driver state machine possible states.
00092  */
00093 typedef enum {
00094   SD_UNINIT = 0,                    /**< Not initialized.                   */
00095   SD_STOP = 1,                      /**< Stopped.                           */
00096   SD_READY = 2                      /**< Ready.                             */
00097 } sdstate_t;
00098 
00099 /**
00100  * @brief   Structure representing a serial driver.
00101  */
00102 typedef struct SerialDriver SerialDriver;
00103 
00104 #include "serial_lld.h"
00105 
00106 /**
00107  * @brief   @p SerialDriver specific methods.
00108  */
00109 #define _serial_driver_methods                                              \
00110   _base_asynchronous_channel_methods
00111 
00112 /**
00113  * @extends BaseAsynchronousChannelVMT
00114  *
00115  * @brief   @p SerialDriver virtual methods table.
00116  */
00117 struct SerialDriverVMT {
00118   _serial_driver_methods
00119 };
00120 
00121 /**
00122  * @extends BaseAsynchronousChannel
00123  *
00124  * @brief   Full duplex serial driver class.
00125  * @details This class extends @p BaseAsynchronousChannel by adding physical
00126  *          I/O queues.
00127  */
00128 struct SerialDriver {
00129   /** @brief Virtual Methods Table.*/
00130   const struct SerialDriverVMT *vmt;
00131   _serial_driver_data
00132 };
00133 
00134 /*===========================================================================*/
00135 /* Driver macros.                                                            */
00136 /*===========================================================================*/
00137 
00138 /**
00139  * @name    Macro Functions
00140  * @{
00141  */
00142 /**
00143  * @brief   Direct output check on a @p SerialDriver.
00144  * @note    This function bypasses the indirect access to the channel and
00145  *          checks directly the output queue. This is faster but cannot
00146  *          be used to check different channels implementations.
00147  *
00148  * @see     chIOPutWouldBlock()
00149  * @deprecated
00150  *
00151  * @api
00152  */
00153 #define sdPutWouldBlock(sdp) chOQIsFullI(&(sdp)->oqueue)
00154 
00155 /**
00156  * @brief   Direct input check on a @p SerialDriver.
00157  * @note    This function bypasses the indirect access to the channel and
00158  *          checks directly the input queue. This is faster but cannot
00159  *          be used to check different channels implementations.
00160  *
00161  * @see     chIOGetWouldBlock()
00162  * @deprecated
00163  *
00164  * @api
00165  */
00166 #define sdGetWouldBlock(sdp) chIQIsEmptyI(&(sdp)->iqueue)
00167 
00168 /**
00169  * @brief   Direct write to a @p SerialDriver.
00170  * @note    This function bypasses the indirect access to the channel and
00171  *          writes directly on the output queue. This is faster but cannot
00172  *          be used to write to different channels implementations.
00173  *
00174  * @see     chIOPut()
00175  *
00176  * @api
00177  */
00178 #define sdPut(sdp, b) chOQPut(&(sdp)->oqueue, b)
00179 
00180 /**
00181  * @brief   Direct write to a @p SerialDriver with timeout specification.
00182  * @note    This function bypasses the indirect access to the channel and
00183  *          writes directly on the output queue. This is faster but cannot
00184  *          be used to write to different channels implementations.
00185  *
00186  * @see     chIOPutTimeout()
00187  *
00188  * @api
00189  */
00190 #define sdPutTimeout(sdp, b, t) chOQPutTimeout(&(sdp)->oqueue, b, t)
00191 
00192 /**
00193  * @brief   Direct read from a @p SerialDriver.
00194  * @note    This function bypasses the indirect access to the channel and
00195  *          reads directly from the input queue. This is faster but cannot
00196  *          be used to read from different channels implementations.
00197  *
00198  * @see     chIOGet()
00199  *
00200  * @api
00201  */
00202 #define sdGet(sdp) chIQGet(&(sdp)->iqueue)
00203 
00204 /**
00205  * @brief   Direct read from a @p SerialDriver with timeout specification.
00206  * @note    This function bypasses the indirect access to the channel and
00207  *          reads directly from the input queue. This is faster but cannot
00208  *          be used to read from different channels implementations.
00209  *
00210  * @see     chIOGetTimeout()
00211  *
00212  * @api
00213  */
00214 #define sdGetTimeout(sdp, t) chIQGetTimeout(&(sdp)->iqueue, t)
00215 
00216 /**
00217  * @brief   Direct blocking write to a @p SerialDriver.
00218  * @note    This function bypasses the indirect access to the channel and
00219  *          writes directly to the output queue. This is faster but cannot
00220  *          be used to write from different channels implementations.
00221  *
00222  * @see     chIOWriteTimeout()
00223  *
00224  * @api
00225  */
00226 #define sdWrite(sdp, b, n)                                                  \
00227   chOQWriteTimeout(&(sdp)->oqueue, b, n, TIME_INFINITE)
00228 
00229 /**
00230  * @brief   Direct blocking write to a @p SerialDriver with timeout
00231  *          specification.
00232  * @note    This function bypasses the indirect access to the channel and
00233  *          writes directly to the output queue. This is faster but cannot
00234  *          be used to write to different channels implementations.
00235  *
00236  * @see     chIOWriteTimeout()
00237  *
00238  * @api
00239  */
00240 #define sdWriteTimeout(sdp, b, n, t)                                        \
00241   chOQWriteTimeout(&(sdp)->oqueue, b, n, t)
00242 
00243 /**
00244  * @brief   Direct non-blocking write to a @p SerialDriver.
00245  * @note    This function bypasses the indirect access to the channel and
00246  *          writes directly to the output queue. This is faster but cannot
00247  *          be used to write to different channels implementations.
00248  *
00249  * @see     chIOWriteTimeout()
00250  *
00251  * @api
00252  */
00253 #define sdAsynchronousWrite(sdp, b, n)                                      \
00254   chOQWriteTimeout(&(sdp)->oqueue, b, n, TIME_IMMEDIATE)
00255 
00256 /**
00257  * @brief   Direct blocking read from a @p SerialDriver.
00258  * @note    This function bypasses the indirect access to the channel and
00259  *          reads directly from the input queue. This is faster but cannot
00260  *          be used to read from different channels implementations.
00261  *
00262  * @see     chIOReadTimeout()
00263  *
00264  * @api
00265  */
00266 #define sdRead(sdp, b, n)                                                   \
00267   chIQReadTimeout(&(sdp)->iqueue, b, n, TIME_INFINITE)
00268 
00269 /**
00270  * @brief   Direct blocking read from a @p SerialDriver with timeout
00271  *          specification.
00272  * @note    This function bypasses the indirect access to the channel and
00273  *          reads directly from the input queue. This is faster but cannot
00274  *          be used to read from different channels implementations.
00275  *
00276  * @see     chIOReadTimeout()
00277  *
00278  * @api
00279  */
00280 #define sdReadTimeout(sdp, b, n, t)                                         \
00281   chIQReadTimeout(&(sdp)->iqueue, b, n, t)
00282 
00283 /**
00284  * @brief   Direct non-blocking read from a @p SerialDriver.
00285  * @note    This function bypasses the indirect access to the channel and
00286  *          reads directly from the input queue. This is faster but cannot
00287  *          be used to read from different channels implementations.
00288  *
00289  * @see     chIOReadTimeout()
00290  *
00291  * @api
00292  */
00293 #define sdAsynchronousRead(sdp, b, n)                                       \
00294   chIQReadTimeout(&(sdp)->iqueue, b, n, TIME_IMMEDIATE)
00295 /** @} */
00296 
00297 /*===========================================================================*/
00298 /* External declarations.                                                    */
00299 /*===========================================================================*/
00300 
00301 #ifdef __cplusplus
00302 extern "C" {
00303 #endif
00304   void sdInit(void);
00305   void sdObjectInit(SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify);
00306   void sdStart(SerialDriver *sdp, const SerialConfig *config);
00307   void sdStop(SerialDriver *sdp);
00308   void sdIncomingDataI(SerialDriver *sdp, uint8_t b);
00309   msg_t sdRequestDataI(SerialDriver *sdp);
00310 #ifdef __cplusplus
00311 }
00312 #endif
00313 
00314 #endif /* HAL_USE_SERIAL */
00315 
00316 #endif /* _SERIAL_H_ */
00317 
00318 /** @} */