Simple SX1272 Test Application

Dependencies:   SX1272Lib mbed

main.cpp

Committer:
mick_ccc
Date:
2017-04-26
Revision:
3:e42efecbbe1b
Parent:
2:61713a461cf1
Child:
4:fac0fc95a644

File content as of revision 3:e42efecbbe1b:

#include "mbed.h"

#include "sx1272-hal.h"
#include "radio.h"
#include "main.h"

#define TX_LOOP 1
#define TX_FREQ 866700000
#define PAYLOAD_SIZE_LORA 97
#define PAYLOAD_SIZE_FSK 14

/*!
 * Serial communication for debug logs
 */
Serial pc(USBTX, USBRX); // tx, rx

/*!
 * Interrupt handler for nucleo user button
 */
InterruptIn DatarateButton( USER_BUTTON );

/*!
 * Radio events function pointer
 */
static RadioEvents_t RadioEvents;
 
/*
 *  Global variables declarations
 */
SX1272MB2xAS Radio( NULL );

static uint8_t LoRaWANBuffer[255];
static uint16_t FCnt = 0;

/*
 *  ISR variables declarations
 */
static volatile bool SingleDatarate = true;
static volatile uint8_t CurrentDatarate = 7;
static volatile bool ButtonPressed = false;

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

void UserButtonPressed( void )
{
    if( ButtonPressed == true )
    {
        return;
    }

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

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

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

    pc.printf( "\n > Initializing... < \n" );
    
    // Get USER button pressed
    DatarateButton.fall( &UserButtonPressed );
    
    // Initialize Radio driver
    RadioEvents.TxDone = OnTxDone;
    RadioEvents.RxDone = OnRxDone;
    RadioEvents.RxError = OnRxError;
    RadioEvents.TxTimeout = OnTxTimeout;
    RadioEvents.RxTimeout = OnRxTimeout;
    Radio.Init( &RadioEvents );
 
    // verify the connection with the board
    while( Radio.Read( REG_VERSION ) == 0x00  )
    {
        pc.printf( "Radio could not be detected!\n", NULL );
        wait( 1 );
    }
 
    pc.printf( "\n > Board Type: SX1272MB2xAS < \n" );
    
    Radio.SetPublicNetwork( true );
    
    while( 1 )
    {
        // Check if there is a packet to be sent
        if( ButtonPressed == true )
        {
            // Configure radio
            TxFreq = (uint32_t)TX_FREQ;
            Radio.SetChannel( TxFreq );

            // 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] = 0; // FCnt, filled later
            LoRaWANBuffer[7] = 0; // FCnt, filled later
            LoRaWANBuffer[8] = 1; // FPort
            LoRaWANBuffer[9] = 0x0; // FRMPayload
            
            FCnt = 0;
            for( i = 0; i < (int)TX_LOOP; i++ )
            {
                LoRaWANBuffer[6] = FCnt;
                LoRaWANBuffer[7] = FCnt >> 8;
                
                // Send LoRa packet
                Radio.SetTxConfig( MODEM_LORA, 2, 0, 0, CurrentDatarate, 1, 8, false, true, 0, 0, false, 10e3 );
                PktToA = Radio.TimeOnAir( MODEM_LORA, PAYLOAD_SIZE_LORA );
                Radio.Send( LoRaWANBuffer, PAYLOAD_SIZE_LORA );
                pc.printf( "(%d) Sending LoRa packet: Freq=%u, SF%u (%.1lfms), FCnt=%u...\n", i, TxFreq, CurrentDatarate, PktToA, FCnt );
                wait_ms( PktToA );
                //wait_ms( 10 );
                
                FCnt += 1;
                LoRaWANBuffer[6] = FCnt;
                LoRaWANBuffer[7] = FCnt >> 8;
                
                // Send FSK packet
                Radio.SetTxConfig( MODEM_FSK, 2, 25e3, 0, 50e3, 0, 5, false, true, 0, 0, false, 3e3 );
                PktToA = Radio.TimeOnAir( MODEM_FSK, PAYLOAD_SIZE_FSK );
                Radio.Send( LoRaWANBuffer, PAYLOAD_SIZE_FSK );
                pc.printf( "(%d) Sending FSK packet: Freq=%u, FCnt=%u...\n", i, TxFreq, PktToA, FCnt );
                wait_ms( PktToA );
                FCnt += 1;
            }

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

void OnTxDone( void )
{
    Radio.Sleep( );
    pc.printf( "> OnTxDone\n\r" );
}
 
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
    Radio.Sleep( );
    pc.printf( "> OnRxDone\n\r" );
}
 
void OnTxTimeout( void )
{
    Radio.Sleep( );
    pc.printf( "> OnTxTimeout\n\r" );
}
 
void OnRxTimeout( void )
{
    Radio.Sleep( );
    pc.printf( "> OnRxTimeout\n\r" );
}
 
void OnRxError( void )
{
    Radio.Sleep( );
    pc.printf( "> OnRxError\n\r" );
}