Update of W5500 Interface for mbed-os
WIZnet/W5500.cpp@1:2dee44ea52a9, 2017-06-15 (annotated)
- Committer:
- dgriffin65
- Date:
- Thu Jun 15 20:19:23 2017 +0000
- Revision:
- 1:2dee44ea52a9
- Parent:
- 0:77e050d1fb12
Updated to mbed-os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dgriffin65 | 0:77e050d1fb12 | 1 | |
dgriffin65 | 0:77e050d1fb12 | 2 | #include "mbed.h" |
dgriffin65 | 0:77e050d1fb12 | 3 | #include "mbed_debug.h" |
dgriffin65 | 0:77e050d1fb12 | 4 | #include "wiznet.h" |
dgriffin65 | 0:77e050d1fb12 | 5 | |
dgriffin65 | 0:77e050d1fb12 | 6 | #ifdef USE_W5500 |
dgriffin65 | 0:77e050d1fb12 | 7 | |
dgriffin65 | 0:77e050d1fb12 | 8 | //Debug is disabled by default |
dgriffin65 | 0:77e050d1fb12 | 9 | #if 0 |
dgriffin65 | 0:77e050d1fb12 | 10 | #define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0); |
dgriffin65 | 0:77e050d1fb12 | 11 | //#define DBG(x, ...) debug("[W5500:DBG]"x"\r\n", ##__VA_ARGS__); |
dgriffin65 | 0:77e050d1fb12 | 12 | #define WARN(x, ...) debug("[W5500:WARN]"x"\r\n", ##__VA_ARGS__); |
dgriffin65 | 0:77e050d1fb12 | 13 | #define ERR(x, ...) debug("[W5500:ERR]"x"\r\n", ##__VA_ARGS__); |
dgriffin65 | 0:77e050d1fb12 | 14 | #else |
dgriffin65 | 0:77e050d1fb12 | 15 | #define DBG(x, ...) |
dgriffin65 | 0:77e050d1fb12 | 16 | #define WARN(x, ...) |
dgriffin65 | 0:77e050d1fb12 | 17 | #define ERR(x, ...) |
dgriffin65 | 0:77e050d1fb12 | 18 | #endif |
dgriffin65 | 0:77e050d1fb12 | 19 | |
dgriffin65 | 0:77e050d1fb12 | 20 | #if 1 |
dgriffin65 | 0:77e050d1fb12 | 21 | #define INFO(x, ...) debug("[W5500:INFO]"x"\r\n", ##__VA_ARGS__); |
dgriffin65 | 0:77e050d1fb12 | 22 | #else |
dgriffin65 | 0:77e050d1fb12 | 23 | #define INFO(x, ...) |
dgriffin65 | 0:77e050d1fb12 | 24 | #endif |
dgriffin65 | 0:77e050d1fb12 | 25 | |
dgriffin65 | 0:77e050d1fb12 | 26 | #define DBG_SPI 0 |
dgriffin65 | 0:77e050d1fb12 | 27 | |
dgriffin65 | 0:77e050d1fb12 | 28 | WIZnet_Chip* WIZnet_Chip::inst; |
dgriffin65 | 0:77e050d1fb12 | 29 | |
dgriffin65 | 0:77e050d1fb12 | 30 | WIZnet_Chip::WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset): |
dgriffin65 | 0:77e050d1fb12 | 31 | cs(_cs), reset_pin(_reset) |
dgriffin65 | 0:77e050d1fb12 | 32 | { |
dgriffin65 | 0:77e050d1fb12 | 33 | spi = new SPI(mosi, miso, sclk); |
dgriffin65 | 0:77e050d1fb12 | 34 | cs = 1; |
dgriffin65 | 0:77e050d1fb12 | 35 | reset_pin = 1; |
dgriffin65 | 0:77e050d1fb12 | 36 | inst = this; |
dgriffin65 | 0:77e050d1fb12 | 37 | dhcp = false; |
dgriffin65 | 0:77e050d1fb12 | 38 | } |
dgriffin65 | 0:77e050d1fb12 | 39 | |
dgriffin65 | 0:77e050d1fb12 | 40 | WIZnet_Chip::WIZnet_Chip(SPI* spi, PinName _cs, PinName _reset): |
dgriffin65 | 0:77e050d1fb12 | 41 | cs(_cs), reset_pin(_reset) |
dgriffin65 | 0:77e050d1fb12 | 42 | { |
dgriffin65 | 0:77e050d1fb12 | 43 | this->spi = spi; |
dgriffin65 | 0:77e050d1fb12 | 44 | cs = 1; |
dgriffin65 | 0:77e050d1fb12 | 45 | reset_pin = 1; |
dgriffin65 | 0:77e050d1fb12 | 46 | inst = this; |
dgriffin65 | 0:77e050d1fb12 | 47 | dhcp = false; |
dgriffin65 | 0:77e050d1fb12 | 48 | } |
dgriffin65 | 0:77e050d1fb12 | 49 | |
dgriffin65 | 0:77e050d1fb12 | 50 | bool WIZnet_Chip::setmac() |
dgriffin65 | 0:77e050d1fb12 | 51 | { |
dgriffin65 | 0:77e050d1fb12 | 52 | for (int i =0; i < 6; i++) reg_wr<uint8_t>(SHAR+i, mac[i]); |
dgriffin65 | 0:77e050d1fb12 | 53 | return true; |
dgriffin65 | 0:77e050d1fb12 | 54 | } |
dgriffin65 | 0:77e050d1fb12 | 55 | |
dgriffin65 | 0:77e050d1fb12 | 56 | // Set the IP |
dgriffin65 | 0:77e050d1fb12 | 57 | bool WIZnet_Chip::setip() |
dgriffin65 | 0:77e050d1fb12 | 58 | { |
dgriffin65 | 0:77e050d1fb12 | 59 | reg_wr<uint32_t>(SIPR, ip); |
dgriffin65 | 0:77e050d1fb12 | 60 | reg_wr<uint32_t>(GAR, gateway); |
dgriffin65 | 0:77e050d1fb12 | 61 | reg_wr<uint32_t>(SUBR, netmask); |
dgriffin65 | 0:77e050d1fb12 | 62 | return true; |
dgriffin65 | 0:77e050d1fb12 | 63 | } |
dgriffin65 | 0:77e050d1fb12 | 64 | |
dgriffin65 | 0:77e050d1fb12 | 65 | bool WIZnet_Chip::setProtocol(int socket, Protocol p) |
dgriffin65 | 0:77e050d1fb12 | 66 | { |
dgriffin65 | 0:77e050d1fb12 | 67 | if (socket < 0) { |
dgriffin65 | 0:77e050d1fb12 | 68 | return false; |
dgriffin65 | 0:77e050d1fb12 | 69 | } |
dgriffin65 | 0:77e050d1fb12 | 70 | sreg<uint8_t>(socket, Sn_MR, p); |
dgriffin65 | 0:77e050d1fb12 | 71 | return true; |
dgriffin65 | 0:77e050d1fb12 | 72 | } |
dgriffin65 | 0:77e050d1fb12 | 73 | |
dgriffin65 | 0:77e050d1fb12 | 74 | bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms) |
dgriffin65 | 0:77e050d1fb12 | 75 | { |
dgriffin65 | 0:77e050d1fb12 | 76 | if (socket < 0) { |
dgriffin65 | 0:77e050d1fb12 | 77 | return false; |
dgriffin65 | 0:77e050d1fb12 | 78 | } |
dgriffin65 | 0:77e050d1fb12 | 79 | sreg<uint8_t>(socket, Sn_MR, TCP); |
dgriffin65 | 0:77e050d1fb12 | 80 | scmd(socket, OPEN); |
dgriffin65 | 0:77e050d1fb12 | 81 | sreg_ip(socket, Sn_DIPR, host); |
dgriffin65 | 0:77e050d1fb12 | 82 | sreg<uint16_t>(socket, Sn_DPORT, port); |
dgriffin65 | 0:77e050d1fb12 | 83 | sreg<uint16_t>(socket, Sn_PORT, new_port()); |
dgriffin65 | 0:77e050d1fb12 | 84 | scmd(socket, CONNECT); |
dgriffin65 | 0:77e050d1fb12 | 85 | Timer t; |
dgriffin65 | 0:77e050d1fb12 | 86 | t.reset(); |
dgriffin65 | 0:77e050d1fb12 | 87 | t.start(); |
dgriffin65 | 0:77e050d1fb12 | 88 | while(!is_connected(socket)) { |
dgriffin65 | 0:77e050d1fb12 | 89 | if (t.read_ms() > timeout_ms) { |
dgriffin65 | 0:77e050d1fb12 | 90 | return false; |
dgriffin65 | 0:77e050d1fb12 | 91 | } |
dgriffin65 | 0:77e050d1fb12 | 92 | } |
dgriffin65 | 0:77e050d1fb12 | 93 | return true; |
dgriffin65 | 0:77e050d1fb12 | 94 | } |
dgriffin65 | 0:77e050d1fb12 | 95 | |
dgriffin65 | 0:77e050d1fb12 | 96 | bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip) |
dgriffin65 | 0:77e050d1fb12 | 97 | { |
dgriffin65 | 0:77e050d1fb12 | 98 | #if 0 |
dgriffin65 | 0:77e050d1fb12 | 99 | uint32_t addr = str_to_ip(host); |
dgriffin65 | 0:77e050d1fb12 | 100 | char buf[17]; |
dgriffin65 | 0:77e050d1fb12 | 101 | snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff); |
dgriffin65 | 0:77e050d1fb12 | 102 | if (strcmp(buf, host) == 0) { |
dgriffin65 | 0:77e050d1fb12 | 103 | *ip = addr; |
dgriffin65 | 0:77e050d1fb12 | 104 | return true; |
dgriffin65 | 0:77e050d1fb12 | 105 | } |
dgriffin65 | 0:77e050d1fb12 | 106 | DNSClient client; |
dgriffin65 | 0:77e050d1fb12 | 107 | if(client.lookup(host)) { |
dgriffin65 | 0:77e050d1fb12 | 108 | *ip = client.ip; |
dgriffin65 | 0:77e050d1fb12 | 109 | return true; |
dgriffin65 | 0:77e050d1fb12 | 110 | } |
dgriffin65 | 0:77e050d1fb12 | 111 | #endif |
dgriffin65 | 0:77e050d1fb12 | 112 | return false; |
dgriffin65 | 0:77e050d1fb12 | 113 | } |
dgriffin65 | 0:77e050d1fb12 | 114 | |
dgriffin65 | 0:77e050d1fb12 | 115 | bool WIZnet_Chip::disconnect() |
dgriffin65 | 0:77e050d1fb12 | 116 | { |
dgriffin65 | 0:77e050d1fb12 | 117 | return true; |
dgriffin65 | 0:77e050d1fb12 | 118 | } |
dgriffin65 | 0:77e050d1fb12 | 119 | |
dgriffin65 | 0:77e050d1fb12 | 120 | bool WIZnet_Chip::is_connected(int socket) |
dgriffin65 | 0:77e050d1fb12 | 121 | { |
dgriffin65 | 0:77e050d1fb12 | 122 | uint8_t tmpSn_SR; |
dgriffin65 | 0:77e050d1fb12 | 123 | tmpSn_SR = sreg<uint8_t>(socket, Sn_SR); |
dgriffin65 | 0:77e050d1fb12 | 124 | // packet sending is possible, when state is SOCK_CLOSE_WAIT. |
dgriffin65 | 0:77e050d1fb12 | 125 | if ((tmpSn_SR == SOCK_ESTABLISHED) || (tmpSn_SR == SOCK_CLOSE_WAIT)) { |
dgriffin65 | 0:77e050d1fb12 | 126 | return true; |
dgriffin65 | 0:77e050d1fb12 | 127 | } |
dgriffin65 | 0:77e050d1fb12 | 128 | return false; |
dgriffin65 | 0:77e050d1fb12 | 129 | } |
dgriffin65 | 0:77e050d1fb12 | 130 | |
dgriffin65 | 0:77e050d1fb12 | 131 | // Reset the chip & set the buffer |
dgriffin65 | 0:77e050d1fb12 | 132 | void WIZnet_Chip::reset() |
dgriffin65 | 0:77e050d1fb12 | 133 | { |
dgriffin65 | 0:77e050d1fb12 | 134 | reset_pin = 1; |
dgriffin65 | 0:77e050d1fb12 | 135 | reset_pin = 0; |
dgriffin65 | 0:77e050d1fb12 | 136 | wait_us(500); // 500us (w5500) |
dgriffin65 | 0:77e050d1fb12 | 137 | reset_pin = 1; |
dgriffin65 | 0:77e050d1fb12 | 138 | wait_ms(400); // 400ms (w5500) |
dgriffin65 | 0:77e050d1fb12 | 139 | |
dgriffin65 | 0:77e050d1fb12 | 140 | #if defined(USE_WIZ550IO_MAC) |
dgriffin65 | 0:77e050d1fb12 | 141 | //reg_rd_mac(SHAR, mac); // read the MAC address inside the module |
dgriffin65 | 0:77e050d1fb12 | 142 | #endif |
dgriffin65 | 0:77e050d1fb12 | 143 | |
dgriffin65 | 0:77e050d1fb12 | 144 | //reg_wr_mac(SHAR, mac); |
dgriffin65 | 0:77e050d1fb12 | 145 | |
dgriffin65 | 0:77e050d1fb12 | 146 | // set RX and TX buffer size |
dgriffin65 | 0:77e050d1fb12 | 147 | for (int socket = 0; socket < MAX_SOCK_NUM; socket++) { |
dgriffin65 | 0:77e050d1fb12 | 148 | sreg<uint8_t>(socket, Sn_RXBUF_SIZE, 2); |
dgriffin65 | 0:77e050d1fb12 | 149 | sreg<uint8_t>(socket, Sn_TXBUF_SIZE, 2); |
dgriffin65 | 0:77e050d1fb12 | 150 | } |
dgriffin65 | 0:77e050d1fb12 | 151 | } |
dgriffin65 | 0:77e050d1fb12 | 152 | |
dgriffin65 | 0:77e050d1fb12 | 153 | |
dgriffin65 | 0:77e050d1fb12 | 154 | bool WIZnet_Chip::close(int socket) |
dgriffin65 | 0:77e050d1fb12 | 155 | { |
dgriffin65 | 0:77e050d1fb12 | 156 | if (socket < 0) { |
dgriffin65 | 0:77e050d1fb12 | 157 | return false; |
dgriffin65 | 0:77e050d1fb12 | 158 | } |
dgriffin65 | 0:77e050d1fb12 | 159 | // if not connected, return |
dgriffin65 | 0:77e050d1fb12 | 160 | if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) { |
dgriffin65 | 0:77e050d1fb12 | 161 | return true; |
dgriffin65 | 0:77e050d1fb12 | 162 | } |
dgriffin65 | 0:77e050d1fb12 | 163 | if (sreg<uint8_t>(socket, Sn_MR) == TCP) { |
dgriffin65 | 0:77e050d1fb12 | 164 | scmd(socket, DISCON); |
dgriffin65 | 0:77e050d1fb12 | 165 | } |
dgriffin65 | 0:77e050d1fb12 | 166 | scmd(socket, CLOSE); |
dgriffin65 | 0:77e050d1fb12 | 167 | sreg<uint8_t>(socket, Sn_IR, 0xff); |
dgriffin65 | 0:77e050d1fb12 | 168 | return true; |
dgriffin65 | 0:77e050d1fb12 | 169 | } |
dgriffin65 | 0:77e050d1fb12 | 170 | |
dgriffin65 | 0:77e050d1fb12 | 171 | int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size) |
dgriffin65 | 0:77e050d1fb12 | 172 | { |
dgriffin65 | 0:77e050d1fb12 | 173 | if (socket < 0) { |
dgriffin65 | 0:77e050d1fb12 | 174 | return -1; |
dgriffin65 | 0:77e050d1fb12 | 175 | } |
dgriffin65 | 0:77e050d1fb12 | 176 | |
dgriffin65 | 0:77e050d1fb12 | 177 | Timer t; |
dgriffin65 | 0:77e050d1fb12 | 178 | t.reset(); |
dgriffin65 | 0:77e050d1fb12 | 179 | t.start(); |
dgriffin65 | 0:77e050d1fb12 | 180 | while(1) { |
dgriffin65 | 0:77e050d1fb12 | 181 | //int size = sreg<uint16_t>(socket, Sn_RX_RSR); |
dgriffin65 | 0:77e050d1fb12 | 182 | int size1, size2; |
dgriffin65 | 0:77e050d1fb12 | 183 | // during the reading Sn_RX_RSR, it has the possible change of this register. |
dgriffin65 | 0:77e050d1fb12 | 184 | // so read twice and get same value then use size information. |
dgriffin65 | 1:2dee44ea52a9 | 185 | while (1) { |
dgriffin65 | 0:77e050d1fb12 | 186 | size1 = sreg<uint16_t>(socket, Sn_RX_RSR); |
dgriffin65 | 0:77e050d1fb12 | 187 | size2 = sreg<uint16_t>(socket, Sn_RX_RSR); |
dgriffin65 | 1:2dee44ea52a9 | 188 | |
dgriffin65 | 1:2dee44ea52a9 | 189 | if (size1 == size2) { |
dgriffin65 | 1:2dee44ea52a9 | 190 | break; |
dgriffin65 | 1:2dee44ea52a9 | 191 | } |
dgriffin65 | 0:77e050d1fb12 | 192 | |
dgriffin65 | 0:77e050d1fb12 | 193 | if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { |
dgriffin65 | 0:77e050d1fb12 | 194 | return NSAPI_ERROR_WOULD_BLOCK; |
dgriffin65 | 0:77e050d1fb12 | 195 | } |
dgriffin65 | 1:2dee44ea52a9 | 196 | |
dgriffin65 | 1:2dee44ea52a9 | 197 | if (!is_connected(socket)) { |
dgriffin65 | 1:2dee44ea52a9 | 198 | return -1; |
dgriffin65 | 1:2dee44ea52a9 | 199 | } |
dgriffin65 | 1:2dee44ea52a9 | 200 | } |
dgriffin65 | 0:77e050d1fb12 | 201 | |
dgriffin65 | 0:77e050d1fb12 | 202 | if (size1 > req_size) { |
dgriffin65 | 0:77e050d1fb12 | 203 | return size1; |
dgriffin65 | 0:77e050d1fb12 | 204 | } |
dgriffin65 | 0:77e050d1fb12 | 205 | if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { |
dgriffin65 | 0:77e050d1fb12 | 206 | break; |
dgriffin65 | 0:77e050d1fb12 | 207 | } |
dgriffin65 | 0:77e050d1fb12 | 208 | } |
dgriffin65 | 0:77e050d1fb12 | 209 | return NSAPI_ERROR_WOULD_BLOCK; |
dgriffin65 | 0:77e050d1fb12 | 210 | } |
dgriffin65 | 0:77e050d1fb12 | 211 | |
dgriffin65 | 0:77e050d1fb12 | 212 | int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size) |
dgriffin65 | 0:77e050d1fb12 | 213 | { |
dgriffin65 | 0:77e050d1fb12 | 214 | if (socket < 0) { |
dgriffin65 | 0:77e050d1fb12 | 215 | return -1; |
dgriffin65 | 0:77e050d1fb12 | 216 | } |
dgriffin65 | 0:77e050d1fb12 | 217 | Timer t; |
dgriffin65 | 0:77e050d1fb12 | 218 | t.reset(); |
dgriffin65 | 0:77e050d1fb12 | 219 | t.start(); |
dgriffin65 | 0:77e050d1fb12 | 220 | while(1) { |
dgriffin65 | 0:77e050d1fb12 | 221 | //int size = sreg<uint16_t>(socket, Sn_TX_FSR); |
dgriffin65 | 0:77e050d1fb12 | 222 | int size1, size2; |
dgriffin65 | 0:77e050d1fb12 | 223 | // during the reading Sn_TX_FSR, it has the possible change of this register. |
dgriffin65 | 0:77e050d1fb12 | 224 | // so read twice and get same value then use size information. |
dgriffin65 | 0:77e050d1fb12 | 225 | do { |
dgriffin65 | 0:77e050d1fb12 | 226 | size1 = sreg<uint16_t>(socket, Sn_TX_FSR); |
dgriffin65 | 0:77e050d1fb12 | 227 | size2 = sreg<uint16_t>(socket, Sn_TX_FSR); |
dgriffin65 | 0:77e050d1fb12 | 228 | if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { |
dgriffin65 | 0:77e050d1fb12 | 229 | return NSAPI_ERROR_WOULD_BLOCK; |
dgriffin65 | 0:77e050d1fb12 | 230 | } |
dgriffin65 | 0:77e050d1fb12 | 231 | } while (size1 != size2); |
dgriffin65 | 0:77e050d1fb12 | 232 | if (size1 > req_size) { |
dgriffin65 | 0:77e050d1fb12 | 233 | return size1; |
dgriffin65 | 0:77e050d1fb12 | 234 | } |
dgriffin65 | 0:77e050d1fb12 | 235 | if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { |
dgriffin65 | 0:77e050d1fb12 | 236 | break; |
dgriffin65 | 0:77e050d1fb12 | 237 | } |
dgriffin65 | 0:77e050d1fb12 | 238 | } |
dgriffin65 | 0:77e050d1fb12 | 239 | return NSAPI_ERROR_WOULD_BLOCK; |
dgriffin65 | 0:77e050d1fb12 | 240 | } |
dgriffin65 | 0:77e050d1fb12 | 241 | |
dgriffin65 | 0:77e050d1fb12 | 242 | int WIZnet_Chip::send(int socket, const char * str, int len) |
dgriffin65 | 0:77e050d1fb12 | 243 | { |
dgriffin65 | 0:77e050d1fb12 | 244 | if (socket < 0) { |
dgriffin65 | 0:77e050d1fb12 | 245 | return -1; |
dgriffin65 | 0:77e050d1fb12 | 246 | } |
dgriffin65 | 0:77e050d1fb12 | 247 | uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR); |
dgriffin65 | 0:77e050d1fb12 | 248 | uint8_t cntl_byte = (0x14 + (socket << 5)); |
dgriffin65 | 0:77e050d1fb12 | 249 | spi_write(ptr, cntl_byte, (uint8_t*)str, len); |
dgriffin65 | 0:77e050d1fb12 | 250 | sreg<uint16_t>(socket, Sn_TX_WR, ptr + len); |
dgriffin65 | 0:77e050d1fb12 | 251 | scmd(socket, SEND); |
dgriffin65 | 0:77e050d1fb12 | 252 | uint8_t tmp_Sn_IR; |
dgriffin65 | 0:77e050d1fb12 | 253 | while (( (tmp_Sn_IR = sreg<uint8_t>(socket, Sn_IR)) & INT_SEND_OK) != INT_SEND_OK) { |
dgriffin65 | 0:77e050d1fb12 | 254 | // @Jul.10, 2014 fix contant name, and udp sendto function. |
dgriffin65 | 0:77e050d1fb12 | 255 | switch (sreg<uint8_t>(socket, Sn_SR)) { |
dgriffin65 | 0:77e050d1fb12 | 256 | case SOCK_CLOSED : |
dgriffin65 | 0:77e050d1fb12 | 257 | close(socket); |
dgriffin65 | 0:77e050d1fb12 | 258 | return 0; |
dgriffin65 | 0:77e050d1fb12 | 259 | //break; |
dgriffin65 | 0:77e050d1fb12 | 260 | case SOCK_UDP : |
dgriffin65 | 0:77e050d1fb12 | 261 | // ARP timeout is possible. |
dgriffin65 | 0:77e050d1fb12 | 262 | if ((tmp_Sn_IR & INT_TIMEOUT) == INT_TIMEOUT) { |
dgriffin65 | 0:77e050d1fb12 | 263 | sreg<uint8_t>(socket, Sn_IR, INT_TIMEOUT); |
dgriffin65 | 0:77e050d1fb12 | 264 | return 0; |
dgriffin65 | 0:77e050d1fb12 | 265 | } |
dgriffin65 | 0:77e050d1fb12 | 266 | break; |
dgriffin65 | 0:77e050d1fb12 | 267 | default : |
dgriffin65 | 0:77e050d1fb12 | 268 | break; |
dgriffin65 | 0:77e050d1fb12 | 269 | } |
dgriffin65 | 0:77e050d1fb12 | 270 | } |
dgriffin65 | 0:77e050d1fb12 | 271 | sreg<uint8_t>(socket, Sn_IR, INT_SEND_OK); |
dgriffin65 | 0:77e050d1fb12 | 272 | |
dgriffin65 | 0:77e050d1fb12 | 273 | return len; |
dgriffin65 | 0:77e050d1fb12 | 274 | } |
dgriffin65 | 0:77e050d1fb12 | 275 | |
dgriffin65 | 0:77e050d1fb12 | 276 | int WIZnet_Chip::recv(int socket, char* buf, int len) |
dgriffin65 | 0:77e050d1fb12 | 277 | { |
dgriffin65 | 0:77e050d1fb12 | 278 | if (socket < 0) { |
dgriffin65 | 0:77e050d1fb12 | 279 | return -1; |
dgriffin65 | 0:77e050d1fb12 | 280 | } |
dgriffin65 | 0:77e050d1fb12 | 281 | uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD); |
dgriffin65 | 0:77e050d1fb12 | 282 | uint8_t cntl_byte = (0x18 + (socket << 5)); |
dgriffin65 | 0:77e050d1fb12 | 283 | spi_read(ptr, cntl_byte, (uint8_t*)buf, len); |
dgriffin65 | 0:77e050d1fb12 | 284 | sreg<uint16_t>(socket, Sn_RX_RD, ptr + len); |
dgriffin65 | 0:77e050d1fb12 | 285 | scmd(socket, RECV); |
dgriffin65 | 0:77e050d1fb12 | 286 | return len; |
dgriffin65 | 0:77e050d1fb12 | 287 | } |
dgriffin65 | 0:77e050d1fb12 | 288 | |
dgriffin65 | 0:77e050d1fb12 | 289 | int WIZnet_Chip::new_socket() |
dgriffin65 | 0:77e050d1fb12 | 290 | { |
dgriffin65 | 0:77e050d1fb12 | 291 | for(int s = 0; s < MAX_SOCK_NUM; s++) { |
dgriffin65 | 0:77e050d1fb12 | 292 | if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) { |
dgriffin65 | 0:77e050d1fb12 | 293 | return s; |
dgriffin65 | 0:77e050d1fb12 | 294 | } |
dgriffin65 | 0:77e050d1fb12 | 295 | } |
dgriffin65 | 0:77e050d1fb12 | 296 | return -1; |
dgriffin65 | 0:77e050d1fb12 | 297 | } |
dgriffin65 | 0:77e050d1fb12 | 298 | |
dgriffin65 | 0:77e050d1fb12 | 299 | uint16_t WIZnet_Chip::new_port() |
dgriffin65 | 0:77e050d1fb12 | 300 | { |
dgriffin65 | 0:77e050d1fb12 | 301 | uint16_t port = rand(); |
dgriffin65 | 0:77e050d1fb12 | 302 | port |= 49152; |
dgriffin65 | 0:77e050d1fb12 | 303 | return port; |
dgriffin65 | 0:77e050d1fb12 | 304 | } |
dgriffin65 | 0:77e050d1fb12 | 305 | |
dgriffin65 | 0:77e050d1fb12 | 306 | void WIZnet_Chip::scmd(int socket, Command cmd) |
dgriffin65 | 0:77e050d1fb12 | 307 | { |
dgriffin65 | 0:77e050d1fb12 | 308 | sreg<uint8_t>(socket, Sn_CR, cmd); |
dgriffin65 | 0:77e050d1fb12 | 309 | while(sreg<uint8_t>(socket, Sn_CR)); |
dgriffin65 | 0:77e050d1fb12 | 310 | } |
dgriffin65 | 0:77e050d1fb12 | 311 | |
dgriffin65 | 0:77e050d1fb12 | 312 | void WIZnet_Chip::spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len) |
dgriffin65 | 0:77e050d1fb12 | 313 | { |
dgriffin65 | 1:2dee44ea52a9 | 314 | spi->lock(); |
dgriffin65 | 0:77e050d1fb12 | 315 | cs = 0; |
dgriffin65 | 0:77e050d1fb12 | 316 | spi->write(addr >> 8); |
dgriffin65 | 0:77e050d1fb12 | 317 | spi->write(addr & 0xff); |
dgriffin65 | 0:77e050d1fb12 | 318 | spi->write(cb); |
dgriffin65 | 0:77e050d1fb12 | 319 | for(int i = 0; i < len; i++) { |
dgriffin65 | 0:77e050d1fb12 | 320 | spi->write(buf[i]); |
dgriffin65 | 0:77e050d1fb12 | 321 | } |
dgriffin65 | 0:77e050d1fb12 | 322 | cs = 1; |
dgriffin65 | 0:77e050d1fb12 | 323 | |
dgriffin65 | 0:77e050d1fb12 | 324 | #if DBG_SPI |
dgriffin65 | 0:77e050d1fb12 | 325 | debug("[SPI]W %04x(%02x %d)", addr, cb, len); |
dgriffin65 | 0:77e050d1fb12 | 326 | for(int i = 0; i < len; i++) { |
dgriffin65 | 0:77e050d1fb12 | 327 | debug(" %02x", buf[i]); |
dgriffin65 | 0:77e050d1fb12 | 328 | if (i > 16) { |
dgriffin65 | 0:77e050d1fb12 | 329 | debug(" ..."); |
dgriffin65 | 0:77e050d1fb12 | 330 | break; |
dgriffin65 | 0:77e050d1fb12 | 331 | } |
dgriffin65 | 0:77e050d1fb12 | 332 | } |
dgriffin65 | 0:77e050d1fb12 | 333 | debug("\r\n"); |
dgriffin65 | 0:77e050d1fb12 | 334 | #endif |
dgriffin65 | 1:2dee44ea52a9 | 335 | spi->unlock(); |
dgriffin65 | 1:2dee44ea52a9 | 336 | |
dgriffin65 | 0:77e050d1fb12 | 337 | } |
dgriffin65 | 0:77e050d1fb12 | 338 | |
dgriffin65 | 0:77e050d1fb12 | 339 | void WIZnet_Chip::spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len) |
dgriffin65 | 0:77e050d1fb12 | 340 | { |
dgriffin65 | 1:2dee44ea52a9 | 341 | spi->lock(); |
dgriffin65 | 0:77e050d1fb12 | 342 | cs = 0; |
dgriffin65 | 0:77e050d1fb12 | 343 | spi->write(addr >> 8); |
dgriffin65 | 0:77e050d1fb12 | 344 | spi->write(addr & 0xff); |
dgriffin65 | 0:77e050d1fb12 | 345 | spi->write(cb); |
dgriffin65 | 0:77e050d1fb12 | 346 | for(int i = 0; i < len; i++) { |
dgriffin65 | 0:77e050d1fb12 | 347 | buf[i] = spi->write(0); |
dgriffin65 | 0:77e050d1fb12 | 348 | } |
dgriffin65 | 0:77e050d1fb12 | 349 | cs = 1; |
dgriffin65 | 0:77e050d1fb12 | 350 | |
dgriffin65 | 0:77e050d1fb12 | 351 | #if DBG_SPI |
dgriffin65 | 0:77e050d1fb12 | 352 | debug("[SPI]R %04x(%02x %d)", addr, cb, len); |
dgriffin65 | 0:77e050d1fb12 | 353 | for(int i = 0; i < len; i++) { |
dgriffin65 | 0:77e050d1fb12 | 354 | debug(" %02x", buf[i]); |
dgriffin65 | 0:77e050d1fb12 | 355 | if (i > 16) { |
dgriffin65 | 0:77e050d1fb12 | 356 | debug(" ..."); |
dgriffin65 | 0:77e050d1fb12 | 357 | break; |
dgriffin65 | 0:77e050d1fb12 | 358 | } |
dgriffin65 | 0:77e050d1fb12 | 359 | } |
dgriffin65 | 0:77e050d1fb12 | 360 | debug("\r\n"); |
dgriffin65 | 0:77e050d1fb12 | 361 | if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) { |
dgriffin65 | 0:77e050d1fb12 | 362 | wait_ms(200); |
dgriffin65 | 0:77e050d1fb12 | 363 | } |
dgriffin65 | 0:77e050d1fb12 | 364 | #endif |
dgriffin65 | 1:2dee44ea52a9 | 365 | spi->unlock(); |
dgriffin65 | 0:77e050d1fb12 | 366 | } |
dgriffin65 | 0:77e050d1fb12 | 367 | |
dgriffin65 | 0:77e050d1fb12 | 368 | uint32_t str_to_ip(const char* str) |
dgriffin65 | 0:77e050d1fb12 | 369 | { |
dgriffin65 | 0:77e050d1fb12 | 370 | uint32_t ip = 0; |
dgriffin65 | 0:77e050d1fb12 | 371 | char* p = (char*)str; |
dgriffin65 | 0:77e050d1fb12 | 372 | for(int i = 0; i < 4; i++) { |
dgriffin65 | 0:77e050d1fb12 | 373 | ip |= atoi(p); |
dgriffin65 | 0:77e050d1fb12 | 374 | p = strchr(p, '.'); |
dgriffin65 | 0:77e050d1fb12 | 375 | if (p == NULL) { |
dgriffin65 | 0:77e050d1fb12 | 376 | break; |
dgriffin65 | 0:77e050d1fb12 | 377 | } |
dgriffin65 | 0:77e050d1fb12 | 378 | ip <<= 8; |
dgriffin65 | 0:77e050d1fb12 | 379 | p++; |
dgriffin65 | 0:77e050d1fb12 | 380 | } |
dgriffin65 | 0:77e050d1fb12 | 381 | return ip; |
dgriffin65 | 0:77e050d1fb12 | 382 | } |
dgriffin65 | 0:77e050d1fb12 | 383 | |
dgriffin65 | 0:77e050d1fb12 | 384 | void printfBytes(char* str, uint8_t* buf, int len) |
dgriffin65 | 0:77e050d1fb12 | 385 | { |
dgriffin65 | 0:77e050d1fb12 | 386 | printf("%s %d:", str, len); |
dgriffin65 | 0:77e050d1fb12 | 387 | for(int i = 0; i < len; i++) { |
dgriffin65 | 0:77e050d1fb12 | 388 | printf(" %02x", buf[i]); |
dgriffin65 | 0:77e050d1fb12 | 389 | } |
dgriffin65 | 0:77e050d1fb12 | 390 | printf("\n"); |
dgriffin65 | 0:77e050d1fb12 | 391 | } |
dgriffin65 | 0:77e050d1fb12 | 392 | |
dgriffin65 | 0:77e050d1fb12 | 393 | void printHex(uint8_t* buf, int len) |
dgriffin65 | 0:77e050d1fb12 | 394 | { |
dgriffin65 | 0:77e050d1fb12 | 395 | for(int i = 0; i < len; i++) { |
dgriffin65 | 0:77e050d1fb12 | 396 | if ((i%16) == 0) { |
dgriffin65 | 0:77e050d1fb12 | 397 | printf("%p", buf+i); |
dgriffin65 | 0:77e050d1fb12 | 398 | } |
dgriffin65 | 0:77e050d1fb12 | 399 | printf(" %02x", buf[i]); |
dgriffin65 | 0:77e050d1fb12 | 400 | if ((i%16) == 15) { |
dgriffin65 | 0:77e050d1fb12 | 401 | printf("\n"); |
dgriffin65 | 0:77e050d1fb12 | 402 | } |
dgriffin65 | 0:77e050d1fb12 | 403 | } |
dgriffin65 | 0:77e050d1fb12 | 404 | printf("\n"); |
dgriffin65 | 0:77e050d1fb12 | 405 | } |
dgriffin65 | 0:77e050d1fb12 | 406 | |
dgriffin65 | 0:77e050d1fb12 | 407 | void debug_hex(uint8_t* buf, int len) |
dgriffin65 | 0:77e050d1fb12 | 408 | { |
dgriffin65 | 0:77e050d1fb12 | 409 | for(int i = 0; i < len; i++) { |
dgriffin65 | 0:77e050d1fb12 | 410 | if ((i%16) == 0) { |
dgriffin65 | 0:77e050d1fb12 | 411 | debug("%p", buf+i); |
dgriffin65 | 0:77e050d1fb12 | 412 | } |
dgriffin65 | 0:77e050d1fb12 | 413 | debug(" %02x", buf[i]); |
dgriffin65 | 0:77e050d1fb12 | 414 | if ((i%16) == 15) { |
dgriffin65 | 0:77e050d1fb12 | 415 | debug("\n"); |
dgriffin65 | 0:77e050d1fb12 | 416 | } |
dgriffin65 | 0:77e050d1fb12 | 417 | } |
dgriffin65 | 0:77e050d1fb12 | 418 | debug("\n"); |
dgriffin65 | 0:77e050d1fb12 | 419 | } |
dgriffin65 | 0:77e050d1fb12 | 420 | |
dgriffin65 | 0:77e050d1fb12 | 421 | #endif |