Update of W5500 Interface for mbed-os
WIZnet/W5500.h@0:77e050d1fb12, 2017-06-02 (annotated)
- Committer:
- dgriffin65
- Date:
- Fri Jun 02 22:00:49 2017 +0000
- Revision:
- 0:77e050d1fb12
- Child:
- 1:2dee44ea52a9
Many updates for implementing for W5500 support in mbed os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dgriffin65 | 0:77e050d1fb12 | 1 | |
dgriffin65 | 0:77e050d1fb12 | 2 | #pragma once |
dgriffin65 | 0:77e050d1fb12 | 3 | |
dgriffin65 | 0:77e050d1fb12 | 4 | #include "mbed.h" |
dgriffin65 | 0:77e050d1fb12 | 5 | #include "mbed_debug.h" |
dgriffin65 | 0:77e050d1fb12 | 6 | |
dgriffin65 | 0:77e050d1fb12 | 7 | #define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; |
dgriffin65 | 0:77e050d1fb12 | 8 | |
dgriffin65 | 0:77e050d1fb12 | 9 | #define DEFAULT_WAIT_RESP_TIMEOUT 500 |
dgriffin65 | 0:77e050d1fb12 | 10 | |
dgriffin65 | 0:77e050d1fb12 | 11 | enum Protocol { |
dgriffin65 | 0:77e050d1fb12 | 12 | CLOSED = 0, |
dgriffin65 | 0:77e050d1fb12 | 13 | TCP = 1, |
dgriffin65 | 0:77e050d1fb12 | 14 | UDP = 2, |
dgriffin65 | 0:77e050d1fb12 | 15 | }; |
dgriffin65 | 0:77e050d1fb12 | 16 | |
dgriffin65 | 0:77e050d1fb12 | 17 | enum Command { |
dgriffin65 | 0:77e050d1fb12 | 18 | OPEN = 0x01, |
dgriffin65 | 0:77e050d1fb12 | 19 | LISTEN = 0x02, |
dgriffin65 | 0:77e050d1fb12 | 20 | CONNECT = 0x04, |
dgriffin65 | 0:77e050d1fb12 | 21 | DISCON = 0x08, |
dgriffin65 | 0:77e050d1fb12 | 22 | CLOSE = 0x10, |
dgriffin65 | 0:77e050d1fb12 | 23 | SEND = 0x20, |
dgriffin65 | 0:77e050d1fb12 | 24 | SEND_MAC = 0x21, |
dgriffin65 | 0:77e050d1fb12 | 25 | SEND_KEEP = 0x22, |
dgriffin65 | 0:77e050d1fb12 | 26 | RECV = 0x40, |
dgriffin65 | 0:77e050d1fb12 | 27 | |
dgriffin65 | 0:77e050d1fb12 | 28 | }; |
dgriffin65 | 0:77e050d1fb12 | 29 | |
dgriffin65 | 0:77e050d1fb12 | 30 | enum Interrupt { |
dgriffin65 | 0:77e050d1fb12 | 31 | INT_CON = 0x01, |
dgriffin65 | 0:77e050d1fb12 | 32 | INT_DISCON = 0x02, |
dgriffin65 | 0:77e050d1fb12 | 33 | INT_RECV = 0x04, |
dgriffin65 | 0:77e050d1fb12 | 34 | INT_TIMEOUT = 0x08, |
dgriffin65 | 0:77e050d1fb12 | 35 | INT_SEND_OK = 0x10, |
dgriffin65 | 0:77e050d1fb12 | 36 | }; |
dgriffin65 | 0:77e050d1fb12 | 37 | |
dgriffin65 | 0:77e050d1fb12 | 38 | enum Status { |
dgriffin65 | 0:77e050d1fb12 | 39 | SOCK_CLOSED = 0x00, |
dgriffin65 | 0:77e050d1fb12 | 40 | SOCK_INIT = 0x13, |
dgriffin65 | 0:77e050d1fb12 | 41 | SOCK_LISTEN = 0x14, |
dgriffin65 | 0:77e050d1fb12 | 42 | SOCK_SYNSENT = 0x15, |
dgriffin65 | 0:77e050d1fb12 | 43 | SOCK_ESTABLISHED = 0x17, |
dgriffin65 | 0:77e050d1fb12 | 44 | SOCK_CLOSE_WAIT = 0x1c, |
dgriffin65 | 0:77e050d1fb12 | 45 | SOCK_UDP = 0x22, |
dgriffin65 | 0:77e050d1fb12 | 46 | }; |
dgriffin65 | 0:77e050d1fb12 | 47 | |
dgriffin65 | 0:77e050d1fb12 | 48 | #define MAX_SOCK_NUM 8 |
dgriffin65 | 0:77e050d1fb12 | 49 | |
dgriffin65 | 0:77e050d1fb12 | 50 | #define MR 0x0000 |
dgriffin65 | 0:77e050d1fb12 | 51 | #define GAR 0x0001 |
dgriffin65 | 0:77e050d1fb12 | 52 | #define SUBR 0x0005 |
dgriffin65 | 0:77e050d1fb12 | 53 | #define SHAR 0x0009 |
dgriffin65 | 0:77e050d1fb12 | 54 | #define SIPR 0x000f |
dgriffin65 | 0:77e050d1fb12 | 55 | #define PHYSTATUS 0x0035 |
dgriffin65 | 0:77e050d1fb12 | 56 | |
dgriffin65 | 0:77e050d1fb12 | 57 | // W5500 socket register |
dgriffin65 | 0:77e050d1fb12 | 58 | #define Sn_MR 0x0000 |
dgriffin65 | 0:77e050d1fb12 | 59 | #define Sn_CR 0x0001 |
dgriffin65 | 0:77e050d1fb12 | 60 | #define Sn_IR 0x0002 |
dgriffin65 | 0:77e050d1fb12 | 61 | #define Sn_SR 0x0003 |
dgriffin65 | 0:77e050d1fb12 | 62 | #define Sn_PORT 0x0004 |
dgriffin65 | 0:77e050d1fb12 | 63 | #define Sn_DIPR 0x000c |
dgriffin65 | 0:77e050d1fb12 | 64 | #define Sn_DPORT 0x0010 |
dgriffin65 | 0:77e050d1fb12 | 65 | #define Sn_RXBUF_SIZE 0x001e |
dgriffin65 | 0:77e050d1fb12 | 66 | #define Sn_TXBUF_SIZE 0x001f |
dgriffin65 | 0:77e050d1fb12 | 67 | #define Sn_TX_FSR 0x0020 |
dgriffin65 | 0:77e050d1fb12 | 68 | #define Sn_TX_WR 0x0024 |
dgriffin65 | 0:77e050d1fb12 | 69 | #define Sn_RX_RSR 0x0026 |
dgriffin65 | 0:77e050d1fb12 | 70 | #define Sn_RX_RD 0x0028 |
dgriffin65 | 0:77e050d1fb12 | 71 | |
dgriffin65 | 0:77e050d1fb12 | 72 | class WIZnet_Chip { |
dgriffin65 | 0:77e050d1fb12 | 73 | public: |
dgriffin65 | 0:77e050d1fb12 | 74 | /* |
dgriffin65 | 0:77e050d1fb12 | 75 | * Constructor |
dgriffin65 | 0:77e050d1fb12 | 76 | * |
dgriffin65 | 0:77e050d1fb12 | 77 | * @param spi spi class |
dgriffin65 | 0:77e050d1fb12 | 78 | * @param cs cs of the W5500 |
dgriffin65 | 0:77e050d1fb12 | 79 | * @param reset reset pin of the W5500 |
dgriffin65 | 0:77e050d1fb12 | 80 | */ |
dgriffin65 | 0:77e050d1fb12 | 81 | WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); |
dgriffin65 | 0:77e050d1fb12 | 82 | WIZnet_Chip(SPI* spi, PinName cs, PinName reset); |
dgriffin65 | 0:77e050d1fb12 | 83 | |
dgriffin65 | 0:77e050d1fb12 | 84 | /* |
dgriffin65 | 0:77e050d1fb12 | 85 | * Set MAC Address to W5500 |
dgriffin65 | 0:77e050d1fb12 | 86 | * |
dgriffin65 | 0:77e050d1fb12 | 87 | * @return true if connected, false otherwise |
dgriffin65 | 0:77e050d1fb12 | 88 | */ |
dgriffin65 | 0:77e050d1fb12 | 89 | bool setmac(); |
dgriffin65 | 0:77e050d1fb12 | 90 | |
dgriffin65 | 0:77e050d1fb12 | 91 | /* |
dgriffin65 | 0:77e050d1fb12 | 92 | * Set Network Informations (SrcIP, Netmask, Gataway) |
dgriffin65 | 0:77e050d1fb12 | 93 | * |
dgriffin65 | 0:77e050d1fb12 | 94 | * @return true if connected, false otherwise |
dgriffin65 | 0:77e050d1fb12 | 95 | */ |
dgriffin65 | 0:77e050d1fb12 | 96 | bool setip(); |
dgriffin65 | 0:77e050d1fb12 | 97 | |
dgriffin65 | 0:77e050d1fb12 | 98 | /* |
dgriffin65 | 0:77e050d1fb12 | 99 | * Disconnect the connection |
dgriffin65 | 0:77e050d1fb12 | 100 | * |
dgriffin65 | 0:77e050d1fb12 | 101 | * @ returns true |
dgriffin65 | 0:77e050d1fb12 | 102 | */ |
dgriffin65 | 0:77e050d1fb12 | 103 | bool disconnect(); |
dgriffin65 | 0:77e050d1fb12 | 104 | |
dgriffin65 | 0:77e050d1fb12 | 105 | /* |
dgriffin65 | 0:77e050d1fb12 | 106 | * Open a tcp connection with the specified host on the specified port |
dgriffin65 | 0:77e050d1fb12 | 107 | * |
dgriffin65 | 0:77e050d1fb12 | 108 | * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) |
dgriffin65 | 0:77e050d1fb12 | 109 | * @param port port |
dgriffin65 | 0:77e050d1fb12 | 110 | * @ returns true if successful |
dgriffin65 | 0:77e050d1fb12 | 111 | */ |
dgriffin65 | 0:77e050d1fb12 | 112 | bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000); |
dgriffin65 | 0:77e050d1fb12 | 113 | |
dgriffin65 | 0:77e050d1fb12 | 114 | /* |
dgriffin65 | 0:77e050d1fb12 | 115 | * Set the protocol (UDP or TCP) |
dgriffin65 | 0:77e050d1fb12 | 116 | * |
dgriffin65 | 0:77e050d1fb12 | 117 | * @param p protocol |
dgriffin65 | 0:77e050d1fb12 | 118 | * @ returns true if successful |
dgriffin65 | 0:77e050d1fb12 | 119 | */ |
dgriffin65 | 0:77e050d1fb12 | 120 | bool setProtocol(int socket, Protocol p); |
dgriffin65 | 0:77e050d1fb12 | 121 | |
dgriffin65 | 0:77e050d1fb12 | 122 | /* |
dgriffin65 | 0:77e050d1fb12 | 123 | * Reset the W5500 |
dgriffin65 | 0:77e050d1fb12 | 124 | */ |
dgriffin65 | 0:77e050d1fb12 | 125 | void reset(); |
dgriffin65 | 0:77e050d1fb12 | 126 | |
dgriffin65 | 0:77e050d1fb12 | 127 | int wait_readable(int socket, int wait_time_ms, int req_size = 0); |
dgriffin65 | 0:77e050d1fb12 | 128 | |
dgriffin65 | 0:77e050d1fb12 | 129 | int wait_writeable(int socket, int wait_time_ms, int req_size = 0); |
dgriffin65 | 0:77e050d1fb12 | 130 | |
dgriffin65 | 0:77e050d1fb12 | 131 | /* |
dgriffin65 | 0:77e050d1fb12 | 132 | * Check if a tcp link is active |
dgriffin65 | 0:77e050d1fb12 | 133 | * |
dgriffin65 | 0:77e050d1fb12 | 134 | * @returns true if successful |
dgriffin65 | 0:77e050d1fb12 | 135 | */ |
dgriffin65 | 0:77e050d1fb12 | 136 | bool is_connected(int socket); |
dgriffin65 | 0:77e050d1fb12 | 137 | |
dgriffin65 | 0:77e050d1fb12 | 138 | /* |
dgriffin65 | 0:77e050d1fb12 | 139 | * Close a tcp connection |
dgriffin65 | 0:77e050d1fb12 | 140 | * |
dgriffin65 | 0:77e050d1fb12 | 141 | * @ returns true if successful |
dgriffin65 | 0:77e050d1fb12 | 142 | */ |
dgriffin65 | 0:77e050d1fb12 | 143 | bool close(int socket); |
dgriffin65 | 0:77e050d1fb12 | 144 | |
dgriffin65 | 0:77e050d1fb12 | 145 | /* |
dgriffin65 | 0:77e050d1fb12 | 146 | * @param str string to be sent |
dgriffin65 | 0:77e050d1fb12 | 147 | * @param len string length |
dgriffin65 | 0:77e050d1fb12 | 148 | */ |
dgriffin65 | 0:77e050d1fb12 | 149 | int send(int socket, const char * str, int len); |
dgriffin65 | 0:77e050d1fb12 | 150 | |
dgriffin65 | 0:77e050d1fb12 | 151 | int recv(int socket, char* buf, int len); |
dgriffin65 | 0:77e050d1fb12 | 152 | |
dgriffin65 | 0:77e050d1fb12 | 153 | /* |
dgriffin65 | 0:77e050d1fb12 | 154 | * Return true if the module is using dhcp |
dgriffin65 | 0:77e050d1fb12 | 155 | * |
dgriffin65 | 0:77e050d1fb12 | 156 | * @returns true if the module is using dhcp |
dgriffin65 | 0:77e050d1fb12 | 157 | */ |
dgriffin65 | 0:77e050d1fb12 | 158 | bool isDHCP() { |
dgriffin65 | 0:77e050d1fb12 | 159 | return dhcp; |
dgriffin65 | 0:77e050d1fb12 | 160 | } |
dgriffin65 | 0:77e050d1fb12 | 161 | |
dgriffin65 | 0:77e050d1fb12 | 162 | bool gethostbyname(const char* host, uint32_t* ip); |
dgriffin65 | 0:77e050d1fb12 | 163 | |
dgriffin65 | 0:77e050d1fb12 | 164 | static WIZnet_Chip * getInstance() { |
dgriffin65 | 0:77e050d1fb12 | 165 | return inst; |
dgriffin65 | 0:77e050d1fb12 | 166 | }; |
dgriffin65 | 0:77e050d1fb12 | 167 | |
dgriffin65 | 0:77e050d1fb12 | 168 | int new_socket(); |
dgriffin65 | 0:77e050d1fb12 | 169 | uint16_t new_port(); |
dgriffin65 | 0:77e050d1fb12 | 170 | void scmd(int socket, Command cmd); |
dgriffin65 | 0:77e050d1fb12 | 171 | |
dgriffin65 | 0:77e050d1fb12 | 172 | template<typename T> |
dgriffin65 | 0:77e050d1fb12 | 173 | void sreg(int socket, uint16_t addr, T data) { |
dgriffin65 | 0:77e050d1fb12 | 174 | reg_wr<T>(addr, (0x0C + (socket << 5)), data); |
dgriffin65 | 0:77e050d1fb12 | 175 | } |
dgriffin65 | 0:77e050d1fb12 | 176 | |
dgriffin65 | 0:77e050d1fb12 | 177 | template<typename T> |
dgriffin65 | 0:77e050d1fb12 | 178 | T sreg(int socket, uint16_t addr) { |
dgriffin65 | 0:77e050d1fb12 | 179 | return reg_rd<T>(addr, (0x08 + (socket << 5))); |
dgriffin65 | 0:77e050d1fb12 | 180 | } |
dgriffin65 | 0:77e050d1fb12 | 181 | |
dgriffin65 | 0:77e050d1fb12 | 182 | template<typename T> |
dgriffin65 | 0:77e050d1fb12 | 183 | void reg_wr(uint16_t addr, T data) { |
dgriffin65 | 0:77e050d1fb12 | 184 | return reg_wr(addr, 0x04, data); |
dgriffin65 | 0:77e050d1fb12 | 185 | } |
dgriffin65 | 0:77e050d1fb12 | 186 | |
dgriffin65 | 0:77e050d1fb12 | 187 | template<typename T> |
dgriffin65 | 0:77e050d1fb12 | 188 | void reg_wr(uint16_t addr, uint8_t cb, T data) { |
dgriffin65 | 0:77e050d1fb12 | 189 | uint8_t buf[sizeof(T)]; |
dgriffin65 | 0:77e050d1fb12 | 190 | *reinterpret_cast<T*>(buf) = data; |
dgriffin65 | 0:77e050d1fb12 | 191 | for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian |
dgriffin65 | 0:77e050d1fb12 | 192 | uint8_t t = buf[i]; |
dgriffin65 | 0:77e050d1fb12 | 193 | buf[i] = buf[sizeof(buf)-1-i]; |
dgriffin65 | 0:77e050d1fb12 | 194 | buf[sizeof(buf)-1-i] = t; |
dgriffin65 | 0:77e050d1fb12 | 195 | } |
dgriffin65 | 0:77e050d1fb12 | 196 | spi_write(addr, cb, buf, sizeof(buf)); |
dgriffin65 | 0:77e050d1fb12 | 197 | } |
dgriffin65 | 0:77e050d1fb12 | 198 | |
dgriffin65 | 0:77e050d1fb12 | 199 | template<typename T> |
dgriffin65 | 0:77e050d1fb12 | 200 | T reg_rd(uint16_t addr) { |
dgriffin65 | 0:77e050d1fb12 | 201 | return reg_rd<T>(addr, 0x00); |
dgriffin65 | 0:77e050d1fb12 | 202 | } |
dgriffin65 | 0:77e050d1fb12 | 203 | |
dgriffin65 | 0:77e050d1fb12 | 204 | template<typename T> |
dgriffin65 | 0:77e050d1fb12 | 205 | T reg_rd(uint16_t addr, uint8_t cb) { |
dgriffin65 | 0:77e050d1fb12 | 206 | uint8_t buf[sizeof(T)]; |
dgriffin65 | 0:77e050d1fb12 | 207 | spi_read(addr, cb, buf, sizeof(buf)); |
dgriffin65 | 0:77e050d1fb12 | 208 | for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian |
dgriffin65 | 0:77e050d1fb12 | 209 | uint8_t t = buf[i]; |
dgriffin65 | 0:77e050d1fb12 | 210 | buf[i] = buf[sizeof(buf)-1-i]; |
dgriffin65 | 0:77e050d1fb12 | 211 | buf[sizeof(buf)-1-i] = t; |
dgriffin65 | 0:77e050d1fb12 | 212 | } |
dgriffin65 | 0:77e050d1fb12 | 213 | return *reinterpret_cast<T*>(buf); |
dgriffin65 | 0:77e050d1fb12 | 214 | } |
dgriffin65 | 0:77e050d1fb12 | 215 | |
dgriffin65 | 0:77e050d1fb12 | 216 | void reg_rd_mac(uint16_t addr, uint8_t* data) { |
dgriffin65 | 0:77e050d1fb12 | 217 | spi_read(addr, 0x00, data, 6); |
dgriffin65 | 0:77e050d1fb12 | 218 | } |
dgriffin65 | 0:77e050d1fb12 | 219 | |
dgriffin65 | 0:77e050d1fb12 | 220 | void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip) { |
dgriffin65 | 0:77e050d1fb12 | 221 | uint8_t buf[4]; |
dgriffin65 | 0:77e050d1fb12 | 222 | char* p = (char*)ip; |
dgriffin65 | 0:77e050d1fb12 | 223 | for(int i = 0; i < 4; i++) { |
dgriffin65 | 0:77e050d1fb12 | 224 | buf[i] = atoi(p); |
dgriffin65 | 0:77e050d1fb12 | 225 | p = strchr(p, '.'); |
dgriffin65 | 0:77e050d1fb12 | 226 | if (p == NULL) { |
dgriffin65 | 0:77e050d1fb12 | 227 | break; |
dgriffin65 | 0:77e050d1fb12 | 228 | } |
dgriffin65 | 0:77e050d1fb12 | 229 | p++; |
dgriffin65 | 0:77e050d1fb12 | 230 | } |
dgriffin65 | 0:77e050d1fb12 | 231 | spi_write(addr, cb, buf, sizeof(buf)); |
dgriffin65 | 0:77e050d1fb12 | 232 | } |
dgriffin65 | 0:77e050d1fb12 | 233 | |
dgriffin65 | 0:77e050d1fb12 | 234 | void sreg_ip(int socket, uint16_t addr, const char* ip) { |
dgriffin65 | 0:77e050d1fb12 | 235 | reg_wr_ip(addr, (0x0C + (socket << 5)), ip); |
dgriffin65 | 0:77e050d1fb12 | 236 | } |
dgriffin65 | 0:77e050d1fb12 | 237 | |
dgriffin65 | 0:77e050d1fb12 | 238 | protected: |
dgriffin65 | 0:77e050d1fb12 | 239 | uint8_t mac[6]; |
dgriffin65 | 0:77e050d1fb12 | 240 | uint32_t ip; |
dgriffin65 | 0:77e050d1fb12 | 241 | uint32_t netmask; |
dgriffin65 | 0:77e050d1fb12 | 242 | uint32_t gateway; |
dgriffin65 | 0:77e050d1fb12 | 243 | uint32_t dnsaddr; |
dgriffin65 | 0:77e050d1fb12 | 244 | bool dhcp; |
dgriffin65 | 0:77e050d1fb12 | 245 | |
dgriffin65 | 0:77e050d1fb12 | 246 | static WIZnet_Chip* inst; |
dgriffin65 | 0:77e050d1fb12 | 247 | |
dgriffin65 | 0:77e050d1fb12 | 248 | void reg_wr_mac(uint16_t addr, uint8_t* data) { |
dgriffin65 | 0:77e050d1fb12 | 249 | spi_write(addr, 0x04, data, 6); |
dgriffin65 | 0:77e050d1fb12 | 250 | } |
dgriffin65 | 0:77e050d1fb12 | 251 | |
dgriffin65 | 0:77e050d1fb12 | 252 | void spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len); |
dgriffin65 | 0:77e050d1fb12 | 253 | void spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len); |
dgriffin65 | 0:77e050d1fb12 | 254 | SPI* spi; |
dgriffin65 | 0:77e050d1fb12 | 255 | DigitalOut cs; |
dgriffin65 | 0:77e050d1fb12 | 256 | DigitalOut reset_pin; |
dgriffin65 | 0:77e050d1fb12 | 257 | }; |
dgriffin65 | 0:77e050d1fb12 | 258 | |
dgriffin65 | 0:77e050d1fb12 | 259 | extern uint32_t str_to_ip(const char* str); |
dgriffin65 | 0:77e050d1fb12 | 260 | extern void printfBytes(char* str, uint8_t* buf, int len); |
dgriffin65 | 0:77e050d1fb12 | 261 | extern void printHex(uint8_t* buf, int len); |
dgriffin65 | 0:77e050d1fb12 | 262 | extern void debug_hex(uint8_t* buf, int len); |