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