📄 enc28j60_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/enc28j60.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>enc28j60.c</h1><a href="enc28j60_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file enc28j60.c \brief Microchip ENC28J60 Ethernet Interface Driver. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'enc28j60.c'</span>00005 <span class="comment">// Title : Microchip ENC28J60 Ethernet Interface Driver</span>00006 <span class="comment">// Author : Pascal Stang (c)2005</span>00007 <span class="comment">// Created : 9/22/2005</span>00008 <span class="comment">// Revised : 9/22/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 Microchip ENC28J60 10Mb Ethernet Controller and PHY.</span>00015 <span class="comment">// This chip is novel in that it is a full MAC+PHY interface all in a 28-pin</span>00016 <span class="comment">// chip, using an SPI interface to the host processor.</span>00017 <span class="comment">//</span>00018 <span class="comment">//*****************************************************************************</span>00019 00020 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00021 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span>00022 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>00023 00024 <span class="preprocessor">#include "<a class="code" href="enc28j60_8h.html">enc28j60.h</a>"</span>00025 00026 <span class="comment">// include configuration</span>00027 <span class="comment">//#include "ax88796conf.h"</span>00028 00029 u08 Enc28j60Bank;00030 u16 NextPacketPtr;00031 <a name="l00032"></a><a class="code" href="group__nic.html#ga0">00032</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga0">nicInit</a>(<span class="keywordtype">void</span>)00033 {00034 <a class="code" href="group__enc28j60.html#ga9">enc28j60Init</a>();00035 }00036 <a name="l00037"></a><a class="code" href="group__nic.html#ga1">00037</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)00038 {00039 <a class="code" href="group__enc28j60.html#ga10">enc28j60PacketSend</a>(len, packet);00040 }00041 <a name="l00042"></a><a class="code" href="group__nic.html#ga2">00042</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)00043 {00044 <span class="keywordflow">return</span> <a class="code" href="group__enc28j60.html#ga11">enc28j60PacketReceive</a>(maxlen, packet);00045 }00046 00047 <span class="keywordtype">void</span> nicGetMacAddress(u08* macaddr)00048 {00049 <span class="comment">// read MAC address registers</span>00050 <span class="comment">// NOTE: MAC address in ENC28J60 is byte-backward</span>00051 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR5);00052 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR4);00053 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR3);00054 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR2);00055 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR1);00056 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR0);00057 }00058 00059 <span class="keywordtype">void</span> nicSetMacAddress(u08* macaddr)00060 {00061 <span class="comment">// write MAC address</span>00062 <span class="comment">// NOTE: MAC address in ENC28J60 is byte-backward</span>00063 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR5, *macaddr++);00064 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR4, *macaddr++);00065 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR3, *macaddr++);00066 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR2, *macaddr++);00067 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR1, *macaddr++);00068 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR0, *macaddr++);00069 }00070 <a name="l00071"></a><a class="code" href="group__nic.html#ga5">00071</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga5">nicRegDump</a>(<span class="keywordtype">void</span>)00072 {00073 <a class="code" href="group__enc28j60.html#ga13">enc28j60RegDump</a>();00074 }00075 00076 <span class="comment">/*</span>00077 <span class="comment">void ax88796SetupPorts(void)</span>00078 <span class="comment">{</span>00079 <span class="comment">#if NIC_CONNECTION == MEMORY_MAPPED</span>00080 <span class="comment"> // enable external SRAM interface - no wait states</span>00081 <span class="comment"> sbi(MCUCR, SRE);</span>00082 <span class="comment">// sbi(MCUCR, SRW10);</span>00083 <span class="comment">// sbi(XMCRA, SRW00);</span>00084 <span class="comment">// sbi(XMCRA, SRW01);</span>00085 <span class="comment">// sbi(XMCRA, SRW11);</span>00086 <span class="comment">#else</span>00087 <span class="comment"> // set address port to output</span>00088 <span class="comment"> AX88796_ADDRESS_DDR = AX88796_ADDRESS_MASK;</span>00089 <span class="comment"> </span>00090 <span class="comment"> // set data port to input with pull-ups</span>00091 <span class="comment"> AX88796_DATA_DDR = 0x00;</span>00092 <span class="comment"> AX88796_DATA_PORT = 0xFF;</span>00093 <span class="comment"></span>00094 <span class="comment"> // initialize the control port read and write pins to de-asserted</span>00095 <span class="comment"> sbi( AX88796_CONTROL_PORT, AX88796_CONTROL_READPIN );</span>00096 <span class="comment"> sbi( AX88796_CONTROL_PORT, AX88796_CONTROL_WRITEPIN );</span>00097 <span class="comment"> // set the read and write pins to output</span>00098 <span class="comment"> sbi( AX88796_CONTROL_DDR, AX88796_CONTROL_READPIN );</span>00099 <span class="comment"> sbi( AX88796_CONTROL_DDR, AX88796_CONTROL_WRITEPIN );</span>00100 <span class="comment">#endif</span>00101 <span class="comment"> // set reset pin to output</span>00102 <span class="comment"> sbi( AX88796_RESET_DDR, AX88796_RESET_PIN );</span>00103 <span class="comment">}</span>00104 <span class="comment">*/</span>00105 <a name="l00106"></a><a class="code" href="group__enc28j60.html#ga0">00106</a> u08 <a class="code" href="group__enc28j60.html#ga0">enc28j60ReadOp</a>(u08 <a class="code" href="structnetBootpHeader.html#o0">op</a>, u08 address)00107 {00108 u08 data;00109 00110 <span class="comment">// assert CS</span>00111 ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_CONTROL_CS);00112 00113 <span class="comment">// issue read command</span>00114 SPDR = op | (address & ADDR_MASK);00115 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00116 <span class="comment">// read data</span>00117 SPDR = 0x00;00118 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00119 <span class="comment">// do dummy read if needed</span>00120 <span class="keywordflow">if</span>(address & 0x80)00121 {00122 SPDR = 0x00;00123 <span class="keywordflow">while</span>(!(inb(SPSR) & (1<<SPIF)));00124 }00125 data = SPDR;00126 00127 <span class="comment">// release CS</span>00128 ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS);00129 00130 <span class="keywordflow">return</span> data;00131 }00132 <a name="l00133"></a><a class="code" href="group__enc28j60.html#ga1">00133</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(u08 <a class="code" href="structnetBootpHeader.html#o0">op</a>, u08 address, u08 data)00134 {00135 <span class="comment">// assert CS</span>00136 ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_CONTROL_CS);00137 00138 <span class="comment">// issue write command</span>00139 SPDR = op | (address & ADDR_MASK);00140 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00141 <span class="comment">// write data</span>00142 SPDR = data;00143 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00144 00145 <span class="comment">// release CS</span>00146 ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS);00147 }00148 <a name="l00149"></a><a class="code" href="group__enc28j60.html#ga2">00149</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga2">enc28j60ReadBuffer</a>(u16 len, u08* data)00150 {00151 <span class="comment">// assert CS</span>00152 ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_CONTROL_CS);00153 00154 <span class="comment">// issue read command</span>00155 SPDR = ENC28J60_READ_BUF_MEM;00156 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00157 <span class="keywordflow">while</span>(len--)00158 {00159 <span class="comment">// read data</span>00160 SPDR = 0x00;00161 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00162 *data++ = SPDR;00163 } 00164 <span class="comment">// release CS</span>00165 ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS);00166 }00167 <a name="l00168"></a><a class="code" href="group__enc28j60.html#ga3">00168</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga3">enc28j60WriteBuffer</a>(u16 len, u08* data)00169 {00170 <span class="comment">// assert CS</span>00171 ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_CONTROL_CS);00172
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -