Robocup Twente
/
nRF24L01P_Hello_World
Making it work for the FRDM K64F
Fork of nRF24L01P_Hello_World by
main.cpp@2:f32f686460b2, 2015-12-16 (annotated)
- Committer:
- ewoud
- Date:
- Wed Dec 16 22:35:59 2015 +0000
- Revision:
- 2:f32f686460b2
- Parent:
- 1:5be2682710c6
Wireless serial communication between FRDM K64F boards using the nF24L01P chip
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Owen | 0:a51a6e7da590 | 1 | #include "mbed.h" |
Owen | 0:a51a6e7da590 | 2 | #include "nRF24L01P.h" |
Owen | 0:a51a6e7da590 | 3 | |
Owen | 0:a51a6e7da590 | 4 | Serial pc(USBTX, USBRX); // tx, rx |
Owen | 0:a51a6e7da590 | 5 | |
ewoud | 2:f32f686460b2 | 6 | nRF24L01P my_nrf24l01p(PTD6, PTD7, PTD5, PTD4, PTB12, PTC18); // mosi, miso, sck, csn, ce, irq |
Owen | 0:a51a6e7da590 | 7 | |
ewoud | 2:f32f686460b2 | 8 | //DigitalOut myled1(LED1); |
ewoud | 2:f32f686460b2 | 9 | //DigitalOut myled2(LED2); |
Owen | 0:a51a6e7da590 | 10 | |
Owen | 0:a51a6e7da590 | 11 | int main() { |
ewoud | 2:f32f686460b2 | 12 | |
Owen | 0:a51a6e7da590 | 13 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
Owen | 0:a51a6e7da590 | 14 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
Owen | 0:a51a6e7da590 | 15 | // only handles 4 byte transfers in the ATMega code. |
Owen | 0:a51a6e7da590 | 16 | #define TRANSFER_SIZE 4 |
Owen | 0:a51a6e7da590 | 17 | |
Owen | 0:a51a6e7da590 | 18 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
Owen | 0:a51a6e7da590 | 19 | int txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 20 | int rxDataCnt = 0; |
ewoud | 2:f32f686460b2 | 21 | my_nrf24l01p.setRfFrequency(2400); |
Owen | 0:a51a6e7da590 | 22 | my_nrf24l01p.powerUp(); |
ewoud | 2:f32f686460b2 | 23 | //my_nrf24l01p.setRfFrequency(2400); |
Owen | 0:a51a6e7da590 | 24 | // Display the (default) setup of the nRF24L01+ chip |
Owen | 0:a51a6e7da590 | 25 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
Owen | 0:a51a6e7da590 | 26 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
Owen | 0:a51a6e7da590 | 27 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
Owen | 0:a51a6e7da590 | 28 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
Owen | 0:a51a6e7da590 | 29 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
Owen | 0:a51a6e7da590 | 30 | |
Owen | 0:a51a6e7da590 | 31 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 32 | |
Owen | 0:a51a6e7da590 | 33 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 34 | |
Owen | 0:a51a6e7da590 | 35 | my_nrf24l01p.setReceiveMode(); |
Owen | 0:a51a6e7da590 | 36 | my_nrf24l01p.enable(); |
Owen | 0:a51a6e7da590 | 37 | |
Owen | 0:a51a6e7da590 | 38 | while (1) { |
Owen | 0:a51a6e7da590 | 39 | |
Owen | 0:a51a6e7da590 | 40 | // If we've received anything over the host serial link... |
Owen | 0:a51a6e7da590 | 41 | if ( pc.readable() ) { |
Owen | 0:a51a6e7da590 | 42 | |
Owen | 0:a51a6e7da590 | 43 | // ...add it to the transmit buffer |
Owen | 0:a51a6e7da590 | 44 | txData[txDataCnt++] = pc.getc(); |
ewoud | 2:f32f686460b2 | 45 | printf(txData); |
ewoud | 2:f32f686460b2 | 46 | printf("\n\r"); |
Owen | 0:a51a6e7da590 | 47 | // If the transmit buffer is full |
Owen | 0:a51a6e7da590 | 48 | if ( txDataCnt >= sizeof( txData ) ) { |
ewoud | 2:f32f686460b2 | 49 | //myled1 = !myled1; |
Owen | 0:a51a6e7da590 | 50 | // Send the transmitbuffer via the nRF24L01+ |
Owen | 0:a51a6e7da590 | 51 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
Owen | 0:a51a6e7da590 | 52 | |
Owen | 0:a51a6e7da590 | 53 | txDataCnt = 0; |
ewoud | 2:f32f686460b2 | 54 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
ewoud | 2:f32f686460b2 | 55 | |
Owen | 0:a51a6e7da590 | 56 | } |
Owen | 0:a51a6e7da590 | 57 | |
ewoud | 2:f32f686460b2 | 58 | |
Owen | 0:a51a6e7da590 | 59 | } |
Owen | 0:a51a6e7da590 | 60 | |
Owen | 0:a51a6e7da590 | 61 | // If we've received anything in the nRF24L01+... |
Owen | 0:a51a6e7da590 | 62 | if ( my_nrf24l01p.readable() ) { |
Owen | 0:a51a6e7da590 | 63 | |
Owen | 0:a51a6e7da590 | 64 | // ...read the data into the receive buffer |
Owen | 0:a51a6e7da590 | 65 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
Owen | 0:a51a6e7da590 | 66 | |
Owen | 0:a51a6e7da590 | 67 | // Display the receive buffer contents via the host serial link |
Owen | 0:a51a6e7da590 | 68 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
Owen | 0:a51a6e7da590 | 69 | |
Owen | 0:a51a6e7da590 | 70 | pc.putc( rxData[i] ); |
Owen | 0:a51a6e7da590 | 71 | } |
Owen | 0:a51a6e7da590 | 72 | |
Owen | 0:a51a6e7da590 | 73 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
ewoud | 2:f32f686460b2 | 74 | //myled2 = !myled2; |
Owen | 0:a51a6e7da590 | 75 | } |
Owen | 0:a51a6e7da590 | 76 | } |
Owen | 0:a51a6e7da590 | 77 | } |