nRF24L01P Hello World example for mbed 6

Dependencies:   nRF24L01P

Committer:
wkleunen
Date:
Thu May 20 12:56:14 2021 +0000
Revision:
6:18ce87bf9df0
Parent:
4:6ec07c83786b
transfer size fixed and library update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Owen 0:a51a6e7da590 1 #include "mbed.h"
Owen 0:a51a6e7da590 2 #include "nRF24L01P.h"
Owen 0:a51a6e7da590 3
wkleunen 2:d7d36199d4e4 4 BufferedSerial pc(USBTX, USBRX); // tx, rx
Owen 0:a51a6e7da590 5
wkleunen 3:c3afd479d9d8 6 nRF24L01P my_nrf24l01p(D11, D12, D13, D8, D9, D7); // mosi, miso, sck, csn, ce, irq
Owen 0:a51a6e7da590 7
wkleunen 2:d7d36199d4e4 8 FileHandle *mbed::mbed_override_console(int fd)
wkleunen 2:d7d36199d4e4 9 {
wkleunen 2:d7d36199d4e4 10 return &pc;
wkleunen 2:d7d36199d4e4 11 }
wkleunen 2:d7d36199d4e4 12
Owen 0:a51a6e7da590 13 int main() {
wkleunen 6:18ce87bf9df0 14 enum { TRANSFER_SIZE = 1 };
Owen 0:a51a6e7da590 15
Owen 0:a51a6e7da590 16 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
Owen 0:a51a6e7da590 17 int txDataCnt = 0;
Owen 0:a51a6e7da590 18 int rxDataCnt = 0;
Owen 0:a51a6e7da590 19
Owen 0:a51a6e7da590 20 my_nrf24l01p.powerUp();
Owen 0:a51a6e7da590 21
Owen 0:a51a6e7da590 22 // Display the (default) setup of the nRF24L01+ chip
wkleunen 2:d7d36199d4e4 23 printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
wkleunen 2:d7d36199d4e4 24 printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
wkleunen 2:d7d36199d4e4 25 printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
wkleunen 2:d7d36199d4e4 26 printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
wkleunen 2:d7d36199d4e4 27 printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
Owen 0:a51a6e7da590 28
wkleunen 4:6ec07c83786b 29 printf( "Type keys to test transfers:\r\n");
Owen 0:a51a6e7da590 30
Owen 0:a51a6e7da590 31 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
wkleunen 6:18ce87bf9df0 32 my_nrf24l01p.setRfFrequency(NRF24L01P_MAX_RF_FREQUENCY);
wkleunen 6:18ce87bf9df0 33
Owen 0:a51a6e7da590 34 my_nrf24l01p.setReceiveMode();
Owen 0:a51a6e7da590 35 my_nrf24l01p.enable();
Owen 0:a51a6e7da590 36
Owen 0:a51a6e7da590 37 while (1) {
Owen 0:a51a6e7da590 38
Owen 0:a51a6e7da590 39 // If we've received anything over the host serial link...
Owen 0:a51a6e7da590 40 if ( pc.readable() ) {
Owen 0:a51a6e7da590 41
Owen 0:a51a6e7da590 42 // ...add it to the transmit buffer
wkleunen 4:6ec07c83786b 43 pc.read(&txData[txDataCnt], TRANSFER_SIZE);
wkleunen 2:d7d36199d4e4 44 txDataCnt++;
Owen 0:a51a6e7da590 45
Owen 0:a51a6e7da590 46 // If the transmit buffer is full
Owen 0:a51a6e7da590 47 if ( txDataCnt >= sizeof( txData ) ) {
Owen 0:a51a6e7da590 48
Owen 0:a51a6e7da590 49 // Send the transmitbuffer via the nRF24L01+
Owen 0:a51a6e7da590 50 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
Owen 0:a51a6e7da590 51
Owen 0:a51a6e7da590 52 txDataCnt = 0;
Owen 0:a51a6e7da590 53 }
Owen 0:a51a6e7da590 54 }
Owen 0:a51a6e7da590 55
Owen 0:a51a6e7da590 56 // If we've received anything in the nRF24L01+...
Owen 0:a51a6e7da590 57 if ( my_nrf24l01p.readable() ) {
Owen 0:a51a6e7da590 58
Owen 0:a51a6e7da590 59 // ...read the data into the receive buffer
Owen 0:a51a6e7da590 60 rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
Owen 0:a51a6e7da590 61
Owen 0:a51a6e7da590 62 // Display the receive buffer contents via the host serial link
Owen 0:a51a6e7da590 63 for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
Owen 0:a51a6e7da590 64
wkleunen 4:6ec07c83786b 65 pc.write(&rxData[i], sizeof( rxData ));
Owen 0:a51a6e7da590 66 }
Owen 0:a51a6e7da590 67 }
Owen 0:a51a6e7da590 68 }
Owen 0:a51a6e7da590 69 }