GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/

Dependents:   GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more

Fork of WiflyInterface by mbed official

GainSpan Wi-Fi library

The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.

mbed RTOS supported.

/media/uploads/gsfan/gs_im_002.jpg /media/uploads/gsfan/gs1011m_2.jpg

ゲインスパン Wi-Fi モジュール ライブラリ

ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011/GS2100 シリーズ用のライブラリです。

mbed RTOS に対応しています。(mbed2.0)

Files at this revision

API Documentation at this revision

Comitter:
gsfan
Date:
Tue Sep 24 06:24:37 2019 +0000
Parent:
21:6431364fc667
Commit message:
UART Command and SPI Data supported.; bug fix.;

Changed in this revision

GSwifi/GSwifi.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi.h Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_at.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_conf.h Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_hal.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifi/GSwifi_msg.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifiInterface.cpp Show annotated file Show diff for this revision Revisions of this file
GSwifiInterface.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 6431364fc667 -r d25a5a0d2497 GSwifi/GSwifi.cpp
--- a/GSwifi/GSwifi.cpp	Thu Jun 05 06:12:59 2014 +0000
+++ b/GSwifi/GSwifi.cpp	Tue Sep 24 06:24:37 2019 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2013 gsfan, MIT License
+/* Copyright (C) 2019 gsfan, 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,
@@ -23,13 +23,21 @@
 
 GSwifi * GSwifi::_inst;
 
+#ifdef CFG_SPI_DATAINTERFACE
+GSwifi::GSwifi(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset,
+  PinName mosi, PinName miso, PinName sclk, PinName cs, PinName wake,
+  PinName alarm, int baud, int freq):
+    _gs(tx, rx), _spi(mosi, miso, sclk), _wake(wake), _reset(reset)
+#else
 GSwifi::GSwifi(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud):
     _gs(tx, rx), _reset(reset)
+#endif
 {
     _inst = this;
     memset(&_state, 0, sizeof(_state));
     memset(&_con, 0, sizeof(_con));
     _state.initialized = false;
+    _state.datainterface = false;
     _state.status = STAT_READY;
     _state.cid = -1;
 #ifdef CFG_EXTENDED_MEMORY1
@@ -57,6 +65,9 @@
 
     setReset(true);
     initUart(cts, rts, alarm, baud);
+#ifdef CFG_SPI_DATAINTERFACE
+    initSpi (cs, freq);
+#endif
     setAlarm(true);
     wait_ms(10);
     setAlarm(false);
@@ -75,7 +86,11 @@
     if (!strlen(_state.name)) {
         strncpy(_state.name, CFG_DHCPNAME, sizeof(_state.name));
     }
+#ifdef CFG_SPI_DATAINTERFACE
+//    if (strstr(
+#endif
     clearFlags();
+    _state.buf->flush();
     _state.mode = MODE_COMMAND;
     sendCommand(NULL, RES_NULL, 0);
     if (cmdE(false)) return -1;
@@ -193,6 +208,7 @@
     _state.wm = WM_ADHOCK;
     _state.initialized = true;
     clearFlags();
+    _state.buf->flush();
     _state.mode = MODE_COMMAND;
     sendCommand(NULL, RES_NULL, 0);
     if (cmdE(false)) return -1;
@@ -243,6 +259,7 @@
         strncpy(_state.name, CFG_DNSNAME, sizeof(_state.name));
     }
     clearFlags();
+    _state.buf->flush();
     _state.mode = MODE_COMMAND;
     sendCommand(NULL, RES_NULL, 0);
     if (cmdE(false)) return -1;
diff -r 6431364fc667 -r d25a5a0d2497 GSwifi/GSwifi.h
--- a/GSwifi/GSwifi.h	Thu Jun 05 06:12:59 2014 +0000
+++ b/GSwifi/GSwifi.h	Tue Sep 24 06:24:37 2019 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2013 gsfan, MIT License
+/* Copyright (C) 2019 gsfan, 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,
@@ -17,13 +17,14 @@
  *
  * @section DESCRIPTION
  *
- * GainSpan GS1011, Wi-Fi module
+ * GainSpan (Telit) GS1011, GS2000, GS2100 Wi-Fi module
  *
+ *   https://www.telit.com/m2m-iot-products/wifi-bluetooth-modules/
  *   http://www.gainspan.com/modules
  */
 /** @file
- * @brief Gainspan wi-fi module library for mbed
- * GS1011MIC, GS1011MIP, GainSpan WiFi Breakout, etc.
+ * @brief GainSpan (Telit) wi-fi module library for mbed
+ * GS1011MIC, GS1011MIP, GS2100MIP, GainSpan WiFi Breakout, etc.
  */
 
 #ifndef GSwifi_H
@@ -147,7 +148,13 @@
      * \param alarm alarm pin of the wifi module (default NULL)
      * \param baud baud rate of Serial interface (default 9600)
      */
+#ifdef CFG_SPI_DATAINTERFACE
+    GSwifi (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset,
+      PinName mosi, PinName miso, PinName sclk, PinName cs, PinName wake,
+      PinName alarm = NC, int baud = 9600, int freq = 2000000);
+#else
     GSwifi (PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 9600);
+#endif
 
     /** Connect the wifi module to the ssid contained in the constructor.
      * @param sec Security type (NONE, WEP_128 or WPA)
@@ -437,6 +444,11 @@
 
 //    Serial _gs;
     RawSerial _gs;
+#ifdef CFG_SPI_DATAINTERFACE
+    SPI _spi;
+    InterruptIn _wake;
+    Mutex _mutexSpi;
+#endif
     int _baud;
     DigitalIn *_cts;
     DigitalOut *_rts;
@@ -469,7 +481,7 @@
         bool dhcp;
         time_t time;
 
-        bool initialized;
+        bool initialized, datainterface;
         bool associated;
         volatile Mode mode;
         volatile Status status;
@@ -608,6 +620,7 @@
     void msgReset (const char*);
     void msgOutofStandby (const char*);
     void msgOutofDeepsleep (const char*);
+    void msgDataInterfaceReady (const char*);
     void resNormal (const char*);
     void resConnect (const char*);
     void resWpapsk (const char *buf);
@@ -680,6 +693,13 @@
     int lockUart (int ms);
     void unlockUart ();
     void initUart (PinName cts, PinName rts, PinName alarm, int baud);
+#ifdef CFG_SPI_DATAINTERFACE
+    void isrSpi ();
+    void putSpi (char c);
+    int lockSpi (int ms);
+    void unlockSpi ();
+    void initSpi (PinName cs, int freq);
+#endif
 
 };
 
diff -r 6431364fc667 -r d25a5a0d2497 GSwifi/GSwifi_at.cpp
--- a/GSwifi/GSwifi_at.cpp	Thu Jun 05 06:12:59 2014 +0000
+++ b/GSwifi/GSwifi_at.cpp	Tue Sep 24 06:24:37 2019 +0000
@@ -45,8 +45,13 @@
         t.start();
         for (;;) {
             if (_state.ok && _state.res == RES_NULL) break;
-            if (_state.failure || t.read_ms() > timeout) {
-                WARN("failure or timeout\r\n");
+            if (_state.failure) {
+                WARN("failure\r\n");
+                _state.res = RES_NULL;
+                return -1;
+            }
+            if (t.read_ms() > timeout) {
+                WARN("timeout\r\n");
                 _state.res = RES_NULL;
                 return -1;
             }
@@ -63,6 +68,34 @@
     int i;
     Timer t;
 
+#ifdef CFG_SPI_DATAINTERFACE
+    if (_state.datainterface) {
+        if (lockSpi(timeout)) return -1;
+    
+        clearFlags();
+        for (i = 0; i < strlen(cmd); i ++) {
+            putSpi(cmd[i]);
+        }
+        for (i = 0; i < len; i ++) {
+            switch (data[i]) {
+            case 0: // encode
+            case 0xf3:
+            case 0xf5:
+            case 0xfa:
+            case 0xfb:
+            case 0xfd:
+            case 0xff:
+                putSpi(0xfb);
+                putSpi(data[i] ^ 0x20); // xor
+                break;
+            default:
+                putSpi(data[i]);
+                break;
+            }
+        }
+        unlockSpi();
+    } else {
+#endif
     if (lockUart(timeout)) return -1;
 
     clearFlags();
@@ -73,14 +106,21 @@
         putUart(data[i]);
     }
     unlockUart();
-    INFO("data: '%s' %d\r\n", cmd, len);
+#ifdef CFG_SPI_DATAINTERFACE
+    } // _state.datainterface
+#endif
+    INFO("data: '%s' %d %d\r\n", cmd, len, _state.datainterface);
 
     if (timeout) {
         t.start();
         for (;;) {
             if (_state.ok) break;
-            if (_state.failure || t.read_ms() > timeout) {
-                WARN("failure or timeout\r\n");
+            if (_state.failure) {
+                WARN("failure\r\n");
+                return -1;
+            }
+            if (t.read_ms() > timeout) {
+                WARN("timeout\r\n");
                 return -1;
             }
         }
diff -r 6431364fc667 -r d25a5a0d2497 GSwifi/GSwifi_conf.h
--- a/GSwifi/GSwifi_conf.h	Thu Jun 05 06:12:59 2014 +0000
+++ b/GSwifi/GSwifi_conf.h	Tue Sep 24 06:24:37 2019 +0000
@@ -7,6 +7,7 @@
 //#define CFG_ENABLE_WEBSOCKET
 //#define CFG_ENABLE_SMTP
 //#define CFG_UART_DIRECT
+//#define CFG_SPI_DATAINTERFACE // GS2100 s2w (Serial to Wi-Fi, UART Command and SPI Data)
 
 #define CFG_UART_BAUD 9600
 #define CFG_WREGDOMAIN 2 // 0:FCC, 1:ETSI, 2:TELEC
@@ -14,7 +15,7 @@
 #define CFG_DNSNAME "setup.local"
 #define CFG_TRYJOIN 3
 #define CFG_RECONNECT 30 // sec
-#define CFG_MAX_SOCKETS 16
+#define CFG_MAX_SOCKETS 8 // max 16
 
 #define DEFAULT_WAIT_RESP_TIMEOUT 500
 #define CFG_TIMEOUT 30000 // ms
@@ -22,13 +23,13 @@
 #define CFG_CMD_SIZE 128
 
 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC176X)
-#define CFG_DATA_SIZE 1024
+#define CFG_DATA_SIZE 1500
 #elif defined(TARGET_LPC11U24) || defined(TARGET_LPC1114) || defined(TARGET_LPC11UXX) || defined(TARGET_LPC11XX)
 #define CFG_DATA_SIZE 256
 #elif defined(TARGET_LPC812) || defined(TARGET_LPC81X)
 #define CFG_DATA_SIZE 128
 #elif defined(TARGET_LPC4088) || defined(TARGET_LPC408X)
-#define CFG_DATA_SIZE 4096
+#define CFG_DATA_SIZE 1500
 #elif defined(TARGET_KL25Z)
 #define CFG_DATA_SIZE 512
 #endif
diff -r 6431364fc667 -r d25a5a0d2497 GSwifi/GSwifi_hal.cpp
--- a/GSwifi/GSwifi_hal.cpp	Thu Jun 05 06:12:59 2014 +0000
+++ b/GSwifi/GSwifi_hal.cpp	Tue Sep 24 06:24:37 2019 +0000
@@ -223,3 +223,75 @@
         _alarm = NULL;
     }
 }
+
+#ifdef CFG_SPI_DATAINTERFACE
+void GSwifi::putSpi (char c) {
+    _spi.write(c);
+}
+
+void GSwifi::isrSpi () {
+    char c;
+
+    while (_wake == 1) {
+        c = _spi.write(0xf5);
+        recvData(c);
+    }
+}
+
+int GSwifi::lockSpi (int ms) {
+    Timer t;
+
+    if (_state.mode != MODE_COMMAND) {
+        t.start();
+        while (_state.mode != MODE_COMMAND) {
+            if (t.read_ms() >= ms) {
+                WARN("lock timeout (%d)\r\n", _state.mode);
+                return -1;
+            }
+        }
+    }
+
+#ifdef CFG_ENABLE_RTOS
+    if (_mutexSpi.lock(ms) != osOK) return -1;
+#endif
+
+    if (_wake.read()) {
+        t.start();
+        while (_wake.read()) {
+            if (t.read_ms() >= ms) {
+                DBG("hostwake timeout\r\n");
+                return -1;
+            }
+        }
+    }
+    return 0;
+}
+
+void GSwifi::unlockSpi () {
+#ifdef CFG_ENABLE_RTOS
+    _mutexSpi.unlock();
+#endif
+}
+
+void GSwifi::initSpi (PinName cs, int freq) {
+    _spi.format(8, 0);
+    _spi.frequency(freq);
+    _wake.mode(PullDown);
+    _wake.rise(this, &GSwifi::isrSpi);
+
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC176X)
+    if (cs == p8) { // SPI_1 (P0_6)
+        LPC_PINCON->PINSEL0 &= ~(3 << 12);
+        LPC_PINCON->PINSEL0 |= (2 << 12); // SSEL1
+    } else
+    if (cs == p14) { // (SPI_0) P0_16
+        LPC_PINCON->PINSEL1 &= ~(3 << 0);
+        LPC_PINCON->PINSEL1 |= (2 << 0); // SSEL0
+    } else
+    if (cs == P1_21) { // (SPI_0) P1_21
+        LPC_PINCON->PINSEL3 &= ~(3 << 10);
+        LPC_PINCON->PINSEL3 |= (3 << 10); // SSEL0
+    }
+#endif
+}
+#endif
diff -r 6431364fc667 -r d25a5a0d2497 GSwifi/GSwifi_msg.cpp
--- a/GSwifi/GSwifi_msg.cpp	Thu Jun 05 06:12:59 2014 +0000
+++ b/GSwifi/GSwifi_msg.cpp	Tue Sep 24 06:24:37 2019 +0000
@@ -43,11 +43,16 @@
         case 0x1b: // ESC
             _state.buf->flush();
             _state.mode = MODE_ESCAPE;
+            _state.escape = false;
+            break;
+        case 0xf3: // SPI link ready indication
+        case 0xf5: // IDLE
             break;
         default:
             _state.buf->flush();
             _state.buf->queue(c);
             _state.mode = MODE_CMDRESP;
+            _state.escape = false;
             break;
         }
         break;
@@ -224,14 +229,30 @@
             } else
 #endif
             if (_con[cid].buf != NULL) {
+#ifdef CFG_SPI_DATAINTERFACE
+                if (_state.datainterface) {
+                    if (_state.escape) { // dencode
+                        _con[cid].buf->queue(c ^ 0x20); // xor
+                        _state.escape = false;
+                    } else
+                    if (c == 0xfb) {
+                        _state.escape = true;
+                    } else {
+                        _con[cid].buf->queue(c);
+                    }
+                } else {
+                    _con[cid].buf->queue(c);
+                }
+#else
                 _con[cid].buf->queue(c);
+#endif
                 if (_con[cid].func != NULL && _con[cid].buf->available() > CFG_DATA_SIZE - 16) {
                     setRts(false);
                     _con[cid].received = true;
                     WARN("buf full");
                 }
             }
-            count ++;
+            if (!_state.escape) count ++;
             if (count >= len) {
                 DBG("recv bulk %d %d/%d\r\n", cid, count, len);
                 _con[cid].received = true;
@@ -243,7 +264,7 @@
     }
 }
 
-#define MSG_TABLE_NUM 15
+#define MSG_TABLE_NUM 16
 #define RES_TABLE_NUM 11
 int GSwifi::parseMessage () {
     int i;
@@ -267,6 +288,7 @@
       {"Out of StandBy-Timer",    &GSwifi::msgOutofStandby},
       {"Out of StandBy-Alarm",    &GSwifi::msgOutofStandby},
       {"Out of Deep Sleep",       &GSwifi::msgOutofDeepsleep},
+      {"DataInterfaceReady",      &GSwifi::msgDataInterfaceReady},
     };
     static const struct RES_TABLE {
         const Response res;
@@ -422,6 +444,12 @@
     _state.status = STAT_READY;
 }
 
+void GSwifi::msgDataInterfaceReady (const char *buf) {
+    DBG("DataInterfaceReady\r\n");
+    _state.datainterface = true;
+    _state.status = STAT_READY;
+}
+
 void GSwifi::resConnect (const char *buf) {
     int cid;
 
@@ -474,14 +502,14 @@
     if (_state.n == 0 && strstr(buf, "SubNet") && strstr(buf, "Gateway")) {
         _state.n ++;
     } else
-    if (_state.n == 1) {
+    if (_state.n == 1 && buf[0] == ' ') {
         tmp = buf + 1;
         tmp2 = strstr(tmp, ":");
         strncpy(_state.ip, tmp, tmp2 - tmp);
-        tmp = tmp2 + 2;
+        tmp = tmp2 + 1;
         tmp2 = strstr(tmp, ":");
         strncpy(_state.netmask, tmp, tmp2 - tmp);
-        tmp = tmp2 + 2;
+        tmp = tmp2 + 1;
         strncpy(_state.gateway, tmp, sizeof(_state.gateway));
         _state.n ++;
         _state.res = RES_NULL;
diff -r 6431364fc667 -r d25a5a0d2497 GSwifiInterface.cpp
--- a/GSwifiInterface.cpp	Thu Jun 05 06:12:59 2014 +0000
+++ b/GSwifiInterface.cpp	Tue Sep 24 06:24:37 2019 +0000
@@ -1,11 +1,18 @@
-/* Copyright (C) 2013 gsfan, MIT License
- *  port to the GainSpan Wi-FI module GS1011
+/* Copyright (C) 2019 gsfan, MIT License
+ *  port to the GainSpan (Telit) Wi-FI module GS1011/GS2000/GS2100
  */
 
 #include "GSwifiInterface.h"
 
+#ifdef CFG_SPI_DATAINTERFACE
+GSwifiInterface::GSwifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset,
+  PinName mosi, PinName miso, PinName sclk, PinName cs, PinName wake,
+  PinName alarm, int baud, int freq) :
+    GSwifi(tx, rx, cts, rts, reset, mosi, miso, sclk, cs, wake, alarm, baud, freq)
+#else
 GSwifiInterface::GSwifiInterface( PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud) :
     GSwifi(tx, rx, cts, rts, reset, alarm, baud)
+#endif
 {
 }
 
diff -r 6431364fc667 -r d25a5a0d2497 GSwifiInterface.h
--- a/GSwifiInterface.h	Thu Jun 05 06:12:59 2014 +0000
+++ b/GSwifiInterface.h	Tue Sep 24 06:24:37 2019 +0000
@@ -17,8 +17,8 @@
  * 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.
  */
-/* Copyright (C) 2013 gsfan, MIT License
- *  port to the GainSpan Wi-FI module GS1011
+/* Copyright (C) 2019 gsfan, MIT License
+ *  port to the GainSpan (Telit) Wi-FI module GS1011/GS2000/GS2100
  */
  
 #ifndef GSWIFIINTERFACE_H_
@@ -43,7 +43,13 @@
     * \param alarm alarm pin of the wifi module (default: NC)
     * \param baud baud rate of Serial interface (default: 9600)
     */
+#ifdef CFG_SPI_DATAINTERFACE
+  GSwifiInterface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset,
+    PinName mosi, PinName miso, PinName sclk, PinName cs, PinName wake,
+    PinName alarm = NC, int baud = 9600, int freq = 2000000);
+#else
   GSwifiInterface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm = NC, int baud = 9600);
+#endif
 
   /** Initialize the interface with DHCP.
   * Initialize the interface and configure it to use DHCP (no connection at this point).
diff -r 6431364fc667 -r d25a5a0d2497 Socket/TCPSocketConnection.cpp
--- a/Socket/TCPSocketConnection.cpp	Thu Jun 05 06:12:59 2014 +0000
+++ b/Socket/TCPSocketConnection.cpp	Tue Sep 24 06:24:37 2019 +0000
@@ -86,6 +86,7 @@
                 DBG("receive readable");
                 break;
             }
+            Thread::wait(1);
             time = tmr.read_ms();
         }
         if (time >= _timeout + 20) {
@@ -119,6 +120,7 @@
         if (idx == length)
             break;
 
+        Thread::wait(1);
         time = tmr.read_ms();
     }
 
diff -r 6431364fc667 -r d25a5a0d2497 Socket/UDPSocket.cpp
--- a/Socket/UDPSocket.cpp	Thu Jun 05 06:12:59 2014 +0000
+++ b/Socket/UDPSocket.cpp	Tue Sep 24 06:24:37 2019 +0000
@@ -40,6 +40,13 @@
 {
     _port = port;
     _server = true;
+
+    if (_cid < 0 && _wifi->isAssociated()) {
+        // Socket open
+        _cid = _wifi->listen(GSwifi::PROTO_UDP, _port);
+        if (_cid < 0) return -1;
+    }
+
     return 0;
 }
 
@@ -103,10 +110,11 @@
             if (_wifi->readable(_cid)) {
                 break;
             }
+            Thread::wait(1);
             time = tmr.read_ms();
         }
         if (time >= _timeout + 20) {
-            return -1;
+            return 0;
         }
     }
 
@@ -126,7 +134,7 @@
             return -1;
         }
 
-        if (idx == length) {
+        if (idx == length || idx > 0) {
             break;
         }
         
@@ -136,5 +144,6 @@
     if (_server) {
         remote.set_address(ip, port);
     }
-    return (idx == 0) ? -1 : idx;
+//    return (idx == 0) ? -1 : idx;
+    return idx;
 }