MQTT client test with W5200 ethernet shield

Dependents:   IBMIoTClientEthernetExample_W5200

Fork of W5500Interface by W5500-Ethernet-Interface Makers

Committer:
Bongjun
Date:
Wed Aug 20 01:55:36 2014 +0000
Revision:
1:8f4374f932b4
Parent:
0:e11e8793c3ce
Child:
5:8aefaef88f79
comment for usage to EthernetInterface.cpp

Who changed what in which revision?

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