WIZnetInterface using namespace
Dependents: DualNetworkInterface-Basic
Fork of WIZnetInterface by
Revision 20:3e61863c1f67, committed 2015-07-14
- Comitter:
- SteveKim
- Date:
- Tue Jul 14 10:16:16 2015 +0000
- Parent:
- 19:d8773cd4edc5
- Commit message:
- Dual Network Interface for mbed; - WIZwiki-W7500; - ESP8266
Changed in this revision
diff -r d8773cd4edc5 -r 3e61863c1f67 EthernetInterface.cpp --- a/EthernetInterface.cpp Wed Jul 01 04:02:50 2015 +0000 +++ b/EthernetInterface.cpp Tue Jul 14 10:16:16 2015 +0000 @@ -16,8 +16,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "EthernetInterface.h" -#include "DHCPClient.h" +#include "EthernetInterface.hpp" +#include "DHCPClient.hpp" + +using namespace wiznet_space; #if not defined(TARGET_WIZwiki_W7500) EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) : @@ -40,7 +42,7 @@ for (int i =0; i < 6; i++) this->mac[i] = mac[i]; // reset(); - + return 0; }
diff -r d8773cd4edc5 -r 3e61863c1f67 EthernetInterface.h --- a/EthernetInterface.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,89 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#pragma once -#include "eth_arch.h" - /** Interface using Wiznet chip to connect to an IP-based network - * - */ -class EthernetInterface: public WIZnet_Chip { -public: - -#if not defined(TARGET_WIZwiki_W7500) - /** - * Constructor - * - * \param mosi mbed pin to use for SPI - * \param miso mbed pin to use for SPI - * \param sclk mbed pin to use for SPI - * \param cs chip select of the WIZnet_Chip - * \param reset reset pin of the WIZnet_Chip - */ - EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); - EthernetInterface(SPI* spi, PinName cs, PinName reset); -#endif - - /** Initialize the interface with DHCP. - * Initialize the interface and configure it to use DHCP (no connection at this point). - * \return 0 on success, a negative number on failure - */ - int init(uint8_t * mac); //With DHCP - - /** Initialize the interface with a static IP address. - * Initialize the interface and configure it with the following static configuration (no connection at this point). - * \param ip the IP address to use - * \param mask the IP address mask - * \param gateway the gateway to use - * \return 0 on success, a negative number on failure - */ - int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway); - - /** Connect - * Bring the interface up, start DHCP if needed. - * \return 0 on success, a negative number on failure - */ - int connect(); - - /** Disconnect - * Bring the interface down - * \return 0 on success, a negative number on failure - */ - int disconnect(); - - /** Get IP address & MAC address - * - * @ returns ip address - */ - char* getIPAddress(); - char* getNetworkMask(); - char* getGateway(); - char* getMACAddress(); - - int IPrenew(int timeout_ms = 15*1000); - -private: - char ip_string[20]; - char mask_string[20]; - char gw_string[20]; - char mac_string[20]; - bool ip_set; -}; - -#include "TCPSocketConnection.h" -#include "TCPSocketServer.h" -#include "UDPSocket.h"
diff -r d8773cd4edc5 -r 3e61863c1f67 EthernetInterface.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,94 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#pragma once +#include "eth_arch.hpp" + /** Interface using Wiznet chip to connect to an IP-based network + * + */ + + namespace wiznet_space { + +class EthernetInterface: public WIZnet_Chip { +public: + +#if not defined(TARGET_WIZwiki_W7500) + /** + * Constructor + * + * \param mosi mbed pin to use for SPI + * \param miso mbed pin to use for SPI + * \param sclk mbed pin to use for SPI + * \param cs chip select of the WIZnet_Chip + * \param reset reset pin of the WIZnet_Chip + */ + EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); + EthernetInterface(SPI* spi, PinName cs, PinName reset); +#endif + + /** Initialize the interface with DHCP. + * Initialize the interface and configure it to use DHCP (no connection at this point). + * \return 0 on success, a negative number on failure + */ + int init(uint8_t * mac); //With DHCP + + /** Initialize the interface with a static IP address. + * Initialize the interface and configure it with the following static configuration (no connection at this point). + * \param ip the IP address to use + * \param mask the IP address mask + * \param gateway the gateway to use + * \return 0 on success, a negative number on failure + */ + int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway); + + /** Connect + * Bring the interface up, start DHCP if needed. + * \return 0 on success, a negative number on failure + */ + int connect(); + + /** Disconnect + * Bring the interface down + * \return 0 on success, a negative number on failure + */ + int disconnect(); + + /** Get IP address & MAC address + * + * @ returns ip address + */ + char* getIPAddress(); + char* getNetworkMask(); + char* getGateway(); + char* getMACAddress(); + + int IPrenew(int timeout_ms = 15*1000); + +private: + char ip_string[20]; + char mask_string[20]; + char gw_string[20]; + char mac_string[20]; + bool ip_set; +}; + +} + +#include "TCPSocketConnection.hpp" +#include "TCPSocketServer.hpp" +#include "UDPSocket.hpp"
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/DHCPClient.cpp --- a/Socket/DHCPClient.cpp Wed Jul 01 04:02:50 2015 +0000 +++ b/Socket/DHCPClient.cpp Tue Jul 14 10:16:16 2015 +0000 @@ -1,8 +1,8 @@ // DHCPClient.cpp 2013/4/10 #include "mbed.h" #include "mbed_debug.h" -#include "UDPSocket.h" -#include "DHCPClient.h" +#include "UDPSocket.hpp" +#include "DHCPClient.hpp" #define DBG_DHCP 0 @@ -14,6 +14,8 @@ #define DBG_HEX(A,B) while(0); #endif +using namespace wiznet_space; + int DHCPClient::discover() { m_pos = 0;
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/DHCPClient.h --- a/Socket/DHCPClient.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -// DHCPClient.h 2013/4/10 -#ifndef DHCPCLIENT_H -#define DHCPCLIENT_H -#include "eth_arch.h" -#include "UDPSocket.h" - -#define DHCP_OFFSET_OP 0 -#define DHCP_OFFSET_XID 4 -#define DHCP_OFFSET_YIADDR 16 -#define DHCP_OFFSET_SIADDR 20 -#define DHCP_OFFSET_OPTIONS 240 -#define DHCP_MAX_PACKET_SIZE 600 - -// DHCP Message Type -#define DHCPDISCOVER 1 -#define DHCPOFFER 2 -#define DHCPREQUEST 3 -#define DHCPDECLINE 4 -#define DHCPACK 5 -#define DHCPNAK 6 -#define DHCPRELEASE 7 -#define DHCPINFORM 8 - -class DHCPClient { -public: - DHCPClient(); - int setup(int timeout_ms = 15*1000); - uint8_t chaddr[6]; // MAC - uint8_t yiaddr[4]; // IP - uint8_t dnsaddr[4]; // DNS - uint8_t gateway[4]; - uint8_t netmask[4]; - uint8_t siaddr[4]; -private: - int discover(); - int request(); - int offer(uint8_t buf[], int size); - void add_buf(uint8_t* buf, int len); - void fill_buf(int len, uint8_t data = 0x00); - void add_buf(uint8_t c); - void add_option(uint8_t code, uint8_t* buf = NULL, int len = 0); - bool verify(uint8_t buf[], int len); - void callback(); - UDPSocket* m_udp; - Endpoint m_server; - uint8_t xid[4]; - bool exit_flag; - Timer m_interval; - int m_retry; - uint8_t m_buf[DHCP_MAX_PACKET_SIZE]; - int m_pos; - WIZnet_Chip* eth; -}; -#endif //DHCPCLIENT_H -
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/DHCPClient.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/DHCPClient.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,60 @@ +// DHCPClient.h 2013/4/10 +#ifndef DHCPCLIENT_H +#define DHCPCLIENT_H +#include "eth_arch.hpp" +#include "UDPSocket.hpp" + +#define DHCP_OFFSET_OP 0 +#define DHCP_OFFSET_XID 4 +#define DHCP_OFFSET_YIADDR 16 +#define DHCP_OFFSET_SIADDR 20 +#define DHCP_OFFSET_OPTIONS 240 +#define DHCP_MAX_PACKET_SIZE 600 + +// DHCP Message Type +#define DHCPDISCOVER 1 +#define DHCPOFFER 2 +#define DHCPREQUEST 3 +#define DHCPDECLINE 4 +#define DHCPACK 5 +#define DHCPNAK 6 +#define DHCPRELEASE 7 +#define DHCPINFORM 8 + +namespace wiznet_space { + +class DHCPClient { +public: + DHCPClient(); + int setup(int timeout_ms = 15*1000); + uint8_t chaddr[6]; // MAC + uint8_t yiaddr[4]; // IP + uint8_t dnsaddr[4]; // DNS + uint8_t gateway[4]; + uint8_t netmask[4]; + uint8_t siaddr[4]; +private: + int discover(); + int request(); + int offer(uint8_t buf[], int size); + void add_buf(uint8_t* buf, int len); + void fill_buf(int len, uint8_t data = 0x00); + void add_buf(uint8_t c); + void add_option(uint8_t code, uint8_t* buf = NULL, int len = 0); + bool verify(uint8_t buf[], int len); + void callback(); + UDPSocket* m_udp; + Endpoint m_server; + uint8_t xid[4]; + bool exit_flag; + Timer m_interval; + int m_retry; + uint8_t m_buf[DHCP_MAX_PACKET_SIZE]; + int m_pos; + WIZnet_Chip* eth; +}; + +} + +#endif //DHCPCLIENT_H +
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/DNSClient.cpp --- a/Socket/DNSClient.cpp Wed Jul 01 04:02:50 2015 +0000 +++ b/Socket/DNSClient.cpp Tue Jul 14 10:16:16 2015 +0000 @@ -1,10 +1,10 @@ // DNSClient.cpp 2013/8/27 #include "mbed.h" #include "mbed_debug.h" -#include "DNSClient.h" -#include "UDPSocket.h" -#include "dnsname.h" -#include "eth_arch.h" +#include "DNSClient.hpp" +#include "UDPSocket.hpp" +#include "dnsname.hpp" +#include "eth_arch.hpp" #define DBG_DNS 0 @@ -14,6 +14,8 @@ #define DBG2(...) while(0); #endif +using namespace wiznet_space; + DNSClient::DNSClient(const char* hostname) : m_state(MYNETDNS_START), m_udp(NULL) { m_hostname = hostname; }
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/DNSClient.h --- a/Socket/DNSClient.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -// DNSClient.h 2013/4/5 -#pragma once - -#include "UDPSocket.h" - -class DNSClient { -public: - DNSClient(const char* hostname = NULL); - DNSClient(Endpoint* pHost); - virtual ~DNSClient(); - bool lookup(const char* hostname = NULL); - uint32_t ip; -protected: - void poll(); - void callback(); - int response(uint8_t buf[], int size); - int query(uint8_t buf[], int size, const char* hostname); - void resolve(const char* hostname); - uint8_t m_id[2]; - Timer m_interval; - int m_retry; - const char* m_hostname; -private: - enum MyNetDnsState - { - MYNETDNS_START, - MYNETDNS_PROCESSING, //Req has not completed - MYNETDNS_NOTFOUND, - MYNETDNS_ERROR, - MYNETDNS_OK - }; - MyNetDnsState m_state; - UDPSocket *m_udp; -}; -
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/DNSClient.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/DNSClient.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,38 @@ +// DNSClient.h 2013/4/5 +#pragma once + +#include "UDPSocket.hpp" + +namespace wiznet_space { + +class DNSClient { +public: + DNSClient(const char* hostname = NULL); + DNSClient(Endpoint* pHost); + virtual ~DNSClient(); + bool lookup(const char* hostname = NULL); + uint32_t ip; +protected: + void poll(); + void callback(); + int response(uint8_t buf[], int size); + int query(uint8_t buf[], int size, const char* hostname); + void resolve(const char* hostname); + uint8_t m_id[2]; + Timer m_interval; + int m_retry; + const char* m_hostname; +private: + enum MyNetDnsState + { + MYNETDNS_START, + MYNETDNS_PROCESSING, //Req has not completed + MYNETDNS_NOTFOUND, + MYNETDNS_ERROR, + MYNETDNS_OK + }; + MyNetDnsState m_state; + UDPSocket *m_udp; +}; + +}
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/Endpoint.cpp --- a/Socket/Endpoint.cpp Wed Jul 01 04:02:50 2015 +0000 +++ b/Socket/Endpoint.cpp Tue Jul 14 10:16:16 2015 +0000 @@ -15,8 +15,10 @@ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "Socket.h" -#include "Endpoint.h" +#include "Socket.hpp" +#include "Endpoint.hpp" + +using namespace wiznet_space; Endpoint::Endpoint() {
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/Endpoint.h --- a/Socket/Endpoint.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -#ifndef ENDPOINT_H -#define ENDPOINT_H - -#include "eth_arch.h" - -class UDPSocket; - -/** -IP Endpoint (address, port) -*/ -class Endpoint { - friend class UDPSocket; - -public: - /** IP Endpoint (address, port) - */ - Endpoint(void); - - ~Endpoint(void); - - /** Reset the address of this endpoint - */ - void reset_address(void); - - /** Set the address of this endpoint - \param host The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS). - \param port The endpoint port - \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). - */ - int set_address(const char* host, const int port); - - /** Get the IP address of this endpoint - \return The IP address of this endpoint. - */ - char* get_address(void); - - /** Get the port of this endpoint - \return The port of this endpoint - */ - int get_port(void); - -protected: - char _ipAddress[16]; - int _port; -}; - -#endif
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/Endpoint.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Endpoint.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,68 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef ENDPOINT_H +#define ENDPOINT_H + +#include "eth_arch.hpp" + +namespace wiznet_space { + +class UDPSocket; + +/** +IP Endpoint (address, port) +*/ +class Endpoint { + friend class UDPSocket; + +public: + /** IP Endpoint (address, port) + */ + Endpoint(void); + + ~Endpoint(void); + + /** Reset the address of this endpoint + */ + void reset_address(void); + + /** Set the address of this endpoint + \param host The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS). + \param port The endpoint port + \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS). + */ + int set_address(const char* host, const int port); + + /** Get the IP address of this endpoint + \return The IP address of this endpoint. + */ + char* get_address(void); + + /** Get the port of this endpoint + \return The port of this endpoint + */ + int get_port(void); + +protected: + char _ipAddress[16]; + int _port; +}; + +} + +#endif
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/Socket.cpp --- a/Socket/Socket.cpp Wed Jul 01 04:02:50 2015 +0000 +++ b/Socket/Socket.cpp Tue Jul 14 10:16:16 2015 +0000 @@ -16,7 +16,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "Socket.h" +#include "Socket.hpp" + +using namespace wiznet_space; Socket::Socket() : _sock_fd(-1),_blocking(true), _timeout(1500) {
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/Socket.h --- a/Socket/Socket.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -#ifndef SOCKET_H_ -#define SOCKET_H_ - -#include "eth_arch.h" - -#define htons(x) __REV16(x) -#define ntohs(x) __REV16(x) -#define htonl(x) __REV(x) -#define ntohl(x) __REV(x) - -/** Socket file descriptor and select wrapper - */ -class Socket { -public: - /** Socket - */ - Socket(); - - /** Set blocking or non-blocking mode of the socket and a timeout on - blocking socket operations - \param blocking true for blocking mode, false for non-blocking mode. - \param timeout timeout in ms [Default: (1500)ms]. - */ - void set_blocking(bool blocking, unsigned int timeout=1500); - - /** Close the socket file descriptor - */ - int close(); - - ~Socket(); - -protected: - int _sock_fd; - bool _blocking; - int _timeout; - - WIZnet_Chip* eth; -}; - - -#endif /* SOCKET_H_ */ -
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/Socket.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/Socket.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,61 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef SOCKET_H_ +#define SOCKET_H_ + +#include "eth_arch.hpp" + +#define htons(x) __REV16(x) +#define ntohs(x) __REV16(x) +#define htonl(x) __REV(x) +#define ntohl(x) __REV(x) + +namespace wiznet_space { + +/** Socket file descriptor and select wrapper + */ +class Socket { +public: + /** Socket + */ + Socket(); + + /** Set blocking or non-blocking mode of the socket and a timeout on + blocking socket operations + \param blocking true for blocking mode, false for non-blocking mode. + \param timeout timeout in ms [Default: (1500)ms]. + */ + void set_blocking(bool blocking, unsigned int timeout=1500); + + /** Close the socket file descriptor + */ + int close(); + + ~Socket(); + +protected: + int _sock_fd; + bool _blocking; + int _timeout; + + WIZnet_Chip* eth; +}; + +} +#endif /* SOCKET_H_ */ +
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/TCPSocketConnection.cpp --- a/Socket/TCPSocketConnection.cpp Wed Jul 01 04:02:50 2015 +0000 +++ b/Socket/TCPSocketConnection.cpp Tue Jul 14 10:16:16 2015 +0000 @@ -16,12 +16,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "TCPSocketConnection.h" +#include "TCPSocketConnection.hpp" #include <cstring> using std::memset; using std::memcpy; +using namespace wiznet_space; + // not a big code. // refer from EthernetInterface by mbed official driver TCPSocketConnection::TCPSocketConnection() :
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/TCPSocketConnection.h --- a/Socket/TCPSocketConnection.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef TCPSOCKET_H -#define TCPSOCKET_H - -#include "Socket.h" -#include "Endpoint.h" - -/** -TCP socket connection -*/ -class TCPSocketConnection: public Socket, public Endpoint { - friend class TCPSocketServer; - -public: - /** TCP socket connection - */ - TCPSocketConnection(); - - /** Connects this TCP socket to the server - \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS. - \param port The host's port to connect to. - \return 0 on success, -1 on failure. - */ - int connect(const char* host, const int port); - - /** Check if the socket is connected - \return true if connected, false otherwise. - */ - bool is_connected(void); - - /** Send data to the remote host. - \param data The buffer to send to the host. - \param length The length of the buffer to send. - \return the number of written bytes on success (>=0) or -1 on failure - */ - int send(char* data, int length); - - /** Send all the data to the remote host. - \param data The buffer to send to the host. - \param length The length of the buffer to send. - \return the number of written bytes on success (>=0) or -1 on failure - */ - int send_all(char* data, int length); - - /** Receive data from the remote host. - \param data The buffer in which to store the data received from the host. - \param length The maximum length of the buffer. - \return the number of received bytes on success (>=0) or -1 on failure - */ - int receive(char* data, int length); - - /** Receive all the data from the remote host. - \param data The buffer in which to store the data received from the host. - \param length The maximum length of the buffer. - \return the number of received bytes on success (>=0) or -1 on failure - */ - int receive_all(char* data, int length); - -private: - bool _is_connected; -}; - -#endif
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/TCPSocketConnection.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketConnection.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,83 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef TCPSOCKET_H +#define TCPSOCKET_H + +#include "Socket.hpp" +#include "Endpoint.hpp" + +namespace wiznet_space { + +/** +TCP socket connection +*/ +class TCPSocketConnection: public Socket, public Endpoint { + friend class TCPSocketServer; + +public: + /** TCP socket connection + */ + TCPSocketConnection(); + + /** Connects this TCP socket to the server + \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS. + \param port The host's port to connect to. + \return 0 on success, -1 on failure. + */ + int connect(const char* host, const int port); + + /** Check if the socket is connected + \return true if connected, false otherwise. + */ + bool is_connected(void); + + /** Send data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send(char* data, int length); + + /** Send all the data to the remote host. + \param data The buffer to send to the host. + \param length The length of the buffer to send. + \return the number of written bytes on success (>=0) or -1 on failure + */ + int send_all(char* data, int length); + + /** Receive data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive(char* data, int length); + + /** Receive all the data from the remote host. + \param data The buffer in which to store the data received from the host. + \param length The maximum length of the buffer. + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receive_all(char* data, int length); + +private: + bool _is_connected; +}; +} + +#endif
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/TCPSocketServer.cpp --- a/Socket/TCPSocketServer.cpp Wed Jul 01 04:02:50 2015 +0000 +++ b/Socket/TCPSocketServer.cpp Tue Jul 14 10:16:16 2015 +0000 @@ -16,8 +16,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "TCPSocketServer.h" +#include "TCPSocketServer.hpp" +using namespace wiznet_space; + TCPSocketServer::TCPSocketServer() {} // Server initialization
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/TCPSocketServer.h --- a/Socket/TCPSocketServer.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,57 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -#ifndef TCPSOCKETSERVER_H -#define TCPSOCKETSERVER_H - -#include "Socket.h" -#include "TCPSocketConnection.h" - -/** TCP Server. - */ -class TCPSocketServer : public Socket -{ -public: - /** Instantiate a TCP Server. - */ - TCPSocketServer(); - - /** Bind a socket to a specific port. - \param port The port to listen for incoming connections on. - \return 0 on success, -1 on failure. - */ - int bind(int port); - - /** Start listening for incoming connections. - \param backlog number of pending connections that can be queued up at any - one time [Default: 1]. - \return 0 on success, -1 on failure. - */ - int listen(int backlog=1); - - /** Accept a new connection. - \param connection A TCPSocketConnection instance that will handle the incoming connection. - \return 0 on success, -1 on failure. - */ - int accept(TCPSocketConnection& connection); - -private : - int listen_port; -}; - -#endif -
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/TCPSocketServer.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/TCPSocketServer.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,61 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef TCPSOCKETSERVER_H +#define TCPSOCKETSERVER_H + +#include "Socket.hpp" +#include "TCPSocketConnection.hpp" + +namespace wiznet_space { + +/** TCP Server. + */ +class TCPSocketServer : public Socket +{ +public: + /** Instantiate a TCP Server. + */ + TCPSocketServer(); + + /** Bind a socket to a specific port. + \param port The port to listen for incoming connections on. + \return 0 on success, -1 on failure. + */ + int bind(int port); + + /** Start listening for incoming connections. + \param backlog number of pending connections that can be queued up at any + one time [Default: 1]. + \return 0 on success, -1 on failure. + */ + int listen(int backlog=1); + + /** Accept a new connection. + \param connection A TCPSocketConnection instance that will handle the incoming connection. + \return 0 on success, -1 on failure. + */ + int accept(TCPSocketConnection& connection); + +private : + int listen_port; +}; + +} + +#endif +
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/UDPSocket.cpp --- a/Socket/UDPSocket.cpp Wed Jul 01 04:02:50 2015 +0000 +++ b/Socket/UDPSocket.cpp Tue Jul 14 10:16:16 2015 +0000 @@ -16,7 +16,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "UDPSocket.h" +#include "UDPSocket.hpp" + +using namespace wiznet_space; static int udp_local_port;
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/UDPSocket.h --- a/Socket/UDPSocket.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef UDPSOCKET_H -#define UDPSOCKET_H - -#include "Endpoint.h" -#include "Socket.h" - -/** -UDP Socket -*/ -class UDPSocket: public Socket { - -public: - /** Instantiate an UDP Socket. - */ - UDPSocket(); - - /** Init the UDP Client Socket without binding it to any specific port - \return 0 on success, -1 on failure. - */ - int init(void); - - /** Bind a UDP Server Socket to a specific port - \param port The port to listen for incoming connections on - \return 0 on success, -1 on failure. - */ - int bind(int port = -1); - - /** Send a packet to a remote endpoint - \param remote The remote endpoint - \param packet The packet to be sent - \param length The length of the packet to be sent - \return the number of written bytes on success (>=0) or -1 on failure - */ - int sendTo(Endpoint &remote, char *packet, int length); - - /** Receive a packet from a remote endpoint - \param remote The remote endpoint - \param buffer The buffer for storing the incoming packet data. If a packet - is too long to fit in the supplied buffer, excess bytes are discarded - \param length The length of the buffer - \return the number of received bytes on success (>=0) or -1 on failure - */ - int receiveFrom(Endpoint &remote, char *buffer, int length); - -private: - void confEndpoint(Endpoint & ep); - void readEndpoint(Endpoint & ep, uint8_t info[]); -}; - -#endif -
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/UDPSocket.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/UDPSocket.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,73 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef UDPSOCKET_H +#define UDPSOCKET_H + +#include "Endpoint.hpp" +#include "Socket.hpp" + +namespace wiznet_space { + +/** +UDP Socket +*/ +class UDPSocket: public Socket { + +public: + /** Instantiate an UDP Socket. + */ + UDPSocket(); + + /** Init the UDP Client Socket without binding it to any specific port + \return 0 on success, -1 on failure. + */ + int init(void); + + /** Bind a UDP Server Socket to a specific port + \param port The port to listen for incoming connections on + \return 0 on success, -1 on failure. + */ + int bind(int port = -1); + + /** Send a packet to a remote endpoint + \param remote The remote endpoint + \param packet The packet to be sent + \param length The length of the packet to be sent + \return the number of written bytes on success (>=0) or -1 on failure + */ + int sendTo(Endpoint &remote, char *packet, int length); + + /** Receive a packet from a remote endpoint + \param remote The remote endpoint + \param buffer The buffer for storing the incoming packet data. If a packet + is too long to fit in the supplied buffer, excess bytes are discarded + \param length The length of the buffer + \return the number of received bytes on success (>=0) or -1 on failure + */ + int receiveFrom(Endpoint &remote, char *buffer, int length); + +private: + void confEndpoint(Endpoint & ep); + void readEndpoint(Endpoint & ep, uint8_t info[]); +}; + +} + +#endif +
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/dnsname.h --- a/Socket/dnsname.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -// dnsname.h 2013/8/27 -#pragma once -//#include <string> -#include "pico_string.h" -class dnsname { -public: - uint8_t *buf; - pico_string str; - dnsname(uint8_t *s) { - buf = s; - } - int decode(int pos) { - while(1) { - int len = buf[pos++]; - if (len == 0x00) { - break; - } - if ((len&0xc0) == 0xc0) { //compress - int offset = (len&0x3f)<<8|buf[pos]; - decode(offset); - return pos+1; - } - if (!str.empty()) { - str.append("."); - } - str.append((const char*)(buf+pos), len); - pos += len; - } - return pos; - } - - int encode(int pos, char* s) { - while(*s) { - char *f = strchr(s, '.'); - if (f == NULL) { - int len = strlen(s); - buf[pos++] = len; - memcpy(buf+pos, s, len); - pos += len; - break; - } - int len = f - s; - buf[pos++] = len; - memcpy(buf+pos, s, len); - s = f+1; - pos += len; - } - buf[pos++] = 0x00; - return pos; - } -}; -
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/dnsname.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/dnsname.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,52 @@ +// dnsname.h 2013/8/27 +#pragma once +//#include <string> +#include "pico_string.hpp" +class dnsname { +public: + uint8_t *buf; + pico_string str; + dnsname(uint8_t *s) { + buf = s; + } + int decode(int pos) { + while(1) { + int len = buf[pos++]; + if (len == 0x00) { + break; + } + if ((len&0xc0) == 0xc0) { //compress + int offset = (len&0x3f)<<8|buf[pos]; + decode(offset); + return pos+1; + } + if (!str.empty()) { + str.append("."); + } + str.append((const char*)(buf+pos), len); + pos += len; + } + return pos; + } + + int encode(int pos, char* s) { + while(*s) { + char *f = strchr(s, '.'); + if (f == NULL) { + int len = strlen(s); + buf[pos++] = len; + memcpy(buf+pos, s, len); + pos += len; + break; + } + int len = f - s; + buf[pos++] = len; + memcpy(buf+pos, s, len); + s = f+1; + pos += len; + } + buf[pos++] = 0x00; + return pos; + } +}; +
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/pico_string.h --- a/Socket/pico_string.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -// pico_string.h 2013/8/27 -#pragma once -class pico_string { -public: - pico_string(){ - _len = 0; - _buf = (char*)malloc(1); - if (_buf) { - _buf[0] = '\0'; - } - } - ~pico_string() { - if (_buf) { - free(_buf); - } - } - bool empty() { - return _len == 0; - } - void append(const char* s, int len) { - if (_buf == NULL) { - return; - } - char* p = (char*)malloc(_len+len+1); - if (p == NULL) { - return; - } - memcpy(p, _buf, _len); - memcpy(p+_len, s, len); - p[_len+len] = '\0'; - free(_buf); - _buf = p; - } - void append(const char* s) { - append(s, strlen(s)); - } - char* c_str() { - if (_buf) { - return _buf; - } - return ""; - } -private: - char* _buf; - int _len; -};
diff -r d8773cd4edc5 -r 3e61863c1f67 Socket/pico_string.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket/pico_string.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,46 @@ +// pico_string.h 2013/8/27 +#pragma once +class pico_string { +public: + pico_string(){ + _len = 0; + _buf = (char*)malloc(1); + if (_buf) { + _buf[0] = '\0'; + } + } + ~pico_string() { + if (_buf) { + free(_buf); + } + } + bool empty() { + return _len == 0; + } + void append(const char* s, int len) { + if (_buf == NULL) { + return; + } + char* p = (char*)malloc(_len+len+1); + if (p == NULL) { + return; + } + memcpy(p, _buf, _len); + memcpy(p+_len, s, len); + p[_len+len] = '\0'; + free(_buf); + _buf = p; + } + void append(const char* s) { + append(s, strlen(s)); + } + char* c_str() { + if (_buf) { + return _buf; + } + return ""; + } +private: + char* _buf; + int _len; +};
diff -r d8773cd4edc5 -r 3e61863c1f67 WiznetLibraryOption.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WiznetLibraryOption.h Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,20 @@ +#pragma once + + +#define WIZNET_LIBRARY_OPTION_NAMESPACE 1 + + + + +#if (WIZNET_LIBRARY_OPTION_NAMESPACE==1) + #define WIZNET_LIBRARY_CPP_S using namespace wiznet_space; + #define WIZNET_LIBRARY_CPP_E + #define WIZNET_LIBRARY_HPP_S namespace wiznet_space { + #define WIZNET_LIBRARY_HPP_E } +#else + #define WIZNET_LIBRARY_CPP_S + #define WIZNET_LIBRARY_CPP_E + #define WIZNET_LIBRARY_HPP_S + #define WIZNET_LIBRARY_HPP_E +#endif +
diff -r d8773cd4edc5 -r 3e61863c1f67 arch/ext/W5500.cpp --- a/arch/ext/W5500.cpp Wed Jul 01 04:02:50 2015 +0000 +++ b/arch/ext/W5500.cpp Tue Jul 14 10:16:16 2015 +0000 @@ -15,12 +15,12 @@ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "eth_arch.h" +#include "eth_arch.hpp" #if not defined(TARGET_WIZwiki_W7500) #include "mbed.h" #include "mbed_debug.h" -#include "DNSClient.h" +#include "DNSClient.hpp" //Debug is disabled by default
diff -r d8773cd4edc5 -r 3e61863c1f67 arch/ext/W5500.h --- a/arch/ext/W5500.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1012 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ - -#pragma once - -#include "mbed.h" -#include "mbed_debug.h" - -#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; - -#define DEFAULT_WAIT_RESP_TIMEOUT 500 - -#define SOCK_ERROR 0 -#define SOCKERR_SOCKNUM (SOCK_ERROR - 1) ///< Invalid socket number -#define SOCKERR_SOCKOPT (SOCK_ERROR - 2) ///< Invalid socket option -#define SOCKERR_SOCKINIT (SOCK_ERROR - 3) ///< Socket is not initialized -#define SOCKERR_SOCKCLOSED (SOCK_ERROR - 4) ///< Socket unexpectedly closed. -#define SOCKERR_SOCKMODE (SOCK_ERROR - 5) ///< Invalid socket mode for socket operation. -#define SOCKERR_SOCKFLAG (SOCK_ERROR - 6) ///< Invalid socket flag -#define SOCKERR_SOCKSTATUS (SOCK_ERROR - 7) ///< Invalid socket status for socket operation. -#define SOCKERR_ARG (SOCK_ERROR - 10) ///< Invalid argrument. -#define SOCKERR_PORTZERO (SOCK_ERROR - 11) ///< Port number is zero -#define SOCKERR_IPINVALID (SOCK_ERROR - 12) ///< Invalid IP address -#define SOCKERR_TIMEOUT (SOCK_ERROR - 13) ///< Timeout occurred -#define SOCKERR_DATALEN (SOCK_ERROR - 14) ///< Data length is zero or greater than buffer max size. -#define SOCKERR_BUFFER (SOCK_ERROR - 15) ///< Socket buffer is not enough for data communication. - -#define SOCK_ANY_PORT_NUM 0xC000; - - -#define MAX_SOCK_NUM 8 - -#define MR 0x0000 -#define GAR 0x0001 -#define SUBR 0x0005 -#define SHAR 0x0009 -#define SIPR 0x000f -#define IR 0x0015 -#define IMR 0x0016 -#define SIR 0x0017 -#define SIMR 0x0018 -#define RTR 0x0019 -#define RCR 0x001b -#define UIPR 0x0028 -#define UPORTR 0x002c -#define PHYCFGR 0x002e - -// W5500 socket register -#define Sn_MR 0x0000 -#define Sn_CR 0x0001 -#define Sn_IR 0x0002 -#define Sn_SR 0x0003 -#define Sn_PORT 0x0004 -#define Sn_DHAR 0x0006 -#define Sn_DIPR 0x000c -#define Sn_DPORT 0x0010 -#define Sn_RXBUF_SIZE 0x001e -#define Sn_TXBUF_SIZE 0x001f -#define Sn_TX_FSR 0x0020 -#define Sn_TX_RD 0x0022 -#define Sn_TX_WR 0x0024 -#define Sn_RX_RSR 0x0026 -#define Sn_RX_RD 0x0028 -#define Sn_RX_WR 0x002a -#define Sn_IMR 0x002c - - -//Define for Socket Command register option value -#define Sn_CR_OPEN 0x01 -#define Sn_CR_LISTEN 0x02 -#define Sn_CR_CONNECT 0x04 -#define Sn_CR_DISCON 0x08 -#define Sn_CR_CLOSE 0x10 -#define Sn_CR_SEND 0x20 -#define Sn_CR_SEND_MAC 0x21 -#define Sn_CR_SEND_KEEP 0x22 -#define Sn_CR_RECV 0x40 - - -//Define for Socket Mode register option value -#define Sn_MR_CLOSE 0x00 -#define Sn_MR_TCP 0x01 -#define Sn_MR_UDP 0x02 -#define Sn_MR_MACRAW 0x04 -#define Sn_MR_UCASTB 0x10 -#define Sn_MR_ND 0x20 -#define Sn_MR_BCASTB 0x40 -#define Sn_MR_MULTI 0x80 - -#define Sn_IR_SENDOK 0x10 - -//Sn_IR values - -#define Sn_IR_TIMEOUT 0x08 -#define Sn_IR_RECV 0x04 -#define Sn_IR_DISCON 0x02 -#define Sn_IR_CON 0x01 - -/* PHYCFGR register value */ -#define PHYCFGR_RST ~(1<<7) //< For PHY reset, must operate AND mask. -#define PHYCFGR_OPMD (1<<6) // Configre PHY with OPMDC value -#define PHYCFGR_OPMDC_ALLA (7<<3) -#define PHYCFGR_OPMDC_PDOWN (6<<3) -#define PHYCFGR_OPMDC_NA (5<<3) -#define PHYCFGR_OPMDC_100FA (4<<3) -#define PHYCFGR_OPMDC_100F (3<<3) -#define PHYCFGR_OPMDC_100H (2<<3) -#define PHYCFGR_OPMDC_10F (1<<3) -#define PHYCFGR_OPMDC_10H (0<<3) -#define PHYCFGR_DPX_FULL (1<<2) -#define PHYCFGR_DPX_HALF (0<<2) -#define PHYCFGR_SPD_100 (1<<1) -#define PHYCFGR_SPD_10 (0<<1) -#define PHYCFGR_LNK_ON (1<<0) -#define PHYCFGR_LNK_OFF (0<<0) - -//PHY status define -#define PHY_CONFBY_HW 0 ///< Configured PHY operation mode by HW pin -#define PHY_CONFBY_SW 1 ///< Configured PHY operation mode by SW register -#define PHY_MODE_MANUAL 0 ///< Configured PHY operation mode with user setting. -#define PHY_MODE_AUTONEGO 1 ///< Configured PHY operation mode with auto-negotiation -#define PHY_SPEED_10 0 ///< Link Speed 10 -#define PHY_SPEED_100 1 ///< Link Speed 100 -#define PHY_DUPLEX_HALF 0 ///< Link Half-Duplex -#define PHY_DUPLEX_FULL 1 ///< Link Full-Duplex -#define PHY_LINK_OFF 0 ///< Link Off -#define PHY_LINK_ON 1 ///< Link On -#define PHY_POWER_NORM 0 ///< PHY power normal mode -#define PHY_POWER_DOWN 1 ///< PHY power down mode - -enum PHYMode { - AutoNegotiate = 0, - HalfDuplex10 = 1, - FullDuplex10 = 2, - HalfDuplex100 = 3, - FullDuplex100 = 4, -}; -class WIZnet_Chip { -public: -enum Protocol { - CLOSED = 0, - TCP = 1, - UDP = 2, -}; - -enum Command { - OPEN = 0x01, - LISTEN = 0x02, - CONNECT = 0x04, - DISCON = 0x08, - CLOSE = 0x10, - SEND = 0x20, - SEND_MAC = 0x21, - SEND_KEEP = 0x22, - RECV = 0x40, - -}; - -enum Interrupt { - INT_CON = 0x01, - INT_DISCON = 0x02, - INT_RECV = 0x04, - INT_TIMEOUT = 0x08, - INT_SEND_OK = 0x10, -}; - -enum Status { - SOCK_CLOSED = 0x00, - SOCK_INIT = 0x13, - SOCK_LISTEN = 0x14, - SOCK_SYNSENT = 0x15, - SOCK_ESTABLISHED = 0x17, - SOCK_CLOSE_WAIT = 0x1c, - SOCK_UDP = 0x22, -}; - - - uint16_t sock_any_port; - - /* - * Constructor - * - * @param spi spi class - * @param cs cs of the W5500 - * @param reset reset pin of the W5500 - */ - WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); - WIZnet_Chip(SPI* spi, PinName cs, PinName reset); - - /* - * Set MAC Address to W5500 - * - * @return true if connected, false otherwise - */ - bool setmac(); - - /* - * Set Network Informations (SrcIP, Netmask, Gataway) - * - * @return true if connected, false otherwise - */ - bool setip(); - - /* - * Disconnect the connection - * - * @ returns true - */ - bool disconnect(); - - /* - * Open a tcp connection with the specified host on the specified port - * - * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) - * @param port port - * @ returns true if successful - */ - bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000); - - /* - * Set the protocol (UDP or TCP) - * - * @param p protocol - * @ returns true if successful - */ - bool setProtocol(int socket, Protocol p); - - /* - * Reset the W5500 - */ - void reset(); - - int wait_readable(int socket, int wait_time_ms, int req_size = 0); - - int wait_writeable(int socket, int wait_time_ms, int req_size = 0); - - /* - * Check if an ethernet link is pressent or not. - * - * @returns true if successful - */ - bool link(int wait_time_ms= 3*1000); - - /* - * Sets the speed and duplex parameters of an ethernet link. - * - * @returns true if successful - */ - void set_link(PHYMode phymode); - - /* - * Check if a tcp link is active - * - * @returns true if successful - */ - bool is_connected(int socket); - - /* - * Close a tcp connection - * - * @ returns true if successful - */ - bool close(int socket); - - /* - * @param str string to be sent - * @param len string length - */ - int send(int socket, const char * str, int len); - - int recv(int socket, char* buf, int len); - - /* - * Return true if the module is using dhcp - * - * @returns true if the module is using dhcp - */ - bool isDHCP() { - return dhcp; - } - - bool gethostbyname(const char* host, uint32_t* ip); - - static WIZnet_Chip * getInstance() { - return inst; - }; - - int new_socket(); - uint16_t new_port(); - void scmd(int socket, Command cmd); - - template<typename T> - void sreg(int socket, uint16_t addr, T data) { - reg_wr<T>(addr, (0x0C + (socket << 5)), data); - } - - template<typename T> - T sreg(int socket, uint16_t addr) { - return reg_rd<T>(addr, (0x08 + (socket << 5))); - } - - template<typename T> - void reg_wr(uint16_t addr, T data) { - return reg_wr(addr, 0x04, data); - } - - template<typename T> - void reg_wr(uint16_t addr, uint8_t cb, T data) { - uint8_t buf[sizeof(T)]; - *reinterpret_cast<T*>(buf) = data; - for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian - uint8_t t = buf[i]; - buf[i] = buf[sizeof(buf)-1-i]; - buf[sizeof(buf)-1-i] = t; - } - spi_write(addr, cb, buf, sizeof(buf)); - } - - template<typename T> - T reg_rd(uint16_t addr) { - return reg_rd<T>(addr, 0x00); - } - - template<typename T> - T reg_rd(uint16_t addr, uint8_t cb) { - uint8_t buf[sizeof(T)]; - spi_read(addr, cb, buf, sizeof(buf)); - for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian - uint8_t t = buf[i]; - buf[i] = buf[sizeof(buf)-1-i]; - buf[sizeof(buf)-1-i] = t; - } - return *reinterpret_cast<T*>(buf); - } - - void reg_rd_mac(uint16_t addr, uint8_t* data); -/* { - spi_read(addr, 0x00, data, 6); - }*/ - - void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip); - /* { - uint8_t buf[4]; - char* p = (char*)ip; - for(int i = 0; i < 4; i++) { - buf[i] = atoi(p); - p = strchr(p, '.'); - if (p == NULL) { - break; - } - p++; - } - spi_write(addr, cb, buf, sizeof(buf)); - } - */ - void sreg_ip(int socket, uint16_t addr, const char* ip); -/* { - reg_wr_ip(addr, (0x0C + (socket << 5)), ip); - }*/ - - void reg_rd_ip_byte(uint16_t addr, uint8_t* data); -/* { - spi_read(addr, 0x00, data, 4); - }*/ - - void reg_wr_ip_byte(uint16_t addr, uint8_t* data); -/* { - spi_write(addr, 0x04, data, 4); - }*/ - -///////////////////////////////// -// Common Register I/O function // -///////////////////////////////// -/** - * @ingroup Common_register_access_function - * @brief Set Mode Register - * @param (uint8_t)mr The value to be set. - * @sa getMR() - */ - void setMR(uint8_t mr) { - reg_wr<uint8_t>(MR,mr); - } - - -/** - * @ingroup Common_register_access_function - * @brief Get Mode Register - * @return uint8_t. The value of Mode register. - * @sa setMR() - */ - uint8_t getMR() { - return reg_rd<uint8_t>(MR); - } - -/** - * @ingroup Common_register_access_function - * @brief Set gateway IP address - * @param (uint8_t*)gar Pointer variable to set gateway IP address. It should be allocated 4 bytes. - * @sa getGAR() - */ - void setGAR(uint8_t * gar) { - reg_wr_ip_byte(GAR,gar); - } - -/** - * @ingroup Common_register_access_function - * @brief Get gateway IP address - * @param (uint8_t*)gar Pointer variable to get gateway IP address. It should be allocated 4 bytes. - * @sa setGAR() - */ - void getGAR(uint8_t * gar) { - reg_rd_ip_byte(GAR,gar); - } - -/** - * @ingroup Common_register_access_function - * @brief Set subnet mask address - * @param (uint8_t*)subr Pointer variable to set subnet mask address. It should be allocated 4 bytes. - * @sa getSUBR() - */ - void setSUBR(uint8_t * subr) { - reg_wr_ip_byte(SUBR, subr); - } - - -/** - * @ingroup Common_register_access_function - * @brief Get subnet mask address - * @param (uint8_t*)subr Pointer variable to get subnet mask address. It should be allocated 4 bytes. - * @sa setSUBR() - */ - void getSUBR(uint8_t * subr) { - reg_rd_ip_byte(SUBR, subr); - } - -/** - * @ingroup Common_register_access_function - * @brief Set local MAC address - * @param (uint8_t*)shar Pointer variable to set local MAC address. It should be allocated 6 bytes. - * @sa getSHAR() - */ - void setSHAR(uint8_t * shar) { - reg_wr_mac(SHAR, shar); - } - -/** - * @ingroup Common_register_access_function - * @brief Get local MAC address - * @param (uint8_t*)shar Pointer variable to get local MAC address. It should be allocated 6 bytes. - * @sa setSHAR() - */ - void getSHAR(uint8_t * shar) { - reg_rd_mac(SHAR, shar); - } - -/** - * @ingroup Common_register_access_function - * @brief Set local IP address - * @param (uint8_t*)sipr Pointer variable to set local IP address. It should be allocated 4 bytes. - * @sa getSIPR() - */ - void setSIPR(uint8_t * sipr) { - reg_wr_ip_byte(SIPR, sipr); - } - -/** - * @ingroup Common_register_access_function - * @brief Get local IP address - * @param (uint8_t*)sipr Pointer variable to get local IP address. It should be allocated 4 bytes. - * @sa setSIPR() - */ - void getSIPR(uint8_t * sipr) { - reg_rd_ip_byte(SIPR, sipr); - } - -/** - * @ingroup Common_register_access_function - * @brief Set @ref IR register - * @param (uint8_t)ir Value to set @ref IR register. - * @sa getIR() - */ - void setIR(uint8_t ir) { - reg_wr<uint8_t>(IR, (ir & 0xF0)); - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref IR register - * @return uint8_t. Value of @ref IR register. - * @sa setIR() - */ - uint8_t getIR() { - return reg_rd<uint8_t>(IR & 0xF0); - } - -/** - * @ingroup Common_register_access_function - * @brief Set @ref IMR register - * @param (uint8_t)imr Value to set @ref IMR register. - * @sa getIMR() - */ - void setIMR(uint8_t imr) { - reg_wr<uint8_t>(IMR, imr); - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref IMR register - * @return uint8_t. Value of @ref IMR register. - * @sa setIMR() - */ - uint8_t getIMR() { - return reg_rd<uint8_t>(IMR); - } - - -/** - * @ingroup Common_register_access_function - * @brief Set @ref SIR register - * @param (uint8_t)sir Value to set @ref SIR register. - * @sa getSIR() - */ - void setSIR(uint8_t sir) { - reg_wr<uint8_t>(SIR, sir); - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref SIR register - * @return uint8_t. Value of @ref SIR register. - * @sa setSIR() - */ - uint8_t getSIR() { - return reg_rd<uint8_t>(SIR); - } -/** - * @ingroup Common_register_access_function - * @brief Set @ref SIMR register - * @param (uint8_t)simr Value to set @ref SIMR register. - * @sa getSIMR() - */ - void setSIMR(uint8_t simr) { - reg_wr<uint8_t>(SIMR, simr); - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref SIMR register - * @return uint8_t. Value of @ref SIMR register. - * @sa setSIMR() - */ - uint8_t getSIMR() { - return reg_rd<uint8_t>(SIMR); - } - -/** - * @ingroup Common_register_access_function - * @brief Set @ref RTR register - * @param (uint16_t)rtr Value to set @ref RTR register. - * @sa getRTR() - */ - void setRTR(uint16_t rtr) { - reg_wr<uint16_t>(RTR, rtr); - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref RTR register - * @return uint16_t. Value of @ref RTR register. - * @sa setRTR() - */ - uint16_t getRTR() { - return reg_rd<uint16_t>(RTR); - } - -/** - * @ingroup Common_register_access_function - * @brief Set @ref RCR register - * @param (uint8_t)rcr Value to set @ref RCR register. - * @sa getRCR() - */ - void setRCR(uint8_t rcr) { - reg_wr<uint8_t>(RCR, rcr); - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref RCR register - * @return uint8_t. Value of @ref RCR register. - * @sa setRCR() - */ - uint8_t getRCR() { - return reg_rd<uint8_t>(RCR); - } - -//================================================== test done =========================================================== - -/** - * @ingroup Common_register_access_function - * @brief Set @ref PHYCFGR register - * @param (uint8_t)phycfgr Value to set @ref PHYCFGR register. - * @sa setPHYCFGR() - */ - void setPHYCFGR(uint8_t phycfgr) { - reg_wr<uint8_t>(PHYCFGR, phycfgr); - } - -/** - * @ingroup Common_register_access_function - * @brief Get @ref PHYCFGR register - * @return uint8_t. Value of @ref PHYCFGR register. - * @sa getPHYCFGR() - */ - uint8_t getPHYCFGR() { - return reg_rd<uint8_t>(PHYCFGR); - } - - -///////////////////////////////////// - -/////////////////////////////////// -// Socket N register I/O function // -/////////////////////////////////// -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_MR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint8_t)mr Value to set @ref Sn_MR - * @sa getSn_MR() - */ - void setSn_MR(uint8_t sn, uint8_t mr) { - sreg<uint8_t>(sn, MR, mr); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_MR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint8_t. Value of @ref Sn_MR. - * @sa setSn_MR() - */ - uint8_t getSn_MR(uint8_t sn) { - return sreg<uint8_t>(sn, Sn_MR); - } - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_CR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint8_t)cr Value to set @ref Sn_CR - * @sa getSn_CR() - */ - void setSn_CR(uint8_t sn, uint8_t cr) { - sreg<uint8_t>(sn, Sn_CR, cr); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_CR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint8_t. Value of @ref Sn_CR. - * @sa setSn_CR() - */ - uint8_t getSn_CR(uint8_t sn) { - return sreg<uint8_t>(sn, Sn_CR); - } - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_IR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint8_t)ir Value to set @ref Sn_IR - * @sa getSn_IR() - */ - void setSn_IR(uint8_t sn, uint8_t ir) { - sreg<uint8_t>(sn, Sn_IR, (ir & 0x1F)); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_IR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint8_t. Value of @ref Sn_IR. - * @sa setSn_IR() - */ - uint8_t getSn_IR(uint8_t sn) { - return (sreg<uint8_t>(sn, Sn_IR)) & 0x1F; - } - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_IMR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint8_t)imr Value to set @ref Sn_IMR - * @sa getSn_IMR() - */ - void setSn_IMR(uint8_t sn, uint8_t imr) { - sreg<uint8_t>(sn, Sn_IMR, (imr & 0x1F)); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_IMR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint8_t. Value of @ref Sn_IMR. - * @sa setSn_IMR() - */ - uint8_t getSn_IMR(uint8_t sn) { - return (sreg<uint8_t>(sn, Sn_IMR)) & 0x1F; - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_SR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint8_t. Value of @ref Sn_SR. - */ - uint8_t getSn_SR(uint8_t sn) { - return sreg<uint8_t>(sn, Sn_SR); - } - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_PORT register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint16_t)port Value to set @ref Sn_PORT. - * @sa getSn_PORT() - */ - void setSn_PORT(uint8_t sn, uint16_t port) { - sreg<uint16_t>(sn, Sn_PORT, port ); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_PORT register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint16_t. Value of @ref Sn_PORT. - * @sa setSn_PORT() - */ - uint16_t getSn_PORT(uint8_t sn) { - return sreg<uint16_t>(sn, Sn_PORT); - } - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_DHAR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint8_t*)dhar Pointer variable to set socket n destination hardware address. It should be allocated 6 bytes. - * @sa getSn_DHAR() - */ - void setSn_DHAR(uint8_t sn, uint8_t * dhar) { - spi_write(Sn_DHAR, (0x0C + (sn << 5)), dhar, 6); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_MR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint8_t*)dhar Pointer variable to get socket n destination hardware address. It should be allocated 6 bytes. - * @sa setSn_DHAR() - */ - void getSn_DHAR(uint8_t sn, uint8_t * dhar) { - spi_read(Sn_DHAR, (0x08 + (sn << 5)), dhar, 6); - } - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_DIPR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint8_t*)dipr Pointer variable to set socket n destination IP address. It should be allocated 4 bytes. - * @sa getSn_DIPR() - */ - void setSn_DIPR(uint8_t sn, uint8_t * dipr) { - spi_write(Sn_DIPR, (0x0C + (sn << 5)), dipr, 4); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_DIPR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint8_t*)dipr Pointer variable to get socket n destination IP address. It should be allocated 4 bytes. - * @sa SetSn_DIPR() - */ - void getSn_DIPR(uint8_t sn, uint8_t * dipr) { - spi_read(Sn_DIPR, (0x08 + (sn << 5)), dipr, 4); - } - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_DPORT register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint16_t)dport Value to set @ref Sn_DPORT - * @sa getSn_DPORT() - */ - void setSn_DPORT(uint8_t sn, uint16_t dport) { - sreg<uint16_t>(sn, Sn_DPORT, dport); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_DPORT register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint16_t. Value of @ref Sn_DPORT. - * @sa setSn_DPORT() - */ - uint16_t getSn_DPORT(uint8_t sn) { - return sreg<uint16_t>(sn, Sn_DPORT); - } - - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_RXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint8_t)rxbufsize Value to set @ref Sn_RXBUF_SIZE - * @sa getSn_RXBUF_SIZE() - */ - void setSn_RXBUF_SIZE(uint8_t sn, uint8_t rxbufsize) { - sreg<uint8_t>(sn, Sn_RXBUF_SIZE ,rxbufsize); - } - - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint8_t. Value of @ref Sn_RXBUF_SIZE. - * @sa setSn_RXBUF_SIZE() - */ - uint8_t getSn_RXBUF_SIZE(uint8_t sn) { - return sreg<uint8_t>(sn, Sn_RXBUF_SIZE); - } - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_TXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint8_t)txbufsize Value to set @ref Sn_TXBUF_SIZE - * @sa getSn_TXBUF_SIZE() - */ - void setSn_TXBUF_SIZE(uint8_t sn, uint8_t txbufsize) { - sreg<uint8_t>(sn, Sn_TXBUF_SIZE, txbufsize); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TXBUF_SIZE register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint8_t. Value of @ref Sn_TXBUF_SIZE. - * @sa setSn_TXBUF_SIZE() - */ - uint8_t getSn_TXBUF_SIZE(uint8_t sn) { - return sreg<uint8_t>(sn, Sn_TXBUF_SIZE); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TX_FSR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint16_t. Value of @ref Sn_TX_FSR. - */ - uint16_t getSn_TX_FSR(uint8_t sn) { - return sreg<uint16_t>(sn, Sn_TX_FSR); - } - - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TX_RD register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint16_t. Value of @ref Sn_TX_RD. - */ - uint16_t getSn_TX_RD(uint8_t sn) { - return sreg<uint16_t>(sn, Sn_TX_RD); - } - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_TX_WR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint16_t)txwr Value to set @ref Sn_TX_WR - * @sa GetSn_TX_WR() - */ - void setSn_TX_WR(uint8_t sn, uint16_t txwr) { - sreg<uint16_t>(sn, Sn_TX_WR, txwr); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_TX_WR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint16_t. Value of @ref Sn_TX_WR. - * @sa setSn_TX_WR() - */ - uint16_t getSn_TX_WR(uint8_t sn) { - return sreg<uint16_t>(sn, Sn_TX_WR); - } - - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RX_RSR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint16_t. Value of @ref Sn_RX_RSR. - */ - uint16_t getSn_RX_RSR(uint8_t sn) { - return sreg<uint16_t>(sn, Sn_RX_RSR); - } - - -/** - * @ingroup Socket_register_access_function - * @brief Set @ref Sn_RX_RD register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @param (uint16_t)rxrd Value to set @ref Sn_RX_RD - * @sa getSn_RX_RD() - */ - void setSn_RX_RD(uint8_t sn, uint16_t rxrd) { - sreg<uint16_t>(sn, Sn_RX_RD, rxrd); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RX_RD register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @regurn uint16_t. Value of @ref Sn_RX_RD. - * @sa setSn_RX_RD() - */ - uint16_t getSn_RX_RD(uint8_t sn) { - return sreg<uint16_t>(sn, Sn_RX_RD); - } - -/** - * @ingroup Socket_register_access_function - * @brief Get @ref Sn_RX_WR register - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint16_t. Value of @ref Sn_RX_WR. - */ - uint16_t getSn_RX_WR(uint8_t sn) { - return sreg<uint16_t>(sn, Sn_RX_WR); - } - - -////////////////////////////////////// - -///////////////////////////////////// -// Sn_TXBUF & Sn_RXBUF IO function // -///////////////////////////////////// -/** - * @brief Gets the max buffer size of socket sn passed as parameter. - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint16_t. Value of Socket n RX max buffer size. - */ - uint16_t getSn_RxMAX(uint8_t sn) { - return (getSn_RXBUF_SIZE(sn) << 10); - } - -/** - * @brief Gets the max buffer size of socket sn passed as parameters. - * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. - * @return uint16_t. Value of Socket n TX max buffer size. - */ -//uint16_t getSn_TxMAX(uint8_t sn); - uint16_t getSn_TxMAX(uint8_t sn) { - return (getSn_TXBUF_SIZE(sn) << 10); - } - - - int ethernet_link(void); - void ethernet_set_link(int speed, int duplex); -protected: - uint8_t mac[6]; - uint32_t ip; - uint32_t netmask; - uint32_t gateway; - uint32_t dnsaddr; - bool dhcp; - - void spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len); - void spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len); - SPI* spi; - DigitalOut cs; - DigitalOut reset_pin; - static WIZnet_Chip* inst; - - void reg_wr_mac(uint16_t addr, uint8_t* data) { - spi_write(addr, 0x04, data, 6); - } - -}; - - -extern uint32_t str_to_ip(const char* str); -extern void printfBytes(char* str, uint8_t* buf, int len); -extern void printHex(uint8_t* buf, int len); -extern void debug_hex(uint8_t* buf, int len);
diff -r d8773cd4edc5 -r 3e61863c1f67 arch/ext/W5500.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arch/ext/W5500.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,1012 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#pragma once + +#include "mbed.h" +#include "mbed_debug.h" + +#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; + +#define DEFAULT_WAIT_RESP_TIMEOUT 500 + +#define SOCK_ERROR 0 +#define SOCKERR_SOCKNUM (SOCK_ERROR - 1) ///< Invalid socket number +#define SOCKERR_SOCKOPT (SOCK_ERROR - 2) ///< Invalid socket option +#define SOCKERR_SOCKINIT (SOCK_ERROR - 3) ///< Socket is not initialized +#define SOCKERR_SOCKCLOSED (SOCK_ERROR - 4) ///< Socket unexpectedly closed. +#define SOCKERR_SOCKMODE (SOCK_ERROR - 5) ///< Invalid socket mode for socket operation. +#define SOCKERR_SOCKFLAG (SOCK_ERROR - 6) ///< Invalid socket flag +#define SOCKERR_SOCKSTATUS (SOCK_ERROR - 7) ///< Invalid socket status for socket operation. +#define SOCKERR_ARG (SOCK_ERROR - 10) ///< Invalid argrument. +#define SOCKERR_PORTZERO (SOCK_ERROR - 11) ///< Port number is zero +#define SOCKERR_IPINVALID (SOCK_ERROR - 12) ///< Invalid IP address +#define SOCKERR_TIMEOUT (SOCK_ERROR - 13) ///< Timeout occurred +#define SOCKERR_DATALEN (SOCK_ERROR - 14) ///< Data length is zero or greater than buffer max size. +#define SOCKERR_BUFFER (SOCK_ERROR - 15) ///< Socket buffer is not enough for data communication. + +#define SOCK_ANY_PORT_NUM 0xC000; + + +#define MAX_SOCK_NUM 8 + +#define MR 0x0000 +#define GAR 0x0001 +#define SUBR 0x0005 +#define SHAR 0x0009 +#define SIPR 0x000f +#define IR 0x0015 +#define IMR 0x0016 +#define SIR 0x0017 +#define SIMR 0x0018 +#define RTR 0x0019 +#define RCR 0x001b +#define UIPR 0x0028 +#define UPORTR 0x002c +#define PHYCFGR 0x002e + +// W5500 socket register +#define Sn_MR 0x0000 +#define Sn_CR 0x0001 +#define Sn_IR 0x0002 +#define Sn_SR 0x0003 +#define Sn_PORT 0x0004 +#define Sn_DHAR 0x0006 +#define Sn_DIPR 0x000c +#define Sn_DPORT 0x0010 +#define Sn_RXBUF_SIZE 0x001e +#define Sn_TXBUF_SIZE 0x001f +#define Sn_TX_FSR 0x0020 +#define Sn_TX_RD 0x0022 +#define Sn_TX_WR 0x0024 +#define Sn_RX_RSR 0x0026 +#define Sn_RX_RD 0x0028 +#define Sn_RX_WR 0x002a +#define Sn_IMR 0x002c + + +//Define for Socket Command register option value +#define Sn_CR_OPEN 0x01 +#define Sn_CR_LISTEN 0x02 +#define Sn_CR_CONNECT 0x04 +#define Sn_CR_DISCON 0x08 +#define Sn_CR_CLOSE 0x10 +#define Sn_CR_SEND 0x20 +#define Sn_CR_SEND_MAC 0x21 +#define Sn_CR_SEND_KEEP 0x22 +#define Sn_CR_RECV 0x40 + + +//Define for Socket Mode register option value +#define Sn_MR_CLOSE 0x00 +#define Sn_MR_TCP 0x01 +#define Sn_MR_UDP 0x02 +#define Sn_MR_MACRAW 0x04 +#define Sn_MR_UCASTB 0x10 +#define Sn_MR_ND 0x20 +#define Sn_MR_BCASTB 0x40 +#define Sn_MR_MULTI 0x80 + +#define Sn_IR_SENDOK 0x10 + +//Sn_IR values + +#define Sn_IR_TIMEOUT 0x08 +#define Sn_IR_RECV 0x04 +#define Sn_IR_DISCON 0x02 +#define Sn_IR_CON 0x01 + +/* PHYCFGR register value */ +#define PHYCFGR_RST ~(1<<7) //< For PHY reset, must operate AND mask. +#define PHYCFGR_OPMD (1<<6) // Configre PHY with OPMDC value +#define PHYCFGR_OPMDC_ALLA (7<<3) +#define PHYCFGR_OPMDC_PDOWN (6<<3) +#define PHYCFGR_OPMDC_NA (5<<3) +#define PHYCFGR_OPMDC_100FA (4<<3) +#define PHYCFGR_OPMDC_100F (3<<3) +#define PHYCFGR_OPMDC_100H (2<<3) +#define PHYCFGR_OPMDC_10F (1<<3) +#define PHYCFGR_OPMDC_10H (0<<3) +#define PHYCFGR_DPX_FULL (1<<2) +#define PHYCFGR_DPX_HALF (0<<2) +#define PHYCFGR_SPD_100 (1<<1) +#define PHYCFGR_SPD_10 (0<<1) +#define PHYCFGR_LNK_ON (1<<0) +#define PHYCFGR_LNK_OFF (0<<0) + +//PHY status define +#define PHY_CONFBY_HW 0 ///< Configured PHY operation mode by HW pin +#define PHY_CONFBY_SW 1 ///< Configured PHY operation mode by SW register +#define PHY_MODE_MANUAL 0 ///< Configured PHY operation mode with user setting. +#define PHY_MODE_AUTONEGO 1 ///< Configured PHY operation mode with auto-negotiation +#define PHY_SPEED_10 0 ///< Link Speed 10 +#define PHY_SPEED_100 1 ///< Link Speed 100 +#define PHY_DUPLEX_HALF 0 ///< Link Half-Duplex +#define PHY_DUPLEX_FULL 1 ///< Link Full-Duplex +#define PHY_LINK_OFF 0 ///< Link Off +#define PHY_LINK_ON 1 ///< Link On +#define PHY_POWER_NORM 0 ///< PHY power normal mode +#define PHY_POWER_DOWN 1 ///< PHY power down mode + +enum PHYMode { + AutoNegotiate = 0, + HalfDuplex10 = 1, + FullDuplex10 = 2, + HalfDuplex100 = 3, + FullDuplex100 = 4, +}; +class WIZnet_Chip { +public: +enum Protocol { + CLOSED = 0, + TCP = 1, + UDP = 2, +}; + +enum Command { + OPEN = 0x01, + LISTEN = 0x02, + CONNECT = 0x04, + DISCON = 0x08, + CLOSE = 0x10, + SEND = 0x20, + SEND_MAC = 0x21, + SEND_KEEP = 0x22, + RECV = 0x40, + +}; + +enum Interrupt { + INT_CON = 0x01, + INT_DISCON = 0x02, + INT_RECV = 0x04, + INT_TIMEOUT = 0x08, + INT_SEND_OK = 0x10, +}; + +enum Status { + SOCK_CLOSED = 0x00, + SOCK_INIT = 0x13, + SOCK_LISTEN = 0x14, + SOCK_SYNSENT = 0x15, + SOCK_ESTABLISHED = 0x17, + SOCK_CLOSE_WAIT = 0x1c, + SOCK_UDP = 0x22, +}; + + + uint16_t sock_any_port; + + /* + * Constructor + * + * @param spi spi class + * @param cs cs of the W5500 + * @param reset reset pin of the W5500 + */ + WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); + WIZnet_Chip(SPI* spi, PinName cs, PinName reset); + + /* + * Set MAC Address to W5500 + * + * @return true if connected, false otherwise + */ + bool setmac(); + + /* + * Set Network Informations (SrcIP, Netmask, Gataway) + * + * @return true if connected, false otherwise + */ + bool setip(); + + /* + * Disconnect the connection + * + * @ returns true + */ + bool disconnect(); + + /* + * Open a tcp connection with the specified host on the specified port + * + * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) + * @param port port + * @ returns true if successful + */ + bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000); + + /* + * Set the protocol (UDP or TCP) + * + * @param p protocol + * @ returns true if successful + */ + bool setProtocol(int socket, Protocol p); + + /* + * Reset the W5500 + */ + void reset(); + + int wait_readable(int socket, int wait_time_ms, int req_size = 0); + + int wait_writeable(int socket, int wait_time_ms, int req_size = 0); + + /* + * Check if an ethernet link is pressent or not. + * + * @returns true if successful + */ + bool link(int wait_time_ms= 3*1000); + + /* + * Sets the speed and duplex parameters of an ethernet link. + * + * @returns true if successful + */ + void set_link(PHYMode phymode); + + /* + * Check if a tcp link is active + * + * @returns true if successful + */ + bool is_connected(int socket); + + /* + * Close a tcp connection + * + * @ returns true if successful + */ + bool close(int socket); + + /* + * @param str string to be sent + * @param len string length + */ + int send(int socket, const char * str, int len); + + int recv(int socket, char* buf, int len); + + /* + * Return true if the module is using dhcp + * + * @returns true if the module is using dhcp + */ + bool isDHCP() { + return dhcp; + } + + bool gethostbyname(const char* host, uint32_t* ip); + + static WIZnet_Chip * getInstance() { + return inst; + }; + + int new_socket(); + uint16_t new_port(); + void scmd(int socket, Command cmd); + + template<typename T> + void sreg(int socket, uint16_t addr, T data) { + reg_wr<T>(addr, (0x0C + (socket << 5)), data); + } + + template<typename T> + T sreg(int socket, uint16_t addr) { + return reg_rd<T>(addr, (0x08 + (socket << 5))); + } + + template<typename T> + void reg_wr(uint16_t addr, T data) { + return reg_wr(addr, 0x04, data); + } + + template<typename T> + void reg_wr(uint16_t addr, uint8_t cb, T data) { + uint8_t buf[sizeof(T)]; + *reinterpret_cast<T*>(buf) = data; + for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian + uint8_t t = buf[i]; + buf[i] = buf[sizeof(buf)-1-i]; + buf[sizeof(buf)-1-i] = t; + } + spi_write(addr, cb, buf, sizeof(buf)); + } + + template<typename T> + T reg_rd(uint16_t addr) { + return reg_rd<T>(addr, 0x00); + } + + template<typename T> + T reg_rd(uint16_t addr, uint8_t cb) { + uint8_t buf[sizeof(T)]; + spi_read(addr, cb, buf, sizeof(buf)); + for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian + uint8_t t = buf[i]; + buf[i] = buf[sizeof(buf)-1-i]; + buf[sizeof(buf)-1-i] = t; + } + return *reinterpret_cast<T*>(buf); + } + + void reg_rd_mac(uint16_t addr, uint8_t* data); +/* { + spi_read(addr, 0x00, data, 6); + }*/ + + void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip); + /* { + uint8_t buf[4]; + char* p = (char*)ip; + for(int i = 0; i < 4; i++) { + buf[i] = atoi(p); + p = strchr(p, '.'); + if (p == NULL) { + break; + } + p++; + } + spi_write(addr, cb, buf, sizeof(buf)); + } + */ + void sreg_ip(int socket, uint16_t addr, const char* ip); +/* { + reg_wr_ip(addr, (0x0C + (socket << 5)), ip); + }*/ + + void reg_rd_ip_byte(uint16_t addr, uint8_t* data); +/* { + spi_read(addr, 0x00, data, 4); + }*/ + + void reg_wr_ip_byte(uint16_t addr, uint8_t* data); +/* { + spi_write(addr, 0x04, data, 4); + }*/ + +///////////////////////////////// +// Common Register I/O function // +///////////////////////////////// +/** + * @ingroup Common_register_access_function + * @brief Set Mode Register + * @param (uint8_t)mr The value to be set. + * @sa getMR() + */ + void setMR(uint8_t mr) { + reg_wr<uint8_t>(MR,mr); + } + + +/** + * @ingroup Common_register_access_function + * @brief Get Mode Register + * @return uint8_t. The value of Mode register. + * @sa setMR() + */ + uint8_t getMR() { + return reg_rd<uint8_t>(MR); + } + +/** + * @ingroup Common_register_access_function + * @brief Set gateway IP address + * @param (uint8_t*)gar Pointer variable to set gateway IP address. It should be allocated 4 bytes. + * @sa getGAR() + */ + void setGAR(uint8_t * gar) { + reg_wr_ip_byte(GAR,gar); + } + +/** + * @ingroup Common_register_access_function + * @brief Get gateway IP address + * @param (uint8_t*)gar Pointer variable to get gateway IP address. It should be allocated 4 bytes. + * @sa setGAR() + */ + void getGAR(uint8_t * gar) { + reg_rd_ip_byte(GAR,gar); + } + +/** + * @ingroup Common_register_access_function + * @brief Set subnet mask address + * @param (uint8_t*)subr Pointer variable to set subnet mask address. It should be allocated 4 bytes. + * @sa getSUBR() + */ + void setSUBR(uint8_t * subr) { + reg_wr_ip_byte(SUBR, subr); + } + + +/** + * @ingroup Common_register_access_function + * @brief Get subnet mask address + * @param (uint8_t*)subr Pointer variable to get subnet mask address. It should be allocated 4 bytes. + * @sa setSUBR() + */ + void getSUBR(uint8_t * subr) { + reg_rd_ip_byte(SUBR, subr); + } + +/** + * @ingroup Common_register_access_function + * @brief Set local MAC address + * @param (uint8_t*)shar Pointer variable to set local MAC address. It should be allocated 6 bytes. + * @sa getSHAR() + */ + void setSHAR(uint8_t * shar) { + reg_wr_mac(SHAR, shar); + } + +/** + * @ingroup Common_register_access_function + * @brief Get local MAC address + * @param (uint8_t*)shar Pointer variable to get local MAC address. It should be allocated 6 bytes. + * @sa setSHAR() + */ + void getSHAR(uint8_t * shar) { + reg_rd_mac(SHAR, shar); + } + +/** + * @ingroup Common_register_access_function + * @brief Set local IP address + * @param (uint8_t*)sipr Pointer variable to set local IP address. It should be allocated 4 bytes. + * @sa getSIPR() + */ + void setSIPR(uint8_t * sipr) { + reg_wr_ip_byte(SIPR, sipr); + } + +/** + * @ingroup Common_register_access_function + * @brief Get local IP address + * @param (uint8_t*)sipr Pointer variable to get local IP address. It should be allocated 4 bytes. + * @sa setSIPR() + */ + void getSIPR(uint8_t * sipr) { + reg_rd_ip_byte(SIPR, sipr); + } + +/** + * @ingroup Common_register_access_function + * @brief Set @ref IR register + * @param (uint8_t)ir Value to set @ref IR register. + * @sa getIR() + */ + void setIR(uint8_t ir) { + reg_wr<uint8_t>(IR, (ir & 0xF0)); + } + +/** + * @ingroup Common_register_access_function + * @brief Get @ref IR register + * @return uint8_t. Value of @ref IR register. + * @sa setIR() + */ + uint8_t getIR() { + return reg_rd<uint8_t>(IR & 0xF0); + } + +/** + * @ingroup Common_register_access_function + * @brief Set @ref IMR register + * @param (uint8_t)imr Value to set @ref IMR register. + * @sa getIMR() + */ + void setIMR(uint8_t imr) { + reg_wr<uint8_t>(IMR, imr); + } + +/** + * @ingroup Common_register_access_function + * @brief Get @ref IMR register + * @return uint8_t. Value of @ref IMR register. + * @sa setIMR() + */ + uint8_t getIMR() { + return reg_rd<uint8_t>(IMR); + } + + +/** + * @ingroup Common_register_access_function + * @brief Set @ref SIR register + * @param (uint8_t)sir Value to set @ref SIR register. + * @sa getSIR() + */ + void setSIR(uint8_t sir) { + reg_wr<uint8_t>(SIR, sir); + } + +/** + * @ingroup Common_register_access_function + * @brief Get @ref SIR register + * @return uint8_t. Value of @ref SIR register. + * @sa setSIR() + */ + uint8_t getSIR() { + return reg_rd<uint8_t>(SIR); + } +/** + * @ingroup Common_register_access_function + * @brief Set @ref SIMR register + * @param (uint8_t)simr Value to set @ref SIMR register. + * @sa getSIMR() + */ + void setSIMR(uint8_t simr) { + reg_wr<uint8_t>(SIMR, simr); + } + +/** + * @ingroup Common_register_access_function + * @brief Get @ref SIMR register + * @return uint8_t. Value of @ref SIMR register. + * @sa setSIMR() + */ + uint8_t getSIMR() { + return reg_rd<uint8_t>(SIMR); + } + +/** + * @ingroup Common_register_access_function + * @brief Set @ref RTR register + * @param (uint16_t)rtr Value to set @ref RTR register. + * @sa getRTR() + */ + void setRTR(uint16_t rtr) { + reg_wr<uint16_t>(RTR, rtr); + } + +/** + * @ingroup Common_register_access_function + * @brief Get @ref RTR register + * @return uint16_t. Value of @ref RTR register. + * @sa setRTR() + */ + uint16_t getRTR() { + return reg_rd<uint16_t>(RTR); + } + +/** + * @ingroup Common_register_access_function + * @brief Set @ref RCR register + * @param (uint8_t)rcr Value to set @ref RCR register. + * @sa getRCR() + */ + void setRCR(uint8_t rcr) { + reg_wr<uint8_t>(RCR, rcr); + } + +/** + * @ingroup Common_register_access_function + * @brief Get @ref RCR register + * @return uint8_t. Value of @ref RCR register. + * @sa setRCR() + */ + uint8_t getRCR() { + return reg_rd<uint8_t>(RCR); + } + +//================================================== test done =========================================================== + +/** + * @ingroup Common_register_access_function + * @brief Set @ref PHYCFGR register + * @param (uint8_t)phycfgr Value to set @ref PHYCFGR register. + * @sa setPHYCFGR() + */ + void setPHYCFGR(uint8_t phycfgr) { + reg_wr<uint8_t>(PHYCFGR, phycfgr); + } + +/** + * @ingroup Common_register_access_function + * @brief Get @ref PHYCFGR register + * @return uint8_t. Value of @ref PHYCFGR register. + * @sa getPHYCFGR() + */ + uint8_t getPHYCFGR() { + return reg_rd<uint8_t>(PHYCFGR); + } + + +///////////////////////////////////// + +/////////////////////////////////// +// Socket N register I/O function // +/////////////////////////////////// +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_MR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint8_t)mr Value to set @ref Sn_MR + * @sa getSn_MR() + */ + void setSn_MR(uint8_t sn, uint8_t mr) { + sreg<uint8_t>(sn, MR, mr); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_MR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint8_t. Value of @ref Sn_MR. + * @sa setSn_MR() + */ + uint8_t getSn_MR(uint8_t sn) { + return sreg<uint8_t>(sn, Sn_MR); + } + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_CR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint8_t)cr Value to set @ref Sn_CR + * @sa getSn_CR() + */ + void setSn_CR(uint8_t sn, uint8_t cr) { + sreg<uint8_t>(sn, Sn_CR, cr); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_CR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint8_t. Value of @ref Sn_CR. + * @sa setSn_CR() + */ + uint8_t getSn_CR(uint8_t sn) { + return sreg<uint8_t>(sn, Sn_CR); + } + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_IR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint8_t)ir Value to set @ref Sn_IR + * @sa getSn_IR() + */ + void setSn_IR(uint8_t sn, uint8_t ir) { + sreg<uint8_t>(sn, Sn_IR, (ir & 0x1F)); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_IR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint8_t. Value of @ref Sn_IR. + * @sa setSn_IR() + */ + uint8_t getSn_IR(uint8_t sn) { + return (sreg<uint8_t>(sn, Sn_IR)) & 0x1F; + } + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_IMR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint8_t)imr Value to set @ref Sn_IMR + * @sa getSn_IMR() + */ + void setSn_IMR(uint8_t sn, uint8_t imr) { + sreg<uint8_t>(sn, Sn_IMR, (imr & 0x1F)); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_IMR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint8_t. Value of @ref Sn_IMR. + * @sa setSn_IMR() + */ + uint8_t getSn_IMR(uint8_t sn) { + return (sreg<uint8_t>(sn, Sn_IMR)) & 0x1F; + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_SR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint8_t. Value of @ref Sn_SR. + */ + uint8_t getSn_SR(uint8_t sn) { + return sreg<uint8_t>(sn, Sn_SR); + } + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_PORT register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint16_t)port Value to set @ref Sn_PORT. + * @sa getSn_PORT() + */ + void setSn_PORT(uint8_t sn, uint16_t port) { + sreg<uint16_t>(sn, Sn_PORT, port ); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_PORT register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint16_t. Value of @ref Sn_PORT. + * @sa setSn_PORT() + */ + uint16_t getSn_PORT(uint8_t sn) { + return sreg<uint16_t>(sn, Sn_PORT); + } + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_DHAR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint8_t*)dhar Pointer variable to set socket n destination hardware address. It should be allocated 6 bytes. + * @sa getSn_DHAR() + */ + void setSn_DHAR(uint8_t sn, uint8_t * dhar) { + spi_write(Sn_DHAR, (0x0C + (sn << 5)), dhar, 6); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_MR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint8_t*)dhar Pointer variable to get socket n destination hardware address. It should be allocated 6 bytes. + * @sa setSn_DHAR() + */ + void getSn_DHAR(uint8_t sn, uint8_t * dhar) { + spi_read(Sn_DHAR, (0x08 + (sn << 5)), dhar, 6); + } + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_DIPR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint8_t*)dipr Pointer variable to set socket n destination IP address. It should be allocated 4 bytes. + * @sa getSn_DIPR() + */ + void setSn_DIPR(uint8_t sn, uint8_t * dipr) { + spi_write(Sn_DIPR, (0x0C + (sn << 5)), dipr, 4); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_DIPR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint8_t*)dipr Pointer variable to get socket n destination IP address. It should be allocated 4 bytes. + * @sa SetSn_DIPR() + */ + void getSn_DIPR(uint8_t sn, uint8_t * dipr) { + spi_read(Sn_DIPR, (0x08 + (sn << 5)), dipr, 4); + } + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_DPORT register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint16_t)dport Value to set @ref Sn_DPORT + * @sa getSn_DPORT() + */ + void setSn_DPORT(uint8_t sn, uint16_t dport) { + sreg<uint16_t>(sn, Sn_DPORT, dport); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_DPORT register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint16_t. Value of @ref Sn_DPORT. + * @sa setSn_DPORT() + */ + uint16_t getSn_DPORT(uint8_t sn) { + return sreg<uint16_t>(sn, Sn_DPORT); + } + + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_RXBUF_SIZE register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint8_t)rxbufsize Value to set @ref Sn_RXBUF_SIZE + * @sa getSn_RXBUF_SIZE() + */ + void setSn_RXBUF_SIZE(uint8_t sn, uint8_t rxbufsize) { + sreg<uint8_t>(sn, Sn_RXBUF_SIZE ,rxbufsize); + } + + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_RXBUF_SIZE register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint8_t. Value of @ref Sn_RXBUF_SIZE. + * @sa setSn_RXBUF_SIZE() + */ + uint8_t getSn_RXBUF_SIZE(uint8_t sn) { + return sreg<uint8_t>(sn, Sn_RXBUF_SIZE); + } + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_TXBUF_SIZE register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint8_t)txbufsize Value to set @ref Sn_TXBUF_SIZE + * @sa getSn_TXBUF_SIZE() + */ + void setSn_TXBUF_SIZE(uint8_t sn, uint8_t txbufsize) { + sreg<uint8_t>(sn, Sn_TXBUF_SIZE, txbufsize); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_TXBUF_SIZE register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint8_t. Value of @ref Sn_TXBUF_SIZE. + * @sa setSn_TXBUF_SIZE() + */ + uint8_t getSn_TXBUF_SIZE(uint8_t sn) { + return sreg<uint8_t>(sn, Sn_TXBUF_SIZE); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_TX_FSR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint16_t. Value of @ref Sn_TX_FSR. + */ + uint16_t getSn_TX_FSR(uint8_t sn) { + return sreg<uint16_t>(sn, Sn_TX_FSR); + } + + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_TX_RD register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint16_t. Value of @ref Sn_TX_RD. + */ + uint16_t getSn_TX_RD(uint8_t sn) { + return sreg<uint16_t>(sn, Sn_TX_RD); + } + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_TX_WR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint16_t)txwr Value to set @ref Sn_TX_WR + * @sa GetSn_TX_WR() + */ + void setSn_TX_WR(uint8_t sn, uint16_t txwr) { + sreg<uint16_t>(sn, Sn_TX_WR, txwr); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_TX_WR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint16_t. Value of @ref Sn_TX_WR. + * @sa setSn_TX_WR() + */ + uint16_t getSn_TX_WR(uint8_t sn) { + return sreg<uint16_t>(sn, Sn_TX_WR); + } + + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_RX_RSR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint16_t. Value of @ref Sn_RX_RSR. + */ + uint16_t getSn_RX_RSR(uint8_t sn) { + return sreg<uint16_t>(sn, Sn_RX_RSR); + } + + +/** + * @ingroup Socket_register_access_function + * @brief Set @ref Sn_RX_RD register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @param (uint16_t)rxrd Value to set @ref Sn_RX_RD + * @sa getSn_RX_RD() + */ + void setSn_RX_RD(uint8_t sn, uint16_t rxrd) { + sreg<uint16_t>(sn, Sn_RX_RD, rxrd); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_RX_RD register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @regurn uint16_t. Value of @ref Sn_RX_RD. + * @sa setSn_RX_RD() + */ + uint16_t getSn_RX_RD(uint8_t sn) { + return sreg<uint16_t>(sn, Sn_RX_RD); + } + +/** + * @ingroup Socket_register_access_function + * @brief Get @ref Sn_RX_WR register + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint16_t. Value of @ref Sn_RX_WR. + */ + uint16_t getSn_RX_WR(uint8_t sn) { + return sreg<uint16_t>(sn, Sn_RX_WR); + } + + +////////////////////////////////////// + +///////////////////////////////////// +// Sn_TXBUF & Sn_RXBUF IO function // +///////////////////////////////////// +/** + * @brief Gets the max buffer size of socket sn passed as parameter. + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint16_t. Value of Socket n RX max buffer size. + */ + uint16_t getSn_RxMAX(uint8_t sn) { + return (getSn_RXBUF_SIZE(sn) << 10); + } + +/** + * @brief Gets the max buffer size of socket sn passed as parameters. + * @param (uint8_t)sn Socket number. It should be <b>0 ~ 7</b>. + * @return uint16_t. Value of Socket n TX max buffer size. + */ +//uint16_t getSn_TxMAX(uint8_t sn); + uint16_t getSn_TxMAX(uint8_t sn) { + return (getSn_TXBUF_SIZE(sn) << 10); + } + + + int ethernet_link(void); + void ethernet_set_link(int speed, int duplex); +protected: + uint8_t mac[6]; + uint32_t ip; + uint32_t netmask; + uint32_t gateway; + uint32_t dnsaddr; + bool dhcp; + + void spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len); + void spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len); + SPI* spi; + DigitalOut cs; + DigitalOut reset_pin; + static WIZnet_Chip* inst; + + void reg_wr_mac(uint16_t addr, uint8_t* data) { + spi_write(addr, 0x04, data, 6); + } + +}; + + +extern uint32_t str_to_ip(const char* str); +extern void printfBytes(char* str, uint8_t* buf, int len); +extern void printHex(uint8_t* buf, int len); +extern void debug_hex(uint8_t* buf, int len);
diff -r d8773cd4edc5 -r 3e61863c1f67 arch/int/W7500x_toe.cpp --- a/arch/int/W7500x_toe.cpp Wed Jul 01 04:02:50 2015 +0000 +++ b/arch/int/W7500x_toe.cpp Tue Jul 14 10:16:16 2015 +0000 @@ -14,13 +14,14 @@ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "eth_arch.h" +#include "eth_arch.hpp" #if defined(TARGET_WIZwiki_W7500) #include "mbed.h" #include "mbed_debug.h" -#include "DNSClient.h" +#include "DNSClient.hpp" +using namespace wiznet_space; /* * MDIO via GPIO
diff -r d8773cd4edc5 -r 3e61863c1f67 arch/int/W7500x_toe.h --- a/arch/int/W7500x_toe.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,300 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ -#pragma once - -#include "mbed.h" -#include "mbed_debug.h" - -#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; - -#define DEFAULT_WAIT_RESP_TIMEOUT 500 - - -#define MAX_SOCK_NUM 8 - -// Peripheral base address -#define W7500x_WZTOE_BASE (0x46000000) -#define W7500x_TXMEM_BASE (W7500x_WZTOE_BASE + 0x00020000) -#define W7500x_RXMEM_BASE (W7500x_WZTOE_BASE + 0x00030000) -// Common register -#define MR (0x2300) -#define GAR (0x6008) -#define SUBR (0x600C) -#define SHAR (0x6000) -#define SIPR (0x6010) - -// Added Common register @W7500 -#define TIC100US (0x2000) - -// Socket register -#define Sn_MR (0x0000) -#define Sn_CR (0x0010) -#define Sn_IR (0x0020) //--Sn_ISR -#define Sn_SR (0x0030) -#define Sn_PORT (0x0114) -#define Sn_DIPR (0x0124) -#define Sn_DPORT (0x0120) -#define Sn_RXBUF_SIZE (0x0200) -#define Sn_TXBUF_SIZE (0x0220) -#define Sn_TX_FSR (0x0204) -#define Sn_TX_WR (0x020C) -#define Sn_RX_RSR (0x0224) -#define Sn_RX_RD (0x0228) -// added Socket register @W7500 -#define Sn_ICR (0x0028) -enum PHYMode { - AutoNegotiate = 0, - HalfDuplex10 = 1, - FullDuplex10 = 2, - HalfDuplex100 = 3, - FullDuplex100 = 4, -}; - -//bool plink(int wait_time_ms= 3*1000); - -class WIZnet_Chip { -public: -enum Protocol { - CLOSED = 0, - TCP = 1, - UDP = 2, -}; - -enum Command { - OPEN = 0x01, - LISTEN = 0x02, - CONNECT = 0x04, - DISCON = 0x08, - CLOSE = 0x10, - SEND = 0x20, - SEND_MAC = 0x21, - SEND_KEEP = 0x22, - RECV = 0x40, - -}; - -enum Interrupt { - INT_CON = 0x01, - INT_DISCON = 0x02, - INT_RECV = 0x04, - INT_TIMEOUT = 0x08, - INT_SEND_OK = 0x10, -}; -enum Status { - SOCK_CLOSED = 0x00, - SOCK_INIT = 0x13, - SOCK_LISTEN = 0x14, - SOCK_SYNSENT = 0x15, - SOCK_ESTABLISHED = 0x17, - SOCK_CLOSE_WAIT = 0x1c, - SOCK_UDP = 0x22, -}; -enum Mode { - MR_RST = 0x80, - MR_WOL = 0x20, - MR_PB = 0x10, - MR_FARP = 0x02, -}; - - WIZnet_Chip(); - - /* - * Set MAC Address to W7500x_TOE - * - * @return true if connected, false otherwise - */ - bool setmac(); - - /* - * Connect the W7500 WZTOE to the ssid contained in the constructor. - * - * @return true if connected, false otherwise - */ - bool setip(); - - - /* - * Open a tcp connection with the specified host on the specified port - * - * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) - * @param port port - * @ returns true if successful - */ - bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000); - - /* - * Set the protocol (UDP or TCP) - * - * @param p protocol - * @ returns true if successful - */ - bool setProtocol(int socket, Protocol p); - - /* - * Reset the W7500 WZTOE - */ - void reset(); - - int wait_readable(int socket, int wait_time_ms, int req_size = 0); - - int wait_writeable(int socket, int wait_time_ms, int req_size = 0); - - /* - * Check if an ethernet link is pressent or not. - * - * @returns true if successful - */ - bool link(int wait_time_ms= 3*1000); - - /* - * Sets the speed and duplex parameters of an ethernet link. - * - * @returns true if successful - */ - void set_link(PHYMode phymode); - - /* - * Check if a tcp link is active - * - * @returns true if successful - */ - bool is_connected(int socket); - - /* - * Close a tcp connection - * - * @ returns true if successful - */ - bool close(int socket); - - /* - * @param str string to be sent - * @param len string length - */ - int send(int socket, const char * str, int len); - - int recv(int socket, char* buf, int len); - - /* - * Return true if the module is using dhcp - * - * @returns true if the module is using dhcp - */ - bool isDHCP() { - return dhcp; - } - - bool gethostbyname(const char* host, uint32_t* ip); - - static WIZnet_Chip * getInstance() { - return inst; - }; - - int new_socket(); - uint16_t new_port(); - - void scmd(int socket, Command cmd); - - template<typename T> - void sreg(int socket, uint16_t addr, T data) { - reg_wr<T>(addr, (uint8_t)(0x01+(socket<<2)), data); - } - - template<typename T> - T sreg(int socket, uint16_t addr) { - return reg_rd<T>(addr, (uint8_t)(0x01+(socket<<2))); - } - - template<typename T> - void reg_wr(uint16_t addr, T data) { - return reg_wr(addr, 0x00, data); - } - - template<typename T> - void reg_wr(uint16_t addr, uint8_t cb, T data) { - uint8_t buf[sizeof(T)]; - *reinterpret_cast<T*>(buf) = data; - /* - for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian - uint8_t t = buf[i]; - buf[i] = buf[sizeof(buf)-1-i]; - buf[sizeof(buf)-1-i] = t; - } - */ - for(int i = 0; i < sizeof(buf); i++) { // Little Endian to Big Endian - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)((cb<<16)+addr)+i) = buf[i]; - } - } - - template<typename T> - T reg_rd(uint16_t addr) { - return reg_rd<T>(addr, (uint8_t)(0x00)); - } - - template<typename T> - T reg_rd(uint16_t addr, uint8_t cb) { - uint8_t buf[sizeof(T)] = {0,}; - for(int i = 0; i < sizeof(buf); i++) { // Little Endian to Big Endian - buf[i] = *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)((cb<<16)+addr)+i); - } - /* - for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian - uint8_t t = buf[i]; - buf[i] = buf[sizeof(buf)-1-i]; - buf[sizeof(buf)-1-i] = t; - } - */ - return *reinterpret_cast<T*>(buf); - } - - void reg_rd_mac(uint16_t addr, uint8_t* data); - - void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip); - - void sreg_ip(int socket, uint16_t addr, const char* ip); - - int ethernet_link(void); - - void ethernet_set_link(int speed, int duplex); - - -protected: - uint8_t mac[6]; - uint32_t ip; - uint32_t netmask; - uint32_t gateway; - uint32_t dnsaddr; - bool dhcp; - - static WIZnet_Chip* inst; - - void reg_wr_mac(uint16_t addr, uint8_t* data) { - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+3)) = data[0] ; - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+2)) = data[1] ; - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+1)) = data[2] ; - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+0)) = data[3] ; - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+7)) = data[4] ; - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+6)) = data[5] ; - } -}; - -extern uint32_t str_to_ip(const char* str); -extern void printfBytes(char* str, uint8_t* buf, int len); -extern void printHex(uint8_t* buf, int len); -extern void debug_hex(uint8_t* buf, int len);
diff -r d8773cd4edc5 -r 3e61863c1f67 arch/int/W7500x_toe.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arch/int/W7500x_toe.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,300 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ +#pragma once + +#include "mbed.h" +#include "mbed_debug.h" + +#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; + +#define DEFAULT_WAIT_RESP_TIMEOUT 500 + + +#define MAX_SOCK_NUM 8 + +// Peripheral base address +#define W7500x_WZTOE_BASE (0x46000000) +#define W7500x_TXMEM_BASE (W7500x_WZTOE_BASE + 0x00020000) +#define W7500x_RXMEM_BASE (W7500x_WZTOE_BASE + 0x00030000) +// Common register +#define MR (0x2300) +#define GAR (0x6008) +#define SUBR (0x600C) +#define SHAR (0x6000) +#define SIPR (0x6010) + +// Added Common register @W7500 +#define TIC100US (0x2000) + +// Socket register +#define Sn_MR (0x0000) +#define Sn_CR (0x0010) +#define Sn_IR (0x0020) //--Sn_ISR +#define Sn_SR (0x0030) +#define Sn_PORT (0x0114) +#define Sn_DIPR (0x0124) +#define Sn_DPORT (0x0120) +#define Sn_RXBUF_SIZE (0x0200) +#define Sn_TXBUF_SIZE (0x0220) +#define Sn_TX_FSR (0x0204) +#define Sn_TX_WR (0x020C) +#define Sn_RX_RSR (0x0224) +#define Sn_RX_RD (0x0228) +// added Socket register @W7500 +#define Sn_ICR (0x0028) +enum PHYMode { + AutoNegotiate = 0, + HalfDuplex10 = 1, + FullDuplex10 = 2, + HalfDuplex100 = 3, + FullDuplex100 = 4, +}; + +//bool plink(int wait_time_ms= 3*1000); + +class WIZnet_Chip { +public: +enum Protocol { + CLOSED = 0, + TCP = 1, + UDP = 2, +}; + +enum Command { + OPEN = 0x01, + LISTEN = 0x02, + CONNECT = 0x04, + DISCON = 0x08, + CLOSE = 0x10, + SEND = 0x20, + SEND_MAC = 0x21, + SEND_KEEP = 0x22, + RECV = 0x40, + +}; + +enum Interrupt { + INT_CON = 0x01, + INT_DISCON = 0x02, + INT_RECV = 0x04, + INT_TIMEOUT = 0x08, + INT_SEND_OK = 0x10, +}; +enum Status { + SOCK_CLOSED = 0x00, + SOCK_INIT = 0x13, + SOCK_LISTEN = 0x14, + SOCK_SYNSENT = 0x15, + SOCK_ESTABLISHED = 0x17, + SOCK_CLOSE_WAIT = 0x1c, + SOCK_UDP = 0x22, +}; +enum Mode { + MR_RST = 0x80, + MR_WOL = 0x20, + MR_PB = 0x10, + MR_FARP = 0x02, +}; + + WIZnet_Chip(); + + /* + * Set MAC Address to W7500x_TOE + * + * @return true if connected, false otherwise + */ + bool setmac(); + + /* + * Connect the W7500 WZTOE to the ssid contained in the constructor. + * + * @return true if connected, false otherwise + */ + bool setip(); + + + /* + * Open a tcp connection with the specified host on the specified port + * + * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) + * @param port port + * @ returns true if successful + */ + bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000); + + /* + * Set the protocol (UDP or TCP) + * + * @param p protocol + * @ returns true if successful + */ + bool setProtocol(int socket, Protocol p); + + /* + * Reset the W7500 WZTOE + */ + void reset(); + + int wait_readable(int socket, int wait_time_ms, int req_size = 0); + + int wait_writeable(int socket, int wait_time_ms, int req_size = 0); + + /* + * Check if an ethernet link is pressent or not. + * + * @returns true if successful + */ + bool link(int wait_time_ms= 3*1000); + + /* + * Sets the speed and duplex parameters of an ethernet link. + * + * @returns true if successful + */ + void set_link(PHYMode phymode); + + /* + * Check if a tcp link is active + * + * @returns true if successful + */ + bool is_connected(int socket); + + /* + * Close a tcp connection + * + * @ returns true if successful + */ + bool close(int socket); + + /* + * @param str string to be sent + * @param len string length + */ + int send(int socket, const char * str, int len); + + int recv(int socket, char* buf, int len); + + /* + * Return true if the module is using dhcp + * + * @returns true if the module is using dhcp + */ + bool isDHCP() { + return dhcp; + } + + bool gethostbyname(const char* host, uint32_t* ip); + + static WIZnet_Chip * getInstance() { + return inst; + }; + + int new_socket(); + uint16_t new_port(); + + void scmd(int socket, Command cmd); + + template<typename T> + void sreg(int socket, uint16_t addr, T data) { + reg_wr<T>(addr, (uint8_t)(0x01+(socket<<2)), data); + } + + template<typename T> + T sreg(int socket, uint16_t addr) { + return reg_rd<T>(addr, (uint8_t)(0x01+(socket<<2))); + } + + template<typename T> + void reg_wr(uint16_t addr, T data) { + return reg_wr(addr, 0x00, data); + } + + template<typename T> + void reg_wr(uint16_t addr, uint8_t cb, T data) { + uint8_t buf[sizeof(T)]; + *reinterpret_cast<T*>(buf) = data; + /* + for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian + uint8_t t = buf[i]; + buf[i] = buf[sizeof(buf)-1-i]; + buf[sizeof(buf)-1-i] = t; + } + */ + for(int i = 0; i < sizeof(buf); i++) { // Little Endian to Big Endian + *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)((cb<<16)+addr)+i) = buf[i]; + } + } + + template<typename T> + T reg_rd(uint16_t addr) { + return reg_rd<T>(addr, (uint8_t)(0x00)); + } + + template<typename T> + T reg_rd(uint16_t addr, uint8_t cb) { + uint8_t buf[sizeof(T)] = {0,}; + for(int i = 0; i < sizeof(buf); i++) { // Little Endian to Big Endian + buf[i] = *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)((cb<<16)+addr)+i); + } + /* + for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian + uint8_t t = buf[i]; + buf[i] = buf[sizeof(buf)-1-i]; + buf[sizeof(buf)-1-i] = t; + } + */ + return *reinterpret_cast<T*>(buf); + } + + void reg_rd_mac(uint16_t addr, uint8_t* data); + + void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip); + + void sreg_ip(int socket, uint16_t addr, const char* ip); + + int ethernet_link(void); + + void ethernet_set_link(int speed, int duplex); + + +protected: + uint8_t mac[6]; + uint32_t ip; + uint32_t netmask; + uint32_t gateway; + uint32_t dnsaddr; + bool dhcp; + + static WIZnet_Chip* inst; + + void reg_wr_mac(uint16_t addr, uint8_t* data) { + *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+3)) = data[0] ; + *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+2)) = data[1] ; + *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+1)) = data[2] ; + *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+0)) = data[3] ; + *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+7)) = data[4] ; + *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+6)) = data[5] ; + } +}; + +extern uint32_t str_to_ip(const char* str); +extern void printfBytes(char* str, uint8_t* buf, int len); +extern void printHex(uint8_t* buf, int len); +extern void debug_hex(uint8_t* buf, int len);
diff -r d8773cd4edc5 -r 3e61863c1f67 eth_arch.h --- a/eth_arch.h Wed Jul 01 04:02:50 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ - -#pragma once - -#if defined(TARGET_WIZwiki_W7500) - -#include "W7500x_toe.h" -#define __DEF_USED_IC101AG__ //For using IC+101AG@WIZwiki-W7500 - -#else - -#include "W5500.h" // W5500 Ethernet Shield -//#define USE_WIZ550IO_MAC // WIZ550io; using the MAC address - -#endif
diff -r d8773cd4edc5 -r 3e61863c1f67 eth_arch.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/eth_arch.hpp Tue Jul 14 10:16:16 2015 +0000 @@ -0,0 +1,32 @@ +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#pragma once + +#if defined(TARGET_WIZwiki_W7500) + +#include "W7500x_toe.hpp" +#define __DEF_USED_IC101AG__ //For using IC+101AG@WIZwiki-W7500 + +#else + +#include "W5500.hpp" // W5500 Ethernet Shield +//#define USE_WIZ550IO_MAC // WIZ550io; using the MAC address + +#endif