Init Project for PSM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* WIZnet IoT Shield Cat.M1 Sample code for Arm MBED
00002  * Copyright (c) 2019 WIZnet Co., Ltd.
00003  * SPDX-License-Identifier: Apache-2.0
00004  */
00005  
00006  /* 
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *
00017  * See the License for the specific language governing permissions and
00018  * limitations under the License.
00019  *
00020  */
00021 
00022 #include "mbed.h"
00023 
00024 #include <string>
00025 
00026 #define RET_OK                      1
00027 #define RET_NOK                     -1
00028 #define DEBUG_ENABLE                1
00029 #define DEBUG_DISABLE               0
00030 #define ON                          1
00031 #define OFF                         0
00032 
00033 #define MAX_BUF_SIZE                1024
00034 
00035 #define WM01_APN_PROTOCOL_IPv4      1
00036 #define WM01_APN_PROTOCOL_IPv6      2
00037 #define WM01_DEFAULT_TIMEOUT        1000
00038 #define WM01_CONNECT_TIMEOUT        15000
00039 #define WM01_SEND_TIMEOUT           500
00040 #define WM01_RECV_TIMEOUT           500
00041 #define WM01_BOOTING_TIME           15000
00042 
00043 #define WM01_APN_PROTOCOL           WM01_APN_PROTOCOL_IPv6
00044 #define WM01_DEFAULT_BAUD_RATE      115200
00045 #define WM01_PARSER_DELIMITER       "\r\n"
00046 
00047 #define CATM1_APN_SKT               "lte-internet.sktelecom.com"
00048 
00049 #define CATM1_DEVICE_NAME_WM01      "WM01"
00050 #define DEVNAME                     CATM1_DEVICE_NAME_WM01
00051 
00052 #define devlog(f_, ...)             if(CATM1_DEVICE_DEBUG == DEBUG_ENABLE) { pc.printf("\r\n[%s] ", DEVNAME);  pc.printf((f_), ##__VA_ARGS__); }
00053 #define myprintf(f_, ...)           {pc.printf("\r\n[MAIN] ");  pc.printf((f_), ##__VA_ARGS__);}
00054 
00055 /* Pin configuraiton */
00056 // Cat.M1
00057 #define MBED_CONF_IOTSHIELD_CATM1_TX        D8
00058 #define MBED_CONF_IOTSHIELD_CATM1_RX        D2
00059 #define MBED_CONF_IOTSHIELD_CATM1_RESET     D7
00060 #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY    D9
00061 
00062 // Sensors
00063 #define MBED_CONF_IOTSHIELD_SENSOR_CDS      A0
00064 #define MBED_CONF_IOTSHIELD_SENSOR_TEMP     A1
00065 
00066 /* Debug message settings */
00067 #define WM01_PARSER_DEBUG                   DEBUG_DISABLE
00068 #define CATM1_DEVICE_DEBUG                  DEBUG_ENABLE 
00069 
00070 char periodic_tau[] = "10100001";   // (60s * 1) + (3s ~ 4s) = 63s ~ 64s
00071 char active_time[] = "00000001";    // (2s * 1) + (24s ~ 27s) = 26s ~ 29s
00072 
00073 // Functions: Print information
00074 void printInfo(void);
00075 
00076 // Functions: Module Status
00077 void waitCatM1Ready(void);
00078 int8_t setEchoStatus_WM01(bool onoff);
00079 int8_t getUsimStatus_WM01(void);
00080 int8_t getNetworkStatus_WM01(void);
00081 
00082 // Functions: PSM (Power Saving Mode)
00083 int8_t setPsmActivate_WM01(char *Requested_Periodic_TAU, char *Requested_Active_Time);
00084 int8_t setPsmDeactivate_WM01(void);
00085 
00086 // Functions: Network time
00087 int8_t getNetworkTimeLocal_WM01(char *time);
00088 void setFlagGettime(void);
00089 void clearFlagGettime(void);
00090 
00091 Serial pc(USBTX, USBRX);    // USB debug
00092 
00093 UARTSerial *_serial;        // Cat.M1 module    
00094 ATCmdParser *_parser;
00095 
00096 DigitalOut _RESET_WM01(MBED_CONF_IOTSHIELD_CATM1_RESET);
00097 DigitalOut _PWRKEY_WM01(MBED_CONF_IOTSHIELD_CATM1_PWRKEY);
00098 
00099 Ticker flip;
00100 
00101 bool flag_gettime = false;
00102 
00103 void serialPcInit(void)
00104 {
00105     pc.baud(115200);
00106     pc.format(8, Serial::None, 1);
00107 }
00108 
00109 void serialDeviceInit(PinName tx, PinName rx, int baudrate) 
00110 {        
00111     _serial = new UARTSerial(tx, rx, baudrate);    
00112 }
00113 
00114 void serialAtParserInit(const char *delimiter, bool debug_en)
00115 {
00116     _parser = new ATCmdParser(_serial);    
00117     _parser->debug_on(debug_en);
00118     _parser->set_delimiter(delimiter);    
00119     _parser->set_timeout(WM01_DEFAULT_TIMEOUT);
00120 }
00121 
00122 void catm1DeviceInit(void)
00123 {
00124     serialDeviceInit(   MBED_CONF_IOTSHIELD_CATM1_TX, 
00125                         MBED_CONF_IOTSHIELD_CATM1_RX, 
00126                         WM01_DEFAULT_BAUD_RATE);
00127                         
00128     serialAtParserInit( WM01_PARSER_DELIMITER, 
00129                         WM01_PARSER_DEBUG);
00130 }
00131 
00132 void catm1DeviceReset_WM01(void)
00133 {
00134     _RESET_WM01 = 1;
00135     _PWRKEY_WM01 = 1;
00136     wait_ms(300);
00137     
00138     _RESET_WM01 = 0;
00139     _PWRKEY_WM01 = 0;
00140     wait_ms(400);
00141     
00142     _RESET_WM01 = 1;    
00143     wait_ms(1000);
00144 }
00145 
00146 // ----------------------------------------------------------------
00147 // Main routine
00148 // ----------------------------------------------------------------
00149 
00150 int main()
00151 {     
00152     bool psm_en = false;
00153     char time[30] = {0, };
00154     float elapsed_time_sec = 0; 
00155 
00156     Timer t;
00157 
00158     serialPcInit();    
00159     catm1DeviceInit();
00160     
00161     myprintf("Waiting for Cat.M1 Module Ready...\r\n");
00162     
00163     catm1DeviceReset_WM01();
00164     
00165     waitCatM1Ready();
00166     
00167     wait_ms(5000);
00168             
00169     myprintf("System Init Complete\r\n");
00170         
00171     printInfo();
00172     
00173     setEchoStatus_WM01(OFF);
00174    
00175     getUsimStatus_WM01();
00176     
00177     getNetworkStatus_WM01();
00178 
00179 #if 0
00180 // PSM disable   
00181     setPsmDeactivate_WM01();
00182 #endif    
00183 
00184     if(psm_en != true) 
00185     {
00186         if(setPsmActivate_WM01(periodic_tau, active_time) == RET_OK)
00187         {
00188             myprintf("Cat.M1 PSM enable, Device reboot\r\n");    
00189 
00190             // Cat.M1 reboot
00191             catm1DeviceReset_WM01();
00192 
00193             waitCatM1Ready();
00194         } 
00195         else 
00196         {
00197             myprintf("Cat.M1 PSM enable failed\r\n");
00198         }            
00199     }
00200 
00201     // Timer event callback       
00202     flip.attach(callback(&setFlagGettime), 1.0);
00203 
00204     while(1)
00205     {
00206         if(flag_gettime) 
00207         {
00208             if(getNetworkTimeLocal_WM01(time) == RET_OK) 
00209             {
00210                 if(elapsed_time_sec > 0) 
00211                 {
00212                     t.stop();
00213 
00214                     myprintf("Cat.M1 Active, Sleep time: %.2fsec", elapsed_time_sec);                    
00215 
00216                     elapsed_time_sec = 0;   
00217                 }    
00218 
00219                 myprintf("%s", time);
00220             } 
00221             else 
00222             {
00223                 if(elapsed_time_sec == 0) 
00224                 {            
00225                     t.reset();        
00226                     t.start();         
00227 
00228                     myprintf("%s", "PSM Power Down Start");               
00229                 }
00230 
00231                 elapsed_time_sec = t.read();
00232 
00233                 myprintf("Cat.M1 PSM, %.2f", elapsed_time_sec);               
00234             }
00235 
00236             clearFlagGettime();
00237         }
00238     }
00239 }
00240 
00241 // ----------------------------------------------------------------
00242 // Functions: Print information
00243 // ----------------------------------------------------------------
00244 
00245 void printInfo(void)
00246 {
00247     myprintf("WIZnet IoT Shield for Arm MBED");
00248     myprintf("LTE Cat.M1 Version");
00249     myprintf("=================================================");
00250     myprintf(">> Target Board: WIoT-WM01 (Woorinet WM-N400MSE)");
00251     myprintf(">> Sample Code: PSM Test");
00252     myprintf("=================================================\r\n");
00253 }
00254 
00255 // ----------------------------------------------------------------
00256 // Functions: Cat.M1 Status
00257 // ----------------------------------------------------------------
00258 
00259 void waitCatM1Ready(void)
00260 {
00261     while(1) 
00262     {
00263         if(_parser->send("AT") && _parser->recv("OK"))
00264         {
00265             myprintf("WM01 is Available\r\n");
00266             
00267             return;
00268         }
00269     }
00270 }
00271 
00272 int8_t setEchoStatus_WM01(bool onoff)
00273 {
00274     int8_t ret = RET_NOK;
00275     char _buf[10];
00276     
00277     sprintf((char *)_buf, "ATE%d", onoff);    
00278     
00279     if(_parser->send(_buf) && _parser->recv("OK")) 
00280     {        
00281         devlog("Turn Echo %s : success\r\n", onoff ? "ON" : "OFF");
00282 
00283         ret = RET_OK;
00284     } 
00285     else 
00286     { 
00287         devlog("Turn Echo %s : failed\r\n", onoff ? "ON" : "OFF");
00288     }    
00289 
00290     return ret;
00291 }
00292  
00293 int8_t getUsimStatus_WM01(void)
00294 {
00295     int8_t ret = RET_NOK;
00296     
00297     if(_parser->send("AT$$STAT?") && _parser->recv("$$STAT:READY") && _parser->recv("OK")) 
00298     {
00299         devlog("USIM Status : READY\r\n");
00300 
00301         ret = RET_OK;
00302     } 
00303     else 
00304     { 
00305         devlog("Retrieving USIM Status failed\r\n");        
00306     }
00307     
00308     return ret;
00309 }
00310 
00311 int8_t getNetworkStatus_WM01(void)
00312 {
00313     int8_t ret = RET_NOK;
00314     int val, stat;
00315     
00316     if(_parser->send("AT+CEREG?") && _parser->recv("+CEREG: %d,%d", &val, &stat) && _parser->recv("OK")) 
00317     {
00318         if((val == 0) && (stat == 1))
00319         {
00320             devlog("Network Status : attached\r\n");
00321 
00322             ret = RET_OK;
00323         }
00324         else
00325         {
00326             devlog("Network Status : %d, %d\r\n", val, stat);
00327         }
00328     }
00329     else
00330     {
00331         devlog("Network Status : Error\r\n");
00332     }
00333 
00334     return ret;
00335 }
00336 
00337 // ----------------------------------------------------------------
00338 // Functions: Cat.M1 PSM activate / deactivate
00339 // ----------------------------------------------------------------
00340 
00341 int8_t setPsmActivate_WM01(char *Requested_Periodic_TAU, char *Requested_Active_Time)
00342 {
00343     int8_t ret = RET_NOK;
00344 
00345     if(_parser->send("AT+CPSMS=1,,,\"%s\",\"%s\"", Requested_Periodic_TAU, Requested_Active_Time) && _parser->recv("OK")) 
00346     {
00347         devlog("PSM activate : success\r\n");
00348 
00349         ret = RET_OK;
00350     }
00351     else
00352     {
00353         devlog("PSM activate : failed\r\n");
00354     }
00355 
00356     return ret;
00357 }
00358 
00359 int8_t setPsmDeactivate_WM01(void)
00360 {
00361     int8_t ret = RET_NOK;
00362     
00363     if(_parser->send("AT+CPSMS=0") && _parser->recv("OK"))
00364     {
00365         devlog("PSM deactivate : success\r\n");
00366     }
00367     else
00368     {
00369         devlog("PSM deactivate : failed\r\n");
00370     }
00371     
00372     return ret;
00373 }
00374 
00375 // ----------------------------------------------------------------
00376 // Functions: Cat.M1 Network time
00377 // ----------------------------------------------------------------
00378 
00379 int8_t getNetworkTimeLocal_WM01(char *time)
00380 {
00381     int8_t ret = RET_NOK;
00382     
00383     if(_parser->send("AT$$MSTIME?") && _parser->recv("$$MSTIME:%s\n", time) && _parser->recv("OK"))  
00384     {
00385         //devlog("Get current local time success\r\n");      
00386 
00387         ret = RET_OK;
00388     }
00389 
00390     return ret;
00391 }
00392 
00393 void setFlagGettime(void)
00394 {
00395     flag_gettime = true;
00396 }
00397 
00398 void clearFlagGettime(void)
00399 {
00400     flag_gettime = false;
00401 }