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