Skip to content

MM User Guide : Main Examples Description

LoRaWanMiniMouse edited this page Apr 13, 2018 · 11 revisions

Introduction

This guide will give a description of the 3 examples main files already implemented with MM stack. These files are named :

  1. main.cpp
  2. main4certification.cpp
  3. CraZyMain.cpp

Detailed review of main.cpp

This main file give an example of a usual LoRaWan application implementation. In this example, the application layer process periodically the following sequence :

  1. Build a LoRa Tx frame,
  2. Send this frame with the MM stack,
  3. in case of LoRa downlink, manages the receive frame,
  4. goto to sleep.

Create and Configure a LoRaWan object :

  1. Create a radio type and a radio object :
SX1276  RadioUser( LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET );
  1. Set the LoRaWan Keys and select between APB/OTA mode

this is done filling a sLoRaWanKeys structure :

sLoRaWanKeys  LoraWanKeys ={LoRaMacNwkSKeyInit, LoRaMacAppSKeyInit, LoRaMacAppKeyInit, AppEuiInit, DevEuiInit, LoRaDevAddrInit,OTA_DEVICE};

Note : In APB mode only LoRaMacNwkSKey, LoRaMacAppSKey and LoRaDevaddr are relevant. In OTA mode only LoRaMacAppKey, AppEuiInit and DevEuiInit are relevant.

  1. Finally, create a LoRaWan Object :

This object requires in template parameters : the radio type, and the Region type.

This object requires as parameters : the keys structure, the radio object itself, and the Flash address (to store context).

 LoraWanObject<LoraRegionsEU,SX1276> Lp( LoraWanKeys,&RadioUser,USERFLASHADRESS); 

User have to choose the DataRate strategy (as describe in architecture)

eDataRateStrategy AppDataRate = STATIC_ADR_MODE; 
Lp.SetDataRateStrategy( AppDataRate );

Build the Tx Frame :

In the appli.cpp creates the applicative Tx payload. User have to select a Lora Port , a Msg Type and set the userpayload size :

    UserFport       = 3;
    UserPayloadSize = 9;
    MsgType = UNCONF_DATA_UP; 

Restore The LoRaWan Context in case of Reset.

In case of Reset due to watch dog or power off, execute Lp.RestoreContext ( ); to retrieve the LoraWan context saved in Flash and protected by a Crc (as described in architecture)

The Infinite Loop

Prepare the next Tx frame

 while(1) {  
        UserFport       = 3;
        UserPayloadSize = 9;
        PrepareFrame ( UserPayload );
        UserPayload[8]  = Lp.GetNbOfReset(); // in this example adding number of reset inside the applicatif payload
        Lp.SetDataRateStrategy( AppDataRate );

Note In this example, the number of resets (store into the flash) is sent in the applicative payload.

In Case of OTA Mode Join if not already done Else Send Payload

        if ( Lp.IsJoined ( ) == NOT_JOINED ) {            
            LpState = Lp.Join( );
        } else {
            LpState = Lp.SendPayload( UserFport, UserPayload, UserPayloadSize, MsgType );
        }

Call Periodically LoraWanProcess

As describe architecture, MM stack require to be call periodically to update it internal state machine. It is fully compatible with low power implementation. After each call mcu is set in deep sleep.

        while ( LpState != LWPSTATE_IDLE ){
            LpState = Lp.LoraWanProcess( &AvailableRxPacket );
            mcu.GotoSleepMSecond ( 100 );
            mcu.WatchDogRelease ( );
        }

Special Case of failsafe occurs

        if ( LpState == LWPSTATE_ERROR ) {
        // user application have to save all the need
            NVIC_SystemReset();
        }

Application Downlink available ?

        if ( AvailableRxPacket == LORA_RX_PACKET_AVAILABLE ) { 
            Lp.ReceivePayload( &UserRxFport, UserRxPayload, &UserRxPayloadSize );
            DEBUG_PRINTF("Receive on port %d  an Applicative Downlink \n DATA[%d] = [ ",UserRxFport,UserRxPayloadSize);
            for ( i = 0 ; i < UserRxPayloadSize ; i++){
                DEBUG_PRINTF( "0x%.2x ",UserRxPayload[i]);
            }
            DEBUG_MSG("]\n");
            // call the application layer to manage the application downlink
        }

Goto to sleep until the next Tx frame

        if ( Lp.IsJoined ( ) == NOT_JOINED ) {
            mcu.GotoSleepSecond(5);
        } else {
            mcu.GotoSleepSecond ( AppTimeSleeping );
        }

Note : The MM stack implements a duty cycle for Join. So if the time to send a new join is too short, MM stack go back in idle.

Brief review of main4certification.cpp.

This main file have to be used for certification purposes. Its implements special case of frames received on Port 224.

Brief review of CraZyMAin.cpp

This example of main is probably unusable. But it allows to illustrate the capabilities of the MM stack. This example could be renamed "Dual LoRaWan Network" + User Radio.

In this example, the same radio is successively used :

  1. to Transmit/receive on a first lorawan network in OTAmode,
  2. to transmit on a no LoraWan network ( to illustrate that the user can reuse the radio as it wants to )
  3. to Transmit/receive on a second lorawan network in apb mode.

It will be also possible to support more than one Region or to support more than one Radio.