📄 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></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 <a name="l00076"></a><a class="code" href="group__nic.html#ga3">00076</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga3">nicGetMacAddress</a>(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">unsigned</span> <span class="keywordtype">int</span> cs8900BeginPacketRetreive(<span class="keywordtype">void</span>)00086 {00087 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> status;00088 00089 <span class="comment">// check RxEvent</span>00090 status = cs8900ReadReg(PP_RxEvent);00091 00092 <span class="keywordflow">if</span>( !((status&RX_OK)||(status&RX_IA)||(status&RX_BROADCAST)) )00093 {00094 <span class="keywordflow">return</span> 0;00095 }00096 00097 <span class="comment">// return cs8900ReadReg(PP_RxFrameByteCnt);</span>00098 <span class="comment">// read RxStatus high-byte first</span>00099 status = cs8900Read(CS8900_IO_RXTX_DATA_PORT0+1)<<8;00100 status |= cs8900Read(CS8900_IO_RXTX_DATA_PORT0+0);00101 <span class="comment">// read packet length high-byte first</span>00102 status = cs8900Read(CS8900_IO_RXTX_DATA_PORT0+1)<<8;00103 status |= cs8900Read(CS8900_IO_RXTX_DATA_PORT0+0);00104 00105 <span class="keywordflow">return</span> status;00106 }00107 00108 <span class="keywordtype">void</span> cs8900RetreivePacketData(u08* packet, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> packetLength )00109 {00110 cs8900CopyFromFrame(packet, packetLength);00111 }00112 00113 <span class="keywordtype">void</span> cs8900EndPacketRetreive(<span class="keywordtype">void</span>)00114 {00115 <span class="comment">// dummy read first four bytes</span>00116 <span class="comment">//cs8900CopyFromFrame(packet, 4);</span>00117 }00118 00119 00120 00121 <span class="keywordtype">void</span> cs8900InitPorts(<span class="keywordtype">void</span>)00122 {00123 <span class="preprocessor">#if MEMORY_MAPPED_NIC == 1</span>00124 <span class="preprocessor"></span> <span class="comment">// enable external SRAM interface - no wait states</span>00125 sbi(MCUSR, SRE);00126 <span class="preprocessor">#else</span>00127 <span class="preprocessor"></span> <span class="comment">// set address port to output</span>00128 outb(CS8900_ADDRESS_DDR, CS8900_ADDRESS_MASK);00129 00130 <span class="comment">// set data port to input with pull-ups</span>00131 outb(CS8900_DATA_DDR, 0x00);00132 outb(CS8900_DATA_PORT, 0xFF);00133 00134 <span class="comment">// initialize the control port read and write pins to de-asserted</span>00135 sbi( CS8900_CONTROL_PORT, CS8900_CONTROL_READPIN );00136 sbi( CS8900_CONTROL_PORT, CS8900_CONTROL_WRITEPIN );00137 <span class="comment">// set the read and write pins to output</span>00138 sbi( CS8900_CONTROL_DDR, CS8900_CONTROL_READPIN );00139 sbi( CS8900_CONTROL_DDR, CS8900_CONTROL_WRITEPIN );00140 <span class="preprocessor">#endif</span>00141 <span class="preprocessor"></span> <span class="comment">// set reset pin to output</span>00142 sbi( CS8900_RESET_DDR, CS8900_RESET_PIN );00143 }00144 00145 <span class="keywordtype">void</span> cs8900Init(<span class="keywordtype">void</span>)00146 {00147 cs8900InitPorts();00148 00149 <span class="comment">// assert hardware reset</span>00150 sbi( CS8900_RESET_PORT, CS8900_RESET_PIN );00151 <span class="comment">// wait</span>00152 delay_ms(10);00153 <span class="comment">// release hardware reset</span>00154 cbi( CS8900_RESET_PORT, CS8900_RESET_PIN );00155 delay_ms(10);00156 00157 <span class="comment">// Reset the Ethernet-Controller</span>00158 cs8900Write16(CS8900_IO_PP_PTR, PP_SelfCTL);00159 cs8900Write16(CS8900_IO_PP_DATA_PORT0, POWER_ON_RESET);00160 <span class="comment">// wait until chip-reset is done</span>00161 cs8900Write16(CS8900_IO_PP_PTR, PP_SelfST);00162 <span class="keywordflow">while</span>(!(cs8900Read16(CS8900_IO_PP_DATA_PORT0) & INIT_DONE));00163 00164 <span class="comment">// set our MAC as Individual Address</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -