?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{FILE START}

library

?curdirlinks? - Rev 6

?prevdifflink? - Blame - ?getfile?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Procyon AVRlib: uartsw2.c Source File</title>
<link href="dox.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.2 -->
<div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<h1>uartsw2.c</h1><a href="uartsw2_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file uartsw2.c \brief Software Interrupt-driven UART Driver. */</span>
00002 <span class="comment">//*****************************************************************************</span>
00003 <span class="comment">//</span>
00004 <span class="comment">// File Name    : 'uartsw2.c'</span>
00005 <span class="comment">// Title        : Software Interrupt-driven UART Driver</span>
00006 <span class="comment">// Author       : Pascal Stang - Copyright (C) 2002-2004</span>
00007 <span class="comment">// Created      : 7/20/2002</span>
00008 <span class="comment">// Revised      : 4/27/2004</span>
00009 <span class="comment">// Version      : 0.6</span>
00010 <span class="comment">// Target MCU   : Atmel AVR Series (intended for the ATmega16 and ATmega32)</span>
00011 <span class="comment">// Editor Tabs  : 4</span>
00012 <span class="comment">//</span>
00013 <span class="comment">// This code is distributed under the GNU Public License</span>
00014 <span class="comment">//      which can be found at http://www.gnu.org/licenses/gpl.txt</span>
00015 <span class="comment">//</span>
00016 <span class="comment">//*****************************************************************************</span>
00017 
00018 <span class="preprocessor">#include &lt;avr/io.h&gt;</span>
00019 <span class="preprocessor">#include &lt;avr/interrupt.h&gt;</span>
00020 
00021 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>
00022 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>
00023 <span class="preprocessor">#include "<a class="code" href="uartsw2_8h.html">uartsw2.h</a>"</span>
00024 
00025 <span class="comment">// Program ROM constants</span>
00026 
00027 <span class="comment">// Global variables</span>
00028 
00029 <span class="comment">// uartsw transmit status and data variables</span>
00030 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswTxBusy;
00031 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswTxData;
00032 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswTxBitNum;
00033 
00034 <span class="comment">// baud rate common to transmit and receive</span>
00035 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswBaudRateDiv;
00036 
00037 <span class="comment">// uartsw receive status and data variables</span>
00038 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswRxBusy;
00039 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswRxData;
00040 <span class="keyword">static</span> <span class="keyword">volatile</span> u08 UartswRxBitNum;
00041 <span class="comment">// receive buffer</span>
00042 <span class="keyword">static</span> <a class="code" href="structstruct__cBuffer.html">cBuffer</a> uartswRxBuffer;               <span class="comment">///&lt; uartsw receive buffer</span>
00043 <span class="comment"></span><span class="comment">// automatically allocate space in ram for each buffer</span>
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>];
00045 
00046 <span class="comment">// functions</span>
00047 <span class="comment"></span>
00048 <span class="comment">//! enable and initialize the software uart</span>
<a name="l00049"></a><a class="code" href="uartsw2_8c.html#a9">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>)
00050 {
00051     <span class="comment">// initialize the buffers</span>
00052     <a class="code" href="uartsw_8c.html#a10">uartswInitBuffers</a>();
00053     <span class="comment">// initialize the ports</span>
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>);
00055 <span class="preprocessor">    #ifdef UARTSW_INVERT</span>
00056 <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>);
00057 <span class="preprocessor">    #else</span>
00058 <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>);
00059 <span class="preprocessor">    #endif</span>
00060 <span class="preprocessor"></span>    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>);
00061     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>);
00062     <span class="comment">// initialize baud rate</span>
00063     <a class="code" href="uartsw_8c.html#a12">uartswSetBaudRate</a>(9600);
00064     
00065     <span class="comment">// setup the transmitter</span>
00066     UartswTxBusy = FALSE;
00067     <span class="comment">// disable OC2 interrupt</span>
00068     cbi(TIMSK, OCIE2);
00069     <span class="comment">// attach TxBit service routine to OC2</span>
00070     <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER2OUTCOMPARE_INT, <a class="code" href="uartsw_8c.html#a16">uartswTxBitService</a>);
00071         
00072     <span class="comment">// setup the receiver</span>
00073     UartswRxBusy = FALSE;
00074     <span class="comment">// disable OC0 interrupt</span>
00075     cbi(TIMSK, OCIE0);
00076     <span class="comment">// attach RxBit service routine to OC0</span>
00077     <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER0OUTCOMPARE_INT, <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>);
00078     <span class="comment">// INT2 trigger on rising/falling edge</span>
00079 <span class="preprocessor">    #ifdef UARTSW_INVERT</span>
00080 <span class="preprocessor"></span>    sbi(MCUCSR, ISC2);  <span class="comment">// rising edge</span>
00081 <span class="preprocessor">    #else</span>
00082 <span class="preprocessor"></span>    cbi(MCUCSR, ISC2);  <span class="comment">// falling edge</span>
00083 <span class="preprocessor">    #endif</span>
00084 <span class="preprocessor"></span>    <span class="comment">// enable INT2 interrupt</span>
00085     sbi(GICR, INT2);
00086 
00087     <span class="comment">// turn on interrupts</span>
00088     sei();
00089 }
00090 <span class="comment"></span>
00091 <span class="comment">//! create and initialize the uart buffers</span>
<a name="l00092"></a><a class="code" href="uartsw2_8c.html#a10">00092</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>)
00093 {
00094     <span class="comment">// initialize the UART receive buffer</span>
00095     <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartswRxBuffer, uartswRxData, <a class="code" href="uartsw2conf_8h.html#a0">UARTSW_RX_BUFFER_SIZE</a>);
00096 }
00097 <span class="comment"></span>
00098 <span class="comment">//! turns off software UART</span>
<a name="l00099"></a><a class="code" href="uartsw2_8c.html#a11">00099</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>)
00100 {
00101     <span class="comment">// disable interrupts</span>
00102     cbi(TIMSK, OCIE2);
00103     cbi(TIMSK, OCIE0);
00104     cbi(GICR, INT2);
00105     <span class="comment">// detach the service routines</span>
00106     <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER2OUTCOMPARE_INT);
00107     <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER0OUTCOMPARE_INT);
00108 }
00109 
<a name="l00110"></a><a class="code" href="uartsw2_8c.html#a12">00110</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a12">uartswSetBaudRate</a>(u32 baudrate)
00111 {
00112     u16 div;
00113 
00114     <span class="comment">// set timer prescaler</span>
00115     <span class="keywordflow">if</span>( baudrate &gt; (F_CPU/64L*256L) )
00116     {
00117         <span class="comment">// if the requested baud rate is high,</span>
00118         <span class="comment">// set timer prescalers to div-by-64</span>
00119         <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(<a class="code" href="group__timer.html#ga28">TIMERRTC_CLK_DIV64</a>);
00120         <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(<a class="code" href="group__timer.html#ga18">TIMER_CLK_DIV64</a>);
00121         div = 64;
00122     }
00123     <span class="keywordflow">else</span>
00124     {
00125         <span class="comment">// if the requested baud rate is low,</span>
00126         <span class="comment">// set timer prescalers to div-by-256</span>
00127         <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(<a class="code" href="group__timer.html#ga30">TIMERRTC_CLK_DIV256</a>);
00128         <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(<a class="code" href="group__timer.html#ga19">TIMER_CLK_DIV256</a>);
00129         div = 256;
00130     }
00131 
00132     <span class="comment">// calculate division factor for requested baud rate, and set it</span>
00133     <span class="comment">//UartswBaudRateDiv = (u08)(((F_CPU/64L)+(baudrate/2L))/(baudrate*1L));</span>
00134     <span class="comment">//UartswBaudRateDiv = (u08)(((F_CPU/256L)+(baudrate/2L))/(baudrate*1L));</span>
00135     UartswBaudRateDiv = (u08)(((F_CPU/div)+(baudrate/2L))/(baudrate*1L));
00136 }
00137 <span class="comment"></span>
00138 <span class="comment">//! returns the receive buffer structure </span>
<a name="l00139"></a><a class="code" href="uartsw2_8c.html#a13">00139</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>)
00140 {
00141     <span class="comment">// return rx buffer pointer</span>
00142     <span class="keywordflow">return</span> &amp;uartswRxBuffer;
00143 }
00144 
<a name="l00145"></a><a class="code" href="uartsw2_8c.html#a14">00145</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a14">uartswSendByte</a>(u08 data)
00146 {
00147     <span class="comment">// wait until uart is ready</span>
00148     <span class="keywordflow">while</span>(UartswTxBusy);
00149     <span class="comment">// set busy flag</span>
00150     UartswTxBusy = TRUE;
00151     <span class="comment">// save data</span>
00152     UartswTxData = data;
00153     <span class="comment">// set number of bits (+1 for stop bit)</span>
00154     UartswTxBitNum = 9;
00155     
00156     <span class="comment">// set the start bit</span>
00157 <span class="preprocessor">    #ifdef UARTSW_INVERT</span>
00158 <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>);
00159 <span class="preprocessor">    #else</span>
00160 <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>);
00161 <span class="preprocessor">    #endif</span>
00162 <span class="preprocessor"></span>    <span class="comment">// schedule the next bit</span>
00163     outb(OCR2, inb(TCNT2) + UartswBaudRateDiv);
00164     <span class="comment">// enable OC2 interrupt</span>
00165     sbi(TIMSK, OCIE2);
00166 }
00167 <span class="comment"></span>
00168 <span class="comment">//! gets a byte (if available) from the uart receive buffer</span>
<a name="l00169"></a><a class="code" href="uartsw2_8c.html#a15">00169</a> <span class="comment"></span>u08 <a class="code" href="uartsw_8c.html#a15">uartswReceiveByte</a>(u08* rxData)
00170 {
00171     <span class="comment">// make sure we have a receive buffer</span>
00172     <span class="keywordflow">if</span>(uartswRxBuffer.<a class="code" href="structstruct__cBuffer.html#o1">size</a>)
00173     {
00174         <span class="comment">// make sure we have data</span>
00175         <span class="keywordflow">if</span>(uartswRxBuffer.<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>)
00176         {
00177             <span class="comment">// get byte from beginning of buffer</span>
00178             *rxData = <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&amp;uartswRxBuffer);
00179             <span class="keywordflow">return</span> TRUE;
00180         }
00181         <span class="keywordflow">else</span>
00182         {
00183             <span class="comment">// no data</span>
00184             <span class="keywordflow">return</span> FALSE;
00185         }
00186     }
00187     <span class="keywordflow">else</span>
00188     {
00189         <span class="comment">// no buffer</span>
00190         <span class="keywordflow">return</span> FALSE;
00191     }
00192 }
00193 
<a name="l00194"></a><a class="code" href="uartsw2_8c.html#a16">00194</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a16">uartswTxBitService</a>(<span class="keywordtype">void</span>)
00195 {
00196     <span class="keywordflow">if</span>(UartswTxBitNum)
00197     {
00198         <span class="comment">// there are bits still waiting to be transmitted</span>
00199         <span class="keywordflow">if</span>(UartswTxBitNum &gt; 1)
00200         {
00201             <span class="comment">// transmit data bits (inverted, LSB first)</span>
00202 <span class="preprocessor">            #ifdef UARTSW_INVERT</span>
00203 <span class="preprocessor"></span>            <span class="keywordflow">if</span>( !(UartswTxData &amp; 0x01) )
00204 <span class="preprocessor">            #else</span>
00205 <span class="preprocessor"></span>            <span class="keywordflow">if</span>( (UartswTxData &amp; 0x01) )
00206 <span class="preprocessor">            #endif</span>
00207 <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>);
00208             <span class="keywordflow">else</span>
00209                 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>);
00210             <span class="comment">// shift bits down</span>
00211             UartswTxData = UartswTxData&gt;&gt;1;
00212         }
00213         <span class="keywordflow">else</span>
00214         {
00215             <span class="comment">// transmit stop bit</span>
00216 <span class="preprocessor">            #ifdef UARTSW_INVERT</span>
00217 <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>);
00218 <span class="preprocessor">            #else</span>
00219 <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>);
00220 <span class="preprocessor">            #endif</span>
00221 <span class="preprocessor"></span>        }
00222         <span class="comment">// schedule the next bit</span>
00223         outb(OCR2, inb(OCR2) + UartswBaudRateDiv);
00224         <span class="comment">// count down</span>
00225         UartswTxBitNum--;
00226     }
00227     <span class="keywordflow">else</span>
00228     {
00229         <span class="comment">// transmission is done</span>
00230         <span class="comment">// clear busy flag</span>
00231         UartswTxBusy = FALSE;
00232         <span class="comment">// disable OC2 interrupt</span>
00233         cbi(TIMSK, OCIE2);
00234     }
00235 }
00236 
<a name="l00237"></a><a class="code" href="uartsw2_8c.html#a17">00237</a> <span class="keywordtype">void</span> <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>(<span class="keywordtype">void</span>)
00238 {
00239     <span class="comment">// this function runs on either:</span>
00240     <span class="comment">// - a rising edge interrupt</span>
00241     <span class="comment">// - Timer 0 output compare</span>
00242     <span class="keywordflow">if</span>(!UartswRxBusy)
00243     {
00244         <span class="comment">// UART was not previously busy,</span>
00245         <span class="comment">// this must be is a start bit</span>
00246         
00247         <span class="comment">// disable INT2 interrupt</span>
00248         cbi(GICR, INT2);
00249         <span class="comment">// schedule data bit sampling 1.5 bit periods from now</span>
00250         outb(OCR0, inb(TCNT0) + UartswBaudRateDiv + UartswBaudRateDiv/2);
00251         <span class="comment">// clear OC0 interrupt flag</span>
00252         sbi(TIFR, OCF0);
00253         <span class="comment">// enable OC0 interrupt</span>
00254         sbi(TIMSK, OCIE0);
00255         <span class="comment">// set busy flag</span>
00256         UartswRxBusy = TRUE;
00257         <span class="comment">// reset bit counter</span>
00258         UartswRxBitNum = 0;
00259         <span class="comment">// reset data</span>
00260         UartswRxData = 0;
00261     }
00262     <span class="keywordflow">else</span>
00263     {
00264         <span class="comment">// start bit has already been received</span>
00265         <span class="comment">// we're in the data bits</span>
00266         
00267         <span class="comment">// shift data byte to make room for new bit</span>
00268         UartswRxData = UartswRxData&gt;&gt;1;
00269 
00270         <span class="comment">// sample the data line</span>
00271 <span class="preprocessor">        #ifdef UARTSW_INVERT</span>
00272 <span class="preprocessor"></span>        <span class="keywordflow">if</span>( !(inb(<a class="code" href="uartsw2conf_8h.html#a7">UARTSW_RX_PORTIN</a>) &amp; (1&lt;&lt;<a class="code" href="uartsw2conf_8h.html#a8">UARTSW_RX_PIN</a>)) )
00273 <span class="preprocessor">        #else</span>
00274 <span class="preprocessor"></span>        <span class="keywordflow">if</span>( (inb(<a class="code" href="uartsw2conf_8h.html#a7">UARTSW_RX_PORTIN</a>) &amp; (1&lt;&lt;<a class="code" href="uartsw2conf_8h.html#a8">UARTSW_RX_PIN</a>)) )
00275 <span class="preprocessor">        #endif</span>
00276 <span class="preprocessor"></span>        {
00277             <span class="comment">// serial line is marking</span>
00278             <span class="comment">// record '1' bit</span>
00279             UartswRxData |= 0x80;
00280         }
00281 
00282         <span class="comment">// increment bit counter</span>
00283         UartswRxBitNum++;
00284         <span class="comment">// schedule next bit sample</span>
00285         outb(OCR0, inb(OCR0) + UartswBaudRateDiv);
00286 
00287         <span class="comment">// check if we have a full byte</span>
00288         <span class="keywordflow">if</span>(UartswRxBitNum &gt;= 8)
00289         {
00290             <span class="comment">// save data in receive buffer</span>
00291             <a class="code" href="group__buffer.html#ga5">bufferAddToEnd</a>(&amp;uartswRxBuffer, UartswRxData);
00292             <span class="comment">// disable OC0 interrupt</span>
00293             cbi(TIMSK, OCIE0);
00294             <span class="comment">// clear INT2 interrupt flag</span>
00295             sbi(GIFR, INTF2);
00296             <span class="comment">// enable INT interrupt</span>
00297             sbi(GICR, INT2);
00298             <span class="comment">// clear busy flag</span>
00299             UartswRxBusy = FALSE;
00300         }
00301     }
00302 }
00303 
00304 <a class="code" href="a2d_8c.html#a10">SIGNAL</a>(SIG_INTERRUPT2)
00305 {
00306     <span class="comment">// run RxBit service routine</span>
00307     <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>();
00308 }
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:08 2006 for Procyon AVRlib by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
</body>
</html>
{FILE END}
{FOOTER START}

Powered by WebSVN v2.8.3