no chance
Fork of WIZnet_Library by
WIZnetInterface/WIZnetInterface.cpp@4:3c94b1ba5411, 2014-10-16 (annotated)
- Committer:
- slawcio
- Date:
- Thu Oct 16 09:35:53 2014 +0000
- Revision:
- 4:3c94b1ba5411
- Parent:
- 1:8138a268fbd2
W5100
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 | 1:8138a268fbd2 | 34 | int WIZnetInterface::init(uint8_t * mac) |
jbkim | 0:b72d22e10709 | 35 | { |
jbkim | 0:b72d22e10709 | 36 | dhcp = true; |
jbkim | 1:8138a268fbd2 | 37 | // |
jbkim | 1:8138a268fbd2 | 38 | for (int i =0; i < 6; i++) this->mac[i] = mac[i]; |
jbkim | 1:8138a268fbd2 | 39 | // |
jbkim | 0:b72d22e10709 | 40 | reset(); |
jbkim | 0:b72d22e10709 | 41 | return 0; |
jbkim | 0:b72d22e10709 | 42 | } |
jbkim | 0:b72d22e10709 | 43 | |
jbkim | 1:8138a268fbd2 | 44 | int WIZnetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway) |
jbkim | 0:b72d22e10709 | 45 | { |
jbkim | 0:b72d22e10709 | 46 | dhcp = false; |
jbkim | 1:8138a268fbd2 | 47 | // |
jbkim | 1:8138a268fbd2 | 48 | for (int i =0; i < 6; i++) this->mac[i] = mac[i]; |
jbkim | 1:8138a268fbd2 | 49 | // |
jbkim | 0:b72d22e10709 | 50 | this->ip = str_to_ip(ip); |
jbkim | 0:b72d22e10709 | 51 | strcpy(ip_string, ip); |
jbkim | 0:b72d22e10709 | 52 | ip_set = true; |
jbkim | 0:b72d22e10709 | 53 | this->netmask = str_to_ip(mask); |
jbkim | 0:b72d22e10709 | 54 | this->gateway = str_to_ip(gateway); |
jbkim | 0:b72d22e10709 | 55 | reset(); |
jbkim | 0:b72d22e10709 | 56 | return 0; |
jbkim | 0:b72d22e10709 | 57 | } |
jbkim | 0:b72d22e10709 | 58 | |
jbkim | 0:b72d22e10709 | 59 | // Connect Bring the interface up, start DHCP if needed. |
jbkim | 0:b72d22e10709 | 60 | int WIZnetInterface::connect() |
jbkim | 0:b72d22e10709 | 61 | { |
jbkim | 0:b72d22e10709 | 62 | if (dhcp) { |
jbkim | 0:b72d22e10709 | 63 | int r = IPrenew(); |
jbkim | 0:b72d22e10709 | 64 | if (r < 0) { |
jbkim | 0:b72d22e10709 | 65 | return r; |
jbkim | 0:b72d22e10709 | 66 | } |
jbkim | 0:b72d22e10709 | 67 | } |
jbkim | 0:b72d22e10709 | 68 | |
jbkim | 0:b72d22e10709 | 69 | if (WIZnet_Chip::setip() == false) return -1; |
jbkim | 0:b72d22e10709 | 70 | return 0; |
jbkim | 0:b72d22e10709 | 71 | } |
jbkim | 0:b72d22e10709 | 72 | |
jbkim | 0:b72d22e10709 | 73 | // Disconnect Bring the interface down. |
jbkim | 0:b72d22e10709 | 74 | int WIZnetInterface::disconnect() |
jbkim | 0:b72d22e10709 | 75 | { |
jbkim | 0:b72d22e10709 | 76 | if (WIZnet_Chip::disconnect() == false) return -1; |
jbkim | 0:b72d22e10709 | 77 | return 0; |
jbkim | 0:b72d22e10709 | 78 | } |
jbkim | 0:b72d22e10709 | 79 | |
jbkim | 0:b72d22e10709 | 80 | char* WIZnetInterface::getIPAddress() |
jbkim | 0:b72d22e10709 | 81 | { |
jbkim | 0:b72d22e10709 | 82 | uint32_t ip = reg_rd<uint32_t>(SIPR); |
jbkim | 0:b72d22e10709 | 83 | snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); |
jbkim | 0:b72d22e10709 | 84 | return ip_string; |
jbkim | 0:b72d22e10709 | 85 | } |
jbkim | 0:b72d22e10709 | 86 | |
jbkim | 0:b72d22e10709 | 87 | char* WIZnetInterface::getNetworkMask() |
jbkim | 0:b72d22e10709 | 88 | { |
jbkim | 0:b72d22e10709 | 89 | uint32_t ip = reg_rd<uint32_t>(SUBR); |
jbkim | 0:b72d22e10709 | 90 | snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); |
jbkim | 0:b72d22e10709 | 91 | return mask_string; |
jbkim | 0:b72d22e10709 | 92 | } |
jbkim | 0:b72d22e10709 | 93 | |
jbkim | 0:b72d22e10709 | 94 | char* WIZnetInterface::getGateway() |
jbkim | 0:b72d22e10709 | 95 | { |
jbkim | 0:b72d22e10709 | 96 | uint32_t ip = reg_rd<uint32_t>(GAR); |
jbkim | 0:b72d22e10709 | 97 | snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); |
jbkim | 0:b72d22e10709 | 98 | return gw_string; |
jbkim | 0:b72d22e10709 | 99 | } |
jbkim | 0:b72d22e10709 | 100 | |
jbkim | 0:b72d22e10709 | 101 | char* WIZnetInterface::getMACAddress() |
jbkim | 0:b72d22e10709 | 102 | { |
jbkim | 0:b72d22e10709 | 103 | uint8_t mac[6]; |
jbkim | 0:b72d22e10709 | 104 | reg_rd_mac(SHAR, mac); |
jbkim | 0:b72d22e10709 | 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]); |
jbkim | 1:8138a268fbd2 | 106 | return mac_string; |
jbkim | 1:8138a268fbd2 | 107 | |
jbkim | 0:b72d22e10709 | 108 | } |
jbkim | 0:b72d22e10709 | 109 | |
jbkim | 0:b72d22e10709 | 110 | int WIZnetInterface::IPrenew(int timeout_ms) |
jbkim | 0:b72d22e10709 | 111 | { |
jbkim | 0:b72d22e10709 | 112 | DHCPClient dhcp; |
jbkim | 0:b72d22e10709 | 113 | int err = dhcp.setup(timeout_ms); |
jbkim | 0:b72d22e10709 | 114 | if (err == (-1)) { |
jbkim | 0:b72d22e10709 | 115 | return -1; |
jbkim | 0:b72d22e10709 | 116 | } |
jbkim | 0:b72d22e10709 | 117 | // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); |
jbkim | 0:b72d22e10709 | 118 | ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3]; |
jbkim | 0:b72d22e10709 | 119 | gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3]; |
jbkim | 0:b72d22e10709 | 120 | netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3]; |
jbkim | 0:b72d22e10709 | 121 | dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3]; |
jbkim | 0:b72d22e10709 | 122 | return 0; |
jbkim | 0:b72d22e10709 | 123 | } |