Example based on UDAKZ example + OWEN EDWARDS example to test nRF24L01P with STM32F103C8T6 board
Dependencies: mbed-STM32F103C8T6 mbed nRF24L01P
Fork of STM32F103C8T6_Hello by
main.cpp@8:c3006edbb507, 2016-07-29 (annotated)
- Committer:
- cfb95
- Date:
- Fri Jul 29 14:45:55 2016 +0000
- Revision:
- 8:c3006edbb507
- Parent:
- 7:accb2c83a007
Example based on HUDAKZ example + OWEN EDWARDS example to test nRF24L01P with STM32F103C8T6.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 5:3c3ef17a17a6 | 1 | #include "stm32f103c8t6.h" |
hudakz | 0:ab218237069e | 2 | #include "mbed.h" |
cfb95 | 8:c3006edbb507 | 3 | #include "nRF24L01P.h" |
cfb95 | 8:c3006edbb507 | 4 | |
cfb95 | 8:c3006edbb507 | 5 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
cfb95 | 8:c3006edbb507 | 6 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
cfb95 | 8:c3006edbb507 | 7 | // only handles 4 byte transfers in the ATMega code. |
cfb95 | 8:c3006edbb507 | 8 | #define TRANSFER_SIZE 4 |
cfb95 | 8:c3006edbb507 | 9 | |
cfb95 | 8:c3006edbb507 | 10 | |
cfb95 | 8:c3006edbb507 | 11 | #define LED2 PB_15 |
cfb95 | 8:c3006edbb507 | 12 | |
cfb95 | 8:c3006edbb507 | 13 | #define CSN PC_14 |
cfb95 | 8:c3006edbb507 | 14 | #define CE PC_15 |
cfb95 | 8:c3006edbb507 | 15 | #define IRQ PB_1 |
cfb95 | 8:c3006edbb507 | 16 | |
cfb95 | 8:c3006edbb507 | 17 | #define LED_ON 0 |
cfb95 | 8:c3006edbb507 | 18 | #define LED_OFF 1 |
hudakz | 0:ab218237069e | 19 | |
hudakz | 6:d99931fc19c0 | 20 | Serial pc(PA_2, PA_3); |
cfb95 | 8:c3006edbb507 | 21 | DigitalOut myled1(LED1);//tx activity |
cfb95 | 8:c3006edbb507 | 22 | DigitalOut myled2(LED2);//rx activity |
cfb95 | 8:c3006edbb507 | 23 | |
cfb95 | 8:c3006edbb507 | 24 | |
cfb95 | 8:c3006edbb507 | 25 | nRF24L01P my_nrf24l01p(SPI_MOSI, SPI_MISO, SPI_SCK, CSN, CE, IRQ); // mosi, miso, sck, csn, ce, irq |
hudakz | 0:ab218237069e | 26 | |
hudakz | 0:ab218237069e | 27 | int main() { |
cfb95 | 8:c3006edbb507 | 28 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
cfb95 | 8:c3006edbb507 | 29 | int txDataCnt = 0; |
cfb95 | 8:c3006edbb507 | 30 | int rxDataCnt = 0; |
hudakz | 7:accb2c83a007 | 31 | confSysClock(); // Configuring system clock |
cfb95 | 8:c3006edbb507 | 32 | pc.baud(115200); |
cfb95 | 8:c3006edbb507 | 33 | myled1 = LED_OFF; // turn the LED off |
cfb95 | 8:c3006edbb507 | 34 | myled2 = LED_OFF; |
cfb95 | 8:c3006edbb507 | 35 | pc.printf("STM32+nRF24L01+ Test\r\n"); |
cfb95 | 8:c3006edbb507 | 36 | |
cfb95 | 8:c3006edbb507 | 37 | |
cfb95 | 8:c3006edbb507 | 38 | my_nrf24l01p.powerUp(); |
cfb95 | 8:c3006edbb507 | 39 | |
cfb95 | 8:c3006edbb507 | 40 | // Display the (default) setup of the nRF24L01+ chip |
cfb95 | 8:c3006edbb507 | 41 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
cfb95 | 8:c3006edbb507 | 42 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
cfb95 | 8:c3006edbb507 | 43 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
cfb95 | 8:c3006edbb507 | 44 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
cfb95 | 8:c3006edbb507 | 45 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
cfb95 | 8:c3006edbb507 | 46 | |
cfb95 | 8:c3006edbb507 | 47 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
cfb95 | 8:c3006edbb507 | 48 | |
cfb95 | 8:c3006edbb507 | 49 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
cfb95 | 8:c3006edbb507 | 50 | |
cfb95 | 8:c3006edbb507 | 51 | my_nrf24l01p.setReceiveMode(); |
cfb95 | 8:c3006edbb507 | 52 | my_nrf24l01p.enable(); |
cfb95 | 8:c3006edbb507 | 53 | |
cfb95 | 8:c3006edbb507 | 54 | while (1) { |
cfb95 | 8:c3006edbb507 | 55 | |
cfb95 | 8:c3006edbb507 | 56 | // If we've received anything over the host serial link... |
cfb95 | 8:c3006edbb507 | 57 | if ( pc.readable() ) { |
cfb95 | 8:c3006edbb507 | 58 | |
cfb95 | 8:c3006edbb507 | 59 | // ...add it to the transmit buffer |
cfb95 | 8:c3006edbb507 | 60 | txData[txDataCnt++] = pc.getc(); |
cfb95 | 8:c3006edbb507 | 61 | |
cfb95 | 8:c3006edbb507 | 62 | // If the transmit buffer is full |
cfb95 | 8:c3006edbb507 | 63 | if ( txDataCnt >= sizeof( txData ) ) { |
cfb95 | 8:c3006edbb507 | 64 | |
cfb95 | 8:c3006edbb507 | 65 | // Send the transmitbuffer via the nRF24L01+ |
cfb95 | 8:c3006edbb507 | 66 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
cfb95 | 8:c3006edbb507 | 67 | |
cfb95 | 8:c3006edbb507 | 68 | txDataCnt = 0; |
cfb95 | 8:c3006edbb507 | 69 | } |
cfb95 | 8:c3006edbb507 | 70 | |
cfb95 | 8:c3006edbb507 | 71 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
cfb95 | 8:c3006edbb507 | 72 | myled1 = !myled1; |
cfb95 | 8:c3006edbb507 | 73 | } |
cfb95 | 8:c3006edbb507 | 74 | |
cfb95 | 8:c3006edbb507 | 75 | // If we've received anything in the nRF24L01+... |
cfb95 | 8:c3006edbb507 | 76 | if ( my_nrf24l01p.readable() ) { |
cfb95 | 8:c3006edbb507 | 77 | |
cfb95 | 8:c3006edbb507 | 78 | // ...read the data into the receive buffer |
cfb95 | 8:c3006edbb507 | 79 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
cfb95 | 8:c3006edbb507 | 80 | |
cfb95 | 8:c3006edbb507 | 81 | // Display the receive buffer contents via the host serial link |
cfb95 | 8:c3006edbb507 | 82 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
cfb95 | 8:c3006edbb507 | 83 | |
cfb95 | 8:c3006edbb507 | 84 | pc.putc( rxData[i] ); |
cfb95 | 8:c3006edbb507 | 85 | } |
cfb95 | 8:c3006edbb507 | 86 | |
cfb95 | 8:c3006edbb507 | 87 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
cfb95 | 8:c3006edbb507 | 88 | myled2 = !myled2; |
cfb95 | 8:c3006edbb507 | 89 | } |
hudakz | 0:ab218237069e | 90 | } |
cfb95 | 8:c3006edbb507 | 91 | |
hudakz | 0:ab218237069e | 92 | } |