Skip to content

Commit ba5b5a3

Browse files
author
Cruz Monrreal
authored
Merge pull request #7191 from hasnainvirk/precise_timing
LoRaWAN: Fine tuning timing for delays and receive windows
2 parents 31df3d2 + d335f82 commit ba5b5a3

File tree

5 files changed

+261
-243
lines changed

5 files changed

+261
-243
lines changed

features/lorawan/LoRaWANStack.cpp

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,20 @@ using namespace events;
6868
* Constructor *
6969
****************************************************************************/
7070
LoRaWANStack::LoRaWANStack()
71-
: _loramac(),
72-
_device_current_state(DEVICE_STATE_NOT_INITIALIZED),
73-
_lw_session(),
74-
_tx_msg(),
75-
_rx_msg(),
76-
_tx_metadata(),
77-
_rx_metadata(),
78-
_num_retry(1),
79-
_ctrl_flags(IDLE_FLAG),
80-
_app_port(INVALID_PORT),
81-
_link_check_requested(false),
82-
_automatic_uplink_ongoing(false),
83-
_ready_for_rx(true),
84-
_queue(NULL)
71+
: _loramac(),
72+
_device_current_state(DEVICE_STATE_NOT_INITIALIZED),
73+
_lw_session(),
74+
_tx_msg(),
75+
_rx_msg(),
76+
_tx_metadata(),
77+
_rx_metadata(),
78+
_num_retry(1),
79+
_ctrl_flags(IDLE_FLAG),
80+
_app_port(INVALID_PORT),
81+
_link_check_requested(false),
82+
_automatic_uplink_ongoing(false),
83+
_ready_for_rx(true),
84+
_queue(NULL)
8585
{
8686
_tx_metadata.stale = true;
8787
_rx_metadata.stale = true;
@@ -481,7 +481,7 @@ lorawan_status_t LoRaWANStack::acquire_rx_metadata(lorawan_rx_metadata &metadata
481481
return LORAWAN_STATUS_METADATA_NOT_AVAILABLE;
482482
}
483483

484-
lorawan_status_t LoRaWANStack::acquire_backoff_metadata(int& backoff)
484+
lorawan_status_t LoRaWANStack::acquire_backoff_metadata(int &backoff)
485485
{
486486
if (DEVICE_STATE_NOT_INITIALIZED == _device_current_state) {
487487
return LORAWAN_STATUS_NOT_INITIALIZED;
@@ -503,6 +503,7 @@ lorawan_status_t LoRaWANStack::acquire_backoff_metadata(int& backoff)
503503
****************************************************************************/
504504
void LoRaWANStack::tx_interrupt_handler(void)
505505
{
506+
_tx_timestamp = _loramac.get_current_time();
506507
const int ret = _queue->call(this, &LoRaWANStack::process_transmission);
507508
MBED_ASSERT(ret != 0);
508509
(void)ret;
@@ -565,7 +566,7 @@ void LoRaWANStack::process_transmission_timeout()
565566
void LoRaWANStack::process_transmission(void)
566567
{
567568
tr_debug("Transmission completed");
568-
_loramac.on_radio_tx_done();
569+
_loramac.on_radio_tx_done(_tx_timestamp);
569570

570571
make_tx_metadata_available();
571572

@@ -680,10 +681,12 @@ void LoRaWANStack::process_reception(const uint8_t *const payload, uint16_t size
680681

681682
void LoRaWANStack::process_reception_timeout(bool is_timeout)
682683
{
684+
rx_slot_t slot = _loramac.get_current_slot();
685+
683686
// when is_timeout == false, a CRC error took place in the received frame
684687
// we treat that erroneous frame as no frame received at all, hence handle
685688
// it exactly as we would handle timeout
686-
rx_slot_t slot = _loramac.on_radio_rx_timeout(is_timeout);
689+
_loramac.on_radio_rx_timeout(is_timeout);
687690

688691
if (slot == RX_SLOT_WIN_2 && !_loramac.nwk_joined()) {
689692
state_controller(DEVICE_STATE_JOINING);
@@ -851,9 +854,11 @@ void LoRaWANStack::mlme_indication_handler()
851854
#if MBED_CONF_LORA_AUTOMATIC_UPLINK_MESSAGE
852855
_automatic_uplink_ongoing = true;
853856
tr_debug("mlme indication: sending empty uplink to port 0 to acknowledge MAC commands...");
854-
send_automatic_uplink_message(0);
857+
const uint8_t port = 0;
858+
const int ret = _queue->call(this, &LoRaWANStack::send_automatic_uplink_message, port);
859+
MBED_ASSERT(ret != 0);
860+
(void)ret;
855861
#else
856-
857862
send_event_to_application(UPLINK_REQUIRED);
858863
#endif
859864
return;
@@ -957,8 +962,9 @@ void LoRaWANStack::mcps_indication_handler()
957962
_rx_msg.msg.mcps_indication.type = mcps_indication->type;
958963

959964
// Notify application about received frame..
960-
tr_debug("Packet Received %d bytes",
961-
_rx_msg.msg.mcps_indication.buffer_size);
965+
tr_debug("Packet Received %d bytes, Port=%d",
966+
_rx_msg.msg.mcps_indication.buffer_size,
967+
mcps_indication->port);
962968
_rx_msg.receive_ready = true;
963969
send_event_to_application(RX_DONE);
964970
}
@@ -981,7 +987,9 @@ void LoRaWANStack::mcps_indication_handler()
981987
#if (MBED_CONF_LORA_AUTOMATIC_UPLINK_MESSAGE)
982988
tr_debug("Sending empty uplink message...");
983989
_automatic_uplink_ongoing = true;
984-
send_automatic_uplink_message(mcps_indication->port);
990+
const int ret = _queue->call(this, &LoRaWANStack::send_automatic_uplink_message, mcps_indication->port);
991+
MBED_ASSERT(ret != 0);
992+
(void)ret;
985993
#else
986994
send_event_to_application(UPLINK_REQUIRED);
987995
#endif
@@ -1354,9 +1362,11 @@ void LoRaWANStack::compliance_test_handler(loramac_mcps_indication_t *mcps_indic
13541362
} else if (mcps_indication->buffer_size == 7) {
13551363
loramac_mlme_req_t mlme_req;
13561364
mlme_req.type = MLME_TXCW_1;
1357-
mlme_req.cw_tx_mode.timeout = (uint16_t)((mcps_indication->buffer[1] << 8) | mcps_indication->buffer[2]);
1358-
mlme_req.cw_tx_mode.frequency = (uint32_t)((mcps_indication->buffer[3] << 16) | (mcps_indication->buffer[4] << 8)
1359-
| mcps_indication->buffer[5]) * 100;
1365+
mlme_req.cw_tx_mode.timeout = (uint16_t)((mcps_indication->buffer[1] << 8)
1366+
| mcps_indication->buffer[2]);
1367+
mlme_req.cw_tx_mode.frequency = (uint32_t)((mcps_indication->buffer[3] << 16)
1368+
| (mcps_indication->buffer[4] << 8)
1369+
| mcps_indication->buffer[5]) * 100;
13601370
mlme_req.cw_tx_mode.power = mcps_indication->buffer[6];
13611371
_loramac.mlme_request(&mlme_req);
13621372
}

features/lorawan/LoRaWANStack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
548548
volatile bool _ready_for_rx;
549549
uint8_t _rx_payload[LORAMAC_PHY_MAXPAYLOAD];
550550
events::EventQueue *_queue;
551+
lorawan_time_t _tx_timestamp;
551552

552553
#if defined(LORAWAN_COMPLIANCE_TEST)
553554

0 commit comments

Comments
 (0)