BG96 Cat.M1 basic sample for WIZnet IoT Shield based on Mbed-OS Cellular APIs (SK telecom network in Korea)

Overview

This code could be access via Cat.M1(BG96 module) of SK telecom network in Korea. Need a WIZnet IoT Shield BG96 board and development board. The code forked Daniel_Lee's mbed-os-example-cellular-BG96 repository and added some features.

/media/uploads/hkjung/wiznetiotshield_bg96_nucleo-l476rg_stacking.png /media/uploads/hkjung/wiznetiotshield_bg96_nucleo-l476rg_separate.png

Tested with

  • NUCLEO_L476RG
  • DISCO_L475VG_IOT01A
  • K64F

Example functionality

This example showcases the following device functionality:

1. Import the application into your desktop:

mbed import https://os.mbed.com/users/hkjung/code/mbed-os-example-cellular-BG96/
 
cd mbed-os-example-cellular-BG96

2. Compile and program:

mbed compile -t <toolchain> -m <TARGET_BOARD>

(supported toolchains : GCC_ARM / ARM / IAR)

3. Download binary to a target board

If need more information such as how to test, please look at https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-cellular/.

Committer:
hkjung
Date:
Mon Apr 29 00:37:57 2019 +0000
Revision:
38:8e4d8ba30f9f
Parent:
36:6294a71c9e9e
Added hardware reset function for WIZnet IoT Shield BG96

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:4611f6cf2413 1 /*
mbed_official 0:4611f6cf2413 2 * Copyright (c) 2017 ARM Limited. All rights reserved.
mbed_official 0:4611f6cf2413 3 * SPDX-License-Identifier: Apache-2.0
mbed_official 0:4611f6cf2413 4 * Licensed under the Apache License, Version 2.0 (the License); you may
mbed_official 0:4611f6cf2413 5 * not use this file except in compliance with the License.
mbed_official 0:4611f6cf2413 6 * You may obtain a copy of the License at
mbed_official 0:4611f6cf2413 7 *
mbed_official 0:4611f6cf2413 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:4611f6cf2413 9 *
mbed_official 0:4611f6cf2413 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:4611f6cf2413 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
mbed_official 0:4611f6cf2413 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:4611f6cf2413 13 * See the License for the specific language governing permissions and
mbed_official 0:4611f6cf2413 14 * limitations under the License.
mbed_official 0:4611f6cf2413 15 */
mbed_official 0:4611f6cf2413 16
mbed_official 0:4611f6cf2413 17 #include "mbed.h"
mbed_official 0:4611f6cf2413 18 #include "common_functions.h"
mbed_official 0:4611f6cf2413 19 #include "UDPSocket.h"
mbed_official 6:5678c0b6f74e 20 #include "CellularLog.h"
mbed_official 0:4611f6cf2413 21
hkjung 38:8e4d8ba30f9f 22 /* Pin configuraiton */
hkjung 38:8e4d8ba30f9f 23 // Cat.M1
hkjung 38:8e4d8ba30f9f 24 #define MBED_CONF_IOTSHIELD_CATM1_RESET D7
hkjung 38:8e4d8ba30f9f 25 #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9
hkjung 38:8e4d8ba30f9f 26 // On-board sensors
hkjung 38:8e4d8ba30f9f 27 #define MBED_CONF_IOTSHIELD_SENSOR_CDS A0
hkjung 38:8e4d8ba30f9f 28 #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1
hkjung 38:8e4d8ba30f9f 29
hkjung 38:8e4d8ba30f9f 30
mbed_official 0:4611f6cf2413 31 #define UDP 0
mbed_official 0:4611f6cf2413 32 #define TCP 1
mbed_official 0:4611f6cf2413 33
mbed_official 0:4611f6cf2413 34 // Number of retries /
mbed_official 0:4611f6cf2413 35 #define RETRY_COUNT 3
mbed_official 0:4611f6cf2413 36
mbed_official 28:232da3ce8a88 37 NetworkInterface *iface;
mbed_official 0:4611f6cf2413 38
mbed_official 0:4611f6cf2413 39 // Echo server hostname
mbed_official 11:23ea0907186e 40 const char *host_name = MBED_CONF_APP_ECHO_SERVER_HOSTNAME;
mbed_official 0:4611f6cf2413 41
mbed_official 0:4611f6cf2413 42 // Echo server port (same for TCP and UDP)
mbed_official 11:23ea0907186e 43 const int port = MBED_CONF_APP_ECHO_SERVER_PORT;
mbed_official 0:4611f6cf2413 44
mbed_official 6:5678c0b6f74e 45 static rtos::Mutex trace_mutex;
mbed_official 6:5678c0b6f74e 46
hkjung 38:8e4d8ba30f9f 47 void iotshield_modem_init(void)
hkjung 38:8e4d8ba30f9f 48 {
hkjung 38:8e4d8ba30f9f 49 DigitalOut _RESET_IOTSHIELD(MBED_CONF_IOTSHIELD_CATM1_RESET);
hkjung 38:8e4d8ba30f9f 50 DigitalOut _PWRKEY_IOTSHIELD(MBED_CONF_IOTSHIELD_CATM1_PWRKEY);
hkjung 38:8e4d8ba30f9f 51
hkjung 38:8e4d8ba30f9f 52 _RESET_IOTSHIELD = 1;
hkjung 38:8e4d8ba30f9f 53 _PWRKEY_IOTSHIELD = 1;
hkjung 38:8e4d8ba30f9f 54 wait_ms(300);
hkjung 38:8e4d8ba30f9f 55
hkjung 38:8e4d8ba30f9f 56 _RESET_IOTSHIELD = 0;
hkjung 38:8e4d8ba30f9f 57 _PWRKEY_IOTSHIELD = 0;
hkjung 38:8e4d8ba30f9f 58 wait_ms(400);
hkjung 38:8e4d8ba30f9f 59
hkjung 38:8e4d8ba30f9f 60 _RESET_IOTSHIELD = 1;
hkjung 38:8e4d8ba30f9f 61 wait_ms(1000);
hkjung 38:8e4d8ba30f9f 62 }
hkjung 38:8e4d8ba30f9f 63
hkjung 38:8e4d8ba30f9f 64
mbed_official 6:5678c0b6f74e 65 #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 66 static void trace_wait()
mbed_official 6:5678c0b6f74e 67 {
mbed_official 6:5678c0b6f74e 68 trace_mutex.lock();
mbed_official 6:5678c0b6f74e 69 }
mbed_official 6:5678c0b6f74e 70
mbed_official 6:5678c0b6f74e 71 static void trace_release()
mbed_official 6:5678c0b6f74e 72 {
mbed_official 6:5678c0b6f74e 73 trace_mutex.unlock();
mbed_official 6:5678c0b6f74e 74 }
mbed_official 6:5678c0b6f74e 75
mbed_official 6:5678c0b6f74e 76 static char time_st[50];
mbed_official 6:5678c0b6f74e 77
mbed_official 6:5678c0b6f74e 78 static char* trace_time(size_t ss)
mbed_official 6:5678c0b6f74e 79 {
mbed_official 6:5678c0b6f74e 80 snprintf(time_st, 49, "[%08llums]", Kernel::get_ms_count());
mbed_official 6:5678c0b6f74e 81 return time_st;
mbed_official 6:5678c0b6f74e 82 }
mbed_official 6:5678c0b6f74e 83
mbed_official 6:5678c0b6f74e 84 static void trace_open()
mbed_official 6:5678c0b6f74e 85 {
mbed_official 6:5678c0b6f74e 86 mbed_trace_init();
mbed_official 6:5678c0b6f74e 87 mbed_trace_prefix_function_set( &trace_time );
mbed_official 6:5678c0b6f74e 88
mbed_official 6:5678c0b6f74e 89 mbed_trace_mutex_wait_function_set(trace_wait);
mbed_official 6:5678c0b6f74e 90 mbed_trace_mutex_release_function_set(trace_release);
mbed_official 11:23ea0907186e 91
mbed_official 11:23ea0907186e 92 mbed_cellular_trace::mutex_wait_function_set(trace_wait);
mbed_official 11:23ea0907186e 93 mbed_cellular_trace::mutex_release_function_set(trace_release);
mbed_official 6:5678c0b6f74e 94 }
mbed_official 6:5678c0b6f74e 95
mbed_official 6:5678c0b6f74e 96 static void trace_close()
mbed_official 6:5678c0b6f74e 97 {
mbed_official 11:23ea0907186e 98 mbed_cellular_trace::mutex_wait_function_set(NULL);
mbed_official 11:23ea0907186e 99 mbed_cellular_trace::mutex_release_function_set(NULL);
mbed_official 11:23ea0907186e 100
mbed_official 6:5678c0b6f74e 101 mbed_trace_free();
mbed_official 6:5678c0b6f74e 102 }
mbed_official 6:5678c0b6f74e 103 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 104
mbed_official 2:0f644d6045cf 105 Thread dot_thread(osPriorityNormal, 512);
mbed_official 0:4611f6cf2413 106
mbed_official 6:5678c0b6f74e 107 void print_function(const char *format, ...)
mbed_official 0:4611f6cf2413 108 {
mbed_official 6:5678c0b6f74e 109 trace_mutex.lock();
mbed_official 6:5678c0b6f74e 110 va_list arglist;
mbed_official 6:5678c0b6f74e 111 va_start( arglist, format );
mbed_official 6:5678c0b6f74e 112 vprintf(format, arglist);
mbed_official 6:5678c0b6f74e 113 va_end( arglist );
mbed_official 6:5678c0b6f74e 114 trace_mutex.unlock();
mbed_official 0:4611f6cf2413 115 }
mbed_official 0:4611f6cf2413 116
mbed_official 0:4611f6cf2413 117 void dot_event()
mbed_official 0:4611f6cf2413 118 {
mbed_official 0:4611f6cf2413 119 while (true) {
mbed_official 26:348eec457e58 120 ThisThread::sleep_for(4000);
mbed_official 28:232da3ce8a88 121 if (iface && iface->get_connection_status() == NSAPI_STATUS_GLOBAL_UP) {
mbed_official 13:4bea5334b419 122 break;
mbed_official 13:4bea5334b419 123 } else {
mbed_official 6:5678c0b6f74e 124 trace_mutex.lock();
mbed_official 6:5678c0b6f74e 125 printf(".");
mbed_official 6:5678c0b6f74e 126 fflush(stdout);
mbed_official 6:5678c0b6f74e 127 trace_mutex.unlock();
mbed_official 0:4611f6cf2413 128 }
mbed_official 0:4611f6cf2413 129 }
mbed_official 0:4611f6cf2413 130 }
mbed_official 0:4611f6cf2413 131
mbed_official 0:4611f6cf2413 132 /**
mbed_official 0:4611f6cf2413 133 * Connects to the Cellular Network
mbed_official 0:4611f6cf2413 134 */
mbed_official 0:4611f6cf2413 135 nsapi_error_t do_connect()
mbed_official 0:4611f6cf2413 136 {
mbed_official 6:5678c0b6f74e 137 nsapi_error_t retcode = NSAPI_ERROR_OK;
mbed_official 0:4611f6cf2413 138 uint8_t retry_counter = 0;
mbed_official 0:4611f6cf2413 139
mbed_official 28:232da3ce8a88 140 while (iface->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) {
mbed_official 11:23ea0907186e 141 retcode = iface->connect();
mbed_official 0:4611f6cf2413 142 if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
mbed_official 0:4611f6cf2413 143 print_function("\n\nAuthentication Failure. Exiting application\n");
mbed_official 6:5678c0b6f74e 144 } else if (retcode == NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 145 print_function("\n\nConnection Established.\n");
mbed_official 6:5678c0b6f74e 146 } else if (retry_counter > RETRY_COUNT) {
mbed_official 6:5678c0b6f74e 147 print_function("\n\nFatal connection failure: %d\n", retcode);
mbed_official 6:5678c0b6f74e 148 } else {
mbed_official 6:5678c0b6f74e 149 print_function("\n\nCouldn't connect: %d, will retry\n", retcode);
mbed_official 0:4611f6cf2413 150 retry_counter++;
mbed_official 0:4611f6cf2413 151 continue;
mbed_official 0:4611f6cf2413 152 }
mbed_official 0:4611f6cf2413 153 break;
mbed_official 0:4611f6cf2413 154 }
mbed_official 6:5678c0b6f74e 155 return retcode;
mbed_official 0:4611f6cf2413 156 }
mbed_official 0:4611f6cf2413 157
mbed_official 0:4611f6cf2413 158 /**
mbed_official 0:4611f6cf2413 159 * Opens a UDP or a TCP socket with the given echo server and performs an echo
mbed_official 0:4611f6cf2413 160 * transaction retrieving current.
mbed_official 0:4611f6cf2413 161 */
mbed_official 0:4611f6cf2413 162 nsapi_error_t test_send_recv()
mbed_official 0:4611f6cf2413 163 {
mbed_official 0:4611f6cf2413 164 nsapi_size_or_error_t retcode;
mbed_official 0:4611f6cf2413 165 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 0:4611f6cf2413 166 TCPSocket sock;
mbed_official 0:4611f6cf2413 167 #else
mbed_official 0:4611f6cf2413 168 UDPSocket sock;
mbed_official 0:4611f6cf2413 169 #endif
mbed_official 0:4611f6cf2413 170
mbed_official 11:23ea0907186e 171 retcode = sock.open(iface);
mbed_official 0:4611f6cf2413 172 if (retcode != NSAPI_ERROR_OK) {
mbed_official 21:e356e039f917 173 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 21:e356e039f917 174 print_function("TCPSocket.open() fails, code: %d\n", retcode);
mbed_official 21:e356e039f917 175 #else
mbed_official 6:5678c0b6f74e 176 print_function("UDPSocket.open() fails, code: %d\n", retcode);
mbed_official 21:e356e039f917 177 #endif
mbed_official 0:4611f6cf2413 178 return -1;
mbed_official 0:4611f6cf2413 179 }
mbed_official 0:4611f6cf2413 180
mbed_official 0:4611f6cf2413 181 SocketAddress sock_addr;
mbed_official 11:23ea0907186e 182 retcode = iface->gethostbyname(host_name, &sock_addr);
mbed_official 0:4611f6cf2413 183 if (retcode != NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 184 print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode);
mbed_official 0:4611f6cf2413 185 return -1;
mbed_official 0:4611f6cf2413 186 }
mbed_official 0:4611f6cf2413 187
mbed_official 0:4611f6cf2413 188 sock_addr.set_port(port);
mbed_official 0:4611f6cf2413 189
mbed_official 0:4611f6cf2413 190 sock.set_timeout(15000);
mbed_official 0:4611f6cf2413 191 int n = 0;
mbed_official 0:4611f6cf2413 192 const char *echo_string = "TEST";
mbed_official 0:4611f6cf2413 193 char recv_buf[4];
mbed_official 0:4611f6cf2413 194 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 0:4611f6cf2413 195 retcode = sock.connect(sock_addr);
mbed_official 0:4611f6cf2413 196 if (retcode < 0) {
mbed_official 6:5678c0b6f74e 197 print_function("TCPSocket.connect() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 198 return -1;
mbed_official 0:4611f6cf2413 199 } else {
mbed_official 6:5678c0b6f74e 200 print_function("TCP: connected with %s server\n", host_name);
mbed_official 0:4611f6cf2413 201 }
mbed_official 0:4611f6cf2413 202 retcode = sock.send((void*) echo_string, sizeof(echo_string));
mbed_official 0:4611f6cf2413 203 if (retcode < 0) {
mbed_official 6:5678c0b6f74e 204 print_function("TCPSocket.send() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 205 return -1;
mbed_official 0:4611f6cf2413 206 } else {
mbed_official 6:5678c0b6f74e 207 print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name);
mbed_official 0:4611f6cf2413 208 }
mbed_official 0:4611f6cf2413 209
mbed_official 0:4611f6cf2413 210 n = sock.recv((void*) recv_buf, sizeof(recv_buf));
mbed_official 0:4611f6cf2413 211 #else
mbed_official 0:4611f6cf2413 212
mbed_official 0:4611f6cf2413 213 retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
mbed_official 0:4611f6cf2413 214 if (retcode < 0) {
mbed_official 6:5678c0b6f74e 215 print_function("UDPSocket.sendto() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 216 return -1;
mbed_official 0:4611f6cf2413 217 } else {
mbed_official 6:5678c0b6f74e 218 print_function("UDP: Sent %d Bytes to %s\n", retcode, host_name);
mbed_official 0:4611f6cf2413 219 }
mbed_official 0:4611f6cf2413 220
mbed_official 0:4611f6cf2413 221 n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
mbed_official 0:4611f6cf2413 222 #endif
mbed_official 0:4611f6cf2413 223
mbed_official 0:4611f6cf2413 224 sock.close();
mbed_official 0:4611f6cf2413 225
mbed_official 0:4611f6cf2413 226 if (n > 0) {
mbed_official 6:5678c0b6f74e 227 print_function("Received from echo server %d Bytes\n", n);
mbed_official 0:4611f6cf2413 228 return 0;
mbed_official 0:4611f6cf2413 229 }
mbed_official 0:4611f6cf2413 230
mbed_official 0:4611f6cf2413 231 return -1;
mbed_official 0:4611f6cf2413 232 }
mbed_official 0:4611f6cf2413 233
mbed_official 0:4611f6cf2413 234 int main()
mbed_official 0:4611f6cf2413 235 {
mbed_official 6:5678c0b6f74e 236 print_function("\n\nmbed-os-example-cellular\n");
mbed_official 34:6f85d44597ac 237 print_function("\n\nBuilt: %s, %s\n", __DATE__, __TIME__);
mbed_official 34:6f85d44597ac 238 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN
mbed_official 36:6294a71c9e9e 239 print_function("\n\n[MAIN], plmn: %s\n", (MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN ? MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN : "NULL"));
mbed_official 34:6f85d44597ac 240 #endif
mbed_official 34:6f85d44597ac 241
mbed_official 6:5678c0b6f74e 242 print_function("Establishing connection\n");
mbed_official 6:5678c0b6f74e 243 #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 244 trace_open();
mbed_official 6:5678c0b6f74e 245 #else
mbed_official 6:5678c0b6f74e 246 dot_thread.start(dot_event);
mbed_official 6:5678c0b6f74e 247 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 27:97054be1a741 248
hkjung 38:8e4d8ba30f9f 249 // Cat.M1 module init: Hardware reset and power on
hkjung 38:8e4d8ba30f9f 250 iotshield_modem_init();
hkjung 38:8e4d8ba30f9f 251
mbed_official 27:97054be1a741 252 // sim pin, apn, credentials and possible plmn are taken atuomtically from json when using get_default_instance()
mbed_official 28:232da3ce8a88 253 iface = NetworkInterface::get_default_instance();
mbed_official 11:23ea0907186e 254 MBED_ASSERT(iface);
mbed_official 11:23ea0907186e 255
mbed_official 6:5678c0b6f74e 256 nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION;
mbed_official 0:4611f6cf2413 257
mbed_official 0:4611f6cf2413 258 /* Attempt to connect to a cellular network */
mbed_official 0:4611f6cf2413 259 if (do_connect() == NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 260 retcode = test_send_recv();
mbed_official 0:4611f6cf2413 261 }
mbed_official 0:4611f6cf2413 262
mbed_official 26:348eec457e58 263 if (iface->disconnect() != NSAPI_ERROR_OK) {
mbed_official 26:348eec457e58 264 print_function("\n\n disconnect failed.\n\n");
mbed_official 26:348eec457e58 265 }
mbed_official 26:348eec457e58 266
mbed_official 6:5678c0b6f74e 267 if (retcode == NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 268 print_function("\n\nSuccess. Exiting \n\n");
mbed_official 6:5678c0b6f74e 269 } else {
mbed_official 6:5678c0b6f74e 270 print_function("\n\nFailure. Exiting \n\n");
mbed_official 6:5678c0b6f74e 271 }
mbed_official 26:348eec457e58 272
mbed_official 6:5678c0b6f74e 273 #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 274 trace_close();
mbed_official 26:348eec457e58 275 #else
mbed_official 26:348eec457e58 276 dot_thread.terminate();
mbed_official 6:5678c0b6f74e 277 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 278
mbed_official 6:5678c0b6f74e 279 return 0;
mbed_official 0:4611f6cf2413 280 }
mbed_official 0:4611f6cf2413 281 // EOF