Example based on UDAKZ example + OWEN EDWARDS example to test nRF24L01P with STM32F103C8T6 board

Dependencies:   mbed-STM32F103C8T6 mbed nRF24L01P

Fork of STM32F103C8T6_Hello by Zoltan Hudak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "stm32f103c8t6.h"
00002 #include "mbed.h"
00003 #include "nRF24L01P.h"
00004 
00005 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
00006 //  "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
00007 //  only handles 4 byte transfers in the ATMega code.
00008 #define TRANSFER_SIZE   4
00009 
00010 
00011 #define LED2 PB_15
00012 
00013 #define CSN  PC_14
00014 #define CE   PC_15
00015 #define IRQ  PB_1
00016 
00017 #define LED_ON 0
00018 #define LED_OFF 1
00019 
00020 Serial      pc(PA_2, PA_3);
00021 DigitalOut  myled1(LED1);//tx activity
00022 DigitalOut  myled2(LED2);//rx activity
00023 
00024 
00025 nRF24L01P my_nrf24l01p(SPI_MOSI, SPI_MISO, SPI_SCK, CSN, CE, IRQ);    // mosi, miso, sck, csn, ce, irq
00026 
00027 int main() {
00028     char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
00029     int txDataCnt = 0;
00030     int rxDataCnt = 0;
00031     confSysClock();     // Configuring system clock
00032     pc.baud(115200);
00033     myled1 = LED_OFF;      // turn the LED off
00034     myled2 = LED_OFF;    
00035     pc.printf("STM32+nRF24L01+ Test\r\n");
00036     
00037 
00038     my_nrf24l01p.powerUp();
00039 
00040     // Display the (default) setup of the nRF24L01+ chip
00041     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00042     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00043     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00044     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00045     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00046 
00047     pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
00048 
00049     my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
00050 
00051     my_nrf24l01p.setReceiveMode();
00052     my_nrf24l01p.enable();
00053 
00054     while (1) {
00055 
00056         // If we've received anything over the host serial link...
00057         if ( pc.readable() ) {
00058 
00059             // ...add it to the transmit buffer
00060             txData[txDataCnt++] = pc.getc();
00061 
00062             // If the transmit buffer is full
00063             if ( txDataCnt >= sizeof( txData ) ) {
00064 
00065                 // Send the transmitbuffer via the nRF24L01+
00066                 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
00067 
00068                 txDataCnt = 0;
00069             }
00070 
00071             // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
00072             myled1 = !myled1;
00073         }
00074 
00075         // If we've received anything in the nRF24L01+...
00076         if ( my_nrf24l01p.readable() ) {
00077 
00078             // ...read the data into the receive buffer
00079             rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
00080 
00081             // Display the receive buffer contents via the host serial link
00082             for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
00083 
00084                 pc.putc( rxData[i] );
00085             }
00086 
00087             // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
00088             myled2 = !myled2;
00089         }
00090     }
00091 
00092 }