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 