christian b / WiflyLib

Files at this revision

API Documentation at this revision

Comitter:
user christian b
Date:
Fri May 11 00:23:48 2012 +0000
Commit message:

Changed in this revision

MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
wifly.cpp Show annotated file Show diff for this revision Revisions of this file
wifly.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MODSERIAL.lib	Fri May 11 00:23:48 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/MODSERIAL/#c11ea36f17f9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri May 11 00:23:48 2012 +0000
@@ -0,0 +1,36 @@
+#include "mbed.h"
+#include "wifly.h"
+
+DigitalOut  myled(LED1);
+wifly       d(p28,p27,p29);
+Serial      pc(USBTX,USBRX);
+
+int main() {
+    if(d.wiflyInit()){
+        pc.printf("Wifly Alive!\n\r");
+    }
+    else{
+        return -1;
+    }
+    if(d.CmdMode()){
+        pc.printf("Succesful Entry in command mode!\n\r");
+    }
+    else{
+        pc.printf("Critical Error Fail!\n\r");
+        return -1;
+    }
+    wait(1.0);
+    if(d.EndCmdMode()){
+        pc.printf("Succesful Exited command mode!\n\r");
+    }
+    else{
+        pc.printf("Critical Error Fail!\n\r");
+        return -1;
+    }
+    while(1) {
+        myled = 1;
+        wait(0.2);
+        myled = 0;
+        wait(0.2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri May 11 00:23:48 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wifly.cpp	Fri May 11 00:23:48 2012 +0000
@@ -0,0 +1,62 @@
+#include "mbed.h"
+#include "wifly.h"
+
+wifly::wifly(   PinName tx, PinName rx, PinName _reset   ): wifi(tx, rx), reset_pin(_reset) {
+    reset_pin = 0;
+    
+    
+    
+}
+
+bool    wifly::wiflyInit(void){
+    wifi.baud(9600);
+    reset_pin = 1;
+    if(!WaitForConfirmation(RDY_PHRASE)) return 0;
+    
+    //wifi.attach(this, &wifly::handler_rx, MODSERIAL::RxIrq);
+    return 1;
+}
+
+
+bool wifly::WaitForConfirmation(const char * tbl){
+    char x = 0;
+    t.start();
+    t.reset();
+    do{
+        if(wifi.readable()){
+            if(wifi.getc() == tbl[x])x++;
+        }
+        if(t.read_ms() > WAIT_TIME_OUT){
+            t.stop();
+            return 0;
+        }
+    }
+    while(tbl[x] != '\0'); //whole command received!
+    t.stop();
+    return 1;
+}
+
+/*
+*/
+bool wifly::CmdMode(void){
+    while(DataRxInProgress);    //we have to wait till packet has come in
+
+    //now we are set to send commands    
+    wifi.printf("$$$");
+    return WaitForConfirmation(ENTER_CMD); 
+}
+/*
+*/
+bool wifly::EndCmdMode(void){
+    bool result;
+    wifi.printf("exit\r");
+    result = WaitForConfirmation(EXIT_CMD); 
+    return result;
+}
+
+
+void wifly::handler_rx(void) {
+    char c = wifi.getc();
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wifly.h	Fri May 11 00:23:48 2012 +0000
@@ -0,0 +1,111 @@
+/**
+* @author Christian Brisson
+*
+* @section LICENSE
+*
+* Copyright (c) 2011 mbed
+*
+* 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.
+*
+* @section DESCRIPTION
+*
+* Wifly RN131-C, wifi module
+*
+* Datasheet:
+*
+* http://www.sparkfun.com/datasheets/Wireless/WiFi/WiFlyGSX-um2.pdf
+*/
+#ifndef WIFLY_H
+#define WIFLY_H
+
+#include    "mbed.h"
+#define     MODSERIAL_DEFAULT_RX_BUFFER_SIZE 256
+#define     MODSERIAL_DEFAULT_TX_BUFFER_SIZE 32 
+#include    "MODSERIAL.h"
+
+
+
+#define     RX_BUFFER_SIZE  256
+#define     END_LINE        0x0A
+#define     SPACE           0x20
+#define     WAIT_TIME_OUT   5000    //5seconds
+#define     DATAMODE        true
+#define     CMDMODE         false
+
+const   char    RDY_PHRASE[] = "*READY*\0"; // + 0d 0a
+const   char    ACK_PHRASE[] = "AOK\0" ; //always +0d 0a
+const   char    ENTER_CMD[] = {'C','M','D',0x0D,0x0A,'\0'};
+const   char    EXIT_CMD[] = {0x0D,'E','X','I','T',0x0D,0x0A,'\0'};
+
+class wifly {
+    
+    public:
+    /*
+    */
+    wifly(  PinName tx, PinName rx, PinName reset);
+    /*
+        return 0  if no response from wifly within 5 seconds
+        This fonctions waits the the *READY* sequence from wifly
+        return 1 if sequence received!
+    */
+    bool    wiflyInit(void);
+    /*
+    */
+    void ResetBuffer(void);
+    /*
+    */
+    bool CmdMode(void);
+    /*
+    */
+    bool EndCmdMode(void);
+    /*
+    */
+    bool WaitForConfirmation(const char * tbl);
+    /*
+    */
+    void Push(char c);
+    /*
+    */
+    char Pop(void);
+    /*
+    */
+    int readable(void);
+    
+    
+    
+    private:
+    void    SwitchToCmdBuffer(void);
+    void    SwitchToDataBuffer(void);
+    void    InitBuffer(void);
+    
+    Timer           t;
+    MODSERIAL          wifi;
+    DigitalOut      reset_pin;
+    bool            Mode;               //true : datamode   false : cmdmode
+    bool            DataRxInProgress;
+    //circular rx buffer stuff
+    char            RxBuffer[RX_BUFFER_SIZE+1];
+    int             RxR,RxW;
+    int             RxCnt;
+    
+    void            handler_rx(void);
+    bool            WasReset;
+    bool            wifly_rdy;
+};
+#endif
\ No newline at end of file