I-O DATA DEV2
/
stm32_hello_nrf24
nrf24 test
main.cpp@0:b1276e0d29a3, 2020-11-24 (annotated)
- Committer:
- hakusan270
- Date:
- Tue Nov 24 04:53:57 2020 +0000
- Revision:
- 0:b1276e0d29a3
nrf24 test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hakusan270 | 0:b1276e0d29a3 | 1 | #include "mbed.h" |
hakusan270 | 0:b1276e0d29a3 | 2 | #include "nRF24L01P.h" |
hakusan270 | 0:b1276e0d29a3 | 3 | RawSerial pc(PA_2, PA_3,9600 ); |
hakusan270 | 0:b1276e0d29a3 | 4 | |
hakusan270 | 0:b1276e0d29a3 | 5 | |
hakusan270 | 0:b1276e0d29a3 | 6 | nRF24L01P my_nrf24l01p(D11, D12, D13, D10, D9,D8); // mosi, miso, sck, csn, ce, irq |
hakusan270 | 0:b1276e0d29a3 | 7 | //nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq |
hakusan270 | 0:b1276e0d29a3 | 8 | |
hakusan270 | 0:b1276e0d29a3 | 9 | //DigitalOut myled1(LED1); |
hakusan270 | 0:b1276e0d29a3 | 10 | //DigitalOut myled2(D3); |
hakusan270 | 0:b1276e0d29a3 | 11 | |
hakusan270 | 0:b1276e0d29a3 | 12 | int main() { |
hakusan270 | 0:b1276e0d29a3 | 13 | |
hakusan270 | 0:b1276e0d29a3 | 14 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
hakusan270 | 0:b1276e0d29a3 | 15 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
hakusan270 | 0:b1276e0d29a3 | 16 | // only handles 4 byte transfers in the ATMega code. |
hakusan270 | 0:b1276e0d29a3 | 17 | #define TRANSFER_SIZE 4 |
hakusan270 | 0:b1276e0d29a3 | 18 | |
hakusan270 | 0:b1276e0d29a3 | 19 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
hakusan270 | 0:b1276e0d29a3 | 20 | int txDataCnt = 0; |
hakusan270 | 0:b1276e0d29a3 | 21 | int rxDataCnt = 0; |
hakusan270 | 0:b1276e0d29a3 | 22 | int i=0; |
hakusan270 | 0:b1276e0d29a3 | 23 | my_nrf24l01p.powerUp(); |
hakusan270 | 0:b1276e0d29a3 | 24 | my_nrf24l01p.setRfFrequency(NRF24L01P_MIN_RF_FREQUENCY);//2400-2525 |
hakusan270 | 0:b1276e0d29a3 | 25 | my_nrf24l01p.setRfOutputPower(NRF24L01P_TX_PWR_ZERO_DB);//mAX 0 -6 -12 -18 |
hakusan270 | 0:b1276e0d29a3 | 26 | my_nrf24l01p.setAirDataRate(NRF24L01P_DATARATE_250_KBPS);//250k,1000,2000K |
hakusan270 | 0:b1276e0d29a3 | 27 | // Display the (default) setup of the nRF24L01+ chip |
hakusan270 | 0:b1276e0d29a3 | 28 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
hakusan270 | 0:b1276e0d29a3 | 29 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
hakusan270 | 0:b1276e0d29a3 | 30 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
hakusan270 | 0:b1276e0d29a3 | 31 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
hakusan270 | 0:b1276e0d29a3 | 32 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
hakusan270 | 0:b1276e0d29a3 | 33 | |
hakusan270 | 0:b1276e0d29a3 | 34 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
hakusan270 | 0:b1276e0d29a3 | 35 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE );//mAX 4 |
hakusan270 | 0:b1276e0d29a3 | 36 | |
hakusan270 | 0:b1276e0d29a3 | 37 | my_nrf24l01p.setReceiveMode(); |
hakusan270 | 0:b1276e0d29a3 | 38 | my_nrf24l01p.enable(); |
hakusan270 | 0:b1276e0d29a3 | 39 | |
hakusan270 | 0:b1276e0d29a3 | 40 | while (1) { |
hakusan270 | 0:b1276e0d29a3 | 41 | |
hakusan270 | 0:b1276e0d29a3 | 42 | // If we've received anything over the host serial link... |
hakusan270 | 0:b1276e0d29a3 | 43 | if ( pc.readable() ) { |
hakusan270 | 0:b1276e0d29a3 | 44 | |
hakusan270 | 0:b1276e0d29a3 | 45 | // ...add it to the transmit buffer |
hakusan270 | 0:b1276e0d29a3 | 46 | txData[txDataCnt++] = pc.getc(); |
hakusan270 | 0:b1276e0d29a3 | 47 | |
hakusan270 | 0:b1276e0d29a3 | 48 | // If the transmit buffer is full |
hakusan270 | 0:b1276e0d29a3 | 49 | if ( txDataCnt >= sizeof( txData ) ) { |
hakusan270 | 0:b1276e0d29a3 | 50 | |
hakusan270 | 0:b1276e0d29a3 | 51 | // Send the transmitbuffer via the nRF24L01+ |
hakusan270 | 0:b1276e0d29a3 | 52 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
hakusan270 | 0:b1276e0d29a3 | 53 | |
hakusan270 | 0:b1276e0d29a3 | 54 | txDataCnt = 0; |
hakusan270 | 0:b1276e0d29a3 | 55 | } |
hakusan270 | 0:b1276e0d29a3 | 56 | |
hakusan270 | 0:b1276e0d29a3 | 57 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
hakusan270 | 0:b1276e0d29a3 | 58 | // myled1 = !myled1; |
hakusan270 | 0:b1276e0d29a3 | 59 | } |
hakusan270 | 0:b1276e0d29a3 | 60 | txData[0]='A'; |
hakusan270 | 0:b1276e0d29a3 | 61 | txData[1]='B'; |
hakusan270 | 0:b1276e0d29a3 | 62 | txData[2]='C'; |
hakusan270 | 0:b1276e0d29a3 | 63 | txData[3]='0' + (i++ & 0x0f); |
hakusan270 | 0:b1276e0d29a3 | 64 | #if defined ( TARGET_NUCLEO_L053R8 ) |
hakusan270 | 0:b1276e0d29a3 | 65 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, 4 ); |
hakusan270 | 0:b1276e0d29a3 | 66 | #endif |
hakusan270 | 0:b1276e0d29a3 | 67 | // If we've received anything in the nRF24L01+... |
hakusan270 | 0:b1276e0d29a3 | 68 | if ( my_nrf24l01p.readable() ) { |
hakusan270 | 0:b1276e0d29a3 | 69 | |
hakusan270 | 0:b1276e0d29a3 | 70 | // ...read the data into the receive buffer |
hakusan270 | 0:b1276e0d29a3 | 71 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
hakusan270 | 0:b1276e0d29a3 | 72 | |
hakusan270 | 0:b1276e0d29a3 | 73 | // Display the receive buffer contents via the host serial link |
hakusan270 | 0:b1276e0d29a3 | 74 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
hakusan270 | 0:b1276e0d29a3 | 75 | |
hakusan270 | 0:b1276e0d29a3 | 76 | pc.putc( rxData[i] ); |
hakusan270 | 0:b1276e0d29a3 | 77 | } |
hakusan270 | 0:b1276e0d29a3 | 78 | |
hakusan270 | 0:b1276e0d29a3 | 79 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
hakusan270 | 0:b1276e0d29a3 | 80 | // myled1 = !myled1; |
hakusan270 | 0:b1276e0d29a3 | 81 | } |
hakusan270 | 0:b1276e0d29a3 | 82 | } |
hakusan270 | 0:b1276e0d29a3 | 83 | } |