This repository has been archived on 2020-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
arduino_universal_serial_ad.../Libraries/SerialPort/html/a00005_source.html

528 lines
58 KiB
HTML
Raw Permalink Normal View History

<!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"/>
<title>Serial Port: Arduino/libraries/SerialPort/SerialPort.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Generated by Doxygen 1.7.4 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Search');
--></script>
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">Serial Port</div>
</td>
</tr>
</tbody>
</table>
</div>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</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>
<li id="searchli">
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
<div id="navrow2" class="tabs2">
<ul class="tablist">
<li><a href="files.html"><span>File&#160;List</span></a></li>
<li><a href="globals.html"><span>File&#160;Members</span></a></li>
</ul>
</div>
<div class="header">
<div class="headertitle">
<div class="title">Arduino/libraries/SerialPort/SerialPort.h</div> </div>
</div>
<div class="contents">
<a href="a00005.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* Arduino SerialPort Library</span>
<a name="l00002"></a>00002 <span class="comment"> * Copyright (C) 2012 by William Greiman</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * This file is part of the Arduino SerialPort Library</span>
<a name="l00005"></a>00005 <span class="comment"> *</span>
<a name="l00006"></a>00006 <span class="comment"> * This Library is free software: you can redistribute it and/or modify</span>
<a name="l00007"></a>00007 <span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<a name="l00008"></a>00008 <span class="comment"> * the Free Software Foundation, either version 3 of the License, or</span>
<a name="l00009"></a>00009 <span class="comment"> * (at your option) any later version.</span>
<a name="l00010"></a>00010 <span class="comment"> *</span>
<a name="l00011"></a>00011 <span class="comment"> * This Library is distributed in the hope that it will be useful,</span>
<a name="l00012"></a>00012 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00013"></a>00013 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<a name="l00014"></a>00014 <span class="comment"> * GNU General Public License for more details.</span>
<a name="l00015"></a>00015 <span class="comment"> *</span>
<a name="l00016"></a>00016 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
<a name="l00017"></a>00017 <span class="comment"> * along with the Arduino SerialPort Library. If not, see</span>
<a name="l00018"></a>00018 <span class="comment"> * &lt;http://www.gnu.org/licenses/&gt;.</span>
<a name="l00019"></a>00019 <span class="comment"> */</span>
<a name="l00024"></a>00024 <span class="preprocessor">#ifndef SerialPort_h</span>
<a name="l00025"></a>00025 <span class="preprocessor"></span><span class="preprocessor">#define SerialPort_h</span>
<a name="l00026"></a>00026 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00028"></a><a class="code" href="a00005.html#a44e23de08452d49d4b2dcb57570222ce">00028</a> <span class="comment"></span><span class="preprocessor">#define SERIAL_PORT_VERSION 20130222</span>
<a name="l00029"></a>00029 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00036"></a><a class="code" href="a00005.html#a0a18cb484aedac7af838a4cf5080406f">00036</a> <span class="comment"></span><span class="preprocessor">#define ALLOW_LARGE_BUFFERS 1</span>
<a name="l00037"></a>00037 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00044"></a><a class="code" href="a00005.html#a460c9e53d7d709cf8f90ba98d901dd80">00044</a> <span class="comment"></span><span class="preprocessor">#define USE_WRITE_OVERRIDES 1</span>
<a name="l00045"></a>00045 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00052"></a><a class="code" href="a00005.html#a492b740c6f5dfa881640261970c21e82">00052</a> <span class="comment"></span><span class="preprocessor">#define BUFFERED_RX 1</span>
<a name="l00053"></a>00053 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00060"></a><a class="code" href="a00005.html#ab9814b492800e7e722e5c4c47c4e7d6a">00060</a> <span class="comment"></span><span class="preprocessor">#define BUFFERED_TX 1</span>
<a name="l00061"></a>00061 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00065"></a><a class="code" href="a00005.html#aa347a5f07ca656ebbf07f0df83eb34b3">00065</a> <span class="comment"></span><span class="preprocessor">#define ENABLE_RX_ERROR_CHECKING 1</span>
<a name="l00066"></a>00066 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00067"></a>00067 <span class="comment">// Define symbols to allocate 64 byte ring buffers with capacity for 63 bytes.</span>
<a name="l00069"></a><a class="code" href="a00005.html#a7966cb9ae597cfc98175b3330e336c50">00069</a> <span class="comment"></span><span class="preprocessor">#define USE_NEW_SERIAL SerialPort&lt;0, 63, 63&gt; NewSerial</span>
<a name="l00070"></a>00070 <span class="preprocessor"></span>
<a name="l00071"></a><a class="code" href="a00005.html#a6d27f04ec011a817c44ef9a0898c0570">00071</a> <span class="preprocessor">#define USE_NEW_SERIAL1 SerialPort&lt;1, 63, 63&gt; NewSerial1</span>
<a name="l00072"></a>00072 <span class="preprocessor"></span>
<a name="l00073"></a><a class="code" href="a00005.html#a6f0dbd3bdf75bd4aaec5d1f9aa02fff3">00073</a> <span class="preprocessor">#define USE_NEW_SERIAL2 SerialPort&lt;2, 63, 63&gt; NewSerial2</span>
<a name="l00074"></a>00074 <span class="preprocessor"></span>
<a name="l00075"></a><a class="code" href="a00005.html#a03e12542dc110fbc675676bd7ddb6c8a">00075</a> <span class="preprocessor">#define USE_NEW_SERIAL3 SerialPort&lt;3, 63, 63&gt; NewSerial3</span>
<a name="l00076"></a>00076 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00077"></a>00077 <span class="preprocessor">#include &lt;avr/io.h&gt;</span>
<a name="l00078"></a>00078 <span class="preprocessor">#include &lt;avr/pgmspace.h&gt;</span>
<a name="l00079"></a>00079 <span class="preprocessor">#include &lt;Arduino.h&gt;</span>
<a name="l00080"></a>00080 <span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00081"></a>00081 <span class="preprocessor">#if defined(UCSR3A)</span>
<a name="l00082"></a>00082 <span class="preprocessor"></span><span class="keyword">static</span> <span class="keyword">const</span> uint8_t SERIAL_PORT_COUNT = 4;
<a name="l00083"></a>00083 <span class="preprocessor">#elif defined(UCSR2A)</span>
<a name="l00084"></a>00084 <span class="preprocessor"></span><span class="keyword">static</span> <span class="keyword">const</span> uint8_t SERIAL_PORT_COUNT = 3;
<a name="l00085"></a>00085 <span class="preprocessor">#elif defined(UCSR1A)</span>
<a name="l00086"></a>00086 <span class="preprocessor"></span><span class="keyword">static</span> <span class="keyword">const</span> uint8_t SERIAL_PORT_COUNT = 2;
<a name="l00087"></a>00087 <span class="preprocessor">#elif defined(UCSR0A) || defined(UCSRA)</span>
<a name="l00088"></a>00088 <span class="preprocessor"></span><span class="keyword">static</span> <span class="keyword">const</span> uint8_t SERIAL_PORT_COUNT = 1;
<a name="l00089"></a>00089 <span class="preprocessor">#else</span>
<a name="l00090"></a>00090 <span class="preprocessor"></span><span class="preprocessor">#error no serial ports.</span>
<a name="l00091"></a>00091 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00092"></a>00092 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00093"></a>00093 <span class="preprocessor">#ifdef UCSR0A</span>
<a name="l00094"></a>00094 <span class="preprocessor"></span><span class="comment">// Bits in UCSRA.</span>
<a name="l00095"></a>00095 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_RXC = 1 &lt;&lt; RXC0;
<a name="l00096"></a>00096 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_TXC = 1 &lt;&lt; TXC0;
<a name="l00097"></a>00097 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UDRE = 1 &lt;&lt; UDRE0;
<a name="l00098"></a>00098 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_FE = 1 &lt;&lt; FE0;
<a name="l00099"></a>00099 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_DOR = 1 &lt;&lt; DOR0;
<a name="l00100"></a>00100 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UPE = 1 &lt;&lt; UPE0;
<a name="l00101"></a>00101 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_U2X = 1 &lt;&lt; U2X0;
<a name="l00102"></a>00102 <span class="comment">// Bits in UCSRB.</span>
<a name="l00103"></a>00103 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_RXCIE = 1 &lt;&lt; RXCIE0;
<a name="l00104"></a>00104 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_TXCIE = 1 &lt;&lt; TXCIE0;
<a name="l00105"></a>00105 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UDRIE = 1 &lt;&lt; UDRIE0;
<a name="l00106"></a>00106 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_RXEN = 1 &lt;&lt; RXEN0;
<a name="l00107"></a>00107 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_TXEN = 1 &lt;&lt; TXEN0;
<a name="l00108"></a>00108 <span class="comment">// Bits in UCSRC.</span>
<a name="l00109"></a>00109 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UPM0 = 1 &lt;&lt; UPM00;
<a name="l00110"></a>00110 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UPM1 = 1 &lt;&lt; UPM01;
<a name="l00111"></a>00111 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_USBS = 1 &lt;&lt; USBS0;
<a name="l00112"></a>00112 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UCSZ0 = 1 &lt;&lt; UCSZ00;
<a name="l00113"></a>00113 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UCSZ1 = 1 &lt;&lt; UCSZ01;
<a name="l00114"></a>00114 <span class="preprocessor">#elif defined(UCSRA) // UCSR0A</span>
<a name="l00115"></a>00115 <span class="preprocessor"></span><span class="comment">// Bits in UCSRA.</span>
<a name="l00116"></a>00116 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_RXC = 1 &lt;&lt; RXC;
<a name="l00117"></a>00117 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_TXC = 1 &lt;&lt; TXC;
<a name="l00118"></a>00118 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UDRE = 1 &lt;&lt; UDRE;
<a name="l00119"></a>00119 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_FE = 1 &lt;&lt; FE;
<a name="l00120"></a>00120 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_DOR = 1 &lt;&lt; DOR;
<a name="l00121"></a>00121 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UPE = 1 &lt;&lt; PE;
<a name="l00122"></a>00122 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_U2X = 1 &lt;&lt; U2X;
<a name="l00123"></a>00123 <span class="comment">// Bits in UCSRB.</span>
<a name="l00124"></a>00124 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_RXCIE = 1 &lt;&lt; RXCIE;
<a name="l00125"></a>00125 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_TXCIE = 1 &lt;&lt; TXCIE;
<a name="l00126"></a>00126 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UDRIE = 1 &lt;&lt; UDRIE;
<a name="l00127"></a>00127 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_RXEN = 1 &lt;&lt; RXEN;
<a name="l00128"></a>00128 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_TXEN = 1 &lt;&lt; TXEN;
<a name="l00129"></a>00129 <span class="comment">// Bits in UCSRC.</span>
<a name="l00130"></a>00130 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UPM0 = 1 &lt;&lt; UPM0;
<a name="l00131"></a>00131 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UPM1 = 1 &lt;&lt; UPM1;
<a name="l00132"></a>00132 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_USBS = 1 &lt;&lt; USBS;
<a name="l00133"></a>00133 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UCSZ0 = 1 &lt;&lt; UCSZ0;
<a name="l00134"></a>00134 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UCSZ1 = 1 &lt;&lt; UCSZ1;
<a name="l00135"></a>00135 <span class="preprocessor">#elif defined(UCSR1A) // UCSR0A</span>
<a name="l00136"></a>00136 <span class="preprocessor"></span><span class="comment">// Bits in UCSRA.</span>
<a name="l00137"></a>00137 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_RXC = 1 &lt;&lt; RXC1;
<a name="l00138"></a>00138 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_TXC = 1 &lt;&lt; TXC1;
<a name="l00139"></a>00139 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UDRE = 1 &lt;&lt; UDRE1;
<a name="l00140"></a>00140 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_FE = 1 &lt;&lt; FE1;
<a name="l00141"></a>00141 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_DOR = 1 &lt;&lt; DOR1;
<a name="l00142"></a>00142 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UPE = 1 &lt;&lt; UPE1;
<a name="l00143"></a>00143 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_U2X = 1 &lt;&lt; U2X1;
<a name="l00144"></a>00144 <span class="comment">// Bits in UCSRB.</span>
<a name="l00145"></a>00145 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_RXCIE = 1 &lt;&lt; RXCIE1;
<a name="l00146"></a>00146 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_TXCIE = 1 &lt;&lt; TXCIE1;
<a name="l00147"></a>00147 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UDRIE = 1 &lt;&lt; UDRIE1;
<a name="l00148"></a>00148 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_RXEN = 1 &lt;&lt; RXEN1;
<a name="l00149"></a>00149 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_TXEN = 1 &lt;&lt; TXEN1;
<a name="l00150"></a>00150 <span class="comment">// Bits in UCSRC.</span>
<a name="l00151"></a>00151 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UPM0 = 1 &lt;&lt; UPM10;
<a name="l00152"></a>00152 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UPM1 = 1 &lt;&lt; UPM11;
<a name="l00153"></a>00153 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_USBS = 1 &lt;&lt; USBS1;
<a name="l00154"></a>00154 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UCSZ0 = 1 &lt;&lt; UCSZ10;
<a name="l00155"></a>00155 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t M_UCSZ1 = 1 &lt;&lt; UCSZ11;
<a name="l00156"></a>00156 <span class="preprocessor">#else // UCSR0A</span>
<a name="l00157"></a>00157 <span class="preprocessor"></span><span class="preprocessor">#error no serial ports</span>
<a name="l00158"></a>00158 <span class="preprocessor"></span><span class="preprocessor">#endif // UCSR0A</span>
<a name="l00159"></a>00159 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00161"></a><a class="code" href="a00005.html#aa52e1ab0a02b31e608e2f789d0f21515">00161</a> <span class="comment"></span><span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#aa52e1ab0a02b31e608e2f789d0f21515">SP_1_STOP_BIT</a> = 0;
<a name="l00163"></a><a class="code" href="a00005.html#a0caf477e5dd815acc9c3496d31e78040">00163</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#a0caf477e5dd815acc9c3496d31e78040">SP_2_STOP_BIT</a> = M_USBS;
<a name="l00164"></a>00164
<a name="l00166"></a><a class="code" href="a00005.html#a1408abd4630ac1b2a5432f07636fb2ba">00166</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#a1408abd4630ac1b2a5432f07636fb2ba">SP_NO_PARITY</a> = 0;
<a name="l00168"></a><a class="code" href="a00005.html#ac2c5b2c5c91965edc51ab6e12b1cf604">00168</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#ac2c5b2c5c91965edc51ab6e12b1cf604">SP_EVEN_PARITY</a> = M_UPM1;
<a name="l00170"></a><a class="code" href="a00005.html#a9625f0899cb845451b46f3aff94ffe01">00170</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#a9625f0899cb845451b46f3aff94ffe01">SP_ODD_PARITY</a> = M_UPM0 | M_UPM1;
<a name="l00171"></a>00171
<a name="l00173"></a><a class="code" href="a00005.html#a7fceca316014ed58d8a6bf003d56af5a">00173</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#a7fceca316014ed58d8a6bf003d56af5a">SP_5_BIT_CHAR</a> = 0;
<a name="l00175"></a><a class="code" href="a00005.html#a5cb767bae10b9aa5a02be00c5b570967">00175</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#a5cb767bae10b9aa5a02be00c5b570967">SP_6_BIT_CHAR</a> = M_UCSZ0;
<a name="l00177"></a><a class="code" href="a00005.html#a66e3e7fbd73f8b499b7062e3d9ac4981">00177</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#a66e3e7fbd73f8b499b7062e3d9ac4981">SP_7_BIT_CHAR</a> = M_UCSZ1;
<a name="l00179"></a><a class="code" href="a00005.html#aaacdef21e05f2054cef451d4f840317b">00179</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#aaacdef21e05f2054cef451d4f840317b">SP_8_BIT_CHAR</a> = M_UCSZ0 | M_UCSZ1;
<a name="l00181"></a><a class="code" href="a00005.html#ac81a11e48b532ac5ab752e866e5e51e6">00181</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#ac81a11e48b532ac5ab752e866e5e51e6">SP_OPT_MASK</a> = M_USBS | M_UPM0 | M_UPM1 |M_UCSZ0 | M_UCSZ1;
<a name="l00182"></a>00182
<a name="l00184"></a><a class="code" href="a00005.html#a2a7a0989d2d7a5f27a0eef14b4052f5e">00184</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#a2a7a0989d2d7a5f27a0eef14b4052f5e">SP_FRAMING_ERROR</a> = M_FE;
<a name="l00186"></a><a class="code" href="a00005.html#aba276deb6f876a8cf7e0f895cfbb37c8">00186</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#aba276deb6f876a8cf7e0f895cfbb37c8">SP_RX_DATA_OVERRUN</a> = M_DOR;
<a name="l00188"></a><a class="code" href="a00005.html#a2ea2d7ccb17a58a56c0726bf21e12b61">00188</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#a2ea2d7ccb17a58a56c0726bf21e12b61">SP_PARITY_ERROR</a> = M_UPE;
<a name="l00190"></a><a class="code" href="a00005.html#a672521fec83294e7cd4e2061bfb5765c">00190</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#a672521fec83294e7cd4e2061bfb5765c">SP_UCSRA_ERROR_MASK</a> = M_FE | M_DOR | M_UPE;
<a name="l00192"></a><a class="code" href="a00005.html#a2ad06f836f28d76e35006eabe2a70f53">00192</a> <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <a class="code" href="a00005.html#a2ad06f836f28d76e35006eabe2a70f53">SP_RX_BUF_OVERRUN</a> = 1;
<a name="l00193"></a>00193 <span class="preprocessor">#if 1 &amp; ((1 &lt;&lt; FE0) | (1 &lt;&lt; DOR0) |(1 &lt;&lt; UPE0))</span>
<a name="l00194"></a>00194 <span class="preprocessor"></span><span class="preprocessor">#error Invalid SP_RX_BUF_OVERRUN bit</span>
<a name="l00195"></a>00195 <span class="preprocessor"></span><span class="preprocessor">#endif // SP_RX_BUF_OVERRUN</span>
<a name="l00196"></a>00196 <span class="preprocessor"></span><span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00201"></a><a class="code" href="a00003.html">00201</a> <span class="comment"></span><span class="keyword">struct </span><a class="code" href="a00003.html" title="Addresses of USART registers.">UsartRegister</a> {
<a name="l00202"></a><a class="code" href="a00003.html#a5bdd2ef00c6c6c0c46c2901320e64a86">00202</a> <span class="keyword">volatile</span> uint8_t* <a class="code" href="a00003.html#a5bdd2ef00c6c6c0c46c2901320e64a86">ucsra</a>;
<a name="l00203"></a><a class="code" href="a00003.html#a354769340a12aa52981dba62d27c1668">00203</a> <span class="keyword">volatile</span> uint8_t* <a class="code" href="a00003.html#a354769340a12aa52981dba62d27c1668">ucsrb</a>;
<a name="l00204"></a><a class="code" href="a00003.html#a1a5bc79c9dce54bdf7f670a9dba2db47">00204</a> <span class="keyword">volatile</span> uint8_t* <a class="code" href="a00003.html#a1a5bc79c9dce54bdf7f670a9dba2db47">ucsrc</a>;
<a name="l00205"></a><a class="code" href="a00003.html#a622ae9987b714b0f8f6daeee146a8804">00205</a> <span class="keyword">volatile</span> uint8_t* <a class="code" href="a00003.html#a622ae9987b714b0f8f6daeee146a8804">ubrrl</a>;
<a name="l00206"></a><a class="code" href="a00003.html#a31bdf3a197f3db00c70d40806c8f3425">00206</a> <span class="keyword">volatile</span> uint8_t* <a class="code" href="a00003.html#a31bdf3a197f3db00c70d40806c8f3425">ubrrh</a>;
<a name="l00207"></a><a class="code" href="a00003.html#a57975075b179df2cb353ca5d633e5fa2">00207</a> <span class="keyword">volatile</span> uint8_t* <a class="code" href="a00003.html#a57975075b179df2cb353ca5d633e5fa2">udr</a>;
<a name="l00208"></a>00208 };
<a name="l00209"></a>00209 <span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00214"></a><a class="code" href="a00005.html#a2ca8ac27d295096da3a374bd1ef99492">00214</a> <span class="comment"></span><span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="a00003.html" title="Addresses of USART registers.">UsartRegister</a> <a class="code" href="a00005.html#a2ca8ac27d295096da3a374bd1ef99492">usart</a>[] = {
<a name="l00215"></a>00215 <span class="preprocessor">#ifdef UCSR0A</span>
<a name="l00216"></a>00216 <span class="preprocessor"></span> {&amp;UCSR0A, &amp;UCSR0B, &amp;UCSR0C, &amp;UBRR0L, &amp;UBRR0H, &amp;UDR0},
<a name="l00217"></a>00217 <span class="preprocessor">#elif defined(UCSRA)</span>
<a name="l00218"></a>00218 <span class="preprocessor"></span> {&amp;UCSRA, &amp;UCSRB, &amp;UCSRC, &amp;UBRRL, &amp;UBRRH, &amp;UDR},
<a name="l00219"></a>00219 <span class="preprocessor">#else // UCSR0A</span>
<a name="l00220"></a>00220 <span class="preprocessor"></span> {0, 0, 0, 0, 0, 0},
<a name="l00221"></a>00221 <span class="preprocessor">#endif // UCSR0A</span>
<a name="l00222"></a>00222 <span class="preprocessor"></span>
<a name="l00223"></a>00223 <span class="preprocessor">#ifdef UCSR1A</span>
<a name="l00224"></a>00224 <span class="preprocessor"></span> {&amp;UCSR1A, &amp;UCSR1B, &amp;UCSR1C, &amp;UBRR1L, &amp;UBRR1H, &amp;UDR1},
<a name="l00225"></a>00225 <span class="preprocessor">#else // UCSR1A</span>
<a name="l00226"></a>00226 <span class="preprocessor"></span> {0, 0, 0, 0, 0, 0},
<a name="l00227"></a>00227 <span class="preprocessor">#endif // UCSR1A</span>
<a name="l00228"></a>00228 <span class="preprocessor"></span>
<a name="l00229"></a>00229 <span class="preprocessor">#ifdef UCSR2A</span>
<a name="l00230"></a>00230 <span class="preprocessor"></span> {&amp;UCSR2A, &amp;UCSR2B, &amp;UCSR2C, &amp;UBRR2L, &amp;UBRR2H, &amp;UDR2},
<a name="l00231"></a>00231 <span class="preprocessor">#else // UCSR2A</span>
<a name="l00232"></a>00232 <span class="preprocessor"></span> {0, 0, 0, 0, 0, 0},
<a name="l00233"></a>00233 <span class="preprocessor">#endif // UCSR2A</span>
<a name="l00234"></a>00234 <span class="preprocessor"></span>
<a name="l00235"></a>00235 <span class="preprocessor">#ifdef UCSR3A</span>
<a name="l00236"></a>00236 <span class="preprocessor"></span> {&amp;UCSR3A, &amp;UCSR3B, &amp;UCSR3C, &amp;UBRR3L, &amp;UBRR3H, &amp;UDR3}
<a name="l00237"></a>00237 <span class="preprocessor">#else // UCSR3A</span>
<a name="l00238"></a>00238 <span class="preprocessor"></span> {0, 0, 0, 0, 0, 0}
<a name="l00239"></a>00239 <span class="preprocessor">#endif // UCSR3A</span>
<a name="l00240"></a>00240 <span class="preprocessor"></span>};
<a name="l00241"></a>00241 <span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00246"></a><a class="code" href="a00002.html">00246</a> <span class="comment"></span><span class="keyword">class </span><a class="code" href="a00002.html" title="Ring buffer for RX and TX data.">SerialRingBuffer</a> {
<a name="l00247"></a>00247 <span class="keyword">public</span>:
<a name="l00249"></a>00249 <span class="preprocessor">#if ALLOW_LARGE_BUFFERS</span>
<a name="l00250"></a><a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">00250</a> <span class="preprocessor"></span> <span class="keyword">typedef</span> uint16_t <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a>;
<a name="l00251"></a>00251 <span class="preprocessor">#else // ALLOW_LARGE_BUFFERS</span>
<a name="l00252"></a>00252 <span class="preprocessor"></span> <span class="keyword">typedef</span> uint8_t <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a>;
<a name="l00253"></a>00253 <span class="preprocessor">#endif // ALLOW_LARGE_BUFFERS</span>
<a name="l00254"></a>00254 <span class="preprocessor"></span> <span class="keywordtype">int</span> <a class="code" href="a00002.html#a6ddd7ba8df8faf8d2ad4f531598ce0ef">available</a>();
<a name="l00256"></a><a class="code" href="a00002.html#a59e5d391e818cabc7825e9ed1fbdf1d4">00256</a> <span class="keywordtype">bool</span> <a class="code" href="a00002.html#a59e5d391e818cabc7825e9ed1fbdf1d4">empty</a>() {<span class="keywordflow">return</span> head_ == tail_;}
<a name="l00257"></a>00257 <span class="keywordtype">void</span> <a class="code" href="a00002.html#aced2c894d319649da813ec2793b0e1ef">flush</a>();
<a name="l00258"></a>00258 <span class="keywordtype">bool</span> <span class="keyword">get</span>(uint8_t* b);
<a name="l00259"></a>00259 <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a> <span class="keyword">get</span>(uint8_t* b, <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a> n);
<a name="l00260"></a>00260 <span class="keywordtype">void</span> <a class="code" href="a00002.html#a685b9bf8f88ae50a19cfa966536013ca">init</a>(uint8_t* b, <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a> s);
<a name="l00261"></a>00261 <span class="keywordtype">int</span> <a class="code" href="a00002.html#a46f0f82e9272c91b8142ef866e630347">peek</a>();
<a name="l00262"></a>00262 <span class="keywordtype">bool</span> <a class="code" href="a00002.html#a8f1b71cc976869c4793d0c7cafb4a2d0">put</a>(uint8_t b);
<a name="l00263"></a>00263 <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a> <a class="code" href="a00002.html#a8f1b71cc976869c4793d0c7cafb4a2d0">put</a>(<span class="keyword">const</span> uint8_t* b, <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a> n);
<a name="l00264"></a>00264 <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a> <a class="code" href="a00002.html#ad358c07849edc45194672af52ba36b6e">put_P</a>(PGM_P b, <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a> n);
<a name="l00265"></a>00265 <span class="keyword">private</span>:
<a name="l00266"></a>00266 uint8_t* buf_;
<a name="l00267"></a>00267 <span class="keyword">volatile</span> <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a> head_;
<a name="l00268"></a>00268 <span class="keyword">volatile</span> <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a> tail_;
<a name="l00269"></a>00269 <a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">buf_size_t</a> size_;
<a name="l00270"></a>00270 };
<a name="l00271"></a>00271 <span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00273"></a>00273 <span class="comment"></span><span class="keyword">extern</span> <a class="code" href="a00002.html" title="Ring buffer for RX and TX data.">SerialRingBuffer</a> <a class="code" href="a00005.html#a9357d415ed299b0aa00c9333c7d1cf7f">rxRingBuf</a>[];
<a name="l00275"></a>00275 <span class="keyword">extern</span> <a class="code" href="a00002.html" title="Ring buffer for RX and TX data.">SerialRingBuffer</a> <a class="code" href="a00005.html#ad658e906f050f38e4de63e1ad7664b30">txRingBuf</a>[];
<a name="l00277"></a>00277 <span class="keyword">extern</span> uint8_t <a class="code" href="a00005.html#a122aa87d6d517cf6fef0ac595ade2a3b">rxErrorBits</a>[];
<a name="l00278"></a>00278 <span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00282"></a>00282 <span class="comment"></span>uint8_t <a class="code" href="a00005.html#afba54bf68161da2539416c1c79079322">badPortNumber</a>(<span class="keywordtype">void</span>)
<a name="l00283"></a>00283 __attribute__((error(&quot;Bad port number&quot;)));
<a name="l00287"></a>00287 uint8_t <a class="code" href="a00005.html#ae0695a8a8d72f8b8d99505fe349cace9">badRxBufSize</a>(<span class="keywordtype">void</span>)
<a name="l00288"></a>00288 __attribute__((error(&quot;RX buffer size too large&quot;)));
<a name="l00292"></a>00292 uint8_t <a class="code" href="a00005.html#a3e34f6fae0601bde1dc223e1dae0461c">badTxBufSize</a>(<span class="keywordtype">void</span>)
<a name="l00293"></a>00293 __attribute__((error(&quot;TX buffer size too large&quot;)));
<a name="l00294"></a>00294 <span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00299"></a>00299 <span class="comment"></span>template&lt;uint8_t PortNumber, <span class="keywordtype">size_t</span> RxBufSize, <span class="keywordtype">size_t</span> TxBufSize&gt;
<a name="l00300"></a><a class="code" href="a00001.html">00300</a> class <a class="code" href="a00001.html" title="Class for avr hardware USART ports.">SerialPort</a> : public Stream {
<a name="l00301"></a>00301 <span class="keyword">public</span>:
<a name="l00302"></a>00302 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00304"></a><a class="code" href="a00001.html#a33b60beed8333ea6902da22bcbfa8ef9">00304</a> <span class="comment"></span> <a class="code" href="a00001.html#a33b60beed8333ea6902da22bcbfa8ef9">SerialPort</a>() {
<a name="l00305"></a>00305 <span class="keywordflow">if</span> (PortNumber &gt;= SERIAL_PORT_COUNT || !usart[PortNumber].ucsra) {
<a name="l00306"></a>00306 <a class="code" href="a00005.html#afba54bf68161da2539416c1c79079322">badPortNumber</a>();
<a name="l00307"></a>00307 }
<a name="l00308"></a>00308 <span class="keywordflow">if</span> (<span class="keyword">sizeof</span>(<a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">SerialRingBuffer::buf_size_t</a>) == 1) {
<a name="l00309"></a>00309 <span class="keywordflow">if</span> (RxBufSize &gt; 254) <a class="code" href="a00005.html#ae0695a8a8d72f8b8d99505fe349cace9">badRxBufSize</a>();
<a name="l00310"></a>00310 <span class="keywordflow">if</span> (TxBufSize &gt; 254) <a class="code" href="a00005.html#a3e34f6fae0601bde1dc223e1dae0461c">badTxBufSize</a>();
<a name="l00311"></a>00311 }
<a name="l00312"></a>00312 <span class="keywordflow">if</span> (RxBufSize) rxRingBuf[PortNumber].<a class="code" href="a00002.html#a685b9bf8f88ae50a19cfa966536013ca">init</a>(rxBuffer_, <span class="keyword">sizeof</span>(rxBuffer_));
<a name="l00313"></a>00313 <span class="keywordflow">if</span> (TxBufSize) txRingBuf[PortNumber].<a class="code" href="a00002.html#a685b9bf8f88ae50a19cfa966536013ca">init</a>(txBuffer_, <span class="keyword">sizeof</span>(txBuffer_));
<a name="l00314"></a>00314 }
<a name="l00315"></a>00315 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00320"></a><a class="code" href="a00001.html#af1024de6e41a21d1f362fedae73a2b39">00320</a> <span class="comment"></span> <span class="keywordtype">int</span> <a class="code" href="a00001.html#af1024de6e41a21d1f362fedae73a2b39">available</a>(<span class="keywordtype">void</span>) {
<a name="l00321"></a>00321 <span class="keywordflow">if</span> (!RxBufSize) {
<a name="l00322"></a>00322 <span class="keywordflow">return</span> *usart[PortNumber].<a class="code" href="a00003.html#a5bdd2ef00c6c6c0c46c2901320e64a86">ucsra</a> &amp; M_RXC ? 1 : 0;
<a name="l00323"></a>00323 } <span class="keywordflow">else</span> {
<a name="l00324"></a>00324 <span class="keywordflow">return</span> rxRingBuf[PortNumber].<a class="code" href="a00002.html#a6ddd7ba8df8faf8d2ad4f531598ce0ef">available</a>();
<a name="l00325"></a>00325 }
<a name="l00326"></a>00326 }
<a name="l00327"></a>00327 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00349"></a><a class="code" href="a00001.html#a38b8cda8cb7c94a38de9f29ee48f4259">00349</a> <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="a00001.html#a38b8cda8cb7c94a38de9f29ee48f4259">begin</a>(uint32_t baud, uint8_t options = <a class="code" href="a00005.html#aaacdef21e05f2054cef451d4f840317b">SP_8_BIT_CHAR</a>) {
<a name="l00350"></a>00350 uint16_t baud_setting;
<a name="l00351"></a>00351
<a name="l00352"></a>00352 <span class="comment">// disable USART interrupts. Set UCSRB to reset values.</span>
<a name="l00353"></a>00353 *usart[PortNumber].<a class="code" href="a00003.html#a354769340a12aa52981dba62d27c1668">ucsrb</a> = 0;
<a name="l00354"></a>00354
<a name="l00355"></a>00355 <span class="comment">// set option bits</span>
<a name="l00356"></a>00356 *usart[PortNumber].<a class="code" href="a00003.html#a1a5bc79c9dce54bdf7f670a9dba2db47">ucsrc</a> = options &amp; <a class="code" href="a00005.html#ac81a11e48b532ac5ab752e866e5e51e6">SP_OPT_MASK</a>;
<a name="l00357"></a>00357
<a name="l00358"></a>00358 <span class="keywordflow">if</span> (F_CPU == 16000000UL &amp;&amp; baud == 57600) {
<a name="l00359"></a>00359 <span class="comment">// hardcoded exception for compatibility with the bootloader shipped</span>
<a name="l00360"></a>00360 <span class="comment">// with the Duemilanove and previous boards and the firmware on the 8U2</span>
<a name="l00361"></a>00361 <span class="comment">// on the Uno and Mega 2560.</span>
<a name="l00362"></a>00362 *usart[PortNumber].<a class="code" href="a00003.html#a5bdd2ef00c6c6c0c46c2901320e64a86">ucsra</a> = 0;
<a name="l00363"></a>00363 baud_setting = (F_CPU / 8 / baud - 1) / 2;
<a name="l00364"></a>00364 } <span class="keywordflow">else</span> {
<a name="l00365"></a>00365 *usart[PortNumber].<a class="code" href="a00003.html#a5bdd2ef00c6c6c0c46c2901320e64a86">ucsra</a> = M_U2X;
<a name="l00366"></a>00366 baud_setting = (F_CPU / 4 / baud - 1) / 2;
<a name="l00367"></a>00367 }
<a name="l00368"></a>00368 <span class="comment">// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)</span>
<a name="l00369"></a>00369 *usart[PortNumber].<a class="code" href="a00003.html#a31bdf3a197f3db00c70d40806c8f3425">ubrrh</a> = baud_setting &gt;&gt; 8;
<a name="l00370"></a>00370 *usart[PortNumber].<a class="code" href="a00003.html#a622ae9987b714b0f8f6daeee146a8804">ubrrl</a> = baud_setting;
<a name="l00371"></a>00371
<a name="l00372"></a>00372 <span class="comment">// enable RX and TX</span>
<a name="l00373"></a>00373 uint8_t bits = M_TXEN | M_RXEN;
<a name="l00374"></a>00374
<a name="l00375"></a>00375 <span class="comment">// enable receive interrupt if buffered</span>
<a name="l00376"></a>00376 <span class="keywordflow">if</span> (RxBufSize) bits |= M_RXCIE;
<a name="l00377"></a>00377 *usart[PortNumber].<a class="code" href="a00003.html#a354769340a12aa52981dba62d27c1668">ucsrb</a> = bits;
<a name="l00378"></a>00378 }
<a name="l00379"></a>00379 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00380"></a>00380 <span class="preprocessor"> #if ENABLE_RX_ERROR_CHECKING</span>
<a name="l00381"></a>00381 <span class="preprocessor"></span>
<a name="l00382"></a><a class="code" href="a00001.html#a1de98218b494b92e683624aed3363386">00382</a> <span class="keywordtype">void</span> <a class="code" href="a00001.html#a1de98218b494b92e683624aed3363386">clearRxError</a>() {<a class="code" href="a00005.html#a122aa87d6d517cf6fef0ac595ade2a3b">rxErrorBits</a>[PortNumber] = 0;}
<a name="l00390"></a><a class="code" href="a00001.html#a1987a9d938995932929830be0adf4bf6">00390</a> uint8_t <a class="code" href="a00001.html#a1987a9d938995932929830be0adf4bf6">getRxError</a>() {<span class="keywordflow">return</span> <a class="code" href="a00005.html#a122aa87d6d517cf6fef0ac595ade2a3b">rxErrorBits</a>[PortNumber];}
<a name="l00391"></a>00391 <span class="preprocessor"> #endif // ENABLE_RX_ERROR_CHECKING</span>
<a name="l00392"></a>00392 <span class="preprocessor"></span> <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00398"></a><a class="code" href="a00001.html#a2f3d7c793365fcd861d3cce795d986f8">00398</a> <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="a00001.html#a2f3d7c793365fcd861d3cce795d986f8">end</a>() {
<a name="l00399"></a>00399 <span class="comment">// wait for transmission of outgoing data</span>
<a name="l00400"></a>00400 flushTx();
<a name="l00401"></a>00401 <span class="comment">// disable USART</span>
<a name="l00402"></a>00402 cli();
<a name="l00403"></a>00403 *usart[PortNumber].<a class="code" href="a00003.html#a354769340a12aa52981dba62d27c1668">ucsrb</a> &amp;= ~(M_RXEN | M_TXEN | M_RXCIE | M_UDRIE);
<a name="l00404"></a>00404 sei();
<a name="l00405"></a>00405 <span class="comment">// clear any received data</span>
<a name="l00406"></a>00406 flushRx();
<a name="l00407"></a>00407 }
<a name="l00408"></a>00408 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00412"></a><a class="code" href="a00001.html#a980a0188ad3ad64df2814462a5ebc707">00412</a> <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="a00001.html#a980a0188ad3ad64df2814462a5ebc707">flush</a>() {flushTx();}
<a name="l00413"></a>00413 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00417"></a><a class="code" href="a00001.html#ad2757ef24789a658405832447ae66bf5">00417</a> <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="a00001.html#ad2757ef24789a658405832447ae66bf5">flushRx</a>() {
<a name="l00418"></a>00418 <span class="keywordflow">if</span> (RxBufSize) {
<a name="l00419"></a>00419 rxRingBuf[PortNumber].<a class="code" href="a00002.html#aced2c894d319649da813ec2793b0e1ef">flush</a>();
<a name="l00420"></a>00420 } <span class="keywordflow">else</span> {
<a name="l00421"></a>00421 uint8_t b;
<a name="l00422"></a>00422 <span class="keywordflow">while</span> (*usart[PortNumber].ucsra &amp; M_RXC) b = *usart[PortNumber].<a class="code" href="a00003.html#a57975075b179df2cb353ca5d633e5fa2">udr</a>;
<a name="l00423"></a>00423 }
<a name="l00424"></a>00424 }
<a name="l00425"></a>00425 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00429"></a><a class="code" href="a00001.html#abb49b6680762ee9b87fc9b0c578fb878">00429</a> <span class="comment"></span> <span class="keywordtype">void</span> <a class="code" href="a00001.html#abb49b6680762ee9b87fc9b0c578fb878">flushTx</a>() {
<a name="l00430"></a>00430 <span class="keywordflow">if</span> (TxBufSize) {
<a name="l00431"></a>00431 <span class="keywordflow">while</span> (!txRingBuf[PortNumber].empty()) {}
<a name="l00432"></a>00432 }
<a name="l00433"></a>00433 }
<a name="l00434"></a>00434 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00439"></a><a class="code" href="a00001.html#aa467bbb3efb104c8f52cba13ef3b30a0">00439</a> <span class="comment"></span> <span class="keywordtype">int</span> <a class="code" href="a00001.html#aa467bbb3efb104c8f52cba13ef3b30a0">peek</a>(<span class="keywordtype">void</span>) {
<a name="l00440"></a>00440 <span class="keywordflow">return</span> RxBufSize ? rxRingBuf[PortNumber].<a class="code" href="a00002.html#a46f0f82e9272c91b8142ef866e630347">peek</a>() : -1;
<a name="l00441"></a>00441 }
<a name="l00442"></a>00442 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00449"></a>00449 <span class="comment"></span> __attribute__((noinline))
<a name="l00450"></a><a class="code" href="a00001.html#a6f20328c42db732c9243ce3a90d1e029">00450</a> int read() {
<a name="l00451"></a>00451 <span class="keywordflow">if</span> (!RxBufSize) {
<a name="l00452"></a>00452 uint8_t s = *usart[PortNumber].<a class="code" href="a00003.html#a5bdd2ef00c6c6c0c46c2901320e64a86">ucsra</a>;
<a name="l00453"></a>00453 <span class="preprocessor"> #if ENABLE_RX_ERROR_CHECKING</span>
<a name="l00454"></a>00454 <span class="preprocessor"></span> <a class="code" href="a00005.html#a122aa87d6d517cf6fef0ac595ade2a3b">rxErrorBits</a>[PortNumber] |= s &amp; <a class="code" href="a00005.html#a672521fec83294e7cd4e2061bfb5765c">SP_UCSRA_ERROR_MASK</a>;
<a name="l00455"></a>00455 <span class="preprocessor"> #endif // ENABLE_RX_ERROR_CHECKING</span>
<a name="l00456"></a>00456 <span class="preprocessor"></span> <span class="keywordflow">return</span> s &amp; M_RXC ? *usart[PortNumber].<a class="code" href="a00003.html#a57975075b179df2cb353ca5d633e5fa2">udr</a> : -1;
<a name="l00457"></a>00457 } <span class="keywordflow">else</span> {
<a name="l00458"></a>00458 uint8_t b;
<a name="l00459"></a>00459 <span class="keywordflow">return</span> rxRingBuf[PortNumber].<a class="code" href="a00002.html#a8311099fd2c67ec9e8359f69bac74494">get</a>(&amp;b) ? b : -1;
<a name="l00460"></a>00460 }
<a name="l00461"></a>00461 }
<a name="l00462"></a>00462 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00471"></a>00471 <span class="comment"></span> __attribute__((noinline))
<a name="l00472"></a><a class="code" href="a00001.html#acb3106febfe5e47077bfa3439b4631c0">00472</a> size_t read(uint8_t* b, <span class="keywordtype">size_t</span> n) {
<a name="l00473"></a>00473 uint8_t* limit = b + n;
<a name="l00474"></a>00474 uint8_t* p = b;
<a name="l00475"></a>00475 <span class="keywordflow">if</span> (RxBufSize) {
<a name="l00476"></a>00476 <span class="keywordflow">while</span> (p &lt; limit &amp;&amp; !rxRingBuf[PortNumber].empty()) {
<a name="l00477"></a>00477 <span class="keywordtype">size_t</span> nr = limit - p;
<a name="l00478"></a>00478 <span class="keywordflow">if</span> (<span class="keyword">sizeof</span>(<a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">SerialRingBuffer::buf_size_t</a>) == 1 &amp;&amp; nr &gt; 255) nr = 255;
<a name="l00479"></a>00479 p += rxRingBuf[PortNumber].<a class="code" href="a00002.html#a8311099fd2c67ec9e8359f69bac74494">get</a>(p, nr);
<a name="l00480"></a>00480 }
<a name="l00481"></a>00481 } <span class="keywordflow">else</span> {
<a name="l00482"></a>00482 <span class="keywordflow">while</span> (p &lt; limit) {
<a name="l00483"></a>00483 <span class="keywordtype">int</span> rb = read();
<a name="l00484"></a>00484 <span class="keywordflow">if</span> (rb &lt; 0) <span class="keywordflow">break</span>;
<a name="l00485"></a>00485 *p++ = rb;
<a name="l00486"></a>00486 }
<a name="l00487"></a>00487 }
<a name="l00488"></a>00488 <span class="keywordflow">return</span> p - b;
<a name="l00489"></a>00489 }
<a name="l00490"></a>00490 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00497"></a>00497 <span class="comment"></span> __attribute__((noinline))
<a name="l00498"></a><a class="code" href="a00001.html#a47aa3f8bdc6709e92275ac7d26e8cdb2">00498</a> size_t write(uint8_t b) {
<a name="l00499"></a>00499 <span class="keywordflow">if</span> (!TxBufSize) {
<a name="l00500"></a>00500 <span class="keywordflow">while</span> (!(*usart[PortNumber].ucsra &amp; M_UDRE)) {}
<a name="l00501"></a>00501 *usart[PortNumber].<a class="code" href="a00003.html#a57975075b179df2cb353ca5d633e5fa2">udr</a> = b;
<a name="l00502"></a>00502 } <span class="keywordflow">else</span> {
<a name="l00503"></a>00503 <span class="comment">// Wait for TX ISR if buffer is full.</span>
<a name="l00504"></a>00504 <span class="keywordflow">while</span> (!txRingBuf[PortNumber].put(b)) {}
<a name="l00505"></a>00505
<a name="l00506"></a>00506 <span class="comment">// Enable interrupts.</span>
<a name="l00507"></a>00507 *usart[PortNumber].<a class="code" href="a00003.html#a354769340a12aa52981dba62d27c1668">ucsrb</a> |= M_UDRIE;
<a name="l00508"></a>00508 }
<a name="l00509"></a>00509 <span class="keywordflow">return</span> 1;
<a name="l00510"></a>00510 }
<a name="l00511"></a>00511 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00515"></a>00515 <span class="comment"></span> __attribute__((noinline))
<a name="l00516"></a><a class="code" href="a00001.html#a903a10dc74404eda0ec8ae8b7eac93c2">00516</a> size_t writeln() {
<a name="l00517"></a>00517 write(<span class="charliteral">&#39;\r&#39;</span>);
<a name="l00518"></a>00518 write(<span class="charliteral">&#39;\n&#39;</span>);
<a name="l00519"></a>00519 <span class="keywordflow">return</span> 2;
<a name="l00520"></a>00520 }
<a name="l00521"></a>00521 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00528"></a>00528 <span class="comment"></span> __attribute__((noinline))
<a name="l00529"></a><a class="code" href="a00001.html#a44c4ee83aa047ba1c458ca0969a9bf47">00529</a> size_t writeln(const <span class="keywordtype">char</span>* s) {
<a name="l00530"></a>00530 <span class="keywordflow">return</span> write(s) + writeln();
<a name="l00531"></a>00531 }
<a name="l00532"></a>00532
<a name="l00533"></a>00533 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00541"></a>00541 <span class="comment"></span> __attribute__((noinline))
<a name="l00542"></a><a class="code" href="a00001.html#a3e1d910680b806a12b8505c8f20d0a76">00542</a> size_t write_P(PGM_P b, <span class="keywordtype">size_t</span> n) {
<a name="l00543"></a>00543 <span class="keywordflow">if</span> (!TxBufSize) {
<a name="l00544"></a>00544 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; n; i++) write(pgm_read_byte(b + i));
<a name="l00545"></a>00545 } <span class="keywordflow">else</span> {
<a name="l00546"></a>00546 <span class="keywordtype">size_t</span> w = n;
<a name="l00547"></a>00547 <span class="keywordflow">while</span> (w) {
<a name="l00548"></a>00548 <span class="keywordtype">size_t</span> nw = w;
<a name="l00549"></a>00549 <span class="keywordflow">if</span> (<span class="keyword">sizeof</span>(<a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">SerialRingBuffer::buf_size_t</a>) == 1 &amp;&amp; nw &gt; 255) nw = 255;
<a name="l00550"></a>00550 <span class="keywordtype">size_t</span> m = txRingBuf[PortNumber].<a class="code" href="a00002.html#ad358c07849edc45194672af52ba36b6e">put_P</a>(b, nw);
<a name="l00551"></a>00551
<a name="l00552"></a>00552 <span class="comment">// enable interrupts</span>
<a name="l00553"></a>00553 *usart[PortNumber].<a class="code" href="a00003.html#a354769340a12aa52981dba62d27c1668">ucsrb</a> |= M_UDRIE;
<a name="l00554"></a>00554 w -= m;
<a name="l00555"></a>00555 b += m;
<a name="l00556"></a>00556 }
<a name="l00557"></a>00557 }
<a name="l00558"></a>00558 <span class="keywordflow">return</span> n;
<a name="l00559"></a>00559 }
<a name="l00560"></a>00560 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00567"></a>00567 <span class="comment"></span> __attribute__((noinline))
<a name="l00568"></a><a class="code" href="a00001.html#a63bb0a672b9bbe05d798379e3cfb57b3">00568</a> size_t write(const __FlashStringHelper* s) {
<a name="l00569"></a>00569 <span class="keyword">const</span> <span class="keywordtype">char</span> PROGMEM* p = (<span class="keyword">const</span> <span class="keywordtype">char</span> PROGMEM*)s;
<a name="l00570"></a>00570 <span class="keywordtype">size_t</span> n = strlen_P(p);
<a name="l00571"></a>00571 <span class="keywordflow">return</span> write_P(p, n);
<a name="l00572"></a>00572 }
<a name="l00573"></a>00573 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00580"></a>00580 <span class="comment"></span> __attribute__((noinline))
<a name="l00581"></a><a class="code" href="a00001.html#ae783622f9524b84c547577dff64a3d73">00581</a> size_t writeln(const __FlashStringHelper* s) {
<a name="l00582"></a>00582 <span class="keywordflow">return</span> write(s) + writeln();
<a name="l00583"></a>00583 }
<a name="l00584"></a>00584 <span class="preprocessor"> #if USE_WRITE_OVERRIDES</span>
<a name="l00585"></a>00585 <span class="preprocessor"></span> <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00593"></a>00593 <span class="comment"></span> __attribute__((noinline))
<a name="l00594"></a><a class="code" href="a00001.html#ae56f856288b08e2b7304a79f62081947">00594</a> size_t write(const uint8_t* b, <span class="keywordtype">size_t</span> n) {
<a name="l00595"></a>00595 <span class="keywordflow">if</span> (!TxBufSize) {
<a name="l00596"></a>00596 <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; n; i++) write(b[i]);
<a name="l00597"></a>00597 } <span class="keywordflow">else</span> {
<a name="l00598"></a>00598 <span class="keywordtype">size_t</span> w = n;
<a name="l00599"></a>00599 <span class="keywordflow">while</span> (w) {
<a name="l00600"></a>00600 <span class="keywordtype">size_t</span> nw = w;
<a name="l00601"></a>00601 <span class="keywordflow">if</span> (<span class="keyword">sizeof</span>(<a class="code" href="a00002.html#ac7bbaea80ad0ee33dbb5223d240784c9">SerialRingBuffer::buf_size_t</a>) == 1 &amp;&amp; nw &gt; 255) nw = 255;
<a name="l00602"></a>00602 <span class="keywordtype">size_t</span> m = txRingBuf[PortNumber].<a class="code" href="a00002.html#a8f1b71cc976869c4793d0c7cafb4a2d0">put</a>(b, nw);
<a name="l00603"></a>00603
<a name="l00604"></a>00604 <span class="comment">// Enable interrupts.</span>
<a name="l00605"></a>00605 *usart[PortNumber].<a class="code" href="a00003.html#a354769340a12aa52981dba62d27c1668">ucsrb</a> |= M_UDRIE;
<a name="l00606"></a>00606 w -= m;
<a name="l00607"></a>00607 b += m;
<a name="l00608"></a>00608 }
<a name="l00609"></a>00609 }
<a name="l00610"></a>00610 <span class="keywordflow">return</span> n;
<a name="l00611"></a>00611 }
<a name="l00612"></a>00612 <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00619"></a>00619 <span class="comment"></span> __attribute__((noinline))
<a name="l00620"></a><a class="code" href="a00001.html#aae0a527e45a020a56fb3ec50bc7dee3b">00620</a> size_t write(const <span class="keywordtype">char</span>* s) {
<a name="l00621"></a>00621 <span class="keywordtype">size_t</span> n = strlen(s);
<a name="l00622"></a>00622 <span class="keywordflow">return</span> write(reinterpret_cast&lt;const uint8_t*&gt;(s), n);
<a name="l00623"></a>00623 }
<a name="l00624"></a>00624 <span class="preprocessor"> #else // USE_WRITE_OVERRIDES</span>
<a name="l00625"></a>00625 <span class="preprocessor"></span> <span class="keyword">using</span> Print::write; <span class="comment">// use write(str) and write(buf, size) from Print</span>
<a name="l00626"></a>00626 <span class="preprocessor"> #endif // USE_WRITE_OVERRIDES</span>
<a name="l00627"></a>00627 <span class="preprocessor"></span> <span class="comment">//----------------------------------------------------------------------------</span>
<a name="l00628"></a>00628 <span class="keyword">private</span>:
<a name="l00629"></a>00629 <span class="comment">// RX buffer with a capacity of RxBufSize.</span>
<a name="l00630"></a>00630 uint8_t rxBuffer_[RxBufSize + 1];
<a name="l00631"></a>00631 <span class="comment">// TX buffer with a capacity of TxBufSize.</span>
<a name="l00632"></a>00632 uint8_t txBuffer_[TxBufSize + 1];
<a name="l00633"></a>00633 };
<a name="l00634"></a>00634 <span class="comment">//------------------------------------------------------------------------------</span>
<a name="l00635"></a>00635 <span class="preprocessor">#endif // SerialPort_h</span>
</pre></div></div>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Defines</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Fri Feb 22 2013 11:38:19 for Serial Port by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>
</html>