M2X Ethernet demo using Seeed Ethernet W5200 Shield

Dependencies:   LM75B M2XStreamClient jsonlite mbed-rtos mbed Nucleo_Sensor_Shield

Fork of m2x-seeed_ethernet_demo by Sean Newton

Committer:
dangriffin
Date:
Wed Dec 17 20:45:13 2014 +0000
Revision:
11:40d8cfc941ed
Parent:
7:a94ba2e0cd04
Use the public Nucleo Sensor Shield library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SeanNewton 7:a94ba2e0cd04 1 /* Copyright (C) 2012 mbed.org, MIT License
SeanNewton 7:a94ba2e0cd04 2 *
SeanNewton 7:a94ba2e0cd04 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
SeanNewton 7:a94ba2e0cd04 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
SeanNewton 7:a94ba2e0cd04 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
SeanNewton 7:a94ba2e0cd04 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
SeanNewton 7:a94ba2e0cd04 7 * furnished to do so, subject to the following conditions:
SeanNewton 7:a94ba2e0cd04 8 *
SeanNewton 7:a94ba2e0cd04 9 * The above copyright notice and this permission notice shall be included in all copies or
SeanNewton 7:a94ba2e0cd04 10 * substantial portions of the Software.
SeanNewton 7:a94ba2e0cd04 11 *
SeanNewton 7:a94ba2e0cd04 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
SeanNewton 7:a94ba2e0cd04 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
SeanNewton 7:a94ba2e0cd04 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
SeanNewton 7:a94ba2e0cd04 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SeanNewton 7:a94ba2e0cd04 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SeanNewton 7:a94ba2e0cd04 17 */
SeanNewton 7:a94ba2e0cd04 18
SeanNewton 7:a94ba2e0cd04 19 #include "WIZnetInterface.h"
SeanNewton 7:a94ba2e0cd04 20 #include "DHCPClient.h"
SeanNewton 7:a94ba2e0cd04 21
SeanNewton 7:a94ba2e0cd04 22 WIZnetInterface::WIZnetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
SeanNewton 7:a94ba2e0cd04 23 WIZnet_Chip(mosi, miso, sclk, cs, reset)
SeanNewton 7:a94ba2e0cd04 24 {
SeanNewton 7:a94ba2e0cd04 25 ip_set = false;
SeanNewton 7:a94ba2e0cd04 26 }
SeanNewton 7:a94ba2e0cd04 27
SeanNewton 7:a94ba2e0cd04 28 WIZnetInterface::WIZnetInterface(SPI* spi, PinName cs, PinName reset) :
SeanNewton 7:a94ba2e0cd04 29 WIZnet_Chip(spi, cs, reset)
SeanNewton 7:a94ba2e0cd04 30 {
SeanNewton 7:a94ba2e0cd04 31 ip_set = false;
SeanNewton 7:a94ba2e0cd04 32 }
SeanNewton 7:a94ba2e0cd04 33
SeanNewton 7:a94ba2e0cd04 34 int WIZnetInterface::init(uint8_t * mac)
SeanNewton 7:a94ba2e0cd04 35 {
SeanNewton 7:a94ba2e0cd04 36 dhcp = true;
SeanNewton 7:a94ba2e0cd04 37 //
SeanNewton 7:a94ba2e0cd04 38 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
SeanNewton 7:a94ba2e0cd04 39 //
SeanNewton 7:a94ba2e0cd04 40 reset();
SeanNewton 7:a94ba2e0cd04 41 return 0;
SeanNewton 7:a94ba2e0cd04 42 }
SeanNewton 7:a94ba2e0cd04 43
SeanNewton 7:a94ba2e0cd04 44 int WIZnetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
SeanNewton 7:a94ba2e0cd04 45 {
SeanNewton 7:a94ba2e0cd04 46 dhcp = false;
SeanNewton 7:a94ba2e0cd04 47 //
SeanNewton 7:a94ba2e0cd04 48 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
SeanNewton 7:a94ba2e0cd04 49 //
SeanNewton 7:a94ba2e0cd04 50 this->ip = str_to_ip(ip);
SeanNewton 7:a94ba2e0cd04 51 strcpy(ip_string, ip);
SeanNewton 7:a94ba2e0cd04 52 ip_set = true;
SeanNewton 7:a94ba2e0cd04 53 this->netmask = str_to_ip(mask);
SeanNewton 7:a94ba2e0cd04 54 this->gateway = str_to_ip(gateway);
SeanNewton 7:a94ba2e0cd04 55 reset();
SeanNewton 7:a94ba2e0cd04 56 return 0;
SeanNewton 7:a94ba2e0cd04 57 }
SeanNewton 7:a94ba2e0cd04 58
SeanNewton 7:a94ba2e0cd04 59 // Connect Bring the interface up, start DHCP if needed.
SeanNewton 7:a94ba2e0cd04 60 int WIZnetInterface::connect()
SeanNewton 7:a94ba2e0cd04 61 {
SeanNewton 7:a94ba2e0cd04 62 if (dhcp) {
SeanNewton 7:a94ba2e0cd04 63 int r = IPrenew();
SeanNewton 7:a94ba2e0cd04 64 if (r < 0) {
SeanNewton 7:a94ba2e0cd04 65 return r;
SeanNewton 7:a94ba2e0cd04 66 }
SeanNewton 7:a94ba2e0cd04 67 }
SeanNewton 7:a94ba2e0cd04 68
SeanNewton 7:a94ba2e0cd04 69 if (WIZnet_Chip::setip() == false) return -1;
SeanNewton 7:a94ba2e0cd04 70 return 0;
SeanNewton 7:a94ba2e0cd04 71 }
SeanNewton 7:a94ba2e0cd04 72
SeanNewton 7:a94ba2e0cd04 73 // Disconnect Bring the interface down.
SeanNewton 7:a94ba2e0cd04 74 int WIZnetInterface::disconnect()
SeanNewton 7:a94ba2e0cd04 75 {
SeanNewton 7:a94ba2e0cd04 76 if (WIZnet_Chip::disconnect() == false) return -1;
SeanNewton 7:a94ba2e0cd04 77 return 0;
SeanNewton 7:a94ba2e0cd04 78 }
SeanNewton 7:a94ba2e0cd04 79
SeanNewton 7:a94ba2e0cd04 80 char* WIZnetInterface::getIPAddress()
SeanNewton 7:a94ba2e0cd04 81 {
SeanNewton 7:a94ba2e0cd04 82 uint32_t ip = reg_rd<uint32_t>(SIPR);
SeanNewton 7:a94ba2e0cd04 83 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
SeanNewton 7:a94ba2e0cd04 84 return ip_string;
SeanNewton 7:a94ba2e0cd04 85 }
SeanNewton 7:a94ba2e0cd04 86
SeanNewton 7:a94ba2e0cd04 87 char* WIZnetInterface::getNetworkMask()
SeanNewton 7:a94ba2e0cd04 88 {
SeanNewton 7:a94ba2e0cd04 89 uint32_t ip = reg_rd<uint32_t>(SUBR);
SeanNewton 7:a94ba2e0cd04 90 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
SeanNewton 7:a94ba2e0cd04 91 return mask_string;
SeanNewton 7:a94ba2e0cd04 92 }
SeanNewton 7:a94ba2e0cd04 93
SeanNewton 7:a94ba2e0cd04 94 char* WIZnetInterface::getGateway()
SeanNewton 7:a94ba2e0cd04 95 {
SeanNewton 7:a94ba2e0cd04 96 uint32_t ip = reg_rd<uint32_t>(GAR);
SeanNewton 7:a94ba2e0cd04 97 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
SeanNewton 7:a94ba2e0cd04 98 return gw_string;
SeanNewton 7:a94ba2e0cd04 99 }
SeanNewton 7:a94ba2e0cd04 100
SeanNewton 7:a94ba2e0cd04 101 char* WIZnetInterface::getMACAddress()
SeanNewton 7:a94ba2e0cd04 102 {
SeanNewton 7:a94ba2e0cd04 103 uint8_t mac[6];
SeanNewton 7:a94ba2e0cd04 104 reg_rd_mac(SHAR, mac);
SeanNewton 7:a94ba2e0cd04 105 snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
SeanNewton 7:a94ba2e0cd04 106 return mac_string;
SeanNewton 7:a94ba2e0cd04 107
SeanNewton 7:a94ba2e0cd04 108 }
SeanNewton 7:a94ba2e0cd04 109
SeanNewton 7:a94ba2e0cd04 110 int WIZnetInterface::IPrenew(int timeout_ms)
SeanNewton 7:a94ba2e0cd04 111 {
SeanNewton 7:a94ba2e0cd04 112 DHCPClient dhcp;
SeanNewton 7:a94ba2e0cd04 113 int err = dhcp.setup(timeout_ms);
SeanNewton 7:a94ba2e0cd04 114 if (err == (-1)) {
SeanNewton 7:a94ba2e0cd04 115 return -1;
SeanNewton 7:a94ba2e0cd04 116 }
SeanNewton 7:a94ba2e0cd04 117 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
SeanNewton 7:a94ba2e0cd04 118 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
SeanNewton 7:a94ba2e0cd04 119 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
SeanNewton 7:a94ba2e0cd04 120 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
SeanNewton 7:a94ba2e0cd04 121 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
SeanNewton 7:a94ba2e0cd04 122 return 0;
SeanNewton 7:a94ba2e0cd04 123 }