?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: uart.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>uart.c</h1><a href="uart_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file uart.c \brief UART driver with buffer support. */</span>
9 00002 <span class="comment">// *****************************************************************************</span>
10 00003 <span class="comment">//</span>
11 00004 <span class="comment">// File Name : 'uart.c'</span>
12 00005 <span class="comment">// Title : UART driver with buffer support</span>
13 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2000-2002</span>
14 00007 <span class="comment">// Created : 11/22/2000</span>
15 00008 <span class="comment">// Revised : 06/09/2003</span>
16 00009 <span class="comment">// Version : 1.3</span>
17 00010 <span class="comment">// Target MCU : ATMEL AVR Series</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="buffer_8h.html">buffer.h</a>"</span>
29 00022 <span class="preprocessor">#include "<a class="code" href="uart_8h.html">uart.h</a>"</span>
30 00023
31 00024 <span class="comment">// UART global variables</span>
32 00025 <span class="comment">// flag variables</span>
33 <a name="l00026"></a><a class="code" href="uart_8c.html#a0">00026</a> <span class="keyword">volatile</span> u08 <a class="code" href="uart_8c.html#a0">uartReadyTx</a>; <span class="comment">///&lt; uartReadyTx flag</span>
34 <a name="l00027"></a><a class="code" href="uart_8c.html#a1">00027</a> <span class="comment"></span><span class="keyword">volatile</span> u08 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>; <span class="comment">///&lt; uartBufferedTx flag</span>
35 00028 <span class="comment"></span><span class="comment">// receive and transmit buffers</span>
36 <a name="l00029"></a><a class="code" href="uart_8c.html#a2">00029</a> <a class="code" href="structstruct__cBuffer.html">cBuffer</a> <a class="code" href="uart_8c.html#a2">uartRxBuffer</a>; <span class="comment">///&lt; uart receive buffer</span>
37 <a name="l00030"></a><a class="code" href="uart_8c.html#a3">00030</a> <span class="comment"></span><a class="code" href="structstruct__cBuffer.html">cBuffer</a> <a class="code" href="uart_8c.html#a3">uartTxBuffer</a>; <span class="comment">///&lt; uart transmit buffer</span>
38 <a name="l00031"></a><a class="code" href="uart_8c.html#a4">00031</a> <span class="comment"></span><span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>; <span class="comment">///&lt; receive overflow counter</span>
39 00032 <span class="comment"></span>
40 00033 <span class="preprocessor">#ifndef UART_BUFFERS_EXTERNAL_RAM</span>
41 00034 <span class="preprocessor"></span> <span class="comment">// using internal ram,</span>
42 00035 <span class="comment">// automatically allocate space in ram for each buffer</span>
43 00036 <span class="keyword">static</span> <span class="keywordtype">char</span> uartRxData[<a class="code" href="group__uart.html#ga16">UART_RX_BUFFER_SIZE</a>];
44 00037 <span class="keyword">static</span> <span class="keywordtype">char</span> uartTxData[<a class="code" href="group__uart.html#ga15">UART_TX_BUFFER_SIZE</a>];
45 00038 <span class="preprocessor">#endif</span>
46 00039 <span class="preprocessor"></span>
47 00040 <span class="keyword">typedef</span> void (*voidFuncPtru08)(<span class="keywordtype">unsigned</span> char);
48 00041 <span class="keyword">volatile</span> <span class="keyword">static</span> voidFuncPtru08 UartRxFunc;
49 00042
50 00043 <span class="comment">// enable and initialize the uart</span>
51 <a name="l00044"></a><a class="code" href="group__uart2.html#ga0">00044</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga0">uartInit</a>(<span class="keywordtype">void</span>)
52 00045 {
53 00046 <span class="comment">// initialize the buffers</span>
54 00047 <a class="code" href="group__uart.html#ga1">uartInitBuffers</a>();
55 00048 <span class="comment">// initialize user receive handler</span>
56 00049 UartRxFunc = 0;
57 00050
58 00051 <span class="comment">// enable RxD/TxD and interrupts</span>
59 00052 outb(UCR, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN));
60 00053
61 00054 <span class="comment">// set default baud rate</span>
62 00055 <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(<a class="code" href="group__uart.html#ga14">UART_DEFAULT_BAUD_RATE</a>);
63 00056 <span class="comment">// initialize states</span>
64 00057 <a class="code" href="uart_8c.html#a0">uartReadyTx</a> = TRUE;
65 00058 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a> = FALSE;
66 00059 <span class="comment">// clear overflow count</span>
67 00060 <a class="code" href="uart_8c.html#a4">uartRxOverflow</a> = 0;
68 00061 <span class="comment">// enable interrupts</span>
69 00062 sei();
70 00063 }
71 00064
72 00065 <span class="comment">// create and initialize the uart transmit and receive buffers</span>
73 <a name="l00066"></a><a class="code" href="group__uart.html#ga1">00066</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga1">uartInitBuffers</a>(<span class="keywordtype">void</span>)
74 00067 {
75 00068 <span class="preprocessor"> #ifndef UART_BUFFERS_EXTERNAL_RAM</span>
76 00069 <span class="preprocessor"></span> <span class="comment">// initialize the UART receive buffer</span>
77 00070 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartRxBuffer, uartRxData, <a class="code" href="group__uart.html#ga16">UART_RX_BUFFER_SIZE</a>);
78 00071 <span class="comment">// initialize the UART transmit buffer</span>
79 00072 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartTxBuffer, uartTxData, <a class="code" href="group__uart.html#ga15">UART_TX_BUFFER_SIZE</a>);
80 00073 <span class="preprocessor"> #else</span>
81 00074 <span class="preprocessor"></span> <span class="comment">// initialize the UART receive buffer</span>
82 00075 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartRxBuffer, (u08*) UART_RX_BUFFER_ADDR, <a class="code" href="group__uart.html#ga16">UART_RX_BUFFER_SIZE</a>);
83 00076 <span class="comment">// initialize the UART transmit buffer</span>
84 00077 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&amp;uartTxBuffer, (u08*) UART_TX_BUFFER_ADDR, <a class="code" href="group__uart.html#ga15">UART_TX_BUFFER_SIZE</a>);
85 00078 <span class="preprocessor"> #endif</span>
86 00079 <span class="preprocessor"></span>}
87 00080
88 00081 <span class="comment">// redirects received data to a user function</span>
89 <a name="l00082"></a><a class="code" href="group__uart.html#ga2">00082</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga2">uartSetRxHandler</a>(<span class="keywordtype">void</span> (*rx_func)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c))
90 00083 {
91 00084 <span class="comment">// set the receive interrupt to run the supplied user function</span>
92 00085 UartRxFunc = rx_func;
93 00086 }
94 00087
95 00088 <span class="comment">// set the uart baud rate</span>
96 <a name="l00089"></a><a class="code" href="group__uart.html#ga3">00089</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(u32 baudrate)
97 00090 {
98 00091 <span class="comment">// calculate division factor for requested baud rate, and set it</span>
99 00092 u16 bauddiv = ((F_CPU+(baudrate*8L))/(baudrate*16L)-1);
100 00093 outb(UBRRL, bauddiv);
101 00094 <span class="preprocessor"> #ifdef UBRRH</span>
102 00095 <span class="preprocessor"></span> outb(UBRRH, bauddiv&gt;&gt;8);
103 00096 <span class="preprocessor"> #endif</span>
104 00097 <span class="preprocessor"></span>}
105 00098
106 00099 <span class="comment">// returns the receive buffer structure </span>
107 <a name="l00100"></a><a class="code" href="group__uart.html#ga4">00100</a> <a class="code" href="structstruct__cBuffer.html">cBuffer</a>* <a class="code" href="group__uart.html#ga4">uartGetRxBuffer</a>(<span class="keywordtype">void</span>)
108 00101 {
109 00102 <span class="comment">// return rx buffer pointer</span>
110 00103 <span class="keywordflow">return</span> &amp;uartRxBuffer;
111 00104 }
112 00105
113 00106 <span class="comment">// returns the transmit buffer structure </span>
114 <a name="l00107"></a><a class="code" href="group__uart.html#ga5">00107</a> <a class="code" href="structstruct__cBuffer.html">cBuffer</a>* <a class="code" href="group__uart.html#ga5">uartGetTxBuffer</a>(<span class="keywordtype">void</span>)
115 00108 {
116 00109 <span class="comment">// return tx buffer pointer</span>
117 00110 <span class="keywordflow">return</span> &amp;uartTxBuffer;
118 00111 }
119 00112
120 00113 <span class="comment">// transmits a byte over the uart</span>
121 <a name="l00114"></a><a class="code" href="group__uart.html#ga6">00114</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga6">uartSendByte</a>(u08 txData)
122 00115 {
123 00116 <span class="comment">// wait for the transmitter to be ready</span>
124 00117 <span class="keywordflow">while</span>(!<a class="code" href="uart_8c.html#a0">uartReadyTx</a>);
125 00118 <span class="comment">// send byte</span>
126 00119 outb(UDR, txData);
127 00120 <span class="comment">// set ready state to FALSE</span>
128 00121 <a class="code" href="uart_8c.html#a0">uartReadyTx</a> = FALSE;
129 00122 }
130 00123
131 00124 <span class="comment">// gets a single byte from the uart receive buffer (getchar-style)</span>
132 <a name="l00125"></a><a class="code" href="group__uart.html#ga7">00125</a> <span class="keywordtype">int</span> <a class="code" href="group__uart.html#ga7">uartGetByte</a>(<span class="keywordtype">void</span>)
133 00126 {
134 00127 u08 c;
135 00128 <span class="keywordflow">if</span>(<a class="code" href="group__uart.html#ga8">uartReceiveByte</a>(&amp;c))
136 00129 <span class="keywordflow">return</span> c;
137 00130 <span class="keywordflow">else</span>
138 00131 <span class="keywordflow">return</span> -1;
139 00132 }
140 00133
141 00134 <span class="comment">// gets a byte (if available) from the uart receive buffer</span>
142 <a name="l00135"></a><a class="code" href="group__uart.html#ga8">00135</a> u08 <a class="code" href="group__uart.html#ga8">uartReceiveByte</a>(u08* rxData)
143 00136 {
144 00137 <span class="comment">// make sure we have a receive buffer</span>
145 00138 <span class="keywordflow">if</span>(uartRxBuffer.<a class="code" href="structstruct__cBuffer.html#o1">size</a>)
146 00139 {
147 00140 <span class="comment">// make sure we have data</span>
148 00141 <span class="keywordflow">if</span>(uartRxBuffer.<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>)
149 00142 {
150 00143 <span class="comment">// get byte from beginning of buffer</span>
151 00144 *rxData = <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&amp;uartRxBuffer);
152 00145 <span class="keywordflow">return</span> TRUE;
153 00146 }
154 00147 <span class="keywordflow">else</span>
155 00148 {
156 00149 <span class="comment">// no data</span>
157 00150 <span class="keywordflow">return</span> FALSE;
158 00151 }
159 00152 }
160 00153 <span class="keywordflow">else</span>
161 00154 {
162 00155 <span class="comment">// no buffer</span>
163 00156 <span class="keywordflow">return</span> FALSE;
164 00157 }
165 00158 }
166 00159
167 00160 <span class="comment">// flush all data out of the receive buffer</span>
168 <a name="l00161"></a><a class="code" href="group__uart.html#ga10">00161</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga10">uartFlushReceiveBuffer</a>(<span class="keywordtype">void</span>)
169 00162 {
170 00163 <span class="comment">// flush all data from receive buffer</span>
171 00164 <span class="comment">//bufferFlush(&amp;uartRxBuffer);</span>
172 00165 <span class="comment">// same effect as above</span>
173 00166 uartRxBuffer.<a class="code" href="structstruct__cBuffer.html#o2">datalength</a> = 0;
174 00167 }
175 00168
176 00169 <span class="comment">// return true if uart receive buffer is empty</span>
177 <a name="l00170"></a><a class="code" href="group__uart.html#ga9">00170</a> u08 <a class="code" href="group__uart.html#ga9">uartReceiveBufferIsEmpty</a>(<span class="keywordtype">void</span>)
178 00171 {
179 00172 <span class="keywordflow">if</span>(uartRxBuffer.<a class="code" href="structstruct__cBuffer.html#o2">datalength</a> == 0)
180 00173 {
181 00174 <span class="keywordflow">return</span> TRUE;
182 00175 }
183 00176 <span class="keywordflow">else</span>
184 00177 {
185 00178 <span class="keywordflow">return</span> FALSE;
186 00179 }
187 00180 }
188 00181
189 00182 <span class="comment">// add byte to end of uart Tx buffer</span>
190 <a name="l00183"></a><a class="code" href="group__uart.html#ga11">00183</a> u08 <a class="code" href="group__uart.html#ga11">uartAddToTxBuffer</a>(u08 data)
191 00184 {
192 00185 <span class="comment">// add data byte to the end of the tx buffer</span>
193 00186 <span class="keywordflow">return</span> <a class="code" href="group__buffer.html#ga5">bufferAddToEnd</a>(&amp;uartTxBuffer, data);
194 00187 }
195 00188
196 00189 <span class="comment">// start transmission of the current uart Tx buffer contents</span>
197 <a name="l00190"></a><a class="code" href="group__uart.html#ga12">00190</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga12">uartSendTxBuffer</a>(<span class="keywordtype">void</span>)
198 00191 {
199 00192 <span class="comment">// turn on buffered transmit</span>
200 00193 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a> = TRUE;
201 00194 <span class="comment">// send the first byte to get things going by interrupts</span>
202 00195 <a class="code" href="group__uart.html#ga6">uartSendByte</a>(<a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&amp;uartTxBuffer));
203 00196 }
204 00197 <span class="comment">/*</span>
205 00198 <span class="comment">// transmit nBytes from buffer out the uart</span>
206 00199 <span class="comment">u08 uartSendBuffer(char *buffer, u16 nBytes)</span>
207 00200 <span class="comment">{</span>
208 00201 <span class="comment"> register u08 first;</span>
209 00202 <span class="comment"> register u16 i;</span>
210 00203 <span class="comment"></span>
211 00204 <span class="comment"> // check if there's space (and that we have any bytes to send at all)</span>
212 00205 <span class="comment"> if((uartTxBuffer.datalength + nBytes &lt; uartTxBuffer.size) &amp;&amp; nBytes)</span>
213 00206 <span class="comment"> {</span>
214 00207 <span class="comment"> // grab first character</span>
215 00208 <span class="comment"> first = *buffer++;</span>
216 00209 <span class="comment"> // copy user buffer to uart transmit buffer</span>
217 00210 <span class="comment"> for(i = 0; i &lt; nBytes-1; i++)</span>
218 00211 <span class="comment"> {</span>
219 00212 <span class="comment"> // put data bytes at end of buffer</span>
220 00213 <span class="comment"> bufferAddToEnd(&amp;uartTxBuffer, *buffer++);</span>
221 00214 <span class="comment"> }</span>
222 00215 <span class="comment"></span>
223 00216 <span class="comment"> // send the first byte to get things going by interrupts</span>
224 00217 <span class="comment"> uartBufferedTx = TRUE;</span>
225 00218 <span class="comment"> uartSendByte(first);</span>
226 00219 <span class="comment"> // return success</span>
227 00220 <span class="comment"> return TRUE;</span>
228 00221 <span class="comment"> }</span>
229 00222 <span class="comment"> else</span>
230 00223 <span class="comment"> {</span>
231 00224 <span class="comment"> // return failure</span>
232 00225 <span class="comment"> return FALSE;</span>
233 00226 <span class="comment"> }</span>
234 00227 <span class="comment">}</span>
235 00228 <span class="comment">*/</span>
236 00229 <span class="comment">// UART Transmit Complete Interrupt Handler</span>
237 00230 <a class="code" href="group__uart.html#ga17">UART_INTERRUPT_HANDLER</a>(SIG_UART_TRANS)
238 00231 {
239 00232 <span class="comment">// check if buffered tx is enabled</span>
240 00233 <span class="keywordflow">if</span>(<a class="code" href="uart_8c.html#a1">uartBufferedTx</a>)
241 00234 {
242 00235 <span class="comment">// check if there's data left in the buffer</span>
243 00236 <span class="keywordflow">if</span>(uartTxBuffer.<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>)
244 00237 {
245 00238 <span class="comment">// send byte from top of buffer</span>
246 00239 outb(UDR, <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&amp;uartTxBuffer));
247 00240 }
248 00241 <span class="keywordflow">else</span>
249 00242 {
250 00243 <span class="comment">// no data left</span>
251 00244 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a> = FALSE;
252 00245 <span class="comment">// return to ready state</span>
253 00246 <a class="code" href="uart_8c.html#a0">uartReadyTx</a> = TRUE;
254 00247 }
255 00248 }
256 00249 <span class="keywordflow">else</span>
257 00250 {
258 00251 <span class="comment">// we're using single-byte tx mode</span>
259 00252 <span class="comment">// indicate transmit complete, back to ready</span>
260 00253 <a class="code" href="uart_8c.html#a0">uartReadyTx</a> = TRUE;
261 00254 }
262 00255 }
263 00256
264 00257 <span class="comment">// UART Receive Complete Interrupt Handler</span>
265 00258 <a class="code" href="group__uart.html#ga17">UART_INTERRUPT_HANDLER</a>(SIG_UART_RECV)
266 00259 {
267 00260 u08 c;
268 00261
269 00262 <span class="comment">// get received char</span>
270 00263 c = inb(UDR);
271 00264
272 00265 <span class="comment">// if there's a user function to handle this receive event</span>
273 00266 <span class="keywordflow">if</span>(UartRxFunc)
274 00267 {
275 00268 <span class="comment">// call it and pass the received data</span>
276 00269 UartRxFunc(c);
277 00270 }
278 00271 <span class="keywordflow">else</span>
279 00272 {
280 00273 <span class="comment">// otherwise do default processing</span>
281 00274 <span class="comment">// put received char in buffer</span>
282 00275 <span class="comment">// check if there's space</span>
283 00276 <span class="keywordflow">if</span>( !<a class="code" href="group__buffer.html#ga5">bufferAddToEnd</a>(&amp;uartRxBuffer, c) )
284 00277 {
285 00278 <span class="comment">// no space in buffer</span>
286 00279 <span class="comment">// count overflow</span>
287 00280 <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>++;
288 00281 }
289 00282 }
290 00283 }
291 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:08 2006 for Procyon AVRlib by&nbsp;
292 <a href="http://www.doxygen.org/index.html">
293 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
294 </body>
295 </html>
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3