| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> |
||
| 3 | <title>Procyon AVRlib: uartsw.c Source File</title> |
||
| 4 | <link href="dox.css" rel="stylesheet" type="text/css"> |
||
| 5 | </head><body> |
||
| 6 | <!-- Generated by Doxygen 1.4.2 --> |
||
| 7 | <div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div> |
||
| 8 | <h1>uartsw.c</h1><a href="uartsw_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file uartsw.c \brief Software Interrupt-driven UART Driver. */</span> |
||
| 9 | 00002 <span class="comment">//*****************************************************************************</span> |
||
| 10 | 00003 <span class="comment">//</span> |
||
| 11 | 00004 <span class="comment">// File Name : 'uartsw.c'</span> |
||
| 12 | 00005 <span class="comment">// Title : Software Interrupt-driven UART Driver</span> |
||
| 13 | 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2002-2004</span> |
||
| 14 | 00007 <span class="comment">// Created : 7/20/2002</span> |
||
| 15 | 00008 <span class="comment">// Revised : 4/27/2004</span> |
||
| 16 | 00009 <span class="comment">// Version : 0.1</span> |
||
| 17 | 00010 <span class="comment">// Target MCU : Atmel AVR Series (intended for the ATmega16 and ATmega32)</span> |
||
| 18 | 00011 <span class="comment">// Editor Tabs : 4</span> |
||
| 19 | 00012 <span class="comment">//</span> |
||
| 20 | 00013 <span class="comment">// This code is distributed under the GNU Public License</span> |
||
| 21 | 00014 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span> |
||
| 22 | 00015 <span class="comment">//</span> |
||
| 23 | 00016 <span class="comment">//*****************************************************************************</span> |
||
| 24 | 00017 |
||
| 25 | 00018 <span class="preprocessor">#include <avr/io.h></span> |
||
| 26 | 00019 <span class="preprocessor">#include <avr/interrupt.h></span> |
||
| 27 | 00020 |
||
| 28 | 00021 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> |
||
| 29 | 00022 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span> |
||
| 30 | 00023 <span class="preprocessor">#include "<a class="code" href="uartsw_8h.html">uartsw.h</a>"</span> |
||
| 31 | 00024 |
||
| 32 | 00025 <span class="comment">// Program ROM constants</span> |
||
| 33 | 00026 |
||
| 34 | 00027 <span class="comment">// Global variables</span> |
||
| 35 | 00028 |
||
| 36 | 00029 <span class="comment">// uartsw transmit status and data variables</span> |
||
| 37 | 00030 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswTxBusy; |
||
| 38 | 00031 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswTxData; |
||
| 39 | 00032 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswTxBitNum; |
||
| 40 | 00033 |
||
| 41 | 00034 <span class="comment">// baud rate common to transmit and receive</span> |
||
| 42 | 00035 <span class="keyword">static</span> <span class="keyword">volatile</span> u16 UartswBaudRateDiv; |
||
| 43 | 00036 |
||
| 44 | 00037 <span class="comment">// uartsw receive status and data variables</span> |
||
| 45 | 00038 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswRxBusy; |
||
| 46 | 00039 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswRxData; |
||
| 47 | 00040 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswRxBitNum; |
||
| 48 | 00041 <span class="comment">// receive buffer</span> |
||
| 49 | 00042 <span class="keyword">static</span> <a class="code" href="structstruct__cBuffer.html">cBuffer</a> uartswRxBuffer; <span class="comment">///< uartsw receive buffer</span> |
||
| 50 | 00043 <span class="comment"></span><span class="comment">// automatically allocate space in ram for each buffer</span> |
||
| 51 | 00044 <span class="keyword">static</span> <span class="keywordtype">char</span> uartswRxData[<a class="code" href="uartsw2conf_8h.html#a0">UARTSW_RX_BUFFER_SIZE</a>]; |
||
| 52 | 00045 |
||
| 53 | 00046 <span class="comment">// functions</span> |
||
| 54 | 00047 <span class="comment"></span> |
||
| 55 | 00048 <span class="comment">//! enable and initialize the software uart</span> |
||
| 56 | <a name="l00049"></a><a class="code" href="uartsw2_8h.html#a0">00049</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a9">uartswInit</a>(<span class="keywordtype">void</span>) |
||
| 57 | 00050 { |
||
| 58 | 00051 <span class="comment">// initialize the buffers</span> |
||
| 59 | 00052 <a class="code" href="uartsw_8c.html#a10">uartswInitBuffers</a>(); |
||
| 60 | 00053 <span class="comment">// initialize the ports</span> |
||
| 61 | 00054 sbi(<a class="code" href="uartsw2conf_8h.html#a3">UARTSW_TX_DDR</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>); |
||
| 62 | 00055 cbi(<a class="code" href="uartsw2conf_8h.html#a6">UARTSW_RX_DDR</a>, <a class="code" href="uartsw2conf_8h.html#a8">UARTSW_RX_PIN</a>); |
||
| 63 | 00056 cbi(<a class="code" href="uartsw2conf_8h.html#a5">UARTSW_RX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a8">UARTSW_RX_PIN</a>); |
||
| 64 | 00057 <span class="comment">// initialize baud rate</span> |
||
| 65 | 00058 <a class="code" href="uartsw_8c.html#a12">uartswSetBaudRate</a>(9600); |
||
| 66 | 00059 |
||
| 67 | 00060 <span class="comment">// setup the transmitter</span> |
||
| 68 | 00061 UartswTxBusy = FALSE; |
||
| 69 | 00062 <span class="comment">// disable OC1A interrupt</span> |
||
| 70 | 00063 cbi(TIMSK, OCIE1A); |
||
| 71 | 00064 <span class="comment">// attach TxBit service routine to OC1A</span> |
||
| 72 | 00065 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER1OUTCOMPAREA_INT, <a class="code" href="uartsw_8c.html#a16">uartswTxBitService</a>); |
||
| 73 | 00066 |
||
| 74 | 00067 <span class="comment">// setup the receiver</span> |
||
| 75 | 00068 UartswRxBusy = FALSE; |
||
| 76 | 00069 <span class="comment">// disable OC1B interrupt</span> |
||
| 77 | 00070 cbi(TIMSK, OCIE1B); |
||
| 78 | 00071 <span class="comment">// attach RxBit service routine to OC1B</span> |
||
| 79 | 00072 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER1OUTCOMPAREB_INT, <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>); |
||
| 80 | 00073 <span class="comment">// attach RxBit service routine to ICP</span> |
||
| 81 | 00074 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER1INPUTCAPTURE_INT, <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>); |
||
| 82 | 00075 <span class="preprocessor"> #ifdef UARTSW_INVERT </span> |
||
| 83 | 00076 <span class="preprocessor"></span> <span class="comment">// trigger on rising edge </span> |
||
| 84 | 00077 sbi(TCCR1B, ICES1); |
||
| 85 | 00078 <span class="preprocessor"> #else </span> |
||
| 86 | 00079 <span class="preprocessor"></span> <span class="comment">// trigger on falling edge </span> |
||
| 87 | 00080 cbi(TCCR1B, ICES1); |
||
| 88 | 00081 <span class="preprocessor"> #endif </span> |
||
| 89 | 00082 <span class="preprocessor"></span> <span class="comment">// enable ICP interrupt</span> |
||
| 90 | 00083 sbi(TIMSK, TICIE1); |
||
| 91 | 00084 |
||
| 92 | 00085 <span class="comment">// turn on interrupts</span> |
||
| 93 | 00086 sei(); |
||
| 94 | 00087 } |
||
| 95 | 00088 <span class="comment"></span> |
||
| 96 | 00089 <span class="comment">//! create and initialize the uart buffers</span> |
||
| 97 | <a name="l00090"></a><a class="code" href="uartsw2_8h.html#a1">00090</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a10">uartswInitBuffers</a>(<span class="keywordtype">void</span>) |
||
| 98 | 00091 { |
||
| 99 | 00092 <span class="comment">// initialize the UART receive buffer</span> |
||
| 100 | 00093 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&uartswRxBuffer, uartswRxData, <a class="code" href="uartsw2conf_8h.html#a0">UARTSW_RX_BUFFER_SIZE</a>); |
||
| 101 | 00094 } |
||
| 102 | 00095 <span class="comment"></span> |
||
| 103 | 00096 <span class="comment">//! turns off software UART</span> |
||
| 104 | <a name="l00097"></a><a class="code" href="uartsw2_8h.html#a2">00097</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a11">uartswOff</a>(<span class="keywordtype">void</span>) |
||
| 105 | 00098 { |
||
| 106 | 00099 <span class="comment">// disable interrupts</span> |
||
| 107 | 00100 cbi(TIMSK, OCIE1A); |
||
| 108 | 00101 cbi(TIMSK, OCIE1B); |
||
| 109 | 00102 cbi(TIMSK, TICIE1); |
||
| 110 | 00103 <span class="comment">// detach the service routines</span> |
||
| 111 | 00104 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER1OUTCOMPAREA_INT); |
||
| 112 | 00105 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER1OUTCOMPAREB_INT); |
||
| 113 | 00106 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER1INPUTCAPTURE_INT); |
||
| 114 | 00107 } |
||
| 115 | 00108 |
||
| 116 | <a name="l00109"></a><a class="code" href="uartsw2_8h.html#a4">00109</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a12">uartswSetBaudRate</a>(u32 baudrate) |
||
| 117 | 00110 { |
||
| 118 | 00111 <span class="comment">// set timer prescaler</span> |
||
| 119 | 00112 <a class="code" href="group__timer.html#ga6">timer1SetPrescaler</a>(<a class="code" href="group__timer.html#ga16">TIMER_CLK_DIV1</a>); |
||
| 120 | 00113 <span class="comment">// calculate division factor for requested baud rate, and set it</span> |
||
| 121 | 00114 UartswBaudRateDiv = (u16)((F_CPU+(baudrate/2L))/(baudrate*1L)); |
||
| 122 | 00115 } |
||
| 123 | 00116 <span class="comment"></span> |
||
| 124 | 00117 <span class="comment">//! returns the receive buffer structure </span> |
||
| 125 | <a name="l00118"></a><a class="code" href="uartsw2_8h.html#a3">00118</a> <span class="comment"></span><a class="code" href="structstruct__cBuffer.html">cBuffer</a>* <a class="code" href="uartsw_8c.html#a13">uartswGetRxBuffer</a>(<span class="keywordtype">void</span>) |
||
| 126 | 00119 { |
||
| 127 | 00120 <span class="comment">// return rx buffer pointer</span> |
||
| 128 | 00121 <span class="keywordflow">return</span> &uartswRxBuffer; |
||
| 129 | 00122 } |
||
| 130 | 00123 |
||
| 131 | <a name="l00124"></a><a class="code" href="uartsw2_8h.html#a5">00124</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a14">uartswSendByte</a>(u08 data) |
||
| 132 | 00125 { |
||
| 133 | 00126 <span class="comment">// wait until uart is ready</span> |
||
| 134 | 00127 <span class="keywordflow">while</span>(UartswTxBusy); |
||
| 135 | 00128 <span class="comment">// set busy flag</span> |
||
| 136 | 00129 UartswTxBusy = TRUE; |
||
| 137 | 00130 <span class="comment">// save data</span> |
||
| 138 | 00131 UartswTxData = data; |
||
| 139 | 00132 <span class="comment">// set number of bits (+1 for stop bit)</span> |
||
| 140 | 00133 UartswTxBitNum = 9; |
||
| 141 | 00134 |
||
| 142 | 00135 <span class="comment">// set the start bit</span> |
||
| 143 | 00136 <span class="preprocessor"> #ifdef UARTSW_INVERT</span> |
||
| 144 | 00137 <span class="preprocessor"></span> sbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>); |
||
| 145 | 00138 <span class="preprocessor"> #else</span> |
||
| 146 | 00139 <span class="preprocessor"></span> cbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>); |
||
| 147 | 00140 <span class="preprocessor"> #endif</span> |
||
| 148 | 00141 <span class="preprocessor"></span> |
||
| 149 | 00142 <span class="comment">// schedule the next bit</span> |
||
| 150 | 00143 outw(OCR1A, inw(TCNT1) + UartswBaudRateDiv); |
||
| 151 | 00144 <span class="comment">// enable OC1A interrupt</span> |
||
| 152 | 00145 sbi(TIMSK, OCIE1A); |
||
| 153 | 00146 } |
||
| 154 | 00147 <span class="comment"></span> |
||
| 155 | 00148 <span class="comment">//! gets a byte (if available) from the uart receive buffer</span> |
||
| 156 | <a name="l00149"></a><a class="code" href="uartsw2_8h.html#a6">00149</a> <span class="comment"></span>u08 <a class="code" href="uartsw_8c.html#a15">uartswReceiveByte</a>(u08* rxData) |
||
| 157 | 00150 { |
||
| 158 | 00151 <span class="comment">// make sure we have a receive buffer</span> |
||
| 159 | 00152 <span class="keywordflow">if</span>(uartswRxBuffer.<a class="code" href="structstruct__cBuffer.html#o1">size</a>) |
||
| 160 | 00153 { |
||
| 161 | 00154 <span class="comment">// make sure we have data</span> |
||
| 162 | 00155 <span class="keywordflow">if</span>(uartswRxBuffer.<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>) |
||
| 163 | 00156 { |
||
| 164 | 00157 <span class="comment">// get byte from beginning of buffer</span> |
||
| 165 | 00158 *rxData = <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&uartswRxBuffer); |
||
| 166 | 00159 <span class="keywordflow">return</span> TRUE; |
||
| 167 | 00160 } |
||
| 168 | 00161 <span class="keywordflow">else</span> |
||
| 169 | 00162 { |
||
| 170 | 00163 <span class="comment">// no data</span> |
||
| 171 | 00164 <span class="keywordflow">return</span> FALSE; |
||
| 172 | 00165 } |
||
| 173 | 00166 } |
||
| 174 | 00167 <span class="keywordflow">else</span> |
||
| 175 | 00168 { |
||
| 176 | 00169 <span class="comment">// no buffer</span> |
||
| 177 | 00170 <span class="keywordflow">return</span> FALSE; |
||
| 178 | 00171 } |
||
| 179 | 00172 } |
||
| 180 | 00173 |
||
| 181 | <a name="l00174"></a><a class="code" href="uartsw2_8h.html#a7">00174</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a16">uartswTxBitService</a>(<span class="keywordtype">void</span>) |
||
| 182 | 00175 { |
||
| 183 | 00176 <span class="keywordflow">if</span>(UartswTxBitNum) |
||
| 184 | 00177 { |
||
| 185 | 00178 <span class="comment">// there are bits still waiting to be transmitted</span> |
||
| 186 | 00179 <span class="keywordflow">if</span>(UartswTxBitNum > 1) |
||
| 187 | 00180 { |
||
| 188 | 00181 <span class="comment">// transmit data bits (inverted, LSB first)</span> |
||
| 189 | 00182 <span class="preprocessor"> #ifdef UARTSW_INVERT</span> |
||
| 190 | 00183 <span class="preprocessor"></span> <span class="keywordflow">if</span>( !(UartswTxData & 0x01) ) |
||
| 191 | 00184 <span class="preprocessor"> #else</span> |
||
| 192 | 00185 <span class="preprocessor"></span> <span class="keywordflow">if</span>( (UartswTxData & 0x01) ) |
||
| 193 | 00186 <span class="preprocessor"> #endif</span> |
||
| 194 | 00187 <span class="preprocessor"></span> sbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>); |
||
| 195 | 00188 <span class="keywordflow">else</span> |
||
| 196 | 00189 cbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>); |
||
| 197 | 00190 <span class="comment">// shift bits down</span> |
||
| 198 | 00191 UartswTxData = UartswTxData>>1; |
||
| 199 | 00192 } |
||
| 200 | 00193 <span class="keywordflow">else</span> |
||
| 201 | 00194 { |
||
| 202 | 00195 <span class="comment">// transmit stop bit</span> |
||
| 203 | 00196 <span class="preprocessor"> #ifdef UARTSW_INVERT</span> |
||
| 204 | 00197 <span class="preprocessor"></span> cbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>); |
||
| 205 | 00198 <span class="preprocessor"> #else</span> |
||
| 206 | 00199 <span class="preprocessor"></span> sbi(<a class="code" href="uartsw2conf_8h.html#a2">UARTSW_TX_PORT</a>, <a class="code" href="uartsw2conf_8h.html#a4">UARTSW_TX_PIN</a>); |
||
| 207 | 00200 <span class="preprocessor"> #endif</span> |
||
| 208 | 00201 <span class="preprocessor"></span> } |
||
| 209 | 00202 <span class="comment">// schedule the next bit</span> |
||
| 210 | 00203 outw(OCR1A, inw(OCR1A) + UartswBaudRateDiv); |
||
| 211 | 00204 <span class="comment">// count down</span> |
||
| 212 | 00205 UartswTxBitNum--; |
||
| 213 | 00206 } |
||
| 214 | 00207 <span class="keywordflow">else</span> |
||
| 215 | 00208 { |
||
| 216 | 00209 <span class="comment">// transmission is done</span> |
||
| 217 | 00210 <span class="comment">// clear busy flag</span> |
||
| 218 | 00211 UartswTxBusy = FALSE; |
||
| 219 | 00212 } |
||
| 220 | 00213 } |
||
| 221 | 00214 |
||
| 222 | <a name="l00215"></a><a class="code" href="uartsw2_8h.html#a8">00215</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>(<span class="keywordtype">void</span>) |
||
| 223 | 00216 { |
||
| 224 | 00217 <span class="comment">// this function runs on either:</span> |
||
| 225 | 00218 <span class="comment">// - a rising edge interrupt</span> |
||
| 226 | 00219 <span class="comment">// - OC1B</span> |
||
| 227 | 00220 <span class="keywordflow">if</span>(!UartswRxBusy) |
||
| 228 | 00221 { |
||
| 229 | 00222 <span class="comment">// this is a start bit</span> |
||
| 230 | 00223 <span class="comment">// disable ICP interrupt</span> |
||
| 231 | 00224 cbi(TIMSK, TICIE1); |
||
| 232 | 00225 <span class="comment">// schedule data bit sampling 1.5 bit periods from now</span> |
||
| 233 | 00226 outw(OCR1B, inw(TCNT1) + UartswBaudRateDiv + UartswBaudRateDiv/2); |
||
| 234 | 00227 <span class="comment">// clear OC1B interrupt flag</span> |
||
| 235 | 00228 sbi(TIFR, OCF1B); |
||
| 236 | 00229 <span class="comment">// enable OC1B interrupt</span> |
||
| 237 | 00230 sbi(TIMSK, OCIE1B); |
||
| 238 | 00231 <span class="comment">// set start bit flag</span> |
||
| 239 | 00232 UartswRxBusy = TRUE; |
||
| 240 | 00233 <span class="comment">// reset bit counter</span> |
||
| 241 | 00234 UartswRxBitNum = 0; |
||
| 242 | 00235 <span class="comment">// reset data</span> |
||
| 243 | 00236 UartswRxData = 0; |
||
| 244 | 00237 } |
||
| 245 | 00238 <span class="keywordflow">else</span> |
||
| 246 | 00239 { |
||
| 247 | 00240 <span class="comment">// start bit has already been received</span> |
||
| 248 | 00241 <span class="comment">// we're in the data bits</span> |
||
| 249 | 00242 |
||
| 250 | 00243 <span class="comment">// shift data byte to make room for new bit</span> |
||
| 251 | 00244 UartswRxData = UartswRxData>>1; |
||
| 252 | 00245 |
||
| 253 | 00246 <span class="comment">// sample the data line</span> |
||
| 254 | 00247 <span class="preprocessor"> #ifdef UARTSW_INVERT</span> |
||
| 255 | 00248 <span class="preprocessor"></span> <span class="keywordflow">if</span>( !(inb(<a class="code" href="uartsw2conf_8h.html#a7">UARTSW_RX_PORTIN</a>) & (1<<<a class="code" href="uartsw2conf_8h.html#a8">UARTSW_RX_PIN</a>)) ) |
||
| 256 | 00249 <span class="preprocessor"> #else</span> |
||
| 257 | 00250 <span class="preprocessor"></span> <span class="keywordflow">if</span>( (inb(<a class="code" href="uartsw2conf_8h.html#a7">UARTSW_RX_PORTIN</a>) & (1<<<a class="code" href="uartsw2conf_8h.html#a8">UARTSW_RX_PIN</a>)) ) |
||
| 258 | 00251 <span class="preprocessor"> #endif</span> |
||
| 259 | 00252 <span class="preprocessor"></span> { |
||
| 260 | 00253 <span class="comment">// serial line is marking</span> |
||
| 261 | 00254 <span class="comment">// record '1' bit</span> |
||
| 262 | 00255 UartswRxData |= 0x80; |
||
| 263 | 00256 } |
||
| 264 | 00257 |
||
| 265 | 00258 <span class="comment">// increment bit counter</span> |
||
| 266 | 00259 UartswRxBitNum++; |
||
| 267 | 00260 <span class="comment">// schedule next bit sample</span> |
||
| 268 | 00261 outw(OCR1B, inw(OCR1B) + UartswBaudRateDiv); |
||
| 269 | 00262 |
||
| 270 | 00263 <span class="comment">// check if we have a full byte</span> |
||
| 271 | 00264 <span class="keywordflow">if</span>(UartswRxBitNum >= 8) |
||
| 272 | 00265 { |
||
| 273 | 00266 <span class="comment">// save data in receive buffer</span> |
||
| 274 | 00267 <a class="code" href="group__buffer.html#ga5">bufferAddToEnd</a>(&uartswRxBuffer, UartswRxData); |
||
| 275 | 00268 <span class="comment">// disable OC1B interrupt</span> |
||
| 276 | 00269 cbi(TIMSK, OCIE1B); |
||
| 277 | 00270 <span class="comment">// clear ICP interrupt flag</span> |
||
| 278 | 00271 sbi(TIFR, ICF1); |
||
| 279 | 00272 <span class="comment">// enable ICP interrupt</span> |
||
| 280 | 00273 sbi(TIMSK, TICIE1); |
||
| 281 | 00274 <span class="comment">// clear start bit flag</span> |
||
| 282 | 00275 UartswRxBusy = FALSE; |
||
| 283 | 00276 } |
||
| 284 | 00277 } |
||
| 285 | 00278 } |
||
| 286 | 00279 |
||
| 287 | 00280 <span class="comment">/*</span> |
||
| 288 | 00281 <span class="comment">void uartswRxBitService(void)</span> |
||
| 289 | 00282 <span class="comment">{</span> |
||
| 290 | 00283 <span class="comment"> u16 thisBitTime;</span> |
||
| 291 | 00284 <span class="comment"> u08 bitperiods;</span> |
||
| 292 | 00285 <span class="comment"> u08 i;</span> |
||
| 293 | 00286 <span class="comment"></span> |
||
| 294 | 00287 <span class="comment"> // bit transition was detected</span> |
||
| 295 | 00288 <span class="comment"> // record bit's edge time</span> |
||
| 296 | 00289 <span class="comment"> thisBitTime = inw(ICR1);</span> |
||
| 297 | 00290 <span class="comment"></span> |
||
| 298 | 00291 <span class="comment"> cbi(PORTB, 0);</span> |
||
| 299 | 00292 <span class="comment"></span> |
||
| 300 | 00293 <span class="comment"> if(!UartswRxStartBit)</span> |
||
| 301 | 00294 <span class="comment"> {</span> |
||
| 302 | 00295 <span class="comment"> // this is a start bit</span> |
||
| 303 | 00296 <span class="comment"> // switch to falling-edge trigger</span> |
||
| 304 | 00297 <span class="comment"> cbi(TCCR1B, ICES1);</span> |
||
| 305 | 00298 <span class="comment"> // record bit time</span> |
||
| 306 | 00299 <span class="comment"> UartswRxBitTime = thisBitTime;</span> |
||
| 307 | 00300 <span class="comment"> // set start bit flag</span> |
||
| 308 | 00301 <span class="comment"> UartswRxStartBit = TRUE;</span> |
||
| 309 | 00302 <span class="comment"> // reset bit counter</span> |
||
| 310 | 00303 <span class="comment"> UartswRxBitNum = 0;</span> |
||
| 311 | 00304 <span class="comment"> // reset data</span> |
||
| 312 | 00305 <span class="comment"> UartswRxData = 0;</span> |
||
| 313 | 00306 <span class="comment"> }</span> |
||
| 314 | 00307 <span class="comment"> else</span> |
||
| 315 | 00308 <span class="comment"> {</span> |
||
| 316 | 00309 <span class="comment"> // start bit has already been received</span> |
||
| 317 | 00310 <span class="comment"> // we're in the data bits</span> |
||
| 318 | 00311 <span class="comment"> </span> |
||
| 319 | 00312 <span class="comment"> // how many bit periods since last edge?</span> |
||
| 320 | 00313 <span class="comment"> bitperiods = (thisBitTime - UartswRxBitTime + UartswBaudRateDiv/2)/UartswBaudRateDiv;</span> |
||
| 321 | 00314 <span class="comment"> // set last edge time</span> |
||
| 322 | 00315 <span class="comment"> UartswRxBitTime = thisBitTime;</span> |
||
| 323 | 00316 <span class="comment"></span> |
||
| 324 | 00317 <span class="comment"> if(bitperiods > 10)</span> |
||
| 325 | 00318 <span class="comment"> {</span> |
||
| 326 | 00319 <span class="comment"> // switch to trigger on rising edge</span> |
||
| 327 | 00320 <span class="comment"> sbi(TCCR1B, ICES1);</span> |
||
| 328 | 00321 <span class="comment"> // clear start bit flag</span> |
||
| 329 | 00322 <span class="comment"> UartswRxStartBit = FALSE;</span> |
||
| 330 | 00323 <span class="comment"> }</span> |
||
| 331 | 00324 <span class="comment"> else</span> |
||
| 332 | 00325 <span class="comment"> {</span> |
||
| 333 | 00326 <span class="comment"></span> |
||
| 334 | 00327 <span class="comment"></span> |
||
| 335 | 00328 <span class="comment"> if( inb(TCCR1B) & (1<<ICES1) )</span> |
||
| 336 | 00329 <span class="comment"> {</span> |
||
| 337 | 00330 <span class="comment"> // just triggered on a rising edge</span> |
||
| 338 | 00331 <span class="comment"> // previous bits were zero</span> |
||
| 339 | 00332 <span class="comment"> // shift in the data (data bits are inverted)</span> |
||
| 340 | 00333 <span class="comment"> for(i=0; i<bitperiods; i++)</span> |
||
| 341 | 00334 <span class="comment"> {</span> |
||
| 342 | 00335 <span class="comment"> UartswRxData = UartswRxData<<1;</span> |
||
| 343 | 00336 <span class="comment"> UartswRxData |= 0x01;</span> |
||
| 344 | 00337 <span class="comment"> }</span> |
||
| 345 | 00338 <span class="comment"> // switch to trigger on falling edge</span> |
||
| 346 | 00339 <span class="comment"> cbi(TCCR1B, ICES1);</span> |
||
| 347 | 00340 <span class="comment"> }</span> |
||
| 348 | 00341 <span class="comment"> else</span> |
||
| 349 | 00342 <span class="comment"> {</span> |
||
| 350 | 00343 <span class="comment"> // just triggered on a falling edge</span> |
||
| 351 | 00344 <span class="comment"> // previous bits were one</span> |
||
| 352 | 00345 <span class="comment"> // shift in the data (data bits are inverted)</span> |
||
| 353 | 00346 <span class="comment"> for(i=0; i<bitperiods; i++)</span> |
||
| 354 | 00347 <span class="comment"> {</span> |
||
| 355 | 00348 <span class="comment"> UartswRxData = UartswRxData<<1;</span> |
||
| 356 | 00349 <span class="comment"> }</span> |
||
| 357 | 00350 <span class="comment"> // switch to trigger on rising edge</span> |
||
| 358 | 00351 <span class="comment"> sbi(TCCR1B, ICES1);</span> |
||
| 359 | 00352 <span class="comment"> }</span> |
||
| 360 | 00353 <span class="comment"> </span> |
||
| 361 | 00354 <span class="comment"> // increment bit counter</span> |
||
| 362 | 00355 <span class="comment"> UartswRxBitNum += bitperiods;</span> |
||
| 363 | 00356 <span class="comment"> </span> |
||
| 364 | 00357 <span class="comment"> // check if we have a full byte + start bit</span> |
||
| 365 | 00358 <span class="comment"> if(bitperiods > 8)</span> |
||
| 366 | 00359 <span class="comment"> {</span> |
||
| 367 | 00360 <span class="comment"> // save data in receive buffer</span> |
||
| 368 | 00361 <span class="comment"> bufferAddToEnd(&uartswRxBuffer, UartswRxData);</span> |
||
| 369 | 00362 <span class="comment"> // switch to trigger on rising edge</span> |
||
| 370 | 00363 <span class="comment"> sbi(TCCR1B, ICES1);</span> |
||
| 371 | 00364 <span class="comment"> // clear start bit flag</span> |
||
| 372 | 00365 <span class="comment"> UartswRxStartBit = FALSE;</span> |
||
| 373 | 00366 <span class="comment"> }</span> |
||
| 374 | 00367 <span class="comment"> }</span> |
||
| 375 | 00368 <span class="comment"> }</span> |
||
| 376 | 00369 <span class="comment"></span> |
||
| 377 | 00370 <span class="comment"> // turn off debug LEDs</span> |
||
| 378 | 00371 <span class="comment"> delay(10);</span> |
||
| 379 | 00372 <span class="comment"> sbi(PORTB, 0);</span> |
||
| 380 | 00373 <span class="comment"> sbi(PORTB, 1);</span> |
||
| 381 | 00374 <span class="comment">}</span> |
||
| 382 | 00375 <span class="comment">*/</span> |
||
| 383 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:08 2006 for Procyon AVRlib by |
||
| 384 | <a href="http://www.doxygen.org/index.html"> |
||
| 385 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
| 386 | </body> |
||
| 387 | </html> |
Powered by WebSVN v2.8.3