📄 cs8900_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: net/cs8900.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 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><div class="nav"><a class="el" href="dir_000001.html">net</a></div><h1>cs8900.c</h1><a href="cs8900_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file cs8900.c \brief Crystal CS8900 Ethernet Interface Driver. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'cs8900.c'</span>00005 <span class="comment">// Title : Crystal CS8900 Ethernet Interface Driver</span>00006 <span class="comment">// Author : Pascal Stang</span>00007 <span class="comment">// Created : 11/7/2004</span>00008 <span class="comment">// Revised : 11/7/2004</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">//*****************************************************************************</span>00014 00015 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00016 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>00017 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>00018 00019 <span class="preprocessor">#include "<a class="code" href="cs8900_8h.html">cs8900.h</a>"</span>00020 00021 <span class="comment">// include configuration</span>00022 <span class="preprocessor">#include "<a class="code" href="cs8900conf_8h.html">cs8900conf.h</a>"</span>00023 <a name="l00024"></a><a class="code" href="group__nic.html#ga0">00024</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga0">nicInit</a>(<span class="keywordtype">void</span>)00025 {00026 cs8900Init();00027 }00028 <a name="l00029"></a><a class="code" href="group__nic.html#ga1">00029</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)00030 {00031 u08 timeout = 15;00032 00033 <span class="comment">// request space in CS8900's on-chip memory for storing an outgoing frame</span>00034 cs8900Write16(CS8900_IO_TXCMD, TX_START_ALL_BYTES);00035 cs8900Write16(CS8900_IO_TXLENGTH, len);00036 <span class="comment">// check if CS8900 is ready to accept the frame we want to send</span>00037 <span class="comment">// (timeout after 1.5ms since it should only take 1.25ms to</span>00038 <span class="comment">// finish sending previous frame. If we timeout, it's probably</span>00039 <span class="comment">// because there's no link, no ethernet cable.)</span>00040 <span class="keywordflow">while</span>(!(cs8900ReadReg(PP_BusST) & READY_FOR_TX_NOW) && timeout)00041 {00042 <span class="comment">// wait 100us</span>00043 delay_us(100);00044 timeout--;00045 }00046 <span class="comment">// write packet data bytes</span>00047 cs8900CopyToFrame(packet, len);00048 00049 <span class="comment">// packet is automatically sent upon completion of above write</span>00050 }00051 <a name="l00052"></a><a class="code" href="group__nic.html#ga2">00052</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)00053 {00054 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packetLength;00055 00056 packetLength = cs8900BeginPacketRetreive();00057 00058 <span class="comment">// if there's no packet or an error - exit without ending the operation</span>00059 <span class="keywordflow">if</span>( !packetLength )00060 <span class="keywordflow">return</span> 0;00061 00062 <span class="comment">// drop anything too big for the buffer</span>00063 <span class="keywordflow">if</span>( packetLength > maxlen )00064 {00065 cs8900EndPacketRetreive();00066 <span class="keywordflow">return</span> 0;00067 }00068 00069 <span class="comment">// copy the packet data into the packet buffer</span>00070 cs8900RetreivePacketData( packet, packetLength );00071 cs8900EndPacketRetreive();00072 00073 <span class="keywordflow">return</span> packetLength;00074 }00075 00076 <span class="keywordtype">void</span> nicGetMacAddress(u08* macaddr)00077 {00078 <span class="comment">// read MAC address registers</span>00079 <span class="comment">// TODO: check byte order here!</span>00080 *((<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span>*)(macaddr+0)) = cs8900ReadReg(PP_IA+0);00081 *((<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span>*)(macaddr+2)) = cs8900ReadReg(PP_IA+2);00082 *((<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span>*)(macaddr+4)) = cs8900ReadReg(PP_IA+4);00083 }00084 00085 <span class="keywordtype">void</span> nicSetMacAddress(u08* macaddr)00086 {00087 <span class="comment">// write MAC address registers</span>00088 cs8900WriteReg(PP_IA+0, (macaddr[1]<<8) + macaddr[0] );00089 cs8900WriteReg(PP_IA+2, (macaddr[3]<<8) + macaddr[2] );00090 cs8900WriteReg(PP_IA+4, (macaddr[5]<<8) + macaddr[4] );00091 }00092 00093 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> cs8900BeginPacketRetreive(<span class="keywordtype">void</span>)00094 {00095 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> status;00096 00097 <span class="comment">// check RxEvent</span>00098 status = cs8900ReadReg(PP_RxEvent);00099 00100 <span class="keywordflow">if</span>( !((status&RX_OK)||(status&RX_IA)||(status&RX_BROADCAST)) )00101 {00102 <span class="keywordflow">return</span> 0;00103 }00104 00105 <span class="comment">// return cs8900ReadReg(PP_RxFrameByteCnt);</span>00106 <span class="comment">// read RxStatus high-byte first</span>00107 status = cs8900Read(CS8900_IO_RXTX_DATA_PORT0+1)<<8;00108 status |= cs8900Read(CS8900_IO_RXTX_DATA_PORT0+0);00109 <span class="comment">// read packet length high-byte first</span>00110 status = cs8900Read(CS8900_IO_RXTX_DATA_PORT0+1)<<8;00111 status |= cs8900Read(CS8900_IO_RXTX_DATA_PORT0+0);00112 00113 <span class="keywordflow">return</span> status;00114 }00115 00116 <span class="keywordtype">void</span> cs8900RetreivePacketData(u08* packet, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packetLength )00117 {00118 cs8900CopyFromFrame(packet, packetLength);00119 }00120 00121 <span class="keywordtype">void</span> cs8900EndPacketRetreive(<span class="keywordtype">void</span>)00122 {00123 <span class="comment">// dummy read first four bytes</span>00124 <span class="comment">//cs8900CopyFromFrame(packet, 4);</span>00125 }00126 00127 00128 00129 <span class="keywordtype">void</span> cs8900InitPorts(<span class="keywordtype">void</span>)00130 {00131 <span class="preprocessor">#if MEMORY_MAPPED_NIC == 1</span>00132 <span class="preprocessor"></span> <span class="comment">// enable external SRAM interface - no wait states</span>00133 sbi(MCUSR, SRE);00134 <span class="preprocessor">#else</span>00135 <span class="preprocessor"></span> <span class="comment">// set address port to output</span>00136 outb(CS8900_ADDRESS_DDR, CS8900_ADDRESS_MASK);00137 00138 <span class="comment">// set data port to input with pull-ups</span>00139 outb(CS8900_DATA_DDR, 0x00);00140 outb(CS8900_DATA_PORT, 0xFF);00141 00142 <span class="comment">// initialize the control port read and write pins to de-asserted</span>00143 sbi( CS8900_CONTROL_PORT, CS8900_CONTROL_READPIN );00144 sbi( CS8900_CONTROL_PORT, CS8900_CONTROL_WRITEPIN );00145 <span class="comment">// set the read and write pins to output</span>00146 sbi( CS8900_CONTROL_DDR, CS8900_CONTROL_READPIN );00147 sbi( CS8900_CONTROL_DDR, CS8900_CONTROL_WRITEPIN );00148 <span class="preprocessor">#endif</span>00149 <span class="preprocessor"></span> <span class="comment">// set reset pin to output</span>00150 sbi( CS8900_RESET_DDR, CS8900_RESET_PIN );00151 }00152 00153 <span class="keywordtype">void</span> cs8900Init(<span class="keywordtype">void</span>)00154 {00155 cs8900InitPorts();00156 00157 <span class="comment">// assert hardware reset</span>00158 sbi( CS8900_RESET_PORT, CS8900_RESET_PIN );00159 <span class="comment">// wait</span>00160 delay_ms(10);00161 <span class="comment">// release hardware reset</span>00162 cbi( CS8900_RESET_PORT, CS8900_RESET_PIN );00163 delay_ms(10);00164 00165 <span class="comment">// Reset the Ethernet-Controller</span>00166 cs8900Write16(CS8900_IO_PP_PTR, PP_SelfCTL);00167 cs8900Write16(CS8900_IO_PP_DATA_PORT0, POWER_ON_RESET);00168 <span class="comment">// wait until chip-reset is done</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -