Fork of the official LoRaWAN example adapted for Future Electronic's Sequana board with SX1272MB2DAS LoRa shield.

Files at this revision

API Documentation at this revision

Comitter:
lru
Date:
Wed Sep 11 16:08:09 2019 +0000
Parent:
57:36e87c44c920
Commit message:
Initial demo version for Sequana board.

Changed in this revision

bd2808-driver.lib 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bd2808-driver.lib	Wed Sep 11 16:08:09 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/lrusinowicz/bd2808-driver/#62bfd96f856504d53e714b69f701136a75622371
--- a/main.cpp	Tue Sep 03 14:01:54 2019 +0100
+++ b/main.cpp	Wed Sep 11 16:08:09 2019 +0000
@@ -19,13 +19,19 @@
 #include "lorawan/LoRaWANInterface.h"
 #include "lorawan/system/lorawan_data_structures.h"
 #include "events/EventQueue.h"
+#include "psoc6_utils.h"
+#include "mbed.h"
 
 // Application helpers
 #include "DummySensor.h"
 #include "trace_helper.h"
 #include "lora_radio_helper.h"
 
+#include "BD2808.h"
+
 using namespace events;
+using namespace mbed;
+
 
 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
 // This example only communicates with much shorter messages (<30 bytes).
@@ -87,17 +93,47 @@
  */
 static lorawan_app_callbacks_t callbacks;
 
+
+Timer g_timer;
+DigitalOut led(LED1);
+BD2808 leds;
+
+static uint32_t uid = 20;
+
+bool frame_send = false;
+
 /**
  * Entry point for application
  */
 int main(void)
 {
+    static uint8_t mac_address[6];
+    static uint8_t device_eui[8] = {0x00, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x00};
+    static const uint8_t application_eui[] = MBED_CONF_LORA_APPLICATION_EUI;
+    static const uint8_t application_key[] = MBED_CONF_LORA_APPLICATION_KEY;
+    static lorawan_connect_t connect_params = {
+        .connect_type = LORAWAN_CONNECTION_OTAA,
+    };
+
     // setup tracing
     setup_trace();
 
     // stores the status of a call to LoRaWAN protocol
     lorawan_status_t retcode;
 
+    // Create unique, hardware-dependent EUI.
+    cy_get_bd_mac_address(mac_address);
+    // MAC address is in reverse sequence.
+    for (int i = 0; i < 3; ++i) {
+        device_eui[i] = mac_address[5 - i];
+        device_eui[i + 5] = mac_address[2 - i];
+    }
+    printf("\r\nDevice EUI is %02X", device_eui[0]);
+    for (int i = 1; i < 8; ++i) {
+        printf(":%02X", device_eui[i]);
+    }
+    printf("\r\n");
+
     // Initialize LoRaWAN stack
     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
         printf("\r\n LoRa initialization failed! \r\n");
@@ -128,7 +164,22 @@
 
     printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
 
-    retcode = lorawan.connect();
+    // Specify class C
+    if (lorawan.set_device_class(CLASS_C) != LORAWAN_STATUS_OK) {
+        printf("\r\n setting class C failed! \r\n");
+        return -1;
+    }
+
+    printf("\r\n Class C - Enabled \r\n");
+
+    connect_params.connection_u.otaa.dev_eui = device_eui;
+    connect_params.connection_u.otaa.app_eui = const_cast<uint8_t *>(application_eui);
+    connect_params.connection_u.otaa.app_key = const_cast<uint8_t *>(application_key);
+    connect_params.connection_u.otaa.nb_trials = MBED_CONF_LORA_NB_TRIALS;
+
+    g_timer.start();
+
+    retcode = lorawan.connect(connect_params);
 
     if (retcode == LORAWAN_STATUS_OK ||
             retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
@@ -148,12 +199,14 @@
 /**
  * Sends a message to the Network Server
  */
-static void send_message()
+static void send_message(int entry)
 {
     uint16_t packet_len;
     int16_t retcode;
     int32_t sensor_value;
 
+    printf("\r\n[%8u] Sending message (%d) \r\n",  g_timer.read_ms(), entry);
+
     if (ds1820.begin()) {
         ds1820.startConversion();
         sensor_value = ds1820.read();
@@ -167,6 +220,7 @@
     packet_len = sprintf((char *) tx_buffer, "Dummy Sensor Value is %d",
                          sensor_value);
 
+    led = 1;
     retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
                            MSG_UNCONFIRMED_FLAG);
 
@@ -175,15 +229,18 @@
         : printf("\r\n send() - Error code %d \r\n", retcode);
 
         if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
+            lorawan.cancel_sending();
+            led = 0;
             //retry in 3 seconds
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                ev_queue.call_in(3000, send_message);
+                ev_queue.call_in(3000, send_message, 3);
             }
         }
         return;
     }
 
-    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
+    frame_send = true;
+    printf("\r\n[%8u] %d bytes scheduled for transmission \r\n", g_timer.read_ms(), retcode);
     memset(tx_buffer, 0, sizeof(tx_buffer));
 }
 
@@ -201,12 +258,24 @@
         return;
     }
 
-    printf(" RX Data on port %u (%d bytes): ", port, retcode);
+    printf("[%8u] RX Data on port %u (%d bytes): ", g_timer.read_ms(), port, retcode);
     for (uint8_t i = 0; i < retcode; i++) {
         printf("%02x ", rx_buffer[i]);
     }
     printf("\r\n");
-    
+
+    if (rx_buffer[0] != 0) {
+        for (int i = 0; i < 8; ++i) {
+            leds.set_color(i, BGR24_color_t(200,200,200));
+        }
+        leds.refresh();
+    } else {
+        for (int i = 0; i < 8; ++i) {
+            leds.set_color(i, BGR24_color_t(0,0,0));
+        }
+        leds.refresh();
+    }
+
     memset(rx_buffer, 0, sizeof(rx_buffer));
 }
 
@@ -217,49 +286,62 @@
 {
     switch (event) {
         case CONNECTED:
-            printf("\r\n Connection - Successful \r\n");
+            printf("\r\n[%8u] Connection - Successful \r\n", g_timer.read_ms());
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                send_message();
+                send_message(0);
             } else {
-                ev_queue.call_every(TX_TIMER, send_message);
+                printf("\r\n Scheduling new transmission every %u ms\r\n", TX_TIMER);
+                ev_queue.call_every(TX_TIMER, send_message, 1);
             }
 
             break;
         case DISCONNECTED:
+            led = 0;
+            frame_send = false;
             ev_queue.break_dispatch();
-            printf("\r\n Disconnected Successfully \r\n");
+            printf("\r\n[%8u] Disconnected Successfully \r\n", g_timer.read_ms());
             break;
         case TX_DONE:
-            printf("\r\n Message Sent to Network Server \r\n");
-            if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                send_message();
+            led = 0;
+            if (frame_send) {
+                frame_send = false;
+                printf("\r\n[%8u] Message Sent to Network Server \r\n", g_timer.read_ms());
+                if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
+                    printf("           Scheduling message %u\r\n", uid);
+                    //send_message();
+                    ev_queue.call_in(10000, send_message, uid++);
+                }
+            } else {
+                printf("\r\n[%8u] Duplicate TX_DONE !!! \r\n", g_timer.read_ms());
             }
             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);
+            led = 0;
+            frame_send = false;
+            printf("\r\n[%8u] Transmission Error - EventCode = %d \r\n", g_timer.read_ms(), event);
             // try again
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                send_message();
+                send_message(2);
             }
             break;
         case RX_DONE:
-            printf("\r\n Received message from Network Server \r\n");
+            printf("\r\n[%8u] Received message from Network Server \r\n", g_timer.read_ms());
             receive_message();
             break;
         case RX_TIMEOUT:
         case RX_ERROR:
-            printf("\r\n Error in reception - Code = %d \r\n", event);
+            printf("\r\n[%8u] Error in reception - Code = %d \r\n", g_timer.read_ms(), 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");
+            printf("\r\n[%8u] Uplink required by NS \r\n", g_timer.read_ms());
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
-                send_message();
+                send_message(4);
             }
             break;
         default:
--- a/mbed-lora-radio-drv.lib	Tue Sep 03 14:01:54 2019 +0100
+++ b/mbed-lora-radio-drv.lib	Wed Sep 11 16:08:09 2019 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-semtech-lora-rf-drivers#6012fa43cf9f2cae46fa9d424fe4051d00e157a2
+https://github.com/ARMmbed/mbed-semtech-lora-rf-drivers/#6012fa43cf9f2cae46fa9d424fe4051d00e157a2
--- a/mbed-os.lib	Tue Sep 03 14:01:54 2019 +0100
+++ b/mbed-os.lib	Wed Sep 11 16:08:09 2019 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#0063e5de32fc575f061244c96ac60c41c07bd2e6
+https://github.com/lrusinowicz/mbed-os.git/#729eb7df4d993ea31c13bf3c6e8c187f36a36266
--- a/mbed_app.json	Tue Sep 03 14:01:54 2019 +0100
+++ b/mbed_app.json	Wed Sep 11 16:08:09 2019 +0000
@@ -1,7 +1,7 @@
 {
     "config": {
         "lora-radio": {
-            "help": "Which radio to use (options: SX126X, SX1272, SX1276) -- See config/ dir for example configs",
+            "help": "Which radio to use (options: SX1272,SX1276)",
             "value": "SX1276"
         },
         "main_stack_size":     { "value": 4096 },
@@ -32,10 +32,11 @@
             "platform.default-serial-baud-rate": 115200,
             "lora.over-the-air-activation": true,
             "lora.duty-cycle-on": true,
+            "lora.automatic-uplink-message": 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 }"
+            "lora.application-key": "{ 0x47, 0x20, 0x68, 0xea, 0xf7, 0xd4, 0xa4, 0x3e, 0xbf, 0x13, 0xc9, 0xb2, 0xdf, 0x98, 0x91, 0x61 }"
         },
 
         "K64F": {
@@ -258,8 +259,29 @@
             "lora-ant-switch":     "NC",
             "lora-pwr-amp-ctl":    "NC",
             "lora-tcxo":           "NC"
+        },
+
+        "FUTURE_SEQUANA": {
+            "lora-radio":          "SX1272",
+            "lora-spi-mosi":       "SPI_MOSI",
+            "lora-spi-miso":       "SPI_MISO",
+            "lora-spi-sclk":       "SPI_CLK",
+            "lora-cs":             "D10",
+            "lora-reset":          "A0",
+            "lora-dio0":           "D2",
+            "lora-dio1":           "D3",
+            "lora-dio2":           "D4",
+            "lora-dio3":           "D5",
+            "lora-dio4":           "NC",
+            "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"
         }
     },
-    "macros": ["MBEDTLS_USER_CONFIG_FILE=\"mbedtls_lora_config.h\""]
+    "macros": ["MBEDTLS_USER_CONFIG_FILE=\"mbedtls_lora_config.h\"", "FEA_TRACE_SUPPORT"]
 }
-