Test program for the Nordic Semi nRF24L01 Transceiver Module (http://www.sparkfun.com/products/691), talking to another module connected to SparkFun\'s Nordic Serial Interface Board (http://www.sparkfun.com/products/9019).

Dependencies:   mbed nRF24L01P

Revision:
6:c55f867da01d
Parent:
4:7fa578849b23
Child:
7:8099b996329b
--- a/main.cpp	Tue Dec 18 10:59:37 2018 +0000
+++ b/main.cpp	Wed Dec 19 11:46:25 2018 +0000
@@ -4,8 +4,9 @@
 Serial pc(USBTX, USBRX); // tx, rx
 
 nRF24L01P my_nrf24l01p(D11, D12, D13, D8, D9, D7);
+bool receiver = true;
 
-#define TRANSFER_SIZE   4
+#define TRANSFER_SIZE   2
 
 void setDefaults() {
     my_nrf24l01p.setRfFrequency(DEFAULT_NRF24L01P_RF_FREQUENCY);
@@ -32,7 +33,34 @@
     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
-    pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
+    pc.printf( "(transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
+}
+
+void senderTest(int frequency, int outputPower, int dataRate, bool art) {
+    my_nrf24l01p.setRfFrequency(frequency);
+    my_nrf24l01p.setRfOutputPower(outputPower);
+    my_nrf24l01p.setAirDataRate(dateRate);
+    
+    int txData[TRANSFER_SIZE];
+    int txDataCnt = 0;
+    
+    pc.printf("--- SETTINGS: ---")
+    printSettings();
+    pc.printf("-----------------")
+    
+    for (int i = 0; i < 1000; i++) {
+        txData[txDataCnt++] = i % 100;
+        if (txDataCnt >= sizeof(txData)) {
+            // Send the transmitbuffer via the nRF24L01+
+            my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
+            txDataCnt = 0;
+        }
+    }
+    pc.printf("Finished run!");
+}
+
+int syncSettings(int frequency, int outputPower, int dataRate, bool art) {
+    
 }
 
 int main() {
@@ -41,53 +69,61 @@
 //  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
 //  only handles 4 byte transfers in the ATMega code.
 
-    char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
-    int txDataCnt = 0;
-    int rxDataCnt = 0;
+    char rxData[TRANSFER_SIZE];
 
     my_nrf24l01p.powerUp();
     
-    setFrequency(2450);
-    
-    printSettings();
-    my_nrf24l01p.enableAutoAcknowledge(NRF24L01P_PIPE_P0);
-    my_nrf24l01p.enableAutoRetransmit(100000, 10);
+//    my_nrf24l01p.enableAutoAcknowledge(NRF24L01P_PIPE_P0);
+//    my_nrf24l01p.enableAutoRetransmit(100000, 10);
     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
 
     my_nrf24l01p.setReceiveMode();
     my_nrf24l01p.enable();
-
-    while (1) {
-
-        // If we've received anything over the host serial link...
-        if ( pc.readable() ) {
-
-            // ...add it to the transmit buffer
-            txData[txDataCnt++] = pc.getc();
-
-            // If the transmit buffer is full
-            if ( txDataCnt >= sizeof( txData ) ) {
-
-                // Send the transmitbuffer via the nRF24L01+
-                my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
-
-                txDataCnt = 0;
+    
+    int data[1000];
+    if (receiver) {
+        int rxPacketCnt = 0;
+        int rxDataCnt = 0;
+        int i = 0;
+        bool reading = true;
+        pc.printf(">> Receiving Modus << \n\r");
+        while (reading) {
+            if ( my_nrf24l01p.readable() ) {
+                rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
+                for (int i = 0; i < rxDataCnt; i++) {
+                    i = rxData[i];
+                    if (i == 113) {
+                        if (rxPacketCnt == 0) {
+                            pc.printf("\n\r-----------\n\r");
+                            pc.printf("DONE!: length = %d \r\n", rxPacketCnt);
+                            pc.printf("-----------\n\r");
+                            rxPacketCnt = 0;     
+                        }
+                    } else {
+                        data[rxPacketCnt++] = i;
+                    }
+                }
+                rxDataCnt = 0;
             }
-
-        }
-
-        // If we've received anything in the nRF24L01+...
-        if ( my_nrf24l01p.readable() ) {
-
-            // ...read the data into the receive buffer
-            rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
-
-            // Display the receive buffer contents via the host serial link
-            for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
-
-                pc.putc( rxData[i] );
-            }
-
-        }
+        }             
+    }
+    else { // Transmit mode
+       pc.printf(">> Sending Modus << \n\r");
+       // Frequency test
+       pc.printf("FREQUENCY TEST");
+       senderTest(2400, NRF24L01P_TX_PWR_ZERO_DB, NRF24L01P_DATARATE_2_MBPS, false);
+       senderTest(2463, NRF24L01P_TX_PWR_ZERO_DB, NRF24L01P_DATARATE_2_MBPS, false);
+       senderTest(2525, NRF24L01P_TX_PWR_ZERO_DB, NRF24L01P_DATARATE_2_MBPS, false);
+       // Power (dBm) test
+       pc.printf("OUTPUT POWER TEST");
+       senderTest(2400, NRF24L01P_TX_PWR_ZERO_DB, NRF24L01P_DATARATE_2_MBPS, false);
+       senderTest(2400, NRF24L01P_TX_PWR_MINUS_6_DB, NRF24L01P_DATARATE_2_MBPS, false);
+       senderTest(2400, NRF24L01P_TX_PWR_MINUS_12_DB, NRF24L01P_DATARATE_2_MBPS, false);
+       senderTest(2400, NRF24L01P_TX_PWR_MINUS_18_DB, NRF24L01P_DATARATE_2_MBPS, false);
+       // Data rate test
+       pc.printf("DATARATE TEST");
+       senderTest(2400, NRF24L01P_TX_PWR_ZERO_DB, NRF24L01P_DATARATE_250_KBPS, false);
+       senderTest(2400, NRF24L01P_TX_PWR_ZERO_DB, NRF24L01P_DATARATE_1_MBPS, false);
+       senderTest(2400, NRF24L01P_TX_PWR_ZERO_DB, NRF24L01P_DATARATE_2_MBPS, false);
     }
 }
\ No newline at end of file