From 4f7e2f120123d71bd664be5ec0a2c1b4f49dc3cb Mon Sep 17 00:00:00 2001 From: mlafauci Date: Wed, 18 May 2011 08:39:47 +0200 Subject: [PATCH] Bugfix and fix compilation erro with new-extensions branch --- WiFi/Client.cpp | 10 ++-- WiFi/Server.cpp | 3 -- WiFi/WiFi.cpp | 6 +++ WiFi/WiFi.h | 3 ++ .../wifi_Server_example_WPA.pde | 21 ++++++-- WiFi/examples/wifi_example/wifi_example.pde | 46 ++++++++++++++++-- WiFi/utility/debug.h | 2 - WiFi/utility/spi_drv.cpp | 48 +++++++++++++++++-- WiFi/utility/spi_drv.h | 11 ++--- WiFi/utility/wifi_drv.cpp | 21 ++++++++ WiFi/utility/wifi_drv.h | 1 + WiFi/utility/wifi_spi.h | 1 + 12 files changed, 144 insertions(+), 29 deletions(-) diff --git a/WiFi/Client.cpp b/WiFi/Client.cpp index f33cea114..82c2b3e7a 100755 --- a/WiFi/Client.cpp +++ b/WiFi/Client.cpp @@ -3,6 +3,7 @@ extern "C" { #include "utility/wl_types.h" #include "socket.h" #include "string.h" + #include "utility/debug.h" } #include "WProgram.h" @@ -35,25 +36,28 @@ uint8_t Client::connect() { void Client::write(uint8_t b) { if (_sock != 255) { - while (!ServerDrv::isDataSent(_sock)); + START(); ServerDrv::sendData(_sock, &b, 1); + while (!ServerDrv::isDataSent(_sock)); + END(); + } } void Client::write(const char *str) { if (_sock != 255) { - while (!ServerDrv::isDataSent(_sock)); unsigned int len = strlen(str); ServerDrv::sendData(_sock, (const uint8_t *)str, len); + while (!ServerDrv::isDataSent(_sock)); } } void Client::write(const uint8_t *buf, size_t size) { if (_sock != 255) { - while (!ServerDrv::isDataSent(_sock)); ServerDrv::sendData(_sock, buf, size); + while (!ServerDrv::isDataSent(_sock)); } } diff --git a/WiFi/Server.cpp b/WiFi/Server.cpp index 90ef0a321..695c55d25 100755 --- a/WiFi/Server.cpp +++ b/WiFi/Server.cpp @@ -66,10 +66,7 @@ void Server::write(const uint8_t *buffer, size_t size) client.status() == ESTABLISHED) { client.write(buffer, size); - }else{ - delay(20); } - } } } diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 1716bc90b..337c0bb93 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -191,5 +191,11 @@ uint8_t WiFiClass::status() return WiFiDrv::getConnectionStatus(); } +uint8_t WiFiClass::test() +{ + return WiFiDrv::testCmd(); +} + + WiFiClass WiFi; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index d97209b63..738f6f256 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -86,6 +86,9 @@ public: // Return Connection status uint8_t status(); + // function used for test + uint8_t test(); + friend class Client; friend class Server; }; diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde index a3a5c8b0d..fa79002f1 100644 --- a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde +++ b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde @@ -10,6 +10,7 @@ */ #include #include +#define _PRINT_ byte mac[6] = { 0 }; IPAddress ip; @@ -103,11 +104,13 @@ int startWiFiWpa() { Serial.println("\nSetup WiFi Wpa..."); //strcpy(ssid, "AndroidAP9647"); + //strcpy(ssid, "AndroidAP3551"); strcpy(ssid, "Cariddi"); Serial.print("SSID: "); Serial.println(ssid); const char *pass = "1234567890"; status = WiFi.begin(ssid, pass); + //status = WiFi.begin(ssid); if ( status != WL_CONNECTED) { Serial.println("Connection Failed"); @@ -146,9 +149,12 @@ void setup() void execCmd(char* buf) { + #ifdef _PRINT_ Serial.print("\nExecuting command: "); Serial.println(buf); - server.write(buf); + #endif + //server.write(buf); + server.print(buf); } @@ -166,12 +172,18 @@ void loop() } static byte idx = 0; + int c = 0; - while (client.available()) + do { - dataBuf[idx] = client.read(); + c = client.read(); + if (c!=-1) + { + dataBuf[idx] = c; + #ifdef _PRINT_ if (idx == 0) Serial.print("Client chatting...: "); Serial.print(dataBuf[idx]); + #endif if ((dataBuf[idx] == 0xa)/*||(dataBuf[idx] == 0xd)*/) { dataBuf[idx+1]=0; @@ -181,7 +193,8 @@ void loop() }else{ ++idx; } - } + } + }while (c!=-1); } } } diff --git a/WiFi/examples/wifi_example/wifi_example.pde b/WiFi/examples/wifi_example/wifi_example.pde index 6606c2b1f..4a05228b9 100755 --- a/WiFi/examples/wifi_example/wifi_example.pde +++ b/WiFi/examples/wifi_example/wifi_example.pde @@ -114,12 +114,12 @@ void setup() { Serial.begin(9600); Serial.println("*** Start WiFi example ***"); - + WiFi.begin(); //scanNetworks(); delay(3000); - int _status = startWiFi(); - + //int _status = startWiFi(); +/* if ( _status == WL_CONNECTED) { Serial.println("Wifi Connected!"); @@ -129,17 +129,53 @@ void setup() printCurrNet(); scanNetworks(); - /* + Serial.println("Starting server..."); server.begin(); delay(1000); - */ + } + */ } void loop() { + static boolean free = false; + +// if (free) Serial.println(WiFi.test(),10); + if (free) WiFi.test(); + + byte c = Serial.read(); + if (c!=255) + { + switch (c) + { + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + { + int i = 0; + for (;i<(c-0x30);++i) + { + int num = WiFi.test(); + //Serial.println(num,10); + } + break; + } + case 'c': + free = true; + break; + case 's': + free = false; + break; + + } + } /* + Client client = server.available(); static uint8_t count = 0; Serial.println("Retry connect..."); status = WiFi.begin(ssid); diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index b6228d4ed..ef3ba8c22 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -13,8 +13,6 @@ #include #include -#include "HardwareSerial.h" - #define INFO_0 1 #define INFO_1 2 #define INFO_2 4 diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index bd7dd16a1..8688b9b56 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -12,7 +12,8 @@ extern "C" { #define SPICLOCK 13//sck #define SLAVESELECT 2//ss #define SLAVEREADY 3 - + +#define DELAY_100NS asm volatile("nop") void SpiDrv::begin() { @@ -60,6 +61,16 @@ void SpiDrv::spiSlaveDeselect() digitalWrite(SLAVESELECT,HIGH); } +void delaySpi() +{ + int i = 0; + const int DELAY = 1000; + for (;i #include "wifi_spi.h" -#define WAIT_CHAR_DELAY 100 -#define TIMEOUT_CHAR_DELAY 1 //10 -#define TIMEOUT_READY_SLAVE 1000 -#define SPI_TX_DELAY 1 -#define SPI_START_CMD_DELAY 10 - +#define SPI_START_CMD_DELAY 10 #define NO_LAST_PARAM 0 #define LAST_PARAM 1 @@ -25,7 +20,9 @@ class SpiDrv { private: - static bool waitSlaveReady(); + //static bool waitSlaveReady(); + static void waitForSlaveSign(); + static void getParam(uint8_t* param); public: static void begin(); diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index a979659a6..a8dffe351 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -351,4 +351,25 @@ int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) return networkRssi; } +uint8_t WiFiDrv::testCmd() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(TEST_CMD, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(TEST_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _data; +} + + WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index 74af7837b..661e0e45f 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -62,6 +62,7 @@ public: static uint8_t getEncTypeNetowrks(uint8_t networkItem); + static uint8_t testCmd(); }; extern WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index 4f0b30855..576c5fb11 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -23,6 +23,7 @@ enum { SET_NET_CMD = 0x10, SET_PASSPHRASE_CMD = 0x11, SET_KEY_CMD = 0x12, + TEST_CMD = 0x13, GET_CONN_STATUS_CMD = 0x20, GET_IPADDR_CMD = 0x21,