TI's CC3100. A test demo with very little testing done!

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * main.c - sample application to switch to AP mode and ping client
00003  *
00004  * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
00005  *
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *    Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *
00014  *    Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the
00017  *    distribution.
00018  *
00019  *    Neither the name of Texas Instruments Incorporated nor the names of
00020  *    its contributors may be used to endorse or promote products derived
00021  *    from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00026  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  */
00036 
00037 /*
00038  * Application Name     -   Getting started with Wi-Fi Access-Point mode
00039  * Application Overview -   This sample application demonstrates how
00040  *                          to configure CC3100 in Access-Point mode. Any
00041  *                          WLAN station in its range can connect/communicate
00042  *                          to/with it as per the standard networking protocols.
00043  *                          On a successful connection, the device ping's the
00044  *                          connected station.
00045  * Application Details  -   http://processors.wiki.ti.com/index.php/CC31xx_Getting_Started_with_WLAN_AP
00046  *                          doc\examples\getting_started_with_wlan_ap.pdf
00047  */
00048 
00049 #include "cc3100_simplelink.h"
00050 #include "cc3100_sl_common.h"
00051 
00052 #include "fPtr_func.h"
00053 #include "cc3100.h"
00054 #include "cc3100_spi.h"
00055 #include "myBoardInit.h"
00056 
00057 using namespace mbed_cc3100;
00058 
00059 #if (THIS_BOARD == MBED_BOARD_LPC1768)
00060 //cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC1768  irq, nHib, cs, mosi, miso, sck
00061 cc3100 _cc3100(p9, p10, p8, SPI(p11, p12, p13));//LPC1768  irq, nHib, cs, mosi, miso, sck
00062 Serial pc(USBTX, USBRX);//lpc1768
00063 #elif (THIS_BOARD == ST_MBED_NUCLEOF411)
00064 cc3100 _cc3100(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF411  irq, nHib, cs, mosi, miso, sck
00065 Serial pc(SERIAL_TX, SERIAL_RX);//nucleoF411
00066 #elif (THIS_BOARD == ST_MBED_NUCLEOF401)
00067 cc3100 _cc3100(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF401  irq, nHib, cs, mosi, miso, sck
00068 Serial pc(SERIAL_TX, SERIAL_RX);//nucleoF401
00069 #elif (THIS_BOARD == EA_MBED_LPC4088)
00070 cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC4088  irq, nHib, cs, mosi, miso, sck
00071 Serial pc(USBTX, USBRX);//EA_lpc4088
00072 #elif (THIS_BOARD == ST_MBED_NUCLEOF103)
00073 cc3100 _cc3100(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF103  irq, nHib, cs, mosi, miso, sck
00074 Serial pc(SERIAL_TX, SERIAL_RX);
00075 #elif (THIS_BOARD == Seeed_Arch_Max)
00076 cc3100 _cc3100(PD_12, PD_13, PD_11, SPI(PB_5, PB_4, PB_3));//Seeed_Arch_Max  irq, nHib, cs, mosi, miso, sck
00077 Serial pc(USBTX, USBRX);
00078 #else
00079 
00080 #endif
00081 
00082 #define APPLICATION_VERSION "1.2.0"
00083 
00084 /*
00085  * GLOBAL VARIABLES -- Start
00086  */
00087 int32_t demo = 0;
00088 
00089 /*
00090  * GLOBAL VARIABLES -- End
00091  */
00092 
00093 
00094 /*
00095  * STATIC FUNCTION DEFINITIONS -- Start
00096  */
00097 
00098 static void displayBanner();
00099 /*
00100  * STATIC FUNCTION DEFINITIONS -- End
00101  */
00102  
00103 void station_app(void);
00104 void AP_app(void);
00105 
00106 /*
00107  * Application's entry point
00108  */
00109   
00110  
00111 int main(void) {
00112 
00113     pc.baud(115200);
00114 
00115     int32_t retVal = -1;
00116 
00117     retVal = _cc3100.initializeAppVariables();
00118     ASSERT_ON_ERROR(retVal);
00119 
00120     displayBanner();
00121 
00122     _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE);
00123     g_PingPacketsRecv = 0;
00124     
00125     /*
00126      * Following function configures the device to default state by cleaning
00127      * the persistent settings stored in NVMEM (viz. connection profiles &
00128      * policies, power policy etc)
00129      *
00130      * Applications may choose to skip this step if the developer is sure
00131      * that the device is in its default state at start of application
00132      *
00133      * Note that all profiles and persistent settings that were done on the
00134      * device will be lost
00135      */
00136     retVal = _cc3100.configureSimpleLinkToDefaultState();
00137     
00138     if(retVal < 0) {
00139         if (DEVICE_NOT_IN_STATION_MODE == retVal)
00140             printf(" Failed to configure the device to its default state \n\r");
00141 
00142         LOOP_FOREVER();
00143     }
00144 
00145     printf(" Device is configured in it's default state \n\r");
00146     
00147     /*
00148      * Assumption is that the device is configured in station mode already
00149      * and it is in its default state
00150      */
00151     /* Initializing the CC3100 device */
00152     
00153     if(!(demo ==1)){
00154         retVal = _cc3100.sl_Start(0, 0, 0);
00155     
00156         if ((retVal < 0) || (ROLE_STA != retVal) ){
00157         printf(" Failed to start the device \n\r");
00158         LOOP_FOREVER();
00159         }
00160 
00161         printf(" Device started as STATION \n\r");
00162     }
00163     
00164     if(demo == 0 ){
00165        station_app();
00166     }else{
00167        AP_app(); 
00168     }    
00169     return 0;
00170 }    
00171 
00172 /*!
00173     \brief This function displays the application's banner
00174 
00175     \param      None
00176 
00177     \return     None
00178 */
00179 static void displayBanner()
00180 {
00181     if(!demo){
00182         printf("\n\r\n\r");
00183         printf(" Getting started with WLAN access-point application - Version ");
00184         printf("%s",APPLICATION_VERSION);
00185         printf("\n\r*******************************************************************************\n\r");
00186     
00187     }else{
00188          printf("\n\r\n\r");
00189          printf(" Getting started with station application - Version ");
00190          printf(" %s", APPLICATION_VERSION);
00191          printf("\n\r*******************************************************************************\n\r");
00192     }
00193 }
00194 
00195 void AP_app(void){
00196     
00197     SlPingStartCommand_t PingParams = {0};
00198     SlPingReport_t Report = {0};
00199 //    SlNetCfgIpV4Args_t ipV4 = {0};
00200 //    SlNetAppDhcpServerBasicOpt_t dhcpParams = {0};
00201 
00202     uint8_t SecType = 0;
00203     int32_t role = ROLE_STA;
00204     int32_t retVal = -1;
00205     
00206     /*
00207      * Assumption is that the device is configured in station mode already
00208      * and it is in its default state
00209      */
00210     role = _cc3100.sl_Start(0, 0, 0);
00211     if (ROLE_AP == role) {
00212         /* If the device is in AP mode, we need to wait for this event before doing anything */
00213         while(!_cc3100.IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED)) {
00214             _cc3100._nonos._SlNonOsMainLoopTask();
00215         }
00216     } else {
00217         /* Configure CC3100 to start in AP mode */
00218         retVal = _cc3100._wlan.sl_WlanSetMode(ROLE_AP);
00219         if(retVal < 0)
00220             LOOP_FOREVER();
00221             
00222         /* Configure the SSID of the CC3100 */
00223         retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID,
00224                 strlen(SSID_AP_MODE), (uint8_t *)SSID_AP_MODE);
00225         if(retVal < 0)
00226             LOOP_FOREVER();
00227 
00228         SecType = SEC_TYPE_AP_MODE;
00229         /* Configure the Security parameter the AP mode */
00230         retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, (uint8_t *)&SecType);
00231         if(retVal < 0)
00232             LOOP_FOREVER();
00233 
00234         retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(PASSWORD_AP_MODE), (uint8_t *)PASSWORD_AP_MODE);
00235         if(retVal < 0)
00236             LOOP_FOREVER();    
00237 
00238         retVal = _cc3100.sl_Stop(SL_STOP_TIMEOUT);
00239         if(retVal < 0)
00240             LOOP_FOREVER();
00241 
00242         _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_IP_ACQUIRED);
00243 
00244         role = _cc3100.sl_Start(0, 0, 0);
00245         if (ROLE_AP == role) {
00246             /* If the device is in AP mode, we need to wait for this event before doing anything */
00247             while(!_cc3100.IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED)) {
00248                 _cc3100._nonos._SlNonOsMainLoopTask();
00249             }
00250         } else {
00251             printf(" Device couldn't be configured in AP mode \n\r");
00252             LOOP_FOREVER();
00253         }
00254     }
00255     printf(" Device started as Access Point\n\r");
00256 
00257     /* Wait */
00258     printf(" Waiting for clients to connect...!\n\r");
00259     while((!_cc3100.IS_IP_LEASED(g_Status,STATUS_BIT_IP_LEASED)) || (!_cc3100.IS_STA_CONNECTED(g_Status,STATUS_BIT_STA_CONNECTED))) {
00260         _cc3100._nonos._SlNonOsMainLoopTask();
00261     }
00262     printf(" Client connected to the device \n\r");
00263     printf(" Pinging...! \n\r");
00264 
00265     /* Set the ping parameters */
00266     PingParams.PingIntervalTime = PING_INTERVAL;
00267     PingParams.PingSize = PING_SIZE;
00268     PingParams.PingRequestTimeout = PING_TIMEOUT;
00269     PingParams.TotalNumberOfAttempts = PING_ATTEMPTS;
00270     PingParams.Flags = 0;
00271     PingParams.Ip = g_StationIP; /* Fill the station IP address connected to CC3100 */
00272 
00273     /* Ping client connected to CC3100 */
00274     retVal = _cc3100._netapp.sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&Report, &SimpleLinkPingReport);
00275     if(retVal < 0)
00276         LOOP_FOREVER();
00277 
00278     /* Wait */
00279     while(!_cc3100.IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) {
00280         _cc3100._nonos._SlNonOsMainLoopTask();
00281     }
00282 
00283     if (0 == g_PingPacketsRecv) {
00284         printf(" A STATION couldn't connect to the device \n\r");
00285         //ASSERT_ON_ERROR(LAN_CONNECTION_FAILED);
00286         printf(" ERROR code %d\n\r", LAN_CONNECTION_FAILED);
00287     }
00288 
00289     printf(" Device and the station are successfully connected \n\r");
00290     //return SUCCESS;
00291 }
00292 
00293 void station_app(void){
00294     
00295     int32_t retVal = -1;
00296     
00297     /* Connecting to WLAN AP */
00298     retVal = _cc3100.establishConnectionWithAP();
00299     if(retVal < 0)
00300     {
00301         printf(" Failed to establish connection w/ an AP \n\r");
00302         LOOP_FOREVER();
00303     }    
00304     printf(" Connection established w/ AP and IP is acquired \n\r");
00305     
00306     printf(" Pinging...! \n\r");
00307     retVal = _cc3100.checkLanConnection();
00308     if(retVal < 0)
00309     {
00310         printf(" Device couldn't connect to LAN \n\r");
00311         LOOP_FOREVER();
00312     }
00313     printf(" Device successfully connected to the LAN\r\n");
00314     
00315     retVal = _cc3100.checkInternetConnection();
00316     if(retVal < 0)
00317     {
00318         printf(" Device couldn't connect to the internet \n\r");
00319         LOOP_FOREVER();
00320     }
00321 
00322     printf(" Device successfully connected to the internet \n\r");
00323 }              
00324 
00325