W5200(WIZ820io) network interface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers w5100.h Source File

w5100.h

00001 /*
00002  * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
00003  *
00004  * This file is free software; you can redistribute it and/or modify
00005  * it under the terms of either the GNU General Public License version 2
00006  * or the GNU Lesser General Public License version 2.1, both as
00007  * published by the Free Software Foundation.
00008  */
00009 
00010 #ifndef    W5100_H_INCLUDED
00011 #define    W5100_H_INCLUDED
00012 #define MBED
00013 #ifdef MBED
00014 #include "mbed.h"
00015 typedef unsigned char uint8_t;
00016 typedef unsigned short uint16_t;
00017 #else //MBED
00018 #include <avr/pgmspace.h>
00019 #include <SPI.h>
00020 #endif //MBED
00021 #define W5200
00022 
00023 #ifdef W5200
00024 #define MAX_SOCK_NUM 8
00025 #else
00026 #define MAX_SOCK_NUM 4
00027 #endif
00028 
00029 
00030 typedef uint8_t SOCKET;
00031 
00032 #ifndef W5200
00033 #define IDM_OR  0x8000
00034 #define IDM_AR0 0x8001
00035 #define IDM_AR1 0x8002
00036 #define IDM_DR  0x8003
00037 #endif
00038 
00039 /*
00040 class MR {
00041 public:
00042   static const uint8_t RST   = 0x80;
00043   static const uint8_t PB    = 0x10;
00044   static const uint8_t PPPOE = 0x08;
00045   static const uint8_t LB    = 0x04;
00046   static const uint8_t AI    = 0x02;
00047   static const uint8_t IND   = 0x01;
00048 };
00049 */
00050 /*
00051 class IR {
00052 public:
00053   static const uint8_t CONFLICT = 0x80;
00054   static const uint8_t UNREACH  = 0x40;
00055   static const uint8_t PPPoE    = 0x20;
00056   static const uint8_t SOCK0    = 0x01;
00057   static const uint8_t SOCK1    = 0x02;
00058   static const uint8_t SOCK2    = 0x04;
00059   static const uint8_t SOCK3    = 0x08;
00060   static inline uint8_t SOCK(SOCKET ch) { return (0x01 << ch); };
00061 };
00062 */
00063 
00064 class SnMR {
00065 public:
00066   static const uint8_t CLOSE  = 0x00;
00067   static const uint8_t TCP    = 0x01;
00068   static const uint8_t UDP    = 0x02;
00069   static const uint8_t IPRAW  = 0x03;
00070   static const uint8_t MACRAW = 0x04;
00071   static const uint8_t PPPOE  = 0x05;
00072   static const uint8_t ND     = 0x20;
00073   static const uint8_t MULTI  = 0x80;
00074 };
00075 
00076 enum SockCMD {
00077   Sock_OPEN      = 0x01,
00078   Sock_LISTEN    = 0x02,
00079   Sock_CONNECT   = 0x04,
00080   Sock_DISCON    = 0x08,
00081   Sock_CLOSE     = 0x10,
00082   Sock_SEND      = 0x20,
00083   Sock_SEND_MAC  = 0x21,
00084   Sock_SEND_KEEP = 0x22,
00085   Sock_RECV      = 0x40
00086 };
00087 
00088 /*class SnCmd {
00089 public:
00090   static const uint8_t OPEN      = 0x01;
00091   static const uint8_t LISTEN    = 0x02;
00092   static const uint8_t CONNECT   = 0x04;
00093   static const uint8_t DISCON    = 0x08;
00094   static const uint8_t CLOSE     = 0x10;
00095   static const uint8_t SEND      = 0x20;
00096   static const uint8_t SEND_MAC  = 0x21;
00097   static const uint8_t SEND_KEEP = 0x22;
00098   static const uint8_t RECV      = 0x40;
00099 };
00100 */
00101 
00102 class SnIR {
00103 public:
00104   static const uint8_t SEND_OK = 0x10;
00105   static const uint8_t TIMEOUT = 0x08;
00106   static const uint8_t RECV    = 0x04;
00107   static const uint8_t DISCON  = 0x02;
00108   static const uint8_t CON     = 0x01;
00109 };
00110 
00111 class SnSR {
00112 public:
00113   static const uint8_t CLOSED      = 0x00;
00114   static const uint8_t INIT        = 0x13;
00115   static const uint8_t LISTEN      = 0x14;
00116   static const uint8_t SYNSENT     = 0x15;
00117   static const uint8_t SYNRECV     = 0x16;
00118   static const uint8_t ESTABLISHED = 0x17;
00119   static const uint8_t FIN_WAIT    = 0x18;
00120   static const uint8_t CLOSING     = 0x1A;
00121   static const uint8_t TIME_WAIT   = 0x1B;
00122   static const uint8_t CLOSE_WAIT  = 0x1C;
00123   static const uint8_t LAST_ACK    = 0x1D;
00124   static const uint8_t UDP         = 0x22;
00125   static const uint8_t IPRAW       = 0x32;
00126   static const uint8_t MACRAW      = 0x42;
00127   static const uint8_t PPPOE       = 0x5F;
00128 };
00129 
00130 class IPPROTO {
00131 public:
00132   static const uint8_t IP   = 0;
00133   static const uint8_t ICMP = 1;
00134   static const uint8_t IGMP = 2;
00135   static const uint8_t GGP  = 3;
00136   static const uint8_t TCP  = 6;
00137   static const uint8_t PUP  = 12;
00138   static const uint8_t UDP  = 17;
00139   static const uint8_t IDP  = 22;
00140   static const uint8_t ND   = 77;
00141   static const uint8_t RAW  = 255;
00142 };
00143 
00144 class W5100Class {
00145 public:
00146   void init();
00147 
00148   /**
00149    * @brief    This function is being used for copy the data form Receive buffer of the chip to application buffer.
00150    * 
00151    * It calculate the actual physical address where one has to read
00152    * the data from Receive buffer. Here also take care of the condition while it exceed
00153    * the Rx memory uper-bound of socket.
00154    */
00155   void read_data(SOCKET s, volatile uint8_t * src, volatile uint8_t * dst, uint16_t len);
00156   
00157   /**
00158    * @brief     This function is being called by send() and sendto() function also. 
00159    * 
00160    * This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer
00161    * register. User should read upper byte first and lower byte later to get proper value.
00162    */
00163   void send_data_processing(SOCKET s, const uint8_t *data, uint16_t len);
00164   /**
00165    * @brief A copy of send_data_processing that uses the provided ptr for the
00166    *        write offset.  Only needed for the "streaming" UDP API, where
00167    *        a single UDP packet is built up over a number of calls to
00168    *        send_data_processing_ptr, because TX_WR doesn't seem to get updated
00169    *        correctly in those scenarios
00170    * @param ptr value to use in place of TX_WR.  If 0, then the value is read
00171    *        in from TX_WR
00172    * @return New value for ptr, to be used in the next call
00173    */
00174 // FIXME Update documentation
00175   void send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len);
00176 
00177   /**
00178    * @brief    This function is being called by recv() also.
00179    * 
00180    * This function read the Rx read pointer register
00181    * and after copy the data from receive buffer update the Rx write pointer register.
00182    * User should read upper byte first and lower byte later to get proper value.
00183    */
00184   void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0);
00185 
00186   inline void setGatewayIp(uint8_t *_addr);
00187   inline void getGatewayIp(uint8_t *_addr);
00188 
00189   inline void setSubnetMask(uint8_t *_addr);
00190   inline void getSubnetMask(uint8_t *_addr);
00191 
00192   inline void setMACAddress(uint8_t * addr);
00193   inline void getMACAddress(uint8_t * addr);
00194 
00195   inline void setIPAddress(uint8_t * addr);
00196   inline void getIPAddress(uint8_t * addr);
00197 
00198   inline void setRetransmissionTime(uint16_t timeout);
00199   inline void setRetransmissionCount(uint8_t _retry);
00200 
00201   void execCmdSn(SOCKET s, SockCMD _cmd);
00202   
00203   uint16_t getTXFreeSize(SOCKET s);
00204   uint16_t getRXReceivedSize(SOCKET s);
00205   
00206 
00207   // W5100 Registers
00208   // ---------------
00209 private:
00210   static uint8_t write(uint16_t _addr, uint8_t _data);
00211   static uint16_t write(uint16_t addr, const uint8_t *buf, uint16_t len);
00212   static uint8_t read(uint16_t addr);
00213   static uint16_t read(uint16_t addr, uint8_t *buf, uint16_t len);
00214   
00215 #define __GP_REGISTER8(name, address)             \
00216   static inline void write##name(uint8_t _data) { \
00217     write(address, _data);                        \
00218   }                                               \
00219   static inline uint8_t read##name() {            \
00220     return read(address);                         \
00221   }
00222 #define __GP_REGISTER16(name, address)            \
00223   static void write##name(uint16_t _data) {       \
00224     write(address,   _data >> 8);                 \
00225     write(address+1, _data & 0xFF);               \
00226   }                                               \
00227   static uint16_t read##name() {                  \
00228     uint16_t res = read(address);                 \
00229     res = (res << 8) + read(address + 1);         \
00230     return res;                                   \
00231   }
00232 #define __GP_REGISTER_N(name, address, size)      \
00233   static uint16_t write##name(uint8_t *_buff) {   \
00234     return write(address, _buff, size);           \
00235   }                                               \
00236   static uint16_t read##name(uint8_t *_buff) {    \
00237     return read(address, _buff, size);            \
00238   }
00239 
00240 public:
00241   __GP_REGISTER8 (MR,     0x0000);    // Mode
00242   __GP_REGISTER_N(GAR,    0x0001, 4); // Gateway IP address
00243   __GP_REGISTER_N(SUBR,   0x0005, 4); // Subnet mask address
00244   __GP_REGISTER_N(SHAR,   0x0009, 6); // Source MAC address
00245   __GP_REGISTER_N(SIPR,   0x000F, 4); // Source IP address
00246   __GP_REGISTER8 (IR,     0x0015);    // Interrupt
00247   __GP_REGISTER8 (IMR,    0x0016);    // Interrupt Mask
00248   __GP_REGISTER16(RTR,    0x0017);    // Timeout address
00249   __GP_REGISTER8 (RCR,    0x0019);    // Retry count
00250   
00251   #ifndef W5200
00252   __GP_REGISTER8 (RMSR,   0x001A);    // Receive memory size
00253   __GP_REGISTER8 (TMSR,   0x001B);    // Transmit memory size
00254   #endif
00255   __GP_REGISTER8 (PATR,   0x001C);    // Authentication type address in PPPoE mode
00256   
00257   __GP_REGISTER8 (VERSIONR,0x001f);   // Chip version
00258   
00259   __GP_REGISTER8 (PTIMER, 0x0028);    // PPP LCP Request Timer
00260   __GP_REGISTER8 (PMAGIC, 0x0029);    // PPP LCP Magic Number
00261   #ifndef W5200
00262   __GP_REGISTER_N(UIPR,   0x002A, 4); // Unreachable IP address in UDP mode
00263   __GP_REGISTER16(UPORT,  0x002E);    // Unreachable Port address in UDP mode
00264 #endif
00265   __GP_REGISTER8 (PHYSTATUS,0x0035);  // PHY Status
00266   
00267 #undef __GP_REGISTER8
00268 #undef __GP_REGISTER16
00269 #undef __GP_REGISTER_N
00270 
00271   // W5100 Socket registers
00272   // ----------------------
00273 private:
00274   static inline uint8_t readSn(SOCKET _s, uint16_t _addr);
00275   static inline uint8_t writeSn(SOCKET _s, uint16_t _addr, uint8_t _data);
00276   static inline uint16_t readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len);
00277   static inline uint16_t writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len);
00278 
00279 #ifdef W5200
00280   static const uint16_t CH_BASE = 0x4000;
00281 #else
00282   static const uint16_t CH_BASE = 0x0400;
00283 #endif  
00284 
00285   static const uint16_t CH_SIZE = 0x0100;
00286 
00287 #define __SOCKET_REGISTER8(name, address)                    \
00288   static inline void write##name(SOCKET _s, uint8_t _data) { \
00289     writeSn(_s, address, _data);                             \
00290   }                                                          \
00291   static inline uint8_t read##name(SOCKET _s) {              \
00292     return readSn(_s, address);                              \
00293   }
00294 #define __SOCKET_REGISTER16(name, address)                   \
00295   static void write##name(SOCKET _s, uint16_t _data) {       \
00296     writeSn(_s, address,   _data >> 8);                      \
00297     writeSn(_s, address+1, _data & 0xFF);                    \
00298   }                                                          \
00299   static uint16_t read##name(SOCKET _s) {                    \
00300     uint16_t res = readSn(_s, address);                      \
00301     res = (res << 8) + readSn(_s, address + 1);              \
00302     return res;                                              \
00303   }
00304 #define __SOCKET_REGISTER_N(name, address, size)             \
00305   static uint16_t write##name(SOCKET _s, uint8_t *_buff) {   \
00306     return writeSn(_s, address, _buff, size);                \
00307   }                                                          \
00308   static uint16_t read##name(SOCKET _s, uint8_t *_buff) {    \
00309     return readSn(_s, address, _buff, size);                 \
00310   }
00311   
00312 public:
00313   __SOCKET_REGISTER8(SnMR,        0x0000)        // Mode
00314   __SOCKET_REGISTER8(SnCR,        0x0001)        // Command
00315   __SOCKET_REGISTER8(SnIR,        0x0002)        // Interrupt
00316   __SOCKET_REGISTER8(SnSR,        0x0003)        // Status
00317   __SOCKET_REGISTER16(SnPORT,     0x0004)        // Source Port
00318   __SOCKET_REGISTER_N(SnDHAR,     0x0006, 6)     // Destination Hardw Addr
00319   __SOCKET_REGISTER_N(SnDIPR,     0x000C, 4)     // Destination IP Addr
00320   __SOCKET_REGISTER16(SnDPORT,    0x0010)        // Destination Port
00321   __SOCKET_REGISTER16(SnMSSR,     0x0012)        // Max Segment Size
00322   __SOCKET_REGISTER8(SnPROTO,     0x0014)        // Protocol in IP RAW Mode
00323   __SOCKET_REGISTER8(SnTOS,       0x0015)        // IP TOS
00324   __SOCKET_REGISTER8(SnTTL,       0x0016)        // IP TTL
00325   __SOCKET_REGISTER16(SnTX_FSR,   0x0020)        // TX Free Size
00326   __SOCKET_REGISTER16(SnTX_RD,    0x0022)        // TX Read Pointer
00327   __SOCKET_REGISTER16(SnTX_WR,    0x0024)        // TX Write Pointer
00328   __SOCKET_REGISTER16(SnRX_RSR,   0x0026)        // RX Free Size
00329   __SOCKET_REGISTER16(SnRX_RD,    0x0028)        // RX Read Pointer
00330   __SOCKET_REGISTER16(SnRX_WR,    0x002A)        // RX Write Pointer (supported?)
00331   
00332 #undef __SOCKET_REGISTER8
00333 #undef __SOCKET_REGISTER16
00334 #undef __SOCKET_REGISTER_N
00335 
00336 
00337 private:
00338   static const uint8_t  RST = 7; // Reset BIT
00339 
00340 #ifdef W5200
00341   static const int SOCKETS = 8;
00342 #else
00343   static const int SOCKETS = 4;
00344 #endif
00345 
00346   static const uint16_t SMASK = 0x07FF; // Tx buffer MASK
00347   static const uint16_t RMASK = 0x07FF; // Rx buffer MASK
00348 public:
00349   static const uint16_t SSIZE = 2048; // Max Tx buffer size
00350 private:
00351   static const uint16_t RSIZE = 2048; // Max Rx buffer size
00352   uint16_t SBASE[SOCKETS]; // Tx buffer base address
00353   uint16_t RBASE[SOCKETS]; // Rx buffer base address
00354 
00355 private:
00356 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
00357   inline static void initSS()    { DDRB  |=  _BV(4); };
00358   inline static void setSS()     { PORTB &= ~_BV(4); };
00359   inline static void resetSS()   { PORTB |=  _BV(4); };
00360 #elif defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
00361   inline static void initSS()    { DDRB  |=  _BV(0); };
00362   inline static void setSS()     { PORTB &= ~_BV(0); };
00363   inline static void resetSS()   { PORTB |=  _BV(0); }; 
00364 #else
00365 #ifndef MBED
00366   inline static void initSS()    { DDRB  |=  _BV(2); };
00367   inline static void setSS()     { PORTB &= ~_BV(2); };
00368   inline static void resetSS()   { PORTB |=  _BV(2); };
00369 #endif //MBED
00370 #endif
00371 
00372 };
00373 
00374 extern W5100Class W5100;
00375 
00376 uint8_t W5100Class::readSn(SOCKET _s, uint16_t _addr) {
00377   return read(CH_BASE + _s * CH_SIZE + _addr);
00378 }
00379 
00380 uint8_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t _data) {
00381   return write(CH_BASE + _s * CH_SIZE + _addr, _data);
00382 }
00383 
00384 uint16_t W5100Class::readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) {
00385   return read(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
00386 }
00387 
00388 uint16_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) {
00389   return write(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
00390 }
00391 
00392 void W5100Class::getGatewayIp(uint8_t *_addr) {
00393   readGAR(_addr);
00394 }
00395 
00396 void W5100Class::setGatewayIp(uint8_t *_addr) {
00397   writeGAR(_addr);
00398 }
00399 
00400 void W5100Class::getSubnetMask(uint8_t *_addr) {
00401   readSUBR(_addr);
00402 }
00403 
00404 void W5100Class::setSubnetMask(uint8_t *_addr) {
00405   writeSUBR(_addr);
00406 }
00407 
00408 void W5100Class::getMACAddress(uint8_t *_addr) {
00409   readSHAR(_addr);
00410 }
00411 
00412 void W5100Class::setMACAddress(uint8_t *_addr) {
00413   writeSHAR(_addr);
00414 }
00415 
00416 void W5100Class::getIPAddress(uint8_t *_addr) {
00417   readSIPR(_addr);
00418 }
00419 
00420 void W5100Class::setIPAddress(uint8_t *_addr) {
00421   writeSIPR(_addr);
00422 }
00423 
00424 void W5100Class::setRetransmissionTime(uint16_t _timeout) {
00425   writeRTR(_timeout);
00426 }
00427 
00428 void W5100Class::setRetransmissionCount(uint8_t _retry) {
00429   writeRCR(_retry);
00430 }
00431 
00432 #endif