Wouter van Kleunen
/
Saxion_nRF24L01P_Hello_World
nRF24L01P Hello World example for mbed 6
main.cpp@3:c3afd479d9d8, 2021-04-14 (annotated)
- Committer:
- wkleunen
- Date:
- Wed Apr 14 17:25:45 2021 +0000
- Revision:
- 3:c3afd479d9d8
- Parent:
- 2:d7d36199d4e4
- Child:
- 4:6ec07c83786b
Fix SPI communication
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 | |
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() { |
Owen | 0:a51a6e7da590 | 14 | |
Owen | 0:a51a6e7da590 | 15 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
Owen | 0:a51a6e7da590 | 16 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
Owen | 0:a51a6e7da590 | 17 | // only handles 4 byte transfers in the ATMega code. |
Owen | 0:a51a6e7da590 | 18 | #define TRANSFER_SIZE 4 |
Owen | 0:a51a6e7da590 | 19 | |
Owen | 0:a51a6e7da590 | 20 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
Owen | 0:a51a6e7da590 | 21 | int txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 22 | int rxDataCnt = 0; |
Owen | 0:a51a6e7da590 | 23 | |
Owen | 0:a51a6e7da590 | 24 | my_nrf24l01p.powerUp(); |
Owen | 0:a51a6e7da590 | 25 | |
Owen | 0:a51a6e7da590 | 26 | // Display the (default) setup of the nRF24L01+ chip |
wkleunen | 2:d7d36199d4e4 | 27 | printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
wkleunen | 2:d7d36199d4e4 | 28 | printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
wkleunen | 2:d7d36199d4e4 | 29 | printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
wkleunen | 2:d7d36199d4e4 | 30 | printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
wkleunen | 2:d7d36199d4e4 | 31 | printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
Owen | 0:a51a6e7da590 | 32 | |
wkleunen | 2:d7d36199d4e4 | 33 | printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 34 | |
Owen | 0:a51a6e7da590 | 35 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 36 | |
Owen | 0:a51a6e7da590 | 37 | my_nrf24l01p.setReceiveMode(); |
Owen | 0:a51a6e7da590 | 38 | my_nrf24l01p.enable(); |
Owen | 0:a51a6e7da590 | 39 | |
Owen | 0:a51a6e7da590 | 40 | while (1) { |
Owen | 0:a51a6e7da590 | 41 | |
Owen | 0:a51a6e7da590 | 42 | // If we've received anything over the host serial link... |
Owen | 0:a51a6e7da590 | 43 | if ( pc.readable() ) { |
Owen | 0:a51a6e7da590 | 44 | |
Owen | 0:a51a6e7da590 | 45 | // ...add it to the transmit buffer |
wkleunen | 2:d7d36199d4e4 | 46 | pc.read(&txData[txDataCnt], sizeof(char)); |
wkleunen | 2:d7d36199d4e4 | 47 | txDataCnt++; |
Owen | 0:a51a6e7da590 | 48 | |
Owen | 0:a51a6e7da590 | 49 | // If the transmit buffer is full |
Owen | 0:a51a6e7da590 | 50 | if ( txDataCnt >= sizeof( txData ) ) { |
Owen | 0:a51a6e7da590 | 51 | |
Owen | 0:a51a6e7da590 | 52 | // Send the transmitbuffer via the nRF24L01+ |
Owen | 0:a51a6e7da590 | 53 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
Owen | 0:a51a6e7da590 | 54 | |
Owen | 0:a51a6e7da590 | 55 | txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 56 | } |
Owen | 0:a51a6e7da590 | 57 | } |
Owen | 0:a51a6e7da590 | 58 | |
Owen | 0:a51a6e7da590 | 59 | // If we've received anything in the nRF24L01+... |
Owen | 0:a51a6e7da590 | 60 | if ( my_nrf24l01p.readable() ) { |
Owen | 0:a51a6e7da590 | 61 | |
Owen | 0:a51a6e7da590 | 62 | // ...read the data into the receive buffer |
Owen | 0:a51a6e7da590 | 63 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
Owen | 0:a51a6e7da590 | 64 | |
Owen | 0:a51a6e7da590 | 65 | // Display the receive buffer contents via the host serial link |
Owen | 0:a51a6e7da590 | 66 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
Owen | 0:a51a6e7da590 | 67 | |
wkleunen | 2:d7d36199d4e4 | 68 | pc.write(&rxData[i], sizeof(char)); |
Owen | 0:a51a6e7da590 | 69 | } |
Owen | 0:a51a6e7da590 | 70 | } |
Owen | 0:a51a6e7da590 | 71 | } |
Owen | 0:a51a6e7da590 | 72 | } |