![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
PingPong_REC
Dependencies: mbed BufferedSerial SX1276GenericLib
Revision 12:e0677648aa14, committed 2019-03-14
- Comitter:
- lenriquez389
- Date:
- Thu Mar 14 11:21:55 2019 +0000
- Parent:
- 11:9d7409ebfa57
- Commit message:
- RECEPTION ER4
Changed in this revision
SX1276GenericPingPong/GenericPingPong.cpp | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 9d7409ebfa57 -r e0677648aa14 SX1276GenericPingPong/GenericPingPong.cpp --- a/SX1276GenericPingPong/GenericPingPong.cpp Fri Aug 18 07:45:44 2017 +0000 +++ b/SX1276GenericPingPong/GenericPingPong.cpp Thu Mar 14 11:21:55 2019 +0000 @@ -4,7 +4,7 @@ * (c) 2017 Helmut Tschemernjak * 30826 Garbsen (Hannover) Germany */ - + #include "mbed.h" #include "PinMap.h" #include "GenericPingPong.h" @@ -31,11 +31,11 @@ #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx #define LORA_SYMBOL_TIMEOUT 5 // Symbols #define LORA_FIX_LENGTH_PAYLOAD_ON false -#define LORA_FHSS_ENABLED false -#define LORA_NB_SYMB_HOP 4 +#define LORA_FHSS_ENABLED false +#define LORA_NB_SYMB_HOP 4 #define LORA_IQ_INVERSION_ON false #define LORA_CRC_ENABLED true - + #elif USE_MODEM_FSK == 1 #define FSK_FDEV 25000 // Hz @@ -45,10 +45,10 @@ #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx #define FSK_FIX_LENGTH_PAYLOAD_ON false #define FSK_CRC_ENABLED true - + #else - #error "Please define a modem in the compiler options." -#endif +#error "Please define a modem in the compiler options." +#endif #define RX_TIMEOUT_VALUE 3500 // in ms @@ -59,20 +59,10 @@ /* * Global variables declarations */ -typedef enum -{ +typedef enum { LOWPOWER = 0, - IDLE, - - RX, - RX_TIMEOUT, - RX_ERROR, - - TX, - TX_TIMEOUT, - - CAD, - CAD_DONE + TRAITEMENT, + REPONSE } AppStates_t; volatile AppStates_t State = LOWPOWER; @@ -88,16 +78,16 @@ SX1276Generic *Radio; -const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING"; -const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG"; - +uint8_t SLV[] = { 'Z', 'Z'}; +uint8_t MTR[] = { 'X','X'}; +DigitalOut myled(LED1); uint16_t BufferSize = BUFFER_SIZE; uint8_t *Buffer; DigitalOut *led3; -int SX1276PingPong() +int SX1276PingPong() { #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) DigitalOut *led = new DigitalOut(LED2); @@ -108,25 +98,25 @@ DigitalOut *led = new DigitalOut(LED1); led3 = led; #endif - + Buffer = new uint8_t[BUFFER_SIZE]; *led3 = 1; #ifdef B_L072Z_LRWAN1_LORA Radio = new SX1276Generic(NULL, MURATA_SX1276, - LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, - LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, - LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); + LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, + LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5, + LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO); #else // RFM95 Radio = new SX1276Generic(NULL, RFM95_SX1276, - LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, - LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); + LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET, + LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5); #endif - + uint8_t i; - bool isMaster = true; + dprintf("SX1276 Ping Pong Demo Application" ); dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0); dprintf("TXPower: %d dBm", TX_OUTPUT_POWER); @@ -142,15 +132,15 @@ RadioEvents.RxDone = OnRxDone; RadioEvents.RxError = OnRxError; RadioEvents.TxTimeout = OnTxTimeout; - RadioEvents.RxTimeout = OnRxTimeout; + RadioEvents.RxTimeout = OnRxTimeout; if (Radio->Init( &RadioEvents ) == false) { while(1) { - dprintf("Radio could not be detected!"); - wait( 1 ); + dprintf("Radio could not be detected!"); + wait( 1 ); } } - + switch(Radio->DetectBoardType()) { case SX1276MB1LAS: if (DEBUG_MESSAGE) @@ -161,7 +151,7 @@ dprintf(" > Board Type: SX1276MB1LAS <"); case MURATA_SX1276: if (DEBUG_MESSAGE) - dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); + dprintf(" > Board Type: MURATA_SX1276_STM32L0 <"); break; case RFM95_SX1276: if (DEBUG_MESSAGE) @@ -171,204 +161,110 @@ dprintf(" > Board Type: unknown <"); } - Radio->SetChannel(RF_FREQUENCY ); + Radio->SetChannel(RF_FREQUENCY ); #if USE_MODEM_LORA == 1 - + if (LORA_FHSS_ENABLED) dprintf(" > LORA FHSS Mode <"); if (!LORA_FHSS_ENABLED) dprintf(" > LORA Mode <"); Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, - LORA_SPREADING_FACTOR, LORA_CODINGRATE, - LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, - LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, - LORA_IQ_INVERSION_ON, 2000 ); - + LORA_SPREADING_FACTOR, LORA_CODINGRATE, + LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, + LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, + LORA_IQ_INVERSION_ON, 2000 ); + Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, - LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, - LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, - LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, - LORA_IQ_INVERSION_ON, true ); - + LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, + LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, + LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, + LORA_IQ_INVERSION_ON, true ); + #elif USE_MODEM_FSK == 1 dprintf(" > FSK Mode <"); Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, - FSK_DATARATE, 0, - FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, - FSK_CRC_ENABLED, 0, 0, 0, 2000 ); - + FSK_DATARATE, 0, + FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, + FSK_CRC_ENABLED, 0, 0, 0, 2000 ); + Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, - 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, - 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, - 0, 0, false, true ); - + 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, + 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, + 0, 0, false, true ); + #else #error "Please define a modem in the compiler options." #endif - + if (DEBUG_MESSAGE) - dprintf("Starting Ping-Pong loop"); + dprintf("Starting Ping-Pong loop"); - + Radio->Rx( RX_TIMEOUT_VALUE ); - - while( 1 ) - { + + while( 1 ) { #ifdef TARGET_STM32L4 WatchDogUpdate(); -#endif - - switch( State ) - { - case RX: - *led3 = 0; - if( isMaster == true ) - { - if( BufferSize > 0 ) - { - if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 ) - { - *led = !*led; - dprintf( "...Pong" ); - // Send the next PING frame - memcpy(Buffer, PingMsg, sizeof(PingMsg)); - // We fill the buffer with numbers for the payload - for( i = sizeof(PingMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PingMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); - } - else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) - { // A master already exists then become a slave - dprintf( "...Ping" ); - *led = !*led; - isMaster = false; - // Send the next PONG frame - memcpy(Buffer, PongMsg, sizeof(PongMsg)); - // We fill the buffer with numbers for the payload - for( i = sizeof(PongMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PongMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); - } - else // valid reception but neither a PING or a PONG message - { // Set device as master ans start again - isMaster = true; - Radio->Rx( RX_TIMEOUT_VALUE ); - } - } - } - else - { - if( BufferSize > 0 ) - { - if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 ) - { - *led = !*led; - dprintf( "...Ping" ); - // Send the reply to the PING string - memcpy(Buffer, PongMsg, sizeof(PongMsg)); - // We fill the buffer with numbers for the payload - for( i = sizeof(PongMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PongMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); +#endif + //dprintf("%d",State); + switch( State ) { + + case LOWPOWER: + sleep(); + //dprintf("Escuchando\n\r"); + Radio->Rx( RX_TIMEOUT_VALUE ); + break; + case TRAITEMENT: + Radio->Sleep( ); + if( BufferSize > 0 ) { + if(Buffer[0]==MTR[0] && Buffer[1]==MTR[1] && Buffer[2]==SLV[0] && Buffer[3]==SLV[1]){ + dprintf("Me pedieron informacion\n\r"); + *led3 = 0; + myled=0; + dump("Data:", Buffer, BufferSize); + State=REPONSE; + } + else{ + State=LOWPOWER; + myled=1; + wait_ms( 20 ); + } + } + else{ + State=LOWPOWER; + } + break; + case REPONSE: + //Radio->Sleep( ); + + Buffer[0]=SLV[0]; + Buffer[1]=SLV[1]; + Buffer[2]=MTR[0]; + Buffer[3]=MTR[1]; + for( i = 4; i < BufferSize; i++ ) { + Buffer[i] = 0x50; } - else // valid reception but not a PING as expected - { // Set device as master and start again - isMaster = true; - Radio->Rx( RX_TIMEOUT_VALUE ); - } - } - } - State = LOWPOWER; - break; - case TX: - *led3 = 1; - if( isMaster == true ) - { - dprintf("Ping..." ); - } - else - { - dprintf("Pong..." ); - } - Radio->Rx( RX_TIMEOUT_VALUE ); - State = LOWPOWER; - break; - case RX_TIMEOUT: - if( isMaster == true ) - { - // Send the next PING frame - memcpy(Buffer, PingMsg, sizeof(PingMsg)); - for( i = sizeof(PingMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PingMsg); - } - wait_ms( 10 ); + + wait_ms( 20 ); Radio->Send( Buffer, BufferSize ); - } - else - { - Radio->Rx( RX_TIMEOUT_VALUE ); - } - State = LOWPOWER; - break; - case RX_ERROR: - // We have received a Packet with a CRC error, send reply as if packet was correct - if( isMaster == true ) - { - // Send the next PING frame - memcpy(Buffer, PingMsg, sizeof(PingMsg)); - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); - } - else - { - // Send the next PONG frame - memcpy(Buffer, PongMsg, sizeof(PongMsg)); - for( i = sizeof(PongMsg); i < BufferSize; i++ ) - { - Buffer[i] = i - sizeof(PongMsg); - } - wait_ms( 10 ); - Radio->Send( Buffer, BufferSize ); - } - State = LOWPOWER; - break; - case TX_TIMEOUT: - Radio->Rx( RX_TIMEOUT_VALUE ); - State = LOWPOWER; - break; - case LOWPOWER: - sleep(); - break; - default: - State = LOWPOWER; - break; - } + *led3 = 1; + break; + default: + State = LOWPOWER; + break; + } } } void OnTxDone(void *radio, void *userThisPtr, void *userData) { Radio->Sleep( ); - State = TX; + State = LOWPOWER; if (DEBUG_MESSAGE) dprintf("> OnTxDone"); } @@ -376,29 +272,29 @@ void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) { Radio->Sleep( ); - BufferSize = size; + //BufferSize = size; memcpy( Buffer, payload, BufferSize ); - State = RX; + State = TRAITEMENT; if (DEBUG_MESSAGE) dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr); - dump("Data:", payload, size); + //dump("Data:", payload, size); } void OnTxTimeout(void *radio, void *userThisPtr, void *userData) { - *led3 = 0; - Radio->Sleep( ); - State = TX_TIMEOUT; + //*led3 = 0; + Radio->Sleep( ); + State = LOWPOWER; if(DEBUG_MESSAGE) dprintf("> OnTxTimeout"); } void OnRxTimeout(void *radio, void *userThisPtr, void *userData) { - *led3 = 0; + //*led3 = 0; Radio->Sleep( ); Buffer[BufferSize-1] = 0; - State = RX_TIMEOUT; + State = LOWPOWER; if (DEBUG_MESSAGE) dprintf("> OnRxTimeout"); } @@ -406,7 +302,7 @@ void OnRxError(void *radio, void *userThisPtr, void *userData) { Radio->Sleep( ); - State = RX_ERROR; + State = LOWPOWER; if (DEBUG_MESSAGE) dprintf("> OnRxError"); }
diff -r 9d7409ebfa57 -r e0677648aa14 main.cpp --- a/main.cpp Fri Aug 18 07:45:44 2017 +0000 +++ b/main.cpp Thu Mar 14 11:21:55 2019 +0000 @@ -5,7 +5,6 @@ */ #include "main.h" -DigitalOut myled(LED1); BufferedSerial *ser; int main() { @@ -14,7 +13,7 @@ ser->baud(115200*2); ser->format(8); ser->printf("Hello World\n\r"); - myled = 1; + SX1276PingPong(); }