iforce2d Chris
/
nRF24L01SimpleExample
Basic port of the gcopeland nRF24L01 library
main.cpp@0:a51a6e7da590, 2011-01-19 (annotated)
- Committer:
- Owen
- Date:
- Wed Jan 19 23:26:02 2011 +0000
- Revision:
- 0:a51a6e7da590
- Child:
- 1:5be2682710c6
Initial revision.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Owen | 0:a51a6e7da590 | 1 | /** |
Owen | 0:a51a6e7da590 | 2 | * @file main.cpp (nRF24L01P_Hello_World) |
Owen | 0:a51a6e7da590 | 3 | * |
Owen | 0:a51a6e7da590 | 4 | * @author Owen Edwards |
Owen | 0:a51a6e7da590 | 5 | * |
Owen | 0:a51a6e7da590 | 6 | * @section LICENSE |
Owen | 0:a51a6e7da590 | 7 | * |
Owen | 0:a51a6e7da590 | 8 | * Copyright (c) 2010 Owen Edwards |
Owen | 0:a51a6e7da590 | 9 | * |
Owen | 0:a51a6e7da590 | 10 | * This program is free software: you can redistribute it and/or modify |
Owen | 0:a51a6e7da590 | 11 | * it under the terms of the GNU General Public License as published by |
Owen | 0:a51a6e7da590 | 12 | * the Free Software Foundation, either version 3 of the License, or |
Owen | 0:a51a6e7da590 | 13 | * (at your option) any later version. |
Owen | 0:a51a6e7da590 | 14 | * |
Owen | 0:a51a6e7da590 | 15 | * This program is distributed in the hope that it will be useful, |
Owen | 0:a51a6e7da590 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Owen | 0:a51a6e7da590 | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Owen | 0:a51a6e7da590 | 18 | * GNU General Public License for more details. |
Owen | 0:a51a6e7da590 | 19 | * |
Owen | 0:a51a6e7da590 | 20 | * You should have received a copy of the GNU General Public License |
Owen | 0:a51a6e7da590 | 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Owen | 0:a51a6e7da590 | 22 | * |
Owen | 0:a51a6e7da590 | 23 | * The above copyright notice and this permission notice shall be included in |
Owen | 0:a51a6e7da590 | 24 | * all copies or substantial portions of the Software. |
Owen | 0:a51a6e7da590 | 25 | * |
Owen | 0:a51a6e7da590 | 26 | * @section DESCRIPTION |
Owen | 0:a51a6e7da590 | 27 | * |
Owen | 0:a51a6e7da590 | 28 | * Demo the nRF24L01+ Library |
Owen | 0:a51a6e7da590 | 29 | * |
Owen | 0:a51a6e7da590 | 30 | * Datasheet: |
Owen | 0:a51a6e7da590 | 31 | * |
Owen | 0:a51a6e7da590 | 32 | * http://www.nordicsemi.no/files/Product/data_sheet/nRF24L01P_Product_Specification_1_0.pdf |
Owen | 0:a51a6e7da590 | 33 | */ |
Owen | 0:a51a6e7da590 | 34 | |
Owen | 0:a51a6e7da590 | 35 | #include "mbed.h" |
Owen | 0:a51a6e7da590 | 36 | #include "nRF24L01P.h" |
Owen | 0:a51a6e7da590 | 37 | |
Owen | 0:a51a6e7da590 | 38 | Serial pc(USBTX, USBRX); // tx, rx |
Owen | 0:a51a6e7da590 | 39 | |
Owen | 0:a51a6e7da590 | 40 | nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq |
Owen | 0:a51a6e7da590 | 41 | |
Owen | 0:a51a6e7da590 | 42 | DigitalOut myled1(LED1); |
Owen | 0:a51a6e7da590 | 43 | DigitalOut myled2(LED2); |
Owen | 0:a51a6e7da590 | 44 | |
Owen | 0:a51a6e7da590 | 45 | int main() { |
Owen | 0:a51a6e7da590 | 46 | |
Owen | 0:a51a6e7da590 | 47 | // |
Owen | 0:a51a6e7da590 | 48 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
Owen | 0:a51a6e7da590 | 49 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
Owen | 0:a51a6e7da590 | 50 | // only handles 4 byte transfers in the ATMega code. |
Owen | 0:a51a6e7da590 | 51 | // |
Owen | 0:a51a6e7da590 | 52 | #define TRANSFER_SIZE 4 |
Owen | 0:a51a6e7da590 | 53 | |
Owen | 0:a51a6e7da590 | 54 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
Owen | 0:a51a6e7da590 | 55 | int txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 56 | int rxDataCnt = 0; |
Owen | 0:a51a6e7da590 | 57 | |
Owen | 0:a51a6e7da590 | 58 | my_nrf24l01p.powerUp(); |
Owen | 0:a51a6e7da590 | 59 | |
Owen | 0:a51a6e7da590 | 60 | // |
Owen | 0:a51a6e7da590 | 61 | // Display the (default) setup of the nRF24L01+ chip |
Owen | 0:a51a6e7da590 | 62 | // |
Owen | 0:a51a6e7da590 | 63 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
Owen | 0:a51a6e7da590 | 64 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
Owen | 0:a51a6e7da590 | 65 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
Owen | 0:a51a6e7da590 | 66 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
Owen | 0:a51a6e7da590 | 67 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
Owen | 0:a51a6e7da590 | 68 | |
Owen | 0:a51a6e7da590 | 69 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 70 | |
Owen | 0:a51a6e7da590 | 71 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
Owen | 0:a51a6e7da590 | 72 | |
Owen | 0:a51a6e7da590 | 73 | my_nrf24l01p.setReceiveMode(); |
Owen | 0:a51a6e7da590 | 74 | my_nrf24l01p.enable(); |
Owen | 0:a51a6e7da590 | 75 | |
Owen | 0:a51a6e7da590 | 76 | while (1) { |
Owen | 0:a51a6e7da590 | 77 | |
Owen | 0:a51a6e7da590 | 78 | // |
Owen | 0:a51a6e7da590 | 79 | // If we've received anything over the host serial link... |
Owen | 0:a51a6e7da590 | 80 | // |
Owen | 0:a51a6e7da590 | 81 | if ( pc.readable() ) { |
Owen | 0:a51a6e7da590 | 82 | |
Owen | 0:a51a6e7da590 | 83 | // |
Owen | 0:a51a6e7da590 | 84 | // ...add it to the transmit buffer |
Owen | 0:a51a6e7da590 | 85 | // |
Owen | 0:a51a6e7da590 | 86 | txData[txDataCnt++] = pc.getc(); |
Owen | 0:a51a6e7da590 | 87 | |
Owen | 0:a51a6e7da590 | 88 | // |
Owen | 0:a51a6e7da590 | 89 | // If the transmit buffer is full |
Owen | 0:a51a6e7da590 | 90 | // |
Owen | 0:a51a6e7da590 | 91 | if ( txDataCnt >= sizeof( txData ) ) { |
Owen | 0:a51a6e7da590 | 92 | |
Owen | 0:a51a6e7da590 | 93 | // |
Owen | 0:a51a6e7da590 | 94 | // Send the transmitbuffer via the nRF24L01+ |
Owen | 0:a51a6e7da590 | 95 | // |
Owen | 0:a51a6e7da590 | 96 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
Owen | 0:a51a6e7da590 | 97 | |
Owen | 0:a51a6e7da590 | 98 | txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 99 | |
Owen | 0:a51a6e7da590 | 100 | } |
Owen | 0:a51a6e7da590 | 101 | |
Owen | 0:a51a6e7da590 | 102 | // |
Owen | 0:a51a6e7da590 | 103 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
Owen | 0:a51a6e7da590 | 104 | // |
Owen | 0:a51a6e7da590 | 105 | myled1 = !myled1; |
Owen | 0:a51a6e7da590 | 106 | |
Owen | 0:a51a6e7da590 | 107 | } |
Owen | 0:a51a6e7da590 | 108 | |
Owen | 0:a51a6e7da590 | 109 | // |
Owen | 0:a51a6e7da590 | 110 | // If we've received anything in the nRF24L01+... |
Owen | 0:a51a6e7da590 | 111 | // |
Owen | 0:a51a6e7da590 | 112 | if ( my_nrf24l01p.readable() ) { |
Owen | 0:a51a6e7da590 | 113 | |
Owen | 0:a51a6e7da590 | 114 | // |
Owen | 0:a51a6e7da590 | 115 | // ...read the data into the receive buffer |
Owen | 0:a51a6e7da590 | 116 | // |
Owen | 0:a51a6e7da590 | 117 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
Owen | 0:a51a6e7da590 | 118 | |
Owen | 0:a51a6e7da590 | 119 | // |
Owen | 0:a51a6e7da590 | 120 | // Display the receive buffer contents via the host serial link |
Owen | 0:a51a6e7da590 | 121 | // |
Owen | 0:a51a6e7da590 | 122 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
Owen | 0:a51a6e7da590 | 123 | |
Owen | 0:a51a6e7da590 | 124 | pc.putc( rxData[i] ); |
Owen | 0:a51a6e7da590 | 125 | |
Owen | 0:a51a6e7da590 | 126 | } |
Owen | 0:a51a6e7da590 | 127 | |
Owen | 0:a51a6e7da590 | 128 | // |
Owen | 0:a51a6e7da590 | 129 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
Owen | 0:a51a6e7da590 | 130 | // |
Owen | 0:a51a6e7da590 | 131 | myled2 = !myled2; |
Owen | 0:a51a6e7da590 | 132 | |
Owen | 0:a51a6e7da590 | 133 | } |
Owen | 0:a51a6e7da590 | 134 | } |
Owen | 0:a51a6e7da590 | 135 | } |