| 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: uart2.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>uart2.c</h1><a href="uart2_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file uart2.c \brief Dual 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 : 'uart2.c'</span> |
||
| 12 | 00005 <span class="comment">// Title : Dual UART driver with buffer support</span> |
||
| 13 | 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2000-2004</span> |
||
| 14 | 00007 <span class="comment">// Created : 11/20/2000</span> |
||
| 15 | 00008 <span class="comment">// Revised : 07/04/2004</span> |
||
| 16 | 00009 <span class="comment">// Version : 1.0</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">// Description : This is a UART driver for AVR-series processors with two</span> |
||
| 21 | 00014 <span class="comment">// hardware UARTs such as the mega161 and mega128 </span> |
||
| 22 | 00015 <span class="comment">//</span> |
||
| 23 | 00016 <span class="comment">// This code is distributed under the GNU Public License</span> |
||
| 24 | 00017 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span> |
||
| 25 | 00018 <span class="comment">//</span> |
||
| 26 | 00019 <span class="comment">//*****************************************************************************</span> |
||
| 27 | 00020 |
||
| 28 | 00021 <span class="preprocessor">#include <avr/io.h></span> |
||
| 29 | 00022 <span class="preprocessor">#include <avr/interrupt.h></span> |
||
| 30 | 00023 |
||
| 31 | 00024 <span class="preprocessor">#include "<a class="code" href="buffer_8h.html">buffer.h</a>"</span> |
||
| 32 | 00025 <span class="preprocessor">#include "<a class="code" href="uart2_8h.html">uart2.h</a>"</span> |
||
| 33 | 00026 |
||
| 34 | 00027 <span class="comment">// UART global variables</span> |
||
| 35 | 00028 <span class="comment">// flag variables</span> |
||
| 36 | 00029 <span class="keyword">volatile</span> u08 <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[2]; |
||
| 37 | 00030 <span class="keyword">volatile</span> u08 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[2]; |
||
| 38 | 00031 <span class="comment">// receive and transmit buffers</span> |
||
| 39 | 00032 <a class="code" href="structstruct__cBuffer.html">cBuffer</a> <a class="code" href="uart_8c.html#a2">uartRxBuffer</a>[2]; |
||
| 40 | 00033 <a class="code" href="structstruct__cBuffer.html">cBuffer</a> <a class="code" href="uart_8c.html#a3">uartTxBuffer</a>[2]; |
||
| 41 | 00034 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>[2]; |
||
| 42 | 00035 <span class="preprocessor">#ifndef UART_BUFFER_EXTERNAL_RAM</span> |
||
| 43 | 00036 <span class="preprocessor"></span> <span class="comment">// using internal ram,</span> |
||
| 44 | 00037 <span class="comment">// automatically allocate space in ram for each buffer</span> |
||
| 45 | 00038 <span class="keyword">static</span> <span class="keywordtype">char</span> uart0RxData[<a class="code" href="group__uart2.html#ga27">UART0_RX_BUFFER_SIZE</a>]; |
||
| 46 | 00039 <span class="keyword">static</span> <span class="keywordtype">char</span> uart0TxData[<a class="code" href="group__uart2.html#ga26">UART0_TX_BUFFER_SIZE</a>]; |
||
| 47 | 00040 <span class="keyword">static</span> <span class="keywordtype">char</span> uart1RxData[<a class="code" href="group__uart2.html#ga29">UART1_RX_BUFFER_SIZE</a>]; |
||
| 48 | 00041 <span class="keyword">static</span> <span class="keywordtype">char</span> uart1TxData[<a class="code" href="group__uart2.html#ga28">UART1_TX_BUFFER_SIZE</a>]; |
||
| 49 | 00042 <span class="preprocessor">#endif</span> |
||
| 50 | 00043 <span class="preprocessor"></span> |
||
| 51 | 00044 <span class="keyword">typedef</span> void (*voidFuncPtru08)(<span class="keywordtype">unsigned</span> char); |
||
| 52 | 00045 <span class="keyword">volatile</span> <span class="keyword">static</span> voidFuncPtru08 UartRxFunc[2]; |
||
| 53 | 00046 |
||
| 54 | <a name="l00047"></a><a class="code" href="group__uart.html#ga0">00047</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga0">uartInit</a>(<span class="keywordtype">void</span>) |
||
| 55 | 00048 { |
||
| 56 | 00049 <span class="comment">// initialize both uarts</span> |
||
| 57 | 00050 <a class="code" href="group__uart2.html#ga1">uart0Init</a>(); |
||
| 58 | 00051 <a class="code" href="group__uart2.html#ga2">uart1Init</a>(); |
||
| 59 | 00052 } |
||
| 60 | 00053 |
||
| 61 | <a name="l00054"></a><a class="code" href="group__uart2.html#ga1">00054</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga1">uart0Init</a>(<span class="keywordtype">void</span>) |
||
| 62 | 00055 { |
||
| 63 | 00056 <span class="comment">// initialize the buffers</span> |
||
| 64 | 00057 <a class="code" href="group__uart2.html#ga3">uart0InitBuffers</a>(); |
||
| 65 | 00058 <span class="comment">// initialize user receive handlers</span> |
||
| 66 | 00059 UartRxFunc[0] = 0; |
||
| 67 | 00060 <span class="comment">// enable RxD/TxD and interrupts</span> |
||
| 68 | 00061 outb(UCSR0B, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN)); |
||
| 69 | 00062 <span class="comment">// set default baud rate</span> |
||
| 70 | 00063 <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(0, <a class="code" href="group__uart2.html#ga24">UART0_DEFAULT_BAUD_RATE</a>); |
||
| 71 | 00064 <span class="comment">// initialize states</span> |
||
| 72 | 00065 <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[0] = TRUE; |
||
| 73 | 00066 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[0] = FALSE; |
||
| 74 | 00067 <span class="comment">// clear overflow count</span> |
||
| 75 | 00068 <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>[0] = 0; |
||
| 76 | 00069 <span class="comment">// enable interrupts</span> |
||
| 77 | 00070 sei(); |
||
| 78 | 00071 } |
||
| 79 | 00072 |
||
| 80 | <a name="l00073"></a><a class="code" href="group__uart2.html#ga2">00073</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga2">uart1Init</a>(<span class="keywordtype">void</span>) |
||
| 81 | 00074 { |
||
| 82 | 00075 <span class="comment">// initialize the buffers</span> |
||
| 83 | 00076 uart1InitBuffers(); |
||
| 84 | 00077 <span class="comment">// initialize user receive handlers</span> |
||
| 85 | 00078 UartRxFunc[1] = 0; |
||
| 86 | 00079 <span class="comment">// enable RxD/TxD and interrupts</span> |
||
| 87 | 00080 outb(UCSR1B, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN)); |
||
| 88 | 00081 <span class="comment">// set default baud rate</span> |
||
| 89 | 00082 <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(1, <a class="code" href="group__uart2.html#ga25">UART1_DEFAULT_BAUD_RATE</a>); |
||
| 90 | 00083 <span class="comment">// initialize states</span> |
||
| 91 | 00084 <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[1] = TRUE; |
||
| 92 | 00085 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[1] = FALSE; |
||
| 93 | 00086 <span class="comment">// clear overflow count</span> |
||
| 94 | 00087 <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>[1] = 0; |
||
| 95 | 00088 <span class="comment">// enable interrupts</span> |
||
| 96 | 00089 sei(); |
||
| 97 | 00090 } |
||
| 98 | 00091 |
||
| 99 | <a name="l00092"></a><a class="code" href="group__uart2.html#ga3">00092</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga3">uart0InitBuffers</a>(<span class="keywordtype">void</span>) |
||
| 100 | 00093 { |
||
| 101 | 00094 <span class="preprocessor"> #ifndef UART_BUFFER_EXTERNAL_RAM</span> |
||
| 102 | 00095 <span class="preprocessor"></span> <span class="comment">// initialize the UART0 buffers</span> |
||
| 103 | 00096 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&uartRxBuffer[0], uart0RxData, <a class="code" href="group__uart2.html#ga27">UART0_RX_BUFFER_SIZE</a>); |
||
| 104 | 00097 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&uartTxBuffer[0], uart0TxData, <a class="code" href="group__uart2.html#ga26">UART0_TX_BUFFER_SIZE</a>); |
||
| 105 | 00098 <span class="preprocessor"> #else</span> |
||
| 106 | 00099 <span class="preprocessor"></span> <span class="comment">// initialize the UART0 buffers</span> |
||
| 107 | 00100 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&uartRxBuffer[0], (u08*) UART0_RX_BUFFER_ADDR, <a class="code" href="group__uart2.html#ga27">UART0_RX_BUFFER_SIZE</a>); |
||
| 108 | 00101 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&uartTxBuffer[0], (u08*) UART0_TX_BUFFER_ADDR, <a class="code" href="group__uart2.html#ga26">UART0_TX_BUFFER_SIZE</a>); |
||
| 109 | 00102 <span class="preprocessor"> #endif</span> |
||
| 110 | 00103 <span class="preprocessor"></span>} |
||
| 111 | 00104 |
||
| 112 | 00105 <span class="keywordtype">void</span> uart1InitBuffers(<span class="keywordtype">void</span>) |
||
| 113 | 00106 { |
||
| 114 | 00107 <span class="preprocessor"> #ifndef UART_BUFFER_EXTERNAL_RAM</span> |
||
| 115 | 00108 <span class="preprocessor"></span> <span class="comment">// initialize the UART1 buffers</span> |
||
| 116 | 00109 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&uartRxBuffer[1], uart1RxData, <a class="code" href="group__uart2.html#ga29">UART1_RX_BUFFER_SIZE</a>); |
||
| 117 | 00110 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&uartTxBuffer[1], uart1TxData, <a class="code" href="group__uart2.html#ga28">UART1_TX_BUFFER_SIZE</a>); |
||
| 118 | 00111 <span class="preprocessor"> #else</span> |
||
| 119 | 00112 <span class="preprocessor"></span> <span class="comment">// initialize the UART1 buffers</span> |
||
| 120 | 00113 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&uartRxBuffer[1], (u08*) UART1_RX_BUFFER_ADDR, <a class="code" href="group__uart2.html#ga29">UART1_RX_BUFFER_SIZE</a>); |
||
| 121 | 00114 <a class="code" href="group__buffer.html#ga1">bufferInit</a>(&uartTxBuffer[1], (u08*) UART1_TX_BUFFER_ADDR, <a class="code" href="group__uart2.html#ga28">UART1_TX_BUFFER_SIZE</a>); |
||
| 122 | 00115 <span class="preprocessor"> #endif</span> |
||
| 123 | 00116 <span class="preprocessor"></span>} |
||
| 124 | 00117 |
||
| 125 | <a name="l00118"></a><a class="code" href="group__uart2.html#ga5">00118</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga2">uartSetRxHandler</a>(u08 nUart, <span class="keywordtype">void</span> (*rx_func)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c)) |
||
| 126 | 00119 { |
||
| 127 | 00120 <span class="comment">// make sure the uart number is within bounds</span> |
||
| 128 | 00121 <span class="keywordflow">if</span>(nUart < 2) |
||
| 129 | 00122 { |
||
| 130 | 00123 <span class="comment">// set the receive interrupt to run the supplied user function</span> |
||
| 131 | 00124 UartRxFunc[nUart] = rx_func; |
||
| 132 | 00125 } |
||
| 133 | 00126 } |
||
| 134 | 00127 |
||
| 135 | <a name="l00128"></a><a class="code" href="group__uart2.html#ga6">00128</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga3">uartSetBaudRate</a>(u08 nUart, u32 baudrate) |
||
| 136 | 00129 { |
||
| 137 | 00130 <span class="comment">// calculate division factor for requested baud rate, and set it</span> |
||
| 138 | 00131 u16 bauddiv = ((F_CPU+(baudrate*8L))/(baudrate*16L)-1); |
||
| 139 | 00132 <span class="keywordflow">if</span>(nUart) |
||
| 140 | 00133 { |
||
| 141 | 00134 outb(UBRR1L, bauddiv); |
||
| 142 | 00135 <span class="preprocessor"> #ifdef UBRR1H</span> |
||
| 143 | 00136 <span class="preprocessor"></span> outb(UBRR1H, bauddiv>>8); |
||
| 144 | 00137 <span class="preprocessor"> #endif</span> |
||
| 145 | 00138 <span class="preprocessor"></span> } |
||
| 146 | 00139 <span class="keywordflow">else</span> |
||
| 147 | 00140 { |
||
| 148 | 00141 outb(UBRR0L, bauddiv); |
||
| 149 | 00142 <span class="preprocessor"> #ifdef UBRR0H</span> |
||
| 150 | 00143 <span class="preprocessor"></span> outb(UBRR0H, bauddiv>>8); |
||
| 151 | 00144 <span class="preprocessor"> #endif</span> |
||
| 152 | 00145 <span class="preprocessor"></span> } |
||
| 153 | 00146 } |
||
| 154 | 00147 |
||
| 155 | <a name="l00148"></a><a class="code" href="group__uart2.html#ga7">00148</a> <a class="code" href="structstruct__cBuffer.html">cBuffer</a>* <a class="code" href="group__uart.html#ga4">uartGetRxBuffer</a>(u08 nUart) |
||
| 156 | 00149 { |
||
| 157 | 00150 <span class="comment">// return rx buffer pointer</span> |
||
| 158 | 00151 <span class="keywordflow">return</span> &uartRxBuffer[nUart]; |
||
| 159 | 00152 } |
||
| 160 | 00153 |
||
| 161 | <a name="l00154"></a><a class="code" href="group__uart2.html#ga8">00154</a> <a class="code" href="structstruct__cBuffer.html">cBuffer</a>* <a class="code" href="group__uart.html#ga5">uartGetTxBuffer</a>(u08 nUart) |
||
| 162 | 00155 { |
||
| 163 | 00156 <span class="comment">// return tx buffer pointer</span> |
||
| 164 | 00157 <span class="keywordflow">return</span> &uartTxBuffer[nUart]; |
||
| 165 | 00158 } |
||
| 166 | 00159 |
||
| 167 | <a name="l00160"></a><a class="code" href="group__uart2.html#ga9">00160</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga6">uartSendByte</a>(u08 nUart, u08 txData) |
||
| 168 | 00161 { |
||
| 169 | 00162 <span class="comment">// wait for the transmitter to be ready</span> |
||
| 170 | 00163 <span class="comment">// while(!uartReadyTx[nUart]);</span> |
||
| 171 | 00164 <span class="comment">// send byte</span> |
||
| 172 | 00165 <span class="keywordflow">if</span>(nUart) |
||
| 173 | 00166 { |
||
| 174 | 00167 <span class="keywordflow">while</span>(!(UCSR1A & (1<<UDRE))); |
||
| 175 | 00168 outb(UDR1, txData); |
||
| 176 | 00169 } |
||
| 177 | 00170 <span class="keywordflow">else</span> |
||
| 178 | 00171 { |
||
| 179 | 00172 <span class="keywordflow">while</span>(!(UCSR0A & (1<<UDRE))); |
||
| 180 | 00173 outb(UDR0, txData); |
||
| 181 | 00174 } |
||
| 182 | 00175 <span class="comment">// set ready state to FALSE</span> |
||
| 183 | 00176 <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[nUart] = FALSE; |
||
| 184 | 00177 } |
||
| 185 | 00178 |
||
| 186 | <a name="l00179"></a><a class="code" href="group__uart2.html#ga10">00179</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga10">uart0SendByte</a>(u08 data) |
||
| 187 | 00180 { |
||
| 188 | 00181 <span class="comment">// send byte on UART0</span> |
||
| 189 | 00182 <a class="code" href="group__uart.html#ga6">uartSendByte</a>(0, data); |
||
| 190 | 00183 } |
||
| 191 | 00184 |
||
| 192 | 00185 <span class="keywordtype">void</span> uart1SendByte(u08 data) |
||
| 193 | 00186 { |
||
| 194 | 00187 <span class="comment">// send byte on UART1</span> |
||
| 195 | 00188 <a class="code" href="group__uart.html#ga6">uartSendByte</a>(1, data); |
||
| 196 | 00189 } |
||
| 197 | 00190 |
||
| 198 | <a name="l00191"></a><a class="code" href="group__uart2.html#ga12">00191</a> <span class="keywordtype">int</span> <a class="code" href="group__uart2.html#ga12">uart0GetByte</a>(<span class="keywordtype">void</span>) |
||
| 199 | 00192 { |
||
| 200 | 00193 <span class="comment">// get single byte from receive buffer (if available)</span> |
||
| 201 | 00194 u08 c; |
||
| 202 | 00195 <span class="keywordflow">if</span>(<a class="code" href="group__uart.html#ga8">uartReceiveByte</a>(0,&c)) |
||
| 203 | 00196 <span class="keywordflow">return</span> c; |
||
| 204 | 00197 <span class="keywordflow">else</span> |
||
| 205 | 00198 <span class="keywordflow">return</span> -1; |
||
| 206 | 00199 } |
||
| 207 | 00200 |
||
| 208 | 00201 <span class="keywordtype">int</span> uart1GetByte(<span class="keywordtype">void</span>) |
||
| 209 | 00202 { |
||
| 210 | 00203 <span class="comment">// get single byte from receive buffer (if available)</span> |
||
| 211 | 00204 u08 c; |
||
| 212 | 00205 <span class="keywordflow">if</span>(<a class="code" href="group__uart.html#ga8">uartReceiveByte</a>(1,&c)) |
||
| 213 | 00206 <span class="keywordflow">return</span> c; |
||
| 214 | 00207 <span class="keywordflow">else</span> |
||
| 215 | 00208 <span class="keywordflow">return</span> -1; |
||
| 216 | 00209 } |
||
| 217 | 00210 |
||
| 218 | 00211 |
||
| 219 | <a name="l00212"></a><a class="code" href="group__uart2.html#ga14">00212</a> u08 <a class="code" href="group__uart.html#ga8">uartReceiveByte</a>(u08 nUart, u08* rxData) |
||
| 220 | 00213 { |
||
| 221 | 00214 <span class="comment">// make sure we have a receive buffer</span> |
||
| 222 | 00215 <span class="keywordflow">if</span>(uartRxBuffer[nUart].<a class="code" href="structstruct__cBuffer.html#o1">size</a>) |
||
| 223 | 00216 { |
||
| 224 | 00217 <span class="comment">// make sure we have data</span> |
||
| 225 | 00218 <span class="keywordflow">if</span>(uartRxBuffer[nUart].<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>) |
||
| 226 | 00219 { |
||
| 227 | 00220 <span class="comment">// get byte from beginning of buffer</span> |
||
| 228 | 00221 *rxData = <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&uartRxBuffer[nUart]); |
||
| 229 | 00222 <span class="keywordflow">return</span> TRUE; |
||
| 230 | 00223 } |
||
| 231 | 00224 <span class="keywordflow">else</span> |
||
| 232 | 00225 <span class="keywordflow">return</span> FALSE; <span class="comment">// no data</span> |
||
| 233 | 00226 } |
||
| 234 | 00227 <span class="keywordflow">else</span> |
||
| 235 | 00228 <span class="keywordflow">return</span> FALSE; <span class="comment">// no buffer</span> |
||
| 236 | 00229 } |
||
| 237 | 00230 |
||
| 238 | <a name="l00231"></a><a class="code" href="group__uart2.html#ga16">00231</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga10">uartFlushReceiveBuffer</a>(u08 nUart) |
||
| 239 | 00232 { |
||
| 240 | 00233 <span class="comment">// flush all data from receive buffer</span> |
||
| 241 | 00234 <a class="code" href="group__buffer.html#ga7">bufferFlush</a>(&uartRxBuffer[nUart]); |
||
| 242 | 00235 } |
||
| 243 | 00236 |
||
| 244 | <a name="l00237"></a><a class="code" href="group__uart2.html#ga15">00237</a> u08 <a class="code" href="group__uart.html#ga9">uartReceiveBufferIsEmpty</a>(u08 nUart) |
||
| 245 | 00238 { |
||
| 246 | 00239 <span class="keywordflow">return</span> (uartRxBuffer[nUart].datalength == 0); |
||
| 247 | 00240 } |
||
| 248 | 00241 |
||
| 249 | <a name="l00242"></a><a class="code" href="group__uart2.html#ga17">00242</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga11">uartAddToTxBuffer</a>(u08 nUart, u08 data) |
||
| 250 | 00243 { |
||
| 251 | 00244 <span class="comment">// add data byte to the end of the tx buffer</span> |
||
| 252 | 00245 <a class="code" href="group__buffer.html#ga5">bufferAddToEnd</a>(&uartTxBuffer[nUart], data); |
||
| 253 | 00246 } |
||
| 254 | 00247 |
||
| 255 | <a name="l00248"></a><a class="code" href="group__uart2.html#ga18">00248</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga18">uart0AddToTxBuffer</a>(u08 data) |
||
| 256 | 00249 { |
||
| 257 | 00250 <a class="code" href="group__uart.html#ga11">uartAddToTxBuffer</a>(0,data); |
||
| 258 | 00251 } |
||
| 259 | 00252 |
||
| 260 | 00253 <span class="keywordtype">void</span> uart1AddToTxBuffer(u08 data) |
||
| 261 | 00254 { |
||
| 262 | 00255 <a class="code" href="group__uart.html#ga11">uartAddToTxBuffer</a>(1,data); |
||
| 263 | 00256 } |
||
| 264 | 00257 |
||
| 265 | <a name="l00258"></a><a class="code" href="group__uart2.html#ga20">00258</a> <span class="keywordtype">void</span> <a class="code" href="group__uart.html#ga12">uartSendTxBuffer</a>(u08 nUart) |
||
| 266 | 00259 { |
||
| 267 | 00260 <span class="comment">// turn on buffered transmit</span> |
||
| 268 | 00261 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[nUart] = TRUE; |
||
| 269 | 00262 <span class="comment">// send the first byte to get things going by interrupts</span> |
||
| 270 | 00263 <a class="code" href="group__uart.html#ga6">uartSendByte</a>(nUart, <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&uartTxBuffer[nUart])); |
||
| 271 | 00264 } |
||
| 272 | 00265 |
||
| 273 | <a name="l00266"></a><a class="code" href="group__uart2.html#ga21">00266</a> u08 <a class="code" href="group__uart.html#ga13">uartSendBuffer</a>(u08 nUart, <span class="keywordtype">char</span> *buffer, u16 nBytes) |
||
| 274 | 00267 { |
||
| 275 | 00268 <span class="keyword">register</span> u08 first; |
||
| 276 | 00269 <span class="keyword">register</span> u16 i; |
||
| 277 | 00270 |
||
| 278 | 00271 <span class="comment">// check if there's space (and that we have any bytes to send at all)</span> |
||
| 279 | 00272 <span class="keywordflow">if</span>((uartTxBuffer[nUart].<a class="code" href="structstruct__cBuffer.html#o2">datalength</a> + nBytes < uartTxBuffer[nUart].<a class="code" href="structstruct__cBuffer.html#o1">size</a>) && nBytes) |
||
| 280 | 00273 { |
||
| 281 | 00274 <span class="comment">// grab first character</span> |
||
| 282 | 00275 first = *buffer++; |
||
| 283 | 00276 <span class="comment">// copy user buffer to uart transmit buffer</span> |
||
| 284 | 00277 <span class="keywordflow">for</span>(i = 0; i < nBytes-1; i++) |
||
| 285 | 00278 { |
||
| 286 | 00279 <span class="comment">// put data bytes at end of buffer</span> |
||
| 287 | 00280 <a class="code" href="group__buffer.html#ga5">bufferAddToEnd</a>(&uartTxBuffer[nUart], *buffer++); |
||
| 288 | 00281 } |
||
| 289 | 00282 |
||
| 290 | 00283 <span class="comment">// send the first byte to get things going by interrupts</span> |
||
| 291 | 00284 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[nUart] = TRUE; |
||
| 292 | 00285 <a class="code" href="group__uart.html#ga6">uartSendByte</a>(nUart, first); |
||
| 293 | 00286 <span class="comment">// return success</span> |
||
| 294 | 00287 <span class="keywordflow">return</span> TRUE; |
||
| 295 | 00288 } |
||
| 296 | 00289 <span class="keywordflow">else</span> |
||
| 297 | 00290 { |
||
| 298 | 00291 <span class="comment">// return failure</span> |
||
| 299 | 00292 <span class="keywordflow">return</span> FALSE; |
||
| 300 | 00293 } |
||
| 301 | 00294 } |
||
| 302 | 00295 |
||
| 303 | 00296 <span class="comment">// UART Transmit Complete Interrupt Function</span> |
||
| 304 | <a name="l00297"></a><a class="code" href="group__uart2.html#ga22">00297</a> <span class="keywordtype">void</span> <a class="code" href="group__uart2.html#ga22">uartTransmitService</a>(u08 nUart) |
||
| 305 | 00298 { |
||
| 306 | 00299 <span class="comment">// check if buffered tx is enabled</span> |
||
| 307 | 00300 <span class="keywordflow">if</span>(<a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[nUart]) |
||
| 308 | 00301 { |
||
| 309 | 00302 <span class="comment">// check if there's data left in the buffer</span> |
||
| 310 | 00303 <span class="keywordflow">if</span>(uartTxBuffer[nUart].<a class="code" href="structstruct__cBuffer.html#o2">datalength</a>) |
||
| 311 | 00304 { |
||
| 312 | 00305 <span class="comment">// send byte from top of buffer</span> |
||
| 313 | 00306 <span class="keywordflow">if</span>(nUart) |
||
| 314 | 00307 outb(UDR1, <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&uartTxBuffer[1]) ); |
||
| 315 | 00308 <span class="keywordflow">else</span> |
||
| 316 | 00309 outb(UDR0, <a class="code" href="group__buffer.html#ga2">bufferGetFromFront</a>(&uartTxBuffer[0]) ); |
||
| 317 | 00310 } |
||
| 318 | 00311 <span class="keywordflow">else</span> |
||
| 319 | 00312 { |
||
| 320 | 00313 <span class="comment">// no data left</span> |
||
| 321 | 00314 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>[nUart] = FALSE; |
||
| 322 | 00315 <span class="comment">// return to ready state</span> |
||
| 323 | 00316 <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[nUart] = TRUE; |
||
| 324 | 00317 } |
||
| 325 | 00318 } |
||
| 326 | 00319 <span class="keywordflow">else</span> |
||
| 327 | 00320 { |
||
| 328 | 00321 <span class="comment">// we're using single-byte tx mode</span> |
||
| 329 | 00322 <span class="comment">// indicate transmit complete, back to ready</span> |
||
| 330 | 00323 <a class="code" href="uart_8c.html#a0">uartReadyTx</a>[nUart] = TRUE; |
||
| 331 | 00324 } |
||
| 332 | 00325 } |
||
| 333 | 00326 |
||
| 334 | 00327 <span class="comment">// UART Receive Complete Interrupt Function</span> |
||
| 335 | 00328 <span class="keywordtype">void</span> uartReceiveService(u08 nUart) |
||
| 336 | 00329 { |
||
| 337 | 00330 u08 c; |
||
| 338 | 00331 <span class="comment">// get received char</span> |
||
| 339 | 00332 <span class="keywordflow">if</span>(nUart) |
||
| 340 | 00333 c = inb(UDR1); |
||
| 341 | 00334 <span class="keywordflow">else</span> |
||
| 342 | 00335 c = inb(UDR0); |
||
| 343 | 00336 |
||
| 344 | 00337 <span class="comment">// if there's a user function to handle this receive event</span> |
||
| 345 | 00338 <span class="keywordflow">if</span>(UartRxFunc[nUart]) |
||
| 346 | 00339 { |
||
| 347 | 00340 <span class="comment">// call it and pass the received data</span> |
||
| 348 | 00341 UartRxFunc[nUart](c); |
||
| 349 | 00342 } |
||
| 350 | 00343 <span class="keywordflow">else</span> |
||
| 351 | 00344 { |
||
| 352 | 00345 <span class="comment">// otherwise do default processing</span> |
||
| 353 | 00346 <span class="comment">// put received char in buffer</span> |
||
| 354 | 00347 <span class="comment">// check if there's space</span> |
||
| 355 | 00348 <span class="keywordflow">if</span>( !<a class="code" href="group__buffer.html#ga5">bufferAddToEnd</a>(&uartRxBuffer[nUart], c) ) |
||
| 356 | 00349 { |
||
| 357 | 00350 <span class="comment">// no space in buffer</span> |
||
| 358 | 00351 <span class="comment">// count overflow</span> |
||
| 359 | 00352 <a class="code" href="uart_8c.html#a4">uartRxOverflow</a>[nUart]++; |
||
| 360 | 00353 } |
||
| 361 | 00354 } |
||
| 362 | 00355 } |
||
| 363 | 00356 |
||
| 364 | 00357 <a class="code" href="group__uart.html#ga17">UART_INTERRUPT_HANDLER</a>(SIG_UART0_TRANS) |
||
| 365 | 00358 { |
||
| 366 | 00359 <span class="comment">// service UART0 transmit interrupt</span> |
||
| 367 | 00360 <a class="code" href="group__uart2.html#ga22">uartTransmitService</a>(0); |
||
| 368 | 00361 } |
||
| 369 | 00362 |
||
| 370 | 00363 <a class="code" href="group__uart.html#ga17">UART_INTERRUPT_HANDLER</a>(SIG_UART1_TRANS) |
||
| 371 | 00364 { |
||
| 372 | 00365 <span class="comment">// service UART1 transmit interrupt</span> |
||
| 373 | 00366 <a class="code" href="group__uart2.html#ga22">uartTransmitService</a>(1); |
||
| 374 | 00367 } |
||
| 375 | 00368 |
||
| 376 | 00369 <a class="code" href="group__uart.html#ga17">UART_INTERRUPT_HANDLER</a>(SIG_UART0_RECV) |
||
| 377 | 00370 { |
||
| 378 | 00371 <span class="comment">// service UART0 receive interrupt</span> |
||
| 379 | 00372 uartReceiveService(0); |
||
| 380 | 00373 } |
||
| 381 | 00374 |
||
| 382 | 00375 <a class="code" href="group__uart.html#ga17">UART_INTERRUPT_HANDLER</a>(SIG_UART1_RECV) |
||
| 383 | 00376 { |
||
| 384 | 00377 <span class="comment">// service UART1 receive interrupt</span> |
||
| 385 | 00378 uartReceiveService(1); |
||
| 386 | 00379 } |
||
| 387 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:08 2006 for Procyon AVRlib by |
||
| 388 | <a href="http://www.doxygen.org/index.html"> |
||
| 389 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
| 390 | </body> |
||
| 391 | </html> |
Powered by WebSVN v2.8.3