Peer to peer connection to switch on a led on slave

Dependencies:   SX1272Lib mbed

Committer:
radnoumane
Date:
Wed Mar 21 13:16:28 2018 +0000
Revision:
0:eba1236b9395
Child:
1:3ad74105ea9a
first commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
radnoumane 0:eba1236b9395 1 #include "mbed.h"
radnoumane 0:eba1236b9395 2 #include "main.h"
radnoumane 0:eba1236b9395 3 #include "sx1272-hal.h"
radnoumane 0:eba1236b9395 4 #include "debug.h"
radnoumane 0:eba1236b9395 5
radnoumane 0:eba1236b9395 6 /* Set this flag to '1' to display debug messages on the console */
radnoumane 0:eba1236b9395 7 #define DEBUG_MESSAGE 1
radnoumane 0:eba1236b9395 8
radnoumane 0:eba1236b9395 9 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
radnoumane 0:eba1236b9395 10 #define USE_MODEM_LORA 1
radnoumane 0:eba1236b9395 11 #define USE_MODEM_FSK !USE_MODEM_LORA
radnoumane 0:eba1236b9395 12
radnoumane 0:eba1236b9395 13 #define RF_FREQUENCY 868000000 // Hz
radnoumane 0:eba1236b9395 14 #define TX_OUTPUT_POWER 14 // 14 dBm
radnoumane 0:eba1236b9395 15
radnoumane 0:eba1236b9395 16 #if USE_MODEM_LORA == 1
radnoumane 0:eba1236b9395 17
radnoumane 0:eba1236b9395 18 #define LORA_BANDWIDTH 2 // [0: 125 kHz,
radnoumane 0:eba1236b9395 19 // 1: 250 kHz,
radnoumane 0:eba1236b9395 20 // 2: 500 kHz,
radnoumane 0:eba1236b9395 21 // 3: Reserved]
radnoumane 0:eba1236b9395 22 #define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
radnoumane 0:eba1236b9395 23 #define LORA_CODINGRATE 1 // [1: 4/5,
radnoumane 0:eba1236b9395 24 // 2: 4/6,
radnoumane 0:eba1236b9395 25 // 3: 4/7,
radnoumane 0:eba1236b9395 26 // 4: 4/8]
radnoumane 0:eba1236b9395 27 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
radnoumane 0:eba1236b9395 28 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
radnoumane 0:eba1236b9395 29 #define LORA_FIX_LENGTH_PAYLOAD_ON false
radnoumane 0:eba1236b9395 30 #define LORA_FHSS_ENABLED false
radnoumane 0:eba1236b9395 31 #define LORA_NB_SYMB_HOP 4
radnoumane 0:eba1236b9395 32 #define LORA_IQ_INVERSION_ON false
radnoumane 0:eba1236b9395 33 #define LORA_CRC_ENABLED true
radnoumane 0:eba1236b9395 34
radnoumane 0:eba1236b9395 35 #elif USE_MODEM_FSK == 1
radnoumane 0:eba1236b9395 36
radnoumane 0:eba1236b9395 37 #define FSK_FDEV 25000 // Hz
radnoumane 0:eba1236b9395 38 #define FSK_DATARATE 19200 // bps
radnoumane 0:eba1236b9395 39 #define FSK_BANDWIDTH 50000 // Hz
radnoumane 0:eba1236b9395 40 #define FSK_AFC_BANDWIDTH 83333 // Hz
radnoumane 0:eba1236b9395 41 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
radnoumane 0:eba1236b9395 42 #define FSK_FIX_LENGTH_PAYLOAD_ON false
radnoumane 0:eba1236b9395 43 #define FSK_CRC_ENABLED true
radnoumane 0:eba1236b9395 44
radnoumane 0:eba1236b9395 45 #else
radnoumane 0:eba1236b9395 46 #error "Please define a modem in the compiler options."
radnoumane 0:eba1236b9395 47 #endif
radnoumane 0:eba1236b9395 48
radnoumane 0:eba1236b9395 49 #define RX_TIMEOUT_VALUE 3500 // in ms
radnoumane 0:eba1236b9395 50 #define BUFFER_SIZE 32 // Define the payload size here
radnoumane 0:eba1236b9395 51
radnoumane 0:eba1236b9395 52 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
radnoumane 0:eba1236b9395 53 DigitalOut led( LED2 );
radnoumane 0:eba1236b9395 54 #else
radnoumane 0:eba1236b9395 55 DigitalOut led( LED1 );
radnoumane 0:eba1236b9395 56 #endif
radnoumane 0:eba1236b9395 57
radnoumane 0:eba1236b9395 58 /*
radnoumane 0:eba1236b9395 59 * Global variables declarations
radnoumane 0:eba1236b9395 60 */
radnoumane 0:eba1236b9395 61 typedef enum {
radnoumane 0:eba1236b9395 62 LOWPOWER = 0,
radnoumane 0:eba1236b9395 63 IDLE,
radnoumane 0:eba1236b9395 64
radnoumane 0:eba1236b9395 65 RX,
radnoumane 0:eba1236b9395 66 RX_TIMEOUT,
radnoumane 0:eba1236b9395 67 RX_ERROR,
radnoumane 0:eba1236b9395 68
radnoumane 0:eba1236b9395 69 TX,
radnoumane 0:eba1236b9395 70 TX_TIMEOUT,
radnoumane 0:eba1236b9395 71
radnoumane 0:eba1236b9395 72 CAD,
radnoumane 0:eba1236b9395 73 CAD_DONE
radnoumane 0:eba1236b9395 74 } AppStates_t;
radnoumane 0:eba1236b9395 75
radnoumane 0:eba1236b9395 76 volatile AppStates_t State = RX;
radnoumane 0:eba1236b9395 77
radnoumane 0:eba1236b9395 78 /*!
radnoumane 0:eba1236b9395 79 * Radio events function pointer
radnoumane 0:eba1236b9395 80 */
radnoumane 0:eba1236b9395 81 static RadioEvents_t RadioEvents;
radnoumane 0:eba1236b9395 82
radnoumane 0:eba1236b9395 83 /*
radnoumane 0:eba1236b9395 84 * Global variables declarations
radnoumane 0:eba1236b9395 85 */
radnoumane 0:eba1236b9395 86 SX1272MB2xAS Radio( NULL );
radnoumane 0:eba1236b9395 87
radnoumane 0:eba1236b9395 88 const uint8_t PingMsg[] = "PING";
radnoumane 0:eba1236b9395 89 const uint8_t PongMsg[] = "PONG";
radnoumane 0:eba1236b9395 90 const uint8_t Coucou[] = "Coucou";
radnoumane 0:eba1236b9395 91 const uint8_t Loulou[] = "Loulou";
radnoumane 0:eba1236b9395 92
radnoumane 0:eba1236b9395 93 uint16_t BufferSize = BUFFER_SIZE;
radnoumane 0:eba1236b9395 94 uint8_t Buffer[BUFFER_SIZE];
radnoumane 0:eba1236b9395 95
radnoumane 0:eba1236b9395 96 int16_t RssiValue = 0.0;
radnoumane 0:eba1236b9395 97 int8_t SnrValue = 0.0;
radnoumane 0:eba1236b9395 98
radnoumane 0:eba1236b9395 99
radnoumane 0:eba1236b9395 100 //lecture btn
radnoumane 0:eba1236b9395 101 DigitalIn mybutton(USER_BUTTON);
radnoumane 0:eba1236b9395 102
radnoumane 0:eba1236b9395 103 int main( void )
radnoumane 0:eba1236b9395 104 {
radnoumane 0:eba1236b9395 105 uint8_t i;
radnoumane 0:eba1236b9395 106 bool isMaster = true;
radnoumane 0:eba1236b9395 107
radnoumane 0:eba1236b9395 108 debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" );
radnoumane 0:eba1236b9395 109
radnoumane 0:eba1236b9395 110 // Initialize Radio driver
radnoumane 0:eba1236b9395 111 RadioEvents.TxDone = OnTxDone;
radnoumane 0:eba1236b9395 112 RadioEvents.RxDone = OnRxDone;
radnoumane 0:eba1236b9395 113 RadioEvents.RxError = OnRxError;
radnoumane 0:eba1236b9395 114 RadioEvents.TxTimeout = OnTxTimeout;
radnoumane 0:eba1236b9395 115 RadioEvents.RxTimeout = OnRxTimeout;
radnoumane 0:eba1236b9395 116 Radio.Init( &RadioEvents );
radnoumane 0:eba1236b9395 117
radnoumane 0:eba1236b9395 118 // verify the connection with the board
radnoumane 0:eba1236b9395 119 while( Radio.Read( REG_VERSION ) == 0x00 ) {
radnoumane 0:eba1236b9395 120 debug( "Radio could not be detected!\n\r", NULL );
radnoumane 0:eba1236b9395 121 wait( 1 );
radnoumane 0:eba1236b9395 122 }
radnoumane 0:eba1236b9395 123
radnoumane 0:eba1236b9395 124 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ), "\n\r > Board Type: SX1272MB2xAS < \n\r" );
radnoumane 0:eba1236b9395 125
radnoumane 0:eba1236b9395 126 Radio.SetChannel( RF_FREQUENCY );
radnoumane 0:eba1236b9395 127
radnoumane 0:eba1236b9395 128 #if USE_MODEM_LORA == 1
radnoumane 0:eba1236b9395 129
radnoumane 0:eba1236b9395 130 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r" );
radnoumane 0:eba1236b9395 131 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r" );
radnoumane 0:eba1236b9395 132
radnoumane 0:eba1236b9395 133 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
radnoumane 0:eba1236b9395 134 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
radnoumane 0:eba1236b9395 135 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
radnoumane 0:eba1236b9395 136 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
radnoumane 0:eba1236b9395 137 LORA_IQ_INVERSION_ON, 2000 );
radnoumane 0:eba1236b9395 138
radnoumane 0:eba1236b9395 139 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
radnoumane 0:eba1236b9395 140 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
radnoumane 0:eba1236b9395 141 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
radnoumane 0:eba1236b9395 142 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
radnoumane 0:eba1236b9395 143 LORA_IQ_INVERSION_ON, true );
radnoumane 0:eba1236b9395 144
radnoumane 0:eba1236b9395 145 #elif USE_MODEM_FSK == 1
radnoumane 0:eba1236b9395 146
radnoumane 0:eba1236b9395 147 debug("\n\n\r > FSK Mode < \n\n\r" );
radnoumane 0:eba1236b9395 148 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
radnoumane 0:eba1236b9395 149 FSK_DATARATE, 0,
radnoumane 0:eba1236b9395 150 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
radnoumane 0:eba1236b9395 151 FSK_CRC_ENABLED, 0, 0, 0, 2000 );
radnoumane 0:eba1236b9395 152
radnoumane 0:eba1236b9395 153 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
radnoumane 0:eba1236b9395 154 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
radnoumane 0:eba1236b9395 155 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
radnoumane 0:eba1236b9395 156 0, 0, false, true );
radnoumane 0:eba1236b9395 157
radnoumane 0:eba1236b9395 158 #else
radnoumane 0:eba1236b9395 159
radnoumane 0:eba1236b9395 160 #error "Please define a modem in the compiler options."
radnoumane 0:eba1236b9395 161
radnoumane 0:eba1236b9395 162 #endif
radnoumane 0:eba1236b9395 163
radnoumane 0:eba1236b9395 164 debug_if( DEBUG_MESSAGE, "Starting ON/OFF LED loop\r\n" );
radnoumane 0:eba1236b9395 165
radnoumane 0:eba1236b9395 166 led = 0;
radnoumane 0:eba1236b9395 167
radnoumane 0:eba1236b9395 168 Radio.Rx( RX_TIMEOUT_VALUE );
radnoumane 0:eba1236b9395 169
radnoumane 0:eba1236b9395 170 while( 1 ) {
radnoumane 0:eba1236b9395 171 switch( State ) {
radnoumane 0:eba1236b9395 172 case RX:
radnoumane 0:eba1236b9395 173 if( BufferSize > 0 ) {
radnoumane 0:eba1236b9395 174 if( strncmp( ( const char* )Buffer, ( const char* )Coucou, 6 ) == 0 ) {
radnoumane 0:eba1236b9395 175 debug("LED ON");
radnoumane 0:eba1236b9395 176 for(i=10;i<10;i++){
radnoumane 0:eba1236b9395 177 led = 1; // LED is ON
radnoumane 0:eba1236b9395 178 led = 0;
radnoumane 0:eba1236b9395 179 wait(0.2); // 200 ms
radnoumane 0:eba1236b9395 180 led = 0; // LED is OFF
radnoumane 0:eba1236b9395 181 led= 1;
radnoumane 0:eba1236b9395 182 wait(0.2); // 1 sec
radnoumane 0:eba1236b9395 183 }
radnoumane 0:eba1236b9395 184
radnoumane 0:eba1236b9395 185 // envoi de la réponse
radnoumane 0:eba1236b9395 186 strcpy( ( char* )Buffer, ( char* )Loulou );
radnoumane 0:eba1236b9395 187 // We fill the buffer with numbers for the payload
radnoumane 0:eba1236b9395 188 for( i = 6; i < BufferSize; i++ ) {
radnoumane 0:eba1236b9395 189 Buffer[i] = i - 6;
radnoumane 0:eba1236b9395 190 }
radnoumane 0:eba1236b9395 191 wait_ms( 10 );
radnoumane 0:eba1236b9395 192 Radio.Send( Buffer, BufferSize );
radnoumane 0:eba1236b9395 193 }
radnoumane 0:eba1236b9395 194 State = LOWPOWER;
radnoumane 0:eba1236b9395 195 break;
radnoumane 0:eba1236b9395 196 case TX:
radnoumane 0:eba1236b9395 197 debug("state = TX\r\n");
radnoumane 0:eba1236b9395 198 //led = !led;
radnoumane 0:eba1236b9395 199 Radio.Rx( RX_TIMEOUT_VALUE );
radnoumane 0:eba1236b9395 200 State = LOWPOWER;
radnoumane 0:eba1236b9395 201 break;
radnoumane 0:eba1236b9395 202 case RX_TIMEOUT:
radnoumane 0:eba1236b9395 203 debug("state = RX_TIMEOUT\r\n");
radnoumane 0:eba1236b9395 204 /* if( isMaster == true )
radnoumane 0:eba1236b9395 205 {
radnoumane 0:eba1236b9395 206 // Send the next PING frame
radnoumane 0:eba1236b9395 207 strcpy( ( char* )Buffer, ( char* )PingMsg );
radnoumane 0:eba1236b9395 208 for( i = 4; i < BufferSize; i++ )
radnoumane 0:eba1236b9395 209 {
radnoumane 0:eba1236b9395 210 Buffer[i] = i - 4;
radnoumane 0:eba1236b9395 211 }
radnoumane 0:eba1236b9395 212 wait_ms( 10 );
radnoumane 0:eba1236b9395 213 Radio.Send( Buffer, BufferSize );
radnoumane 0:eba1236b9395 214 }
radnoumane 0:eba1236b9395 215 else
radnoumane 0:eba1236b9395 216 {
radnoumane 0:eba1236b9395 217 Radio.Rx( RX_TIMEOUT_VALUE );
radnoumane 0:eba1236b9395 218 }*/
radnoumane 0:eba1236b9395 219 State = LOWPOWER;
radnoumane 0:eba1236b9395 220 break;
radnoumane 0:eba1236b9395 221 case RX_ERROR:
radnoumane 0:eba1236b9395 222 debug("state = RX_ERROR\r\n");
radnoumane 0:eba1236b9395 223 // We have received a Packet with a CRC error, send reply as if packet was correct
radnoumane 0:eba1236b9395 224 /* if( isMaster == true )
radnoumane 0:eba1236b9395 225 {
radnoumane 0:eba1236b9395 226 // Send the next PING frame
radnoumane 0:eba1236b9395 227 strcpy( ( char* )Buffer, ( char* )PingMsg );
radnoumane 0:eba1236b9395 228 for( i = 4; i < BufferSize; i++ )
radnoumane 0:eba1236b9395 229 {
radnoumane 0:eba1236b9395 230 Buffer[i] = i - 4;
radnoumane 0:eba1236b9395 231 }
radnoumane 0:eba1236b9395 232 wait_ms( 10 );
radnoumane 0:eba1236b9395 233 Radio.Send( Buffer, BufferSize );
radnoumane 0:eba1236b9395 234 }
radnoumane 0:eba1236b9395 235 else
radnoumane 0:eba1236b9395 236 {
radnoumane 0:eba1236b9395 237 // Send the next PONG frame
radnoumane 0:eba1236b9395 238 strcpy( ( char* )Buffer, ( char* )PongMsg );
radnoumane 0:eba1236b9395 239 for( i = 4; i < BufferSize; i++ )
radnoumane 0:eba1236b9395 240 {
radnoumane 0:eba1236b9395 241 Buffer[i] = i - 4;
radnoumane 0:eba1236b9395 242 }
radnoumane 0:eba1236b9395 243 wait_ms( 10 );
radnoumane 0:eba1236b9395 244 Radio.Send( Buffer, BufferSize );
radnoumane 0:eba1236b9395 245 }*/
radnoumane 0:eba1236b9395 246 State = LOWPOWER;
radnoumane 0:eba1236b9395 247 break;
radnoumane 0:eba1236b9395 248 case TX_TIMEOUT:
radnoumane 0:eba1236b9395 249 debug("state = TX_TIMEOUT\r\n");
radnoumane 0:eba1236b9395 250 Radio.Rx( RX_TIMEOUT_VALUE );
radnoumane 0:eba1236b9395 251 State = RX;
radnoumane 0:eba1236b9395 252 break;
radnoumane 0:eba1236b9395 253 case LOWPOWER:
radnoumane 0:eba1236b9395 254 // debug("state = LOWPOWER\r\n");
radnoumane 0:eba1236b9395 255 //State = RX;
radnoumane 0:eba1236b9395 256 break;
radnoumane 0:eba1236b9395 257 default:
radnoumane 0:eba1236b9395 258 debug("default\r\n");
radnoumane 0:eba1236b9395 259 State = RX;
radnoumane 0:eba1236b9395 260 break;
radnoumane 0:eba1236b9395 261 }
radnoumane 0:eba1236b9395 262
radnoumane 0:eba1236b9395 263
radnoumane 0:eba1236b9395 264 }
radnoumane 0:eba1236b9395 265
radnoumane 0:eba1236b9395 266
radnoumane 0:eba1236b9395 267
radnoumane 0:eba1236b9395 268 }
radnoumane 0:eba1236b9395 269
radnoumane 0:eba1236b9395 270 void OnTxDone( void )
radnoumane 0:eba1236b9395 271 {
radnoumane 0:eba1236b9395 272 Radio.Sleep( );
radnoumane 0:eba1236b9395 273 State = TX;
radnoumane 0:eba1236b9395 274 debug_if( DEBUG_MESSAGE, "> Transmission terminee\n\r" );
radnoumane 0:eba1236b9395 275 }
radnoumane 0:eba1236b9395 276
radnoumane 0:eba1236b9395 277 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
radnoumane 0:eba1236b9395 278 {
radnoumane 0:eba1236b9395 279 Radio.Sleep( );
radnoumane 0:eba1236b9395 280 BufferSize = size;
radnoumane 0:eba1236b9395 281 memcpy( Buffer, payload, BufferSize );
radnoumane 0:eba1236b9395 282 RssiValue = rssi;
radnoumane 0:eba1236b9395 283 SnrValue = snr;
radnoumane 0:eba1236b9395 284 State = RX;
radnoumane 0:eba1236b9395 285 debug_if( DEBUG_MESSAGE, "> Reception terminee \n\r" );
radnoumane 0:eba1236b9395 286 }
radnoumane 0:eba1236b9395 287
radnoumane 0:eba1236b9395 288 void OnTxTimeout( void )
radnoumane 0:eba1236b9395 289 {
radnoumane 0:eba1236b9395 290 Radio.Sleep( );
radnoumane 0:eba1236b9395 291 State = TX_TIMEOUT;
radnoumane 0:eba1236b9395 292 debug_if( DEBUG_MESSAGE, "> Transmission timeout\n\r" );
radnoumane 0:eba1236b9395 293 }
radnoumane 0:eba1236b9395 294
radnoumane 0:eba1236b9395 295 void OnRxTimeout( void )
radnoumane 0:eba1236b9395 296 {
radnoumane 0:eba1236b9395 297 Radio.Sleep( );
radnoumane 0:eba1236b9395 298 Buffer[BufferSize] = 0;
radnoumane 0:eba1236b9395 299 State = RX_TIMEOUT;
radnoumane 0:eba1236b9395 300 debug_if( DEBUG_MESSAGE, "> Reception timeout\n\r" );
radnoumane 0:eba1236b9395 301 }
radnoumane 0:eba1236b9395 302
radnoumane 0:eba1236b9395 303 void OnRxError( void )
radnoumane 0:eba1236b9395 304 {
radnoumane 0:eba1236b9395 305 Radio.Sleep( );
radnoumane 0:eba1236b9395 306 State = RX_ERROR;
radnoumane 0:eba1236b9395 307 debug_if( DEBUG_MESSAGE, "> Erreur de reception \n\r" );
radnoumane 0:eba1236b9395 308 }