Bohyun Bang / WIZnetInterface

Dependents:   w7500-paho-mqtt openHAB_mqtt_W7500 kakaoIoTchatbot w7500-RFID-mqtt

Fork of WIZnetInterface by WIZnet

Files at this revision

API Documentation at this revision

Comitter:
Soohwan Kim
Date:
Tue Jun 16 11:11:11 2015 +0900
Parent:
7:da6fcec0f3fe
Child:
9:40d25592e6a0
Commit message:
amened

Changed in this revision

EthernetInterface.h Show annotated file Show diff for this revision Revisions of this file
arch/ext/.W5500.h.swp Show diff for this revision Revisions of this file
arch/ext/W5500.cpp Show annotated file Show diff for this revision Revisions of this file
arch/ext/W5500.h Show annotated file Show diff for this revision Revisions of this file
arch/int/W7500x_toe.cpp Show annotated file Show diff for this revision Revisions of this file
arch/int/W7500x_toe.h Show annotated file Show diff for this revision Revisions of this file
eth_arch.h Show annotated file Show diff for this revision Revisions of this file
--- a/EthernetInterface.h	Mon Jun 15 23:43:56 2015 +0000
+++ b/EthernetInterface.h	Tue Jun 16 11:11:11 2015 +0900
@@ -23,10 +23,21 @@
  */
 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
Binary file arch/ext/.W5500.h.swp has changed
--- a/arch/ext/W5500.cpp	Mon Jun 15 23:43:56 2015 +0000
+++ b/arch/ext/W5500.cpp	Tue Jun 16 11:11:11 2015 +0900
@@ -16,7 +16,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 #include "eth_arch.h"
-#ifdef USE_W5500
+#if not defined(TARGET_WIZwiki_W7500)
 
 #include "mbed.h"
 #include "mbed_debug.h"
@@ -82,15 +82,6 @@
     return true;
 }
 
-bool WIZnet_Chip::linkstatus()
-{
-    if ( (reg_rd<uint8_t>(PHYCFGR) & 0x01) != 0x01 )
-        return false;
-
-    return true;
-}
-
-
 bool WIZnet_Chip::setProtocol(int socket, Protocol p)
 {
     if (socket < 0) {
@@ -336,6 +327,40 @@
     return port;
 }
 
+bool WIZnet_Chip::link(int wait_time_ms)
+{
+	Timer t;
+	t.reset();
+	t.start();
+	while(1) {
+		int is_link = ethernet_link();
+		printf("is_link:%d\r\n", is_link);
+		if (is_link) {
+			return true;
+		}
+		if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
+			break;
+		}
+	}
+	return 0;
+}
+
+void WIZnet_Chip::set_link(PHYMode phymode)
+{
+	int speed = -1;
+	int duplex = 0;
+
+	switch(phymode) {
+		case AutoNegotiate : speed = -1; duplex = 0; break;
+		case HalfDuplex10  : speed = 0;  duplex = 0; break;
+		case FullDuplex10  : speed = 0;  duplex = 1; break;
+		case HalfDuplex100 : speed = 1;  duplex = 0; break;
+		case FullDuplex100 : speed = 1;  duplex = 1; break;
+	}
+
+	ethernet_set_link(speed, duplex);
+}
+
 void WIZnet_Chip::scmd(int socket, Command cmd)
 {
     sreg<uint8_t>(socket, Sn_CR, cmd);
@@ -446,5 +471,22 @@
     debug("\n");
 }
 
+int ethernet_link(void) {
+    return (reg_rd<uint8_t>(PHYCFGR) & 0x01);
+}
+
+void ethernet_set_link(int speed, int duplex) {
+	uint32_t val=0;
+	if((speed < 0) || (speed > 1)) {
+		val = (PHYCFGR_OPMDC_ALL)<<3; 
+	} else {
+		val = (((speed&0x01)<<1)+ (duplex&0x01))<<3; 
+	}
+    reg_wr<uint32_t>(PHYCFGR, PHYCFGR_RST&(PHYCFGR_OPMD|val));
+    wait(0.2);
+	reg_wr<uint32_t>(PHYCFGR, (~PHYCFGR_RST)|(PHYCFGR_OPMD|val));
+    wait(0.2);
+}
+
 #endif
 
--- a/arch/ext/W5500.h	Mon Jun 15 23:43:56 2015 +0000
+++ b/arch/ext/W5500.h	Tue Jun 16 11:11:11 2015 +0900
@@ -26,9 +26,6 @@
 
 #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
@@ -54,7 +51,6 @@
 #define SUBR      0x0005
 #define SHAR      0x0009
 #define SIPR      0x000f
-#define INTLEVEL  0x0013
 #define IR        0x0015
 #define IMR       0x0016
 #define SIR       0x0017
@@ -116,8 +112,6 @@
 #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
@@ -150,6 +144,14 @@
 #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 {
@@ -217,13 +219,6 @@
     bool setip();
 
     /*
-    * Get Link Status
-    *
-    * @return true if Link up, false Link down
-    */
-    bool linkstatus();
-
-    /*
     * Disconnect the connection
     *
     * @ returns true 
@@ -257,6 +252,20 @@
     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
@@ -1012,6 +1021,8 @@
     DigitalOut cs;
     DigitalOut reset_pin;
 };
+extern int ethernet_link(void);
+extern void ethernet_set_link(int speed, int duplex);
 
 extern uint32_t str_to_ip(const char* str);
 extern void printfBytes(char* str, uint8_t* buf, int len);
--- a/arch/int/W7500x_toe.cpp	Mon Jun 15 23:43:56 2015 +0000
+++ b/arch/int/W7500x_toe.cpp	Tue Jun 16 11:11:11 2015 +0900
@@ -15,7 +15,7 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 #include "eth_arch.h"
-#ifdef USE_W7500
+#if defined(TARGET_WIZwiki_W7500)
 
 #include "mbed.h"
 #include "mbed_debug.h"
@@ -53,6 +53,14 @@
 	mdio_init(GPIO_MDC, MDC, MDIO);
 }
 
+bool WIZnet_Chip::setmac()
+{
+
+    for (int i =0; i < 6; i++) reg_wr<uint8_t>(SHAR+i, mac[i]);
+
+    return true;
+}
+
 // Set the IP
 bool WIZnet_Chip::setip()
 {
@@ -488,4 +496,3 @@
 }
 #endif
 
-
--- a/arch/int/W7500x_toe.h	Mon Jun 15 23:43:56 2015 +0000
+++ b/arch/int/W7500x_toe.h	Tue Jun 16 11:11:11 2015 +0900
@@ -38,7 +38,7 @@
 #define SUBR                (0x600C)
 #define SHAR                (0x6000)
 #define SIPR                (0x6010)
-//#define PHYSTATUS 0x0035 //not support in W7500
+
 // Added Common register @W7500
 #define TIC100US            (0x2000) 
 
@@ -115,6 +115,13 @@
 	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
--- a/eth_arch.h	Mon Jun 15 23:43:56 2015 +0000
+++ b/eth_arch.h	Tue Jun 16 11:11:11 2015 +0900
@@ -30,9 +30,3 @@
 //#define USE_WIZ550IO_MAC    // WIZ550io; using the MAC address
 
 #endif
-
-
-
-
-
-