Simple SX1272 Test Application

Dependencies:   SX1272Lib mbed

main.cpp

Committer:
mick_ccc
Date:
2017-04-25
Revision:
2:61713a461cf1
Parent:
1:4c82bff12ad0
Child:
3:e42efecbbe1b

File content as of revision 2:61713a461cf1:

#include "mbed.h"

#include "board.h"
#include "radio.h"

Serial pc(USBTX, USBRX); // tx, rx
InterruptIn DatarateButton(USER_BUTTON);

// Global variables
static uint8_t LoRaWANBuffer[255];
static uint16_t FCnt = 0;

// ISR variables
static volatile bool SingleDatarate = true;
static volatile uint8_t CurrentDatarate = 12;
static volatile bool ButtonPressed = false;

/* -------------- */

void UserButtonPressed( void )
{
    if( SingleDatarate == false )
    {
        // Increase datarate by 1
        CurrentDatarate = (CurrentDatarate == 7) ? 12 : CurrentDatarate-1;
    }
   
    ButtonPressed = true;
}

/* -------------- */

int main() {
    Timer t;
    uint32_t TxFreq;
    double PktToA;

    pc.printf("HelLo Ra !\n");
    
    // Get USER button pressed
    DatarateButton.fall( &UserButtonPressed );
    
    // Radio board init
    BoardInit( );
    
    while( 1 )
    {
        // Check if there is a packet to be sent
        if( ButtonPressed == true )
        {
            // Configure radio
            TxFreq = 867720000;
            Radio.SetChannel( TxFreq );
            Radio.SetPublicNetwork( true );
            Radio.SetTxConfig( MODEM_LORA, 14, 0, 0, CurrentDatarate, 1, 8, false, true, 0, 0, false, 3e3 );
            
            // Set packet payload
            //LoRaWANBuffer[0] = 0x40; // Unconfirmed Data Up
            LoRaWANBuffer[0] = 0x80; // Confirmed Data Up
            LoRaWANBuffer[1] = 0x03; // DevAddr
            LoRaWANBuffer[2] = 0x00;
            LoRaWANBuffer[3] = 0xFE;
            LoRaWANBuffer[4] = 0xCA;
            LoRaWANBuffer[5] = 0x0; // FCtrl
            LoRaWANBuffer[6] = FCnt;
            LoRaWANBuffer[7] = FCnt >> 8;
            LoRaWANBuffer[8] = 1; // FPort
            LoRaWANBuffer[9] = 0x0; // FRMPayload
            
            // Send packet
            PktToA = Radio.TimeOnAir( MODEM_LORA, 14 );
            Radio.Send( LoRaWANBuffer, 14 );
            pc.printf( "sending LoRa packet: Freq=%u, SF%u (%.1lfus)...\n", TxFreq, CurrentDatarate, PktToA );
            wait_us( PktToA );
            pc.printf( "sent.\n");
            FCnt += 1;

            // Stop sending
            ButtonPressed = false;
        }
        
        // Receive packets
        
    }
}