⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ax88796_8c-source.html

📁 单片机的软件
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!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: net/ax88796.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></div><div class="nav"><a class="el" href="dir_000001.html">net</a></div><h1>ax88796.c</h1><a href="ax88796_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file ax88796.c \brief ASIX AX88796 Ethernet Interface Driver. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name    : 'ax88796.c'</span>00005 <span class="comment">// Title        : ASIX AX88796 Ethernet Interface Driver</span>00006 <span class="comment">// Author       : Pascal Stang</span>00007 <span class="comment">// Created      : 10/22/2002</span>00008 <span class="comment">// Revised      : 8/21/2005</span>00009 <span class="comment">// Version      : 0.1</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">// Description  : This driver provides initialization and transmit/receive</span>00014 <span class="comment">//      functions for the ASIX AX88796 10/100Mb Ethernet Controller and PHY.</span>00015 <span class="comment">//</span>00016 <span class="comment">// Based in part on code by Louis Beaudoin (www.embedded-creations.com).</span>00017 <span class="comment">// Thanks to Adam Dunkels and Louis Beaudoin for providing the initial</span>00018 <span class="comment">// structure in which to write this driver.</span>00019 <span class="comment">//</span>00020 <span class="comment">//*****************************************************************************</span>00021 00022 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00023 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>00024 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>00025 00026 <span class="preprocessor">#include "<a class="code" href="ax88796_8h.html">ax88796.h</a>"</span>00027 00028 <span class="comment">// include configuration</span>00029 <span class="preprocessor">#include "<a class="code" href="ax88796conf_8h.html">ax88796conf.h</a>"</span>00030 00031 <span class="comment">// pointers to locations in the ax88796 receive buffer</span>00032 <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> NextPage;              <span class="comment">// page pointer to next Rx packet</span>00033 <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> CurrentRetreiveAddress; <span class="comment">// DMA address for read Rx packet location</span>00034 00035 <a name="l00036"></a><a class="code" href="group__nic.html#ga0">00036</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga0">nicInit</a>(<span class="keywordtype">void</span>)00037 {00038     ax88796Init();00039 }00040 <a name="l00041"></a><a class="code" href="group__nic.html#ga1">00041</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga1">nicSend</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* packet)00042 {00043     ax88796BeginPacketSend(len);00044     ax88796SendPacketData(packet, len);00045     ax88796EndPacketSend();00046 }00047 <a name="l00048"></a><a class="code" href="group__nic.html#ga2">00048</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="group__nic.html#ga2">nicPoll</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> maxlen, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* packet)00049 {00050     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packetLength;00051     00052     packetLength = ax88796BeginPacketRetreive();00053 00054     <span class="comment">// if there's no packet or an error - exit without ending the operation</span>00055     <span class="keywordflow">if</span>( !packetLength )00056         <span class="keywordflow">return</span> 0;00057 00058     <span class="comment">// drop anything too big for the buffer</span>00059     <span class="keywordflow">if</span>( packetLength &gt; maxlen )00060     {00061         ax88796EndPacketRetreive();00062         <span class="keywordflow">return</span> 0;00063     }00064     00065     <span class="comment">// copy the packet data into the uIP packet buffer</span>00066     ax88796RetreivePacketData( packet, packetLength );00067     ax88796EndPacketRetreive();00068         00069     <span class="keywordflow">return</span> packetLength;00070 }00071 <a name="l00072"></a><a class="code" href="group__nic.html#ga3">00072</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga3">nicGetMacAddress</a>(u08* macaddr)00073 {00074     u08 tempCR;00075     <span class="comment">// switch register pages</span>00076     tempCR = ax88796Read(CR);00077     ax88796Write(CR,tempCR|PS0);00078     <span class="comment">// read MAC address registers</span>00079     *macaddr++ = ax88796Read(PAR0);00080     *macaddr++ = ax88796Read(PAR1);00081     *macaddr++ = ax88796Read(PAR2);00082     *macaddr++ = ax88796Read(PAR3);00083     *macaddr++ = ax88796Read(PAR4);00084     *macaddr++ = ax88796Read(PAR5);00085     <span class="comment">// switch register pages back</span>00086     ax88796Write(CR,tempCR);00087 }00088 <a name="l00089"></a><a class="code" href="group__nic.html#ga4">00089</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga4">nicRegDump</a>(<span class="keywordtype">void</span>)00090 {00091     ax88796RegDump();00092 }00093 00094 00095 <span class="keywordtype">void</span> ax88796SetupPorts(<span class="keywordtype">void</span>)00096 {00097 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span>00098 <span class="preprocessor"></span>    <span class="comment">// enable external SRAM interface - no wait states</span>00099     sbi(MCUCR, SRE);00100 <span class="comment">//  sbi(MCUCR, SRW10);</span>00101 <span class="comment">//  sbi(XMCRA, SRW00);</span>00102 <span class="comment">//  sbi(XMCRA, SRW01);</span>00103 <span class="comment">//  sbi(XMCRA, SRW11);</span>00104 <span class="preprocessor">#else</span>00105 <span class="preprocessor"></span>    <span class="comment">// set address port to output</span>00106     AX88796_ADDRESS_DDR = AX88796_ADDRESS_MASK;00107     00108     <span class="comment">// set data port to input with pull-ups</span>00109     AX88796_DATA_DDR = 0x00;00110     AX88796_DATA_PORT = 0xFF;00111 00112     <span class="comment">// initialize the control port read and write pins to de-asserted</span>00113     sbi( AX88796_CONTROL_PORT, AX88796_CONTROL_READPIN );00114     sbi( AX88796_CONTROL_PORT, AX88796_CONTROL_WRITEPIN );00115     <span class="comment">// set the read and write pins to output</span>00116     sbi( AX88796_CONTROL_DDR, AX88796_CONTROL_READPIN );00117     sbi( AX88796_CONTROL_DDR, AX88796_CONTROL_WRITEPIN );00118 <span class="preprocessor">#endif</span>00119 <span class="preprocessor"></span>    <span class="comment">// set reset pin to output</span>00120     sbi( AX88796_RESET_DDR, AX88796_RESET_PIN );00121 }00122 00123 00124 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span>00125 <span class="preprocessor"></span><span class="keyword">inline</span> <span class="keywordtype">void</span> ax88796Write(u08 address, u08 data)00126 {00127     *(<span class="keyword">volatile</span> u08*)(AX88796_MEMORY_MAPPED_OFFSET + address) = data;00128 }00129 <span class="preprocessor">#else</span>00130 <span class="preprocessor"></span><span class="keywordtype">void</span> ax88796Write(u08 address, u08 data)00131 {00132     <span class="comment">// assert the address</span>00133     AX88796_ADDRESS_PORT = address | (AX88796_ADDRESS_PORT&amp;~AX88796_ADDRESS_MASK);00134 00135     <span class="comment">// set data bus as output and place data on bus</span>00136     AX88796_DATA_DDR = 0xFF;00137     AX88796_DATA_PORT = data;00138     00139     <span class="comment">// clock write pin</span>00140     cbi(AX88796_CONTROL_PORT, AX88796_CONTROL_WRITEPIN);00141     nop();00142     nop();00143     sbi(AX88796_CONTROL_PORT, AX88796_CONTROL_WRITEPIN);00144     00145     <span class="comment">// set data bus back to input with pullups enabled</span>00146     AX88796_DATA_DDR = 0x00;00147     AX88796_DATA_PORT = 0xFF;00148 }00149 <span class="preprocessor">#endif</span>00150 <span class="preprocessor"></span>00151 00152 <span class="preprocessor">#if NIC_CONNECTION == MEMORY_MAPPED</span>00153 <span class="preprocessor"></span><span class="keyword">inline</span> u08 ax88796Read(u08 address)00154 {00155     <span class="keywordflow">return</span> *(<span class="keyword">volatile</span> u08*)(AX88796_MEMORY_MAPPED_OFFSET + address);00156 }00157 <span class="preprocessor">#else</span>00158 <span class="preprocessor"></span>u08 ax88796Read(u08 address)00159 {00160     u08 data;00161    00162     <span class="comment">// assert the address</span>00163     AX88796_ADDRESS_PORT = address | (AX88796_ADDRESS_PORT&amp;~AX88796_ADDRESS_MASK);00164 00165     <span class="comment">// assert read</span>00166     cbi(AX88796_CONTROL_PORT, AX88796_CONTROL_READPIN);00167     nop();00168     nop();00169     <span class="comment">// read in the data</span>00170     data = AX88796_DATA_PIN;00171 00172     <span class="comment">// negate read</span>00173     sbi(AX88796_CONTROL_PORT, AX88796_CONTROL_READPIN);00174 00175     <span class="keywordflow">return</span> data;00176 }00177 <span class="preprocessor">#endif                       </span>00178 <span class="preprocessor"></span>00179 00180 <span class="keywordtype">void</span> ax88796Init(<span class="keywordtype">void</span>)00181 {00182     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> tcrFduFlag;00183     00184     <span class="comment">// initialize I/O ports</span>00185     ax88796SetupPorts();00186 00187     <span class="comment">// do a hard reset</span>00188     sbi(AX88796_RESET_PORT, AX88796_RESET_PIN);00189     delay_ms(100);00190     cbi(AX88796_RESET_PORT, AX88796_RESET_PIN);00191 00192     <span class="comment">// do soft reset</span>00193     ax88796Write(ISR, ax88796Read(ISR));00194     delay_ms(50);00195 00196     <span class="comment">// wait for PHY to come out of reset</span>00197     ax88796Read(RSTPORT);00198     <span class="keywordflow">while</span>(ax88796Read(TR) &amp; RST_B);00199   00200     ax88796WriteMii(0x10,0x00,0x0800);00201     delay_ms(255);00202     ax88796WriteMii(0x10,0x00,0x1200);00203 00204     ax88796Write(CR,(RD2|STOP));        <span class="comment">// stop the NIC, abort DMA, page 0</span>00205     delay_ms(5);                        <span class="comment">// make sure nothing is coming in or going out</span>00206     ax88796Write(DCR,DCR_INIT);    00207     ax88796Write(RBCR0,0x00);00208     ax88796Write(RBCR1,0x00);00209     ax88796Write(IMR,0x00);00210     ax88796Write(ISR,0xFF);00211     ax88796Write(RCR,0x20);00212     ax88796Write(BNRY,RXSTART_INIT);00213     ax88796Write(PSTART,RXSTART_INIT);00214     ax88796Write(PSTOP,RXSTOP_INIT);00215     00216     <span class="comment">// switch to page 1</span>00217     ax88796Write(CR,(PS0|RD2|STOP));00218     <span class="comment">// write mac address</span>00219     ax88796Write(PAR0+0, AX88796_MAC0);00220     ax88796Write(PAR0+1, AX88796_MAC1);00221     ax88796Write(PAR0+2, AX88796_MAC2);00222     ax88796Write(PAR0+3, AX88796_MAC3);00223     ax88796Write(PAR0+4, AX88796_MAC4);00224     ax88796Write(PAR0+5, AX88796_MAC5);00225     <span class="comment">// set start point</span>00226     ax88796Write(CURR,RXSTART_INIT+1);00227 00228     ax88796Write(CR,(RD2|START));00229     ax88796Write(RCR,RCR_INIT);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -