Test Ethernet, SD card and BLE of Arch Link

Dependencies:   BLE_API SDFileSystem WIZnet_Library mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Arch Link Test Program for BLE
00003 Jacky Zhang @ 20150526
00004 */
00005 
00006 
00007 #define TEST_W5500
00008 #define TEST_SD
00009 //#define TEST_I2C
00010 #define TEST_BLE
00011 
00012 
00013 #include "mbed.h"
00014 #include "BLE.h"
00015 #include "HealthThermometerService.h"
00016 #include "WIZnetInterface.h"
00017 #include "SDFileSystem.h"
00018 
00019 #ifdef TEST_I2C
00020 #include "TSL2561.h"
00021 #endif
00022 
00023 //Arch Link
00024 Serial pc(USBTX, USBRX);
00025 DigitalOut led1(LED1);
00026 
00027 #ifdef TEST_SD
00028 //SD Card
00029 int SD_Test(void);
00030 SDFileSystem sd(SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0, p11, "sd"); // MOSI, MISO, SCK, CS
00031 DirHandle *dir;
00032 FILE *fp;
00033 #endif
00034 
00035 #ifdef TEST_W5500
00036 //W5500
00037 #define USE_DHCP 1
00038 #define LOOPBACKPORT    5000
00039 int W5500_Test(void);
00040 SPI spi(SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0); // mosi, miso, sclk
00041 WIZnetInterface ethernet(&spi, p24, p17); // spi, cs, reset
00042 int ret;
00043 const unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x1C,0xAA,0xCA};
00044 #endif
00045 
00046 #ifdef TEST_BLE
00047 //BLE
00048 void periodicCallback(void);
00049 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason);
00050 int BLE_Test(void);
00051 BLEDevice ble;
00052 static volatile bool  triggerSensorPolling = false;
00053 const static char     DEVICE_NAME[]        = "Arch Link Test";
00054 static const uint16_t uuid16_list[]        = {GattService::UUID_HEALTH_THERMOMETER_SERVICE};
00055 #endif
00056 
00057 #ifdef TEST_I2C
00058 //TSL2561
00059 int I2C_Test(void);
00060 TSL2561 tsl2561(p5, p6);
00061 #endif
00062 
00063 int main(void)
00064 {
00065     dir = (DirHandle *)malloc(sizeof(DirHandle));
00066     
00067     wait(0.2);
00068     pc.baud(115200);
00069     wait(0.2);
00070     pc.printf("hello\r\n");
00071     
00072 //    while(1)
00073 //  {
00074 //      led1 = 1;
00075 //      wait(0.5);
00076 //      led1 = 0;
00077 //      wait(0.5);
00078 //      pc.printf("hello\r\n");
00079 //  }
00080     
00081     
00082     
00083     #ifdef TEST_W5500
00084     if(W5500_Test() == 0)
00085     {
00086         pc.printf("W5500 tested OK\r\n\r\n");
00087     }
00088     else
00089     {
00090         pc.printf("W5500 tested NOT OK!\r\n\r\n");
00091     }
00092     #endif
00093     
00094     #ifdef TEST_SD
00095     if(SD_Test() == 0)
00096     {
00097         pc.printf("SD Card tested OK\r\n\r\n");
00098     }
00099     else
00100     {
00101         pc.printf("SD Card tested NOT OK!\r\n\r\n");
00102     }
00103     #endif
00104     
00105     #ifdef TEST_I2C
00106     if(I2C_Test() == 0)
00107     {
00108         pc.printf("I2C tested OK\r\n\r\n");
00109     }
00110     else
00111     {
00112         pc.printf("I2C tested NOT OK!\r\n\r\n");
00113     }
00114     #endif
00115     
00116     #ifdef TEST_BLE
00117     BLE_Test();
00118     #endif
00119     
00120     while (true)
00121     { 
00122         wait(0.5);
00123     }
00124 }
00125 
00126 #ifdef TEST_SD
00127 int SD_Test(void)
00128 {
00129     int result;
00130     
00131     pc.printf("checking SD Card...\r\n");
00132     result = sd.disk_initialize();
00133     if(result != 0)
00134     {
00135         pc.printf("SD Card initialized failed...\r\n");
00136         return -1;
00137     }
00138     else
00139     {
00140         pc.printf("SD Card initialized OK\r\n");
00141     }
00142     
00143     uint64_t sectors = sd.disk_sectors();
00144     unsigned int capacity = sectors / 2 / 1024;
00145     pc.printf("SD card capacity is: %dMB\r\n", capacity);
00146 
00147     dir = opendir("/sd/testdir");
00148     if(dir == NULL)
00149     {
00150         pc.printf("the folder is not exist, let us create one\r\n");
00151         result = mkdir("/sd/testdir", 0777);
00152         if(result != 0)
00153         {
00154             pc.printf("mkdir WRONG, %d\r\n", result);
00155             return -3;
00156         }
00157         pc.printf("the folder was created\r\n");
00158     }
00159     
00160     pc.printf("open file: sdtest.txt\r\n");
00161     //w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件
00162     //a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)
00163     fp = fopen("/sd/testdir/sdtest.txt", "w+");
00164     pc.printf("opened\r\n");
00165     if(fp == NULL)
00166     {
00167         pc.printf("Could not open file for write\r\n");
00168         return -2;
00169     }
00170 
00171     int num = fprintf(fp, "Hello fun SD Card World!\r\n");
00172     if(num != strlen("Hello fun SD Card World!\r\n"))
00173     {
00174         pc.printf("can't write into sdtested.txt\r\n");
00175         //fclose(fp);
00176         //free(dir);
00177         return -4;
00178     }
00179     pc.printf("%d characters was wrote into sdtested.txt\r\n", num);
00180     
00181     //fclose(fp);
00182     //free(dir);
00183    
00184     return 0;
00185 }
00186 
00187 #endif
00188 
00189 #ifdef TEST_W5500
00190 int W5500_Test(void)
00191 {
00192     mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres
00193     wait(1);
00194     pc.printf("Start to test ethernet!\r\n");
00195     
00196     #if USE_DHCP
00197     pc.printf("use DHCP\r\n");
00198     ret = ethernet.init((unsigned char *)MAC_Addr);
00199     #else
00200     pc.printf("do NOT use DHCP\r\n");
00201     int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
00202     #endif
00203     
00204     if (!ret) {
00205         pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
00206         ret = ethernet.connect();
00207         if (!ret) {
00208             pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
00209                       ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
00210             return 0;
00211         } else {
00212             pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
00213             //exit(0);
00214             return -1;
00215         }
00216     } else {
00217         pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
00218         //exit(0);
00219         return -1;
00220     }
00221     /*
00222     TCPSocketServer server;
00223     server.bind(LOOPBACKPORT);
00224     server.listen();
00225     
00226     while (1) {
00227         pc.printf("\nWait for new connection...\r\n");
00228         TCPSocketConnection client;
00229         server.accept(client);
00230         client.set_blocking(false, 0); // Timeout=0.
00231         pc.printf("Connection from: %s\r\n", client.get_address());
00232         while (client.is_connected() == true) {
00233             int n = client.receive(buffer, sizeof(buffer));
00234             if(n > 0)
00235                 client.send_all(buffer, n);
00236             if(client.is_fin_received())
00237                 client.close();
00238         }
00239         pc.printf("Disconnected.\r\n");
00240     }*/
00241 }
00242 #endif
00243 
00244 #ifdef TEST_I2C
00245 int I2C_Test(void)
00246 {
00247     uint16_t light_sensor_id = 0;
00248     
00249     light_sensor_id = tsl2561.read_ID();
00250     pc.printf("check id: %d\r\n", light_sensor_id);
00251     if(light_sensor_id != 0)
00252     {
00253         for(int i = 0; i < 10; i++)
00254         {
00255             pc.printf("lux = %f\r\n", tsl2561.lux());
00256             wait(0.2);
00257         }
00258         return 0;
00259     }
00260     else
00261     {
00262         return -1;
00263     }
00264 }
00265 #endif
00266 
00267 #ifdef TEST_BLE
00268 /* Restart Advertising on disconnection*/
00269 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00270 {
00271     ble.startAdvertising();
00272 }
00273 
00274 void periodicCallback(void)
00275 {
00276     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00277 
00278     /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
00279      * heavy-weight sensor polling from the main thread. */
00280     triggerSensorPolling = true;
00281 }
00282 
00283 int BLE_Test(void)
00284 {
00285     pc.printf("begin to test BLE\r\n");
00286     
00287     Ticker ticker;
00288     ticker.attach(periodicCallback, 1);
00289     
00290     ble.init();
00291     ble.onDisconnection(disconnectionCallback);
00292 //return 0;
00293     /* Setup primary service. */
00294     float currentTemperature = 39.6;
00295     HealthThermometerService thermometerService(ble, currentTemperature, HealthThermometerService::LOCATION_EAR);
00296 
00297     /* setup advertising */
00298     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00299     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00300     ble.accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR);
00301     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00302     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00303     ble.setAdvertisingInterval(1000); /* 1000ms */
00304     ble.startAdvertising();
00305     
00306     pc.printf("BLE initialized OK\r\n");
00307     
00308     while(1)
00309     {
00310         if (triggerSensorPolling && ble.getGapState().connected) {
00311             triggerSensorPolling = false;
00312 
00313             /* Do blocking calls or whatever is necessary for sensor polling. */
00314             // error = sensor.readData();
00315             // if (!error) {
00316             //     thermometerService.updateTemperature(c);
00317             // }
00318 
00319             /* In our case, we simply update the dummy temperature measurement. */
00320             currentTemperature += 0.1;
00321             thermometerService.updateTemperature(currentTemperature);
00322             pc.printf("data updated\r\n");
00323         } else {
00324             ble.waitForEvent();
00325         }
00326     }
00327     
00328     //return 0;
00329 }
00330 #endif
00331