end node on synchronous star LoRa network.

Dependencies:   SX127x sx12xx_hal TSL2561

radio chip selection

Radio chip driver is not included, allowing choice of radio device.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.

This project for use with LoRaWAN_singlechannel_gateway project.

Alternately gateway running on raspberry pi can be used as gateway.

LoRaWAN on single radio channel

Network description is at gateway project page. Synchronous star network.

Hardware Support

This project supports SX1276 and SX1272, sx126x kit, sx126x shield, and sx128x 2.4GHz. The ST board B-L072Z-LRWAN1 is also supported (TypeABZ module). When B-L072Z-LRWAN1 target is selected, TARGET_DISCO_L072CZ_LRWAN1 is defined by tools, allowing correct radio driver configuration for this platform. Alternately, any mbed board that can use LoRa radio shield board should work, but NUCLEO boards are tested.

End-node Unique ID

DevEUI is created from CPU serial number. AppEUI and AppKey are declared as software constants.

End-node Configuration

Data rate definition LORAMAC_DEFAULT_DATARATE configured in LoRaMac-definitions.h. See gateway project page for configuration of gateway.
LoRaWAN addressing is configured in Comissioning.h; only OTA mode is functional.
Header file board/lora_config.h, selects application layer options (i.e. sensors) to be compiled in.

Serial Interface

Serial port operates at 115200bps.
Application layer single_us915_main.cpp User button triggers uplink (i.e. blue button on nucleo board), or jumper enables continuously sends repeated uplink packets. The MAC layer holds each uplink request until the allocated timeslot.

commandargumentsdescription
?-print available commands
. (period)-print status (DevEUI, DevAddr, etc)
ullength integerset payload length of test uplink packets

sensor demo

Selected grove sensors may be plugged into SX1272 shield.
To enable, edit lora_config.h to define SENSORS.

Sensor connections on SX1272MB2xAS:

D8 D9: buttonRX TX: (unused)A3 A4: Rotary Angle Sensor
D6 D7: RGB LEDSCL SDA: digital light sensorA1 A2: Rotary Angle Sensor

Digital input pin, state reported via uplink: PC8
Digital output pin, controlled via downlink: PC6
PWM out: PB_10

Jumper enables auto-repeated transmit: PC10 and PC12 on NUCLEO board, located on end of morpho headers nearby JP4.

Revision:
34:9c8966cd66a2
Parent:
33:bc0b6be19e07
--- a/mac/LoRaMacSingle.cpp	Wed Dec 19 13:56:04 2018 -0800
+++ b/mac/LoRaMacSingle.cpp	Fri Jul 10 11:12:59 2020 -0700
@@ -1,21 +1,5 @@
-/*
- / _____)             _              | |
-( (____  _____ ____ _| |_ _____  ____| |__
- \____ \| ___ |    (_   _) ___ |/ ___)  _ \
- _____) ) ____| | | || |_| ____( (___| | | |
-(______/|_____)_|_|_| \__)_____)\____)_| |_|
-    (C)2013 Semtech
- ___ _____ _   ___ _  _____ ___  ___  ___ ___
-/ __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
-\__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
-|___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
-embedded.connectivity.solutions===============
-
-Description: LoRa MAC layer implementation
-
-License: Revised BSD License, see LICENSE.TXT file include in the project
-
-*/
+/* */
+
 #include <math.h>
 #include "board.h"
 #include "radio.h"
@@ -23,9 +7,7 @@
 #include "LoRaMacCrypto.h"
 #include "LoRaMacSingle.h"
 #include "LoRaMacTest.h"
-#include "LowPowerTimeoutAbs.h"
-
-//DigitalOut pc2(PC_2);
+
 #define BEACONS_MISSED_LIMIT            16
 
 #define PING_SLOT_RESOLUTION_us         30000
@@ -45,10 +27,7 @@
  */
 #define LORA_MAC_COMMAND_MAX_LENGTH                 15
 
-/*! radio timer started later by this much: */
-static volatile int lpt_offset;
-
-static volatile us_timestamp_t txDoneAt;
+LowPowerClock::time_point txDoneAt;
 
 /*!
  * Device IEEE EUI
@@ -126,17 +105,16 @@
 static uint32_t DownLinkCounter = 0;
 
 /*!
- * IsPacketCounterFixed enables the MIC field tests by fixing the
- * UpLinkCounter value
- */
-//static bool IsUpLinkCounterFixed = false;
-
-/*!
  * Used for test purposes. Disables the opening of the reception windows.
  */
 static bool IsRxWindowsEnabled = true;
-LowPowerTimeoutAbs _rx_timeout;
-volatile us_timestamp_t _rx_timeout_setAt;
+
+LowPowerTimeout rx_timeout;
+
+/* how long this MCU (we're running on) takes to wake-up from deep-sleep */
+microseconds mcu_wakeup_latency;
+
+LowPowerClock::time_point rx_timeout_setAt;
 
 /*!
  * Indicates if the MAC layer wants to send MAC commands
@@ -187,12 +165,6 @@
 
     #define LORA_BANDWIDTH_KHZ           200
 
-/*
-#define STR_HELPER(x) #x
-#define STR(x) STR_HELPER(x)
-#pragma message "content of BEACON_RXDONE_LATENCY_us: " STR(BEACON_RXDONE_LATENCY_us)
-*/
-
     /* end sx1280 */
 #elif defined( USE_BAND_915_SINGLE )
     /*!
@@ -216,12 +188,6 @@
     #define LORA_MAX_NB_CHANNELS                        8
 
     #define LORA_BANDWIDTH_KHZ           500
-/*
-#define STR_HELPER(x) #x
-#define STR(x) STR_HELPER(x)
-#pragma message "content of BEACON_RXDONE_LATENCY_us: " STR(BEACON_RXDONE_LATENCY_us)
-#pragma message "content of BEACON_TOA_us: " STR(BEACON_TOA_us)
-*/
 
     /* end us915 */
 #elif defined(USE_BAND_433)
@@ -309,14 +275,14 @@
 /*!
  * LoRaMac duty cycle delayed Tx timer
  */
-LowPowerTimeoutAbs _tx_timeout;
+LowPowerTimeout tx_timeout;
 
 /*!
  * LoRaMac reception windows delay
  * \remark normal frame: RxWindowXDelay = ReceiveDelayX - RADIO_WAKEUP_TIME
  *         join frame  : RxWindowXDelay = JoinAcceptDelayX - RADIO_WAKEUP_TIME
  */
-static uint32_t RxWindowDelay_us;
+microseconds RxWindowDelay_us;
 
 typedef enum {
     BEACON_STATE_NONE = 0,
@@ -327,10 +293,10 @@
 
 static struct beacon_struct {
     int rx_precession_us; // positive: rxing before tx start, negative: rxing after tx start
-    us_timestamp_t rx_setup_at;
-    us_timestamp_t LastBeaconRx_us;   // updated only at beacon reception
-    us_timestamp_t sendAt;
-    int lastSendAtErr;
+    LowPowerClock::time_point rx_setup_at;
+    LowPowerClock::time_point LastBeaconRxAt;   // was LastBeaconRx_us, updated only at beacon reception
+    LowPowerClock::time_point sendAt;
+    microseconds lastSendAtErr;
     int last_BeaconRxTimerError_us;
     int known_working_BeaconRxTimerError_us;
 
@@ -349,8 +315,8 @@
     uint32_t beaconStartToRxDone;
     uint16_t sendOpportunities;
 
-    LowPowerTimeoutAbs _timeout_rx;
-    LowPowerTimeoutAbs _timeout_guard;
+    LowPowerTimeout timeout_rx;
+    LowPowerTimeout timeout_guard;
     bool guard;
 } BeaconCtx;
 
@@ -535,26 +501,26 @@
 #ifdef SX127x_H
 void printLoraIrqs(bool clear)
 {
-    pc.printf("\r\nIrqFlags:");
+    pc_printf("\r\nIrqFlags:");
     if (Radio::lora.RegIrqFlags.bits.CadDetected)
-        pc.printf("CadDetected ");
+        pc_printf("CadDetected ");
     if (Radio::lora.RegIrqFlags.bits.FhssChangeChannel) {
-        pc.printf("FhssChangeChannel:%d ", Radio::lora.RegHopChannel.bits.FhssPresentChannel);
+        pc_printf("FhssChangeChannel:%d ", Radio::lora.RegHopChannel.bits.FhssPresentChannel);
     }
     if (Radio::lora.RegIrqFlags.bits.CadDone)
-        pc.printf("CadDone ");
+        pc_printf("CadDone ");
     if (Radio::lora.RegIrqFlags.bits.TxDone)
-        pc.printf("TxDone-dio0:%d ", Radio::radio.dio0.read());
+        pc_printf("TxDone-dio0:%d ", Radio::radio.dio0.read());
     if (Radio::lora.RegIrqFlags.bits.ValidHeader)
-        pc.printf("ValidHeader ");
+        pc_printf("ValidHeader ");
     if (Radio::lora.RegIrqFlags.bits.PayloadCrcError)
-        pc.printf("PayloadCrcError ");
+        pc_printf("PayloadCrcError ");
     if (Radio::lora.RegIrqFlags.bits.RxDone)
-        pc.printf("RxDone ");  
+        pc_printf("RxDone ");  
     if (Radio::lora.RegIrqFlags.bits.RxTimeout)
-        pc.printf("RxTimeout ");
-
-    pc.printf("\r\n");
+        pc_printf("RxTimeout ");
+
+    pc_printf("\r\n");
 
     if (clear)
         Radio::radio.write_reg(REG_LR_IRQFLAGS, Radio::lora.RegIrqFlags.octet);
@@ -564,23 +530,23 @@
 {
     Radio::radio.RegOpMode.octet = Radio::radio.read_reg(REG_OPMODE);
     switch (Radio::radio.RegOpMode.bits.Mode) {
-        case RF_OPMODE_SLEEP: pc.printf("sleep"); break;
-        case RF_OPMODE_STANDBY: pc.printf("stby"); break;
-        case RF_OPMODE_SYNTHESIZER_TX: pc.printf("fstx"); break;
-        case RF_OPMODE_TRANSMITTER: pc.printf("tx"); break;
-        case RF_OPMODE_SYNTHESIZER_RX: pc.printf("fsrx"); break;
-        case RF_OPMODE_RECEIVER: pc.printf("rx"); break;
+        case RF_OPMODE_SLEEP: pc_printf("sleep"); break;
+        case RF_OPMODE_STANDBY: pc_printf("stby"); break;
+        case RF_OPMODE_SYNTHESIZER_TX: pc_printf("fstx"); break;
+        case RF_OPMODE_TRANSMITTER: pc_printf("tx"); break;
+        case RF_OPMODE_SYNTHESIZER_RX: pc_printf("fsrx"); break;
+        case RF_OPMODE_RECEIVER: pc_printf("rx"); break;
         case 6:
             if (Radio::radio.RegOpMode.bits.LongRangeMode)
-                pc.printf("rxs");
+                pc_printf("rxs");
             else
-                pc.printf("-6-");
+                pc_printf("-6-");
             break;  // todo: different lora/fsk
         case 7:
             if (Radio::radio.RegOpMode.bits.LongRangeMode)
-                pc.printf("cad");
+                pc_printf("cad");
             else
-                pc.printf("-7-");
+                pc_printf("-7-");
             break;  // todo: different lora/fsk
     }
 }
@@ -594,7 +560,7 @@
 void
 loramac_print_status()
 {
-    int until_beacon = BeaconCtx.rx_setup_at - BeaconCtx._timeout_rx.read_us();
+    int until_beacon = BeaconCtx.rx_setup_at.time_since_epoch().count() - LowPowerClock::now().time_since_epoch().count();
     mac_printf("until_beacon:%d ", until_beacon);
     mac_printf("DR%u=sf%u guard:%d\r\n",
         LoRaMacParams.ChannelsDatarate_fixed,
@@ -605,40 +571,40 @@
     status_t status;
     Radio::radio.xfer(OPCODE_GET_STATUS, 0, 1, &status.octet);
     switch (status.bits.cmdStatus) {
-        case 1: pc.printf("success"); break;
-        case 2: pc.printf("dataAvail"); break;
-        case 3: pc.printf("cmdTimeout"); break;
-        case 4: pc.printf("cmdErr"); break;
-        case 5: pc.printf("exeFail"); break;
-        case 6: pc.printf("txdone"); break;
-        default: pc.printf("cmdStatus:<%u>", status.bits.cmdStatus); break;
+        case 1: pc_printf("success"); break;
+        case 2: pc_printf("dataAvail"); break;
+        case 3: pc_printf("cmdTimeout"); break;
+        case 4: pc_printf("cmdErr"); break;
+        case 5: pc_printf("exeFail"); break;
+        case 6: pc_printf("txdone"); break;
+        default: pc_printf("cmdStatus:<%u>", status.bits.cmdStatus); break;
     }
-    pc.printf(" ");
+    pc_printf(" ");
     switch (status.bits.chipMode) {
-        case 2: pc.printf("stdby_rc"); break;
-        case 3: pc.printf("stdby_xosc"); break;
-        case 4: pc.printf("fs"); break;
-        case 5: pc.printf("\e[32mrx\e[0m"); break;
-        case 6: pc.printf("\e[31mtx\e[0m"); break;
-        default: pc.printf("chipMode:<%u>", status.bits.chipMode); break;
+        case 2: pc_printf("stdby_rc"); break;
+        case 3: pc_printf("stdby_xosc"); break;
+        case 4: pc_printf("fs"); break;
+        case 5: pc_printf("\e[32mrx\e[0m"); break;
+        case 6: pc_printf("\e[31mtx\e[0m"); break;
+        default: pc_printf("chipMode:<%u>", status.bits.chipMode); break;
     }
     LoRaPktPar0_t LoRaPktPar0; 
     LoRaPktPar0.octet = Radio::radio.readReg(REG_ADDR_LORA_PKTPAR0, 1);
-    pc.printf(" bw:%u sf%u ", LoRaPktPar0.bits.modem_bw, LoRaPktPar0.bits.modem_sf);
+    pc_printf(" bw:%u sf%u ", LoRaPktPar0.bits.modem_bw, LoRaPktPar0.bits.modem_sf);
     mac_printf("loraSync:%04x\r\n", Radio::radio.readReg(REG_ADDR_LORA_SYNC, 2));
 #elif defined(SX127x_H)
     Radio::radio.RegPaConfig.octet = Radio::radio.read_reg(REG_PACONFIG);
     if (Radio::radio.RegPaConfig.bits.PaSelect)
-        pc.printf("PA_BOOST ");
+        pc_printf("PA_BOOST ");
     else
-        pc.printf("RFO ");
+        pc_printf("RFO ");
 
     Radio::radio.RegOpMode.octet = Radio::radio.read_reg(REG_OPMODE);
-    pc.printf("%.3fMHz sf%ubw%u ", Radio::radio.get_frf_MHz(), Radio::lora.getSf(), Radio::lora.getBw());
-    pc.printf("dio0pin:%u ", Radio::radio.dio0.read());
+    pc_printf("%.3fMHz sf%ubw%u ", Radio::radio.get_frf_MHz(), Radio::lora.getSf(), Radio::lora.getBw());
+    pc_printf("dio0pin:%u ", Radio::radio.dio0.read());
     printOpMode();
     if (!Radio::radio.RegOpMode.bits.LongRangeMode) {
-        pc.printf("FSK\r\n");
+        pc_printf("FSK\r\n");
         return;
     }
 
@@ -647,10 +613,10 @@
 
     Radio::lora.RegTest33.octet = Radio::radio.read_reg(REG_LR_TEST33);     // invert_i_q
     Radio::lora.RegDriftInvert.octet = Radio::radio.read_reg(REG_LR_DRIFT_INVERT);
-    pc.printf("modemstat:%02x, rxinv:%x,%x\r\n", Radio::radio.read_reg(REG_LR_MODEMSTAT), Radio::lora.RegTest33.octet, Radio::lora.RegDriftInvert.octet);
+    pc_printf("modemstat:%02x, rxinv:%x,%x\r\n", Radio::radio.read_reg(REG_LR_MODEMSTAT), Radio::lora.RegTest33.octet, Radio::lora.RegDriftInvert.octet);
     Radio::radio.RegDioMapping1.octet = Radio::radio.read_reg(REG_DIOMAPPING1);
-    pc.printf("\r\ndio0map:%u\r\n", Radio::radio.RegDioMapping1.bits.Dio0Mapping);
-    pc.printf("FIfoAddrPtr:%02x RxBase:%02x\r\n", Radio::radio.read_reg(REG_LR_FIFOADDRPTR), Radio::radio.read_reg(REG_LR_FIFORXBASEADDR));
+    pc_printf("\r\ndio0map:%u\r\n", Radio::radio.RegDioMapping1.bits.Dio0Mapping);
+    pc_printf("FIfoAddrPtr:%02x RxBase:%02x\r\n", Radio::radio.read_reg(REG_LR_FIFOADDRPTR), Radio::radio.read_reg(REG_LR_FIFORXBASEADDR));
 #elif defined(SX126x_H)
     status_t status;
     Radio::radio.xfer(OPCODE_GET_STATUS, 0, 1, &status.octet);
@@ -662,7 +628,7 @@
         case 6: mac_printf("TX"); break;
         default: mac_printf("%u", status.bits.chipMode); break;
     }
-    pc.printf(" ");
+    pc_printf(" ");
     switch (status.bits.cmdStatus) {
         case 1: mac_printf("rfu"); break;
         case 2: mac_printf("dataAvail"); break;
@@ -703,19 +669,17 @@
 
 static void OnRxWindowTimerEvent( void )
 {
-    int diff = _rx_timeout_setAt - _rx_timeout.read_us();
     RxWindowSetup(Channels[Channel].Frequency, RxWindowsParam.Datarate, RxWindowsParam.RxWindowTimeout, false);
     Radio::Rx( LoRaMacParams.MaxRxWindow );
-    //mac_printf("rxwinTo:%u\r\n", RxWindowsParam.RxWindowTimeout);
 }
 
 static RxConfigParams_t ComputeRxWindowParameters( int8_t datarate, uint32_t rxError );
 
-volatile us_timestamp_t rxto_irqAt;
+LowPowerClock::time_point rxto_irqAt;
 
 static void OnRadioTxDone_topHalf()
 {
-    rxto_irqAt = _rx_timeout.read_us();
+    rxto_irqAt = LowPowerClock::now();
 }
 
 static void OnRadioTxDone_bh()
@@ -723,11 +687,13 @@
     // Setup timers
     if (IsRxWindowsEnabled)
     {
-        _rx_timeout_setAt = rxto_irqAt + RxWindowDelay_us;
-        _rx_timeout.attach_us(&OnRxWindowTimerEvent, _rx_timeout_setAt);
+        rx_timeout_setAt = rxto_irqAt + RxWindowDelay_us;
+        rx_timeout.attach_absolute(&OnRxWindowTimerEvent, rx_timeout_setAt);
         if (LoRaMacFlags.Bits.NodeAckRequested)
         {
-            AckTimeoutTimer.attach_us(&OnAckTimeoutTimerEvent, (RxWindowDelay_us/1000) + ACK_TIMEOUT_us + randr(-ACK_TIMEOUT_RND_us, ACK_TIMEOUT_RND_us));
+            microseconds us(ACK_TIMEOUT_us + randr(-ACK_TIMEOUT_RND_us, ACK_TIMEOUT_RND_us));
+            us += RxWindowDelay_us;
+            AckTimeoutTimer.attach(&OnAckTimeoutTimerEvent, us - mcu_wakeup_latency);
         }
     }
     else
@@ -754,7 +720,7 @@
 
     Radio::Standby( );
 
-    txDoneAt = Radio::irqAt + lpt_offset;
+    txDoneAt = Radio::irqAt /*+ lpt_offset*/;
 }
 
 
@@ -810,17 +776,19 @@
     LoRaMacFlags.Bits.uplink_pending = 0;   // sent
 
     // Compute Rx1 windows parameters, taken at TxDone
-    if (!LoRaMacFlags.Bits.IsLoRaMacNetworkJoined)
-        RxWindowDelay_us = LoRaMacParams.JoinAcceptDelay_us - TARGET_PRECESSION_us;
-    else
-        RxWindowDelay_us = LoRaMacParams.ReceiveDelay_us - TARGET_PRECESSION_us;
+    if (!LoRaMacFlags.Bits.IsLoRaMacNetworkJoined) {
+        microseconds us(LoRaMacParams.JoinAcceptDelay_us - TARGET_PRECESSION_us);
+        RxWindowDelay_us = us - mcu_wakeup_latency;
+    } else {
+        microseconds us(LoRaMacParams.ReceiveDelay_us - TARGET_PRECESSION_us);
+        RxWindowDelay_us = us - mcu_wakeup_latency;
+    }
 
 } // ..send_bh()
 
 void send_callback()
 {
-    int err = 0;
-    us_timestamp_t now = BeaconCtx.sendAt + BeaconCtx.lastSendAtErr;
+    LowPowerClock::time_point now = BeaconCtx.sendAt + BeaconCtx.lastSendAtErr;
 
     if (BeaconCtx.guard) {
         /* last send, this will be restarted after beacon sent */
@@ -828,17 +796,22 @@
     }
     BeaconCtx.sendOpportunities++;
 
-    BeaconCtx.sendAt += BeaconCtx.periodicity_slots * PING_SLOT_RESOLUTION_us;
+    microseconds ping_slot_us(BeaconCtx.periodicity_slots * PING_SLOT_RESOLUTION_us);
+    BeaconCtx.sendAt += ping_slot_us;
     if (BeaconCtx.state == BEACON_STATE_LOCKED) {
-        float sinceBeacon = now - BeaconCtx.LastBeaconRx_us;
+        float sinceBeacon = now.time_since_epoch().count() - BeaconCtx.LastBeaconRxAt.time_since_epoch().count();
         float ratio = sinceBeacon / BEACON_INTERVAL_us;
-        err = BeaconCtx.last_BeaconRxTimerError_us * ratio;
+        microseconds duration_err_us((int)(BeaconCtx.last_BeaconRxTimerError_us * ratio));
+        tx_timeout.attach_absolute(&send_callback, BeaconCtx.sendAt + duration_err_us - mcu_wakeup_latency);
+        BeaconCtx.lastSendAtErr = duration_err_us;
+    } else {
+        microseconds duration_err_us(0);
+        tx_timeout.attach_absolute(&send_callback, BeaconCtx.sendAt - mcu_wakeup_latency);
+        BeaconCtx.lastSendAtErr = duration_err_us;
     }
-    _tx_timeout.attach_us(&send_callback, BeaconCtx.sendAt + err);
-    BeaconCtx.lastSendAtErr = err;
 
     if (LoRaMacFlags.Bits.uplink_pending) {
-        us_timestamp_t untilGuard = (BeaconCtx.rx_setup_at - BEACON_GUARD_us) - BeaconCtx._timeout_guard.read_us();
+        us_timestamp_t untilGuard = (BeaconCtx.rx_setup_at.time_since_epoch().count() - BEACON_GUARD_us) - LowPowerClock::now().time_since_epoch().count();
         if (untilGuard > (RECEIVE_DELAY_us + TARGET_PRECESSION_us))
             LoRaMacFlags.Bits.send = 1;
     }
@@ -851,7 +824,7 @@
 
     Radio::Rx(2000);
 
-    BeaconCtx.lastSendAtErr = 0;
+    BeaconCtx.lastSendAtErr = microseconds(0);
 }
 
 void guard_callback()
@@ -860,7 +833,7 @@
     Radio::Standby( );
     Radio::SetChannel( Channels[Channel].Frequency );
 
-    _tx_timeout.detach();
+    tx_timeout.detach();
     mac_printf("sendOpportunities:%u\r\n", BeaconCtx.sendOpportunities);
     BeaconCtx.sendOpportunities = 0;
 
@@ -918,14 +891,16 @@
 {
     static bool compensate_precession = false;
     int32_t compensation = 0;
-    us_timestamp_t ThisBeaconRx_us = Radio::irqAt + lpt_offset - BeaconCtx.beaconStartToRxDone;
+    /* have beacon end, need beacon start */
+    microseconds beacon_start_offset_us(BeaconCtx.beaconStartToRxDone);
+    LowPowerClock::time_point ThisBeaconRxAt = Radio::irqAt - beacon_start_offset_us;
 
     BeaconCtx.num_consecutive_ok++;
-    mac_printf("rx_beacon %llu ", Radio::irqAt);
-    BeaconCtx.rx_precession_us = ThisBeaconRx_us - BeaconCtx.rx_setup_at;
+    mac_printf("rx_beacon %llu ", Radio::irqAt.time_since_epoch().count());
+    BeaconCtx.rx_precession_us = ThisBeaconRxAt.time_since_epoch().count() - BeaconCtx.rx_setup_at.time_since_epoch().count();
     if (BeaconCtx.state != BEACON_STATE_FIRST_ACQ) {
         BeaconCtx.known_working_BeaconRxTimerError_us = BeaconCtx.last_BeaconRxTimerError_us;
-        BeaconCtx.last_BeaconRxTimerError_us = (ThisBeaconRx_us - BeaconCtx.LastBeaconRx_us) - (BEACON_INTERVAL_us * (BeaconCtx.num_missed+1));
+        BeaconCtx.last_BeaconRxTimerError_us = (ThisBeaconRxAt.time_since_epoch().count() - BeaconCtx.LastBeaconRxAt.time_since_epoch().count()) - (BEACON_INTERVAL_us * (BeaconCtx.num_missed+1));
 
         if (BeaconCtx.num_missed > 0) {
             /* Timer error is measured over more than one beacon period.
@@ -945,18 +920,18 @@
         BeaconCtx.state = BEACON_STATE_ACQ_ERROR;
     }
 
-    mac_printf("err%d=%llu-%llu ", BeaconCtx.last_BeaconRxTimerError_us, ThisBeaconRx_us, BeaconCtx.LastBeaconRx_us);
+    mac_printf("err%d=%llu-%llu ", BeaconCtx.last_BeaconRxTimerError_us, ThisBeaconRxAt.time_since_epoch().count(), BeaconCtx.LastBeaconRxAt.time_since_epoch().count());
     if (BeaconCtx.num_missed > 0)
         mac_printf("missed%u ", BeaconCtx.num_missed);
 
     mac_printf(" rx-before-tx:%d ", BeaconCtx.rx_precession_us);
     if (BeaconCtx.last_BeaconRxTimerError_us > 40000 || BeaconCtx.last_BeaconRxTimerError_us < -40000) {
-        BeaconCtx._timeout_rx.detach();
-        BeaconCtx._timeout_guard.detach();
+        BeaconCtx.timeout_rx.detach();
+        BeaconCtx.timeout_guard.detach();
         mac_printf("halt\r\n");
         for (;;) asm("nop");
     }
-    BeaconCtx.LastBeaconRx_us = ThisBeaconRx_us;
+    BeaconCtx.LastBeaconRxAt = ThisBeaconRxAt;
 
     if (BeaconCtx.state == BEACON_STATE_LOCKED) {
         if (compensate_precession) {
@@ -966,16 +941,17 @@
     }
 
     // reference tick for uplink schedule: when gateway started beacon
-    BeaconCtx.sendAt = BeaconCtx.rx_setup_at + BeaconCtx.rx_precession_us;
-    BeaconCtx.sendAt += BeaconCtx.tx_slot_offset * PING_SLOT_RESOLUTION_us;
-    _tx_timeout.attach_us(&send_callback, BeaconCtx.sendAt);
-    mac_printf("sendAt:%llu ", BeaconCtx.sendAt);
-
-    BeaconCtx.rx_setup_at += BEACON_INTERVAL_us;
-    BeaconCtx.rx_setup_at += compensation;
-
-    BeaconCtx._timeout_rx.attach_us(&OnRxBeaconSetup, BeaconCtx.rx_setup_at);
-    BeaconCtx._timeout_guard.attach_us(&guard_callback, BeaconCtx.rx_setup_at - BEACON_GUARD_us);
+    microseconds send_offset_us(BeaconCtx.rx_precession_us + (BeaconCtx.tx_slot_offset * PING_SLOT_RESOLUTION_us));
+    BeaconCtx.sendAt = BeaconCtx.rx_setup_at + send_offset_us;
+    tx_timeout.attach_absolute(&send_callback, BeaconCtx.sendAt - mcu_wakeup_latency);
+    mac_printf("sendAt:%llu ", BeaconCtx.sendAt.time_since_epoch().count());
+
+    microseconds us_to_next_beacon(BEACON_INTERVAL_us + compensation);
+    BeaconCtx.rx_setup_at += us_to_next_beacon;
+
+    BeaconCtx.timeout_rx.attach_absolute(&OnRxBeaconSetup, BeaconCtx.rx_setup_at - mcu_wakeup_latency);
+    microseconds beacon_guard_us(BEACON_GUARD_us);
+    BeaconCtx.timeout_guard.attach_absolute(&guard_callback, BeaconCtx.rx_setup_at - beacon_guard_us - mcu_wakeup_latency);
 
     BeaconCtx.num_missed = 0;
 
@@ -993,7 +969,6 @@
         rx |= Radio::radio.rx_buf[2] << 16;
         rx |= Radio::radio.rx_buf[3] << 24;
         if (rx != 0) {
-            //mac_printf("beacon payload:%08x\r\n", rx);
             McpsIndication.McpsIndication = MCPS_MULTICAST;
             McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_OK;
             McpsIndication.Buffer = Radio::radio.rx_buf;
@@ -1050,7 +1025,6 @@
 
     Radio::Sleep( );
 
-    //mac_printf("OnRadioRxDone %u", size);
     if (LoRaMacFlags.Bits.expecting_beacon) {
         rx_beacon(size);
         LoRaMacFlags.Bits.expecting_beacon = false;
@@ -1058,7 +1032,6 @@
     }
 
     macHdr.Value = Radio::radio.rx_buf[pktHeaderLen++];
-    //mac_printf("mtype:%x\r\n", macHdr.Bits.MType);
 
     switch( macHdr.Bits.MType )
     {
@@ -1111,11 +1084,13 @@
                     unsigned us_to_beacon = ( PING_SLOT_RESOLUTION_us * beaconTimingDelay );
                     mac_printf(" us_to_beacon:%u ", us_to_beacon);
                     // time to beacon given as referenced to end of join request uplink
-                    BeaconCtx.rx_setup_at = txDoneAt + us_to_beacon - PPM_BEACON_INTERVAL;
-                    BeaconCtx._timeout_rx.attach_us(&OnRxBeaconSetup, BeaconCtx.rx_setup_at);
-                    BeaconCtx._timeout_guard.attach_us(&guard_callback, BeaconCtx.rx_setup_at - BEACON_GUARD_us);
-
-                    mac_printf("beaconIn:%llu\r\n", BeaconCtx.rx_setup_at - BeaconCtx._timeout_rx.read_us());
+                    microseconds tx_done_to_rx_start_us(us_to_beacon - PPM_BEACON_INTERVAL);
+                    BeaconCtx.rx_setup_at = txDoneAt + tx_done_to_rx_start_us;
+                    BeaconCtx.timeout_rx.attach_absolute(&OnRxBeaconSetup, BeaconCtx.rx_setup_at - mcu_wakeup_latency);
+                    microseconds beacon_guard_us(BEACON_GUARD_us);
+                    BeaconCtx.timeout_guard.attach_absolute(&guard_callback, BeaconCtx.rx_setup_at - (beacon_guard_us + mcu_wakeup_latency));
+
+                    mac_printf("beaconIn:%llu\r\n", BeaconCtx.rx_setup_at.time_since_epoch().count() - LowPowerClock::now().time_since_epoch().count());
                 }
 
                 BeaconCtx.tx_slot_offset = _jaDecrypted[15];
@@ -1134,17 +1109,20 @@
 
 
                 /* nowSlot: now vs previous beacon */
-                BeaconCtx.LastBeaconRx_us = BeaconCtx.rx_setup_at - BEACON_INTERVAL_us;
-                unsigned us_since_last_beacon = _rx_timeout.read_us() - BeaconCtx.LastBeaconRx_us;
+                microseconds beacon_interval_us(BEACON_INTERVAL_us);
+                BeaconCtx.LastBeaconRxAt = BeaconCtx.rx_setup_at - beacon_interval_us;
+                unsigned us_since_last_beacon = LowPowerClock::now().time_since_epoch().count() - BeaconCtx.LastBeaconRxAt.time_since_epoch().count();
                 unsigned nowSlot = us_since_last_beacon / PING_SLOT_RESOLUTION_us;
                 unsigned useSlot = BeaconCtx.tx_slot_offset;
                 while (useSlot < nowSlot)
                     useSlot += BeaconCtx.periodicity_slots;
 
-                mac_printf("beaconDur:0x%x (%u) useSlot:%u nowSlot:%u ", beaconDur, BeaconCtx.beaconStartToRxDone, useSlot, nowSlot);
-                BeaconCtx.sendAt = BeaconCtx.LastBeaconRx_us + (useSlot * PING_SLOT_RESOLUTION_us);
-                mac_printf("sendIn:%u\r\n", BeaconCtx.sendAt - _tx_timeout.read_us());
-                _tx_timeout.attach_us(send_callback, BeaconCtx.sendAt);
+                mac_printf("beaconDur:0x%lx (%lu) useSlot:%u nowSlot:%u ", beaconDur, BeaconCtx.beaconStartToRxDone, useSlot, nowSlot);
+                microseconds ping_slot_offset_us(useSlot * PING_SLOT_RESOLUTION_us);
+                BeaconCtx.sendAt = BeaconCtx.LastBeaconRxAt + ping_slot_offset_us;
+
+                mac_printf("sendIn:%lld\r\n", BeaconCtx.sendAt.time_since_epoch().count() - LowPowerClock::now().time_since_epoch().count());
+                tx_timeout.attach_absolute(send_callback, BeaconCtx.sendAt - mcu_wakeup_latency);
                 BeaconCtx.sendOpportunities = 0;
 
                 BeaconCtx.state = BEACON_STATE_FIRST_ACQ;
@@ -1446,7 +1424,7 @@
         default:
             McpsIndication.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_RX_MTYPE;
             McpsConfirm.Status = LORAMAC_EVENT_INFO_STATUS_ERROR_RX_MTYPE;
-            mac_printf("%d:macHdr:%02x(%d,%d) ", size, macHdr.Value, rssi, snr);
+            mac_printf("%d:macHdr:%02x(%f,%f) ", size, macHdr.Value, rssi, snr);
             PrepareRxDoneAbort( );
             break;
     }
@@ -1526,18 +1504,22 @@
 {
     Radio::Sleep( );
 
+    pc_printf("OnRadioRxTimeout eb%u\r\n", LoRaMacFlags.Bits.expecting_beacon);
     if (LoRaMacFlags.Bits.expecting_beacon) {
         float ourErrSecs = BeaconCtx.known_working_BeaconRxTimerError_us / 1000000.0;
 
         LoRaMacFlags.Bits.expecting_beacon = false;
 
-        BeaconCtx.rx_setup_at += BEACON_INTERVAL_us + BeaconCtx.known_working_BeaconRxTimerError_us;
-        BeaconCtx.rx_setup_at -= (PPM_BEACON_INTERVAL / 4);
+        microseconds rx_next_us(BEACON_INTERVAL_us + BeaconCtx.known_working_BeaconRxTimerError_us);
+        BeaconCtx.rx_setup_at += rx_next_us;
+        microseconds pre_rx_us(PPM_BEACON_INTERVAL / 4); // come up early when missed
+        BeaconCtx.rx_setup_at -= pre_rx_us;
         us_to_nSymbTimeout(BeaconCtx.SymbolTimeout_us + PPM_BEACON_INTERVAL);
         mac_printf("beacon timeout ourErr:%f SymbTo:%u(%uus)\r\n", ourErrSecs, BeaconCtx.nSymbsTimeout, BeaconCtx.SymbolTimeout_us);
 
-        BeaconCtx._timeout_rx.attach_us(&OnRxBeaconSetup, BeaconCtx.rx_setup_at);
-        BeaconCtx._timeout_guard.attach_us(&guard_callback, BeaconCtx.rx_setup_at - BEACON_GUARD_us);
+        BeaconCtx.timeout_rx.attach_absolute(&OnRxBeaconSetup, BeaconCtx.rx_setup_at - mcu_wakeup_latency);
+        microseconds guard_us(BEACON_GUARD_us);
+        BeaconCtx.timeout_guard.attach_absolute(&guard_callback, BeaconCtx.rx_setup_at - (guard_us + mcu_wakeup_latency));
 
         if (++BeaconCtx.num_missed > BEACONS_MISSED_LIMIT) {
             LoRaMacFlags.Bits.reJoin = 1;
@@ -1547,15 +1529,18 @@
             LoRaMacPrimitives->MacMlmeIndication( &MlmeIndication );
         }
 
-        BeaconCtx.sendAt = BeaconCtx.LastBeaconRx_us + ((BEACON_INTERVAL_us + BeaconCtx.known_working_BeaconRxTimerError_us) * BeaconCtx.num_missed);
-        BeaconCtx.sendAt += BeaconCtx.tx_slot_offset * PING_SLOT_RESOLUTION_us;
-        _tx_timeout.attach_us(&send_callback, BeaconCtx.sendAt);
+        microseconds base((BEACON_INTERVAL_us + BeaconCtx.known_working_BeaconRxTimerError_us) * BeaconCtx.num_missed);
+        BeaconCtx.sendAt = BeaconCtx.LastBeaconRxAt + base;
+        microseconds tx_slot_us(BeaconCtx.tx_slot_offset * PING_SLOT_RESOLUTION_us);
+        BeaconCtx.sendAt += tx_slot_us;
+        tx_timeout.attach_absolute(&send_callback, BeaconCtx.sendAt - mcu_wakeup_latency);
 
         BeaconCtx.num_consecutive_ok = 0;
     } else {
         if (LoRaMacFlags.Bits.MlmeReq && ( MlmeConfirm.MlmeRequest == MLME_JOIN )) {
             /* no join accept received: join retry */
-            _tx_timeout.attach_us(&join_send, _tx_timeout.read_us() + (JoinRequestTrials*20000) + randr(0, 70000));
+            microseconds us((JoinRequestTrials*20000) + randr(0, 70000));
+            tx_timeout.attach(&join_send, us - mcu_wakeup_latency);
         }
     }
 
@@ -1953,9 +1938,8 @@
     mac_printf("second\r\n");
 }
 
-void on_dio0_top_half(us_timestamp_t dio0_at)
+void on_dio0_top_half()
 {
-    mac_printf("dio0th\r\n");
 }
 
 unsigned get_symbol_period_us(uint8_t sf)
@@ -1977,9 +1961,17 @@
     /* CadDone  */          NULL
 };
 
+osThreadId_t tid_main;
+
+void sleep_test_callback()
+{
+    txDoneAt = LowPowerClock::now();
+    osThreadFlagsSet(tid_main, 1);
+}
+
 LoRaMacStatus_t LoRaMacInitialization( LoRaMacPrimitives_t *primitives, LoRaMacCallback_t *callbacks )
 {
-    if( primitives == NULL )
+    if (primitives == NULL)
     {
         return LORAMAC_STATUS_PARAMETER_INVALID;
     }
@@ -2034,12 +2026,34 @@
     LoRaMacParams.ChannelsNbRep = LoRaMacParamsDefaults.ChannelsNbRep;
 
     ResetMacParameters( );
+    if (LoRaMacCryptoInit() < 0) {
+        return LORAMAC_STATUS_SERVICE_UNKNOWN;
+    }
 
     // Initialize Radio driver
     Radio::Init(&rev);
-    // lpt started later.  lpt will have smaller number.
-    lpt_offset = _rx_timeout.read_us() - Radio::lpt.read_us();
-    mac_printf("lpt_offset:%d\r\n", lpt_offset);
+
+    {
+        unsigned sum = 0;
+        sleep_manager_unlock_deep_sleep();
+        tid_main = ThisThread::get_id();
+        /*** How long this MCU takes to wake up from deep-sleep ***/
+        for (unsigned n = 0; n < 16; n++) {
+            long end, start;
+            txDoneAt = LowPowerClock::now();
+            start = txDoneAt.time_since_epoch().count();
+
+            LoRaMacFlags.Value = 0;
+            rx_timeout.attach(sleep_test_callback, 10ms);
+            ThisThread::flags_wait_any(1);
+            end = LowPowerClock::now().time_since_epoch().count();
+            sum += (end - start) - 10000;
+        }
+        LoRaMacFlags.Value = 0;
+        sum >>= 4;  // 16 samples = 2^4
+        printf("mcu takes %uus to wake up\r\n", sum);
+        mcu_wakeup_latency = microseconds(sum);
+    }
 
     // Random seed initialization
     srand1( Radio::Random( ) );
@@ -2186,8 +2200,8 @@
             LoRaMacFlags.Bits.IsLoRaMacNetworkJoined = mibSet->Param.IsNetworkJoined;
             if (!LoRaMacFlags.Bits.IsLoRaMacNetworkJoined) {
                 mac_printf("beaconDetach\r\n");
-                BeaconCtx._timeout_rx.detach();
-                BeaconCtx._timeout_guard.detach();
+                BeaconCtx.timeout_rx.detach();
+                BeaconCtx.timeout_guard.detach();
                 BeaconCtx.state = BEACON_STATE_NONE;
             }
             break;
@@ -2572,7 +2586,7 @@
     }
  
     rxConfigParams.RxWindowTimeout = MAX( ( uint32_t )ceil( ( ( 2 * LoRaMacParams.MinRxSymbols - 8 ) * tSymbol + 2 * rxError ) / tSymbol ), LoRaMacParams.MinRxSymbols ); // Computed number of symbols
-    mac_printf("RxWindowTimeout:%u\r\n", rxConfigParams.RxWindowTimeout);
+    mac_printf("RxWindowTimeout:%lu\r\n", rxConfigParams.RxWindowTimeout);
  
     return rxConfigParams;
 }