Fork of original library, increased Tx buffer to 16kB

Dependents:   W5500-SNTPClient-example

Committer:
star297
Date:
Thu May 02 20:47:25 2019 +0000
Revision:
0:e9275bdfa393
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:e9275bdfa393 1 // EthernetInterface for W5500 2014/8/20
star297 0:e9275bdfa393 2 /*
star297 0:e9275bdfa393 3 // sample usgae.
star297 0:e9275bdfa393 4 // copy below code block to main code.
star297 0:e9275bdfa393 5
star297 0:e9275bdfa393 6 #if defined(TARGET_LPC1114)
star297 0:e9275bdfa393 7 SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
star297 0:e9275bdfa393 8 EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset
star297 0:e9275bdfa393 9 wait(1); // 1 second for stable state
star297 0:e9275bdfa393 10 #elif defined(TARGET_LPC1768)
star297 0:e9275bdfa393 11 SPI spi(p11, p12, p13); // mosi, miso, sclk
star297 0:e9275bdfa393 12 EthernetInterface eth(&spi, p14, p15); // spi, cs, reset
star297 0:e9275bdfa393 13 wait(1); // 1 second for stable state
star297 0:e9275bdfa393 14 #elif defined(TARGET_LPC11U68)
star297 0:e9275bdfa393 15 SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
star297 0:e9275bdfa393 16 EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
star297 0:e9275bdfa393 17 spi.format(8,0); // 8bit, mode 0
star297 0:e9275bdfa393 18 spi.frequency(7000000); // 7MHz
star297 0:e9275bdfa393 19 wait(1); // 1 second for stable state
star297 0:e9275bdfa393 20 #endif
star297 0:e9275bdfa393 21
star297 0:e9275bdfa393 22 eth.init(); //Use DHCP
star297 0:e9275bdfa393 23 dbg.printf("init\r\n");
star297 0:e9275bdfa393 24 eth.connect();
star297 0:e9275bdfa393 25 dbg.printf("IP address: %s\r\n", eth.getIPAddress());
star297 0:e9275bdfa393 26
star297 0:e9275bdfa393 27 */
star297 0:e9275bdfa393 28
star297 0:e9275bdfa393 29 #include "EthernetInterface.h"
star297 0:e9275bdfa393 30 #include "DHCPClient.h"
star297 0:e9275bdfa393 31 //static char ip_string[17];
star297 0:e9275bdfa393 32 //static uint32_t ipaddr ;
star297 0:e9275bdfa393 33 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
star297 0:e9275bdfa393 34 WIZnet_Chip(mosi, miso, sclk, cs, reset)
star297 0:e9275bdfa393 35 {
star297 0:e9275bdfa393 36 ip_set = false;
star297 0:e9275bdfa393 37 }
star297 0:e9275bdfa393 38
star297 0:e9275bdfa393 39 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) :
star297 0:e9275bdfa393 40 WIZnet_Chip(spi, cs, reset)
star297 0:e9275bdfa393 41 {
star297 0:e9275bdfa393 42 ip_set = false;
star297 0:e9275bdfa393 43 }
star297 0:e9275bdfa393 44
star297 0:e9275bdfa393 45
star297 0:e9275bdfa393 46 int EthernetInterface::init()
star297 0:e9275bdfa393 47 {
star297 0:e9275bdfa393 48 dhcp = true;
star297 0:e9275bdfa393 49 //
star297 0:e9275bdfa393 50 //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
star297 0:e9275bdfa393 51 //
star297 0:e9275bdfa393 52 reset();
star297 0:e9275bdfa393 53 return 0;
star297 0:e9275bdfa393 54 }
star297 0:e9275bdfa393 55
star297 0:e9275bdfa393 56 int EthernetInterface::init(uint8_t * mac)
star297 0:e9275bdfa393 57 {
star297 0:e9275bdfa393 58 dhcp = true;
star297 0:e9275bdfa393 59 //
star297 0:e9275bdfa393 60 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
star297 0:e9275bdfa393 61 //
star297 0:e9275bdfa393 62 reset();
star297 0:e9275bdfa393 63 setmac();
star297 0:e9275bdfa393 64 return 0;
star297 0:e9275bdfa393 65 }
star297 0:e9275bdfa393 66
star297 0:e9275bdfa393 67 // add this function, because sometimes no needed MAC address in init calling.
star297 0:e9275bdfa393 68 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway)
star297 0:e9275bdfa393 69 {
star297 0:e9275bdfa393 70 dhcp = false;
star297 0:e9275bdfa393 71 //
star297 0:e9275bdfa393 72 //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
star297 0:e9275bdfa393 73 //
star297 0:e9275bdfa393 74 this->ip = str_to_ip(ip);
star297 0:e9275bdfa393 75 strcpy(ip_string, ip);
star297 0:e9275bdfa393 76 ip_set = true;
star297 0:e9275bdfa393 77 this->netmask = str_to_ip(mask);
star297 0:e9275bdfa393 78 this->gateway = str_to_ip(gateway);
star297 0:e9275bdfa393 79 reset();
star297 0:e9275bdfa393 80
star297 0:e9275bdfa393 81 // @Jul. 8. 2014 add code. should be called to write chip.
star297 0:e9275bdfa393 82 setip();
star297 0:e9275bdfa393 83
star297 0:e9275bdfa393 84 return 0;
star297 0:e9275bdfa393 85 }
star297 0:e9275bdfa393 86
star297 0:e9275bdfa393 87 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
star297 0:e9275bdfa393 88 {
star297 0:e9275bdfa393 89 dhcp = false;
star297 0:e9275bdfa393 90 //
star297 0:e9275bdfa393 91 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
star297 0:e9275bdfa393 92 //
star297 0:e9275bdfa393 93 this->ip = str_to_ip(ip);
star297 0:e9275bdfa393 94 strcpy(ip_string, ip);
star297 0:e9275bdfa393 95 ip_set = true;
star297 0:e9275bdfa393 96 this->netmask = str_to_ip(mask);
star297 0:e9275bdfa393 97 this->gateway = str_to_ip(gateway);
star297 0:e9275bdfa393 98 reset();
star297 0:e9275bdfa393 99
star297 0:e9275bdfa393 100 // @Jul. 8. 2014 add code. should be called to write chip.
star297 0:e9275bdfa393 101 setmac();
star297 0:e9275bdfa393 102 setip();
star297 0:e9275bdfa393 103
star297 0:e9275bdfa393 104 return 0;
star297 0:e9275bdfa393 105 }
star297 0:e9275bdfa393 106
star297 0:e9275bdfa393 107 // Connect Bring the interface up, start DHCP if needed.
star297 0:e9275bdfa393 108 int EthernetInterface::connect()
star297 0:e9275bdfa393 109 {
star297 0:e9275bdfa393 110 if (dhcp) {
star297 0:e9275bdfa393 111 int r = IPrenew();
star297 0:e9275bdfa393 112 if (r < 0) {
star297 0:e9275bdfa393 113 return r;
star297 0:e9275bdfa393 114 }
star297 0:e9275bdfa393 115 }
star297 0:e9275bdfa393 116
star297 0:e9275bdfa393 117 if (WIZnet_Chip::setip() == false) return -1;
star297 0:e9275bdfa393 118 return 0;
star297 0:e9275bdfa393 119 }
star297 0:e9275bdfa393 120
star297 0:e9275bdfa393 121 // Disconnect Bring the interface down.
star297 0:e9275bdfa393 122 int EthernetInterface::disconnect()
star297 0:e9275bdfa393 123 {
star297 0:e9275bdfa393 124 if (WIZnet_Chip::disconnect() == false) return -1;
star297 0:e9275bdfa393 125 return 0;
star297 0:e9275bdfa393 126 }
star297 0:e9275bdfa393 127
star297 0:e9275bdfa393 128 /*
star297 0:e9275bdfa393 129 void EthernetInterface::getip(void)
star297 0:e9275bdfa393 130 {
star297 0:e9275bdfa393 131 //ip = 0x12345678;
star297 0:e9275bdfa393 132 ipaddr = reg_rd<uint32_t>(SIPR);
star297 0:e9275bdfa393 133 //snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ipaddr>>24)&0xff, (ipaddr>>16)&0xff, (ipaddr>>8)&0xff, ipaddr&0xff);
star297 0:e9275bdfa393 134 }
star297 0:e9275bdfa393 135 */
star297 0:e9275bdfa393 136 char* EthernetInterface::getIPAddress()
star297 0:e9275bdfa393 137 {
star297 0:e9275bdfa393 138 //Suint32_t ip = inet_getip(SIPR);//reg_rd<uint32_t>(SIPR);
star297 0:e9275bdfa393 139 //static uint32_t ip = getip();
star297 0:e9275bdfa393 140 //uint32_t ip = getip();
star297 0:e9275bdfa393 141
star297 0:e9275bdfa393 142 uint32_t ip = reg_rd<uint32_t>(SIPR);
star297 0:e9275bdfa393 143 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
star297 0:e9275bdfa393 144 return ip_string;
star297 0:e9275bdfa393 145 }
star297 0:e9275bdfa393 146
star297 0:e9275bdfa393 147 char* EthernetInterface::getNetworkMask()
star297 0:e9275bdfa393 148 {
star297 0:e9275bdfa393 149 uint32_t ip = reg_rd<uint32_t>(SUBR);
star297 0:e9275bdfa393 150 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
star297 0:e9275bdfa393 151 return mask_string;
star297 0:e9275bdfa393 152 }
star297 0:e9275bdfa393 153
star297 0:e9275bdfa393 154 char* EthernetInterface::getGateway()
star297 0:e9275bdfa393 155 {
star297 0:e9275bdfa393 156 uint32_t ip = reg_rd<uint32_t>(GAR);
star297 0:e9275bdfa393 157 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
star297 0:e9275bdfa393 158 return gw_string;
star297 0:e9275bdfa393 159 }
star297 0:e9275bdfa393 160
star297 0:e9275bdfa393 161 char* EthernetInterface::getMACAddress()
star297 0:e9275bdfa393 162 {
star297 0:e9275bdfa393 163 uint8_t mac[6];
star297 0:e9275bdfa393 164 reg_rd_mac(SHAR, mac);
star297 0:e9275bdfa393 165 snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
star297 0:e9275bdfa393 166 return mac_string;
star297 0:e9275bdfa393 167 }
star297 0:e9275bdfa393 168
star297 0:e9275bdfa393 169 int EthernetInterface::IPrenew(int timeout_ms)
star297 0:e9275bdfa393 170 {
star297 0:e9275bdfa393 171 // printf("DHCP Started, waiting for IP...\n");
star297 0:e9275bdfa393 172 DHCPClient dhcp;
star297 0:e9275bdfa393 173 int err = dhcp.setup(timeout_ms);
star297 0:e9275bdfa393 174 if (err == (-1)) {
star297 0:e9275bdfa393 175 // printf("Timeout.\n");
star297 0:e9275bdfa393 176 return -1;
star297 0:e9275bdfa393 177 }
star297 0:e9275bdfa393 178 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
star297 0:e9275bdfa393 179 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
star297 0:e9275bdfa393 180 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
star297 0:e9275bdfa393 181 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
star297 0:e9275bdfa393 182 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
star297 0:e9275bdfa393 183 return 0;
star297 0:e9275bdfa393 184 }