Peer to peer connection to switch on a led on slave

Dependencies:   SX1272Lib mbed

Committer:
radnoumane
Date:
Wed Mar 21 13:43:48 2018 +0000
Revision:
1:3ad74105ea9a
Parent:
0:eba1236b9395
code master ok;

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 1:3ad74105ea9a 76 volatile AppStates_t State = LOWPOWER;
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 1:3ad74105ea9a 171
radnoumane 0:eba1236b9395 172 switch( State ) {
radnoumane 0:eba1236b9395 173 case RX:
radnoumane 1:3ad74105ea9a 174 debug("RX ?");
radnoumane 1:3ad74105ea9a 175 if( BufferSize > 0 ) { //buffer contient des données
radnoumane 1:3ad74105ea9a 176 if( strncmp( ( const char* )Buffer, ( const char* )Loulou, 6 ) == 0 ) {
radnoumane 1:3ad74105ea9a 177 debug("LED OK \r\n...");
radnoumane 1:3ad74105ea9a 178
radnoumane 0:eba1236b9395 179 }
radnoumane 1:3ad74105ea9a 180 }
radnoumane 0:eba1236b9395 181 State = LOWPOWER;
radnoumane 0:eba1236b9395 182 break;
radnoumane 0:eba1236b9395 183 case TX:
radnoumane 1:3ad74105ea9a 184
radnoumane 1:3ad74105ea9a 185 //debug("state = TX\r\n");
radnoumane 0:eba1236b9395 186 //led = !led;
radnoumane 1:3ad74105ea9a 187 if (mybutton == 0) {
radnoumane 1:3ad74105ea9a 188 debug("Coucou\r\n");
radnoumane 1:3ad74105ea9a 189 debug("TX \r\n");
radnoumane 1:3ad74105ea9a 190 strcpy( ( char* )Buffer, ( char* )Coucou );
radnoumane 1:3ad74105ea9a 191 for( i = 6; i < BufferSize; i++ ) {
radnoumane 1:3ad74105ea9a 192 Buffer[i] = i - 6;
radnoumane 1:3ad74105ea9a 193 }
radnoumane 1:3ad74105ea9a 194 wait_ms( 10 );
radnoumane 1:3ad74105ea9a 195 Radio.Send( Buffer, BufferSize );
radnoumane 1:3ad74105ea9a 196 //debug(State);
radnoumane 1:3ad74105ea9a 197 }
radnoumane 0:eba1236b9395 198 Radio.Rx( RX_TIMEOUT_VALUE );
radnoumane 0:eba1236b9395 199 State = LOWPOWER;
radnoumane 0:eba1236b9395 200 break;
radnoumane 0:eba1236b9395 201 case RX_TIMEOUT:
radnoumane 0:eba1236b9395 202 debug("state = RX_TIMEOUT\r\n");
radnoumane 1:3ad74105ea9a 203 if (mybutton == 0) {
radnoumane 1:3ad74105ea9a 204 debug("\r\n");
radnoumane 1:3ad74105ea9a 205 debug("allumage Led \r\n");
radnoumane 1:3ad74105ea9a 206 strcpy( ( char* )Buffer, ( char* )Coucou );
radnoumane 1:3ad74105ea9a 207 for( i = 6; i < BufferSize; i++ ) {
radnoumane 1:3ad74105ea9a 208 Buffer[i] = i - 6;
radnoumane 1:3ad74105ea9a 209 }
radnoumane 1:3ad74105ea9a 210 wait_ms( 10 );
radnoumane 1:3ad74105ea9a 211 Radio.Send( Buffer, BufferSize );
radnoumane 1:3ad74105ea9a 212 //debug(State);
radnoumane 1:3ad74105ea9a 213 } else {
radnoumane 1:3ad74105ea9a 214 Radio.Rx( RX_TIMEOUT_VALUE );
radnoumane 1:3ad74105ea9a 215 }
radnoumane 0:eba1236b9395 216 State = LOWPOWER;
radnoumane 0:eba1236b9395 217 break;
radnoumane 0:eba1236b9395 218 case RX_ERROR:
radnoumane 0:eba1236b9395 219 debug("state = RX_ERROR\r\n");
radnoumane 0:eba1236b9395 220 State = LOWPOWER;
radnoumane 0:eba1236b9395 221 break;
radnoumane 0:eba1236b9395 222 case TX_TIMEOUT:
radnoumane 0:eba1236b9395 223 debug("state = TX_TIMEOUT\r\n");
radnoumane 0:eba1236b9395 224 Radio.Rx( RX_TIMEOUT_VALUE );
radnoumane 1:3ad74105ea9a 225 State = LOWPOWER;
radnoumane 0:eba1236b9395 226 break;
radnoumane 0:eba1236b9395 227 case LOWPOWER:
radnoumane 0:eba1236b9395 228 // debug("state = LOWPOWER\r\n");
radnoumane 1:3ad74105ea9a 229 //State = TX;
radnoumane 0:eba1236b9395 230 break;
radnoumane 0:eba1236b9395 231 default:
radnoumane 0:eba1236b9395 232 debug("default\r\n");
radnoumane 1:3ad74105ea9a 233 State = LOWPOWER;
radnoumane 0:eba1236b9395 234 break;
radnoumane 0:eba1236b9395 235 }
radnoumane 0:eba1236b9395 236
radnoumane 0:eba1236b9395 237 }
radnoumane 0:eba1236b9395 238
radnoumane 0:eba1236b9395 239 }
radnoumane 0:eba1236b9395 240
radnoumane 0:eba1236b9395 241 void OnTxDone( void )
radnoumane 0:eba1236b9395 242 {
radnoumane 0:eba1236b9395 243 Radio.Sleep( );
radnoumane 0:eba1236b9395 244 State = TX;
radnoumane 0:eba1236b9395 245 debug_if( DEBUG_MESSAGE, "> Transmission terminee\n\r" );
radnoumane 0:eba1236b9395 246 }
radnoumane 0:eba1236b9395 247
radnoumane 0:eba1236b9395 248 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
radnoumane 0:eba1236b9395 249 {
radnoumane 0:eba1236b9395 250 Radio.Sleep( );
radnoumane 0:eba1236b9395 251 BufferSize = size;
radnoumane 0:eba1236b9395 252 memcpy( Buffer, payload, BufferSize );
radnoumane 0:eba1236b9395 253 RssiValue = rssi;
radnoumane 0:eba1236b9395 254 SnrValue = snr;
radnoumane 0:eba1236b9395 255 State = RX;
radnoumane 0:eba1236b9395 256 debug_if( DEBUG_MESSAGE, "> Reception terminee \n\r" );
radnoumane 0:eba1236b9395 257 }
radnoumane 0:eba1236b9395 258
radnoumane 0:eba1236b9395 259 void OnTxTimeout( void )
radnoumane 0:eba1236b9395 260 {
radnoumane 0:eba1236b9395 261 Radio.Sleep( );
radnoumane 0:eba1236b9395 262 State = TX_TIMEOUT;
radnoumane 0:eba1236b9395 263 debug_if( DEBUG_MESSAGE, "> Transmission timeout\n\r" );
radnoumane 0:eba1236b9395 264 }
radnoumane 0:eba1236b9395 265
radnoumane 0:eba1236b9395 266 void OnRxTimeout( void )
radnoumane 0:eba1236b9395 267 {
radnoumane 0:eba1236b9395 268 Radio.Sleep( );
radnoumane 0:eba1236b9395 269 Buffer[BufferSize] = 0;
radnoumane 0:eba1236b9395 270 State = RX_TIMEOUT;
radnoumane 0:eba1236b9395 271 debug_if( DEBUG_MESSAGE, "> Reception timeout\n\r" );
radnoumane 0:eba1236b9395 272 }
radnoumane 0:eba1236b9395 273
radnoumane 0:eba1236b9395 274 void OnRxError( void )
radnoumane 0:eba1236b9395 275 {
radnoumane 0:eba1236b9395 276 Radio.Sleep( );
radnoumane 0:eba1236b9395 277 State = RX_ERROR;
radnoumane 0:eba1236b9395 278 debug_if( DEBUG_MESSAGE, "> Erreur de reception \n\r" );
radnoumane 0:eba1236b9395 279 }