Fork of the Simple Ping-Pong demo application between two SX1272MB2xAs demo board. It's now a simple application demonstrating simple Rx (Receive) from a SX1272 boards.

Dependencies:   SX1272Lib mbed

Fork of SX1272PingPong by Semtech

Committer:
Antoine38
Date:
Mon Mar 13 09:35:55 2017 +0000
Revision:
14:6c312f8635fe
Parent:
13:edb9b443c1dd
Child:
15:dc867bef95d9
R?utilisation du ping-pong pour en faire un only-receive program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregCr 0:1ed39951ab7b 1 #include "mbed.h"
GregCr 4:5ece30264cd9 2 #include "main.h"
GregCr 13:edb9b443c1dd 3 #include "sx1272-hal.h"
GregCr 8:f956dee63a56 4 #include "debug.h"
GregCr 0:1ed39951ab7b 5
GregCr 0:1ed39951ab7b 6 /* Set this flag to '1' to display debug messages on the console */
GregCr 13:edb9b443c1dd 7 #define DEBUG_MESSAGE 1
GregCr 0:1ed39951ab7b 8
GregCr 0:1ed39951ab7b 9
Antoine38 14:6c312f8635fe 10 #define RF_FREQUENCY 868000000 // Hz
Antoine38 14:6c312f8635fe 11 #define TX_OUTPUT_POWER 14 // 14 dBm
GregCr 0:1ed39951ab7b 12
Antoine38 14:6c312f8635fe 13 #define LORA_BANDWIDTH 2 // [0: 125 kHz,
Antoine38 14:6c312f8635fe 14 // 1: 250 kHz,
Antoine38 14:6c312f8635fe 15 // 2: 500 kHz,
Antoine38 14:6c312f8635fe 16 // 3: Reserved]
Antoine38 14:6c312f8635fe 17
Antoine38 14:6c312f8635fe 18 #define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
Antoine38 14:6c312f8635fe 19 #define LORA_CODINGRATE 1 // [1: 4/5,
Antoine38 14:6c312f8635fe 20 // 2: 4/6,
Antoine38 14:6c312f8635fe 21 // 3: 4/7,
Antoine38 14:6c312f8635fe 22 // 4: 4/8]
Antoine38 14:6c312f8635fe 23
Antoine38 14:6c312f8635fe 24 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
Antoine38 14:6c312f8635fe 25 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
Antoine38 14:6c312f8635fe 26 #define LORA_FIX_LENGTH_PAYLOAD_ON false
Antoine38 14:6c312f8635fe 27 #define LORA_FHSS_ENABLED false
Antoine38 14:6c312f8635fe 28 #define LORA_NB_SYMB_HOP 4
Antoine38 14:6c312f8635fe 29 #define LORA_IQ_INVERSION_ON false
Antoine38 14:6c312f8635fe 30 #define LORA_CRC_ENABLED true
GregCr 0:1ed39951ab7b 31
Antoine38 14:6c312f8635fe 32 #define RX_TIMEOUT_VALUE 3500000 // in us
Antoine38 14:6c312f8635fe 33 #define BUFFER_SIZE 32 // Define the payload size here
GregCr 0:1ed39951ab7b 34
GregCr 3:8b9e2a4df4b5 35 DigitalOut led(LED1);
GregCr 3:8b9e2a4df4b5 36
GregCr 0:1ed39951ab7b 37 /*
GregCr 0:1ed39951ab7b 38 * Global variables declarations
GregCr 0:1ed39951ab7b 39 */
Antoine38 14:6c312f8635fe 40 typedef enum {
mluis 10:7af820d1e1df 41 LOWPOWER = 0,
mluis 10:7af820d1e1df 42 IDLE,
Antoine38 14:6c312f8635fe 43
mluis 10:7af820d1e1df 44 RX,
mluis 10:7af820d1e1df 45 RX_TIMEOUT,
mluis 10:7af820d1e1df 46 RX_ERROR,
Antoine38 14:6c312f8635fe 47
mluis 10:7af820d1e1df 48 TX,
mluis 10:7af820d1e1df 49 TX_TIMEOUT,
Antoine38 14:6c312f8635fe 50
mluis 10:7af820d1e1df 51 CAD,
mluis 10:7af820d1e1df 52 CAD_DONE
Antoine38 14:6c312f8635fe 53 } AppStates_t;
GregCr 0:1ed39951ab7b 54
mluis 10:7af820d1e1df 55 volatile AppStates_t State = LOWPOWER;
mluis 10:7af820d1e1df 56
mluis 10:7af820d1e1df 57 /*!
mluis 10:7af820d1e1df 58 * Radio events function pointer
mluis 10:7af820d1e1df 59 */
mluis 10:7af820d1e1df 60 static RadioEvents_t RadioEvents;
mluis 10:7af820d1e1df 61
mluis 10:7af820d1e1df 62 /*
mluis 10:7af820d1e1df 63 * Global variables declarations
mluis 10:7af820d1e1df 64 */
GregCr 13:edb9b443c1dd 65 SX1272MB2xAS Radio( NULL );
GregCr 0:1ed39951ab7b 66
GregCr 0:1ed39951ab7b 67 const uint8_t PingMsg[] = "PING";
GregCr 0:1ed39951ab7b 68 const uint8_t PongMsg[] = "PONG";
GregCr 0:1ed39951ab7b 69
GregCr 0:1ed39951ab7b 70 uint16_t BufferSize = BUFFER_SIZE;
GregCr 0:1ed39951ab7b 71 uint8_t Buffer[BUFFER_SIZE];
GregCr 0:1ed39951ab7b 72
GregCr 5:f2431c4fe3bb 73 int16_t RssiValue = 0.0;
GregCr 5:f2431c4fe3bb 74 int8_t SnrValue = 0.0;
GregCr 0:1ed39951ab7b 75
Antoine38 14:6c312f8635fe 76 int main()
GregCr 0:1ed39951ab7b 77 {
GregCr 0:1ed39951ab7b 78 uint8_t i;
Antoine38 14:6c312f8635fe 79
Antoine38 14:6c312f8635fe 80 debug( "\n\n\r iGreenhouse Application \n\n\r" );
mluis 10:7af820d1e1df 81
mluis 10:7af820d1e1df 82 // Initialize Radio driver
mluis 10:7af820d1e1df 83 RadioEvents.TxDone = OnTxDone;
mluis 10:7af820d1e1df 84 RadioEvents.RxDone = OnRxDone;
mluis 10:7af820d1e1df 85 RadioEvents.RxError = OnRxError;
mluis 10:7af820d1e1df 86 RadioEvents.TxTimeout = OnTxTimeout;
mluis 10:7af820d1e1df 87 RadioEvents.RxTimeout = OnRxTimeout;
mluis 10:7af820d1e1df 88 Radio.Init( &RadioEvents );
Antoine38 14:6c312f8635fe 89
GregCr 7:c1bbd6c56979 90 // verify the connection with the board
Antoine38 14:6c312f8635fe 91 while( Radio.Read( REG_VERSION ) == 0x00 ) {
GregCr 7:c1bbd6c56979 92 debug( "Radio could not be detected!\n\r", NULL );
GregCr 7:c1bbd6c56979 93 wait( 1 );
GregCr 2:59e108728d71 94 }
Antoine38 14:6c312f8635fe 95
GregCr 13:edb9b443c1dd 96 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" );
GregCr 0:1ed39951ab7b 97
Antoine38 14:6c312f8635fe 98 Radio.SetChannel( RF_FREQUENCY );
Antoine38 14:6c312f8635fe 99
Antoine38 14:6c312f8635fe 100
GregCr 7:c1bbd6c56979 101 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r");
GregCr 7:c1bbd6c56979 102 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r");
GregCr 7:c1bbd6c56979 103
GregCr 0:1ed39951ab7b 104 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
Antoine38 14:6c312f8635fe 105 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
Antoine38 14:6c312f8635fe 106 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
Antoine38 14:6c312f8635fe 107 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
Antoine38 14:6c312f8635fe 108 LORA_IQ_INVERSION_ON, 2000000 );
GregCr 0:1ed39951ab7b 109
Antoine38 14:6c312f8635fe 110 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
Antoine38 14:6c312f8635fe 111 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
Antoine38 14:6c312f8635fe 112 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
Antoine38 14:6c312f8635fe 113 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
Antoine38 14:6c312f8635fe 114 LORA_IQ_INVERSION_ON, true );
GregCr 0:1ed39951ab7b 115
Antoine38 14:6c312f8635fe 116
Antoine38 14:6c312f8635fe 117 debug_if( DEBUG_MESSAGE, "Starting listening loop\r\n" );
GregCr 0:1ed39951ab7b 118
GregCr 3:8b9e2a4df4b5 119 led = 0;
Antoine38 14:6c312f8635fe 120
GregCr 0:1ed39951ab7b 121 Radio.Rx( RX_TIMEOUT_VALUE );
Antoine38 14:6c312f8635fe 122
Antoine38 14:6c312f8635fe 123 while( 1 ) {
Antoine38 14:6c312f8635fe 124 switch( State ) {
Antoine38 14:6c312f8635fe 125 case RX:
Antoine38 14:6c312f8635fe 126 if( BufferSize > 0 ) {
Antoine38 14:6c312f8635fe 127 if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) {
Antoine38 14:6c312f8635fe 128 led = !led;
Antoine38 14:6c312f8635fe 129 debug( "...Ping\r\n" );
Antoine38 14:6c312f8635fe 130 // Send the reply to the PING string
Antoine38 14:6c312f8635fe 131 strcpy( ( char* )Buffer, ( char* )PongMsg );
Antoine38 14:6c312f8635fe 132 // We fill the buffer with numbers for the payload
Antoine38 14:6c312f8635fe 133 for( i = 4; i < BufferSize; i++ ) {
Antoine38 14:6c312f8635fe 134 Buffer[i] = i - 4;
Antoine38 14:6c312f8635fe 135 }
Antoine38 14:6c312f8635fe 136 wait_ms( 10 );
Antoine38 14:6c312f8635fe 137 Radio.Send( Buffer, BufferSize );
Antoine38 14:6c312f8635fe 138 } else { // valid reception but not a PING as expected
Antoine38 14:6c312f8635fe 139 // Set device as master and start again
Antoine38 14:6c312f8635fe 140 isMaster = true;
Antoine38 14:6c312f8635fe 141 Radio.Rx( RX_TIMEOUT_VALUE );
GregCr 0:1ed39951ab7b 142 }
GregCr 2:59e108728d71 143 }
Antoine38 14:6c312f8635fe 144 State = LOWPOWER;
Antoine38 14:6c312f8635fe 145 break;
Antoine38 14:6c312f8635fe 146 case TX:
Antoine38 14:6c312f8635fe 147 led = !led;
Antoine38 14:6c312f8635fe 148 debug( "Pong...\r\n" );
Antoine38 14:6c312f8635fe 149 Radio.Rx( RX_TIMEOUT_VALUE );
Antoine38 14:6c312f8635fe 150 State = LOWPOWER;
Antoine38 14:6c312f8635fe 151 break;
Antoine38 14:6c312f8635fe 152 case RX_TIMEOUT:
Antoine38 14:6c312f8635fe 153 Radio.Rx( RX_TIMEOUT_VALUE );
Antoine38 14:6c312f8635fe 154 State = LOWPOWER;
Antoine38 14:6c312f8635fe 155 break;
Antoine38 14:6c312f8635fe 156 case RX_ERROR:
Antoine38 14:6c312f8635fe 157 // We have received a Packet with a CRC error, send reply as if packet was correct
Antoine38 14:6c312f8635fe 158 // Send the next PONG frame
Antoine38 14:6c312f8635fe 159 strcpy( ( char* )Buffer, ( char* )PongMsg );
Antoine38 14:6c312f8635fe 160 for( i = 4; i < BufferSize; i++ ) {
Antoine38 14:6c312f8635fe 161 Buffer[i] = i - 4;
GregCr 0:1ed39951ab7b 162 }
Antoine38 14:6c312f8635fe 163 wait_ms( 10 );
Antoine38 14:6c312f8635fe 164 Radio.Send( Buffer, BufferSize );
Antoine38 14:6c312f8635fe 165 State = LOWPOWER;
Antoine38 14:6c312f8635fe 166 break;
Antoine38 14:6c312f8635fe 167 case TX_TIMEOUT:
Antoine38 14:6c312f8635fe 168 Radio.Rx( RX_TIMEOUT_VALUE );
Antoine38 14:6c312f8635fe 169 State = LOWPOWER;
Antoine38 14:6c312f8635fe 170 break;
Antoine38 14:6c312f8635fe 171 case LOWPOWER:
Antoine38 14:6c312f8635fe 172 break;
Antoine38 14:6c312f8635fe 173 default:
Antoine38 14:6c312f8635fe 174 State = LOWPOWER;
Antoine38 14:6c312f8635fe 175 break;
Antoine38 14:6c312f8635fe 176 }
GregCr 0:1ed39951ab7b 177 }
GregCr 0:1ed39951ab7b 178 }
GregCr 0:1ed39951ab7b 179
GregCr 0:1ed39951ab7b 180 void OnTxDone( void )
GregCr 0:1ed39951ab7b 181 {
GregCr 5:f2431c4fe3bb 182 Radio.Sleep( );
GregCr 0:1ed39951ab7b 183 State = TX;
GregCr 7:c1bbd6c56979 184 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
GregCr 0:1ed39951ab7b 185 }
GregCr 0:1ed39951ab7b 186
GregCr 4:5ece30264cd9 187 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
GregCr 0:1ed39951ab7b 188 {
GregCr 0:1ed39951ab7b 189 Radio.Sleep( );
GregCr 0:1ed39951ab7b 190 BufferSize = size;
GregCr 0:1ed39951ab7b 191 memcpy( Buffer, payload, BufferSize );
GregCr 0:1ed39951ab7b 192 RssiValue = rssi;
GregCr 0:1ed39951ab7b 193 SnrValue = snr;
GregCr 0:1ed39951ab7b 194 State = RX;
GregCr 7:c1bbd6c56979 195 debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
GregCr 0:1ed39951ab7b 196 }
GregCr 0:1ed39951ab7b 197
GregCr 0:1ed39951ab7b 198 void OnTxTimeout( void )
GregCr 0:1ed39951ab7b 199 {
GregCr 0:1ed39951ab7b 200 Radio.Sleep( );
GregCr 0:1ed39951ab7b 201 State = TX_TIMEOUT;
GregCr 7:c1bbd6c56979 202 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
GregCr 0:1ed39951ab7b 203 }
GregCr 0:1ed39951ab7b 204
GregCr 0:1ed39951ab7b 205 void OnRxTimeout( void )
GregCr 0:1ed39951ab7b 206 {
GregCr 0:1ed39951ab7b 207 Radio.Sleep( );
GregCr 1:126d70d374f6 208 Buffer[ BufferSize ] = 0;
GregCr 0:1ed39951ab7b 209 State = RX_TIMEOUT;
GregCr 7:c1bbd6c56979 210 debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
GregCr 0:1ed39951ab7b 211 }
GregCr 0:1ed39951ab7b 212
GregCr 0:1ed39951ab7b 213 void OnRxError( void )
GregCr 0:1ed39951ab7b 214 {
GregCr 0:1ed39951ab7b 215 Radio.Sleep( );
GregCr 0:1ed39951ab7b 216 State = RX_ERROR;
GregCr 7:c1bbd6c56979 217 debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
GregCr 0:1ed39951ab7b 218 }
GregCr 3:8b9e2a4df4b5 219