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.
command | arguments | description |
---|---|---|
? | - | print available commands |
. (period) | - | print status (DevEUI, DevAddr, etc) |
ul | length integer | set 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: button | RX TX: (unused) | A3 A4: Rotary Angle Sensor |
D6 D7: RGB LED | SCL SDA: digital light sensor | A1 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.
Diff: mac/LoRaMac.cpp
- Revision:
- 1:53c30224eda8
- Parent:
- 0:8f0d0ae0a077
- Child:
- 2:f2d9aa163652
--- a/mac/LoRaMac.cpp Thu May 18 15:11:53 2017 -0700 +++ b/mac/LoRaMac.cpp Mon May 22 16:43:39 2017 -0700 @@ -223,10 +223,10 @@ /*! * LoRaMac bands */ -static Band_t Bands[LORA_MAX_NB_BANDS] = +/*static Band_t Bands[LORA_MAX_NB_BANDS] = { BAND0, -}; +};*/ /*! * LoRaMAC channels @@ -649,7 +649,7 @@ void loramac_print_status() { - printf("LoRaMacState:%lx\r\n", LoRaMacState); + isr_printf("LoRaMacState:%lx\r\n", LoRaMacState); } /* @@ -729,7 +729,7 @@ } // Update last tx done time for the current channel - Bands[Channels[Channel].Band].last_tx_done_us = tx_done_us; + //Bands[Channels[Channel].Band].last_tx_done_us = tx_done_us; // Update Aggregated last tx done time AggregatedLastTxDoneTime_us = tx_done_us; // Update Backoff @@ -792,7 +792,7 @@ Radio.Rx(2000); expecting_beacon = true; - //printf("OnRxBeaconSetup() %u\r\n", BeaconCtx.SymbolTimeout); + //isr_printf("OnRxBeaconSetup() %u\r\n", BeaconCtx.SymbolTimeout); } @@ -855,31 +855,31 @@ } if (BeaconCtx.state == BEACON_STATE_ACQ_ERROR) { - printf("-->LOCKED "); + isr_printf("-->LOCKED "); BeaconCtx.state = BEACON_STATE_LOCKED_; compensate_precession = true; set_beacon_symbol_timeout(BEACON_RX_TIMEOUT_LOCKED); } } else { /* ignore precession at first acquisition because it has slot resolution added */ - printf("-->ACQ_ERROR "); + isr_printf("-->ACQ_ERROR "); // next beacon will give us our crystal error BeaconCtx.state = BEACON_STATE_ACQ_ERROR; } - printf("err%d=%u-%u ", BeaconCtx.BeaconRxTimerError_us, ThisBeaconRx_us, BeaconCtx.LastBeaconRx_us); - printf(" rx-before-tx:%d ", BeaconCtx.rx_precession_us); + isr_printf("err%d=%u-%u ", BeaconCtx.BeaconRxTimerError_us, ThisBeaconRx_us, BeaconCtx.LastBeaconRx_us); + isr_printf(" rx-before-tx:%d ", BeaconCtx.rx_precession_us); BeaconCtx.LastBeaconRx_us = ThisBeaconRx_us; BeaconCtx.LastBeaconStart_us = BeaconCtx.LastBeaconRx_us; if (BeaconCtx.state == BEACON_STATE_LOCKED_) { if (compensate_precession) { compensation = BeaconCtx.rx_precession_us - TARGET_PRECESSION_US + BeaconCtx.BeaconRxTimerError_us; - printf(" comp%ld", compensation); + isr_printf(" comp%ld", compensation); } } - printf("\r\n"); + isr_printf("\r\n"); BeaconCtx.next_beacon_expected_us = BEACON_INTERVAL_us + compensation; unsigned now_us = lp_timer.read_us(); @@ -909,9 +909,9 @@ rx |= payload[2] << 16; rx |= payload[3] << 24; if (rx != 0) - printf("beacon payload:%08x\r\n", rx); + isr_printf("beacon payload:%08x\r\n", rx); } else - printf("calc_crc:%04x rx_crc:%04x\r\n", calc_crc, rx_crc); + isr_printf("calc_crc:%04x rx_crc:%04x\r\n", calc_crc, rx_crc); } float get_symbol_period(uint8_t bw, uint8_t sf) @@ -1036,10 +1036,10 @@ if( micRx == mic ) { LoRaMacJoinComputeSKeys( LoRaMacAppKey, LoRaMacRxPayload + 1, LoRaMacDevNonce, LoRaMacNwkSKey, LoRaMacAppSKey ); - printf("decr%u:", size); + isr_printf("decr%u:", size); for (mic = 0; mic < size; mic++) - printf("%02x ", LoRaMacRxPayload[mic]); - printf("\r\n"); + isr_printf("%02x ", LoRaMacRxPayload[mic]); + isr_printf("\r\n"); LoRaMacNetID = ( uint32_t )LoRaMacRxPayload[4]; LoRaMacNetID |= ( ( uint32_t )LoRaMacRxPayload[5] << 8 ); @@ -1062,7 +1062,7 @@ uint16_t beaconTimingDelay = LoRaMacRxPayload[13] & 0xff; beaconTimingDelay |= LoRaMacRxPayload[14] << 8; - printf("%lx slots:%x (rxdelay %lu)", LoRaMacDevAddr, beaconTimingDelay, LoRaMacParams.ReceiveDelay_us); + isr_printf("%lx slots:%x (rxdelay %lu)", LoRaMacDevAddr, beaconTimingDelay, LoRaMacParams.ReceiveDelay_us); // how long from tx-done of join-request is beacon going to occur at uint32_t us_to_beacon = ( PING_SLOT_RESOLUTION_us * beaconTimingDelay ); // get time elapsed since last tx-done @@ -1070,7 +1070,7 @@ unsigned int us_since_TxDone = now_us - TxDone_us; BeaconCtx.timeout.attach_us(&OnRxBeaconSetup, us_to_beacon - us_since_TxDone); BeaconCtx.next_beacon_expected_us = now_us + us_to_beacon; - printf("us_to_beacon:%lu, since_tx_done:%u\r\n", us_to_beacon, us_since_TxDone); + isr_printf("us_to_beacon:%lu, since_tx_done:%u\r\n", us_to_beacon, us_since_TxDone); BeaconCtx.have_beacon = false; BeaconCtx.state = BEACON_STATE_FIRST_ACQ; BeaconCtx.num_missed = 0; @@ -1082,12 +1082,12 @@ BeaconCtx.SymbolTimeout_sec = 0.050 + (BEACON_MIN_SYMBOL_TIMEOUT * BeaconCtx.symbol_period_secs); BeaconCtx.SymbolTimeout = BeaconCtx.SymbolTimeout_sec / BeaconCtx.symbol_period_secs; - printf("symbol_period_secs:%f symbto:%d\r\n", BeaconCtx.symbol_period_secs, BeaconCtx.SymbolTimeout); + isr_printf("sp:%f sto:%d\r\n", BeaconCtx.symbol_period_secs, BeaconCtx.SymbolTimeout); BeaconCtx.tx_slot_offset = LoRaMacRxPayload[15]; BeaconCtx.tx_slot_offset |= LoRaMacRxPayload[16] << 8; BeaconCtx.periodicity_slots = LoRaMacRxPayload[17]; BeaconCtx.periodicity_slots |= LoRaMacRxPayload[18] << 8; - printf("tx_slot_offset:%u, periodicity_slots:%u\r\n", BeaconCtx.tx_slot_offset, BeaconCtx.periodicity_slots); + isr_printf("tso:%u, PS:%u\r\n", BeaconCtx.tx_slot_offset, BeaconCtx.periodicity_slots); MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_OK; IsLoRaMacNetworkJoined = true; @@ -1096,7 +1096,7 @@ else { MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_JOIN_FAIL; - printf("join-mic-fail\r\n"); + isr_printf("join-mic-fail\r\n"); JoinRequestTrials = MaxJoinRequestTrials; // stop trying } break; @@ -1374,9 +1374,9 @@ } LoRaMacFlags.Bits.MacDone = 1; - // Trig OnMacCheckTimerEvent call as soon as possible - OnMacStateCheckTimerEvent(); -} + /* run mac check quickly, but not immedately (for isr_printf) */ + MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, 50000); +} // ..OnRadioRxDone(); static void OnRadioTxTimeout( void ) { @@ -1426,7 +1426,7 @@ unsigned us_since_rx_setup = now_us - BeaconCtx.RxBeaconSetupAt_us; BeaconCtx.timeout.attach_us(&OnRxBeaconSetup, BeaconCtx.next_beacon_expected_us - us_since_rx_setup); - printf("beacon-rx-timeout %u %u next in %uus (%u)\r\n", BeaconCtx.num_missed, BeaconCtx.SymbolTimeout, BeaconCtx.next_beacon_expected_us, us_since_rx_setup); + isr_printf("beacon-rx-timeout %u %u next in %uus (%u)\r\n", BeaconCtx.num_missed, BeaconCtx.SymbolTimeout, BeaconCtx.next_beacon_expected_us, us_since_rx_setup); MlmeIndication.MlmeIndication = MLME_BEACON; MlmeIndication.Status = LORAMAC_EVENT_INFO_STATUS_BEACON_LOST; @@ -1443,21 +1443,22 @@ MlmeConfirm.Status = LORAMAC_EVENT_INFO_STATUS_RX_TIMEOUT; LoRaMacFlags.Bits.MacDone = 1; - OnMacStateCheckTimerEvent(); -} + /* run mac check quickly, but not immedately (for isr_printf) */ + MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, 50000); +} // ..OnRadioRxTimeout(); static void OnMacStateCheckTimerEvent( void ) { bool txTimeout = false; - //printf("mac-check:%d McpsInd%d ", LoRaMacFlags.Bits.MacDone, LoRaMacFlags.Bits.McpsInd); + //isr_printf("mac-check:%d McpsInd%d ", LoRaMacFlags.Bits.MacDone, LoRaMacFlags.Bits.McpsInd); if( LoRaMacFlags.Bits.MacDone == 1 ) { if( ( LoRaMacState & LORAMAC_RX_ABORT ) == LORAMAC_RX_ABORT ) { LoRaMacState &= ~LORAMAC_RX_ABORT; LoRaMacState &= ~LORAMAC_TX_RUNNING; - //printf("tx-run-A\r\n"); + //isr_printf("tx-run-A\r\n"); } if( ( LoRaMacFlags.Bits.MlmeReq == 1 ) || ( ( LoRaMacFlags.Bits.McpsReq == 1 ) ) ) @@ -1467,7 +1468,7 @@ { // Stop transmit cycle due to tx timeout. LoRaMacState &= ~LORAMAC_TX_RUNNING; - //printf("tx-run-B\r\n"); + //isr_printf("tx-run-B\r\n"); MacCommandsBufferIndex = 0; McpsConfirm.AckReceived = false; McpsConfirm.TxTimeOnAir = 0; @@ -1484,18 +1485,18 @@ if( MlmeConfirm.Status == LORAMAC_EVENT_INFO_STATUS_OK ) { // Node joined successfully - printf("mac-check-join ok "); + isr_printf("mac-check-join ok "); UpLinkCounter = 0; ChannelsNbRepCounter = 0; LoRaMacState &= ~LORAMAC_TX_RUNNING; } else { - printf("jrt:%u, maxjrt:%u\r\n", JoinRequestTrials, MaxJoinRequestTrials); + isr_printf("jrt:%u, maxjrt:%u\r\n", JoinRequestTrials, MaxJoinRequestTrials); if( JoinRequestTrials >= MaxJoinRequestTrials ) { LoRaMacState &= ~LORAMAC_TX_RUNNING; - printf("tx-run-D\r\n"); + isr_printf("tx-run-D\r\n"); } else { @@ -1519,7 +1520,7 @@ } LoRaMacState &= ~LORAMAC_TX_RUNNING; - //printf("tx-run-E\r\n"); + //isr_printf("tx-run-E\r\n"); } } @@ -1530,10 +1531,10 @@ { LoRaMacState &= ~LORAMAC_RX; } - //printf("LoRaMacState:%lx ", LoRaMacState); + //isr_printf("LoRaMacState:%lx ", LoRaMacState); if( LoRaMacState == LORAMAC_IDLE ) { - //printf("McpsReq%d ", LoRaMacFlags.Bits.McpsReq); + //isr_printf("McpsReq%d ", LoRaMacFlags.Bits.McpsReq); if( LoRaMacFlags.Bits.McpsReq == 1 ) { LoRaMacPrimitives->MacMcpsConfirm( &McpsConfirm ); @@ -1558,7 +1559,7 @@ else { // Operation not finished restart timer - //printf("mac-restart-%lx ", LoRaMacState); + //isr_printf("mac-restart-%lx ", LoRaMacState); MacStateCheckTimer.attach_us(&OnMacStateCheckTimerEvent, MAC_STATE_CHECK_TIMEOUT_us); } @@ -1571,7 +1572,7 @@ LoRaMacFlags.Bits.McpsIndSkip = 0; LoRaMacFlags.Bits.McpsInd = 0; } - //printf("\r\n"); + //isr_printf("\r\n"); } // ..OnMacStateCheckTimerEvent( void ) static void OnTxDelayedTimerEvent( void ) @@ -1586,8 +1587,8 @@ ResetMacParameters( ); if (++Channel == LORA_MAX_NB_CHANNELS) Channel = 0; - printf("<ch%u>", Channel); - printf("tx-delayed join ch%u\r\n", Channel); + isr_printf("<ch%u>", Channel); + isr_printf("tx-delayed join ch%u\r\n", Channel); macHdr.Value = 0; macHdr.Bits.MType = FRAME_TYPE_JOIN_REQ; @@ -1609,11 +1610,11 @@ Radio.Standby( ); RxWindowSetup( LORAMAC_FIRST_CHANNEL + ( Channel * LORAMAC_STEPWIDTH_CHANNEL), RxWindowsParam.Datarate, RxWindowsParam.Bandwidth, RxWindowsParam.RxWindowTimeout, false ); - //printf("rx-ch%u, %lu\r\n", Channel, RxWindowsParam.RxWindowTimeout); + //isr_printf("rx-ch%u, %lu\r\n", Channel, RxWindowsParam.RxWindowTimeout); /* no retrying TX in this class of operation */ LoRaMacState &= ~LORAMAC_TX_RUNNING; - //printf("rx-window "); + //isr_printf("rx-window "); } static void OnAckTimeoutTimerEvent( void ) @@ -1648,8 +1649,8 @@ { Radio.Rx( 0 ); // Continuous mode } - //printf("rx-setup %u %lu\r\n", timeout, LoRaMacParams.MaxRxWindow); - //printf("rx:%luhz sf%u bw%lu\r\n", freq, downlinkDatarate, bandwidth); + //isr_printf("rx-setup %u %lu\r\n", timeout, LoRaMacParams.MaxRxWindow); + //isr_printf("rx:%luhz sf%u bw%lu\r\n", freq, downlinkDatarate, bandwidth); return true; } return false; @@ -2068,7 +2069,7 @@ if( IsLoRaMacNetworkJoined == false ) { JoinRequestTrials++; - printf("join %luhz try%u len%u\r\n", channel.Frequency, JoinRequestTrials, LoRaMacBufferPktLen); + isr_printf("join %luhz try%u len%u\r\n", channel.Frequency, JoinRequestTrials, LoRaMacBufferPktLen); } /* anything not join request is sent at permitted time slot */ @@ -2095,24 +2096,24 @@ use_slot_us = (use_slot * 30000); #ifdef TX_DEBUG - printf("now_slot:%d, to-guard:%u use_slot:%u ", now_slot, BeaconCtx.next_beacon_expected_us - use_slot_us, use_slot); + isr_printf("now_slot:%d, to-guard:%u use_slot:%u ", now_slot, BeaconCtx.next_beacon_expected_us - use_slot_us, use_slot); #endif if ((BeaconCtx.next_beacon_expected_us - use_slot_us) > BEACON_GUARD_us) { target_us = (use_slot * 30000) + BEACON_RESERVED_us; tx_timeout.attach_us(&send_callback, target_us - us_since_beacon_start); #ifdef TX_DEBUG - printf(" : tx in %uus\r\n", target_us - us_since_beacon_start); + isr_printf(" : tx in %uus\r\n", target_us - us_since_beacon_start); #endif } else { #ifdef TX_DEBUG - printf("\r\n"); + isr_printf("\r\n"); #endif return LORAMAC_STATUS_BUSY; } LoRaMacState |= LORAMAC_TX_SCHED; } else { - printf("dropped tx\r\n"); + isr_printf("dropped tx\r\n"); return LORAMAC_STATUS_SERVICE_UNKNOWN; // TODO use correct failure } @@ -2151,13 +2152,11 @@ void seconds() { - printf("second\r\n"); + isr_printf("second\r\n"); } LoRaMacStatus_t LoRaMacInitialization( LoRaMacPrimitives_t *primitives, LoRaMacCallback_t *callbacks ) { - Thread eventThread; - if( primitives == NULL ) { return LORAMAC_STATUS_PARAMETER_INVALID; @@ -2392,7 +2391,7 @@ break; } case CLASS_D: - printf("TODO MIB_DEVICE_CLASS:D\r\n"); + isr_printf("TODO MIB_DEVICE_CLASS:D\r\n"); break; } break; @@ -2649,9 +2648,9 @@ ResetMacParameters( ); Channel = 0; // start with first channel - printf("<ch0>"); - - printf("mlme-join-send ch%u\r\n", Channel); + isr_printf("<ch0>"); + + isr_printf("mlme-join-send ch%u\r\n", Channel); status = Send( &macHdr, 0, NULL, 0 ); break; } @@ -2779,7 +2778,7 @@ void LoRaMacTestSetChannel( uint8_t channel ) { - printf("set-testch%u\r\n", channel); + isr_printf("set-testch%u\r\n", channel); Channel = channel; }