1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #8864 from aabadie/pr/pkg/semtech-loramac_update_pkg

pkg/semtech-loramac: update to upstream v4.4.1 + cleanup
This commit is contained in:
Alexandre Abadie 2018-09-26 08:57:40 +02:00 committed by GitHub
commit 61f3687ee2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 282 additions and 258 deletions

View File

@ -173,8 +173,8 @@ typedef struct {
uint8_t coderate; /**< Error coding rate */
uint8_t freq_hop_period; /**< Frequency hop period */
uint8_t flags; /**< Boolean flags */
uint32_t rx_timeout; /**< RX timeout in symbols */
uint32_t tx_timeout; /**< TX timeout in symbols */
uint32_t rx_timeout; /**< RX timeout in microseconds */
uint32_t tx_timeout; /**< TX timeout in microseconds */
} sx127x_lora_settings_t;
/**

View File

@ -313,6 +313,7 @@ void sx127x_set_rx(sx127x_t *dev)
xtimer_set(&(dev->_internal.rx_timeout_timer), dev->settings.lora.rx_timeout);
}
if (dev->settings.lora.flags & SX127X_RX_CONTINUOUS_FLAG) {
sx127x_set_op_mode(dev, SX127X_RF_LORA_OPMODE_RECEIVER);
}
@ -830,13 +831,11 @@ void sx127x_set_symbol_timeout(sx127x_t *dev, uint16_t timeout)
{
DEBUG("[sx127x] Set symbol timeout: %d\n", timeout);
dev->settings.lora.rx_timeout = timeout;
uint8_t config2_reg = sx127x_reg_read(dev, SX127X_REG_LR_MODEMCONFIG2);
config2_reg &= SX127X_RF_LORA_MODEMCONFIG2_SYMBTIMEOUTMSB_MASK;
config2_reg |= (timeout >> 8) & ~SX127X_RF_LORA_MODEMCONFIG2_SYMBTIMEOUTMSB_MASK;
sx127x_reg_write(dev, SX127X_REG_LR_MODEMCONFIG2, config2_reg);
sx127x_reg_write(dev, SX127X_REG_LR_SYMBTIMEOUTLSB,timeout & 0xFF);
sx127x_reg_write(dev, SX127X_REG_LR_SYMBTIMEOUTLSB, timeout & 0xFF);
}
bool sx127x_get_iq_invert(const sx127x_t *dev)

View File

@ -194,7 +194,6 @@ static int _recv(netdev_t *netdev, void *buf, size_t len, void *info)
}
xtimer_remove(&dev->_internal.rx_timeout_timer);
/* Read the last packet from FIFO */
uint8_t last_rx_addr = sx127x_reg_read(dev, SX127X_REG_LR_FIFORXCURRENTADDR);
sx127x_reg_write(dev, SX127X_REG_LR_FIFOADDRPTR, last_rx_addr);

View File

@ -28,6 +28,7 @@ FEATURES_REQUIRED += periph_rtc
CFLAGS += -DREGION_$(REGION)
CFLAGS += -DDEVEUI=\"$(DEVEUI)\" -DAPPEUI=\"$(APPEUI)\" -DAPPKEY=\"$(APPKEY)\"
CFLAGS += -DLORAMAC_ACTIVE_REGION=LORAMAC_REGION_$(REGION)
# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the

View File

@ -1,6 +1,6 @@
PKG_NAME=semtech-loramac
PKG_URL=https://github.com/Lora-net/LoRaMac-node.git
PKG_VERSION=f42be67be402a40b3586724800771bfe13fb18e6
PKG_VERSION=1cdd9ccec4c9f05b616e7112059be4a9e358c571
PKG_LICENSE=BSD-3-Clause
.PHONY: all
@ -10,7 +10,7 @@ all: git-download
@cp Makefile.loramac_mac $(PKG_BUILDDIR)/src/mac/Makefile
@cp Makefile.loramac_region $(PKG_BUILDDIR)/src/mac/region/Makefile
@cp Makefile.loramac_crypto $(PKG_BUILDDIR)/src/system/crypto/Makefile
@cp Makefile.loramac_arch $(PKG_BUILDDIR)/src/boards/mcu/stm32/Makefile
@cp Makefile.loramac_arch $(PKG_BUILDDIR)/src/boards/mcu/Makefile
"$(MAKE)" -C $(PKG_BUILDDIR)
include $(RIOTBASE)/pkg/pkg.mk

View File

@ -1,10 +1,10 @@
DIRS += src/mac
DIRS += src/mac/region
DIRS += src/system/crypto
DIRS += src/boards/mcu/stm32
DIRS += src/boards/mcu
INCLUDES += -I$(PKGDIRBASE)/semtech-loramac/src/mac \
-I$(PKGDIRBASE)/semtech-loramac/src/boards/mcu/stm32 \
-I$(PKGDIRBASE)/semtech-loramac/src/boards/mcu \
-I$(PKGDIRBASE)/semtech-loramac/src/system/crypto \
-I$(PKGDIRBASE)/semtech-loramac/src

View File

@ -4,4 +4,6 @@ SRCS := utilities.c
CFLAGS += -Wno-sign-compare
INCLUDES += -I$(PKGDIRBASE)/semtech-loramac/src/boards
include $(RIOTBASE)/Makefile.base

View File

@ -1,3 +1,5 @@
MODULE := semtech_loramac_crypto
INCLUDES += -I$(PKGDIRBASE)/semtech-loramac/src/boards
include $(RIOTBASE)/Makefile.base

View File

@ -2,4 +2,7 @@ MODULE := semtech_loramac_mac
CFLAGS += -Wno-sign-compare
INCLUDES += -I$(PKGDIRBASE)/semtech-loramac/src/boards \
-I$(PKGDIRBASE)/semtech-loramac/src/radio
include $(RIOTBASE)/Makefile.base

View File

@ -2,4 +2,7 @@ MODULE := semtech_loramac_mac_region
CFLAGS += -Wno-missing-field-initializers -Wno-unused-parameter -Wno-sign-compare
INCLUDES += -I$(PKGDIRBASE)/semtech-loramac/src/boards \
-I$(PKGDIRBASE)/semtech-loramac/src/radio
include $(RIOTBASE)/Makefile.base

View File

@ -1,7 +1,8 @@
MODULE := semtech_loramac_contrib
INCLUDES += -I$(PKGDIRBASE)/semtech-loramac/src/mac \
-I$(PKGDIRBASE)/semtech-loramac/src/boards/mcu/stm32 \
-I$(PKGDIRBASE)/semtech-loramac/src/boards \
-I$(PKGDIRBASE)/semtech-loramac/src/radio \
-I$(PKGDIRBASE)/semtech-loramac/src/system/crypto \
-I$(PKGDIRBASE)/semtech-loramac/src

View File

@ -38,30 +38,12 @@
#include "sx127x_netdev.h"
#include "semtech_loramac.h"
#include "semtech-loramac/board.h"
#include "LoRaMac.h"
#include "region/Region.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
#define LORAWAN_MAX_JOIN_RETRIES (3U)
#if defined(REGION_EU868)
#define LORAWAN_DUTYCYCLE_ON (true)
#define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP (1)
#if (USE_SEMTECH_DEFAULT_CHANNEL_LINEUP)
#define LC4 { 867100000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
#define LC5 { 867300000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
#define LC6 { 867500000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
#define LC7 { 867700000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
#define LC8 { 867900000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 }
#define LC9 { 868800000, 0, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 }
#define LC10 { 868300000, 0, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 }
#endif /* USE_SEMTECH_DEFAULT_CHANNEL_LINEUP */
#endif /* REGION_EU868 */
#define SEMTECH_LORAMAC_MSG_QUEUE (16U)
#define SEMTECH_LORAMAC_LORAMAC_STACKSIZE (THREAD_STACKSIZE_DEFAULT)
static msg_t _semtech_loramac_msg_queue[SEMTECH_LORAMAC_MSG_QUEUE];
@ -91,8 +73,8 @@ typedef struct {
} semtech_loramac_call_t;
/* Prepares the payload of the frame */
static bool _semtech_loramac_send(semtech_loramac_t *mac,
uint8_t *payload, uint8_t len)
static uint8_t _semtech_loramac_send(semtech_loramac_t *mac,
uint8_t *payload, uint8_t len)
{
DEBUG("[semtech-loramac] send frame %s\n", (char *)payload);
McpsReq_t mcpsReq;
@ -109,7 +91,7 @@ static bool _semtech_loramac_send(semtech_loramac_t *mac,
}
else {
if (mac->cnf == LORAMAC_TX_UNCNF) {
DEBUG("[semtech-loramac] MCPS_UNCONFIRMED\n");
DEBUG("[semtech-loramac] MCPS request: unconfirmed TX\n");
mcpsReq.Type = MCPS_UNCONFIRMED;
mcpsReq.Req.Unconfirmed.fPort = mac->port;
mcpsReq.Req.Unconfirmed.fBuffer = payload;
@ -117,7 +99,7 @@ static bool _semtech_loramac_send(semtech_loramac_t *mac,
mcpsReq.Req.Unconfirmed.Datarate = (int8_t)dr;
}
else {
DEBUG("[semtech-loramac] MCPS_CONFIRMED\n");
DEBUG("[semtech-loramac] MCPS request: confirmed TX\n");
mcpsReq.Type = MCPS_CONFIRMED;
mcpsReq.Req.Confirmed.fPort = mac->port;
mcpsReq.Req.Confirmed.fBuffer = payload;
@ -127,12 +109,31 @@ static bool _semtech_loramac_send(semtech_loramac_t *mac,
}
}
if (LoRaMacMcpsRequest(&mcpsReq) == LORAMAC_STATUS_OK) {
DEBUG("[semtech-loramac] MCPS request OK\n");
return false;
uint8_t req_status = LoRaMacMcpsRequest(&mcpsReq);
uint8_t ret = SEMTECH_LORAMAC_TX_ERROR;
switch (req_status) {
case LORAMAC_STATUS_OK:
DEBUG("[semtech-loramac] MCPS request: OK\n");
ret = SEMTECH_LORAMAC_TX_OK;
break;
case LORAMAC_STATUS_PARAMETER_INVALID:
DEBUG("[semtech-loramac] MCPS request: invalid parameter\n");
break;
case LORAMAC_STATUS_BUSY:
DEBUG("[semtech-loramac] MCPS request: busy\n");
ret = SEMTECH_LORAMAC_BUSY;
break;
case LORAMAC_STATUS_DUTYCYCLE_RESTRICTED:
DEBUG("[semtech-loramac] MCPS request: dutycycle restricted\n");
ret = SEMTECH_LORAMAC_DUTYCYCLE_RESTRICTED;
break;
default:
DEBUG("[semtech-loramac] MCPS request: unknown status %d\n",
req_status);
break;
}
return true;
return ret;
}
/* MCPS-Confirm event function */
@ -147,9 +148,10 @@ static void mcps_confirm(McpsConfirm_t *confirm)
{
/* Check Datarate
Check TxPower */
DEBUG("[semtech-loramac] MCPS confirm event UNCONFIRMED\n");
DEBUG("[semtech-loramac] MCPS confirm event: UNCONFIRMED\n");
msg_t msg;
msg.type = MSG_TYPE_LORAMAC_TX_DONE;
msg.type = MSG_TYPE_LORAMAC_TX_STATUS;
msg.content.value = SEMTECH_LORAMAC_TX_DONE;
msg_send(&msg, semtech_loramac_pid);
break;
}
@ -159,21 +161,22 @@ static void mcps_confirm(McpsConfirm_t *confirm)
Check TxPower
Check AckReceived
Check NbTrials */
DEBUG("[semtech-loramac] MCPS confirm event CONFIRMED\n");
DEBUG("[semtech-loramac] MCPS confirm event: CONFIRMED\n");
break;
case MCPS_PROPRIETARY:
DEBUG("[semtech-loramac] MCPS confirm event PROPRIETARY\n");
DEBUG("[semtech-loramac] MCPS confirm event: PROPRIETARY\n");
break;
default:
DEBUG("[semtech-loramac] MCPS confirm event UNKNOWN\n");
DEBUG("[semtech-loramac] MCPS confirm event: UNKNOWN\n");
break;
}
}
else {
msg_t msg;
msg.type = MSG_TYPE_LORAMAC_TX_CNF_FAILED;
msg.type = MSG_TYPE_LORAMAC_TX_STATUS;
msg.content.value = SEMTECH_LORAMAC_TX_CNF_FAILED;
msg_send(&msg, semtech_loramac_pid);
}
}
@ -210,6 +213,21 @@ static void mcps_indication(McpsIndication_t *indication)
}
}
/* Check Multicast
Check Port
Check Datarate
Check FramePending */
if (indication->FramePending == true) {
/* The server signals that it has pending data to be sent.
We schedule an uplink as soon as possible to flush the server. */
DEBUG("[semtech-loramac] MCPS indication: pending data, schedule an "
"uplink\n");
msg_t msg;
msg.type = MSG_TYPE_LORAMAC_TX_STATUS;
msg.content.value = SEMTECH_LORAMAC_TX_SCHEDULE;
msg_send(&msg, semtech_loramac_pid);
}
msg_t msg;
if (indication->RxData) {
DEBUG("[semtech-loramac] MCPS indication: data received\n");
@ -217,12 +235,13 @@ static void mcps_indication(McpsIndication_t *indication)
msg.content.ptr = indication;
}
else {
msg.type = MSG_TYPE_LORAMAC_TX_DONE;
msg.type = MSG_TYPE_LORAMAC_TX_STATUS;
msg.content.value = SEMTECH_LORAMAC_TX_DONE;
}
msg_send(&msg, semtech_loramac_pid);
}
/*MLME-Confirm event function */
/* MLME-Confirm event function */
static void mlme_confirm(MlmeConfirm_t *confirm)
{
DEBUG("[semtech-loramac] MLME confirm event\n");
@ -260,6 +279,24 @@ static void mlme_confirm(MlmeConfirm_t *confirm)
}
}
/* MLME-Indication event function */
static void mlme_indication(MlmeIndication_t *indication)
{
switch (indication->MlmeIndication) {
case MLME_SCHEDULE_UPLINK:
/* The MAC signals that we shall provide an uplink
as soon as possible */
DEBUG("[semtech-loramac] MLME indication: schedule an uplink\n");
msg_t msg;
msg.type = MSG_TYPE_LORAMAC_TX_STATUS;
msg.content.value = SEMTECH_LORAMAC_TX_SCHEDULE;
msg_send(&msg, semtech_loramac_pid);
break;
default:
break;
}
}
void _init_loramac(semtech_loramac_t *mac,
LoRaMacPrimitives_t * primitives, LoRaMacCallback_t *callbacks)
{
@ -268,59 +305,11 @@ void _init_loramac(semtech_loramac_t *mac,
primitives->MacMcpsConfirm = mcps_confirm;
primitives->MacMcpsIndication = mcps_indication;
primitives->MacMlmeConfirm = mlme_confirm;
#if defined(REGION_AS923)
DEBUG("[semtech-loramac] initialize loramac for AS923 region\n");
primitives->MacMlmeIndication = mlme_indication;
LoRaMacInitialization(&semtech_loramac_radio_events, primitives, callbacks,
LORAMAC_REGION_AS923);
#elif defined(REGION_AU915)
DEBUG("[semtech-loramac] initialize loramac for AU915 region\n");
LoRaMacInitialization(&semtech_loramac_radio_events, primitives, callbacks,
LORAMAC_REGION_AU915);
#elif defined(REGION_CN779)
DEBUG("[semtech-loramac] initialize loramac for CN779 region\n");
LoRaMacInitialization(&semtech_loramac_radio_events, primitives, callbacks,
LORAMAC_REGION_CN779);
#elif defined(REGION_EU868)
DEBUG("[semtech-loramac] initialize loramac for EU868 region\n");
LoRaMacInitialization(&semtech_loramac_radio_events, primitives, callbacks,
LORAMAC_REGION_EU868);
#elif defined(REGION_IN865)
DEBUG("[semtech-loramac] initialize loramac for IN865 region\n");
LoRaMacInitialization(&semtech_loramac_radio_events, primitives, callbacks,
LORAMAC_REGION_IN865);
#elif defined(REGION_KR920)
DEBUG("[semtech-loramac] initialize loramac for KR920 region\n");
LoRaMacInitialization(&semtech_loramac_radio_events, primitives, callbacks,
LORAMAC_REGION_KR920);
#elif defined(REGION_US915)
DEBUG("[semtech-loramac] initialize loramac for US915 region\n");
LoRaMacInitialization(&semtech_loramac_radio_events, primitives, callbacks,
LORAMAC_REGION_US915);
#elif defined(REGION_US915_HYBRID)
DEBUG("[semtech-loramac] initialize loramac for US915 hybrid region\n");
LoRaMacInitialization(&semtech_loramac_radio_events, primitives, callbacks,
LORAMAC_REGION_US915_HYBRID);
#else
#error "Please define a region in the compiler options."
#endif
LORAMAC_ACTIVE_REGION);
mutex_unlock(&mac->lock);
#if defined(REGION_EU868) && USE_SEMTECH_DEFAULT_CHANNEL_LINEUP
DEBUG("[semtech-loramac] EU868 region: use default channels\n");
mutex_lock(&mac->lock);
LoRaMacChannelAdd(3, (ChannelParams_t)LC4);
LoRaMacChannelAdd(4, (ChannelParams_t)LC5);
LoRaMacChannelAdd(5, (ChannelParams_t)LC6);
LoRaMacChannelAdd(6, (ChannelParams_t)LC7);
LoRaMacChannelAdd(7, (ChannelParams_t)LC8);
LoRaMacChannelAdd(8, (ChannelParams_t)LC9);
LoRaMacChannelAdd(9, (ChannelParams_t)LC10);
mutex_unlock(&mac->lock);
semtech_loramac_set_rx2_dr(mac, LORAMAC_DEFAULT_RX2_DR);
semtech_loramac_set_rx2_freq(mac, LORAMAC_DEFAULT_RX2_FREQ);
#endif
semtech_loramac_set_dr(mac, LORAMAC_DEFAULT_DR);
semtech_loramac_set_adr(mac, LORAMAC_DEFAULT_ADR);
semtech_loramac_set_public_network(mac, LORAMAC_DEFAULT_PUBLIC_NETWORK);
@ -334,6 +323,8 @@ static void _join_otaa(semtech_loramac_t *mac)
{
DEBUG("[semtech-loramac] starting OTAA join\n");
uint8_t dr = semtech_loramac_get_dr(mac);
mutex_lock(&mac->lock);
MibRequestConfirm_t mibReq;
mibReq.Type = MIB_NETWORK_JOINED;
@ -345,9 +336,44 @@ static void _join_otaa(semtech_loramac_t *mac)
mlmeReq.Req.Join.DevEui = mac->deveui;
mlmeReq.Req.Join.AppEui = mac->appeui;
mlmeReq.Req.Join.AppKey = mac->appkey;
mlmeReq.Req.Join.NbTrials = LORAWAN_MAX_JOIN_RETRIES;
LoRaMacMlmeRequest(&mlmeReq);
mlmeReq.Req.Join.Datarate = dr;
mutex_unlock(&mac->lock);
uint8_t ret = LoRaMacMlmeRequest(&mlmeReq);
switch(ret) {
case LORAMAC_STATUS_OK:
/* Let the MAC do his job */
return;
case LORAMAC_STATUS_DUTYCYCLE_RESTRICTED:
{
DEBUG("[semtech-loramac] join otaa: duty cycle restricted\n");
/* Cannot join. */
msg_t msg;
msg.type = MSG_TYPE_LORAMAC_JOIN;
msg.content.value = SEMTECH_LORAMAC_DUTYCYCLE_RESTRICTED;
msg_send(&msg, semtech_loramac_pid);
return;
}
case LORAMAC_STATUS_BUSY:
{
DEBUG("[semtech-loramac] join otaa: mac is busy\n");
/* Cannot join. */
msg_t msg;
msg.type = MSG_TYPE_LORAMAC_JOIN;
msg.content.value = SEMTECH_LORAMAC_BUSY;
msg_send(&msg, semtech_loramac_pid);
return;
}
default:
{
DEBUG("[semtech-loramac] join otaa: failed with status %d\n", ret);
/* Cannot join. */
msg_t msg;
msg.type = MSG_TYPE_LORAMAC_JOIN;
msg.content.value = SEMTECH_LORAMAC_JOIN_FAILED;
msg_send(&msg, semtech_loramac_pid);
return;
}
}
}
static void _join_abp(semtech_loramac_t *mac)
@ -380,9 +406,6 @@ static void _join_abp(semtech_loramac_t *mac)
mibReq.Type = MIB_NETWORK_JOINED;
mibReq.Param.IsNetworkJoined = true;
LoRaMacMibSetRequestConfirm(&mibReq);
/* switch back to idle state now*/
mac->state = SEMTECH_LORAMAC_STATE_IDLE;
mutex_unlock(&mac->lock);
}
@ -404,7 +427,13 @@ static void _join(semtech_loramac_t *mac, void *arg)
static void _send(semtech_loramac_t *mac, void *arg)
{
loramac_send_params_t params = *(loramac_send_params_t *)arg;
_semtech_loramac_send(mac, params.payload, params.len);
uint8_t status = _semtech_loramac_send(mac, params.payload, params.len);
if (status != SEMTECH_LORAMAC_TX_OK) {
msg_t msg;
msg.type = MSG_TYPE_LORAMAC_TX_STATUS;
msg.content.value = (uint8_t)status;
msg_send(&msg, semtech_loramac_pid);
}
}
static void _semtech_loramac_call(semtech_loramac_func_t func, void *arg)
@ -520,8 +549,7 @@ void *_semtech_loramac_event_loop(void *arg)
case MSG_TYPE_LORAMAC_CMD:
{
msg_t msg_resp;
DEBUG("[semtech-loramac] loramac cmd\n");
mac->state = SEMTECH_LORAMAC_STATE_BUSY;
DEBUG("[semtech-loramac] loramac cmd msg\n");
semtech_loramac_call_t *call = msg.content.ptr;
call->func(mac, call->arg);
msg_reply(&msg, &msg_resp);
@ -529,12 +557,10 @@ void *_semtech_loramac_event_loop(void *arg)
}
case MSG_TYPE_LORAMAC_JOIN:
{
DEBUG("[semtech-loramac] loramac join notification\n");
DEBUG("[semtech-loramac] loramac join notification msg\n");
msg_t msg_ret;
msg_ret.content.value = msg.content.value;
msg_send(&msg_ret, mac->caller_pid);
/* switch back to idle state now*/
mac->state = SEMTECH_LORAMAC_STATE_IDLE;
break;
}
case MSG_TYPE_LORAMAC_LINK_CHECK:
@ -550,24 +576,24 @@ void *_semtech_loramac_event_loop(void *arg)
mac->link_chk.nb_gateways);
break;
}
case MSG_TYPE_LORAMAC_TX_DONE:
case MSG_TYPE_LORAMAC_TX_STATUS:
{
DEBUG("[semtech-loramac] loramac TX done\n");
msg_t msg_ret;
msg_ret.type = MSG_TYPE_LORAMAC_TX_DONE;
msg_send(&msg_ret, mac->caller_pid);
/* switch back to idle state now*/
mac->state = SEMTECH_LORAMAC_STATE_IDLE;
DEBUG("[semtech-loramac] loramac TX status msg\n");
if (msg.content.value == SEMTECH_LORAMAC_TX_SCHEDULE) {
DEBUG("[semtech-loramac] schedule immediate TX\n");
uint8_t prev_port = mac->port;
mac->port = 0;
_semtech_loramac_send(mac, NULL, 0);
mac->port = prev_port;
}
else {
msg_t msg_ret;
msg_ret.type = msg.type;
msg_ret.content.value = msg.content.value;
msg_send(&msg_ret, mac->caller_pid);
}
break;
}
case MSG_TYPE_LORAMAC_TX_CNF_FAILED:
DEBUG("[semtech-loramac] loramac TX failed\n");
msg_t msg_ret;
msg_ret.type = MSG_TYPE_LORAMAC_TX_CNF_FAILED;
msg_send(&msg_ret, mac->caller_pid);
/* switch back to idle state now*/
mac->state = SEMTECH_LORAMAC_STATE_IDLE;
break;
case MSG_TYPE_LORAMAC_RX:
{
msg_t msg_ret;
@ -585,8 +611,6 @@ void *_semtech_loramac_event_loop(void *arg)
mac->rx_data.payload_len,
mac->rx_data.port);
msg_send(&msg_ret, mac->caller_pid);
/* switch back to idle state now*/
mac->state = SEMTECH_LORAMAC_STATE_IDLE;
break;
}
default:
@ -620,13 +644,25 @@ int semtech_loramac_init(semtech_loramac_t *mac)
return 0;
}
static bool _is_mac_joined(semtech_loramac_t *mac)
{
mutex_lock(&mac->lock);
MibRequestConfirm_t mibReq;
mibReq.Type = MIB_NETWORK_JOINED;
LoRaMacMibGetRequestConfirm(&mibReq);
bool is_joined = mibReq.Param.IsNetworkJoined;
mutex_unlock(&mac->lock);
return is_joined;
}
uint8_t semtech_loramac_join(semtech_loramac_t *mac, uint8_t type)
{
DEBUG("Starting join procedure: %d\n", type);
DEBUG("[semtech-loramac] Starting join procedure: %d\n", type);
if (mac->state != SEMTECH_LORAMAC_STATE_IDLE) {
DEBUG("[semtech-loramac] internal mac is busy\n");
return SEMTECH_LORAMAC_BUSY;
if (_is_mac_joined(mac)) {
DEBUG("[semtech-loramac] network is already joined\n");
return SEMTECH_LORAMAC_ALREADY_JOINED;
}
mac->caller_pid = thread_getpid();
@ -634,10 +670,9 @@ uint8_t semtech_loramac_join(semtech_loramac_t *mac, uint8_t type)
_semtech_loramac_call(_join, &type);
if (type == LORAMAC_JOIN_OTAA) {
/* Wait until the OTAA join procedure is complete */
/* Wait until the MAC replies about OTAA join procedure */
msg_t msg;
msg_receive(&msg);
mac->state = SEMTECH_LORAMAC_STATE_IDLE;
return (uint8_t)msg.content.value;
}
@ -657,31 +692,19 @@ void semtech_loramac_request_link_check(semtech_loramac_t *mac)
uint8_t semtech_loramac_send(semtech_loramac_t *mac, uint8_t *data, uint8_t len)
{
mutex_lock(&mac->lock);
MibRequestConfirm_t mibReq;
mibReq.Type = MIB_NETWORK_JOINED;
LoRaMacMibGetRequestConfirm(&mibReq);
bool is_joined = mibReq.Param.IsNetworkJoined;
mac->link_chk.available = false;
mutex_unlock(&mac->lock);
if (!is_joined) {
if (!_is_mac_joined(mac)) {
DEBUG("[semtech-loramac] network is not joined\n");
return SEMTECH_LORAMAC_NOT_JOINED;
}
if (mac->state != SEMTECH_LORAMAC_STATE_IDLE) {
DEBUG("[semtech-loramac] internal mac is busy\n");
return SEMTECH_LORAMAC_BUSY;
}
loramac_send_params_t params;
params.payload = data;
params.len = len;
_semtech_loramac_call(_send, &params);
return SEMTECH_LORAMAC_TX_SCHEDULED;
return SEMTECH_LORAMAC_TX_SCHEDULE;
}
uint8_t semtech_loramac_recv(semtech_loramac_t *mac)
@ -696,13 +719,13 @@ uint8_t semtech_loramac_recv(semtech_loramac_t *mac)
case MSG_TYPE_LORAMAC_RX:
ret = SEMTECH_LORAMAC_DATA_RECEIVED;
break;
case MSG_TYPE_LORAMAC_TX_CNF_FAILED:
ret = SEMTECH_LORAMAC_TX_CNF_FAILED;
case MSG_TYPE_LORAMAC_TX_STATUS:
ret = (uint8_t)msg.content.value;
break;
default:
ret = SEMTECH_LORAMAC_TX_DONE;
ret = SEMTECH_LORAMAC_TX_ERROR;
break;
}
}
DEBUG("[semtech-loramac] MAC reply received: %d\n", ret);

View File

@ -24,7 +24,6 @@
#include "net/loramac.h"
#include "semtech-loramac/board.h"
#include "LoRaMac.h"
#define ENABLE_DEBUG (0)

View File

@ -26,8 +26,6 @@
#include "sx127x_internal.h"
#include "sx127x_netdev.h"
#include "semtech-loramac/board.h"
#include "radio/radio.h"
#define ENABLE_DEBUG (0)
@ -35,8 +33,6 @@
extern sx127x_t sx127x;
#define LORAMAC_RX_WINDOW_DURATION (600UL * US_PER_MS)
/*
* Radio driver functions implementation wrappers, the netdev2 object
* is known within the scope of the function
@ -88,7 +84,6 @@ void SX127XSetRxConfig(RadioModems_t modem, uint32_t bandwidth,
bool iqInverted, bool rxContinuous)
{
(void) bandwidthAfc;
(void) symbTimeout;
(void) fixLen;
sx127x_set_modem(&sx127x, modem);
sx127x_set_bandwidth(&sx127x, bandwidth);
@ -101,9 +96,8 @@ void SX127XSetRxConfig(RadioModems_t modem, uint32_t bandwidth,
sx127x_set_freq_hop(&sx127x, freqHopOn);
sx127x_set_hop_period(&sx127x, hopPeriod);
sx127x_set_iq_invert(&sx127x, iqInverted);
sx127x_set_rx_timeout(&sx127x, LORAMAC_RX_WINDOW_DURATION);
sx127x_set_symbol_timeout(&sx127x, 2 * symbTimeout);
sx127x_set_rx_single(&sx127x, !rxContinuous);
sx127x_set_rx(&sx127x);
}
void SX127XSetTxConfig(RadioModems_t modem, int8_t power, uint32_t fdev,
@ -131,7 +125,7 @@ void SX127XSetTxConfig(RadioModems_t modem, int8_t power, uint32_t fdev,
sx127x_set_tx_timeout(&sx127x, timeout * US_PER_MS); /* base unit us, LoRaMAC ms */
}
uint32_t SX127XGetTimeOnAir(RadioModems_t modem, uint8_t pktLen)
uint32_t SX127XTimeOnAir(RadioModems_t modem, uint8_t pktLen)
{
(void) modem;
return sx127x_get_time_on_air(&sx127x, pktLen);
@ -147,19 +141,19 @@ void SX127XSend(uint8_t *buffer, uint8_t size)
dev->driver->send(dev, &iol);
}
void SX127XSetSleep(void)
void SX127XSleep(void)
{
sx127x_set_sleep(&sx127x);
}
void SX127XSetStby(void)
void SX127XStandby(void)
{
sx127x_set_standby(&sx127x);
}
void SX127XSetRx(uint32_t timeout)
void SX127XRx(uint32_t timeout)
{
(void) timeout;
sx127x_set_rx_timeout(&sx127x, timeout * US_PER_MS);
sx127x_set_rx(&sx127x);
}
@ -168,28 +162,28 @@ void SX127XStartCad(void)
sx127x_start_cad(&sx127x);
}
int16_t SX127XReadRssi(RadioModems_t modem)
int16_t SX127XRssi(RadioModems_t modem)
{
sx127x_set_modem(&sx127x, (uint8_t)modem);
return sx127x_read_rssi(&sx127x);
}
void SX127XWrite(uint8_t addr, uint8_t data)
void SX127XWrite(uint16_t addr, uint8_t data)
{
sx127x_reg_write(&sx127x, addr, data);
}
uint8_t SX127XRead(uint8_t addr)
uint8_t SX127XRead(uint16_t addr)
{
return sx127x_reg_read(&sx127x, addr);
}
void SX127XWriteBuffer(uint8_t addr, uint8_t *buffer, uint8_t size)
void SX127XWriteBuffer(uint16_t addr, uint8_t *buffer, uint8_t size)
{
sx127x_reg_write_burst(&sx127x, addr, buffer, size);
}
void SX127XReadBuffer(uint8_t addr, uint8_t *buffer, uint8_t size)
void SX127XReadBuffer(uint16_t addr, uint8_t *buffer, uint8_t size)
{
sx127x_reg_read_burst(&sx127x, addr, buffer, size);
}
@ -227,6 +221,29 @@ void SX127XSetPublicNetwork(bool enable)
}
}
uint32_t SX127XGetWakeupTime(void)
{
return 0;
}
void SX127XIrqProcess(void)
{
return;
}
void SX127XRxBoosted(uint32_t timeout)
{
(void) timeout;
return;
}
void SX127XSetRxDutyCycle(uint32_t rx_time, uint32_t sleep_time)
{
(void) rx_time;
(void) sleep_time;
return;
}
/**
* LoRa function callbacks
*/
@ -241,18 +258,22 @@ const struct Radio_s Radio =
SX127XSetRxConfig,
SX127XSetTxConfig,
SX127XCheckRfFrequency,
SX127XGetTimeOnAir,
SX127XTimeOnAir,
SX127XSend,
SX127XSetSleep,
SX127XSetStby,
SX127XSetRx,
SX127XSleep,
SX127XStandby,
SX127XRx,
SX127XStartCad,
SX127XSetTxContinuousWave,
SX127XReadRssi,
SX127XRssi,
SX127XWrite,
SX127XRead,
SX127XWriteBuffer,
SX127XReadBuffer,
SX127XSetMaxPayloadLength,
SX127XSetPublicNetwork
SX127XSetPublicNetwork,
SX127XGetWakeupTime,
SX127XIrqProcess,
SX127XRxBoosted,
SX127XSetRxDutyCycle,
};

View File

@ -19,9 +19,9 @@
* @}
*/
#include "semtech-loramac/board.h"
#include "xtimer.h"
#include "thread.h"
#include "semtech-loramac/timer.h"
extern kernel_pid_t semtech_loramac_pid;
@ -62,26 +62,26 @@ void TimerSetValue(TimerEvent_t *obj, uint32_t value)
/* According to the lorawan specifications, the data sent from the gateway
could arrive with a short shift in time of +/- 20ms. Here the timeout is
triggered 50ms in advance to make sure the radio switches to RX mode on
triggered 22ms in advance to make sure the radio switches to RX mode on
time and doesn't miss any downlink messages. */
obj->timeout = (value - 50) * 1000;
obj->timeout = (value - 22) * US_PER_MS;
}
TimerTime_t TimerGetCurrentTime(void)
{
uint64_t CurrentTime = xtimer_now_usec64();
uint64_t CurrentTime = xtimer_now_usec64() / US_PER_MS;
return (TimerTime_t)CurrentTime;
}
TimerTime_t TimerGetElapsedTime(TimerTime_t savedTime)
{
uint64_t CurrentTime = xtimer_now_usec64();
uint64_t CurrentTime = xtimer_now_usec64() / US_PER_MS;
return (TimerTime_t)(CurrentTime - savedTime);
}
TimerTime_t TimerGetFutureTime(TimerTime_t eventInFuture)
{
uint64_t CurrentTime = xtimer_now_usec64();
uint64_t CurrentTime = xtimer_now_usec64() / US_PER_MS;
return (TimerTime_t)(CurrentTime + eventInFuture);
}

View File

@ -1,54 +0,0 @@
/*
* Copyright (C) 2017 Fundacion Inria Chile
* 2017 Inria
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup pkg_semtech-loramac
* @brief Internal required Semtech LoRaMAC definitions for radio
* @{
*
* @file
*
* @author José Ignacio Alamos <jialamos@uc.cl>
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
* @author Francisco Molina <francisco.molina@inria.cl>
*/
#ifndef SEMTECH_LORAMAC_BOARD_H
#define SEMTECH_LORAMAC_BOARD_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include "semtech-loramac/timer.h"
/**
* @brief Radio wakeup time from SLEEP mode
*/
#define RADIO_OSC_STARTUP (1U) /* [ms] */
/**
* @brief Radio PLL lock and Mode Ready delay which can vary with the temperature
*/
#define RADIO_SLEEP_TO_RX (2U) /* [ms] */
/**
* @brief Radio complete Wake-up Time with margin for temperature compensation
*/
#define RADIO_WAKEUP_TIME ( RADIO_OSC_STARTUP + RADIO_SLEEP_TO_RX )
#ifdef __cplusplus
}
#endif
#endif /* SEMTECH_LORAMAC_BOARD_H */
/** @} */

View File

@ -36,16 +36,15 @@ extern "C" {
* @name Definitions for messages exchanged between the MAC and call threads
* @{
*/
#define MSG_TYPE_ISR (0x3456) /**< radio device ISR */
#define MSG_TYPE_RX_TIMEOUT (0x3457) /**< radio driver RX timeout */
#define MSG_TYPE_TX_TIMEOUT (0x3458) /**< radio driver TX timeout */
#define MSG_TYPE_MAC_TIMEOUT (0x3459) /**< MAC timers timeout */
#define MSG_TYPE_LORAMAC_CMD (0x3460) /**< Command sent to the MAC */
#define MSG_TYPE_LORAMAC_JOIN (0x3461) /**< MAC join event */
#define MSG_TYPE_LORAMAC_TX_DONE (0x3462) /**< MAC TX completes */
#define MSG_TYPE_LORAMAC_RX (0x3463) /**< Some data received */
#define MSG_TYPE_LORAMAC_LINK_CHECK (0x3464) /**< Link check info received */
#define MSG_TYPE_LORAMAC_TX_CNF_FAILED (0x3465) /**< MAC TX confirmed failed */
#define MSG_TYPE_ISR (0x3456) /**< radio device ISR */
#define MSG_TYPE_RX_TIMEOUT (0x3457) /**< radio driver RX timeout */
#define MSG_TYPE_TX_TIMEOUT (0x3458) /**< radio driver TX timeout */
#define MSG_TYPE_MAC_TIMEOUT (0x3459) /**< MAC timers timeout */
#define MSG_TYPE_LORAMAC_CMD (0x3460) /**< Command sent to the MAC */
#define MSG_TYPE_LORAMAC_JOIN (0x3461) /**< MAC join event */
#define MSG_TYPE_LORAMAC_TX_STATUS (0x3462) /**< MAC TX status */
#define MSG_TYPE_LORAMAC_RX (0x3463) /**< Some data received */
#define MSG_TYPE_LORAMAC_LINK_CHECK (0x3464) /**< Link check info received */
/** @} */
/**
@ -57,14 +56,18 @@ extern "C" {
* @brief LoRaMAC return status
*/
enum {
SEMTECH_LORAMAC_JOIN_SUCCEEDED, /**< Join procedure succeeded */
SEMTECH_LORAMAC_JOIN_FAILED, /**< Join procedure failed */
SEMTECH_LORAMAC_NOT_JOINED, /**< MAC is not joined */
SEMTECH_LORAMAC_TX_SCHEDULED, /**< TX data scheduled */
SEMTECH_LORAMAC_TX_DONE, /**< Transmission completed */
SEMTECH_LORAMAC_TX_CNF_FAILED, /**< Confirmable transmission failed */
SEMTECH_LORAMAC_DATA_RECEIVED, /**< Data received */
SEMTECH_LORAMAC_BUSY /**< Internal MAC is busy */
SEMTECH_LORAMAC_JOIN_SUCCEEDED, /**< Join procedure succeeded */
SEMTECH_LORAMAC_JOIN_FAILED, /**< Join procedure failed */
SEMTECH_LORAMAC_NOT_JOINED, /**< MAC is not joined */
SEMTECH_LORAMAC_ALREADY_JOINED, /**< MAC is already joined */
SEMTECH_LORAMAC_TX_OK, /**< Transmission is in progress */
SEMTECH_LORAMAC_TX_SCHEDULE, /**< TX needs reschedule */
SEMTECH_LORAMAC_TX_DONE, /**< Transmission completed */
SEMTECH_LORAMAC_TX_CNF_FAILED, /**< Confirmable transmission failed */
SEMTECH_LORAMAC_TX_ERROR, /**< Error in TX (invalid param, unknown service) */
SEMTECH_LORAMAC_DATA_RECEIVED, /**< Data received */
SEMTECH_LORAMAC_BUSY, /**< Internal MAC is busy */
SEMTECH_LORAMAC_DUTYCYCLE_RESTRICTED /**< Restricted access to channels */
};
/**
@ -106,7 +109,6 @@ typedef struct {
*/
typedef struct {
mutex_t lock; /**< loramac access lock */
uint8_t state; /**< internal loramac state */
uint8_t caller_pid; /**< pid of caller thread */
uint8_t port; /**< application TX port */
uint8_t cnf; /**< enable/disable confirmable messages */

Binary file not shown.

View File

@ -19,5 +19,6 @@ USEMODULE += shell_commands
USEMODULE += fmt
CFLAGS += -DREGION_$(LORA_REGION)
CFLAGS += -DLORAMAC_ACTIVE_REGION=LORAMAC_REGION_$(LORA_REGION)
include $(RIOTBASE)/Makefile.include

View File

@ -352,12 +352,26 @@ static int _cmd_loramac(int argc, char **argv)
return 1;
}
if (semtech_loramac_join(&loramac, join_type) != SEMTECH_LORAMAC_JOIN_SUCCEEDED) {
puts("Join procedure failed!");
return 1;
switch (semtech_loramac_join(&loramac, join_type)) {
case SEMTECH_LORAMAC_DUTYCYCLE_RESTRICTED:
puts("Cannot join: dutycycle restriction");
return 1;
case SEMTECH_LORAMAC_BUSY:
puts("Cannot join: mac is busy");
return 1;
case SEMTECH_LORAMAC_JOIN_FAILED:
puts("Join procedure failed!");
return 1;
case SEMTECH_LORAMAC_ALREADY_JOINED:
puts("Warning: already joined!");
return 1;
case SEMTECH_LORAMAC_JOIN_SUCCEEDED:
puts("Join procedure succeeded!");
break;
default: /* should not happen */
break;
}
puts("Join procedure succeeded!");
return 0;
}
else if (strcmp(argv[1], "tx") == 0) {
if (argc < 3) {
@ -412,9 +426,17 @@ static int _cmd_loramac(int argc, char **argv)
(char *)loramac.rx_data.payload, loramac.rx_data.port);
break;
case SEMTECH_LORAMAC_TX_CNF_FAILED:
puts("Confirmable TX failed");
break;
case SEMTECH_LORAMAC_DUTYCYCLE_RESTRICTED:
puts("Cannot send: dutycycle restriction");
return 1;
case SEMTECH_LORAMAC_BUSY:
puts("Cannot send: MAC is busy");
return 1;
case SEMTECH_LORAMAC_TX_ERROR:
puts("Cannot send: error");
return 1;
case SEMTECH_LORAMAC_TX_DONE:
puts("TX complete, no data received");