NuMaker mbed OS v5.x LoRaWAN

Files at this revision

API Documentation at this revision

Comitter:
cyliang
Date:
Tue Sep 01 21:06:51 2020 +0800
Commit message:
Mbed OS v5.x LoRaWAN example for NuMaker platforms based on https://github.com/ARMmbed/mbed-os-example-lorawan.git

Changed in this revision

CONTRIBUTING.md Show annotated file Show diff for this revision Revisions of this file
DummySensor.h Show annotated file Show diff for this revision Revisions of this file
Jenkinsfile Show annotated file Show diff for this revision Revisions of this file
LICENSE Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
config/SX126X_example_config.json Show annotated file Show diff for this revision Revisions of this file
config/SX127X_example_config.json Show annotated file Show diff for this revision Revisions of this file
lora_radio_helper.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-lora-radio-drv.lib Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib 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
mbedtls_lora_config.h 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
diff -r 000000000000 -r 895bbd4e6398 CONTRIBUTING.md
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CONTRIBUTING.md	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,5 @@
+# Contributing to Mbed OS
+
+Mbed OS is an open-source, device software platform for the Internet of Things. Contributions are an important part of the platform, and our goal is to make it as simple as possible to become a contributor.
+
+To encourage productive collaboration, as well as robust, consistent and maintainable code, we have a set of guidelines for [contributing to Mbed OS](https://os.mbed.com/docs/mbed-os/latest/contributing/index.html).
diff -r 000000000000 -r 895bbd4e6398 DummySensor.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DummySensor.h	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,47 @@
+/**
+ * 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 MBED_LORAWAN_DUMMYSENSOR_H_
+#define MBED_LORAWAN_DUMMYSENSOR_H_
+
+/*
+ * A dummy sensor for Mbed LoRa Test Application
+ */
+class DS1820 {
+public:
+    DS1820(uint32_t)
+    {
+        value = 1;
+    };
+    bool begin()
+    {
+        return true;
+    };
+    void startConversion() {};
+    int32_t read()
+    {
+        value += 2;
+        return value;
+    }
+
+private:
+    int32_t value;
+};
+
+
+
+#endif /* MBED_LORAWAN_DUMMYSENSOR_H_ */
diff -r 000000000000 -r 895bbd4e6398 Jenkinsfile
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Jenkinsfile	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,188 @@
+properties ([[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
+  [$class: 'StringParameterDefinition', name: 'mbed_os_revision', defaultValue: '', description: 'Revision of mbed-os to build. To access mbed-os PR use format "pull/PR number/head"'],
+  [$class: 'BooleanParameterDefinition', name: 'regions_build_test', defaultValue: true, description: 'Test build all available regions']
+  ]]])
+
+library 'mbed-lib'
+
+if (env.MBED_OS_REVISION == null) {
+  echo 'First run in this branch, using default parameter values'
+  env.MBED_OS_REVISION = ''
+}
+if (env.MBED_OS_REVISION == '') {
+  echo 'Using mbed OS revision from mbed-os.lib'
+} else {
+  echo "Using given mbed OS revision: ${env.MBED_OS_REVISION}"
+  if (env.MBED_OS_REVISION.matches('pull/\\d+/head')) {
+    echo "Revision is a Pull Request"
+  }
+}
+
+// All available regions
+def regions = [
+  "\"0\"", "\"1\"", "\"2\"", "\"3\"", "\"4\"", "\"5\"", "\"6\"", "\"7\"", "\"8\"",
+  "\"EU868\"", "\"AS923\"", "\"AU915\"", "\"CN470\"", "\"CN779\"", "\"EU433\"",
+  "\"IN865\"", "\"KR920\"", "\"US915\""
+]
+
+// Supported targets
+def targets = [
+  "K64F",
+  "MTB_MTS_XDOT",
+  "MTB_MURATA_ABZ",
+  "MTS_MDOT_F411RE",
+  "DISCO_L072CZ_LRWAN1",
+  "MTB_ADV_WISE_1510",
+  "MTB_RAK811"
+]
+
+// Supported toolchains
+def toolchains = [
+  "ARM",
+  "GCC_ARM",
+  "IAR"
+]
+
+def stepsForParallel = [:]
+
+// Jenkins pipeline does not support map.each, we need to use oldschool for loop
+for (int i = 0; i < targets.size(); i++) {
+  for(int j = 0; j < toolchains.size(); j++) {
+      def target = targets.get(i)
+      def toolchain = toolchains.get(j)
+
+      // Skip unwanted combination
+      if (target == "MTB_MURATA_ABZ" && toolchain == "GCC_ARM") {
+        continue
+      }
+      if (target == "DISCO_L072CZ_LRWAN1" && toolchain == "GCC_ARM") {
+        continue
+      }
+      if (target == "MTB_RAK811" && toolchain == "GCC_ARM") {
+        continue
+      }
+
+      def stepName = "${target} ${toolchain}"
+
+      stepsForParallel[stepName] = buildStep(target, toolchain)
+  }
+}
+
+def stepsForRegional = [:]
+
+if (params.regions_build_test == true) {
+  stepsForRegional["REGION BUILDER"] = build_regions(regions)
+}
+
+timestamps {
+  parallel stepsForParallel
+  parallel stepsForRegional
+}
+
+def buildStep(target, toolchain) {
+  return {
+    stage ("${target}_${toolchain}") {
+      node ("all-in-one-build-slave") {
+        deleteDir()
+        dir("mbed-os-example-lorawan") {
+          checkout scm
+
+          // A workaround for mbed-cli caching issues
+          try {
+            execute("mbed deploy --protocol ssh")
+          } catch (err) {
+              echo "mbed deploy failed - retrying after 10s"
+              sleep(10)
+              execute("mbed deploy --protocol ssh")
+          }
+
+          // Set mbed-os to revision received as parameter
+          if (env.MBED_OS_REVISION != '') {
+            dir("mbed-os") {
+              if (env.MBED_OS_REVISION.matches('pull/\\d+/head')) {
+                // Use mbed-os PR and switch to branch created
+                execute("git fetch origin ${env.MBED_OS_REVISION}:_PR_")
+                execute("git checkout _PR_")
+              } else {
+                execute("git checkout ${env.MBED_OS_REVISION}")
+              }
+            }
+          }
+
+          // Adjust stack size and crystal values
+          if ("${target}" == "DISCO_L072CZ_LRWAN1") {
+            execute("sed -i 's/#define RCC_HSICALIBRATION_DEFAULT       ((uint32_t)0x10)/#define RCC_HSICALIBRATION_DEFAULT       ((uint32_t)0x13)/' \
+                    mbed-os/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_rcc.h")
+          }
+
+          if ("${target}" == "MTB_MURATA_ABZ") {
+            execute("sed -i 's/define symbol __size_heap__   = 0x800;/define symbol __size_heap__   = 0x1000;/' \
+                    mbed-os/targets/TARGET_STM/TARGET_STM32L0/TARGET_STM32L0x2xZ/device/TOOLCHAIN_IAR/stm32l082xZ.icf")
+            execute("sed -i 's/#define RCC_HSICALIBRATION_DEFAULT       ((uint32_t)0x10)/#define RCC_HSICALIBRATION_DEFAULT       ((uint32_t)0x16)/' \
+                    mbed-os/targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_rcc.h")
+          }
+
+          if ("${target}" == "MTB_MTS_XDOT") {
+            execute("sed -i 's/define symbol __size_heap__   = 0x800;/define symbol __size_heap__   = 0x1800;/' \
+                    mbed-os/targets/TARGET_STM/TARGET_STM32L1/TARGET_MTB_MTS_XDOT/device/TOOLCHAIN_IAR/stm32l152xc.icf")
+          }
+
+          if ("${target}" == "MTB_RAK811") {
+            execute("sed -i 's/#define RCC_HSICALIBRATION_DEFAULT       (0x10U)/#define RCC_HSICALIBRATION_DEFAULT       (0x11U)/' \
+                    mbed-os/targets/TARGET_STM/TARGET_STM32L1/device/stm32l1xx_hal_rcc.h")
+            execute("sed -i 's/define symbol __size_heap__   = 0x800;/define symbol __size_heap__   = 0x1800;/' \
+                    mbed-os/targets/TARGET_STM/TARGET_STM32L1/TARGET_MTB_RAK811/device/TOOLCHAIN_IAR/stm32l152xba.icf")
+          }
+
+          execute("mbed compile --build out/${target}_${toolchain}/ -m ${target} -t ${toolchain} -c")
+        }
+        stash name: "${target}_${toolchain}", includes: '**/mbed-os-example-lorawan.bin'
+        archive '**/mbed-os-example-lorawan.bin'
+        step([$class: 'WsCleanup'])
+      }
+    }
+  }
+}
+
+def build_regions(regions) {
+  return {
+    stage ("region_builder_K64F_GCC_ARM") {
+      node ("all-in-one-build-slave") {
+        deleteDir()
+        dir("mbed-os-example-lorawan") {
+          checkout scm
+
+          // A workaround for mbed-cli caching issues
+          try {
+            execute("mbed deploy --protocol ssh")
+          } catch (err) {
+              echo "mbed deploy failed - retrying after 10s"
+              sleep(10)
+              execute("mbed deploy --protocol ssh")
+          }
+
+          if (env.MBED_OS_REVISION != '') {
+            dir("mbed-os") {
+              if (env.MBED_OS_REVISION.matches('pull/\\d+/head')) {
+                execute("git fetch origin ${env.MBED_OS_REVISION}:_PR_")
+                execute("git checkout _PR_")
+              } else {
+                execute("git checkout ${env.MBED_OS_REVISION}")
+              }
+            }
+          }
+          //Initial sed to string format for find & replacing
+          execute("sed -i 's/\"lora.phy\": 0,/\"lora.phy\": \"0\",/' mbed_app.json")
+          //lora.phy 0 build tested above already
+          for (int i = 1; i < regions.size(); i++) {
+            def curr_region = regions.get(i)
+            def prev_region = regions.get(i-1)
+            execute("sed -i 's/\"lora.phy\": ${prev_region},/\"lora.phy\": ${curr_region},/' mbed_app.json")
+            echo "Building region: ${curr_region}"
+            execute("mbed compile -t GCC_ARM -m K64F")
+          }
+        }
+      }
+    }
+  }
+}
diff -r 000000000000 -r 895bbd4e6398 LICENSE
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LICENSE	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,165 @@
+Apache License
+Version 2.0, January 2004
+http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+"License" shall mean the terms and conditions for use, reproduction, and
+distribution as defined by Sections 1 through 9 of this document.
+
+"Licensor" shall mean the copyright owner or entity authorized by the copyright
+owner that is granting the License.
+
+"Legal Entity" shall mean the union of the acting entity and all other entities
+that control, are controlled by, or are under common control with that entity.
+For the purposes of this definition, "control" means (i) the power, direct or
+indirect, to cause the direction or management of such entity, whether by
+contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
+outstanding shares, or (iii) beneficial ownership of such entity.
+
+"You" (or "Your") shall mean an individual or Legal Entity exercising
+permissions granted by this License.
+
+"Source" form shall mean the preferred form for making modifications, including
+but not limited to software source code, documentation source, and configuration
+files.
+
+"Object" form shall mean any form resulting from mechanical transformation or
+translation of a Source form, including but not limited to compiled object code,
+generated documentation, and conversions to other media types.
+
+"Work" shall mean the work of authorship, whether in Source or Object form, made
+available under the License, as indicated by a copyright notice that is included
+in or attached to the work (an example is provided in the Appendix below).
+
+"Derivative Works" shall mean any work, whether in Source or Object form, that
+is based on (or derived from) the Work and for which the editorial revisions,
+annotations, elaborations, or other modifications represent, as a whole, an
+original work of authorship. For the purposes of this License, Derivative Works
+shall not include works that remain separable from, or merely link (or bind by
+name) to the interfaces of, the Work and Derivative Works thereof.
+
+"Contribution" shall mean any work of authorship, including the original version
+of the Work and any modifications or additions to that Work or Derivative Works
+thereof, that is intentionally submitted to Licensor for inclusion in the Work
+by the copyright owner or by an individual or Legal Entity authorized to submit
+on behalf of the copyright owner. For the purposes of this definition,
+"submitted" means any form of electronic, verbal, or written communication sent
+to the Licensor or its representatives, including but not limited to
+communication on electronic mailing lists, source code control systems, and
+issue tracking systems that are managed by, or on behalf of, the Licensor for
+the purpose of discussing and improving the Work, but excluding communication
+that is conspicuously marked or otherwise designated in writing by the copyright
+owner as "Not a Contribution."
+
+"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
+of whom a Contribution has been received by Licensor and subsequently
+incorporated within the Work.
+
+2. Grant of Copyright License.
+
+Subject to the terms and conditions of this License, each Contributor hereby
+grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
+irrevocable copyright license to reproduce, prepare Derivative Works of,
+publicly display, publicly perform, sublicense, and distribute the Work and such
+Derivative Works in Source or Object form.
+
+3. Grant of Patent License.
+
+Subject to the terms and conditions of this License, each Contributor hereby
+grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
+irrevocable (except as stated in this section) patent license to make, have
+made, use, offer to sell, sell, import, and otherwise transfer the Work, where
+such license applies only to those patent claims licensable by such Contributor
+that are necessarily infringed by their Contribution(s) alone or by combination
+of their Contribution(s) with the Work to which such Contribution(s) was
+submitted. If You institute patent litigation against any entity (including a
+cross-claim or counterclaim in a lawsuit) alleging that the Work or a
+Contribution incorporated within the Work constitutes direct or contributory
+patent infringement, then any patent licenses granted to You under this License
+for that Work shall terminate as of the date such litigation is filed.
+
+4. Redistribution.
+
+You may reproduce and distribute copies of the Work or Derivative Works thereof
+in any medium, with or without modifications, and in Source or Object form,
+provided that You meet the following conditions:
+
+You must give any other recipients of the Work or Derivative Works a copy of
+this License; and
+You must cause any modified files to carry prominent notices stating that You
+changed the files; and
+You must retain, in the Source form of any Derivative Works that You distribute,
+all copyright, patent, trademark, and attribution notices from the Source form
+of the Work, excluding those notices that do not pertain to any part of the
+Derivative Works; and
+If the Work includes a "NOTICE" text file as part of its distribution, then any
+Derivative Works that You distribute must include a readable copy of the
+attribution notices contained within such NOTICE file, excluding those notices
+that do not pertain to any part of the Derivative Works, in at least one of the
+following places: within a NOTICE text file distributed as part of the
+Derivative Works; within the Source form or documentation, if provided along
+with the Derivative Works; or, within a display generated by the Derivative
+Works, if and wherever such third-party notices normally appear. The contents of
+the NOTICE file are for informational purposes only and do not modify the
+License. You may add Your own attribution notices within Derivative Works that
+You distribute, alongside or as an addendum to the NOTICE text from the Work,
+provided that such additional attribution notices cannot be construed as
+modifying the License.
+You may add Your own copyright statement to Your modifications and may provide
+additional or different license terms and conditions for use, reproduction, or
+distribution of Your modifications, or for any such Derivative Works as a whole,
+provided Your use, reproduction, and distribution of the Work otherwise complies
+with the conditions stated in this License.
+
+5. Submission of Contributions.
+
+Unless You explicitly state otherwise, any Contribution intentionally submitted
+for inclusion in the Work by You to the Licensor shall be under the terms and
+conditions of this License, without any additional terms or conditions.
+Notwithstanding the above, nothing herein shall supersede or modify the terms of
+any separate license agreement you may have executed with Licensor regarding
+such Contributions.
+
+6. Trademarks.
+
+This License does not grant permission to use the trade names, trademarks,
+service marks, or product names of the Licensor, except as required for
+reasonable and customary use in describing the origin of the Work and
+reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty.
+
+Unless required by applicable law or agreed to in writing, Licensor provides the
+Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
+including, without limitation, any warranties or conditions of TITLE,
+NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
+solely responsible for determining the appropriateness of using or
+redistributing the Work and assume any risks associated with Your exercise of
+permissions under this License.
+
+8. Limitation of Liability.
+
+In no event and under no legal theory, whether in tort (including negligence),
+contract, or otherwise, unless required by applicable law (such as deliberate
+and grossly negligent acts) or agreed to in writing, shall any Contributor be
+liable to You for damages, including any direct, indirect, special, incidental,
+or consequential damages of any character arising as a result of this License or
+out of the use or inability to use the Work (including but not limited to
+damages for loss of goodwill, work stoppage, computer failure or malfunction, or
+any and all other commercial damages or losses), even if such Contributor has
+been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability.
+
+While redistributing the Work or Derivative Works thereof, You may choose to
+offer, and charge a fee for, acceptance of support, warranty, indemnity, or
+other liability obligations and/or rights consistent with this License. However,
+in accepting such obligations, You may act only on Your own behalf and on Your
+sole responsibility, not on behalf of any other Contributor, and only if You
+agree to indemnify, defend, and hold each Contributor harmless for any liability
+incurred by, or claims asserted against, such Contributor by reason of your
+accepting any such warranty or additional liability.
diff -r 000000000000 -r 895bbd4e6398 README.md
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,193 @@
+# Example LoRaWAN application for Mbed-OS
+
+This is an example application based on `Mbed-OS` LoRaWAN protocol APIs. The Mbed-OS LoRaWAN stack implementation is compliant with LoRaWAN v1.0.2 specification.  See this [link](https://os.mbed.com/blog/entry/Introducing-LoRaWAN-11-support/) for information on support for other LoRaWAN spec versions. This application can work with any Network Server if you have correct credentials for the said Network Server. 
+
+## Getting Started
+
+### Supported Hardware
+[Mbed Enabled board with an Arduino form factor](https://os.mbed.com/platforms/?q=&Form+Factor=Arduino+Compatible) and one of the following:
+- [SX126X shield](https://os.mbed.com/components/SX126xMB2xAS/)
+- [SX1276 shield](https://os.mbed.com/components/SX1276MB1xAS/)
+- [SX1272 shield](https://os.mbed.com/components/SX1272MB2xAS/) 
+
+OR
+
+[Mbed Enabled LoRa Module](#module-support)
+
+### Import the example application
+For [Mbed Online Compiler](https://ide.mbed.com/compiler/) users:
+- Select "Import", then search for "mbed-os-example-lorawan" from "Team mbed-os-examples".  Or simply, import this repo by URL.
+
+- NOTE: Do NOT select "Update all libraries to latest revision" as this may cause breakage with a new lib version we have not tested.   
+
+For [mbed-cli](https://github.com/ARMmbed/mbed-cli) users:
+```sh
+$ mbed import mbed-os-example-lorawan
+$ cd mbed-os-example-lorawan
+
+#OR
+
+$ git clone git@github.com:ARMmbed/mbed-os-example-lorawan.git
+$ cd mbed-os-example-lorawan
+$ mbed deploy
+```
+
+### Example configuration and radio selection
+
+Because of the pin differences between the SX126x and SX127x radios, example application configuration files are provided with the correct pin sets in the `config/` dir of this project. 
+
+Please start by selecting the correct example configuration for your radio:  
+- For [Mbed Online Compiler](https://ide.mbed.com/compiler/) users, this can be done by simply replacing the contents of the `mbed_app.json` at the root of the project with the content of the correct example configuration in `config/` dir.
+- For [mbed-cli](https://github.com/ARMmbed/mbed-cli) users, the config file can be specifed on the command line with the `--app-config` option (ie `--app-config config/SX12xx_example_config.json`)
+
+With the correct config file selected, the user can then provide a pin set for their target board in the `NC` fields at the top if it is different from the default targets listed.  If your device is one of the LoRa modules supported by Mbed-OS, the pin set is already provided for the modules in the `target-overrides` field of the config file. For more information on supported modules, please refer to the [module support section](#module-support)
+
+### Add network credentials
+
+Open the file `mbed_app.json` in the root directory of your application. This file contains all the user specific configurations your application and the Mbed OS LoRaWAN stack need. Network credentials are typically provided by LoRa network provider.
+
+#### For OTAA
+
+Please add `Device EUI`, `Application EUI` and `Application Key` needed for Over-the-air-activation(OTAA). For example:
+
+```json
+"lora.device-eui": "{ YOUR_DEVICE_EUI }",
+"lora.application-eui": "{ YOUR_APPLICATION_EUI }",
+"lora.application-key": "{ YOUR_APPLICATION_KEY }"
+```
+
+#### For ABP
+
+For Activation-By-Personalization (ABP) connection method, modify the `mbed_app.json` to enable ABP. You can do it by simply turning off OTAA. For example:
+
+```json
+"lora.over-the-air-activation": false,
+```
+
+In addition to that, you need to provide `Application Session Key`, `Network Session Key` and `Device Address`. For example:
+
+```json
+"lora.appskey": "{ YOUR_APPLICATION_SESSION_KEY }",
+"lora.nwkskey": "{ YOUR_NETWORK_SESSION_KEY }",
+"lora.device-address": " YOUR_DEVICE_ADDRESS_IN_HEX  " 
+```
+
+## Configuring the application
+
+The Mbed OS LoRaWAN stack provides a lot of configuration controls to the application through the Mbed OS configuration system. The previous section discusses some of these controls. This section highlights some useful features that you can configure.
+
+### Selecting a PHY
+
+The LoRaWAN protocol is subject to various country specific regulations concerning radio emissions. That's why the Mbed OS LoRaWAN stack provides a `LoRaPHY` class that you can use to implement any region specific PHY layer. Currently, the Mbed OS LoRaWAN stack provides 10 different country specific implementations of `LoRaPHY` class. Selection of a specific PHY layer happens at compile time. By default, the Mbed OS LoRaWAN stack uses `EU 868 MHz` PHY. An example of selecting a PHY can be:
+
+```josn
+        "phy": {
+            "help": "LoRa PHY region. 0 = EU868 (default), 1 = AS923, 2 = AU915, 3 = CN470, 4 = CN779, 5 = EU433, 6 = IN865, 7 = KR920, 8 = US915, 9 = US915_HYBRID",
+            "value": "0"
+        },
+```
+
+### Duty cycling
+
+LoRaWAN v1.0.2 specifcation is exclusively duty cycle based. This application comes with duty cycle enabled by default. In other words, the Mbed OS LoRaWAN stack enforces duty cycle. The stack keeps track of transmissions on the channels in use and schedules transmissions on channels that become available in the shortest time possible. We recommend you keep duty cycle on for compliance with your country specific regulations. 
+
+However, you can define a timer value in the application, which you can use to perform a periodic uplink when the duty cycle is turned off. Such a setup should be used only for testing or with a large enough timer value. For example:
+
+```josn 
+"target_overrides": {
+	"*": {
+		"lora.duty-cycle-on": false
+		},
+	}
+}
+```
+
+## Module support
+
+Here is a nonexhaustive list of boards and modules that we have tested with the Mbed OS LoRaWAN stack:
+
+- MultiTech mDot (SX1272)
+- MultiTech xDot (SX1272)
+- LTEK_FF1705 (SX1272)
+- Advantech Wise 1510 (SX1276)
+- ST B-L072Z-LRWAN1 LoRa®Discovery kit with Murata CMWX1ZZABZ-091 module (SX1276)
+
+Here is a list of boards and modules that have been tested by the community:
+
+- IMST iM880B (SX1272)
+- Embedded Planet Agora (SX1276)
+
+## Compiling the application
+
+Use Mbed CLI commands to generate a binary for the application.
+For example:
+
+```sh
+$ mbed compile -m YOUR_TARGET -t ARM
+```
+
+## Running the application
+
+Drag and drop the application binary from `BUILD/YOUR_TARGET/ARM/mbed-os-example-lora.bin` to your Mbed enabled target hardware, which appears as a USB device on your host machine. 
+
+Attach a serial console emulator of your choice (for example, PuTTY, Minicom or screen) to your USB device. Set the baudrate to 115200 bit/s, and reset your board by pressing the reset button.
+
+You should see an output similar to this:
+
+```
+Mbed LoRaWANStack initialized 
+
+ CONFIRMED message retries : 3 
+
+ Adaptive data  rate (ADR) - Enabled 
+
+ Connection - In Progress ...
+
+ Connection - Successful 
+
+ Dummy Sensor Value = 2.1 
+
+ 25 bytes scheduled for transmission 
+ 
+ Message Sent to Network Server
+
+```
+
+## [Optional] Adding trace library
+To enable Mbed trace, add to your `mbed_app.json` the following fields:
+
+```json
+    "target_overrides": {
+        "*": {
+            "mbed-trace.enable": true
+            }
+     }
+```
+The trace is disabled by default to save RAM and reduce main stack usage (see chapter Memory optimization).
+
+**Please note that some targets with small RAM size (e.g. DISCO_L072CZ_LRWAN1 and MTB_MURATA_ABZ) mbed traces cannot be enabled without increasing the default** `"main_stack_size": 1024`**.**
+
+## [Optional] Memory optimization 
+
+Using `Arm CC compiler` instead of `GCC` reduces `3K` of RAM. Currently the application takes about `15K` of static RAM with Arm CC, which spills over for the platforms with `20K` of RAM because you need to leave space, about `5K`, for dynamic allocation. So if you reduce the application stack size, you can barely fit into the 20K platforms.
+
+For example, add the following into `config` section in your `mbed_app.json`:
+
+```
+"main_stack_size": {
+    "value": 2048
+}
+```
+
+Essentially you can make the whole application with Mbed LoRaWAN stack in 6K if you drop the RTOS from Mbed OS and use a smaller standard C/C++ library like new-lib-nano. Please find instructions [here](https://os.mbed.com/blog/entry/Reducing-memory-usage-with-a-custom-prin/).
+ 
+
+For more information, please follow this [blog post](https://os.mbed.com/blog/entry/Reducing-memory-usage-by-tuning-RTOS-con/).
+
+
+### License and contributions
+
+The software is provided under Apache-2.0 license. Contributions to this project are accepted under the same license. Please see [contributing.md](CONTRIBUTING.md) for more info.
+
+This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide.
+
diff -r 000000000000 -r 895bbd4e6398 config/SX126X_example_config.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config/SX126X_example_config.json	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,65 @@
+{
+    "config": {
+        "lora-radio": {
+            "help": "Which radio to use (options: SX126X, SX1272, SX1276) -- See config/ dir for example configs",
+            "value": "SX126X"
+        },
+        "main_stack_size":     { "value": 4096 },
+
+        "lora-spi-mosi":       { "value": "NC" },
+        "lora-spi-miso":       { "value": "NC" },
+        "lora-spi-sclk":       { "value": "NC" },
+        "lora-cs":             { "value": "NC" },
+        "lora-reset":          { "value": "NC" },
+        "lora-dio1":           { "value": "NC" },
+        "lora-busy":           { "value": "NC" },
+        "lora-freq-sel":       { "value": "NC" },
+        "lora-dev-sel":        { "value": "NC" },
+        "lora-xtal-sel":       { "value": "NC" }, 
+        "lora-ant-switch":     { "value": "NC" }
+    
+    },
+    "target_overrides": {
+        "*": {
+            "platform.stdio-convert-newlines": true,
+            "platform.stdio-baud-rate": 115200,
+            "platform.default-serial-baud-rate": 115200,
+            "lora.over-the-air-activation": true,
+            "lora.duty-cycle-on": true,
+            "lora.phy": "EU868",
+            "lora.device-eui": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }",
+            "lora.application-eui": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }",
+            "lora.application-key": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }"
+        },
+
+        "NUCLEO_L073RZ": {
+            "main_stack_size":     2048,
+            "lora-spi-mosi":       "D11",
+            "lora-spi-miso":       "D12",
+            "lora-spi-sclk":       "D13",
+            "lora-cs":             "D7",
+            "lora-reset":          "A0",
+            "lora-dio1":           "D5",
+            "lora-busy":           "D3",
+            "lora-freq-sel":       "A1",
+            "lora-dev-sel":        "A2",
+            "lora-xtal-sel":       "A3",
+            "lora-ant-switch":     "D8"
+        },
+
+        "K64F": {
+            "lora-spi-mosi":       "D11",
+            "lora-spi-miso":       "D12",
+            "lora-spi-sclk":       "D13",
+            "lora-cs":             "D7",
+            "lora-reset":          "A0",
+            "lora-dio1":           "D5",
+            "lora-busy":           "D3",
+            "lora-freq-sel":       "A1",
+            "lora-dev-sel":        "A2",
+            "lora-xtal-sel":       "A3",
+            "lora-ant-switch":     "D8"
+        }
+    },
+    "macros": ["MBEDTLS_USER_CONFIG_FILE=\"mbedtls_lora_config.h\""]
+}
diff -r 000000000000 -r 895bbd4e6398 config/SX127X_example_config.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config/SX127X_example_config.json	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,264 @@
+{
+    "config": {
+        "lora-radio": {
+            "help": "Which radio to use (options: SX126X, SX1272, SX1276) -- See config/ dir for example configs",
+            "value": "SX1276"
+    },
+    "main_stack_size":     { "value": 4096 },
+
+        "lora-spi-mosi":       { "value": "NC" },
+        "lora-spi-miso":       { "value": "NC" },
+        "lora-spi-sclk":       { "value": "NC" },
+        "lora-cs":             { "value": "NC" },
+        "lora-reset":          { "value": "NC" },
+        "lora-dio0":           { "value": "NC" },
+        "lora-dio1":           { "value": "NC" },
+        "lora-dio2":           { "value": "NC" },
+        "lora-dio3":           { "value": "NC" },
+        "lora-dio4":           { "value": "NC" },
+        "lora-dio5":           { "value": "NC" },
+        "lora-rf-switch-ctl1": { "value": "NC" },
+        "lora-rf-switch-ctl2": { "value": "NC" },
+        "lora-txctl":          { "value": "NC" },
+        "lora-rxctl":          { "value": "NC" },
+        "lora-ant-switch":     { "value": "NC" },
+        "lora-pwr-amp-ctl":    { "value": "NC" },
+        "lora-tcxo":           { "value": "NC" }
+    },
+    "target_overrides": {
+        "*": {
+            "platform.stdio-convert-newlines": true,
+            "platform.stdio-baud-rate": 115200,
+            "platform.default-serial-baud-rate": 115200,
+            "lora.over-the-air-activation": true,
+            "lora.duty-cycle-on": true,
+            "lora.phy": "EU868",
+            "lora.device-eui": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }",
+            "lora.application-eui": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }",
+            "lora.application-key": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }"
+        },
+
+        "K64F": {
+            "lora-spi-mosi":       "D11",
+            "lora-spi-miso":       "D12",
+            "lora-spi-sclk":       "D13",
+            "lora-cs":             "D10",
+            "lora-reset":          "A0",
+            "lora-dio0":           "D2",
+            "lora-dio1":           "D3",
+            "lora-dio2":           "D4",
+            "lora-dio3":           "D5",
+            "lora-dio4":           "D8",
+            "lora-dio5":           "D9",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "NC",
+            "lora-rxctl":          "NC",
+            "lora-ant-switch":     "A4",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "NC"
+        },
+
+        "DISCO_L072CZ_LRWAN1": {
+            "main_stack_size":      1024,
+            "lora-radio":          "SX1276",
+            "lora-spi-mosi":       "PA_7",
+            "lora-spi-miso":       "PA_6",
+            "lora-spi-sclk":       "PB_3",
+            "lora-cs":             "PA_15",
+            "lora-reset":          "PC_0",
+            "lora-dio0":           "PB_4",
+            "lora-dio1":           "PB_1",
+            "lora-dio2":           "PB_0",
+            "lora-dio3":           "PC_13",
+            "lora-dio4":           "NC",
+            "lora-dio5":           "NC",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "PC_2",
+            "lora-rxctl":          "PA_1",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "PC_1",
+            "lora-tcxo":           "PA_12"
+        },
+
+        "MTB_MURATA_ABZ": {
+            "main_stack_size":      1024,
+            "lora-radio":          "SX1276",
+            "lora-spi-mosi":       "PA_7",
+            "lora-spi-miso":       "PA_6",
+            "lora-spi-sclk":       "PB_3",
+            "lora-cs":             "PA_15",
+            "lora-reset":          "PC_0",
+            "lora-dio0":           "PB_4",
+            "lora-dio1":           "PB_1",
+            "lora-dio2":           "PB_0",
+            "lora-dio3":           "PC_13",
+            "lora-dio4":           "NC",
+            "lora-dio5":           "NC",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "PC_2",
+            "lora-rxctl":          "PA_1",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "PC_1",
+            "lora-tcxo":           "PA_12"
+        },
+
+        "XDOT_L151CC": {
+            "lora-radio":           "SX1272",
+            "lora-spi-mosi":        "LORA_MOSI",
+            "lora-spi-miso":        "LORA_MISO",
+            "lora-spi-sclk":        "LORA_SCK",
+            "lora-cs":              "LORA_NSS",
+            "lora-reset":           "LORA_RESET",
+            "lora-dio0":            "LORA_DIO0",
+            "lora-dio1":            "LORA_DIO1",
+            "lora-dio2":            "LORA_DIO2",
+            "lora-dio3":            "LORA_DIO3",
+            "lora-dio4":            "LORA_DIO4",
+            "lora-dio5":            "NC",
+            "lora-rf-switch-ctl1":  "NC",
+            "lora-rf-switch-ctl2":  "NC",
+            "lora-txctl":           "NC",
+            "lora-rxctl":           "NC",
+            "lora-ant-switch":      "NC",
+            "lora-pwr-amp-ctl":     "NC",
+            "lora-tcxo":            "NC"
+        },
+
+        "MTB_MTS_XDOT": {
+            "lora-radio":           "SX1272",
+            "lora-spi-mosi":        "LORA_MOSI",
+            "lora-spi-miso":        "LORA_MISO",
+            "lora-spi-sclk":        "LORA_SCK",
+            "lora-cs":              "LORA_NSS",
+            "lora-reset":           "LORA_RESET",
+            "lora-dio0":            "LORA_DIO0",
+            "lora-dio1":            "LORA_DIO1",
+            "lora-dio2":            "LORA_DIO2",
+            "lora-dio3":            "LORA_DIO3",
+            "lora-dio4":            "LORA_DIO4",
+            "lora-dio5":            "NC",
+            "lora-rf-switch-ctl1":  "NC",
+            "lora-rf-switch-ctl2":  "NC",
+            "lora-txctl":           "NC",
+            "lora-rxctl":           "NC",
+            "lora-ant-switch":      "NC",
+            "lora-pwr-amp-ctl":     "NC",
+            "lora-tcxo":            "NC"
+        },
+
+        "FF1705_L151CC": {
+            "lora-radio":           "SX1272",
+            "lora-spi-mosi":        "LORA_MOSI",
+            "lora-spi-miso":        "LORA_MISO",
+            "lora-spi-sclk":        "LORA_SCK",
+            "lora-cs":              "LORA_NSS",
+            "lora-reset":           "LORA_RESET",
+            "lora-dio0":            "LORA_DIO0",
+            "lora-dio1":            "LORA_DIO1",
+            "lora-dio2":            "LORA_DIO2",
+            "lora-dio3":            "LORA_DIO3",
+            "lora-dio4":            "LORA_DIO4",
+            "lora-dio5":            "NC",
+            "lora-rf-switch-ctl1":  "NC",
+            "lora-rf-switch-ctl2":  "NC",
+            "lora-txctl":           "NC",
+            "lora-rxctl":           "NC",
+            "lora-ant-switch":      "NC",
+            "lora-pwr-amp-ctl":     "NC",
+            "lora-tcxo":            "NC"
+        },
+
+        "MTS_MDOT_F411RE": {
+            "lora-radio":           "SX1272",
+            "lora-spi-mosi":        "LORA_MOSI",
+            "lora-spi-miso":        "LORA_MISO",
+            "lora-spi-sclk":        "LORA_SCK",
+            "lora-cs":              "LORA_NSS",
+            "lora-reset":           "LORA_RESET",
+            "lora-dio0":            "LORA_DIO0",
+            "lora-dio1":            "LORA_DIO1",
+            "lora-dio2":            "LORA_DIO2",
+            "lora-dio3":            "LORA_DIO3",
+            "lora-dio4":            "LORA_DIO4",
+            "lora-dio5":            "LORA_DIO5",
+            "lora-rf-switch-ctl1":  "NC",
+            "lora-rf-switch-ctl2":  "NC",
+            "lora-txctl":           "LORA_TXCTL",
+            "lora-rxctl":           "LORA_RXCTL",
+            "lora-ant-switch":      "NC",
+            "lora-pwr-amp-ctl":     "NC",
+            "lora-tcxo":            "NC"
+        },
+
+        "MTB_ADV_WISE_1510": {
+            "lora-radio":           "SX1276",
+            "lora-spi-mosi":        "SPI_RF_MOSI",
+            "lora-spi-miso":        "SPI_RF_MISO",
+            "lora-spi-sclk":        "SPI_RF_SCK",
+            "lora-cs":              "SPI_RF_CS",
+            "lora-reset":           "SPI_RF_RESET",
+            "lora-dio0":            "DIO0",
+            "lora-dio1":            "DIO1",
+            "lora-dio2":            "DIO2",
+            "lora-dio3":            "DIO3",
+            "lora-dio4":            "DIO4",
+            "lora-dio5":            "DIO5",
+            "lora-rf-switch-ctl1":  "NC",
+            "lora-rf-switch-ctl2":  "NC",
+            "lora-txctl":           "NC",
+            "lora-rxctl":           "NC",
+            "lora-ant-switch":      "ANT_SWITCH",
+            "lora-pwr-amp-ctl":     "NC",
+            "lora-tcxo":            "NC"
+        },
+
+        "MTB_RAK811": {
+            "lora-radio":          "SX1276",
+            "lora-spi-mosi":       "SPI_RF_MOSI",
+            "lora-spi-miso":       "SPI_RF_MISO",
+            "lora-spi-sclk":       "SPI_RF_SCK",
+            "lora-cs":             "SPI_RF_CS",
+            "lora-reset":          "SPI_RF_RESET",
+            "lora-dio0":           "DIO0",
+            "lora-dio1":           "DIO1",
+            "lora-dio2":           "DIO2",
+            "lora-dio3":           "DIO3",
+            "lora-dio4":           "DIO4",
+            "lora-dio5":           "NC",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "ANT_CTX_PA",
+            "lora-rxctl":          "ANT_CRX_RX",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "RF_TCXO_EN"
+        },
+
+        "IM880B": {
+            "main_stack_size":      1024,
+            "lora-radio":          "SX1272",
+            "lora-spi-mosi":       "SPI_RF_MOSI",
+            "lora-spi-miso":       "SPI_RF_MISO",
+            "lora-spi-sclk":       "SPI_RF_SCK",
+            "lora-cs":             "SPI_RF_NSS",
+            "lora-reset":          "SPI_RF_RESET",
+            "lora-dio0":           "DIO0",
+            "lora-dio1":           "DIO1",
+            "lora-dio2":           "DIO2",
+            "lora-dio3":           "DIO3",
+            "lora-dio4":           "DIO4",
+            "lora-dio5":           "NC",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "ANT_CTX_PA",
+            "lora-rxctl":          "ANT_CRX_RX",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "NC"
+        }
+    },
+    "macros": ["MBEDTLS_USER_CONFIG_FILE=\"mbedtls_lora_config.h\""]
+}
\ No newline at end of file
diff -r 000000000000 -r 895bbd4e6398 lora_radio_helper.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lora_radio_helper.h	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,87 @@
+/**
+ * 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_LORA_RADIO_HELPER_H_
+#define APP_LORA_RADIO_HELPER_H_
+
+#include "lorawan/LoRaRadio.h"
+
+#define SX1272   0xFF
+#define SX1276   0xEE
+#define SX126X   0xDD
+
+#if (MBED_CONF_APP_LORA_RADIO == SX1272)
+#include "SX1272_LoRaRadio.h"
+SX1272_LoRaRadio radio(MBED_CONF_APP_LORA_SPI_MOSI,
+                       MBED_CONF_APP_LORA_SPI_MISO,
+                       MBED_CONF_APP_LORA_SPI_SCLK,
+                       MBED_CONF_APP_LORA_CS,
+                       MBED_CONF_APP_LORA_RESET,
+                       MBED_CONF_APP_LORA_DIO0,
+                       MBED_CONF_APP_LORA_DIO1,
+                       MBED_CONF_APP_LORA_DIO2,
+                       MBED_CONF_APP_LORA_DIO3,
+                       MBED_CONF_APP_LORA_DIO4,
+                       MBED_CONF_APP_LORA_DIO5,
+                       MBED_CONF_APP_LORA_RF_SWITCH_CTL1,
+                       MBED_CONF_APP_LORA_RF_SWITCH_CTL2,
+                       MBED_CONF_APP_LORA_TXCTL,
+                       MBED_CONF_APP_LORA_RXCTL,
+                       MBED_CONF_APP_LORA_ANT_SWITCH,
+                       MBED_CONF_APP_LORA_PWR_AMP_CTL,
+                       MBED_CONF_APP_LORA_TCXO);
+
+#elif (MBED_CONF_APP_LORA_RADIO == SX1276)
+#include "SX1276_LoRaRadio.h"
+SX1276_LoRaRadio radio(MBED_CONF_APP_LORA_SPI_MOSI,
+                       MBED_CONF_APP_LORA_SPI_MISO,
+                       MBED_CONF_APP_LORA_SPI_SCLK,
+                       MBED_CONF_APP_LORA_CS,
+                       MBED_CONF_APP_LORA_RESET,
+                       MBED_CONF_APP_LORA_DIO0,
+                       MBED_CONF_APP_LORA_DIO1,
+                       MBED_CONF_APP_LORA_DIO2,
+                       MBED_CONF_APP_LORA_DIO3,
+                       MBED_CONF_APP_LORA_DIO4,
+                       MBED_CONF_APP_LORA_DIO5,
+                       MBED_CONF_APP_LORA_RF_SWITCH_CTL1,
+                       MBED_CONF_APP_LORA_RF_SWITCH_CTL2,
+                       MBED_CONF_APP_LORA_TXCTL,
+                       MBED_CONF_APP_LORA_RXCTL,
+                       MBED_CONF_APP_LORA_ANT_SWITCH,
+                       MBED_CONF_APP_LORA_PWR_AMP_CTL,
+                       MBED_CONF_APP_LORA_TCXO);
+
+#elif (MBED_CONF_APP_LORA_RADIO == SX126X)
+#include "SX126X_LoRaRadio.h"
+SX126X_LoRaRadio radio(MBED_CONF_APP_LORA_SPI_MOSI,
+                       MBED_CONF_APP_LORA_SPI_MISO,
+                       MBED_CONF_APP_LORA_SPI_SCLK,
+                       MBED_CONF_APP_LORA_CS,
+                       MBED_CONF_APP_LORA_RESET,
+                       MBED_CONF_APP_LORA_DIO1,
+                       MBED_CONF_APP_LORA_BUSY,
+                       MBED_CONF_APP_LORA_FREQ_SEL,
+                       MBED_CONF_APP_LORA_DEV_SEL,
+                       MBED_CONF_APP_LORA_XTAL_SEL,
+                       MBED_CONF_APP_LORA_ANT_SWITCH);
+
+#else
+#error "Unknown LoRa radio specified (SX126X, SX1272, SX1276 are valid)"
+#endif
+
+#endif /* APP_LORA_RADIO_HELPER_H_ */
diff -r 000000000000 -r 895bbd4e6398 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,273 @@
+/**
+ * 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 <stdio.h>
+
+#include "lorawan/LoRaWANInterface.h"
+#include "lorawan/system/lorawan_data_structures.h"
+#include "events/EventQueue.h"
+
+// Application helpers
+#include "DummySensor.h"
+#include "trace_helper.h"
+#include "lora_radio_helper.h"
+
+using namespace events;
+
+// Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
+// This example only communicates with much shorter messages (<30 bytes).
+// If longer messages are used, these buffers must be changed accordingly.
+uint8_t tx_buffer[30];
+uint8_t rx_buffer[30];
+
+/*
+ * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
+ */
+#define TX_TIMER                        10000
+
+/**
+ * Maximum number of events for the event queue.
+ * 10 is the safe number for the stack events, however, if application
+ * also uses the queue for whatever purposes, this number should be increased.
+ */
+#define MAX_NUMBER_OF_EVENTS            10
+
+/**
+ * Maximum number of retries for CONFIRMED messages before giving up
+ */
+#define CONFIRMED_MSG_RETRY_COUNTER     3
+
+/**
+ * Dummy pin for dummy sensor
+ */
+#define PC_9                            0
+
+/**
+ * Dummy sensor class object
+ */
+DS1820  ds1820(PC_9);
+
+/**
+* This event queue is the global event queue for both the
+* application and stack. To conserve memory, the stack is designed to run
+* in the same thread as the application and the application is responsible for
+* providing an event queue to the stack that will be used for ISR deferment as
+* well as application information event queuing.
+*/
+static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);
+
+/**
+ * Event handler.
+ *
+ * This will be passed to the LoRaWAN stack to queue events for the
+ * application which in turn drive the application.
+ */
+static void lora_event_handler(lorawan_event_t event);
+
+/**
+ * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper.
+ */
+static LoRaWANInterface lorawan(radio);
+
+/**
+ * Application specific callbacks
+ */
+static lorawan_app_callbacks_t callbacks;
+
+/**
+ * Entry point for application
+ */
+int main(void)
+{
+#ifdef MBED_MAJOR_VERSION
+    printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
+#endif
+    // setup tracing
+    setup_trace();
+
+    // stores the status of a call to LoRaWAN protocol
+    lorawan_status_t retcode;
+
+    // Initialize LoRaWAN stack
+    if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
+        printf("\r\n LoRa initialization failed! \r\n");
+        return -1;
+    }
+
+    printf("\r\n Mbed LoRaWANStack initialized \r\n");
+
+    // prepare application callbacks
+    callbacks.events = mbed::callback(lora_event_handler);
+    lorawan.add_app_callbacks(&callbacks);
+
+    // Set number of retries in case of CONFIRMED messages
+    if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
+            != LORAWAN_STATUS_OK) {
+        printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
+        return -1;
+    }
+
+    printf("\r\n CONFIRMED message retries : %d \r\n",
+           CONFIRMED_MSG_RETRY_COUNTER);
+
+    // Enable adaptive data rate
+    if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
+        printf("\r\n enable_adaptive_datarate failed! \r\n");
+        return -1;
+    }
+
+    printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
+
+    retcode = lorawan.connect();
+
+    if (retcode == LORAWAN_STATUS_OK ||
+            retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
+    } else {
+        printf("\r\n Connection error, code = %d \r\n", retcode);
+        return -1;
+    }
+
+    printf("\r\n Connection - In Progress ...\r\n");
+
+    // make your event queue dispatching events forever
+    ev_queue.dispatch_forever();
+
+    return 0;
+}
+
+/**
+ * Sends a message to the Network Server
+ */
+static void send_message()
+{
+    uint16_t packet_len;
+    int16_t retcode;
+    int32_t sensor_value;
+
+    if (ds1820.begin()) {
+        ds1820.startConversion();
+        sensor_value = ds1820.read();
+        printf("\r\n Dummy Sensor Value = %d \r\n", sensor_value);
+        ds1820.startConversion();
+    } else {
+        printf("\r\n No sensor found \r\n");
+        return;
+    }
+
+    packet_len = sprintf((char *) tx_buffer, "Val: %d",
+                         sensor_value);
+
+    retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
+                           MSG_UNCONFIRMED_FLAG);
+
+    if (retcode < 0) {
+        retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
+        : printf("\r\n send() - Error code %d \r\n", retcode);
+
+        if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
+            //retry in 3 seconds
+            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
+                ev_queue.call_in(3000, send_message);
+            }
+        }
+        return;
+    }
+
+    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
+    memset(tx_buffer, 0, sizeof(tx_buffer));
+}
+
+/**
+ * Receive a message from the Network Server
+ */
+static void receive_message()
+{
+    uint8_t port;
+    int flags;
+    int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags);
+
+    if (retcode < 0) {
+        printf("\r\n receive() - Error code %d \r\n", retcode);
+        return;
+    }
+
+    printf(" RX Data on port %u (%d bytes): ", port, retcode);
+    for (uint8_t i = 0; i < retcode; i++) {
+        printf("%02x ", rx_buffer[i]);
+    }
+    printf("\r\n");
+    
+    memset(rx_buffer, 0, sizeof(rx_buffer));
+}
+
+/**
+ * Event handler
+ */
+static void lora_event_handler(lorawan_event_t event)
+{
+    switch (event) {
+        case CONNECTED:
+            printf("\r\n Connection - Successful \r\n");
+            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
+                send_message();
+            } else {
+                ev_queue.call_every(TX_TIMER, send_message);
+            }
+
+            break;
+        case DISCONNECTED:
+            ev_queue.break_dispatch();
+            printf("\r\n Disconnected Successfully \r\n");
+            break;
+        case TX_DONE:
+            printf("\r\n Message Sent to Network Server \r\n");
+            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
+                send_message();
+            }
+            break;
+        case TX_TIMEOUT:
+        case TX_ERROR:
+        case TX_CRYPTO_ERROR:
+        case TX_SCHEDULING_ERROR:
+            printf("\r\n Transmission Error - EventCode = %d \r\n", event);
+            // try again
+            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
+                send_message();
+            }
+            break;
+        case RX_DONE:
+            printf("\r\n Received message from Network Server \r\n");
+            receive_message();
+            break;
+        case RX_TIMEOUT:
+        case RX_ERROR:
+            printf("\r\n Error in reception - Code = %d \r\n", event);
+            break;
+        case JOIN_FAILURE:
+            printf("\r\n OTAA Failed - Check Keys \r\n");
+            break;
+        case UPLINK_REQUIRED:
+            printf("\r\n Uplink required by NS \r\n");
+            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
+                send_message();
+            }
+            break;
+        default:
+            MBED_ASSERT("Unknown Event");
+    }
+}
+
+// EOF
diff -r 000000000000 -r 895bbd4e6398 mbed-lora-radio-drv.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-lora-radio-drv.lib	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-semtech-lora-rf-drivers#6012fa43cf9f2cae46fa9d424fe4051d00e157a2
diff -r 000000000000 -r 895bbd4e6398 mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#6a244d7adffc0e93872cfc880e539ee11bbc6002
diff -r 000000000000 -r 895bbd4e6398 mbed_app.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,150 @@
+{
+    "config": {
+        "lora-radio": {
+            "help": "Which radio to use (options: SX126X, SX1272, SX1276) -- See config/ dir for example configs",
+            "value": "SX1276"
+        },
+        "main_stack_size":     { "value": 2048 },
+
+        "lora-spi-mosi":       { "value": "NC" },
+        "lora-spi-miso":       { "value": "NC" },
+        "lora-spi-sclk":       { "value": "NC" },
+        "lora-cs":             { "value": "NC" },
+        "lora-reset":          { "value": "NC" },
+        "lora-dio0":           { "value": "NC" },
+        "lora-dio1":           { "value": "NC" },
+        "lora-dio2":           { "value": "NC" },
+        "lora-dio3":           { "value": "NC" },
+        "lora-dio4":           { "value": "NC" },
+        "lora-dio5":           { "value": "NC" },
+        "lora-rf-switch-ctl1": { "value": "NC" },
+        "lora-rf-switch-ctl2": { "value": "NC" },
+        "lora-txctl":          { "value": "NC" },
+        "lora-rxctl":          { "value": "NC" },
+        "lora-ant-switch":     { "value": "NC" },
+        "lora-pwr-amp-ctl":    { "value": "NC" },
+        "lora-tcxo":           { "value": "NC" },
+        "trace-level": {
+            "help": "Options are TRACE_LEVEL_ERROR,TRACE_LEVEL_WARN,TRACE_LEVEL_INFO,TRACE_LEVEL_DEBUG",
+            "macro_name": "MBED_TRACE_MAX_LEVEL",
+            "value": "TRACE_LEVEL_INFO"
+        }
+    },
+    "target_overrides": {
+        "*": {
+            "platform.stdio-convert-newlines": true,
+            "platform.stdio-baud-rate": 115200,
+            "platform.default-serial-baud-rate": 115200,
+            "lora.over-the-air-activation": false,
+            "lora.duty-cycle-on": true,
+            "target.components_add": ["SX1272", "SX1276", "SX126X"],
+            "lora.phy": "US915",
+            "lora.device-eui": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }",
+            "lora.application-eui": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }",
+            "lora.application-key": "{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }",
+            "lora.device-address": "0x00000000"
+        },
+        "NUMAKER_PFM_NANO130": {
+            "lora-spi-mosi":       "D11",
+            "lora-spi-miso":       "D12",
+            "lora-spi-sclk":       "D13",
+            "lora-cs":             "D10",
+            "lora-reset":          "A0",
+            "lora-dio0":           "D2",
+            "lora-dio1":           "D3",
+            "lora-dio2":           "D4",
+            "lora-dio3":           "D5",
+            "lora-dio4":           "D8",
+            "lora-dio5":           "D9",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "NC",
+            "lora-rxctl":          "NC",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "NC"
+        },
+        "NUMAKER_IOT_M252": {
+            "lora-spi-mosi":       "PD_0",
+            "lora-spi-miso":       "PD_1",
+            "lora-spi-sclk":       "PD_2",
+            "lora-cs":             "PD_3",
+            "lora-reset":          "PF_6",
+            "lora-dio0":           "PC_6",
+            "lora-dio1":           "PC_7",
+            "lora-dio2":           "PB_8",
+            "lora-dio3":           "PB_9",
+            "lora-dio4":           "PB_10",
+            "lora-dio5":           "PB_11",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "NC",
+            "lora-rxctl":          "NC",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "NC"
+        },
+        "NUMAKER_IOT_M263A": {
+            "lora-spi-mosi":       "D11",
+            "lora-spi-miso":       "D12",
+            "lora-spi-sclk":       "D13",
+            "lora-cs":             "D10",
+            "lora-reset":          "A0",
+            "lora-dio0":           "D2",
+            "lora-dio1":           "D3",
+            "lora-dio2":           "D4",
+            "lora-dio3":           "D5",
+            "lora-dio4":           "D8",
+            "lora-dio5":           "D9",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "NC",
+            "lora-rxctl":          "NC",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "NC"
+        },        
+        "NUMAKER_IOT_M487": {
+            "lora-spi-mosi":       "D11",
+            "lora-spi-miso":       "D12",
+            "lora-spi-sclk":       "D13",
+            "lora-cs":             "D10",
+            "lora-reset":          "A0",
+            "lora-dio0":           "D2",
+            "lora-dio1":           "D3",
+            "lora-dio2":           "D4",
+            "lora-dio3":           "D5",
+            "lora-dio4":           "D8",
+            "lora-dio5":           "D9",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "NC",
+            "lora-rxctl":          "NC",
+            "lora-ant-switch":     "NC",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "NC"
+        },
+        "K64F": {
+            "lora-spi-mosi":       "D11",
+            "lora-spi-miso":       "D12",
+            "lora-spi-sclk":       "D13",
+            "lora-cs":             "D10",
+            "lora-reset":          "A0",
+            "lora-dio0":           "D2",
+            "lora-dio1":           "D3",
+            "lora-dio2":           "D4",
+            "lora-dio3":           "D5",
+            "lora-dio4":           "D8",
+            "lora-dio5":           "D9",
+            "lora-rf-switch-ctl1": "NC",
+            "lora-rf-switch-ctl2": "NC",
+            "lora-txctl":          "NC",
+            "lora-rxctl":          "NC",
+            "lora-ant-switch":     "A4",
+            "lora-pwr-amp-ctl":    "NC",
+            "lora-tcxo":           "NC"
+        }
+    },
+    "macros": ["MBEDTLS_USER_CONFIG_FILE=\"mbedtls_lora_config.h\""]
+}
+
diff -r 000000000000 -r 895bbd4e6398 mbedtls_lora_config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbedtls_lora_config.h	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,43 @@
+/**
+ * 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 MBEDTLS_LORA_CONFIG_H
+#define MBEDTLS_LORA_CONFIG_H
+
+/*
+ * Configure mbedtls for LoRa stack.
+ *
+ * These settings are just a customization of the main mbedtls config
+ * mbed-os/features/mbedtls/inc/mbedtls/config.h
+ */
+
+// Ensure LoRa required ciphers are enabled
+#define MBEDTLS_CIPHER_C
+#define MBEDTLS_AES_C
+#define MBEDTLS_CMAC_C
+
+// Reduce ROM usage by optimizing some mbedtls features.
+// These are only reference configurations for this LoRa example application.
+// Other LoRa applications might need different configurations.
+#define MBEDTLS_AES_FEWER_TABLES
+
+#undef MBEDTLS_GCM_C
+#undef MBEDTLS_CHACHA20_C
+#undef MBEDTLS_CHACHAPOLY_C
+#undef MBEDTLS_POLY1305_C
+
+#endif /* MBEDTLS_LORA_CONFIG_H */
diff -r 000000000000 -r 895bbd4e6398 trace_helper.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trace_helper.cpp	Tue Sep 01 21:06:51 2020 +0800
@@ -0,0 +1,72 @@
+/**
+ * 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.
+ */
+
+/**
+ * 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.
+ */
+
+#include "mbed_trace.h"
+
+#ifdef FEA_TRACE_SUPPORT
+#include "platform/PlatformMutex.h"
+
+/**
+ * Local mutex object for synchronization
+ */
+static PlatformMutex mutex;
+
+static void serial_lock();
+static void serial_unlock();
+
+/**
+ * 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();
+}
+
+/**
+ * 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();
+}
+#else
+void setup_trace()
+{
+
+}
+#endif
+
+
diff -r 000000000000 -r 895bbd4e6398 trace_helper.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trace_helper.h	Tue Sep 01 21:06:51 2020 +0800
@@ -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_ */