Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
3,360 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* 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 "lorawan/LoRaRadio.h" | ||
|
||
#ifndef APP_LORA_RADIO_HELPER_H_ | ||
#define APP_LORA_RADIO_HELPER_H_ | ||
|
||
#include "SX1272_LoRaRadio.h" | ||
#include "SX1276_LoRaRadio.h" | ||
|
||
#define SX1272 0xFF | ||
#define SX1276 0xEE | ||
|
||
#if (MBED_CONF_APP_LORA_RADIO == SX1272) | ||
|
||
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) | ||
|
||
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); | ||
|
||
#else | ||
#error "Unknown LoRa radio specified (SX1272,SX1276 are valid)" | ||
#endif | ||
|
||
|
||
#endif /* APP_LORA_RADIO_HELPER_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#ifndef _LORAWAN_NETWORK_H_ | ||
#define _LORAWAN_NETWORK_H_ | ||
|
||
#include "mbed.h" | ||
#include "mbed_trace.h" | ||
#include "mbed_events.h" | ||
#include "LoRaWANInterface.h" | ||
#include "SX1276_LoRaRadio.h" | ||
#include "CayenneLPP.h" | ||
#include "lora_radio_helper.h" | ||
|
||
#define MBED_CONF_LORA_APP_PORT 15 | ||
|
||
// EventQueue is required to dispatch events around | ||
EventQueue ev_queue; | ||
|
||
// Constructing Mbed LoRaWANInterface and passing it down the radio object. | ||
static LoRaWANInterface lorawan(radio); | ||
|
||
// Application specific callbacks | ||
static lorawan_app_callbacks_t callbacks; | ||
|
||
// LoRaWAN stack event handler | ||
static void lora_event_handler(lorawan_event_t event); | ||
|
||
static void lorawan_setup(uint32_t devAddr, uint8_t nwkSKey[16], uint8_t appSKey[16], Callback<void(lorawan_event_t)> lw_event_handler) { | ||
if (devAddr == 0x0) { | ||
printf("Set your LoRaWAN credentials first!\n"); | ||
return; | ||
} | ||
|
||
// Enable trace output for this demo, so we can see what the LoRaWAN stack does | ||
mbed_trace_init(); | ||
|
||
if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { | ||
printf("[LNWK][ERR ] LoRa initialization failed!\n"); | ||
return; | ||
} | ||
|
||
// prepare application callbacks | ||
callbacks.events = lw_event_handler; | ||
lorawan.add_app_callbacks(&callbacks); | ||
|
||
// Disable adaptive data rating | ||
if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) { | ||
printf("[LNWK][ERR ] disable_adaptive_datarate failed!\n"); | ||
return; | ||
} | ||
|
||
lorawan.set_datarate(0); // SF12BW125 | ||
|
||
lorawan_connect_t connect_params; | ||
connect_params.connect_type = LORAWAN_CONNECTION_ABP; | ||
// connect_params.connection_u.otaa.dev_eui = DEV_EUI; | ||
// connect_params.connection_u.otaa.app_eui = APP_EUI; | ||
// connect_params.connection_u.otaa.app_key = APP_KEY; | ||
// connect_params.connection_u.otaa.nb_trials = 3; | ||
|
||
connect_params.connection_u.abp.dev_addr = devAddr; | ||
connect_params.connection_u.abp.nwk_skey = nwkSKey; | ||
connect_params.connection_u.abp.app_skey = appSKey; | ||
|
||
lorawan_status_t retcode = lorawan.connect(connect_params); | ||
|
||
if (retcode == LORAWAN_STATUS_OK || | ||
retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { | ||
} else { | ||
printf("[LNWK][ERR ] Connection error, code = %d\n", retcode); | ||
return; | ||
} | ||
|
||
printf("[LNWK][INFO] Connection - In Progress...\n"); | ||
} | ||
|
||
// This is called from RX_DONE, so whenever a message came in | ||
static void receive_message() | ||
{ | ||
uint8_t rx_buffer[50] = { 0 }; | ||
int16_t retcode; | ||
retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer, | ||
sizeof(rx_buffer), | ||
MSG_UNCONFIRMED_FLAG); | ||
|
||
if (retcode < 0) { | ||
printf("[LNWK][WARN] receive() - Error code %d\n", retcode); | ||
return; | ||
} | ||
|
||
printf("[LNWK][INFO] Data received on port %d (length %d): ", MBED_CONF_LORA_APP_PORT, retcode); | ||
|
||
for (uint8_t i = 0; i < retcode; i++) { | ||
printf("%02x ", rx_buffer[i]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
// Event handler | ||
bool lorawan_send(CayenneLPP *payload) { | ||
int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, payload->getBuffer(), payload->getSize(), MSG_UNCONFIRMED_FLAG); | ||
|
||
// for some reason send() ret\urns -1... I cannot find out why, the stack returns the right number. I feel that this is some weird Emscripten quirk | ||
if (retcode < 0) { | ||
retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("[LNWK][WARN] send - duty cycle violation\n") | ||
: printf("[LNWK][WARN] send() - Error code %d\n", retcode); | ||
return false; | ||
} | ||
|
||
printf("[LNWK][INFO] %d bytes scheduled for transmission\n", retcode); | ||
return true; | ||
} | ||
|
||
#endif // _LORAWAN_NETWORK_H_ |
Oops, something went wrong.