Added RAW sockets.

Fork of WIZnetInterface by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers W7500x_toe.h Source File

W7500x_toe.h

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  *
00018  */
00019 #pragma once
00020 
00021 #include "mbed.h"
00022 #include "mbed_debug.h"
00023 
00024 #define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
00025 
00026 #define DEFAULT_WAIT_RESP_TIMEOUT 500
00027 
00028 
00029 #define MAX_SOCK_NUM 8
00030 
00031 // Peripheral base address 
00032 #define W7500x_WZTOE_BASE   (0x46000000)
00033 #define W7500x_TXMEM_BASE   (W7500x_WZTOE_BASE + 0x00020000)
00034 #define W7500x_RXMEM_BASE   (W7500x_WZTOE_BASE + 0x00030000)
00035 // Common register
00036 #define MR                  (0x2300)
00037 #define GAR                 (0x6008)
00038 #define SUBR                (0x600C)
00039 #define SHAR                (0x6000)
00040 #define SIPR                (0x6010)
00041 
00042 // Added Common register @W7500
00043 #define TIC100US            (0x2000) 
00044 
00045 // Socket register
00046 #define Sn_MR               (0x0000)
00047 #define Sn_CR               (0x0010)
00048 #define Sn_IR               (0x0020) //--Sn_ISR
00049 #define Sn_SR               (0x0030)
00050 #define Sn_PORT             (0x0114)
00051 // Geminate Changes
00052 #define Sn_DHAR0            (0x0118)
00053 #define Sn_DHAR1            (0x011C)
00054 #define Sn_RCR              (0x6044)
00055 #define Sn_RTR              (0x6040)
00056 // end
00057 #define Sn_DIPR             (0x0124)
00058 #define Sn_DPORT            (0x0120)
00059 #define Sn_RXBUF_SIZE       (0x0200)
00060 #define Sn_TXBUF_SIZE       (0x0220)
00061 #define Sn_TX_FSR           (0x0204)
00062 #define Sn_TX_WR            (0x020C)
00063 #define Sn_RX_RSR           (0x0224)
00064 #define Sn_RX_RD            (0x0228)
00065 // added Socket register @W7500
00066 #define Sn_ICR              (0x0028)
00067 enum PHYMode {
00068     AutoNegotiate = 0,
00069     HalfDuplex10  = 1,
00070     FullDuplex10  = 2,
00071     HalfDuplex100 = 3,
00072     FullDuplex100 = 4,
00073 };
00074 
00075 //bool plink(int wait_time_ms= 3*1000);
00076 
00077 class WIZnet_Chip {
00078 public:
00079 enum Protocol {
00080     CLOSED = 0,
00081     TCP    = 1,
00082     UDP    = 2,
00083     RAW    = 4
00084 };
00085 
00086 enum Command {
00087     OPEN      = 0x01,
00088     LISTEN    = 0x02,
00089     CONNECT   = 0x04,
00090     DISCON    = 0x08,
00091     CLOSE     = 0x10,
00092     SEND      = 0x20,
00093     SEND_MAC  = 0x21, 
00094     SEND_KEEP = 0x22,
00095     RECV      = 0x40,
00096     
00097 };
00098 
00099 enum Interrupt {
00100     INT_CON     = 0x01,
00101     INT_DISCON  = 0x02,
00102     INT_RECV    = 0x04,
00103     INT_TIMEOUT = 0x08,
00104     INT_SEND_OK = 0x10,
00105 };
00106 enum Status {
00107     SOCK_CLOSED      = 0x00,
00108     SOCK_INIT        = 0x13,
00109     SOCK_LISTEN      = 0x14,
00110     SOCK_SYNSENT     = 0x15,
00111     SOCK_ESTABLISHED = 0x17,
00112     SOCK_CLOSE_WAIT  = 0x1c,
00113     SOCK_UDP         = 0x22,
00114 };
00115 enum Mode {
00116     MR_RST           = 0x80,   
00117     MR_WOL           = 0x20,   
00118     MR_PB            = 0x10,   
00119     MR_FARP          = 0x02,   
00120 };
00121 
00122     WIZnet_Chip();
00123     
00124     // Geminate Changes.
00125     int get_DHAR0(int socket);
00126     int get_DHAR1(int socket);
00127     // End Changes.
00128 
00129     /*
00130     * Set MAC Address to W7500x_TOE
00131     *
00132     * @return true if connected, false otherwise
00133     */ 
00134     bool setmac();
00135 
00136     /*
00137     * Connect the W7500 WZTOE to the ssid contained in the constructor.
00138     *
00139     * @return true if connected, false otherwise
00140     */ 
00141     bool setip();
00142 
00143 
00144     /*
00145     * Open a tcp connection with the specified host on the specified port
00146     *
00147     * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
00148     * @param port port
00149     * @ returns true if successful
00150     */
00151     bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
00152 
00153     /*
00154     * Set the protocol (UDP or TCP)
00155     *
00156     * @param p protocol
00157     * @ returns true if successful
00158     */
00159     bool setProtocol(int socket, Protocol p);
00160 
00161     /*
00162     * Reset the W7500 WZTOE
00163     */
00164     void reset();
00165    
00166     int wait_readable(int socket, int wait_time_ms, int req_size = 0);
00167 
00168     int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
00169 
00170     /*
00171     * Check if an ethernet link is pressent or not.
00172     *
00173     * @returns true if successful
00174     */
00175     bool link(int wait_time_ms= 3*1000);
00176 
00177     /*
00178     * Sets the speed and duplex parameters of an ethernet link.
00179     *
00180     * @returns true if successful
00181     */
00182     void set_link(PHYMode phymode);
00183 
00184     /*
00185     * Check if a tcp link is active
00186     *
00187     * @returns true if successful
00188     */
00189     bool is_connected(int socket);
00190 
00191     /*
00192     * Close a tcp connection
00193     *
00194     * @ returns true if successful
00195     */
00196     bool close(int socket);
00197 
00198     /*
00199     * @param str string to be sent
00200     * @param len string length
00201     */
00202     int send(int socket, const char * str, int len);
00203     int sendRaw(int socket, const char * str, int len);
00204 
00205     int recv(int socket, char* buf, int len);
00206 
00207     /*
00208     * Return true if the module is using dhcp
00209     *
00210     * @returns true if the module is using dhcp
00211     */
00212     bool isDHCP() {
00213         return dhcp;
00214     }
00215 
00216     bool gethostbyname(const char* host, uint32_t* ip);
00217 
00218     static WIZnet_Chip * getInstance() {
00219         return inst;
00220     };
00221 
00222     int new_socket();
00223     uint16_t new_port();
00224 
00225     void scmd(int socket, Command cmd);
00226 
00227     template<typename T>
00228     void sreg(int socket, uint16_t addr, T data) {
00229         reg_wr<T>(addr, (uint8_t)(0x01+(socket<<2)), data);
00230     }
00231 
00232     template<typename T>
00233     T sreg(int socket, uint16_t addr) {
00234         return reg_rd<T>(addr, (uint8_t)(0x01+(socket<<2)));
00235     }
00236 
00237     template<typename T>
00238     void reg_wr(uint16_t addr, T data) {
00239         return reg_wr(addr, 0x00, data);
00240     }
00241     
00242     template<typename T>
00243     void reg_wr(uint16_t addr, uint8_t cb, T data) {
00244         uint8_t buf[sizeof(T)];
00245         *reinterpret_cast<T*>(buf) = data;
00246         /*
00247         for(int i = 0; i < sizeof(buf)/2; i++) { //  Little Endian to Big Endian
00248             uint8_t t = buf[i];
00249             buf[i] = buf[sizeof(buf)-1-i];
00250             buf[sizeof(buf)-1-i] = t;
00251         }
00252         */
00253         for(int i = 0; i < sizeof(buf); i++) { //  Little Endian to Big Endian
00254             *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)((cb<<16)+addr)+i) = buf[i];
00255         }
00256     }
00257 
00258     template<typename T>
00259     T reg_rd(uint16_t addr) {
00260         return reg_rd<T>(addr, (uint8_t)(0x00));
00261     }
00262 
00263     template<typename T>
00264     T reg_rd(uint16_t addr, uint8_t cb) {
00265         uint8_t buf[sizeof(T)] = {0,};
00266         for(int i = 0; i < sizeof(buf); i++) { //  Little Endian to Big Endian
00267             buf[i] = *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)((cb<<16)+addr)+i);
00268         }
00269         /*
00270         for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
00271             uint8_t t = buf[i];
00272             buf[i] = buf[sizeof(buf)-1-i];
00273             buf[sizeof(buf)-1-i] = t;
00274         }
00275         */
00276         return *reinterpret_cast<T*>(buf);
00277     }
00278 
00279     void reg_rd_mac(uint16_t addr, uint8_t* data);
00280     
00281     void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip);
00282 
00283     void sreg_ip(int socket, uint16_t addr, const char* ip);
00284 
00285     int ethernet_link(void);
00286     
00287     void ethernet_set_link(int speed, int duplex);
00288 
00289 
00290 protected:
00291     uint8_t mac[6];
00292     uint32_t ip;
00293     uint32_t netmask;
00294     uint32_t gateway;
00295     uint32_t dnsaddr;
00296     bool dhcp;
00297 
00298     static WIZnet_Chip* inst;
00299 
00300     void reg_wr_mac(uint16_t addr, uint8_t* data) {
00301         *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+3)) = data[0] ;
00302         *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+2)) = data[1] ;
00303         *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+1)) = data[2] ;
00304         *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+0)) = data[3] ;
00305         *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+7)) = data[4] ;
00306         *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+6)) = data[5] ;
00307     }
00308 };
00309 
00310 extern uint32_t str_to_ip(const char* str);
00311 extern void printfBytes(char* str, uint8_t* buf, int len);
00312 extern void printHex(uint8_t* buf, int len);
00313 extern void debug_hex(uint8_t* buf, int len);