Simple SX1272 Test Application

Dependencies:   SX1272Lib mbed

Committer:
mick_ccc
Date:
Wed Apr 26 14:23:44 2017 +0000
Revision:
3:e42efecbbe1b
Parent:
2:61713a461cf1
Child:
4:fac0fc95a644
Removed Board module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mick_ccc 0:7e7575bda256 1 #include "mbed.h"
mick_ccc 0:7e7575bda256 2
mick_ccc 3:e42efecbbe1b 3 #include "sx1272-hal.h"
mick_ccc 0:7e7575bda256 4 #include "radio.h"
mick_ccc 3:e42efecbbe1b 5 #include "main.h"
mick_ccc 0:7e7575bda256 6
mick_ccc 3:e42efecbbe1b 7 #define TX_LOOP 1
mick_ccc 3:e42efecbbe1b 8 #define TX_FREQ 866700000
mick_ccc 3:e42efecbbe1b 9 #define PAYLOAD_SIZE_LORA 97
mick_ccc 3:e42efecbbe1b 10 #define PAYLOAD_SIZE_FSK 14
mick_ccc 3:e42efecbbe1b 11
mick_ccc 3:e42efecbbe1b 12 /*!
mick_ccc 3:e42efecbbe1b 13 * Serial communication for debug logs
mick_ccc 3:e42efecbbe1b 14 */
mick_ccc 0:7e7575bda256 15 Serial pc(USBTX, USBRX); // tx, rx
mick_ccc 3:e42efecbbe1b 16
mick_ccc 3:e42efecbbe1b 17 /*!
mick_ccc 3:e42efecbbe1b 18 * Interrupt handler for nucleo user button
mick_ccc 3:e42efecbbe1b 19 */
mick_ccc 3:e42efecbbe1b 20 InterruptIn DatarateButton( USER_BUTTON );
mick_ccc 0:7e7575bda256 21
mick_ccc 3:e42efecbbe1b 22 /*!
mick_ccc 3:e42efecbbe1b 23 * Radio events function pointer
mick_ccc 3:e42efecbbe1b 24 */
mick_ccc 3:e42efecbbe1b 25 static RadioEvents_t RadioEvents;
mick_ccc 3:e42efecbbe1b 26
mick_ccc 3:e42efecbbe1b 27 /*
mick_ccc 3:e42efecbbe1b 28 * Global variables declarations
mick_ccc 3:e42efecbbe1b 29 */
mick_ccc 3:e42efecbbe1b 30 SX1272MB2xAS Radio( NULL );
mick_ccc 3:e42efecbbe1b 31
mick_ccc 1:4c82bff12ad0 32 static uint8_t LoRaWANBuffer[255];
mick_ccc 1:4c82bff12ad0 33 static uint16_t FCnt = 0;
mick_ccc 0:7e7575bda256 34
mick_ccc 3:e42efecbbe1b 35 /*
mick_ccc 3:e42efecbbe1b 36 * ISR variables declarations
mick_ccc 3:e42efecbbe1b 37 */
mick_ccc 1:4c82bff12ad0 38 static volatile bool SingleDatarate = true;
mick_ccc 3:e42efecbbe1b 39 static volatile uint8_t CurrentDatarate = 7;
mick_ccc 1:4c82bff12ad0 40 static volatile bool ButtonPressed = false;
mick_ccc 0:7e7575bda256 41
mick_ccc 0:7e7575bda256 42 /* -------------- */
mick_ccc 0:7e7575bda256 43
mick_ccc 0:7e7575bda256 44 void UserButtonPressed( void )
mick_ccc 0:7e7575bda256 45 {
mick_ccc 3:e42efecbbe1b 46 if( ButtonPressed == true )
mick_ccc 3:e42efecbbe1b 47 {
mick_ccc 3:e42efecbbe1b 48 return;
mick_ccc 3:e42efecbbe1b 49 }
mick_ccc 3:e42efecbbe1b 50
mick_ccc 1:4c82bff12ad0 51 if( SingleDatarate == false )
mick_ccc 0:7e7575bda256 52 {
mick_ccc 1:4c82bff12ad0 53 // Increase datarate by 1
mick_ccc 1:4c82bff12ad0 54 CurrentDatarate = (CurrentDatarate == 7) ? 12 : CurrentDatarate-1;
mick_ccc 0:7e7575bda256 55 }
mick_ccc 1:4c82bff12ad0 56
mick_ccc 0:7e7575bda256 57 ButtonPressed = true;
mick_ccc 0:7e7575bda256 58 }
mick_ccc 0:7e7575bda256 59
mick_ccc 0:7e7575bda256 60 /* -------------- */
mick_ccc 0:7e7575bda256 61
mick_ccc 0:7e7575bda256 62 int main() {
mick_ccc 1:4c82bff12ad0 63 Timer t;
mick_ccc 1:4c82bff12ad0 64 uint32_t TxFreq;
mick_ccc 2:61713a461cf1 65 double PktToA;
mick_ccc 3:e42efecbbe1b 66 int i;
mick_ccc 1:4c82bff12ad0 67
mick_ccc 3:e42efecbbe1b 68 pc.printf( "\n > Initializing... < \n" );
mick_ccc 0:7e7575bda256 69
mick_ccc 0:7e7575bda256 70 // Get USER button pressed
mick_ccc 0:7e7575bda256 71 DatarateButton.fall( &UserButtonPressed );
mick_ccc 0:7e7575bda256 72
mick_ccc 3:e42efecbbe1b 73 // Initialize Radio driver
mick_ccc 3:e42efecbbe1b 74 RadioEvents.TxDone = OnTxDone;
mick_ccc 3:e42efecbbe1b 75 RadioEvents.RxDone = OnRxDone;
mick_ccc 3:e42efecbbe1b 76 RadioEvents.RxError = OnRxError;
mick_ccc 3:e42efecbbe1b 77 RadioEvents.TxTimeout = OnTxTimeout;
mick_ccc 3:e42efecbbe1b 78 RadioEvents.RxTimeout = OnRxTimeout;
mick_ccc 3:e42efecbbe1b 79 Radio.Init( &RadioEvents );
mick_ccc 3:e42efecbbe1b 80
mick_ccc 3:e42efecbbe1b 81 // verify the connection with the board
mick_ccc 3:e42efecbbe1b 82 while( Radio.Read( REG_VERSION ) == 0x00 )
mick_ccc 3:e42efecbbe1b 83 {
mick_ccc 3:e42efecbbe1b 84 pc.printf( "Radio could not be detected!\n", NULL );
mick_ccc 3:e42efecbbe1b 85 wait( 1 );
mick_ccc 3:e42efecbbe1b 86 }
mick_ccc 3:e42efecbbe1b 87
mick_ccc 3:e42efecbbe1b 88 pc.printf( "\n > Board Type: SX1272MB2xAS < \n" );
mick_ccc 3:e42efecbbe1b 89
mick_ccc 3:e42efecbbe1b 90 Radio.SetPublicNetwork( true );
mick_ccc 0:7e7575bda256 91
mick_ccc 0:7e7575bda256 92 while( 1 )
mick_ccc 0:7e7575bda256 93 {
mick_ccc 2:61713a461cf1 94 // Check if there is a packet to be sent
mick_ccc 0:7e7575bda256 95 if( ButtonPressed == true )
mick_ccc 0:7e7575bda256 96 {
mick_ccc 1:4c82bff12ad0 97 // Configure radio
mick_ccc 3:e42efecbbe1b 98 TxFreq = (uint32_t)TX_FREQ;
mick_ccc 0:7e7575bda256 99 Radio.SetChannel( TxFreq );
mick_ccc 3:e42efecbbe1b 100
mick_ccc 1:4c82bff12ad0 101 // Set packet payload
mick_ccc 3:e42efecbbe1b 102 LoRaWANBuffer[0] = 0x40; // Unconfirmed Data Up
mick_ccc 3:e42efecbbe1b 103 //LoRaWANBuffer[0] = 0x80; // Confirmed Data Up
mick_ccc 1:4c82bff12ad0 104 LoRaWANBuffer[1] = 0x03; // DevAddr
mick_ccc 1:4c82bff12ad0 105 LoRaWANBuffer[2] = 0x00;
mick_ccc 1:4c82bff12ad0 106 LoRaWANBuffer[3] = 0xFE;
mick_ccc 1:4c82bff12ad0 107 LoRaWANBuffer[4] = 0xCA;
mick_ccc 1:4c82bff12ad0 108 LoRaWANBuffer[5] = 0x0; // FCtrl
mick_ccc 3:e42efecbbe1b 109 LoRaWANBuffer[6] = 0; // FCnt, filled later
mick_ccc 3:e42efecbbe1b 110 LoRaWANBuffer[7] = 0; // FCnt, filled later
mick_ccc 1:4c82bff12ad0 111 LoRaWANBuffer[8] = 1; // FPort
mick_ccc 1:4c82bff12ad0 112 LoRaWANBuffer[9] = 0x0; // FRMPayload
mick_ccc 1:4c82bff12ad0 113
mick_ccc 3:e42efecbbe1b 114 FCnt = 0;
mick_ccc 3:e42efecbbe1b 115 for( i = 0; i < (int)TX_LOOP; i++ )
mick_ccc 3:e42efecbbe1b 116 {
mick_ccc 3:e42efecbbe1b 117 LoRaWANBuffer[6] = FCnt;
mick_ccc 3:e42efecbbe1b 118 LoRaWANBuffer[7] = FCnt >> 8;
mick_ccc 3:e42efecbbe1b 119
mick_ccc 3:e42efecbbe1b 120 // Send LoRa packet
mick_ccc 3:e42efecbbe1b 121 Radio.SetTxConfig( MODEM_LORA, 2, 0, 0, CurrentDatarate, 1, 8, false, true, 0, 0, false, 10e3 );
mick_ccc 3:e42efecbbe1b 122 PktToA = Radio.TimeOnAir( MODEM_LORA, PAYLOAD_SIZE_LORA );
mick_ccc 3:e42efecbbe1b 123 Radio.Send( LoRaWANBuffer, PAYLOAD_SIZE_LORA );
mick_ccc 3:e42efecbbe1b 124 pc.printf( "(%d) Sending LoRa packet: Freq=%u, SF%u (%.1lfms), FCnt=%u...\n", i, TxFreq, CurrentDatarate, PktToA, FCnt );
mick_ccc 3:e42efecbbe1b 125 wait_ms( PktToA );
mick_ccc 3:e42efecbbe1b 126 //wait_ms( 10 );
mick_ccc 3:e42efecbbe1b 127
mick_ccc 3:e42efecbbe1b 128 FCnt += 1;
mick_ccc 3:e42efecbbe1b 129 LoRaWANBuffer[6] = FCnt;
mick_ccc 3:e42efecbbe1b 130 LoRaWANBuffer[7] = FCnt >> 8;
mick_ccc 3:e42efecbbe1b 131
mick_ccc 3:e42efecbbe1b 132 // Send FSK packet
mick_ccc 3:e42efecbbe1b 133 Radio.SetTxConfig( MODEM_FSK, 2, 25e3, 0, 50e3, 0, 5, false, true, 0, 0, false, 3e3 );
mick_ccc 3:e42efecbbe1b 134 PktToA = Radio.TimeOnAir( MODEM_FSK, PAYLOAD_SIZE_FSK );
mick_ccc 3:e42efecbbe1b 135 Radio.Send( LoRaWANBuffer, PAYLOAD_SIZE_FSK );
mick_ccc 3:e42efecbbe1b 136 pc.printf( "(%d) Sending FSK packet: Freq=%u, FCnt=%u...\n", i, TxFreq, PktToA, FCnt );
mick_ccc 3:e42efecbbe1b 137 wait_ms( PktToA );
mick_ccc 3:e42efecbbe1b 138 FCnt += 1;
mick_ccc 3:e42efecbbe1b 139 }
mick_ccc 0:7e7575bda256 140
mick_ccc 0:7e7575bda256 141 // Stop sending
mick_ccc 0:7e7575bda256 142 ButtonPressed = false;
mick_ccc 0:7e7575bda256 143 }
mick_ccc 2:61713a461cf1 144
mick_ccc 2:61713a461cf1 145 // Receive packets
mick_ccc 3:e42efecbbe1b 146 // TODO
mick_ccc 0:7e7575bda256 147 }
mick_ccc 0:7e7575bda256 148 }
mick_ccc 3:e42efecbbe1b 149
mick_ccc 3:e42efecbbe1b 150 void OnTxDone( void )
mick_ccc 3:e42efecbbe1b 151 {
mick_ccc 3:e42efecbbe1b 152 Radio.Sleep( );
mick_ccc 3:e42efecbbe1b 153 pc.printf( "> OnTxDone\n\r" );
mick_ccc 3:e42efecbbe1b 154 }
mick_ccc 3:e42efecbbe1b 155
mick_ccc 3:e42efecbbe1b 156 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
mick_ccc 3:e42efecbbe1b 157 {
mick_ccc 3:e42efecbbe1b 158 Radio.Sleep( );
mick_ccc 3:e42efecbbe1b 159 pc.printf( "> OnRxDone\n\r" );
mick_ccc 3:e42efecbbe1b 160 }
mick_ccc 3:e42efecbbe1b 161
mick_ccc 3:e42efecbbe1b 162 void OnTxTimeout( void )
mick_ccc 3:e42efecbbe1b 163 {
mick_ccc 3:e42efecbbe1b 164 Radio.Sleep( );
mick_ccc 3:e42efecbbe1b 165 pc.printf( "> OnTxTimeout\n\r" );
mick_ccc 3:e42efecbbe1b 166 }
mick_ccc 3:e42efecbbe1b 167
mick_ccc 3:e42efecbbe1b 168 void OnRxTimeout( void )
mick_ccc 3:e42efecbbe1b 169 {
mick_ccc 3:e42efecbbe1b 170 Radio.Sleep( );
mick_ccc 3:e42efecbbe1b 171 pc.printf( "> OnRxTimeout\n\r" );
mick_ccc 3:e42efecbbe1b 172 }
mick_ccc 3:e42efecbbe1b 173
mick_ccc 3:e42efecbbe1b 174 void OnRxError( void )
mick_ccc 3:e42efecbbe1b 175 {
mick_ccc 3:e42efecbbe1b 176 Radio.Sleep( );
mick_ccc 3:e42efecbbe1b 177 pc.printf( "> OnRxError\n\r" );
mick_ccc 3:e42efecbbe1b 178 }