ChibiOS/RT Architecture - Reference Manual - Guides |
00001 /* 00002 ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio. 00003 00004 This file is part of ChibiOS/RT. 00005 00006 ChibiOS/RT is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 ChibiOS/RT is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 00019 --- 00020 00021 A special exception to the GPL can be applied should you wish to distribute 00022 a combined work that includes ChibiOS/RT, without being obliged to provide 00023 the source code for any proprietary components. See the file exception.txt 00024 for full details of how and when the exception can be applied. 00025 */ 00026 00027 /** 00028 * @file serial.h 00029 * @brief Serial Driver macros and structures. 00030 * 00031 * @addtogroup SERIAL 00032 * @{ 00033 */ 00034 00035 #ifndef _SERIAL_H_ 00036 #define _SERIAL_H_ 00037 00038 #if CH_HAL_USE_SERIAL || defined(__DOXYGEN__) 00039 00040 /*===========================================================================*/ 00041 /* Driver constants. */ 00042 /*===========================================================================*/ 00043 00044 /** @brief No pending conditions.*/ 00045 #define SD_NO_ERROR 0 00046 /** @brief Connection happened.*/ 00047 #define SD_CONNECTED 1 00048 /** @brief Disconnection happened.*/ 00049 #define SD_DISCONNECTED 2 00050 /** @brief Parity error happened.*/ 00051 #define SD_PARITY_ERROR 4 00052 /** @brief Framing error happened.*/ 00053 #define SD_FRAMING_ERROR 8 00054 /** @brief Overflow happened.*/ 00055 #define SD_OVERRUN_ERROR 16 00056 /** @brief Noise on the line.*/ 00057 #define SD_NOISE_ERROR 32 00058 /** @brief Break detected.*/ 00059 #define SD_BREAK_DETECTED 64 00060 00061 /*===========================================================================*/ 00062 /* Driver pre-compile time settings. */ 00063 /*===========================================================================*/ 00064 00065 /** 00066 * @brief Default bit rate. 00067 * @details Configuration parameter, this is the baud rate selected for the 00068 * default configuration. 00069 */ 00070 #if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__) 00071 #define SERIAL_DEFAULT_BITRATE 38400 00072 #endif 00073 00074 /** 00075 * @brief Serial buffers size. 00076 * @details Configuration parameter, you can change the depth of the queue 00077 * buffers depending on the requirements of your application. 00078 * @note The default is 64 bytes for both the transmission and receive 00079 * buffers. 00080 */ 00081 #if !defined(SERIAL_BUFFERS_SIZE) || defined(__DOXYGEN__) 00082 #define SERIAL_BUFFERS_SIZE 64 00083 #endif 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, /**< @brief Not initialized. */ 00102 SD_STOP = 1, /**< @brief Stopped. */ 00103 SD_READY = 2 /**< @brief 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 * @brief @p SerialDriver virtual methods table. 00121 */ 00122 struct SerialDriverVMT { 00123 _serial_driver_methods 00124 }; 00125 00126 /** 00127 * @extends BaseAsynchronousChannel 00128 * 00129 * @brief Full duplex serial driver class. 00130 * @details This class extends @p BaseAsynchronousChannel by adding physical 00131 * I/O queues. 00132 */ 00133 struct _SerialDriver { 00134 /** @brief Virtual Methods Table.*/ 00135 const struct SerialDriverVMT *vmt; 00136 _serial_driver_data 00137 }; 00138 00139 /*===========================================================================*/ 00140 /* Driver macros. */ 00141 /*===========================================================================*/ 00142 00143 /** 00144 * @brief Direct output check on a @p SerialDriver. 00145 * @details This function bypasses the indirect access to the channel and 00146 * checks directly the output queue. This is faster but cannot 00147 * be used to check different channels implementations. 00148 * 00149 * @see chIOPutWouldBlock() 00150 */ 00151 #define sdPutWouldBlock(sdp) chOQIsFull(&(sdp)->oqueue) 00152 00153 /** 00154 * @brief Direct input check on a @p SerialDriver. 00155 * @details This function bypasses the indirect access to the channel and 00156 * checks directly the input queue. This is faster but cannot 00157 * be used to check different channels implementations. 00158 * 00159 * @see chIOGetWouldBlock() 00160 */ 00161 #define sdGetWouldBlock(sdp) chIQIsEmpty(&(sdp)->iqueue) 00162 00163 /** 00164 * @brief Direct write to a @p SerialDriver. 00165 * @details This function bypasses the indirect access to the channel and 00166 * writes directly on the output queue. This is faster but cannot 00167 * be used to write to different channels implementations. 00168 * 00169 * @see chIOPut() 00170 */ 00171 #define sdPut(sdp, b) chOQPut(&(sdp)->oqueue, b) 00172 00173 /** 00174 * @brief Direct write to a @p SerialDriver with timeout specification. 00175 * @details This function bypasses the indirect access to the channel and 00176 * writes directly on the output queue. This is faster but cannot 00177 * be used to write to different channels implementations. 00178 * 00179 * @see chIOPutTimeout() 00180 */ 00181 #define sdPutTimeout(sdp, b, t) chOQPutTimeout(&(sdp)->iqueue, b, t) 00182 00183 /** 00184 * @brief Direct read from a @p SerialDriver. 00185 * @details This function bypasses the indirect access to the channel and 00186 * reads directly from the input queue. This is faster but cannot 00187 * be used to read from different channels implementations. 00188 * 00189 * @see chIOGet() 00190 */ 00191 #define sdGet(sdp) chIQGet(&(sdp)->iqueue) 00192 00193 /** 00194 * @brief Direct read from a @p SerialDriver with timeout specification. 00195 * @details This function bypasses the indirect access to the channel and 00196 * reads directly from the input queue. This is faster but cannot 00197 * be used to read from different channels implementations. 00198 * 00199 * @see chIOGetTimeout() 00200 */ 00201 #define sdGetTimeout(sdp, t) chIQGetTimeout(&(sdp)->iqueue, t) 00202 00203 /** 00204 * @brief Direct blocking write to a @p SerialDriver. 00205 * @details This function bypasses the indirect access to the channel and 00206 * writes directly to the output queue. This is faster but cannot 00207 * be used to write from different channels implementations. 00208 * 00209 * @see chIOWriteTimeout() 00210 */ 00211 #define sdWrite(sdp, b, n) \ 00212 chOQWriteTimeout(&(sdp)->oqueue, b, n, TIME_INFINITE) 00213 00214 /** 00215 * @brief Direct blocking write to a @p SerialDriver with timeout 00216 * specification. 00217 * @details This function bypasses the indirect access to the channel and 00218 * writes directly to the output queue. This is faster but cannot 00219 * be used to write from different channels implementations. 00220 * 00221 * @see chIOWriteTimeout() 00222 */ 00223 #define sdWriteTimeout(sdp, b, n, t) \ 00224 chOQWriteTimeout(&(sdp)->oqueue, b, n, t) 00225 00226 /** 00227 * @brief Direct non-blocking write to a @p SerialDriver. 00228 * @details This function bypasses the indirect access to the channel and 00229 * writes directly to the output queue. This is faster but cannot 00230 * be used to write from different channels implementations. 00231 * 00232 * @see chIOWriteTimeout() 00233 */ 00234 #define sdAsynchronousWrite(sdp, b, n) \ 00235 chOQWriteTimeout(&(sdp)->oqueue, b, n, TIME_IMMEDIATE) 00236 00237 /** 00238 * @brief Direct blocking read from a @p SerialDriver. 00239 * @details This function bypasses the indirect access to the channel and 00240 * reads directly from the input queue. This is faster but cannot 00241 * be used to read from different channels implementations. 00242 * 00243 * @see chIOReadTimeout() 00244 */ 00245 #define sdRead(sdp, b, n) \ 00246 chIQReadTimeout(&(sdp)->iqueue, b, n, TIME_INFINITE) 00247 00248 /** 00249 * @brief Direct blocking read from a @p SerialDriver with timeout 00250 * specification. 00251 * @details This function bypasses the indirect access to the channel and 00252 * reads directly from the input queue. This is faster but cannot 00253 * be used to read from different channels implementations. 00254 * 00255 * @see chIOReadTimeout() 00256 */ 00257 #define sdReadTimeout(sdp, b, n, t) \ 00258 chIQReadTimeout(&(sdp)->iqueue, b, n, t) 00259 00260 /** 00261 * @brief Direct non-blocking read from a @p SerialDriver. 00262 * @details This function bypasses the indirect access to the channel and 00263 * reads directly from the input queue. This is faster but cannot 00264 * be used to read from different channels implementations. 00265 * 00266 * @see chIOReadTimeout() 00267 */ 00268 #define sdAsynchronousRead(sdp, b, n) \ 00269 chIQReadTimeout(&(sdp)->iqueue, b, n, TIME_IMMEDIATE) 00270 00271 /** 00272 * @brief Returns the status change event source. 00273 * @details The status change event source is broadcasted when the channel 00274 * status is updated, the status flags can then be fetched and 00275 * cheared by using @p sdGetAndClearFlags(). 00276 * 00277 * @param[in] ip pointer to a @p SerialDriver object 00278 * @return A pointer to an @p EventSource object. 00279 */ 00280 #define sdGetStatusChangeEventSource(ip) (&((ip)->vmt->sevent)) 00281 00282 /*===========================================================================*/ 00283 /* External declarations. */ 00284 /*===========================================================================*/ 00285 00286 #ifdef __cplusplus 00287 extern "C" { 00288 #endif 00289 void sdInit(void); 00290 void sdObjectInit(SerialDriver *sdp, qnotify_t inotify, qnotify_t onotify); 00291 void sdStart(SerialDriver *sdp, const SerialConfig *config); 00292 void sdStop(SerialDriver *sdp); 00293 void sdIncomingDataI(SerialDriver *sdp, uint8_t b); 00294 msg_t sdRequestDataI(SerialDriver *sdp); 00295 void sdAddFlagsI(SerialDriver *sdp, sdflags_t mask); 00296 sdflags_t sdGetAndClearFlags(SerialDriver *sdp); 00297 #ifdef __cplusplus 00298 } 00299 #endif 00300 00301 #endif /* CH_HAL_USE_SERIAL */ 00302 00303 #endif /* _SERIAL_H_ */ 00304 00305 /** @} */