📄 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 "avr/io.h"</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="enc28j60_8h.html">enc28j60.h</a>"</span>00027 00028 <span class="preprocessor">#ifdef SPDR0</span>00029 <span class="preprocessor"></span><span class="preprocessor"> #define SPDR SPDR0</span>00030 <span class="preprocessor"></span><span class="preprocessor"> #define SPCR SPCR0</span>00031 <span class="preprocessor"></span><span class="preprocessor"> #define SPSR SPSR0</span>00032 <span class="preprocessor"></span>00033 <span class="preprocessor"> #define SPIF SPIF0</span>00034 <span class="preprocessor"></span><span class="preprocessor"> #define MSTR MSTR0</span>00035 <span class="preprocessor"></span><span class="preprocessor"> #define CPOL CPOL0</span>00036 <span class="preprocessor"></span><span class="preprocessor"> #define DORD DORD0</span>00037 <span class="preprocessor"></span><span class="preprocessor"> #define SPR0 SPR00</span>00038 <span class="preprocessor"></span><span class="preprocessor"> #define SPR1 SPR01</span>00039 <span class="preprocessor"></span><span class="preprocessor"> #define SPI2X SPI2X0</span>00040 <span class="preprocessor"></span><span class="preprocessor"> #define SPE SPE0</span>00041 <span class="preprocessor"></span><span class="preprocessor">#endif</span>00042 <span class="preprocessor"></span>00043 <span class="comment">// include configuration</span>00044 <span class="preprocessor">#include "<a class="code" href="enc28j60conf_8h.html">enc28j60conf.h</a>"</span>00045 00046 u08 Enc28j60Bank;00047 u16 NextPacketPtr;00048 <a name="l00049"></a><a class="code" href="group__nic.html#ga0">00049</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga0">nicInit</a>(<span class="keywordtype">void</span>)00050 {00051 <a class="code" href="group__enc28j60.html#ga9">enc28j60Init</a>();00052 }00053 <a name="l00054"></a><a class="code" href="group__nic.html#ga1">00054</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)00055 {00056 <a class="code" href="group__enc28j60.html#ga10">enc28j60PacketSend</a>(len, packet);00057 }00058 <a name="l00059"></a><a class="code" href="group__nic.html#ga2">00059</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)00060 {00061 <span class="keywordflow">return</span> <a class="code" href="group__enc28j60.html#ga11">enc28j60PacketReceive</a>(maxlen, packet);00062 }00063 00064 <span class="keywordtype">void</span> nicGetMacAddress(u08* macaddr)00065 {00066 <span class="comment">// read MAC address registers</span>00067 <span class="comment">// NOTE: MAC address in ENC28J60 is byte-backward</span>00068 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR5);00069 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR4);00070 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR3);00071 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR2);00072 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR1);00073 *macaddr++ = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR0);00074 }00075 00076 <span class="keywordtype">void</span> nicSetMacAddress(u08* macaddr)00077 {00078 <span class="comment">// write MAC address</span>00079 <span class="comment">// NOTE: MAC address in ENC28J60 is byte-backward</span>00080 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR5, *macaddr++);00081 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR4, *macaddr++);00082 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR3, *macaddr++);00083 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR2, *macaddr++);00084 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR1, *macaddr++);00085 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR0, *macaddr++);00086 }00087 <a name="l00088"></a><a class="code" href="group__nic.html#ga5">00088</a> <span class="keywordtype">void</span> <a class="code" href="group__nic.html#ga5">nicRegDump</a>(<span class="keywordtype">void</span>)00089 {00090 <a class="code" href="group__enc28j60.html#ga13">enc28j60RegDump</a>();00091 }00092 00093 <span class="comment">/*</span>00094 <span class="comment">void ax88796SetupPorts(void)</span>00095 <span class="comment">{</span>00096 <span class="comment">#if NIC_CONNECTION == MEMORY_MAPPED</span>00097 <span class="comment"> // enable external SRAM interface - no wait states</span>00098 <span class="comment"> sbi(MCUCR, SRE);</span>00099 <span class="comment">// sbi(MCUCR, SRW10);</span>00100 <span class="comment">// sbi(XMCRA, SRW00);</span>00101 <span class="comment">// sbi(XMCRA, SRW01);</span>00102 <span class="comment">// sbi(XMCRA, SRW11);</span>00103 <span class="comment">#else</span>00104 <span class="comment"> // set address port to output</span>00105 <span class="comment"> AX88796_ADDRESS_DDR = AX88796_ADDRESS_MASK;</span>00106 <span class="comment"> </span>00107 <span class="comment"> // set data port to input with pull-ups</span>00108 <span class="comment"> AX88796_DATA_DDR = 0x00;</span>00109 <span class="comment"> AX88796_DATA_PORT = 0xFF;</span>00110 <span class="comment"></span>00111 <span class="comment"> // initialize the control port read and write pins to de-asserted</span>00112 <span class="comment"> sbi( AX88796_CONTROL_PORT, AX88796_CONTROL_READPIN );</span>00113 <span class="comment"> sbi( AX88796_CONTROL_PORT, AX88796_CONTROL_WRITEPIN );</span>00114 <span class="comment"> // set the read and write pins to output</span>00115 <span class="comment"> sbi( AX88796_CONTROL_DDR, AX88796_CONTROL_READPIN );</span>00116 <span class="comment"> sbi( AX88796_CONTROL_DDR, AX88796_CONTROL_WRITEPIN );</span>00117 <span class="comment">#endif</span>00118 <span class="comment"> // set reset pin to output</span>00119 <span class="comment"> sbi( AX88796_RESET_DDR, AX88796_RESET_PIN );</span>00120 <span class="comment">}</span>00121 <span class="comment">*/</span>00122 <a name="l00123"></a><a class="code" href="group__enc28j60.html#ga0">00123</a> u08 <a class="code" href="group__enc28j60.html#ga0">enc28j60ReadOp</a>(u08 <a class="code" href="structnetBootpHeader.html#o0">op</a>, u08 address)00124 {00125 u08 data;00126 00127 <span class="comment">// assert CS</span>00128 ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_CONTROL_CS);00129 00130 <span class="comment">// issue read command</span>00131 SPDR = op | (address & ADDR_MASK);00132 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00133 <span class="comment">// read data</span>00134 SPDR = 0x00;00135 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00136 <span class="comment">// do dummy read if needed</span>00137 <span class="keywordflow">if</span>(address & 0x80)00138 {00139 SPDR = 0x00;00140 <span class="keywordflow">while</span>(!(inb(SPSR) & (1<<SPIF)));00141 }00142 data = SPDR;00143 00144 <span class="comment">// release CS</span>00145 ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS);00146 00147 <span class="keywordflow">return</span> data;00148 }00149 <a name="l00150"></a><a class="code" href="group__enc28j60.html#ga1">00150</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)00151 {00152 <span class="comment">// assert CS</span>00153 ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_CONTROL_CS);00154 00155 <span class="comment">// issue write command</span>00156 SPDR = op | (address & ADDR_MASK);00157 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00158 <span class="comment">// write data</span>00159 SPDR = data;00160 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00161 00162 <span class="comment">// release CS</span>00163 ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS);00164 }00165 <a name="l00166"></a><a class="code" href="group__enc28j60.html#ga2">00166</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga2">enc28j60ReadBuffer</a>(u16 len, u08* data)00167 {00168 <span class="comment">// assert CS</span>00169 ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_CONTROL_CS);00170 00171 <span class="comment">// issue read command</span>00172 SPDR = ENC28J60_READ_BUF_MEM;00173 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));00174 <span class="keywordflow">while</span>(len--)00175 {00176 <span class="comment">// read data</span>00177 SPDR = 0x00;00178 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -