📄 uart_8c-source.html
字号:
<!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: uart.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.6 --><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="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>uart.c</h1><a href="uart_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*! \file uart.c \brief UART driver with buffer support. */</span>00002 <span class="comment">// *****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'uart.c'</span>00005 <span class="comment">// Title : UART driver with buffer support</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2000-2002</span>00007 <span class="comment">// Created : 11/22/2000</span>00008 <span class="comment">// Revised : 06/09/2003</span>00009 <span class="comment">// Version : 1.3</span>00010 <span class="comment">// Target MCU : ATMEL AVR Series</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 <avr/io.h></span>00019 <span class="preprocessor">#include <avr/interrupt.h></span>00020 <span class="preprocessor">#include <avr/signal.h></span>00021 00022 <span class="preprocessor">#include "<a class="code" href="buffer_8h.html">buffer.h</a>"</span>00023 <span class="preprocessor">#include "<a class="code" href="uart_8h.html">uart.h</a>"</span>00024 00025 <span class="comment">// UART global variables</span>00026 <span class="comment">// flag variables</span><a name="l00027"></a><a class="code" href="uart_8c.html#a0">00027</a> <span class="keyword">volatile</span> u08 <a class="code" href="uart_8c.html#a0">uartReadyTx</a>; <span class="comment">///< uartReadyTx flag</span><a name="l00028"></a><a class="code" href="uart_8c.html#a1">00028</a> <span class="comment"></span><span class="keyword">volatile</span> u08 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a>; <span class="comment">///< uartBufferedTx flag</span>00029 <span class="comment"></span><span class="comment">// receive and transmit buffers</span><a name="l00030"></a><a class="code" href="uart_8c.html#a2">00030</a> cBuffer <a class="code" href="uart_8c.html#a2">uartRxBuffer</a>; <span class="comment">///< uart receive buffer</span><a name="l00031"></a><a class="code" href="uart_8c.html#a3">00031</a> <span class="comment"></span>cBuffer <a class="code" href="uart_8c.html#a3">uartTxBuffer</a>; <span class="comment">///< uart transmit buffer</span><a name="l00032"></a><a class="code" href="uart_8c.html#a4">00032</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">///< receive overflow counter</span>00033 <span class="comment"></span>00034 <span class="preprocessor">#ifndef UART_BUFFERS_EXTERNAL_RAM</span>00035 <span class="preprocessor"></span> <span class="comment">// using internal ram,</span>00036 <span class="comment">// automatically allocate space in ram for each buffer</span>00037 <span class="keyword">static</span> <span class="keywordtype">char</span> uartRxData[<a class="code" href="uart_8h.html#a2">UART_RX_BUFFER_SIZE</a>];00038 <span class="keyword">static</span> <span class="keywordtype">char</span> uartTxData[<a class="code" href="uart_8h.html#a1">UART_TX_BUFFER_SIZE</a>];00039 <span class="preprocessor">#endif</span>00040 <span class="preprocessor"></span>00041 <span class="keyword">typedef</span> void (*voidFuncPtru08)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>);00042 <span class="keyword">volatile</span> <span class="keyword">static</span> voidFuncPtru08 UartRxFunc;00043 <span class="comment"></span>00044 <span class="comment">//! enable and initialize the uart</span><a name="l00045"></a><a class="code" href="uart2_8h.html#a17">00045</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uart_8c.html#a9">uartInit</a>(<span class="keywordtype">void</span>)00046 {00047 <span class="comment">// initialize the buffers</span>00048 <a class="code" href="uart_8c.html#a10">uartInitBuffers</a>();00049 <span class="comment">// initialize user receive handler</span>00050 UartRxFunc = 0;00051 00052 <span class="comment">// enable RxD/TxD and interrupts</span>00053 outb(UCR, BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN));00054 00055 <span class="comment">// set default baud rate</span>00056 <a class="code" href="uart2_8h.html#a23">uartSetBaudRate</a>(<a class="code" href="uart_8h.html#a0">UART_DEFAULT_BAUD_RATE</a>); 00057 <span class="comment">// initialize states</span>00058 <a class="code" href="uart_8c.html#a0">uartReadyTx</a> = TRUE;00059 <a class="code" href="uart_8c.html#a1">uartBufferedTx</a> = FALSE;00060 <span class="comment">// clear overflow count</span>00061 <a class="code" href="uart_8c.html#a4">uartRxOverflow</a> = 0;00062 <span class="comment">// enable interrupts</span>00063 sei();00064 }00065 <span class="comment"></span>00066 <span class="comment">//! create and initialize the uart transmit and receive buffers</span><a name="l00067"></a><a class="code" href="uart_8h.html#a4">00067</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uart_8c.html#a10">uartInitBuffers</a>(<span class="keywordtype">void</span>)00068 {00069 <span class="preprocessor"> #ifndef UART_BUFFERS_EXTERNAL_RAM</span>00070 <span class="preprocessor"></span> <span class="comment">// initialize the UART receive buffer</span>00071 <a class="code" href="buffer_8h.html#a1">bufferInit</a>(&<a class="code" href="uart_8c.html#a2">uartRxBuffer</a>, uartRxData, <a class="code" href="uart_8h.html#a2">UART_RX_BUFFER_SIZE</a>);00072 <span class="comment">// initialize the UART transmit buffer</span>00073 <a class="code" href="buffer_8h.html#a1">bufferInit</a>(&<a class="code" href="uart_8c.html#a3">uartTxBuffer</a>, uartTxData, <a class="code" href="uart_8h.html#a1">UART_TX_BUFFER_SIZE</a>);00074 <span class="preprocessor"> #else</span>00075 <span class="preprocessor"></span> <span class="comment">// initialize the UART receive buffer</span>00076 <a class="code" href="buffer_8h.html#a1">bufferInit</a>(&<a class="code" href="uart_8c.html#a2">uartRxBuffer</a>, (u08*) UART_RX_BUFFER_ADDR, <a class="code" href="uart_8h.html#a2">UART_RX_BUFFER_SIZE</a>);00077 <span class="comment">// initialize the UART transmit buffer</span>00078 <a class="code" href="buffer_8h.html#a1">bufferInit</a>(&<a class="code" href="uart_8c.html#a3">uartTxBuffer</a>, (u08*) UART_TX_BUFFER_ADDR, <a class="code" href="uart_8h.html#a1">UART_TX_BUFFER_SIZE</a>);00079 <span class="preprocessor"> #endif</span>00080 <span class="preprocessor"></span>}00081 <span class="comment"></span>00082 <span class="comment">//! redirects received data to a user function</span><a name="l00083"></a><a class="code" href="uart_8h.html#a6">00083</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uart2_8h.html#a22">uartSetRxHandler</a>(<span class="keywordtype">void</span> (*rx_func)(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> c))00084 {00085 <span class="comment">// set the receive interrupt to run the supplied user function</span>00086 UartRxFunc = rx_func;00087 }00088 <span class="comment"></span>00089 <span class="comment">//! set the uart baud rate</span><a name="l00090"></a><a class="code" href="uart_8h.html#a7">00090</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uart2_8h.html#a23">uartSetBaudRate</a>(u32 baudrate)00091 {00092 <span class="comment">// calculate division factor for requested baud rate, and set it</span>00093 u16 bauddiv = ((F_CPU+(baudrate*8L))/(baudrate*16L)-1);00094 outb(UBRRL, bauddiv);00095 <span class="preprocessor"> #ifdef UBRRH</span>00096 <span class="preprocessor"></span> outb(UBRRH, bauddiv>>8);00097 <span class="preprocessor"> #endif</span>00098 <span class="preprocessor"></span>}00099 <span class="comment"></span>00100 <span class="comment">//! returns the receive buffer structure </span><a name="l00101"></a><a class="code" href="uart_8h.html#a8">00101</a> <span class="comment"></span>cBuffer* <a class="code" href="uart_8c.html#a13">uartGetRxBuffer</a>(<span class="keywordtype">void</span>)00102 {00103 <span class="comment">// return rx buffer pointer</span>00104 <span class="keywordflow">return</span> &<a class="code" href="uart_8c.html#a2">uartRxBuffer</a>;00105 }00106 <span class="comment"></span>00107 <span class="comment">//! returns the transmit buffer structure </span><a name="l00108"></a><a class="code" href="uart_8h.html#a9">00108</a> <span class="comment"></span>cBuffer* <a class="code" href="uart_8c.html#a14">uartGetTxBuffer</a>(<span class="keywordtype">void</span>)00109 {00110 <span class="comment">// return tx buffer pointer</span>00111 <span class="keywordflow">return</span> &<a class="code" href="uart_8c.html#a3">uartTxBuffer</a>;00112 }00113 <span class="comment"></span>00114 <span class="comment">//! transmits a byte over the uart</span><a name="l00115"></a><a class="code" href="uart_8h.html#a10">00115</a> <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="uart2_8h.html#a26">uartSendByte</a>(u08 txData)00116 {00117 <span class="comment">// wait for the transmitter to be ready</span>00118 <span class="keywordflow">while</span>(!<a class="code" href="uart_8c.html#a0">uartReadyTx</a>);00119 <span class="comment">// send byte</span>00120 outp( txData, UDR );00121 <span class="comment">// set ready state to FALSE</span>00122 <a class="code" href="uart_8c.html#a0">uartReadyTx</a> = FALSE;00123 }00124 <span class="comment"></span>00125 <span class="comment">//! gets a single byte from the uart receive buffer (getchar-style)</span><a name="l00126"></a><a class="code" href="uart_8h.html#a11">00126</a> <span class="comment"></span><span class="keywordtype">int</span> <a class="code" href="uart_8c.html#a16">uartGetByte</a>(<span class="keywordtype">void</span>)00127 {00128 u08 c;00129 <span class="keywordflow">if</span>(<a class="code" href="uart2_8h.html#a31">uartReceiveByte</a>(&c))00130 <span class="keywordflow">return</span> c;00131 <span class="keywordflow">else</span>00132 <span class="keywordflow">return</span> -1;00133 }00134 <span class="comment"></span>00135 <span class="comment">//! gets a byte (if available) from the uart receive buffer</span><a name="l00136"></a><a class="code" href="uart_8h.html#a12">00136</a> <span class="comment"></span>u08 <a class="code" href="uart2_8h.html#a31">uartReceiveByte</a>(u08* rxData)00137 {00138 <span class="comment">// make sure we have a receive buffer</span>00139 <span class="keywordflow">if</span>(<a class="code" href="uart_8c.html#a2">uartRxBuffer</a>.size)00140 {00141 <span class="comment">// make sure we have data</span>00142 <span class="keywordflow">if</span>(<a class="code" href="uart_8c.html#a2">uartRxBuffer</a>.datalength)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -