mbed LoRaWAN stack

Files at this revision

API Documentation at this revision

Comitter:
vsupacha
Date:
Tue Jun 05 08:29:27 2018 +0000
Parent:
1:8ffbba8577cd
Commit message:
Add trace feature

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
trace_helper.cpp Show annotated file Show diff for this revision Revisions of this file
trace_helper.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue May 29 08:44:48 2018 +0000
+++ b/main.cpp	Tue Jun 05 08:29:27 2018 +0000
@@ -4,6 +4,7 @@
 #include "events/EventQueue.h"
 #include "lorawan/LoRaRadio.h"
 #include "SX1276_LoRaRadio.h"
+#include "trace_helper.h"
 
 #define MAX_NUMBER_OF_EVENTS            10
 #define CONFIRMED_MSG_RETRY_COUNTER     3
@@ -39,12 +40,8 @@
 uint8_t rx_buffer[LORAMAC_PHY_MAXPAYLOAD];
 
 lorawan_channelplan_t channel_plan;
-static const channel_params_t AS923_LC3 = { 923600000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 };
-static const channel_params_t AS923_LC4 = { 923800000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 };
-static const channel_params_t AS923_LC5 = { 924000000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 };
-static const channel_params_t AS923_LC6 = { 924200000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 };
-static const channel_params_t AS923_LC7 = { 924400000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 };
-static const channel_params_t AS923_LC8 = { 924600000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 };
+loramac_channel_t channels[8];
+//static const channel_params_t AS923_LC3 = { 923600000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 };
 
 int count = 0;
 InterruptIn mybtn(USER_BUTTON);
@@ -62,6 +59,8 @@
     mybtn.fall(&countBtn);
     lorawan_status_t retcode;
     
+    setup_trace();
+    
     // setup
     if (lorawan.initialize(&ev_queue) == LORAWAN_STATUS_OK) {
         printf("\r\n LoRaWAN stack initialization OK \r\n");
@@ -84,8 +83,18 @@
         printf("\r\n enable_adaptive_datarate failed! \r\n");
         return -1;
     }
+    channel_plan.channels = channels;
+    retcode = lorawan.connect();
     
-    retcode = lorawan.connect();
+    retcode = lorawan.get_channel_plan(channel_plan);
+    if (retcode == LORAWAN_STATUS_OK ) {
+        printf("No. channels: %d\n", channel_plan.nb_channels);
+        for (int i = 0; i < channel_plan.nb_channels; i++) {
+            printf("ID (%d): %d, RX1: %d\n", channel_plan.channels[i].id, channel_plan.channels[i].ch_param.frequency, channel_plan.channels[i].ch_param.rx1_frequency);
+        }
+    } else {
+        printf("retcode: %d\n", retcode);
+    }
 
     if ((retcode == LORAWAN_STATUS_OK) ||
         (retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS)) {
@@ -102,10 +111,22 @@
 
 static void lora_event_handler(lorawan_event_t event)
 {    
+    int retcode;
+    
+    channel_plan.channels = channels;
     switch(event) {
         case CONNECTED:
             printf("\r\n Connected \r\n");
             //sender.attach(&send_message, 30.0);
+            retcode = lorawan.get_channel_plan(channel_plan);
+            if (retcode == LORAWAN_STATUS_OK ) {
+                printf("No. channels: %d\n", channel_plan.nb_channels);
+                for (int i = 0; i < channel_plan.nb_channels; i++) {
+                    printf("ID (%d): %d, RX1: %d\n", channel_plan.channels[i].id, channel_plan.channels[i].ch_param.frequency, channel_plan.channels[i].ch_param.rx1_frequency);
+                }
+            } else {
+                printf("retcode: %d\n", retcode);
+            }
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
                 printf("\r\n Message in queue \r\n");
@@ -114,6 +135,15 @@
         case DISCONNECTED:
         case TX_DONE:
             printf("\r\n Message Sent to Network Server \r\n");
+            retcode = lorawan.get_channel_plan(channel_plan);
+            if (retcode == LORAWAN_STATUS_OK ) {
+                printf("No. channels: %d\n", channel_plan.nb_channels);
+                for (int i = 0; i < channel_plan.nb_channels; i++) {
+                    printf("ID (%d): %d, RX1: %d\n", channel_plan.channels[i].id, channel_plan.channels[i].ch_param.frequency, channel_plan.channels[i].ch_param.rx1_frequency);
+                }
+            } else {
+                printf("retcode: %d\n", retcode);
+            }
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
                 printf("\r\n Message in queue \r\n");
--- a/mbed_app.json	Tue May 29 08:44:48 2018 +0000
+++ b/mbed_app.json	Tue Jun 05 08:29:27 2018 +0000
@@ -4,7 +4,7 @@
             "help": "Which radio to use (options: SX1272,SX1276)",
             "value": "SX1276"
         },
-        "main_stack_size":     { "value": 4096 },
+        "main_stack_size":     { "value": 1024 },
 
         "lorawan-enabled":     { "value": false},
 
@@ -33,10 +33,12 @@
             "platform.stdio-convert-newlines": true,
             "platform.stdio-baud-rate": 115200,
             "platform.default-serial-baud-rate": 115200,
+            "target.features_add": ["COMMON_PAL"],
+            "mbed-trace.enable": true,
             "lora.over-the-air-activation": true,
             "lora.duty-cycle-on": true,
             "lora.phy": 1,
-            "lora.device-eui": "{ 0xAA, 0x00, 0xDB, 0xCA, 0x89, 0xEF, 0x11, 0x55 }",
+            "lora.device-eui": "{ 0xAA, 0x00, 0xDB, 0xCA, 0x89, 0xEF, 0x11, 0x11 }",
             "lora.application-eui": "{ 0xBE, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04 }",
             "lora.application-key": "{ 0x65, 0x33, 0x7F, 0xF1, 0x1E, 0xBE, 0x6D, 0xB0, 0x2B, 0x57, 0x9B, 0x8E, 0x27, 0x26, 0xF8, 0x66 }",
             "lora.appskey": "{ 0x16, 0x28, 0xAE, 0x2B, 0x7E, 0x15, 0xD2, 0xA6, 0xAB, 0xF7, 0xCF, 0x4F, 0x3C, 0x15, 0x88, 0x09 }",
@@ -44,7 +46,7 @@
             "lora.device-address": "0xE77FE552" 
         },
         "DISCO_L072CZ_LRWAN1": {
-            "main_stack_size":      3072,
+            "main_stack_size":      1024,
             "lorawan-enabled":      true,
 
             "lora-radio":          "SX1276",
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trace_helper.cpp	Tue Jun 05 08:29:27 2018 +0000
@@ -0,0 +1,90 @@
+/**
+ * Copyright (c) 2017, Arm Limited and affiliates.
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "drivers/Serial.h"
+
+/**
+ * Serial object for console tracing
+ */
+mbed::Serial pc(USBTX, USBRX, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
+
+/**
+ * If we have tracing library available, we can see traces from within the
+ * stack. The library could be made unavailable by removing FEATURE_COMMON_PAL
+ * from the mbed_app.json to save RAM.
+ */
+#if defined(FEATURE_COMMON_PAL)
+
+    #include "platform/PlatformMutex.h"
+    #include "mbed_trace.h"
+
+    /**
+     * Local mutex object for synchronization
+     */
+    static PlatformMutex mutex;
+
+    static void serial_lock();
+    static void serial_unlock();
+    static void trace_printer(const char* str);
+
+    /**
+     * Sets up trace for the application
+     * Wouldn't do anything if the FEATURE_COMMON_PAL is not added
+     * or if the trace is disabled using mbed_app.json
+     */
+    void setup_trace()
+    {
+        // setting up Mbed trace.
+        mbed_trace_mutex_wait_function_set(serial_lock);
+        mbed_trace_mutex_release_function_set(serial_unlock);
+        mbed_trace_init();
+        mbed_trace_print_function_set(trace_printer);
+    }
+
+    /**
+     * Lock provided for serial printing used by trace library
+     */
+    static void serial_lock()
+    {
+        mutex.lock();
+    }
+
+    /**
+     * Releasing lock provided for serial printing used by trace library
+     */
+    static void serial_unlock()
+    {
+        mutex.unlock();
+    }
+
+    /**
+     * Prints the Mbed trace, used by trace library.
+     * Not intended for local use.
+     */
+    static void trace_printer(const char* str)
+    {
+        pc.printf("%s\r\n", str);
+    }
+
+#else
+
+    void setup_trace()
+    {
+    }
+
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trace_helper.h	Tue Jun 05 08:29:27 2018 +0000
@@ -0,0 +1,28 @@
+/**
+ * Copyright (c) 2017, Arm Limited and affiliates.
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef APP_TRACE_HELPER_H_
+#define APP_TRACE_HELPER_H_
+
+/**
+ * Helper function for the application to setup Mbed trace.
+ * It Wouldn't do anything if the FEATURE_COMMON_PAL is not added
+ * or if the trace is disabled using mbed_app.json
+ */
+void setup_trace();
+
+#endif /* APP_TRACE_HELPER_H_ */
\ No newline at end of file