Servidor Web embebido en FRDM-KL46Z con Wiznet
Dependencies: mbed
Dependents: Embedded_web_wiznet Embedded_web_wiznet
Fork of WIZnet_Library by
WIZnetInterface/WIZnet/W5100.h@1:8138a268fbd2, 2014-05-09 (annotated)
- Committer:
- jbkim
- Date:
- Fri May 09 01:17:51 2014 +0000
- Revision:
- 1:8138a268fbd2
- Parent:
- 0:b72d22e10709
Wiznet Ethernet library 1st release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbkim | 0:b72d22e10709 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
jbkim | 0:b72d22e10709 | 2 | * |
jbkim | 0:b72d22e10709 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
jbkim | 0:b72d22e10709 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
jbkim | 0:b72d22e10709 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
jbkim | 0:b72d22e10709 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
jbkim | 0:b72d22e10709 | 7 | * furnished to do so, subject to the following conditions: |
jbkim | 0:b72d22e10709 | 8 | * |
jbkim | 0:b72d22e10709 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
jbkim | 0:b72d22e10709 | 10 | * substantial portions of the Software. |
jbkim | 0:b72d22e10709 | 11 | * |
jbkim | 0:b72d22e10709 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
jbkim | 0:b72d22e10709 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
jbkim | 0:b72d22e10709 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
jbkim | 0:b72d22e10709 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jbkim | 0:b72d22e10709 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jbkim | 0:b72d22e10709 | 17 | * |
jbkim | 0:b72d22e10709 | 18 | */ |
jbkim | 0:b72d22e10709 | 19 | |
jbkim | 0:b72d22e10709 | 20 | #pragma once |
jbkim | 0:b72d22e10709 | 21 | |
jbkim | 0:b72d22e10709 | 22 | #include "mbed.h" |
jbkim | 0:b72d22e10709 | 23 | #include "mbed_debug.h" |
jbkim | 0:b72d22e10709 | 24 | |
jbkim | 0:b72d22e10709 | 25 | #define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; |
jbkim | 0:b72d22e10709 | 26 | |
jbkim | 0:b72d22e10709 | 27 | #define DEFAULT_WAIT_RESP_TIMEOUT 500 |
jbkim | 0:b72d22e10709 | 28 | |
jbkim | 0:b72d22e10709 | 29 | enum Protocol { |
jbkim | 0:b72d22e10709 | 30 | CLOSED = 0, |
jbkim | 0:b72d22e10709 | 31 | TCP = 1, |
jbkim | 0:b72d22e10709 | 32 | UDP = 2, |
jbkim | 0:b72d22e10709 | 33 | }; |
jbkim | 0:b72d22e10709 | 34 | |
jbkim | 0:b72d22e10709 | 35 | enum Command { |
jbkim | 0:b72d22e10709 | 36 | OPEN = 0x01, |
jbkim | 0:b72d22e10709 | 37 | LISTEN = 0x02, |
jbkim | 0:b72d22e10709 | 38 | CONNECT = 0x04, |
jbkim | 0:b72d22e10709 | 39 | DISCON = 0x08, |
jbkim | 0:b72d22e10709 | 40 | CLOSE = 0x10, |
jbkim | 0:b72d22e10709 | 41 | SEND = 0x20, |
jbkim | 0:b72d22e10709 | 42 | SEND_MAC = 0x21, |
jbkim | 0:b72d22e10709 | 43 | SEND_KEEP = 0x22, |
jbkim | 0:b72d22e10709 | 44 | RECV = 0x40, |
jbkim | 0:b72d22e10709 | 45 | |
jbkim | 0:b72d22e10709 | 46 | }; |
jbkim | 0:b72d22e10709 | 47 | |
jbkim | 0:b72d22e10709 | 48 | enum Interrupt { |
jbkim | 0:b72d22e10709 | 49 | INT_CON = 0x01, |
jbkim | 0:b72d22e10709 | 50 | INT_DISCON = 0x02, |
jbkim | 0:b72d22e10709 | 51 | INT_RECV = 0x04, |
jbkim | 0:b72d22e10709 | 52 | INT_TIMEOUT = 0x08, |
jbkim | 0:b72d22e10709 | 53 | INT_SEND_OK = 0x10, |
jbkim | 0:b72d22e10709 | 54 | }; |
jbkim | 0:b72d22e10709 | 55 | |
jbkim | 0:b72d22e10709 | 56 | enum Status { |
jbkim | 0:b72d22e10709 | 57 | SOCK_CLOSED = 0x00, |
jbkim | 0:b72d22e10709 | 58 | SOCK_INIT = 0x13, |
jbkim | 0:b72d22e10709 | 59 | SOCK_LISTEN = 0x14, |
jbkim | 0:b72d22e10709 | 60 | SOCK_ESTABLISHED = 0x17, |
jbkim | 0:b72d22e10709 | 61 | SOCK_CLOSE_WAIT = 0x1c, |
jbkim | 0:b72d22e10709 | 62 | SOCK_UDP = 0x22, |
jbkim | 0:b72d22e10709 | 63 | }; |
jbkim | 0:b72d22e10709 | 64 | |
jbkim | 0:b72d22e10709 | 65 | #define MAX_SOCK_NUM 4 |
jbkim | 0:b72d22e10709 | 66 | |
jbkim | 0:b72d22e10709 | 67 | #define MR 0x0000 |
jbkim | 0:b72d22e10709 | 68 | #define GAR 0x0001 |
jbkim | 0:b72d22e10709 | 69 | #define SUBR 0x0005 |
jbkim | 0:b72d22e10709 | 70 | #define SHAR 0x0009 |
jbkim | 0:b72d22e10709 | 71 | #define SIPR 0x000f |
jbkim | 0:b72d22e10709 | 72 | |
jbkim | 0:b72d22e10709 | 73 | // W5100 socket |
jbkim | 0:b72d22e10709 | 74 | #define Sn_MR 0x0400 |
jbkim | 0:b72d22e10709 | 75 | #define Sn_CR 0x0401 |
jbkim | 0:b72d22e10709 | 76 | #define Sn_IR 0x0402 |
jbkim | 0:b72d22e10709 | 77 | #define Sn_SR 0x0403 |
jbkim | 0:b72d22e10709 | 78 | #define Sn_PORT 0x0404 |
jbkim | 0:b72d22e10709 | 79 | #define Sn_DIPR 0x040c |
jbkim | 0:b72d22e10709 | 80 | #define Sn_DPORT 0x0410 |
jbkim | 0:b72d22e10709 | 81 | //#define Sn_RXBUF_SIZE 0x401e |
jbkim | 0:b72d22e10709 | 82 | //#define Sn_TXBUF_SIZE 0x401f |
jbkim | 0:b72d22e10709 | 83 | #define Sn_TX_FSR 0x0420 |
jbkim | 0:b72d22e10709 | 84 | #define Sn_TX_WR 0x0424 |
jbkim | 0:b72d22e10709 | 85 | #define Sn_RX_RSR 0x0426 |
jbkim | 0:b72d22e10709 | 86 | #define Sn_RX_RD 0x0428 |
jbkim | 0:b72d22e10709 | 87 | |
jbkim | 0:b72d22e10709 | 88 | class WIZnet_Chip { |
jbkim | 0:b72d22e10709 | 89 | public: |
jbkim | 0:b72d22e10709 | 90 | /* |
jbkim | 0:b72d22e10709 | 91 | * Constructor |
jbkim | 0:b72d22e10709 | 92 | * |
jbkim | 0:b72d22e10709 | 93 | * @param spi spi class |
jbkim | 0:b72d22e10709 | 94 | * @param cs cs of the W5200 |
jbkim | 0:b72d22e10709 | 95 | * @param reset reset pin of the W5200 |
jbkim | 0:b72d22e10709 | 96 | */ |
jbkim | 0:b72d22e10709 | 97 | WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); |
jbkim | 0:b72d22e10709 | 98 | WIZnet_Chip(SPI* spi, PinName cs, PinName reset); |
jbkim | 0:b72d22e10709 | 99 | |
jbkim | 0:b72d22e10709 | 100 | /* |
jbkim | 0:b72d22e10709 | 101 | * Connect the W5200 module to the ssid contained in the constructor. |
jbkim | 0:b72d22e10709 | 102 | * |
jbkim | 0:b72d22e10709 | 103 | * @return true if connected, false otherwise |
jbkim | 0:b72d22e10709 | 104 | */ |
jbkim | 0:b72d22e10709 | 105 | bool setip(); |
jbkim | 0:b72d22e10709 | 106 | |
jbkim | 0:b72d22e10709 | 107 | /* |
jbkim | 0:b72d22e10709 | 108 | * Disconnect the W5200 module from the access point |
jbkim | 0:b72d22e10709 | 109 | * |
jbkim | 0:b72d22e10709 | 110 | * @ returns true if successful |
jbkim | 0:b72d22e10709 | 111 | */ |
jbkim | 0:b72d22e10709 | 112 | bool disconnect(); |
jbkim | 0:b72d22e10709 | 113 | |
jbkim | 0:b72d22e10709 | 114 | /* |
jbkim | 0:b72d22e10709 | 115 | * Open a tcp connection with the specified host on the specified port |
jbkim | 0:b72d22e10709 | 116 | * |
jbkim | 0:b72d22e10709 | 117 | * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) |
jbkim | 0:b72d22e10709 | 118 | * @param port port |
jbkim | 0:b72d22e10709 | 119 | * @ returns true if successful |
jbkim | 0:b72d22e10709 | 120 | */ |
jbkim | 0:b72d22e10709 | 121 | bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000); |
jbkim | 0:b72d22e10709 | 122 | |
jbkim | 0:b72d22e10709 | 123 | /* |
jbkim | 0:b72d22e10709 | 124 | * Set the protocol (UDP or TCP) |
jbkim | 0:b72d22e10709 | 125 | * |
jbkim | 0:b72d22e10709 | 126 | * @param p protocol |
jbkim | 0:b72d22e10709 | 127 | * @ returns true if successful |
jbkim | 0:b72d22e10709 | 128 | */ |
jbkim | 0:b72d22e10709 | 129 | bool setProtocol(int socket, Protocol p); |
jbkim | 0:b72d22e10709 | 130 | |
jbkim | 0:b72d22e10709 | 131 | /* |
jbkim | 0:b72d22e10709 | 132 | * Reset the W5100 module |
jbkim | 0:b72d22e10709 | 133 | */ |
jbkim | 0:b72d22e10709 | 134 | void reset(); |
jbkim | 0:b72d22e10709 | 135 | |
jbkim | 0:b72d22e10709 | 136 | |
jbkim | 0:b72d22e10709 | 137 | int wait_readable(int socket, int wait_time_ms, int req_size = 0); |
jbkim | 0:b72d22e10709 | 138 | |
jbkim | 0:b72d22e10709 | 139 | int wait_writeable(int socket, int wait_time_ms, int req_size = 0); |
jbkim | 0:b72d22e10709 | 140 | |
jbkim | 0:b72d22e10709 | 141 | /* |
jbkim | 0:b72d22e10709 | 142 | * Check if a tcp link is active |
jbkim | 0:b72d22e10709 | 143 | * |
jbkim | 0:b72d22e10709 | 144 | * @returns true if successful |
jbkim | 0:b72d22e10709 | 145 | */ |
jbkim | 0:b72d22e10709 | 146 | bool is_connected(int socket); |
jbkim | 0:b72d22e10709 | 147 | |
jbkim | 0:b72d22e10709 | 148 | /* |
jbkim | 0:b72d22e10709 | 149 | * Close a tcp connection |
jbkim | 0:b72d22e10709 | 150 | * |
jbkim | 0:b72d22e10709 | 151 | * @ returns true if successful |
jbkim | 0:b72d22e10709 | 152 | */ |
jbkim | 0:b72d22e10709 | 153 | bool close(int socket); |
jbkim | 0:b72d22e10709 | 154 | |
jbkim | 0:b72d22e10709 | 155 | /* |
jbkim | 0:b72d22e10709 | 156 | * @param str string to be sent |
jbkim | 0:b72d22e10709 | 157 | * @param len string length |
jbkim | 0:b72d22e10709 | 158 | */ |
jbkim | 0:b72d22e10709 | 159 | int send(int socket, const char * str, int len); |
jbkim | 0:b72d22e10709 | 160 | |
jbkim | 0:b72d22e10709 | 161 | int recv(int socket, char* buf, int len); |
jbkim | 0:b72d22e10709 | 162 | |
jbkim | 0:b72d22e10709 | 163 | /* |
jbkim | 0:b72d22e10709 | 164 | * Return true if the module is using dhcp |
jbkim | 0:b72d22e10709 | 165 | * |
jbkim | 0:b72d22e10709 | 166 | * @returns true if the module is using dhcp |
jbkim | 0:b72d22e10709 | 167 | */ |
jbkim | 0:b72d22e10709 | 168 | bool isDHCP() { |
jbkim | 0:b72d22e10709 | 169 | return dhcp; |
jbkim | 0:b72d22e10709 | 170 | } |
jbkim | 0:b72d22e10709 | 171 | |
jbkim | 0:b72d22e10709 | 172 | bool gethostbyname(const char* host, uint32_t* ip); |
jbkim | 0:b72d22e10709 | 173 | |
jbkim | 0:b72d22e10709 | 174 | static WIZnet_Chip * getInstance() { |
jbkim | 0:b72d22e10709 | 175 | return inst; |
jbkim | 0:b72d22e10709 | 176 | }; |
jbkim | 0:b72d22e10709 | 177 | |
jbkim | 0:b72d22e10709 | 178 | int new_socket(); |
jbkim | 0:b72d22e10709 | 179 | uint16_t new_port(); |
jbkim | 0:b72d22e10709 | 180 | void scmd(int socket, Command cmd); |
jbkim | 0:b72d22e10709 | 181 | |
jbkim | 0:b72d22e10709 | 182 | template<typename T> |
jbkim | 0:b72d22e10709 | 183 | void sreg(int socket, uint16_t addr, T data) { |
jbkim | 0:b72d22e10709 | 184 | reg_wr<T>(addr+0x100*socket, data); |
jbkim | 0:b72d22e10709 | 185 | } |
jbkim | 0:b72d22e10709 | 186 | |
jbkim | 0:b72d22e10709 | 187 | template<typename T> |
jbkim | 0:b72d22e10709 | 188 | T sreg(int socket, uint16_t addr) { |
jbkim | 0:b72d22e10709 | 189 | return reg_rd<T>(addr+0x100*socket); |
jbkim | 0:b72d22e10709 | 190 | } |
jbkim | 0:b72d22e10709 | 191 | |
jbkim | 0:b72d22e10709 | 192 | template<typename T> |
jbkim | 0:b72d22e10709 | 193 | void reg_wr(uint16_t addr, T data) { |
jbkim | 0:b72d22e10709 | 194 | uint8_t buf[sizeof(T)]; |
jbkim | 0:b72d22e10709 | 195 | *reinterpret_cast<T*>(buf) = data; |
jbkim | 0:b72d22e10709 | 196 | for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian |
jbkim | 0:b72d22e10709 | 197 | uint8_t t = buf[i]; |
jbkim | 0:b72d22e10709 | 198 | buf[i] = buf[sizeof(buf)-1-i]; |
jbkim | 0:b72d22e10709 | 199 | buf[sizeof(buf)-1-i] = t; |
jbkim | 0:b72d22e10709 | 200 | } |
jbkim | 0:b72d22e10709 | 201 | spi_write(addr, buf, sizeof(buf)); |
jbkim | 0:b72d22e10709 | 202 | } |
jbkim | 0:b72d22e10709 | 203 | |
jbkim | 0:b72d22e10709 | 204 | template<typename T> |
jbkim | 0:b72d22e10709 | 205 | T reg_rd(uint16_t addr) { |
jbkim | 0:b72d22e10709 | 206 | uint8_t buf[sizeof(T)]; |
jbkim | 0:b72d22e10709 | 207 | spi_read(addr, buf, sizeof(buf)); |
jbkim | 0:b72d22e10709 | 208 | for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian |
jbkim | 0:b72d22e10709 | 209 | uint8_t t = buf[i]; |
jbkim | 0:b72d22e10709 | 210 | buf[i] = buf[sizeof(buf)-1-i]; |
jbkim | 0:b72d22e10709 | 211 | buf[sizeof(buf)-1-i] = t; |
jbkim | 0:b72d22e10709 | 212 | } |
jbkim | 0:b72d22e10709 | 213 | return *reinterpret_cast<T*>(buf); |
jbkim | 0:b72d22e10709 | 214 | } |
jbkim | 0:b72d22e10709 | 215 | |
jbkim | 0:b72d22e10709 | 216 | void reg_rd_mac(uint16_t addr, uint8_t* data) { |
jbkim | 0:b72d22e10709 | 217 | spi_read(addr, data, 6); |
jbkim | 0:b72d22e10709 | 218 | } |
jbkim | 0:b72d22e10709 | 219 | |
jbkim | 0:b72d22e10709 | 220 | void reg_wr_ip(uint16_t addr, const char* ip) { |
jbkim | 0:b72d22e10709 | 221 | uint8_t buf[4]; |
jbkim | 0:b72d22e10709 | 222 | char* p = (char*)ip; |
jbkim | 0:b72d22e10709 | 223 | for(int i = 0; i < 4; i++) { |
jbkim | 0:b72d22e10709 | 224 | buf[i] = atoi(p); |
jbkim | 0:b72d22e10709 | 225 | p = strchr(p, '.'); |
jbkim | 0:b72d22e10709 | 226 | if (p == NULL) { |
jbkim | 0:b72d22e10709 | 227 | break; |
jbkim | 0:b72d22e10709 | 228 | } |
jbkim | 0:b72d22e10709 | 229 | p++; |
jbkim | 0:b72d22e10709 | 230 | } |
jbkim | 0:b72d22e10709 | 231 | spi_write(addr, buf, sizeof(buf)); |
jbkim | 0:b72d22e10709 | 232 | } |
jbkim | 0:b72d22e10709 | 233 | |
jbkim | 0:b72d22e10709 | 234 | void sreg_ip(int socket, uint16_t addr, const char* ip) { |
jbkim | 0:b72d22e10709 | 235 | reg_wr_ip(addr+0x100*socket, ip); |
jbkim | 0:b72d22e10709 | 236 | } |
jbkim | 0:b72d22e10709 | 237 | |
jbkim | 0:b72d22e10709 | 238 | protected: |
jbkim | 1:8138a268fbd2 | 239 | uint8_t mac[6]; |
jbkim | 0:b72d22e10709 | 240 | uint32_t ip; |
jbkim | 0:b72d22e10709 | 241 | uint32_t netmask; |
jbkim | 0:b72d22e10709 | 242 | uint32_t gateway; |
jbkim | 0:b72d22e10709 | 243 | uint32_t dnsaddr; |
jbkim | 0:b72d22e10709 | 244 | bool dhcp; |
jbkim | 0:b72d22e10709 | 245 | |
jbkim | 0:b72d22e10709 | 246 | static WIZnet_Chip* inst; |
jbkim | 0:b72d22e10709 | 247 | |
jbkim | 0:b72d22e10709 | 248 | void reg_wr_mac(uint16_t addr, uint8_t* data) { |
jbkim | 0:b72d22e10709 | 249 | spi_write(addr, data, 6); |
jbkim | 0:b72d22e10709 | 250 | } |
jbkim | 0:b72d22e10709 | 251 | |
jbkim | 0:b72d22e10709 | 252 | void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len); |
jbkim | 0:b72d22e10709 | 253 | void spi_read(uint16_t addr, uint8_t *buf, uint16_t len); |
jbkim | 0:b72d22e10709 | 254 | SPI* spi; |
jbkim | 0:b72d22e10709 | 255 | DigitalOut cs; |
jbkim | 0:b72d22e10709 | 256 | DigitalOut reset_pin; |
jbkim | 0:b72d22e10709 | 257 | }; |
jbkim | 0:b72d22e10709 | 258 | |
jbkim | 0:b72d22e10709 | 259 | extern uint32_t str_to_ip(const char* str); |
jbkim | 0:b72d22e10709 | 260 | extern void printfBytes(char* str, uint8_t* buf, int len); |
jbkim | 0:b72d22e10709 | 261 | extern void printHex(uint8_t* buf, int len); |
jbkim | 0:b72d22e10709 | 262 | extern void debug_hex(uint8_t* buf, int len); |