Giung Kim
/
WIZnet-IoTShield-WM-N400MSE-PSM
Init project for PSM
Embed:
(wiki syntax)
Show/hide line numbers
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 00042 #define WM01_APN_PROTOCOL WM01_APN_PROTOCOL_IPv6 00043 #define WM01_DEFAULT_BAUD_RATE 115200 00044 #define WM01_PARSER_DELIMITER "\r\n" 00045 00046 #define CATM1_APN_SKT "lte-internet.sktelecom.com" 00047 00048 #define CATM1_DEVICE_NAME_WM01 "WM01" 00049 #define DEVNAME CATM1_DEVICE_NAME_WM01 00050 00051 #define devlog(f_, ...) if(CATM1_DEVICE_DEBUG == DEBUG_ENABLE) { pc.printf("\r\n[%s] ", DEVNAME); pc.printf((f_), ##__VA_ARGS__); } 00052 #define myprintf(f_, ...) {pc.printf("\r\n[MAIN] "); pc.printf((f_), ##__VA_ARGS__);} 00053 00054 /* Pin configuraiton */ 00055 // Cat.M1 00056 #define MBED_CONF_IOTSHIELD_CATM1_TX D8 00057 #define MBED_CONF_IOTSHIELD_CATM1_RX D2 00058 #define MBED_CONF_IOTSHIELD_CATM1_RESET D7 00059 #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9 00060 00061 // Sensors 00062 #define MBED_CONF_IOTSHIELD_SENSOR_CDS A0 00063 #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1 00064 00065 /* Debug message settings */ 00066 #define WM01_PARSER_DEBUG DEBUG_DISABLE 00067 #define CATM1_DEVICE_DEBUG DEBUG_ENABLE 00068 00069 char periodic_tau[] = "10000110"; 00070 char active_time[] = "00100001"; 00071 00072 // Functions: Print information 00073 void printInfo(void); 00074 00075 // Functions: Module Status 00076 void waitCatM1Ready(void); 00077 int8_t setEchoStatus_WM01(bool onoff); 00078 int8_t getUsimStatus_WM01(void); 00079 int8_t getNetworkStatus_WM01(void); 00080 00081 // Functions: PSM (Power Saving Mode) 00082 int8_t setPsmActivate_WM01(char *Requested_Periodic_TAU, char *Requested_Active_Time); 00083 int8_t setPsmDeactivate_WM01(void); 00084 00085 // Functions: Network time 00086 int8_t getNetworkTimeLocal_WM01(char *time); 00087 void setFlagGettime(void); 00088 void clearFlagGettime(void); 00089 00090 Serial pc(USBTX, USBRX); // USB debug 00091 00092 UARTSerial *_serial; // Cat.M1 module 00093 ATCmdParser *_parser; 00094 00095 DigitalOut _RESET_WM01(MBED_CONF_IOTSHIELD_CATM1_RESET); 00096 DigitalOut _PWRKEY_WM01(MBED_CONF_IOTSHIELD_CATM1_PWRKEY); 00097 00098 Ticker flip; 00099 00100 bool flag_gettime = false; 00101 00102 void serialPcInit(void) 00103 { 00104 pc.baud(115200); 00105 pc.format(8, Serial::None, 1); 00106 } 00107 00108 void serialDeviceInit(PinName tx, PinName rx, int baudrate) 00109 { 00110 _serial = new UARTSerial(tx, rx, baudrate); 00111 } 00112 00113 void serialAtParserInit(const char *delimiter, bool debug_en) 00114 { 00115 _parser = new ATCmdParser(_serial); 00116 _parser->debug_on(debug_en); 00117 _parser->set_delimiter(delimiter); 00118 _parser->set_timeout(WM01_DEFAULT_TIMEOUT); 00119 } 00120 00121 void catm1DeviceInit(void) 00122 { 00123 serialDeviceInit( MBED_CONF_IOTSHIELD_CATM1_TX, 00124 MBED_CONF_IOTSHIELD_CATM1_RX, 00125 WM01_DEFAULT_BAUD_RATE); 00126 00127 serialAtParserInit( WM01_PARSER_DELIMITER, 00128 WM01_PARSER_DEBUG); 00129 } 00130 00131 void catm1DeviceReset_WM01(void) 00132 { 00133 _RESET_WM01 = 1; 00134 _PWRKEY_WM01 = 1; 00135 wait_ms(300); 00136 00137 _RESET_WM01 = 0; 00138 _PWRKEY_WM01 = 0; 00139 wait_ms(400); 00140 00141 _RESET_WM01 = 1; 00142 wait_ms(1000); 00143 } 00144 00145 // ---------------------------------------------------------------- 00146 // Main routine 00147 // ---------------------------------------------------------------- 00148 00149 int main() 00150 { 00151 bool psm_en = false; 00152 char time[30] = {0, }; 00153 float elapsed_time_sec = 0; 00154 00155 Timer t; 00156 00157 serialPcInit(); 00158 catm1DeviceInit(); 00159 00160 myprintf("Waiting for Cat.M1 Module Ready...\r\n"); 00161 00162 catm1DeviceReset_WM01(); 00163 00164 waitCatM1Ready(); 00165 00166 wait_ms(5000); 00167 00168 myprintf("System Init Complete\r\n"); 00169 00170 printInfo(); 00171 00172 setEchoStatus_WM01(OFF); 00173 00174 getUsimStatus_WM01(); 00175 00176 getNetworkStatus_WM01(); 00177 00178 #if 0 00179 // PSM disable 00180 setPsmDeactivate_WM01(); 00181 #endif 00182 00183 if(psm_en != true) 00184 { 00185 if(setPsmActivate_WM01(periodic_tau, active_time) == RET_OK) 00186 { 00187 myprintf("Cat.M1 PSM enable, Device reboot\r\n"); 00188 00189 // Cat.M1 reboot 00190 catm1DeviceReset_WM01(); 00191 00192 waitCatM1Ready(); 00193 } 00194 else 00195 { 00196 myprintf("Cat.M1 PSM enable failed\r\n"); 00197 } 00198 } 00199 00200 // Timer event callback 00201 flip.attach(callback(&setFlagGettime), 1.0); 00202 00203 while(1) 00204 { 00205 if(flag_gettime) 00206 { 00207 if(getNetworkTimeLocal_WM01(time) == RET_OK) 00208 { 00209 if(elapsed_time_sec > 0) 00210 { 00211 t.stop(); 00212 00213 myprintf("Cat.M1 Active, Sleep time: %.2fsec", elapsed_time_sec); 00214 00215 elapsed_time_sec = 0; 00216 } 00217 00218 myprintf("%s", time); 00219 } 00220 else 00221 { 00222 if(elapsed_time_sec == 0) 00223 { 00224 t.reset(); 00225 t.start(); 00226 00227 myprintf("%s", "PSM Power Down Start"); 00228 } 00229 00230 elapsed_time_sec = t.read(); 00231 00232 myprintf("Cat.M1 PSM, %.2f", elapsed_time_sec); 00233 } 00234 00235 clearFlagGettime(); 00236 } 00237 } 00238 } 00239 00240 // ---------------------------------------------------------------- 00241 // Functions: Print information 00242 // ---------------------------------------------------------------- 00243 00244 void printInfo(void) 00245 { 00246 myprintf("WIZnet IoT Shield for Arm MBED"); 00247 myprintf("LTE Cat.M1 Version"); 00248 myprintf("================================================="); 00249 myprintf(">> Target Board: WIoT-WM01 (Woorinet WM-N400MSE)"); 00250 myprintf(">> Sample Code: PSM Test"); 00251 myprintf("=================================================\r\n"); 00252 } 00253 00254 // ---------------------------------------------------------------- 00255 // Functions: Cat.M1 Status 00256 // ---------------------------------------------------------------- 00257 00258 void waitCatM1Ready(void) 00259 { 00260 while(1) 00261 { 00262 if(_parser->send("AT") && _parser->recv("OK")) 00263 { 00264 myprintf("WM01 is Available\r\n"); 00265 00266 return; 00267 } 00268 } 00269 } 00270 00271 int8_t setEchoStatus_WM01(bool onoff) 00272 { 00273 int8_t ret = RET_NOK; 00274 char _buf[10]; 00275 00276 sprintf((char *)_buf, "ATE%d", onoff); 00277 00278 if(_parser->send(_buf) && _parser->recv("OK")) 00279 { 00280 devlog("Turn Echo %s : success\r\n", onoff ? "ON" : "OFF"); 00281 00282 ret = RET_OK; 00283 } 00284 else 00285 { 00286 devlog("Turn Echo %s : failed\r\n", onoff ? "ON" : "OFF"); 00287 } 00288 00289 return ret; 00290 } 00291 00292 int8_t getUsimStatus_WM01(void) 00293 { 00294 int8_t ret = RET_NOK; 00295 00296 if(_parser->send("AT$$STAT?") && _parser->recv("$$STAT:READY") && _parser->recv("OK")) 00297 { 00298 devlog("USIM Status : READY\r\n"); 00299 00300 ret = RET_OK; 00301 } 00302 else 00303 { 00304 devlog("Retrieving USIM Status failed\r\n"); 00305 } 00306 00307 return ret; 00308 } 00309 00310 int8_t getNetworkStatus_WM01(void) 00311 { 00312 int8_t ret = RET_NOK; 00313 int val, stat; 00314 00315 if(_parser->send("AT+CEREG?") && _parser->recv("+CEREG: %d,%d", &val, &stat) && _parser->recv("OK")) 00316 { 00317 if((val == 0) && (stat == 1)) 00318 { 00319 devlog("Network Status : attached\r\n"); 00320 00321 ret = RET_OK; 00322 } 00323 else 00324 { 00325 devlog("Network Status : %d, %d\r\n", val, stat); 00326 } 00327 } 00328 else 00329 { 00330 devlog("Network Status : Error\r\n"); 00331 } 00332 00333 return ret; 00334 } 00335 00336 // ---------------------------------------------------------------- 00337 // Functions: Cat.M1 PSM activate / deactivate 00338 // ---------------------------------------------------------------- 00339 00340 int8_t setPsmActivate_WM01(char *Requested_Periodic_TAU, char *Requested_Active_Time) 00341 { 00342 int8_t ret = RET_NOK; 00343 00344 if(_parser->send("AT+CPSMS=1,,,\"%s\",\"%s\"", Requested_Periodic_TAU, Requested_Active_Time) && _parser->recv("OK")) 00345 { 00346 devlog("PSM activate : success\r\n"); 00347 00348 ret = RET_OK; 00349 } 00350 00351 return ret; 00352 } 00353 00354 int8_t setPsmDeactivate_WM01(void) 00355 { 00356 int8_t ret = RET_NOK; 00357 00358 if(_parser->send("AT+CPSMS=0") && _parser->recv("OK")) 00359 { 00360 devlog("PSM deactivate : success\r\n"); 00361 } 00362 00363 return ret; 00364 } 00365 00366 // ---------------------------------------------------------------- 00367 // Functions: Cat.M1 Network time 00368 // ---------------------------------------------------------------- 00369 00370 int8_t getNetworkTimeLocal_WM01(char *time) 00371 { 00372 int8_t ret = RET_NOK; 00373 00374 if(_parser->send("AT$$MSTIME?") && _parser->recv("$$MSTIME:%*[^,],%s\n", time) && _parser->recv("OK")) 00375 { 00376 //devlog("Get current local time success\r\n"); 00377 00378 ret = RET_OK; 00379 } 00380 00381 return ret; 00382 } 00383 00384 void setFlagGettime(void) 00385 { 00386 flag_gettime = true; 00387 } 00388 00389 void clearFlagGettime(void) 00390 { 00391 flag_gettime = false; 00392 }
Generated on Fri Jul 15 2022 03:19:19 by 1.7.2