p2p with rssi value

Dependents:   Lora_SX1272_serial_apr29-rssi

Revision:
3:82630593359c
Parent:
2:a5a72d30cb18
Child:
4:f77a79f4239a
diff -r a5a72d30cb18 -r 82630593359c SX1272.cpp
--- a/SX1272.cpp	Tue Feb 06 09:58:17 2018 +0000
+++ b/SX1272.cpp	Fri Aug 30 19:00:41 2019 +0000
@@ -105,9 +105,16 @@
 //ajoute par C.DUPATY
 //Serial pc(USBTX, USBRX); // tx, rx
 // config pour SX1272MB2xAS sur NUCLEO-L073RZ
-SPI spi(SPI_MOSI,SPI_MISO,SPI_SCK);; // PA_7, PA_6, PA_5
-DigitalOut ss(SPI_CS);     			//(PB_6)
-DigitalOut rst(PA_0);					
+
+
+#define SX1272_debug_mode 0
+
+
+//Coragem pins
+SPI spi(P0_4,P0_6,P0_8);; // PA_7, PA_6, PA_5
+DigitalOut ss(P0_26);     			//(PB_6)
+DigitalOut rst(P1_15);		
+
 
 //**********************************************************************/
 // Public functions.
@@ -136,7 +143,7 @@
     _retries = 0;
     // added by C. Pham
     _defaultSyncWord=0x12;
-    _rawFormat=false;
+    _rawFormat=true;//____________________________________________________
     _extendedIFS=true;
     _RSSIonSend=true;
     // disabled by default
@@ -202,6 +209,8 @@
  Function: Sets the module ON.
  Returns: uint8_t setLORA state
 */
+//uint8_t SX1272::Read()
+//readRegister(
 uint8_t SX1272::ON()
 {
     uint8_t state = 2;
@@ -211,6 +220,7 @@
     printf("Starting 'ON'\n");
 #endif
 
+
     // Powering the module
 //    pinMode(SX1272_SS,OUTPUT);
 //    digitalWrite(SX1272_SS,HIGH);
@@ -252,30 +262,36 @@
 	rst=0;
 
     // from single_chan_pkt_fwd by Thomas Telkamp
+    
+    //__________________________lendo todos os registradores________________________
+  //  for(char i=0;i<100;i++){
+//    	int value =readRegister(i);
+////    	printf("reg%d =%d ",i,value);
+//    }
+    readRegister(0);//evitar falsos positivos
     uint8_t version = readRegister(REG_VERSION);
 
-    if (version == 0x22) {
+    if (version == 0x22) { //sx1272 ?
         // sx1272
-        printf("SX1272 detected, starting\n");
+        printf("\nSX1272 detected, starting\n");
         _board = SX1272Chip;
-    } else {
-        // sx1276?
-    //    digitalWrite(SX1272_RST, LOW);
-    	rst=0;
-        wait_ms(100);
-       // digitalWrite(SX1272_RST, HIGH);
-       rst=1;
-        wait_ms(100);
-        version = readRegister(REG_VERSION);
-        if (version == 0x12) {
-            // sx1276
-            printf("SX1276 detected, starting\n");
-            _board = SX1276Chip;
-        } else {
-            printf("Unrecognized transceiver\n");
-        }
-    }
-    // end from single_chan_pkt_fwd by Thomas Telkamp
+    }
+    else if (version ==0x12) {// sx1276?
+		printf("SX1276 detected, starting\n");
+		_board = SX1276Chip;
+	}
+	else {
+//    	rst=0;
+//        wait_ms(100);
+//		rst=1;
+//        wait_ms(100);
+		
+		while(1){
+	        printf("Unrecognized transceiver\n");
+	        wait_ms(2000);
+		}
+    }
+    
 
     // added by C. Pham
     RxChainCalibration();
@@ -321,7 +337,7 @@
     if (_board==SX1272Chip)
         // RFIO_pin RFU OutputPower
         // 0 000 0000
-        writeRegister(0x9,0x0);
+        writeRegister(0x9,0x8F);// was 0, 8F gives max outputpower
     else
         // RFO_pin MaxP OutputPower
         // 0 100 1111
@@ -441,6 +457,9 @@
     printf("\n");
 #endif
     //end
+    
+
+//   	printf("reg1=%d \n",readRegister(1));
 
     return state;
 }
@@ -5279,6 +5298,7 @@
     printf("Starting 'setPacket'\n");
 #endif
 
+//	printf("dentro do setPacket payload = %s\n",payload);
     // added by C. Pham
     // check for enough remaining ToA
     // when operating under duty-cycle mode
@@ -5296,14 +5316,16 @@
     }
 
     clearFlags();	// Initializing flags
-
+	printf("setPacket1____________________\n");
     if( _modem == LORA )
     { // LoRa mode
         writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);	// Stdby LoRa mode to write in FIFO
+        printf("_modem == Lora\n");
     }
     else
     { // FSK mode
         writeRegister(REG_OP_MODE, FSK_STANDBY_MODE);	// Stdby FSK mode to write in FIFO
+        printf("_modem == FSK\n");
     }
 
     _reception = CORRECT_PACKET;	// Updating incorrect value
@@ -5355,7 +5377,7 @@
         packet_sent.netkey[0]=_my_netkey[0];
         packet_sent.netkey[1]=_my_netkey[1];
         //#if (SX1272_debug_mode > 0)
-        printf("## Setting net key ##");
+//        printf("## Setting net key ##");
         //#endif
         writeRegister(REG_FIFO, packet_sent.netkey[0]);
         writeRegister(REG_FIFO, packet_sent.netkey[1]);
@@ -5363,9 +5385,10 @@
         // added by C. Pham
         // we can skip the header for instance when we want to generate
         // at a higher layer a LoRaWAN packet
+            writeRegister(0x01,129);//standby mode
         if (!_rawFormat) {
             writeRegister(REG_FIFO, packet_sent.dst); 		// Writing the destination in FIFO
-            // added by C. Pham
+//            // added by C. Pham
             writeRegister(REG_FIFO, packet_sent.type); 		// Writing the packet type in FIFO
             writeRegister(REG_FIFO, packet_sent.src);		// Writing the source in FIFO
             writeRegister(REG_FIFO, packet_sent.packnum);	// Writing the packet number in FIFO
@@ -5374,13 +5397,16 @@
         //writeRegister(REG_FIFO, packet_sent.length); 	// Writing the packet length in FIFO
         for(unsigned int i = 0; i < _payloadlength; i++)
         {
-            writeRegister(REG_FIFO, packet_sent.data[i]);  // Writing the payload in FIFO
+             writeRegister(REG_FIFO, packet_sent.data[i]);  // Writing the payload in FIFO
         }
         // commented by C. Pham
         //writeRegister(REG_FIFO, packet_sent.retry);		// Writing the number retry in FIFO
+ //       for (int i=0 ; i<255 ; i++)
+//        	printf("%d ",readRegister(REG_FIFO));
+//        printf("/n");
         state = 0;
 #if (SX1272_debug_mode > 0)
-        printf("## Packet set and written in FIFO ##");
+        printf("in FIFO ##");
         // Print the complete packet if debug_mode
         printf("## Packet to send: \n");
         printf("Destination: %d\n",packet_sent.dst);
@@ -5420,7 +5446,8 @@
 {
     int8_t state = 2;
     byte st0;
-
+    
+//    sx1272.writeRegister(0x01,129);	//standby mode
 #if (SX1272_debug_mode > 1)
     printf("\n");
     printf("Starting 'setPacket'\n");
@@ -5446,16 +5473,19 @@
 
     st0 = readRegister(REG_OP_MODE);	// Save the previous status
     clearFlags();	// Initializing flags
-
+//	printf("setPacket_2_____________________\n");
+//	printf("mode =%d\n",readRegister(REG_OP_MODE)&3);
     if( _modem == LORA )
     { // LoRa mode
         writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);	// Stdby LoRa mode to write in FIFO
+//        printf("setpacket_2 LORA\n");
     }
     else
     { // FSK mode
         writeRegister(REG_OP_MODE, FSK_STANDBY_MODE);	// Stdby FSK mode to write in FIFO
-    }
-
+        printf("setpacket_2 FSK\n");
+    }
+//	printf("mode =%d\n",readRegister(REG_OP_MODE)&3);
     _reception = CORRECT_PACKET;	// Updating incorrect value to send a packet (old or new)
     if( _retries == 0 )
     { // Sending new packet
@@ -5494,7 +5524,6 @@
     if (_requestACK)
         packet_sent.type |= PKT_FLAG_ACK_REQ;
 #endif
-
     writeRegister(REG_FIFO_ADDR_PTR, 0x80);  // Setting address pointer in FIFO data buffer
     if( state == 0 )
     {
@@ -5502,6 +5531,7 @@
         // Writing packet to send in FIFO
 #ifdef W_NET_KEY
         // added by C. Pham
+
         packet_sent.netkey[0]=_my_netkey[0];
         packet_sent.netkey[1]=_my_netkey[1];
         //#if (SX1272_debug_mode > 0)
@@ -5513,6 +5543,7 @@
         // added by C. Pham
         // we can skip the header for instance when we want to generate
         // at a higher layer a LoRaWAN packet
+
         if (!_rawFormat) {
             writeRegister(REG_FIFO, packet_sent.dst); 		// Writing the destination in FIFO
             // added by C. Pham
@@ -5520,12 +5551,17 @@
             writeRegister(REG_FIFO, packet_sent.src);		// Writing the source in FIFO
             writeRegister(REG_FIFO, packet_sent.packnum);	// Writing the packet number in FIFO
         }
+        this->packet_sent.packnum++;
         // commented by C. Pham
         //writeRegister(REG_FIFO, packet_sent.length); 	// Writing the packet length in FIFO
         for(unsigned int i = 0; i < _payloadlength; i++)
         {
             writeRegister(REG_FIFO, packet_sent.data[i]);  // Writing the payload in FIFO
         }
+//        printf("FIFO depois da escrita\n");
+//        for(int i=0; i!= 255 ;i++)
+//            	printf("%c",readRegister(REG_FIFO));
+//        printf("\n");
         // commented by C. Pham
         //writeRegister(REG_FIFO, packet_sent.retry);		// Writing the number retry in FIFO
         state = 0;
@@ -5555,7 +5591,7 @@
         printf("##");
 #endif
     }
-    writeRegister(REG_OP_MODE, st0);	// Getting back to previous status
+    //writeRegister(REG_OP_MODE, st0);	// Getting back to previous status
     return state;
 }
 
@@ -5747,15 +5783,15 @@
 {
     uint8_t state = 2;
     uint8_t state_f = 2;
-
+//	printf("chegou no sendPacketTimeout\n");
 #if (SX1272_debug_mode > 1)
     printf("\n");
     printf("Starting 'sendPacketTimeout'\n");
 #endif
-
     state = truncPayload(length16);
     if( state == 0 )
     {
+//        printf("vai pro setpacket\n");
         state_f = setPacket(dest, payload);	// Setting a packet with 'dest' destination
     }												// and writing it in FIFO.
     else
@@ -6460,7 +6496,7 @@
 
 #if (SX1272_debug_mode > 1)
     printf("## Temperature is: %d ",_temp);
-  //  Serial.print(_temp);
+    Serial.print(_temp);
     printf(" ##");
     printf("\n");
 #endif
@@ -6779,21 +6815,21 @@
                 e = doCAD(_send_cad_number);
                 _endDoCad=millis();
 
-                printf("--> CAD duration %d\n",_endDoCad-_startDoCad);
+//                printf("--> CAD duration %d\n",_endDoCad-_startDoCad);
              //   Serial.print(_endDoCad-_startDoCad);
-                printf("\n");
+//                printf("\n");
 
                 if (!e) {
-                    printf("OK1\n");
+//                    printf("OK1\n");
 
                     if (_extendedIFS)  {
                         // wait for random number of CAD
                        // uint8_t w = random(1,8);
                        	// ajoute par C.Dupaty
 						uint8_t w = rand()%8+1;
-                        printf("--> waiting for %d\n",w);
+//                        printf("--> waiting for %d\n",w);
                        // Serial.print(w);
-                        printf(" CAD = %d\n",sx1272_CAD_value[_loraMode]*w);
+//                        printf(" CAD = %d\n",sx1272_CAD_value[_loraMode]*w);
                    //     Serial.print(sx1272_CAD_value[_loraMode]*w);
                         printf("\n");
 
@@ -6804,16 +6840,16 @@
                         e = doCAD(_send_cad_number);
                         _endDoCad=millis();
 
-                        printf("--> CAD duration %d\n",_endDoCad-_startDoCad);
+//                        printf("--> CAD duration %d\n",_endDoCad-_startDoCad);
                    //     Serial.print(_endDoCad-_startDoCad);
-                        printf("\n");
-
-                        if (!e)
-                            printf("OK2");
-                        else
-                            printf("###2");
-
-                        printf("\n");
+//                        printf("\n");
+
+//                        if (!e)
+////                            printf("OK2");
+//                        else
+//                            printf("###2");
+//
+//                        printf("\n");
                     }
                 }
                 else {
@@ -6822,7 +6858,7 @@
                     // wait for random number of DIFS
                     uint8_t w = rand()%8+1;
 
-                    printf("--> waiting for %d\n",w);
+//                    printf("--> waiting for %d\n",w);
                    // Serial.print(w);
                     printf(" DIFS (DIFS=3SIFS) = %d\n",sx1272_SIFS_value[_loraMode]*3*w);
                   //  Serial.print(sx1272_SIFS_value[_loraMode]*3*w);