<!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)));
00179 *data++ = SPDR;
00180 }
00181 <span class="comment">// release CS</span>
00182 ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS);
00183 }
00184
<a name="l00185"></a><a class="code" href="group__enc28j60.html#ga3">00185</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga3">enc28j60WriteBuffer</a>(u16 len, u08* data)
00186 {
00187 <span class="comment">// assert CS</span>
00188 ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_CONTROL_CS);
00189
00190 <span class="comment">// issue write command</span>
00191 SPDR = ENC28J60_WRITE_BUF_MEM;
00192 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));
00193 <span class="keywordflow">while</span>(len--)
00194 {
00195 <span class="comment">// write data</span>
00196 SPDR = *data++;
00197 <span class="keywordflow">while</span>(!(SPSR & (1<<SPIF)));
00198 }
00199 <span class="comment">// release CS</span>
00200 ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS);
00201 }
00202
<a name="l00203"></a><a class="code" href="group__enc28j60.html#ga4">00203</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga4">enc28j60SetBank</a>(u08 address)
00204 {
00205 <span class="comment">// set the bank (if needed)</span>
00206 <span class="keywordflow">if</span>((address & BANK_MASK) != Enc28j60Bank)
00207 {
00208 <span class="comment">// set the bank</span>
00209 <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(ENC28J60_BIT_FIELD_CLR, ECON1, (ECON1_BSEL1|ECON1_BSEL0));
00210 <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(ENC28J60_BIT_FIELD_SET, ECON1, (address & BANK_MASK)>>5);
00211 Enc28j60Bank = (address & BANK_MASK);
00212 }
00213 }
00214
<a name="l00215"></a><a class="code" href="group__enc28j60.html#ga5">00215</a> u08 <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(u08 address)
00216 {
00217 <span class="comment">// set the bank</span>
00218 <a class="code" href="group__enc28j60.html#ga4">enc28j60SetBank</a>(address);
00219 <span class="comment">// do the read</span>
00220 <span class="keywordflow">return</span> <a class="code" href="group__enc28j60.html#ga0">enc28j60ReadOp</a>(ENC28J60_READ_CTRL_REG, address);
00221 }
00222
<a name="l00223"></a><a class="code" href="group__enc28j60.html#ga6">00223</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(u08 address, u08 data)
00224 {
00225 <span class="comment">// set the bank</span>
00226 <a class="code" href="group__enc28j60.html#ga4">enc28j60SetBank</a>(address);
00227 <span class="comment">// do the write</span>
00228 <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(ENC28J60_WRITE_CTRL_REG, address, data);
00229 }
00230
<a name="l00231"></a><a class="code" href="group__enc28j60.html#ga7">00231</a> u16 <a class="code" href="group__enc28j60.html#ga7">enc28j60PhyRead</a>(u08 address)
00232 {
00233 u16 data;
00234
00235 <span class="comment">// Set the right address and start the register read operation</span>
00236 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MIREGADR, address);
00237 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MICMD, MICMD_MIIRD);
00238
00239 <span class="comment">// wait until the PHY read completes</span>
00240 <span class="keywordflow">while</span>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MISTAT) & MISTAT_BUSY);
00241
00242 <span class="comment">// quit reading</span>
00243 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MICMD, 0x00);
00244
00245 <span class="comment">// get data value</span>
00246 data = <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MIRDL);
00247 data |= <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MIRDH);
00248 <span class="comment">// return the data</span>
00249 <span class="keywordflow">return</span> data;
00250 }
00251
<a name="l00252"></a><a class="code" href="group__enc28j60.html#ga8">00252</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga8">enc28j60PhyWrite</a>(u08 address, u16 data)
00253 {
00254 <span class="comment">// set the PHY register address</span>
00255 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MIREGADR, address);
00256
00257 <span class="comment">// write the PHY data</span>
00258 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MIWRL, data);
00259 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MIWRH, data>>8);
00260
00261 <span class="comment">// wait until the PHY write completes</span>
00262 <span class="keywordflow">while</span>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MISTAT) & MISTAT_BUSY);
00263 }
00264
<a name="l00265"></a><a class="code" href="group__enc28j60.html#ga9">00265</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga9">enc28j60Init</a>(<span class="keywordtype">void</span>)
00266 {
00267 <span class="comment">// initialize I/O</span>
00268 sbi(ENC28J60_CONTROL_DDR, ENC28J60_CONTROL_CS);
00269 sbi(ENC28J60_CONTROL_PORT, ENC28J60_CONTROL_CS);
00270
00271 <span class="comment">// setup SPI I/O pins</span>
00272 sbi(ENC28J60_SPI_PORT, ENC28J60_SPI_SCK); <span class="comment">// set SCK hi</span>
00273 sbi(ENC28J60_SPI_DDR, ENC28J60_SPI_SCK); <span class="comment">// set SCK as output</span>
00274 cbi(ENC28J60_SPI_DDR, ENC28J60_SPI_MISO); <span class="comment">// set MISO as input</span>
00275 sbi(ENC28J60_SPI_DDR, ENC28J60_SPI_MOSI); <span class="comment">// set MOSI as output</span>
00276 sbi(ENC28J60_SPI_DDR, ENC28J60_SPI_SS); <span class="comment">// SS must be output for Master mode to work</span>
00277 <span class="comment">// initialize SPI interface</span>
00278 <span class="comment">// master mode</span>
00279 sbi(SPCR, MSTR);
00280 <span class="comment">// select clock phase positive-going in middle of data</span>
00281 cbi(SPCR, CPOL);
00282 <span class="comment">// Data order MSB first</span>
00283 cbi(SPCR,DORD);
00284 <span class="comment">// switch to f/4 2X = f/2 bitrate</span>
00285 cbi(SPCR, SPR0);
00286 cbi(SPCR, SPR1);
00287 sbi(SPSR, SPI2X);
00288 <span class="comment">// enable SPI</span>
00289 sbi(SPCR, SPE);
00290
00291 <span class="comment">// perform system reset</span>
00292 <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(ENC28J60_SOFT_RESET, 0, ENC28J60_SOFT_RESET);
00293 <span class="comment">// check CLKRDY bit to see if reset is complete</span>
00294 delay_us(50);
00295 <span class="keywordflow">while</span>(!(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ESTAT) & ESTAT_CLKRDY));
00296
00297 <span class="comment">// do bank 0 stuff</span>
00298 <span class="comment">// initialize receive buffer</span>
00299 <span class="comment">// 16-bit transfers, must write low byte first</span>
00300 <span class="comment">// set receive buffer start address</span>
00301 NextPacketPtr = RXSTART_INIT;
00302 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ERXSTL, RXSTART_INIT&0xFF);
00303 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ERXSTH, RXSTART_INIT>>8);
00304 <span class="comment">// set receive pointer address</span>
00305 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ERXRDPTL, RXSTART_INIT&0xFF);
00306 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ERXRDPTH, RXSTART_INIT>>8);
00307 <span class="comment">// set receive buffer end</span>
00308 <span class="comment">// ERXND defaults to 0x1FFF (end of ram)</span>
00309 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ERXNDL, RXSTOP_INIT&0xFF);
00310 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ERXNDH, RXSTOP_INIT>>8);
00311 <span class="comment">// set transmit buffer start</span>
00312 <span class="comment">// ETXST defaults to 0x0000 (beginnging of ram)</span>
00313 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ETXSTL, TXSTART_INIT&0xFF);
00314 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ETXSTH, TXSTART_INIT>>8);
00315
00316 <span class="comment">// do bank 2 stuff</span>
00317 <span class="comment">// enable MAC receive</span>
00318 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MACON1, MACON1_MARXEN|MACON1_TXPAUS|MACON1_RXPAUS);
00319 <span class="comment">// bring MAC out of reset</span>
00320 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MACON2, 0x00);
00321 <span class="comment">// enable automatic padding and CRC operations</span>
00322 <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(ENC28J60_BIT_FIELD_SET, MACON3, MACON3_PADCFG0|MACON3_TXCRCEN|MACON3_FRMLNEN);
00323 <span class="comment">// enc28j60Write(MACON3, MACON3_PADCFG0|MACON3_TXCRCEN|MACON3_FRMLNEN);</span>
00324 <span class="comment">// set inter-frame gap (non-back-to-back)</span>
00325 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAIPGL, 0x12);
00326 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAIPGH, 0x0C);
00327 <span class="comment">// set inter-frame gap (back-to-back)</span>
00328 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MABBIPG, 0x12);
00329 <span class="comment">// Set the maximum packet size which the controller will accept</span>
00330 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAMXFLL, MAX_FRAMELEN&0xFF);
00331 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAMXFLH, MAX_FRAMELEN>>8);
00332
00333 <span class="comment">// do bank 3 stuff</span>
00334 <span class="comment">// write MAC address</span>
00335 <span class="comment">// NOTE: MAC address in ENC28J60 is byte-backward</span>
00336 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR5, ENC28J60_MAC0);
00337 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR4, ENC28J60_MAC1);
00338 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR3, ENC28J60_MAC2);
00339 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR2, ENC28J60_MAC3);
00340 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR1, ENC28J60_MAC4);
00341 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(MAADR0, ENC28J60_MAC5);
00342
00343 <span class="comment">// no loopback of transmitted frames</span>
00344 <a class="code" href="group__enc28j60.html#ga8">enc28j60PhyWrite</a>(PHCON2, PHCON2_HDLDIS);
00345
00346 <span class="comment">// switch to bank 0</span>
00347 <a class="code" href="group__enc28j60.html#ga4">enc28j60SetBank</a>(ECON1);
00348 <span class="comment">// enable interrutps</span>
00349 <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(ENC28J60_BIT_FIELD_SET, EIE, EIE_INTIE|EIE_PKTIE);
00350 <span class="comment">// enable packet reception</span>
00351 <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);
00352 <span class="comment">/*</span>
00353 <span class="comment"> enc28j60PhyWrite(PHLCON, 0x0AA2);</span>
00354 <span class="comment"></span>
00355 <span class="comment"> // setup duplex ----------------------</span>
00356 <span class="comment"></span>
00357 <span class="comment"> // Disable receive logic and abort any packets currently being transmitted</span>
00358 <span class="comment"> enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ECON1, ECON1_TXRTS|ECON1_RXEN);</span>
00359 <span class="comment"> </span>
00360 <span class="comment"> {</span>
00361 <span class="comment"> u16 temp;</span>
00362 <span class="comment"> // Set the PHY to the proper duplex mode</span>
00363 <span class="comment"> temp = enc28j60PhyRead(PHCON1);</span>
00364 <span class="comment"> temp &= ~PHCON1_PDPXMD;</span>
00365 <span class="comment"> enc28j60PhyWrite(PHCON1, temp);</span>
00366 <span class="comment"> // Set the MAC to the proper duplex mode</span>
00367 <span class="comment"> temp = enc28j60Read(MACON3);</span>
00368 <span class="comment"> temp &= ~MACON3_FULDPX;</span>
00369 <span class="comment"> enc28j60Write(MACON3, temp);</span>
00370 <span class="comment"> }</span>
00371 <span class="comment"></span>
00372 <span class="comment"> // Set the back-to-back inter-packet gap time to IEEE specified </span>
00373 <span class="comment"> // requirements. The meaning of the MABBIPG value changes with the duplex</span>
00374 <span class="comment"> // state, so it must be updated in this function.</span>
00375 <span class="comment"> // In full duplex, 0x15 represents 9.6us; 0x12 is 9.6us in half duplex</span>
00376 <span class="comment"> //enc28j60Write(MABBIPG, DuplexState ? 0x15 : 0x12); </span>
00377 <span class="comment"> </span>
00378 <span class="comment"> // Reenable receive logic</span>
00379 <span class="comment"> enc28j60WriteOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);</span>
00380 <span class="comment"></span>
00381 <span class="comment"> // setup duplex ----------------------</span>
00382 <span class="comment">*/</span>
00383 }
00384
<a name="l00385"></a><a class="code" href="group__enc28j60.html#ga10">00385</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga10">enc28j60PacketSend</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* packet)
00386 {
00387 <span class="comment">// Set the write pointer to start of transmit buffer area</span>
00388 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(EWRPTL, TXSTART_INIT);
00389 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(EWRPTH, TXSTART_INIT>>8);
00390 <span class="comment">// Set the TXND pointer to correspond to the packet size given</span>
00391 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ETXNDL, (TXSTART_INIT+len));
00392 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ETXNDH, (TXSTART_INIT+len)>>8);
00393
00394 <span class="comment">// write per-packet control byte</span>
00395 <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(ENC28J60_WRITE_BUF_MEM, 0, 0x00);
00396
00397 <span class="comment">// copy the packet into the transmit buffer</span>
00398 <a class="code" href="group__enc28j60.html#ga3">enc28j60WriteBuffer</a>(len, packet);
00399
00400 <span class="comment">// send the contents of the transmit buffer onto the network</span>
00401 <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_TXRTS);
00402 }
00403
<a name="l00404"></a><a class="code" href="group__enc28j60.html#ga11">00404</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="group__enc28j60.html#ga11">enc28j60PacketReceive</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> maxlen, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* packet)
00405 {
00406 u16 rxstat;
00407 u16 len;
00408
00409 <span class="comment">// check if a packet has been received and buffered</span>
00410 <span class="comment">// if( !(enc28j60Read(EIR) & EIR_PKTIF) )</span>
00411 <span class="keywordflow">if</span>( !<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(EPKTCNT) )
00412 <span class="keywordflow">return</span> 0;
00413
00414 <span class="comment">// Make absolutely certain that any previous packet was discarded </span>
00415 <span class="comment">//if( WasDiscarded == FALSE)</span>
00416 <span class="comment">// MACDiscardRx();</span>
00417
00418 <span class="comment">// Set the read pointer to the start of the received packet</span>
00419 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ERDPTL, (NextPacketPtr));
00420 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ERDPTH, (NextPacketPtr)>>8);
00421 <span class="comment">// read the next packet pointer</span>
00422 NextPacketPtr = <a class="code" href="group__enc28j60.html#ga0">enc28j60ReadOp</a>(ENC28J60_READ_BUF_MEM, 0);
00423 NextPacketPtr |= <a class="code" href="group__enc28j60.html#ga0">enc28j60ReadOp</a>(ENC28J60_READ_BUF_MEM, 0)<<8;
00424 <span class="comment">// read the packet length</span>
00425 len = <a class="code" href="group__enc28j60.html#ga0">enc28j60ReadOp</a>(ENC28J60_READ_BUF_MEM, 0);
00426 len |= <a class="code" href="group__enc28j60.html#ga0">enc28j60ReadOp</a>(ENC28J60_READ_BUF_MEM, 0)<<8;
00427 <span class="comment">// read the receive status</span>
00428 rxstat = <a class="code" href="group__enc28j60.html#ga0">enc28j60ReadOp</a>(ENC28J60_READ_BUF_MEM, 0);
00429 rxstat |= <a class="code" href="group__enc28j60.html#ga0">enc28j60ReadOp</a>(ENC28J60_READ_BUF_MEM, 0)<<8;
00430
00431 <span class="comment">// limit retrieve length</span>
00432 <span class="comment">// (we reduce the MAC-reported length by 4 to remove the CRC)</span>
00433 len = MIN(len, maxlen);
00434
00435 <span class="comment">// copy the packet from the receive buffer</span>
00436 <a class="code" href="group__enc28j60.html#ga2">enc28j60ReadBuffer</a>(len, packet);
00437
00438 <span class="comment">// Move the RX read pointer to the start of the next received packet</span>
00439 <span class="comment">// This frees the memory we just read out</span>
00440 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ERXRDPTL, (NextPacketPtr));
00441 <a class="code" href="group__enc28j60.html#ga6">enc28j60Write</a>(ERXRDPTH, (NextPacketPtr)>>8);
00442
00443 <span class="comment">// decrement the packet counter indicate we are done with this packet</span>
00444 <a class="code" href="group__enc28j60.html#ga1">enc28j60WriteOp</a>(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC);
00445
00446 <span class="keywordflow">return</span> len;
00447 }
00448
<a name="l00449"></a><a class="code" href="group__enc28j60.html#ga12">00449</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga12">enc28j60ReceiveOverflowRecover</a>(<span class="keywordtype">void</span>)
00450 {
00451 <span class="comment">// receive buffer overflow handling procedure</span>
00452
00453 <span class="comment">// recovery completed</span>
00454 }
00455
<a name="l00456"></a><a class="code" href="group__enc28j60.html#ga13">00456</a> <span class="keywordtype">void</span> <a class="code" href="group__enc28j60.html#ga13">enc28j60RegDump</a>(<span class="keywordtype">void</span>)
00457 {
00458 <span class="comment">// unsigned char macaddr[6];</span>
00459 <span class="comment">// result = ax88796Read(TR);</span>
00460
00461 <span class="comment">// rprintf("Media State: ");</span>
00462 <span class="comment">// if(!(result & AUTOD))</span>
00463 <span class="comment">// rprintf("Autonegotiation\r\n");</span>
00464 <span class="comment">// else if(result & RST_B)</span>
00465 <span class="comment">// rprintf("PHY in Reset \r\n");</span>
00466 <span class="comment">// else if(!(result & RST_10B))</span>
00467 <span class="comment">// rprintf("10BASE-T \r\n");</span>
00468 <span class="comment">// else if(!(result & RST_TXB))</span>
00469 <span class="comment">// rprintf("100BASE-T \r\n");</span>
00470
00471 rprintf(<span class="stringliteral">"RevID: 0x%x\r\n"</span>, <a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(EREVID));
00472
00473 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Cntrl: ECON1 ECON2 ESTAT EIR EIE\r\n"</span>);
00474 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);
00475 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ECON1));
00476 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);
00477 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ECON2));
00478 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);
00479 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ESTAT));
00480 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);
00481 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(EIR));
00482 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);
00483 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(EIE));
00484 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00485
00486 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"MAC : MACON1 MACON2 MACON3 MACON4 MAC-Address\r\n"</span>);
00487 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00488 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MACON1));
00489 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00490 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MACON2));
00491 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00492 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MACON3));
00493 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00494 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MACON4));
00495 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);
00496 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR5));
00497 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR4));
00498 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR3));
00499 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR2));
00500 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR1));
00501 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAADR0));
00502 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00503
00504 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Rx : ERXST ERXND ERXWRPT ERXRDPT ERXFCON EPKTCNT MAMXFL\r\n"</span>);
00505 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00506 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ERXSTH));
00507 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ERXSTL));
00508 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00509 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ERXNDH));
00510 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ERXNDL));
00511 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00512 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ERXWRPTH));
00513 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ERXWRPTL));
00514 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00515 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ERXRDPTH));
00516 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ERXRDPTL));
00517 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00518 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ERXFCON));
00519 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00520 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(EPKTCNT));
00521 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00522 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAMXFLH));
00523 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAMXFLL));
00524 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00525
00526 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Tx : ETXST ETXND MACLCON1 MACLCON2 MAPHSUP\r\n"</span>);
00527 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00528 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ETXSTH));
00529 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ETXSTL));
00530 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00531 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ETXNDH));
00532 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(ETXNDL));
00533 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00534 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MACLCON1));
00535 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00536 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MACLCON2));
00537 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" 0x"</span>);
00538 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(<a class="code" href="group__enc28j60.html#ga5">enc28j60Read</a>(MAPHSUP));
00539 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00540
00541 delay_ms(25);
00542 }
00543
00544
00545
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
</body>
</html>
|