2010-12-26 20:59:17 -04:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "Content-Type" content = "text/xhtml;charset=UTF-8" / >
2010-12-30 03:51:53 -04:00
< title > ArduPilot Libraries: FastSerial.cpp Source File< / title >
2010-12-26 20:59:17 -04:00
< link href = "tabs.css" rel = "stylesheet" type = "text/css" / >
2010-12-26 23:23:35 -04:00
< link href = "search/search.css" rel = "stylesheet" type = "text/css" / >
< script type = "text/javaScript" src = "search/search.js" > < / script >
2010-12-26 20:59:17 -04:00
< link href = "doxygen.css" rel = "stylesheet" type = "text/css" / >
< / head >
2010-12-26 23:23:35 -04:00
< body onload = 'searchBox.OnSelectItem(0);' >
2010-12-26 20:59:17 -04:00
<!-- Generated by Doxygen 1.7.1 -->
< div class = "navigation" id = "top" >
< div class = "tabs" >
< ul class = "tablist" >
< li > < a href = "main.html" > < span > Main Page< / span > < / a > < / li >
2010-12-30 03:51:53 -04:00
< li > < a href = "pages.html" > < span > Related Pages< / span > < / a > < / li >
2010-12-26 20:59:17 -04:00
< li > < a href = "namespaces.html" > < span > Namespaces< / span > < / a > < / li >
< li > < a href = "annotated.html" > < span > Classes< / span > < / a > < / li >
< li class = "current" > < a href = "files.html" > < span > Files< / span > < / a > < / li >
< / ul >
< / div >
< div class = "tabs2" >
< ul class = "tablist" >
< li > < a href = "files.html" > < span > File List< / span > < / a > < / li >
< li > < a href = "globals.html" > < span > File Members< / span > < / a > < / li >
< / ul >
< / div >
< div class = "header" >
< div class = "headertitle" >
2010-12-30 03:51:53 -04:00
< h1 > FastSerial.cpp< / h1 > < / div >
2010-12-26 20:59:17 -04:00
< / div >
< div class = "contents" >
< a href = "_fast_serial_8cpp.html" > Go to the documentation of this file.< / a > < div class = "fragment" > < pre class = "fragment" > < a name = "l00001" > < / a > 00001 < span class = "comment" > // -*- Mode: C++; c-basic-offset: 8; indent-tabs-mode: nil -*-< / span >
< a name = "l00002" > < / a > 00002 < span class = "comment" > //< / span >
< a name = "l00003" > < / a > 00003 < span class = "comment" > // Interrupt-driven serial transmit/receive library.< / span >
< a name = "l00004" > < / a > 00004 < span class = "comment" > //< / span >
< a name = "l00005" > < / a > 00005 < span class = "comment" > // Copyright (c) 2010 Michael Smith. All rights reserved.< / span >
< a name = "l00006" > < / a > 00006 < span class = "comment" > //< / span >
< a name = "l00007" > < / a > 00007 < span class = "comment" > // Receive and baudrate calculations derived from the Arduino < / span >
< a name = "l00008" > < / a > 00008 < span class = "comment" > // HardwareSerial driver:< / span >
< a name = "l00009" > < / a > 00009 < span class = "comment" > //< / span >
< a name = "l00010" > < / a > 00010 < span class = "comment" > // Copyright (c) 2006 Nicholas Zambetti. All right reserved.< / span >
< a name = "l00011" > < / a > 00011 < span class = "comment" > //< / span >
< a name = "l00012" > < / a > 00012 < span class = "comment" > // Transmit algorithm inspired by work:< / span >
< a name = "l00013" > < / a > 00013 < span class = "comment" > //< / span >
< a name = "l00014" > < / a > 00014 < span class = "comment" > // Code Jose Julio and Jordi Munoz. DIYDrones.com< / span >
< a name = "l00015" > < / a > 00015 < span class = "comment" > //< / span >
< a name = "l00016" > < / a > 00016 < span class = "comment" > // This library is free software; you can redistribute it and/or< / span >
< a name = "l00017" > < / a > 00017 < span class = "comment" > // modify it under the terms of the GNU Lesser General Public< / span >
< a name = "l00018" > < / a > 00018 < span class = "comment" > // License as published by the Free Software Foundation; either< / span >
< a name = "l00019" > < / a > 00019 < span class = "comment" > // version 2.1 of the License, or (at your option) any later version.< / span >
< a name = "l00020" > < / a > 00020 < span class = "comment" > //< / span >
< a name = "l00021" > < / a > 00021 < span class = "comment" > // This library is distributed in the hope that it will be useful,< / span >
< a name = "l00022" > < / a > 00022 < span class = "comment" > // but WITHOUT ANY WARRANTY; without even the implied warranty of< / span >
< a name = "l00023" > < / a > 00023 < span class = "comment" > // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU< / span >
< a name = "l00024" > < / a > 00024 < span class = "comment" > // Lesser General Public License for more details.< / span >
< a name = "l00025" > < / a > 00025 < span class = "comment" > //< / span >
< a name = "l00026" > < / a > 00026 < span class = "comment" > // You should have received a copy of the GNU Lesser General Public< / span >
< a name = "l00027" > < / a > 00027 < span class = "comment" > // License along with this library; if not, write to the Free Software< / span >
< a name = "l00028" > < / a > 00028 < span class = "comment" > // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA< / span >
< a name = "l00029" > < / a > 00029 < span class = "comment" > //< / span >
< a name = "l00030" > < / a > 00030
< a name = "l00031" > < / a > 00031
< a name = "l00032" > < / a > 00032 < span class = "comment" > //#include " ../AP_Common/AP_Common.h" < / span >
< a name = "l00033" > < / a > 00033 < span class = "preprocessor" > #include " < a class = "code" href = "_fast_serial_8h.html" > FastSerial.h< / a > " < / span >
< a name = "l00034" > < / a > 00034 < span class = "preprocessor" > #include " < a class = "code" href = "_w_program_8h.html" > WProgram.h< / a > " < / span >
< a name = "l00035" > < / a > 00035
< a name = "l00036" > < / a > 00036 < span class = "preprocessor" > #if defined(UDR3)< / span >
< a name = "l00037" > < / a > 00037 < span class = "preprocessor" > < / span > < span class = "preprocessor" > # define FS_MAX_PORTS 4< / span >
< a name = "l00038" > < / a > 00038 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #elif defined(UDR2)< / span >
< a name = "l00039" > < / a > 00039 < span class = "preprocessor" > < / span > < span class = "preprocessor" > # define FS_MAX_PORTS 3< / span >
< a name = "l00040" > < / a > 00040 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #elif defined(UDR1)< / span >
< a name = "l00041" > < / a > 00041 < span class = "preprocessor" > < / span > < span class = "preprocessor" > # define FS_MAX_PORTS 2< / span >
< a name = "l00042" > < / a > 00042 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #else< / span >
< a name = "l00043" > < / a > < a class = "code" href = "_fast_serial_8cpp.html#a52a40ec3245d529723af3ce0a95bd7b6" > 00043< / a > < span class = "preprocessor" > < / span > < span class = "preprocessor" > # define FS_MAX_PORTS 1< / span >
< a name = "l00044" > < / a > 00044 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #endif< / span >
< a name = "l00045" > < / a > 00045 < span class = "preprocessor" > < / span >
< a name = "l00046" > < / a > < a class = "code" href = "_fast_serial_8h.html#ad2687d3a6a3d7444849df9edc5597f6b" > 00046< / a > < a class = "code" href = "struct_fast_serial_1_1_buffer.html" > FastSerial::Buffer< / a > < a class = "code" href = "_fast_serial_8cpp.html#ae8641bf6f22cf7893cb462460417caf9" > __FastSerial__rxBuffer< / a > [FS_MAX_PORTS];
< a name = "l00047" > < / a > < a class = "code" href = "_fast_serial_8h.html#a50dd604252dbc7918b4da78e2229e0ce" > 00047< / a > < a class = "code" href = "struct_fast_serial_1_1_buffer.html" > FastSerial::Buffer< / a > < a class = "code" href = "_fast_serial_8cpp.html#ada936bc2cdf111796f90cdb7dcdc54dd" > __FastSerial__txBuffer< / a > [FS_MAX_PORTS];
< a name = "l00048" > < / a > 00048
< a name = "l00049" > < / a > 00049 < span class = "comment" > // Default buffer sizes< / span >
< a name = "l00050" > < / a > < a class = "code" href = "_fast_serial_8cpp.html#a739a2a1a0047c98ac1b18ecd25dac092" > 00050< / a > < span class = "preprocessor" > #define RX_BUFFER_SIZE 128< / span >
< a name = "l00051" > < / a > < a class = "code" href = "_fast_serial_8cpp.html#a9ab33647617098646990fe263600b650" > 00051< / a > < span class = "preprocessor" > < / span > < span class = "preprocessor" > #define TX_BUFFER_SIZE 64< / span >
< a name = "l00052" > < / a > < a class = "code" href = "_fast_serial_8cpp.html#a65c660282414eff7e990f21f6b306cb8" > 00052< / a > < span class = "preprocessor" > < / span > < span class = "preprocessor" > #define BUFFER_MAX 512< / span >
< a name = "l00053" > < / a > 00053 < span class = "preprocessor" > < / span >
< a name = "l00054" > < / a > 00054 < span class = "comment" > // Constructor /////////////////////////////////////////////////////////////////< / span >
< a name = "l00055" > < / a > 00055
< a name = "l00056" > < / a > < a class = "code" href = "class_fast_serial.html#a9f20535e34fab50c1f771b9a41790219" > 00056< / a > < a class = "code" href = "class_fast_serial.html#a9f20535e34fab50c1f771b9a41790219" > FastSerial::FastSerial< / a > (< span class = "keyword" > const< / span > uint8_t portNumber,
< a name = "l00057" > < / a > 00057 < span class = "keyword" > volatile< / span > uint8_t *ubrrh,
< a name = "l00058" > < / a > 00058 < span class = "keyword" > volatile< / span > uint8_t *ubrrl,
< a name = "l00059" > < / a > 00059 < span class = "keyword" > volatile< / span > uint8_t *ucsra,
< a name = "l00060" > < / a > 00060 < span class = "keyword" > volatile< / span > uint8_t *ucsrb,
< a name = "l00061" > < / a > 00061 < span class = "keyword" > const< / span > uint8_t u2x,
< a name = "l00062" > < / a > 00062 < span class = "keyword" > const< / span > uint8_t portEnableBits,
< a name = "l00063" > < / a > 00063 < span class = "keyword" > const< / span > uint8_t portTxBits)
< a name = "l00064" > < / a > 00064 {
< a name = "l00065" > < / a > 00065 _ubrrh = ubrrh;
< a name = "l00066" > < / a > 00066 _ubrrl = ubrrl;
< a name = "l00067" > < / a > 00067 _ucsra = ucsra;
< a name = "l00068" > < / a > 00068 _ucsrb = ucsrb;
< a name = "l00069" > < / a > 00069 _u2x = u2x;
< a name = "l00070" > < / a > 00070 _portEnableBits = portEnableBits;
< a name = "l00071" > < / a > 00071 _portTxBits = portTxBits;
< a name = "l00072" > < / a > 00072
< a name = "l00073" > < / a > 00073 < span class = "comment" > // init buffers< / span >
< a name = "l00074" > < / a > 00074 _rxBuffer = & __FastSerial__rxBuffer[portNumber];
< a name = "l00075" > < / a > 00075 _txBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a21455465b69c8fd0225236bd8b949158" > head< / a > = _txBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > = 0;
< a name = "l00076" > < / a > 00076 _txBuffer = & __FastSerial__txBuffer[portNumber];
< a name = "l00077" > < / a > 00077 _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a21455465b69c8fd0225236bd8b949158" > head< / a > = _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > = 0;
< a name = "l00078" > < / a > 00078 }
< a name = "l00079" > < / a > 00079
< a name = "l00080" > < / a > 00080 < span class = "comment" > // Public Methods //////////////////////////////////////////////////////////////< / span >
< a name = "l00081" > < / a > 00081
< a name = "l00082" > < / a > < a class = "code" href = "class_fast_serial.html#a1ae3f5f5649d7a72daf2806a6ec1cf45" > 00082< / a > < span class = "keywordtype" > void< / span > < a class = "code" href = "class_fast_serial.html#a1ae3f5f5649d7a72daf2806a6ec1cf45" > FastSerial::begin< / a > (< span class = "keywordtype" > long< / span > baud)
< a name = "l00083" > < / a > 00083 {
< a name = "l00084" > < / a > 00084 < span class = "keywordtype" > unsigned< / span > < span class = "keywordtype" > int< / span > rxb, txb;
< a name = "l00085" > < / a > 00085
< a name = "l00086" > < / a > 00086 < span class = "comment" > // If we are re-configuring an already-open port, preserve the< / span >
< a name = "l00087" > < / a > 00087 < span class = "comment" > // existing buffer sizes.< / span >
< a name = "l00088" > < / a > 00088 < span class = "keywordflow" > if< / span > (_open) {
< a name = "l00089" > < / a > 00089 rxb = _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a8797629ef75561f8382aa84d4f62c451" > mask< / a > + 1;
< a name = "l00090" > < / a > 00090 txb = _txBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a8797629ef75561f8382aa84d4f62c451" > mask< / a > + 1;
< a name = "l00091" > < / a > 00091 } < span class = "keywordflow" > else< / span > {
< a name = "l00092" > < / a > 00092 rxb = RX_BUFFER_SIZE;
< a name = "l00093" > < / a > 00093 txb = TX_BUFFER_SIZE;
< a name = "l00094" > < / a > 00094 }
< a name = "l00095" > < / a > 00095
< a name = "l00096" > < / a > 00096 < a class = "code" href = "class_fast_serial.html#a1ae3f5f5649d7a72daf2806a6ec1cf45" > begin< / a > (baud, < a class = "code" href = "_fast_serial_8cpp.html#a739a2a1a0047c98ac1b18ecd25dac092" > RX_BUFFER_SIZE< / a > , < a class = "code" href = "_fast_serial_8cpp.html#a9ab33647617098646990fe263600b650" > TX_BUFFER_SIZE< / a > );
< a name = "l00097" > < / a > 00097 }
< a name = "l00098" > < / a > 00098
< a name = "l00099" > < / a > < a class = "code" href = "class_fast_serial.html#aca4c10e234de6ef56f255dbe8461f8b2" > 00099< / a > < span class = "keywordtype" > void< / span > < a class = "code" href = "class_fast_serial.html#a1ae3f5f5649d7a72daf2806a6ec1cf45" > FastSerial::begin< / a > (< span class = "keywordtype" > long< / span > baud, < span class = "keywordtype" > unsigned< / span > < span class = "keywordtype" > int< / span > rxSpace, < span class = "keywordtype" > unsigned< / span > < span class = "keywordtype" > int< / span > txSpace)
< a name = "l00100" > < / a > 00100 {
< a name = "l00101" > < / a > 00101 uint16_t ubrr;
< a name = "l00102" > < / a > 00102 < span class = "keywordtype" > bool< / span > use_u2x = < span class = "keyword" > false< / span > ;
< a name = "l00103" > < / a > 00103 < span class = "keywordtype" > int< / span > ureg, u2;
< a name = "l00104" > < / a > 00104 < span class = "keywordtype" > long< / span > breg, b2, dreg, d2;
< a name = "l00105" > < / a > 00105
< a name = "l00106" > < / a > 00106 < span class = "comment" > // if we are currently open, close and restart< / span >
< a name = "l00107" > < / a > 00107 < span class = "keywordflow" > if< / span > (_open)
< a name = "l00108" > < / a > 00108 < a class = "code" href = "class_fast_serial.html#a18dc7ab6584b8e01bed86d74d664e6fa" > end< / a > ();
< a name = "l00109" > < / a > 00109
< a name = "l00110" > < / a > 00110 < span class = "comment" > // allocate buffers< / span >
< a name = "l00111" > < / a > 00111 < span class = "keywordflow" > if< / span > (!_allocBuffer(_rxBuffer, rxSpace ? : < a class = "code" href = "_fast_serial_8cpp.html#a739a2a1a0047c98ac1b18ecd25dac092" > RX_BUFFER_SIZE< / a > ) ||
< a name = "l00112" > < / a > 00112 !_allocBuffer(_txBuffer, txSpace ? : < a class = "code" href = "_fast_serial_8cpp.html#a9ab33647617098646990fe263600b650" > TX_BUFFER_SIZE< / a > )) {
< a name = "l00113" > < / a > 00113 < a class = "code" href = "class_fast_serial.html#a18dc7ab6584b8e01bed86d74d664e6fa" > end< / a > ();
< a name = "l00114" > < / a > 00114 < span class = "keywordflow" > return< / span > ; < span class = "comment" > // couldn' t allocate buffers - fatal< / span >
< a name = "l00115" > < / a > 00115 }
< a name = "l00116" > < / a > 00116 _open = < span class = "keyword" > true< / span > ;
< a name = "l00117" > < / a > 00117
< a name = "l00118" > < / a > 00118 < span class = "comment" > // U2X mode is needed for bitrates higher than (F_CPU / 16)< / span >
< a name = "l00119" > < / a > 00119 < span class = "keywordflow" > if< / span > (baud > F_CPU / 16) {
< a name = "l00120" > < / a > 00120 use_u2x = < span class = "keyword" > true< / span > ;
< a name = "l00121" > < / a > 00121 ubrr = F_CPU / (8 * baud) - 1;
< a name = "l00122" > < / a > 00122 } < span class = "keywordflow" > else< / span > {
< a name = "l00123" > < / a > 00123 < span class = "comment" > // Determine whether u2x mode would give a closer< / span >
< a name = "l00124" > < / a > 00124 < span class = "comment" > // approximation of the desired speed.< / span >
< a name = "l00125" > < / a > 00125
< a name = "l00126" > < / a > 00126 < span class = "comment" > // ubrr for non-2x mode, corresponding baudrate and delta< / span >
< a name = "l00127" > < / a > 00127 ureg = F_CPU / 16 / baud - 1;
< a name = "l00128" > < / a > 00128 breg = F_CPU / 16 / (ureg + 1);
< a name = "l00129" > < / a > 00129 dreg = abs(baud - breg);
< a name = "l00130" > < / a > 00130
< a name = "l00131" > < / a > 00131 < span class = "comment" > // ubrr for 2x mode, corresponding bitrate and delta< / span >
< a name = "l00132" > < / a > 00132 u2 = F_CPU / 8 / baud - 1;
< a name = "l00133" > < / a > 00133 b2 = F_CPU / 8 / (u2 + 1);
< a name = "l00134" > < / a > 00134 d2 = abs(baud - b2);
< a name = "l00135" > < / a > 00135
< a name = "l00136" > < / a > 00136 < span class = "comment" > // Pick the setting that gives the smallest rate< / span >
< a name = "l00137" > < / a > 00137 < span class = "comment" > // error, preferring non-u2x mode if the delta is< / span >
< a name = "l00138" > < / a > 00138 < span class = "comment" > // identical.< / span >
< a name = "l00139" > < / a > 00139 < span class = "keywordflow" > if< / span > (dreg < = d2) {
< a name = "l00140" > < / a > 00140 ubrr = ureg;
< a name = "l00141" > < / a > 00141 } < span class = "keywordflow" > else< / span > {
< a name = "l00142" > < / a > 00142 ubrr = u2;
< a name = "l00143" > < / a > 00143 use_u2x = < span class = "keyword" > true< / span > ;
< a name = "l00144" > < / a > 00144 }
< a name = "l00145" > < / a > 00145 }
< a name = "l00146" > < / a > 00146
< a name = "l00147" > < / a > 00147 *_ucsra = use_u2x ? _BV(_u2x) : 0;
< a name = "l00148" > < / a > 00148 *_ubrrh = ubrr > > 8;
< a name = "l00149" > < / a > 00149 *_ubrrl = ubrr;
< a name = "l00150" > < / a > 00150 *_ucsrb |= _portEnableBits;
< a name = "l00151" > < / a > 00151 }
< a name = "l00152" > < / a > 00152
< a name = "l00153" > < / a > < a class = "code" href = "class_fast_serial.html#a18dc7ab6584b8e01bed86d74d664e6fa" > 00153< / a > < span class = "keywordtype" > void< / span > < a class = "code" href = "class_fast_serial.html#a18dc7ab6584b8e01bed86d74d664e6fa" > FastSerial::end< / a > ()
< a name = "l00154" > < / a > 00154 {
< a name = "l00155" > < / a > 00155 *_ucsrb & = ~(_portEnableBits | _portTxBits);
< a name = "l00156" > < / a > 00156
< a name = "l00157" > < / a > 00157 _freeBuffer(_rxBuffer);
< a name = "l00158" > < / a > 00158 _freeBuffer(_txBuffer);
< a name = "l00159" > < / a > 00159 _open = < span class = "keyword" > false< / span > ;
< a name = "l00160" > < / a > 00160 }
< a name = "l00161" > < / a > 00161
< a name = "l00162" > < / a > 00162 < span class = "keywordtype" > int< / span >
< a name = "l00163" > < / a > < a class = "code" href = "class_fast_serial.html#a3908f5db6a1e946207a4b92b7c0b9ee5" > 00163< / a > < a class = "code" href = "class_fast_serial.html#a3908f5db6a1e946207a4b92b7c0b9ee5" > FastSerial::available< / a > (< span class = "keywordtype" > void< / span > )
< a name = "l00164" > < / a > 00164 {
< a name = "l00165" > < / a > 00165 < span class = "keywordflow" > if< / span > (!_open)
< a name = "l00166" > < / a > 00166 < span class = "keywordflow" > return< / span > (-1);
< a name = "l00167" > < / a > 00167 < span class = "keywordflow" > return< / span > ((_rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a21455465b69c8fd0225236bd8b949158" > head< / a > - _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > ) & _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a8797629ef75561f8382aa84d4f62c451" > mask< / a > );
< a name = "l00168" > < / a > 00168 }
< a name = "l00169" > < / a > 00169
< a name = "l00170" > < / a > 00170 < span class = "keywordtype" > int< / span >
< a name = "l00171" > < / a > < a class = "code" href = "class_fast_serial.html#a6b63b52d3dedb762340675f0dbb29622" > 00171< / a > < a class = "code" href = "class_fast_serial.html#a6b63b52d3dedb762340675f0dbb29622" > FastSerial::read< / a > (< span class = "keywordtype" > void< / span > )
< a name = "l00172" > < / a > 00172 {
< a name = "l00173" > < / a > 00173 uint8_t c;
< a name = "l00174" > < / a > 00174
< a name = "l00175" > < / a > 00175 < span class = "comment" > // if the head and tail are equal, the buffer is empty< / span >
< a name = "l00176" > < / a > 00176 < span class = "keywordflow" > if< / span > (!_open || (_rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a21455465b69c8fd0225236bd8b949158" > head< / a > == _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > ))
< a name = "l00177" > < / a > 00177 < span class = "keywordflow" > return< / span > (-1);
< a name = "l00178" > < / a > 00178
< a name = "l00179" > < / a > 00179 < span class = "comment" > // pull character from tail< / span >
< a name = "l00180" > < / a > 00180 c = _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a5eb7885018db67980c5b8bd1279d2b3d" > bytes< / a > [_rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > ];
< a name = "l00181" > < / a > 00181 _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > = (_rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > + 1) & _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a8797629ef75561f8382aa84d4f62c451" > mask< / a > ;
< a name = "l00182" > < / a > 00182
< a name = "l00183" > < / a > 00183 < span class = "keywordflow" > return< / span > (c);
< a name = "l00184" > < / a > 00184 }
< a name = "l00185" > < / a > 00185
< a name = "l00186" > < / a > 00186 < span class = "keywordtype" > int< / span >
< a name = "l00187" > < / a > < a class = "code" href = "class_fast_serial.html#a1bb9d282cdf6abb37c95f3acc314d7a2" > 00187< / a > < a class = "code" href = "class_fast_serial.html#a1bb9d282cdf6abb37c95f3acc314d7a2" > FastSerial::peek< / a > (< span class = "keywordtype" > void< / span > )
< a name = "l00188" > < / a > 00188 {
< a name = "l00189" > < / a > 00189
< a name = "l00190" > < / a > 00190 < span class = "comment" > // if the head and tail are equal, the buffer is empty< / span >
< a name = "l00191" > < / a > 00191 < span class = "keywordflow" > if< / span > (!_open || (_rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a21455465b69c8fd0225236bd8b949158" > head< / a > == _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > ))
< a name = "l00192" > < / a > 00192 < span class = "keywordflow" > return< / span > (-1);
< a name = "l00193" > < / a > 00193
< a name = "l00194" > < / a > 00194 < span class = "comment" > // pull character from tail< / span >
< a name = "l00195" > < / a > 00195 < span class = "keywordflow" > return< / span > (_rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a5eb7885018db67980c5b8bd1279d2b3d" > bytes< / a > [_rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > ]);
< a name = "l00196" > < / a > 00196 }
< a name = "l00197" > < / a > 00197
< a name = "l00198" > < / a > 00198
< a name = "l00199" > < / a > 00199 < span class = "keywordtype" > void< / span >
< a name = "l00200" > < / a > < a class = "code" href = "class_fast_serial.html#a80a0f8852702174fc9f771da3713c933" > 00200< / a > < a class = "code" href = "class_fast_serial.html#a80a0f8852702174fc9f771da3713c933" > FastSerial::flush< / a > (< span class = "keywordtype" > void< / span > )
< a name = "l00201" > < / a > 00201 {
< a name = "l00202" > < / a > 00202 < span class = "comment" > // don' t reverse this or there may be problems if the RX interrupt< / span >
< a name = "l00203" > < / a > 00203 < span class = "comment" > // occurs after reading the value of _rxBuffer-> head but before writing< / span >
< a name = "l00204" > < / a > 00204 < span class = "comment" > // the value to _rxBuffer-> tail; the previous value of head< / span >
< a name = "l00205" > < / a > 00205 < span class = "comment" > // may be written to tail, making it appear as if the buffer< / span >
< a name = "l00206" > < / a > 00206 < span class = "comment" > // don' t reverse this or there may be problems if the RX interrupt< / span >
< a name = "l00207" > < / a > 00207 < span class = "comment" > // occurs after reading the value of head but before writing< / span >
< a name = "l00208" > < / a > 00208 < span class = "comment" > // the value to tail; the previous value of rx_buffer_head< / span >
< a name = "l00209" > < / a > 00209 < span class = "comment" > // may be written to tail, making it appear as if the buffer< / span >
< a name = "l00210" > < / a > 00210 < span class = "comment" > // were full, not empty.< / span >
< a name = "l00211" > < / a > 00211 _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a21455465b69c8fd0225236bd8b949158" > head< / a > = _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > ;
< a name = "l00212" > < / a > 00212
< a name = "l00213" > < / a > 00213 < span class = "comment" > // don' t reverse this or there may be problems if the TX interrupt< / span >
< a name = "l00214" > < / a > 00214 < span class = "comment" > // occurs after reading the value of _txBuffer-> tail but before writing< / span >
< a name = "l00215" > < / a > 00215 < span class = "comment" > // the value to _txBuffer-> head.< / span >
< a name = "l00216" > < / a > 00216 _txBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > = _rxBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a21455465b69c8fd0225236bd8b949158" > head< / a > ;
< a name = "l00217" > < / a > 00217 }
< a name = "l00218" > < / a > 00218
< a name = "l00219" > < / a > 00219 < span class = "keywordtype" > void< / span >
< a name = "l00220" > < / a > < a class = "code" href = "class_fast_serial.html#a8e41c9efa4f854ac6ffa0e178c1314e9" > 00220< / a > < a class = "code" href = "class_fast_serial.html#a8e41c9efa4f854ac6ffa0e178c1314e9" > FastSerial::write< / a > (uint8_t c)
< a name = "l00221" > < / a > 00221 {
< a name = "l00222" > < / a > 00222 int16_t i;
< a name = "l00223" > < / a > 00223
< a name = "l00224" > < / a > 00224 < span class = "keywordflow" > if< / span > (!_open) < span class = "comment" > // drop bytes if not open< / span >
< a name = "l00225" > < / a > 00225 < span class = "keywordflow" > return< / span > ;
< a name = "l00226" > < / a > 00226
< a name = "l00227" > < / a > 00227 < span class = "comment" > // wait for room in the tx buffer< / span >
< a name = "l00228" > < / a > 00228 i = (_txBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a21455465b69c8fd0225236bd8b949158" > head< / a > + 1) & _txBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a8797629ef75561f8382aa84d4f62c451" > mask< / a > ;
< a name = "l00229" > < / a > 00229 while (i == _txBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#aec076766d7dbdfba4c1116e8329d1c5f" > tail< / a > )
< a name = "l00230" > < / a > 00230 ;
< a name = "l00231" > < / a > 00231
< a name = "l00232" > < / a > 00232 < span class = "comment" > // add byte to the buffer< / span >
< a name = "l00233" > < / a > 00233 _txBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a5eb7885018db67980c5b8bd1279d2b3d" > bytes< / a > [_txBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a21455465b69c8fd0225236bd8b949158" > head< / a > ] = c;
< a name = "l00234" > < / a > 00234 _txBuffer-> < a class = "code" href = "struct_fast_serial_1_1_buffer.html#a21455465b69c8fd0225236bd8b949158" > head< / a > = i;
< a name = "l00235" > < / a > 00235
< a name = "l00236" > < / a > 00236 < span class = "comment" > // enable the data-ready interrupt, as it may be off if the buffer is empty< / span >
< a name = "l00237" > < / a > 00237 *_ucsrb |= _portTxBits;
< a name = "l00238" > < / a > 00238 }
< a name = "l00239" > < / a > 00239
< a name = "l00240" > < / a > 00240 < span class = "comment" > // Buffer management ///////////////////////////////////////////////////////////< / span >
< a name = "l00241" > < / a > 00241
< a name = "l00242" > < / a > 00242 < span class = "keywordtype" > bool< / span >
< a name = "l00243" > < / a > 00243 FastSerial::_allocBuffer(Buffer *buffer, < span class = "keywordtype" > unsigned< / span > < span class = "keywordtype" > int< / span > size)
< a name = "l00244" > < / a > 00244 {
< a name = "l00245" > < / a > 00245 uint8_t shift;
< a name = "l00246" > < / a > 00246
< a name = "l00247" > < / a > 00247 < span class = "comment" > // init buffer state< / span >
< a name = "l00248" > < / a > 00248 buffer-> head = buffer-> tail = 0;
< a name = "l00249" > < / a > 00249
< a name = "l00250" > < / a > 00250 < span class = "comment" > // Compute the power of 2 greater or equal to the requested buffer size< / span >
< a name = "l00251" > < / a > 00251 < span class = "comment" > // and then a mask to simplify wrapping operations. Using __builtin_clz< / span >
< a name = "l00252" > < / a > 00252 < span class = "comment" > // would seem to make sense, but it uses a 256(!) byte table.< / span >
< a name = "l00253" > < / a > 00253 < span class = "comment" > // Note that we ignore requests for more than BUFFER_MAX space.< / span >
< a name = "l00254" > < / a > 00254 < span class = "keywordflow" > for< / span > (shift = 1; (1U < < shift) < min(< a class = "code" href = "_fast_serial_8cpp.html#a65c660282414eff7e990f21f6b306cb8" > BUFFER_MAX< / a > , size); shift++)
< a name = "l00255" > < / a > 00255 ;
< a name = "l00256" > < / a > 00256 buffer-> mask = (1 < < shift) - 1;
< a name = "l00257" > < / a > 00257
< a name = "l00258" > < / a > 00258 < span class = "comment" > // allocate memory for the buffer - if this fails, we fail< / span >
< a name = "l00259" > < / a > 00259 buffer-> bytes = (uint8_t *)malloc(buffer-> mask + 1);
< a name = "l00260" > < / a > 00260
< a name = "l00261" > < / a > 00261 < span class = "keywordflow" > return< / span > (buffer-> bytes != NULL);
< a name = "l00262" > < / a > 00262 }
< a name = "l00263" > < / a > 00263
< a name = "l00264" > < / a > 00264 < span class = "keywordtype" > void< / span >
< a name = "l00265" > < / a > 00265 FastSerial::_freeBuffer(Buffer *buffer)
< a name = "l00266" > < / a > 00266 {
< a name = "l00267" > < / a > 00267 buffer-> head = buffer-> tail = 0;
< a name = "l00268" > < / a > 00268 buffer-> mask = 0;
< a name = "l00269" > < / a > 00269 < span class = "keywordflow" > if< / span > (NULL != buffer-> bytes) {
< a name = "l00270" > < / a > 00270 free(buffer-> bytes);
< a name = "l00271" > < / a > 00271 buffer-> bytes = NULL;
< a name = "l00272" > < / a > 00272 }
< a name = "l00273" > < / a > 00273 }
< a name = "l00274" > < / a > 00274
< / pre > < / div > < / div >
< / div >
2010-12-26 23:23:35 -04:00
< hr class = "footer" / > < address class = "footer" > < small >
Generated for ArduPilot Libraries by < a href = "http://www.doxygen.org/index.html" > < img class = "footer" src = "doxygen.png" alt = "doxygen" / > < / a > < / small > < / address >
2010-12-26 20:59:17 -04:00
< / body >
< / html >