pING

Dependencies:   SX1276Lib mbed

Fork of SX1276PingPong by Semtech

Committer:
siiruxliu
Date:
Tue Jul 18 14:49:00 2017 +0000
Revision:
16:c790e9194eba
Parent:
15:8b35dd7c1436
tEST

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 14:c7251480feb9 3 #include "sx1276-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 */
mluis 15:8b35dd7c1436 7 #define DEBUG_MESSAGE 1
GregCr 0:1ed39951ab7b 8
GregCr 0:1ed39951ab7b 9 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
GregCr 5:f2431c4fe3bb 10 #define USE_MODEM_LORA 1
GregCr 0:1ed39951ab7b 11 #define USE_MODEM_FSK !USE_MODEM_LORA
GregCr 0:1ed39951ab7b 12
siiruxliu 16:c790e9194eba 13 #define RF_FREQUENCY 904500000 // Hz
siiruxliu 16:c790e9194eba 14 #define TX_OUTPUT_POWER 10 // units: dBm
GregCr 0:1ed39951ab7b 15
GregCr 0:1ed39951ab7b 16 #if USE_MODEM_LORA == 1
GregCr 0:1ed39951ab7b 17
siiruxliu 16:c790e9194eba 18 #define LORA_BANDWIDTH 0 // [0: 125 kHz,
GregCr 0:1ed39951ab7b 19 // 1: 250 kHz,
GregCr 0:1ed39951ab7b 20 // 2: 500 kHz,
GregCr 0:1ed39951ab7b 21 // 3: Reserved]
siiruxliu 16:c790e9194eba 22 #define LORA_SPREADING_FACTOR 10 // [SF7..SF12]
GregCr 0:1ed39951ab7b 23 #define LORA_CODINGRATE 1 // [1: 4/5,
GregCr 0:1ed39951ab7b 24 // 2: 4/6,
GregCr 0:1ed39951ab7b 25 // 3: 4/7,
GregCr 0:1ed39951ab7b 26 // 4: 4/8]
siiruxliu 16:c790e9194eba 27 #define LORA_PREAMBLE_LENGTH 16 // Same for Tx and Rx
GregCr 0:1ed39951ab7b 28 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
GregCr 0:1ed39951ab7b 29 #define LORA_FIX_LENGTH_PAYLOAD_ON false
GregCr 3:8b9e2a4df4b5 30 #define LORA_FHSS_ENABLED false
GregCr 3:8b9e2a4df4b5 31 #define LORA_NB_SYMB_HOP 4
GregCr 0:1ed39951ab7b 32 #define LORA_IQ_INVERSION_ON false
siiruxliu 16:c790e9194eba 33 #define LORA_CRC_ENABLED false
siiruxliu 16:c790e9194eba 34
GregCr 0:1ed39951ab7b 35 #elif USE_MODEM_FSK == 1
GregCr 0:1ed39951ab7b 36
GregCr 2:59e108728d71 37 #define FSK_FDEV 25000 // Hz
GregCr 2:59e108728d71 38 #define FSK_DATARATE 19200 // bps
GregCr 2:59e108728d71 39 #define FSK_BANDWIDTH 50000 // Hz
GregCr 2:59e108728d71 40 #define FSK_AFC_BANDWIDTH 83333 // Hz
GregCr 0:1ed39951ab7b 41 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
GregCr 0:1ed39951ab7b 42 #define FSK_FIX_LENGTH_PAYLOAD_ON false
siiruxliu 16:c790e9194eba 43 #define FSK_CRC_ENABLED false
siiruxliu 16:c790e9194eba 44
GregCr 0:1ed39951ab7b 45 #else
GregCr 0:1ed39951ab7b 46 #error "Please define a modem in the compiler options."
GregCr 0:1ed39951ab7b 47 #endif
GregCr 0:1ed39951ab7b 48
siiruxliu 16:c790e9194eba 49 #define RX_TIMEOUT_VALUE 500000 // in us
siiruxliu 16:c790e9194eba 50 #define BUFFER_SIZE 8 // Define the payload size here
GregCr 0:1ed39951ab7b 51
GregCr 8:f956dee63a56 52 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
siiruxliu 16:c790e9194eba 53 DigitalOut led(LED2);
GregCr 3:8b9e2a4df4b5 54 #else
siiruxliu 16:c790e9194eba 55 DigitalOut led(LED1);
GregCr 3:8b9e2a4df4b5 56 #endif
GregCr 3:8b9e2a4df4b5 57
GregCr 0:1ed39951ab7b 58 /*
GregCr 0:1ed39951ab7b 59 * Global variables declarations
GregCr 0:1ed39951ab7b 60 */
mluis 10:7af820d1e1df 61 typedef enum
mluis 10:7af820d1e1df 62 {
mluis 10:7af820d1e1df 63 LOWPOWER = 0,
mluis 10:7af820d1e1df 64 IDLE,
siiruxliu 16:c790e9194eba 65
mluis 10:7af820d1e1df 66 RX,
mluis 10:7af820d1e1df 67 RX_TIMEOUT,
mluis 10:7af820d1e1df 68 RX_ERROR,
siiruxliu 16:c790e9194eba 69
mluis 10:7af820d1e1df 70 TX,
mluis 10:7af820d1e1df 71 TX_TIMEOUT,
siiruxliu 16:c790e9194eba 72
mluis 10:7af820d1e1df 73 CAD,
mluis 10:7af820d1e1df 74 CAD_DONE
mluis 10:7af820d1e1df 75 }AppStates_t;
GregCr 0:1ed39951ab7b 76
mluis 10:7af820d1e1df 77 volatile AppStates_t State = LOWPOWER;
mluis 10:7af820d1e1df 78
mluis 10:7af820d1e1df 79 /*!
mluis 10:7af820d1e1df 80 * Radio events function pointer
mluis 10:7af820d1e1df 81 */
mluis 10:7af820d1e1df 82 static RadioEvents_t RadioEvents;
mluis 10:7af820d1e1df 83
mluis 10:7af820d1e1df 84 /*
mluis 10:7af820d1e1df 85 * Global variables declarations
mluis 10:7af820d1e1df 86 */
GregCr 14:c7251480feb9 87 SX1276MB1xAS Radio( NULL );
GregCr 0:1ed39951ab7b 88
siiruxliu 16:c790e9194eba 89 const uint8_t PingMsg[] = "BBBBBBBB";
siiruxliu 16:c790e9194eba 90 const uint8_t PongMsg[] = "AAAAAAAA";
GregCr 0:1ed39951ab7b 91
GregCr 0:1ed39951ab7b 92 uint16_t BufferSize = BUFFER_SIZE;
GregCr 0:1ed39951ab7b 93 uint8_t Buffer[BUFFER_SIZE];
GregCr 0:1ed39951ab7b 94
GregCr 5:f2431c4fe3bb 95 int16_t RssiValue = 0.0;
GregCr 5:f2431c4fe3bb 96 int8_t SnrValue = 0.0;
GregCr 0:1ed39951ab7b 97
siiruxliu 16:c790e9194eba 98 int main()
GregCr 0:1ed39951ab7b 99 {
GregCr 0:1ed39951ab7b 100 uint8_t i;
GregCr 0:1ed39951ab7b 101 bool isMaster = true;
siiruxliu 16:c790e9194eba 102
GregCr 14:c7251480feb9 103 debug( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" );
mluis 10:7af820d1e1df 104
mluis 10:7af820d1e1df 105 // Initialize Radio driver
mluis 10:7af820d1e1df 106 RadioEvents.TxDone = OnTxDone;
mluis 10:7af820d1e1df 107 RadioEvents.RxDone = OnRxDone;
mluis 10:7af820d1e1df 108 RadioEvents.RxError = OnRxError;
mluis 10:7af820d1e1df 109 RadioEvents.TxTimeout = OnTxTimeout;
mluis 10:7af820d1e1df 110 RadioEvents.RxTimeout = OnRxTimeout;
mluis 10:7af820d1e1df 111 Radio.Init( &RadioEvents );
siiruxliu 16:c790e9194eba 112
GregCr 7:c1bbd6c56979 113 // verify the connection with the board
GregCr 7:c1bbd6c56979 114 while( Radio.Read( REG_VERSION ) == 0x00 )
GregCr 2:59e108728d71 115 {
GregCr 7:c1bbd6c56979 116 debug( "Radio could not be detected!\n\r", NULL );
GregCr 7:c1bbd6c56979 117 wait( 1 );
GregCr 2:59e108728d71 118 }
siiruxliu 16:c790e9194eba 119
siiruxliu 16:c790e9194eba 120 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "\n\r > Board Type: SX1276MB1LAS < \n\r" );
siiruxliu 16:c790e9194eba 121 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "\n\r > Board Type: SX1276MB1MAS < \n\r" );
siiruxliu 16:c790e9194eba 122
GregCr 0:1ed39951ab7b 123 Radio.SetChannel( RF_FREQUENCY );
GregCr 0:1ed39951ab7b 124
GregCr 0:1ed39951ab7b 125 #if USE_MODEM_LORA == 1
siiruxliu 16:c790e9194eba 126
siiruxliu 16:c790e9194eba 127 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r");
siiruxliu 16:c790e9194eba 128 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r");
GregCr 7:c1bbd6c56979 129
GregCr 0:1ed39951ab7b 130 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
GregCr 0:1ed39951ab7b 131 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
GregCr 0:1ed39951ab7b 132 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
siiruxliu 16:c790e9194eba 133 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
siiruxliu 16:c790e9194eba 134 LORA_IQ_INVERSION_ON, 2000000 );
siiruxliu 16:c790e9194eba 135
GregCr 0:1ed39951ab7b 136 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
GregCr 0:1ed39951ab7b 137 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
mluis 9:e764990e45df 138 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
siiruxliu 16:c790e9194eba 139 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
GregCr 3:8b9e2a4df4b5 140 LORA_IQ_INVERSION_ON, true );
siiruxliu 16:c790e9194eba 141
GregCr 0:1ed39951ab7b 142 #elif USE_MODEM_FSK == 1
GregCr 0:1ed39951ab7b 143
siiruxliu 16:c790e9194eba 144 debug("\n\n\r > FSK Mode < \n\n\r");
GregCr 0:1ed39951ab7b 145 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
GregCr 0:1ed39951ab7b 146 FSK_DATARATE, 0,
GregCr 0:1ed39951ab7b 147 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
siiruxliu 16:c790e9194eba 148 FSK_CRC_ENABLED, 0, 0, 0, 2000000 );
siiruxliu 16:c790e9194eba 149
GregCr 0:1ed39951ab7b 150 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
GregCr 0:1ed39951ab7b 151 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
mluis 9:e764990e45df 152 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
GregCr 3:8b9e2a4df4b5 153 0, 0, false, true );
siiruxliu 16:c790e9194eba 154
GregCr 0:1ed39951ab7b 155 #else
GregCr 0:1ed39951ab7b 156
GregCr 0:1ed39951ab7b 157 #error "Please define a modem in the compiler options."
GregCr 0:1ed39951ab7b 158
GregCr 0:1ed39951ab7b 159 #endif
siiruxliu 16:c790e9194eba 160
siiruxliu 16:c790e9194eba 161 debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" );
siiruxliu 16:c790e9194eba 162
GregCr 3:8b9e2a4df4b5 163 led = 0;
siiruxliu 16:c790e9194eba 164
GregCr 0:1ed39951ab7b 165 Radio.Rx( RX_TIMEOUT_VALUE );
siiruxliu 16:c790e9194eba 166
GregCr 0:1ed39951ab7b 167 while( 1 )
GregCr 0:1ed39951ab7b 168 {
siiruxliu 16:c790e9194eba 169 strcpy( ( char* )Buffer, ( char* )PingMsg );
siiruxliu 16:c790e9194eba 170 Radio.Send( Buffer, BufferSize );
GregCr 0:1ed39951ab7b 171 }
GregCr 0:1ed39951ab7b 172 }
GregCr 0:1ed39951ab7b 173
GregCr 0:1ed39951ab7b 174 void OnTxDone( void )
GregCr 0:1ed39951ab7b 175 {
GregCr 5:f2431c4fe3bb 176 Radio.Sleep( );
GregCr 0:1ed39951ab7b 177 State = TX;
GregCr 7:c1bbd6c56979 178 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
GregCr 0:1ed39951ab7b 179 }
GregCr 0:1ed39951ab7b 180
siiruxliu 16:c790e9194eba 181 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
GregCr 0:1ed39951ab7b 182 {
GregCr 0:1ed39951ab7b 183 Radio.Sleep( );
GregCr 0:1ed39951ab7b 184 BufferSize = size;
siiruxliu 16:c790e9194eba 185 memcpy(Buffer, payload, BufferSize);
siiruxliu 16:c790e9194eba 186 //printf("Payload length = %d \n\r",BufferSize);
siiruxliu 16:c790e9194eba 187 //printf("First 8 bytes -- %d %d %d %d %d %d %d %d\n\r", payload[0], payload[1], payload[2], payload[3], payload[4], payload[5], payload[6], payload[7]);
GregCr 0:1ed39951ab7b 188 RssiValue = rssi;
GregCr 0:1ed39951ab7b 189 SnrValue = snr;
GregCr 0:1ed39951ab7b 190 State = RX;
GregCr 7:c1bbd6c56979 191 debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
siiruxliu 16:c790e9194eba 192 printf("SINR Value = %d \n\r",SnrValue);
siiruxliu 16:c790e9194eba 193 printf("RSSI Value = %d \n\r",RssiValue);
siiruxliu 16:c790e9194eba 194
GregCr 0:1ed39951ab7b 195 }
GregCr 0:1ed39951ab7b 196
GregCr 0:1ed39951ab7b 197 void OnTxTimeout( void )
GregCr 0:1ed39951ab7b 198 {
GregCr 0:1ed39951ab7b 199 Radio.Sleep( );
GregCr 0:1ed39951ab7b 200 State = TX_TIMEOUT;
GregCr 7:c1bbd6c56979 201 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
GregCr 0:1ed39951ab7b 202 }
GregCr 0:1ed39951ab7b 203
GregCr 0:1ed39951ab7b 204 void OnRxTimeout( void )
GregCr 0:1ed39951ab7b 205 {
GregCr 0:1ed39951ab7b 206 Radio.Sleep( );
siiruxliu 16:c790e9194eba 207 Buffer[ BufferSize ] = 0;
GregCr 0:1ed39951ab7b 208 State = RX_TIMEOUT;
GregCr 7:c1bbd6c56979 209 debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
siiruxliu 16:c790e9194eba 210
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" );
siiruxliu 16:c790e9194eba 218 }