GainSpan Wi-Fi library see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Dependents:   GSwifi_httpd GSwifi_websocket GSwifi_tcpclient GSwifi_tcpserver ... more

Fork of GSwifi by gs fan

GainSpan Wi-Fi library

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

see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

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

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

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

解説: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Revision:
36:a70b11e1560f
Parent:
35:515ec79792d3
Child:
37:e61ea8267415
--- a/GSwifi.cpp	Fri Jun 21 06:08:37 2013 +0000
+++ b/GSwifi.cpp	Mon Jun 24 07:36:48 2013 +0000
@@ -40,6 +40,11 @@
     if (_baud) _gs.baud(_baud);
     _gs.attach(this, &GSwifi::isr_recv, Serial::RxIrq);
     _rts = false;
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    _uart = LPC_UART1;
+#elif defined(TARGET_LPC11U24)
+    _uart = LPC_USART;
+#endif
 
     wait_ms(100);
     reset();
@@ -62,29 +67,33 @@
     _gs.attach(this, &GSwifi::isr_recv, Serial::RxIrq);
     _rts = false;
 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+    _uart = LPC_UART1;
     if (p_cts == p12) { // CTS input (P0_17)
-        LPC_UART1->MCR |= (1<<7); // CTSEN
+        _uart->MCR |= (1<<7); // CTSEN
         LPC_PINCON->PINSEL1 &= ~(3 << 2);
         LPC_PINCON->PINSEL1 |= (1 << 2); // UART CTS
     }
     if (p_rts == P0_22) { // RTS output (P0_22)
-        LPC_UART1->MCR |= (1<<6); // RTSEN
+        _uart->MCR |= (1<<6); // RTSEN
         LPC_PINCON->PINSEL1 &= ~(3 << 12);
         LPC_PINCON->PINSEL1 |= (1 << 12); // UART RTS
         _rts = true;
     }
 #elif defined(TARGET_LPC11U24)
+    _uart = LPC_USART;
     if (p_cts == p21) { // CTS input (P0_7)
-        LPC_USART->MCR |= (1<<7); // CTSEN
+        _uart->MCR |= (1<<7); // CTSEN
         LPC_IOCON->PIO0_7 &= ~0x07;
         LPC_IOCON->PIO0_7 |= 0x01; // UART CTS
     }
     if (p_rts == p22) { // RTS output (P0_17)
-        LPC_USART->MCR |= (1<<6); // RTSEN
+        _uart->MCR |= (1<<6); // RTSEN
         LPC_IOCON->PIO0_17 &= ~0x07;
         LPC_IOCON->PIO0_17 |= 0x01; // UART RTS
         _rts = true;
     }
+#elif defined(TARGET_KL25Z)
+    _uart = NULL;
 #endif
 
     wait_ms(100);
@@ -127,36 +136,42 @@
 int GSwifi::autobaud (int flg) {
     int i;
 
-#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
+#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC11U24)
     if (flg == 0) {
-        LPC_UART1->ACR = (1<<2)|(1<<0); // auto-baud mode 0
+        _uart->ACR = (1<<2)|(1<<0); // auto-baud mode 0
         return 0;
     } else {
         for (i = 0; i < 50; i ++) {
-            if (! (LPC_UART1->ACR & (1<<0))) {
+            if (! (_uart->ACR & (1<<0))) {
                 return 0;
             }
             wait_ms(10);
         }
-        LPC_UART1->ACR = 0;
-    }
-#elif defined(TARGET_LPC11U24)
-    if (flg == 0) {
-        LPC_USART->ACR = (1<<2)|(1<<0); // auto-baud mode 0
-        return 0;
-    } else {
-        for (i = 0; i < 50; i ++) {
-            if (! (LPC_USART->ACR & (1<<0))) {
-                return 0;
-            }
-            wait_ms(10);
-        }
-        LPC_USART->ACR = 0;
+        _uart->ACR = 0;
     }
 #endif
     return -1;
 }
 
+int GSwifi::waitCts (int ms) {
+    Timer timeout;
+
+    if (! _rts) return 0;
+    if (LPC_UART1->MSR & (1<<4)) return 0;
+
+    timeout.start();
+    while (timeout.read_ms() < ms) {
+        // CTS check
+        if (LPC_UART1->MSR & (1<<4)) {
+            timeout.stop();
+            return 0;
+        }
+    }
+    timeout.stop();
+    DBG("cts timeout\r\n");
+    return -1;
+}
+
 // uart interrupt
 void GSwifi::isr_recv () {
     static int len, mode;
@@ -382,9 +397,12 @@
 int GSwifi::command (const char *cmd, GSRESPONCE res, int timeout) {
     int i;
 
+    if (waitCts()) return -1;
+
     if (cmd == NULL) {
         // dummy CR+LF
-        _gs.printf("\r\n");
+        _gs_putc('\r');
+        _gs_putc('\n');
         wait_ms(100);
         _buf_cmd.flush();
         return 0;
@@ -1154,12 +1172,15 @@
 }
 
 int GSwifi::setBaud (int baud) {
+    char cmd[GS_CMD_SIZE];
 
-    if (_status != GSSTAT_READY) return -1;
+    if (_status != GSSTAT_READY || waitCts()) return -1;
 
-    _gs.printf("ATB=%d\r\n", baud);
-    _gs.baud(baud);
+    _baud = baud;
+    sprintf(cmd, "ATB=%d\r\n", _baud);
+    _gs_puts(cmd);
     wait_ms(100);
+    _gs.baud(_baud);
     _buf_cmd.flush();
     return 0;
 }