WiFly Fun

How exactly do I use the WiFly shield? I have it connected and its powered up and I’m looking at the code in the example driver but unsure how to proceed.

I’ve seen EnableSerialGateway() however doesn’t that mean I’d have to open it on say COm1 then wire COM1 into COM2 so I can open that up and read from it?

I’m a little lost, I hoped it’d be simply to just read/write commands to it :frowning:

I’m assuming this device is the same one in your topic about thw WiFly shield and on that basis the shield in question appears to have a SPI/I2C to UART on board so you need to commuicate via a SPI connection or a I2C connection. From the schematics you are looking at SPIclock on pin D13, SPIout on pin D11, ChipSelect on pin D10 and SPIin on D12…

Did you download the driver for this sheild? If you did, you don’t need to be worry about low level code such as the interfacing… the driver should have done that for you, and provide a read and write method that you can use…

Cheers Ian

Yes I downloaded the driver but it seems somewhat incomplete, the driver can be found at:

http://www.tinyclr.com/downloads/Shield/FEZ_Shields_WiFly.cs

I’ve just looked at the arduino driver ( its in c++ )

First look at this…[url]http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1278691326[/url] there is a transparent driver tor the SPI/UART you can check varoius differences.

Then the library[url]http://sparkfun.com/Code/wifly/WiFly-20100519-023939.zip[/url]

There is quite a bit left to do

Cheers Ian

Yeh I sort of got something back, I’m currently doing this:


using System;
using System.Collections;
using System.IO.Ports;
using System.Threading;
using System.Text;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.USBClient;
using GHIElectronics.NETMF.Hardware;

namespace TestDomino
{

    public class Program
    {

        public static void Main()
        {
            // Check debug interface
            if (Configuration.DebugInterface.GetCurrent() == Configuration.DebugInterface.Port.USB1)
                throw new InvalidOperationException("Current debug interface is USB. It must be changed to something else before proceeding. Refer to your platform user manual to change the debug interface.");


            // Start CDC
            USBC_CDC cdc = USBClientController.StandardDevices.StartCDC();

            EnableCDCGateway(cdc);
        }

        public static void EnableCDCGateway(USBC_CDC cdc)
        {
            //init SPI<->UART chip
            SPI spi = new SPI(new SPI.Configuration((Cpu.Pin)FEZ_Pin.Digital.Di10, false, 10, 10, false, true, 2000, SPI.SPI_module.SPI1));
            WriteRegister(spi,WiFlyRegister.LCR, 0x80); // 0x80 to program baudrate
            //WriteRegister(spi,WiFlyRegister.DLL, 0x50); //0x50 = 9600 with Xtal = 12.288MHz
            WriteRegister(spi,WiFlyRegister.DLL, 0x60); //0x60 = 9600 with Xtal = 14.7456MHz
            WriteRegister(spi,WiFlyRegister.DLM, 0);

            WriteRegister(spi,WiFlyRegister.LCR, 0xBF); // access EFR register
            WriteRegister(spi,WiFlyRegister.EFR, 0x10); // enable enhanced registers
            WriteRegister(spi,WiFlyRegister.LCR, 3); // 8 data bit, 1 stop bit, no parity
            WriteRegister(spi,WiFlyRegister.FCR, 0x06); // reset TXFIFO, reset RXFIFO, non FIFO mode
            WriteRegister(spi,WiFlyRegister.FCR, 0x01); // enable FIFO mode

            // Perform read/write test to check if UART is working
            WriteRegister(spi,WiFlyRegister.SPR, 0x55);

            byte data = ReadRegister(spi,WiFlyRegister.SPR);

            if (data != 0x55)
                throw new Exception("Failed to init SPI<->UART chip");

                //exit command mode if we haven't
                //SendCommand(spi,"");
                //SendCommand(spi,"exit");
                //Thread.Sleep(500);

                //enter command mode
                SendString(spi,"$$$");
                Thread.Sleep(500);

                            //reboot
                SendCommand(spi,"reboot");
                Thread.Sleep(2000);

                //enter command mode
                //SendString(spi,"$$$");
                //Thread.Sleep(500);

            //SerialPort _SerialPort = new SerialPort(com_port, baud_rate, Parity.None, 8, StopBits.One);
            //_SerialPort.ReadTimeout = 0;
            //_SerialPort.Open();

            // run the gateway between teh UART chip and one of the COM ports
            // you probably want to use COM1 with RS232 shield
            while (true)
            {
                Debug.Print("Bytes available: " + BytesAvailable(spi).ToString());

                byte[] buffer = new byte[1];

                if ((ReadRegister(spi,WiFlyRegister.LSR) & 0x01) > 0)
                {
                    buffer[0] = ReadRegister(spi,WiFlyRegister.RHR);

                    cdc.Write(buffer,0,1);

                    //bytex2[0] = ReadRegister(Register.RHR);
                    //bytex2[1] = (byte) '\r';
                    //_SerialPort.Write(bytex2, 0, 1);
                }

                if (cdc.Read(buffer,0,1) > 0) {
                    WriteRegister(spi,WiFlyRegister.THR,buffer[0]);
                }

                //if (_SerialPort.Read(bytex2, 0, 1) > 0)
                //{
                    //WriteRegister(Register.THR, bytex2[0]);
                //}
                //Thread.Sleep(1);
            }
        }

        public static int BytesAvailable(SPI spi)
        {
            return (int)ReadRegister(spi,WiFlyRegister.RXFIFO);
        }

        private static void WriteRegister(SPI spi, WiFlyRegister reg, byte b)
        {
            byte[] buffer = new byte[2] {(byte)reg, b};

            spi.Write(buffer);
        }
			
        private static byte ReadRegister(SPI spi, WiFlyRegister reg)
        {
            byte[] buffer = new byte[2] {(byte)((byte)reg | 0x80), 0};

            spi.WriteRead(buffer,buffer);
                
            return buffer[1];
        }

        private static void WriteArray(SPI spi, byte[] ba)
        {
            for (int i = 0; i < ba.Length; i++)
                WriteRegister(spi,WiFlyRegister.THR, ba[i]);
        }

        public static void SendString(SPI spi, string str)
        {
            byte[] ba = Encoding.UTF8.GetBytes(str);
            WriteArray(spi,ba);

            Debug.Print(str);
        }

        public static void SendCommand(SPI spi, string command)
        {
            SendString(spi,command);
            WriteRegister(spi,WiFlyRegister.THR, (byte)'\r');
            //byte[] ba = Encoding.UTF8.GetBytes(command + '\r');
            //WriteArray(ba);
        }

    }

}

However the response I get from the CDC is:

MD
reboot
RebootWiFly Ver 2.21, 07-11-2010
Auto-AssoAuto-Assoc roving0Auto-Assoc rovingAuto-Assoc r

And it just repeats that on and on, sometimes it will go into command mode, sometimes it wont and sometimes it just jams and seems to get stuck :confused:

You may have to wait for Joseph… he’s the wiz with networking and WiFi

I am maily into low level interfacing and hardware…

It looks like you’re creating a usb client
I thought that you were using EnableSerialGateway…

I’m not into USB connections atall yet…

Ian

Sort of, i’m using the Virtual COM thing through the main USB connection to a terminal on Windows, sadly it doesn’t have a dedicated COM port or I would just use SerialPort so I had to modify the code a bit to stream out across the CDC.

This is only really for my exploratory benefit though, in reality the device wouldn’t do this, all the commands and output would be processed internally into something more usable I’m just trying to get a handle on how it works and what the output is.

I have another USB connected to COM1 for debugging I guess I could switch the debugger back around and output via COM1 instead.

Where is the register information do you know? The user manual from roving doesn’t describe such raw interactions with the device just the higher level command interface.

I suppose the register information is on the SC16IS750 data sheet

ics.nxp.com/products/sc16/datasheet/sc16is740.sc16is750.sc16is760.pdf

There will also be registers on the WiFly_GSX module aswell…

Cheers Ian

Getting there slowly, changed the code to this:


using System;
using System.Collections;
using System.IO.Ports;
using System.Threading;
using System.Text;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.USBClient;
using GHIElectronics.NETMF.Hardware;

namespace TestDomino
{

    public enum Register
    {

        THR = 0x00 << 3,
        RHR = 0x00 << 3,
        IER = 0x01 << 3,
        FCR = 0x02 << 3,
        IIR = 0x02 << 3,
        LCR = 0x03 << 3,
        MCR = 0x04 << 3,
        LSR = 0x05 << 3,
        MSR = 0x06 << 3,
        SPR = 0x07 << 3,
        TXFIFO = 0x08 << 3,
        RXFIFO = 0x09 << 3,
        DLAB = 0x80 << 3,
        IODIR = 0x0A << 3,
        IOSTATE = 0x0B << 3,
        IOINTMSK = 0x0C << 3,
        IOCTRL = 0x0E << 3,
        EFCR = 0x0F << 3,
        DLL = 0x00 << 3,
        DLM = 0x01 << 3,
        EFR = 0x02 << 3,
        XON1 = 0x04 << 3,
        XON2 = 0x05 << 3,
        XOFF1 = 0x06 << 3,
        XOFF2 = 0x07 << 3,
    }

    public class Program
    {

        public static void Main()
        {
            // Check debug interface
            if (Configuration.DebugInterface.GetCurrent() == Configuration.DebugInterface.Port.USB1)
                throw new InvalidOperationException("Current debug interface is USB. It must be changed to something else before proceeding. Refer to your platform user manual to change the debug interface.");

            // Start CDC
            USBC_CDC cdc = USBClientController.StandardDevices.StartCDC();

            //EnableCDCGateway(cdc);

            //
            PrintLn(cdc,"\n\r\n\rWiFly Shield Terminal Routine");

            SPI spi = new SPI(new SPI.Configuration((Cpu.Pin)FEZ_Pin.Digital.Di10, false, 10, 10, false, true, 2000, SPI.SPI_module.SPI1));

            if (SPI_UART_Init(spi,cdc)) {
                PrintLn(cdc,"Bridge initialized successfully.");
            } else {
                PrintLn(cdc,"Could not intialize bridge, locking up.");

                while (true) Thread.Sleep(100);
            }

                //exit command mode if we haven't
                SendCommand(spi,"");
                SendCommand(spi,"exit");
                Thread.Sleep(500);

                //enter command mode
                SendString(spi,"$$$");
                Thread.Sleep(500);

                //reboot
                SendCommand(spi,"reboot");
                Thread.Sleep(2000);

                //flush all data
                FlushData(spi);

                //enter command mode
                SendString(spi,"$$$");
                Thread.Sleep(500);

                SendCommand(spi,"scan");
                Thread.Sleep(500);
            while (true) {
                byte[] buffer = new byte[1];
                //Debug.Print("Bytes available: " + BytesAvailable(spi).ToString());

                if ((ReadRegister(spi,Register.LSR) & 0x01) > 0) {
                    
                    bool polling = true;

                    while (polling) {
                        if ((ReadRegister(spi,Register.LSR) & 0x01) > 0) {
                            buffer[0] = ReadRegisterEx(spi,Register.RHR);

                            cdc.Write(buffer,0,1);
                        } else {
                            polling = false;
                        }
                    }
                } else {
                    int num_read = cdc.Read(buffer,0,buffer.Length);

                    for(int i = 0; i < buffer.Length; i++) WriteRegister(spi,Register.THR,buffer[i]);
                }
            }
        }

        public static void PrintLn(USBC_CDC cdc, string s)
        {
            Debug.Print(s);

            byte[] buffer = Encoding.UTF8.GetBytes(s);

            cdc.Write(buffer,0,buffer.Length);
        }

        public static int BytesAvailable(SPI spi)
        {
            return (int)ReadRegister(spi,Register.RXFIFO);
        }

        public static bool SPI_UART_Init(SPI spi, USBC_CDC cdc)
        {
            WriteRegister(spi,Register.LCR,0x80);
            WriteRegister(spi,Register.DLL,0x60);

            WriteRegister(spi,Register.LCR, 0x80); // 0x80 to program baudrate
            WriteRegister(spi,Register.DLL, 0x60); //0x50 = 9600 with Xtal = 12.288MHz
            WriteRegister(spi,Register.DLM, 0);

            WriteRegister(spi,Register.LCR, 0xBF); // access EFR register
            WriteRegister(spi,Register.EFR, 0x10); // enable enhanced registers
            WriteRegister(spi,Register.LCR, 3); // 8 data bit, 1 stop bit, no parity
            WriteRegister(spi,Register.FCR, 0x06); // reset TXFIFO, reset RXFIFO, non FIFO mode
            WriteRegister(spi,Register.FCR, 0x01); // enable FIFO mode

            WriteRegister(spi,Register.SPR,0x55);

            byte result = ReadRegister(spi,Register.SPR);

            if (result == 0x55) {
                return true;
            } else {
                return false;
            }
        }

        private static void WriteRegister(SPI spi, Register reg, byte b)
        {
            byte[] buffer = new byte[] {(byte)reg,b};

            spi.Write(buffer);
        }
			
        private static byte ReadRegister(SPI spi, Register reg)
        {
            byte[] buffer = new byte[] {(byte)((byte)reg | 0x80),0xFF};

            spi.WriteRead(buffer, buffer);
                
            return buffer[1];
        }

        private static byte ReadRegisterEx(SPI spi, Register reg)
        {
            byte[] buffer = new byte[] {(byte)((byte)reg | 0x80),0xFF};

            spi.WriteRead(buffer, buffer);
                
            return buffer[1];
        }

        private static void WriteArray(SPI spi, byte[] ba)
        {
            for (int i = 0; i < ba.Length; i++)
                WriteRegister(spi,Register.THR, ba[i]);
        }

        public static void SendString(SPI spi, string str)
        {
            byte[] ba = Encoding.UTF8.GetBytes(str);
            WriteArray(spi,ba);
            Debug.Print(str);
        }

        public static void SendCommand(SPI spi, string command)
        {
            SendString(spi,command);
            WriteRegister(spi,Register.THR, (byte)'\r');
            //byte[] ba = Encoding.UTF8.GetBytes(command + '\r');
            //WriteArray(ba);
        }

        static char[] flush_bytes = new char[100];

        static void FlushData(SPI spi)
        {
            int index = 0;

            Thread.Sleep(500);

            Array.Clear(flush_bytes, 0, flush_bytes.Length);

            //flush all data
            while ((ReadRegister(spi,Register.LSR) & 0x01) > 0)
            {
                byte b;
                b = ReadRegister(spi,Register.RHR);
                flush_bytes[index++] = (char)b;
                if (index >= flush_bytes.Length)
                {
                    index = 0;
                    Debug.Print(new string(flush_bytes));
                }
                    
                //Debug.Print(((char)b).ToString());
            }
            if(index>0)
                Debug.Print(new string(flush_bytes));

        }

    }

}

Based on the links provided and it works a bit better, however the buffer does seem to be scewed still:

CMD
scan
<2.21>
SCAN:Found 4
Num SSID Ch RSSI Sec MAC AddrAuto-Assoc roving1 chan=0 mode=NONE FAILED
CMD
scan
<2.21>
SCAN:Found 3
Num SSID Ch RSSI Sec MAC Addr
<2.21> reboot
RebootWiFly Ver 2.21, 07-11-2010
MAC Addr=00:12:b8:13:3c:Auto-Assoc roving1 chan=0 mode=NONE FAILED
READY
CMD
scan
<2.21>
SCAN:Found 2
Num SSID Ch RSSI Sec MAC Addr

It should show networks but instead just stops after Addr for some reason :confused:

Seems if I switch to true COM instead of CDC it more readily works…

Woop I have it working with CDC! Seems ReadTimeout is important! Also added the polling system from the Arduino code:

Here’s the final CDC function for the shield:


            public static void EnableCDCGateway(USBC_CDC cdc)
            {
                //init SPI<->UART chip
                _spi = new SPI(new SPI.Configuration((Cpu.Pin)FEZ_Pin.Digital.Di10, false, 10, 10, false, true, 2000, SPI.SPI_module.SPI1));
                WriteRegister(Register.LCR, 0x80); // 0x80 to program baudrate
                WriteRegister(Register.DLL, 0x60); //0x50 = 9600 with Xtal = 12.288MHz
                WriteRegister(Register.DLM, 0);

                WriteRegister(Register.LCR, 0xBF); // access EFR register
                WriteRegister(Register.EFR, 0x10); // enable enhanced registers
                WriteRegister(Register.LCR, 3); // 8 data bit, 1 stop bit, no parity
                WriteRegister(Register.FCR, 0x06); // reset TXFIFO, reset RXFIFO, non FIFO mode
                WriteRegister(Register.FCR, 0x01); // enable FIFO mode

                // Perform read/write test to check if UART is working
                WriteRegister(Register.SPR, 0x55);
                byte data = ReadRegister(Register.SPR);

                if (data != 0x55)
                    throw new Exception("Failed to init SPI<->UART chip");

                cdc.ReadTimeout = 0;

                // run the gateway between teh UART chip and one of the COM ports
                // you probably want to use COM1 with RS232 shield
                while (true)
                {
                    if ((ReadRegister(Register.LSR) & 0x01) > 0)
                    {
                        bool polling = true;

                        while (polling) {
                            if ((ReadRegister(Register.LSR) & 0x01) > 0) {
                                bytex2[0] = ReadRegister(Register.RHR);
                                //bytex2[1] = (byte) '\r';
                                //_SerialPort.Write(bytex2, 0, 1);
                                cdc.Write(bytex2,0,1);
                            } else {
                                polling = false;
                            }
                        }
                    }

                    if (cdc.Read(bytex2,0,1) > 0) {
                        WriteRegister(Register.THR,bytex2[0]);
                    }
                    //Thread.Sleep(1);
                }
            }

I think, untill we get an expert on the wifi side trial and error may previel…

A least your learning If i had worked straight off you wouldn’t have dabbled so deep…

Cheers Ian

Here’s a new odd issue, given this code:


            public static void EnableCDCGateway(USBC_CDC cdc)
            {
                //init SPI<->UART chip
                _spi = new SPI(new SPI.Configuration((Cpu.Pin)FEZ_Pin.Digital.Di10, false, 10, 10, false, true, 2000, SPI.SPI_module.SPI1));
                WriteRegister(Register.LCR, 0x80); // 0x80 to program baudrate
                WriteRegister(Register.DLL, 0x60); //0x50 = 9600 with Xtal = 12.288MHz
                WriteRegister(Register.DLM, 0);

                WriteRegister(Register.LCR, 0xBF); // access EFR register
                WriteRegister(Register.EFR, 0x10); // enable enhanced registers
                WriteRegister(Register.LCR, 3); // 8 data bit, 1 stop bit, no parity
                WriteRegister(Register.FCR, 0x06); // reset TXFIFO, reset RXFIFO, non FIFO mode
                WriteRegister(Register.FCR, 0x01); // enable FIFO mode

                // Perform read/write test to check if UART is working
                WriteRegister(Register.SPR, 0x55);
                byte data = ReadRegister(Register.SPR);

                if (data != 0x55)
                    throw new Exception("Failed to init SPI<->UART chip");

                cdc.ReadTimeout = 0;

                // run the gateway between teh UART chip and one of the COM ports
                // you probably want to use COM1 with RS232 shield
                Queue lines = new Queue();
                string line = String.Empty;

                while (true)
                {
                    if ((ReadRegister(Register.LSR) & 0x01) > 0)
                    {
                        bool polling = true;

                        while (polling) {
                            if ((ReadRegister(Register.LSR) & 0x01) > 0) {
                                bytex2[0] = ReadRegister(Register.RHR);

/*
                                if (bytex2[0] == 13) {
                                    lines.Enqueue(line);
                                    line = String.Empty;
                                } else {
                                    line += (char)bytex2[0];
                                }
*/

                                cdc.Write(bytex2,0,1);
                                

                                 
                            } else {
                                polling = false;
                            }
                        }
                    }

                    if (cdc.Read(bytex2,0,1) > 0) {
                        WriteRegister(Register.THR,bytex2[0]);
                    }
                }
            }

Now this works fine with the if statement commented out, however if I add it back in then the data returned starts to go wrong, and I’m curious why because I’m not altering it in any way, I’m just copying it into a string and proceeding as usual?