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

library

?curdirlinks? -

Blame information for rev 6

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: uartsw2.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&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>
8 <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>
9 00002 <span class="comment">//*****************************************************************************</span>
10 00003 <span class="comment">//</span>
11 00004 <span class="comment">// File Name : 'uartsw2.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.6</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 &lt;avr/io.h&gt;</span>
26 00019 <span class="preprocessor">#include &lt;avr/interrupt.h&gt;</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="uartsw2_8h.html">uartsw2.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> u08 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">///&lt; 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_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>)
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 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>
63 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>);
64 00057 <span class="preprocessor"> #else</span>
65 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>);
66 00059 <span class="preprocessor"> #endif</span>
67 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>);
68 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>);
69 00062 <span class="comment">// initialize baud rate</span>
70 00063 <a class="code" href="uartsw_8c.html#a12">uartswSetBaudRate</a>(9600);
71 00064
72 00065 <span class="comment">// setup the transmitter</span>
73 00066 UartswTxBusy = FALSE;
74 00067 <span class="comment">// disable OC2 interrupt</span>
75 00068 cbi(TIMSK, OCIE2);
76 00069 <span class="comment">// attach TxBit service routine to OC2</span>
77 00070 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER2OUTCOMPARE_INT, <a class="code" href="uartsw_8c.html#a16">uartswTxBitService</a>);
78 00071
79 00072 <span class="comment">// setup the receiver</span>
80 00073 UartswRxBusy = FALSE;
81 00074 <span class="comment">// disable OC0 interrupt</span>
82 00075 cbi(TIMSK, OCIE0);
83 00076 <span class="comment">// attach RxBit service routine to OC0</span>
84 00077 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER0OUTCOMPARE_INT, <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>);
85 00078 <span class="comment">// INT2 trigger on rising/falling edge</span>
86 00079 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>
87 00080 <span class="preprocessor"></span> sbi(MCUCSR, ISC2); <span class="comment">// rising edge</span>
88 00081 <span class="preprocessor"> #else</span>
89 00082 <span class="preprocessor"></span> cbi(MCUCSR, ISC2); <span class="comment">// falling edge</span>
90 00083 <span class="preprocessor"> #endif</span>
91 00084 <span class="preprocessor"></span> <span class="comment">// enable INT2 interrupt</span>
92 00085 sbi(GICR, INT2);
93 00086
94 00087 <span class="comment">// turn on interrupts</span>
95 00088 sei();
96 00089 }
97 00090 <span class="comment"></span>
98 00091 <span class="comment">//! create and initialize the uart buffers</span>
99 <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>)
100 00093 {
101 00094 <span class="comment">// initialize the UART receive buffer</span>
102 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>);
103 00096 }
104 00097 <span class="comment"></span>
105 00098 <span class="comment">//! turns off software UART</span>
106 <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>)
107 00100 {
108 00101 <span class="comment">// disable interrupts</span>
109 00102 cbi(TIMSK, OCIE2);
110 00103 cbi(TIMSK, OCIE0);
111 00104 cbi(GICR, INT2);
112 00105 <span class="comment">// detach the service routines</span>
113 00106 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER2OUTCOMPARE_INT);
114 00107 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER0OUTCOMPARE_INT);
115 00108 }
116 00109
117 <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)
118 00111 {
119 00112 u16 div;
120 00113
121 00114 <span class="comment">// set timer prescaler</span>
122 00115 <span class="keywordflow">if</span>( baudrate &gt; (F_CPU/64L*256L) )
123 00116 {
124 00117 <span class="comment">// if the requested baud rate is high,</span>
125 00118 <span class="comment">// set timer prescalers to div-by-64</span>
126 00119 <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(<a class="code" href="group__timer.html#ga28">TIMERRTC_CLK_DIV64</a>);
127 00120 <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(<a class="code" href="group__timer.html#ga18">TIMER_CLK_DIV64</a>);
128 00121 div = 64;
129 00122 }
130 00123 <span class="keywordflow">else</span>
131 00124 {
132 00125 <span class="comment">// if the requested baud rate is low,</span>
133 00126 <span class="comment">// set timer prescalers to div-by-256</span>
134 00127 <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(<a class="code" href="group__timer.html#ga30">TIMERRTC_CLK_DIV256</a>);
135 00128 <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(<a class="code" href="group__timer.html#ga19">TIMER_CLK_DIV256</a>);
136 00129 div = 256;
137 00130 }
138 00131
139 00132 <span class="comment">// calculate division factor for requested baud rate, and set it</span>
140 00133 <span class="comment">//UartswBaudRateDiv = (u08)(((F_CPU/64L)+(baudrate/2L))/(baudrate*1L));</span>
141 00134 <span class="comment">//UartswBaudRateDiv = (u08)(((F_CPU/256L)+(baudrate/2L))/(baudrate*1L));</span>
142 00135 UartswBaudRateDiv = (u08)(((F_CPU/div)+(baudrate/2L))/(baudrate*1L));
143 00136 }
144 00137 <span class="comment"></span>
145 00138 <span class="comment">//! returns the receive buffer structure </span>
146 <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>)
147 00140 {
148 00141 <span class="comment">// return rx buffer pointer</span>
149 00142 <span class="keywordflow">return</span> &amp;uartswRxBuffer;
150 00143 }
151 00144
152 <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)
153 00146 {
154 00147 <span class="comment">// wait until uart is ready</span>
155 00148 <span class="keywordflow">while</span>(UartswTxBusy);
156 00149 <span class="comment">// set busy flag</span>
157 00150 UartswTxBusy = TRUE;
158 00151 <span class="comment">// save data</span>
159 00152 UartswTxData = data;
160 00153 <span class="comment">// set number of bits (+1 for stop bit)</span>
161 00154 UartswTxBitNum = 9;
162 00155
163 00156 <span class="comment">// set the start bit</span>
164 00157 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>
165 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>);
166 00159 <span class="preprocessor"> #else</span>
167 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>);
168 00161 <span class="preprocessor"> #endif</span>
169 00162 <span class="preprocessor"></span> <span class="comment">// schedule the next bit</span>
170 00163 outb(OCR2, inb(TCNT2) + UartswBaudRateDiv);
171 00164 <span class="comment">// enable OC2 interrupt</span>
172 00165 sbi(TIMSK, OCIE2);
173 00166 }
174 00167 <span class="comment"></span>
175 00168 <span class="comment">//! gets a byte (if available) from the uart receive buffer</span>
176 <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)
177 00170 {
178 00171 <span class="comment">// make sure we have a receive buffer</span>
179 00172 <span class="keywordflow">if</span>(uartswRxBuffer.<a class="code" href="structstruct__cBuffer.html#o1">size</a>)
180 00173 {
181 00174 <span class="comment">// make sure we have data</span>
182 00175 <span class="keywordflow">if</span>(uartswRxBuffer.<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>)
183 00176 {
184 00177 <span class="comment">// get byte from beginning of buffer</span>
185 00178 *rxData = <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&amp;uartswRxBuffer);
186 00179 <span class="keywordflow">return</span> TRUE;
187 00180 }
188 00181 <span class="keywordflow">else</span>
189 00182 {
190 00183 <span class="comment">// no data</span>
191 00184 <span class="keywordflow">return</span> FALSE;
192 00185 }
193 00186 }
194 00187 <span class="keywordflow">else</span>
195 00188 {
196 00189 <span class="comment">// no buffer</span>
197 00190 <span class="keywordflow">return</span> FALSE;
198 00191 }
199 00192 }
200 00193
201 <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>)
202 00195 {
203 00196 <span class="keywordflow">if</span>(UartswTxBitNum)
204 00197 {
205 00198 <span class="comment">// there are bits still waiting to be transmitted</span>
206 00199 <span class="keywordflow">if</span>(UartswTxBitNum &gt; 1)
207 00200 {
208 00201 <span class="comment">// transmit data bits (inverted, LSB first)</span>
209 00202 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>
210 00203 <span class="preprocessor"></span> <span class="keywordflow">if</span>( !(UartswTxData &amp; 0x01) )
211 00204 <span class="preprocessor"> #else</span>
212 00205 <span class="preprocessor"></span> <span class="keywordflow">if</span>( (UartswTxData &amp; 0x01) )
213 00206 <span class="preprocessor"> #endif</span>
214 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>);
215 00208 <span class="keywordflow">else</span>
216 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>);
217 00210 <span class="comment">// shift bits down</span>
218 00211 UartswTxData = UartswTxData&gt;&gt;1;
219 00212 }
220 00213 <span class="keywordflow">else</span>
221 00214 {
222 00215 <span class="comment">// transmit stop bit</span>
223 00216 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>
224 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>);
225 00218 <span class="preprocessor"> #else</span>
226 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>);
227 00220 <span class="preprocessor"> #endif</span>
228 00221 <span class="preprocessor"></span> }
229 00222 <span class="comment">// schedule the next bit</span>
230 00223 outb(OCR2, inb(OCR2) + UartswBaudRateDiv);
231 00224 <span class="comment">// count down</span>
232 00225 UartswTxBitNum--;
233 00226 }
234 00227 <span class="keywordflow">else</span>
235 00228 {
236 00229 <span class="comment">// transmission is done</span>
237 00230 <span class="comment">// clear busy flag</span>
238 00231 UartswTxBusy = FALSE;
239 00232 <span class="comment">// disable OC2 interrupt</span>
240 00233 cbi(TIMSK, OCIE2);
241 00234 }
242 00235 }
243 00236
244 <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>)
245 00238 {
246 00239 <span class="comment">// this function runs on either:</span>
247 00240 <span class="comment">// - a rising edge interrupt</span>
248 00241 <span class="comment">// - Timer 0 output compare</span>
249 00242 <span class="keywordflow">if</span>(!UartswRxBusy)
250 00243 {
251 00244 <span class="comment">// UART was not previously busy,</span>
252 00245 <span class="comment">// this must be is a start bit</span>
253 00246
254 00247 <span class="comment">// disable INT2 interrupt</span>
255 00248 cbi(GICR, INT2);
256 00249 <span class="comment">// schedule data bit sampling 1.5 bit periods from now</span>
257 00250 outb(OCR0, inb(TCNT0) + UartswBaudRateDiv + UartswBaudRateDiv/2);
258 00251 <span class="comment">// clear OC0 interrupt flag</span>
259 00252 sbi(TIFR, OCF0);
260 00253 <span class="comment">// enable OC0 interrupt</span>
261 00254 sbi(TIMSK, OCIE0);
262 00255 <span class="comment">// set busy flag</span>
263 00256 UartswRxBusy = TRUE;
264 00257 <span class="comment">// reset bit counter</span>
265 00258 UartswRxBitNum = 0;
266 00259 <span class="comment">// reset data</span>
267 00260 UartswRxData = 0;
268 00261 }
269 00262 <span class="keywordflow">else</span>
270 00263 {
271 00264 <span class="comment">// start bit has already been received</span>
272 00265 <span class="comment">// we're in the data bits</span>
273 00266
274 00267 <span class="comment">// shift data byte to make room for new bit</span>
275 00268 UartswRxData = UartswRxData&gt;&gt;1;
276 00269
277 00270 <span class="comment">// sample the data line</span>
278 00271 <span class="preprocessor"> #ifdef UARTSW_INVERT</span>
279 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>)) )
280 00273 <span class="preprocessor"> #else</span>
281 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>)) )
282 00275 <span class="preprocessor"> #endif</span>
283 00276 <span class="preprocessor"></span> {
284 00277 <span class="comment">// serial line is marking</span>
285 00278 <span class="comment">// record '1' bit</span>
286 00279 UartswRxData |= 0x80;
287 00280 }
288 00281
289 00282 <span class="comment">// increment bit counter</span>
290 00283 UartswRxBitNum++;
291 00284 <span class="comment">// schedule next bit sample</span>
292 00285 outb(OCR0, inb(OCR0) + UartswBaudRateDiv);
293 00286
294 00287 <span class="comment">// check if we have a full byte</span>
295 00288 <span class="keywordflow">if</span>(UartswRxBitNum &gt;= 8)
296 00289 {
297 00290 <span class="comment">// save data in receive buffer</span>
298 00291 <a class="code" href="group__buffer.html#ga5">bufferAddToEnd</a>(&amp;uartswRxBuffer, UartswRxData);
299 00292 <span class="comment">// disable OC0 interrupt</span>
300 00293 cbi(TIMSK, OCIE0);
301 00294 <span class="comment">// clear INT2 interrupt flag</span>
302 00295 sbi(GIFR, INTF2);
303 00296 <span class="comment">// enable INT interrupt</span>
304 00297 sbi(GICR, INT2);
305 00298 <span class="comment">// clear busy flag</span>
306 00299 UartswRxBusy = FALSE;
307 00300 }
308 00301 }
309 00302 }
310 00303
311 00304 <a class="code" href="a2d_8c.html#a10">SIGNAL</a>(SIG_INTERRUPT2)
312 00305 {
313 00306 <span class="comment">// run RxBit service routine</span>
314 00307 <a class="code" href="uartsw_8c.html#a17">uartswRxBitService</a>();
315 00308 }
316 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:08 2006 for Procyon AVRlib by&nbsp;
317 <a href="http://www.doxygen.org/index.html">
318 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
319 </body>
320 </html>
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3