Hello world example for the nRF2401A Library

Dependencies:   mbed nRF2401A

Committer:
TheChrisyd
Date:
Sat Oct 19 11:11:59 2013 +0000
Revision:
4:aa6866d2014a
Parent:
3:34ae527e9d41
Child:
6:f43bf63489bb
added support for KL25Z and added examples of different methods of reading the received data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TheChrisyd 0:8fcb46c5fa63 1 #include "mbed.h"
TheChrisyd 0:8fcb46c5fa63 2 #include "nRF2401A.h"
TheChrisyd 0:8fcb46c5fa63 3
TheChrisyd 4:aa6866d2014a 4 /* comment these out depending on the job of the mbed. If your only using one mbed leave both uncommented. */
TheChrisyd 4:aa6866d2014a 5 //#define TX
TheChrisyd 0:8fcb46c5fa63 6 #define RX
TheChrisyd 0:8fcb46c5fa63 7
TheChrisyd 4:aa6866d2014a 8 /* If using the FRDM-KL25Z uncomment this line */
TheChrisyd 4:aa6866d2014a 9 //#define FRDMKL25Z
TheChrisyd 4:aa6866d2014a 10
TheChrisyd 3:34ae527e9d41 11 Serial pc(USBTX, USBRX);
TheChrisyd 0:8fcb46c5fa63 12 DigitalOut myled(LED1);
TheChrisyd 3:34ae527e9d41 13
TheChrisyd 0:8fcb46c5fa63 14 #ifdef TX
TheChrisyd 4:aa6866d2014a 15 #ifdef FRDMKL25Z
TheChrisyd 4:aa6866d2014a 16 nRF2401A rf1(PTD0, PTD5, PTA13, PTC12, PTC13);
TheChrisyd 4:aa6866d2014a 17 #else
TheChrisyd 0:8fcb46c5fa63 18 nRF2401A rf1(p10, p11, p12, p13, p14);
TheChrisyd 0:8fcb46c5fa63 19 #endif
TheChrisyd 4:aa6866d2014a 20 #endif
TheChrisyd 3:34ae527e9d41 21
TheChrisyd 0:8fcb46c5fa63 22 #ifdef RX
TheChrisyd 4:aa6866d2014a 23 #ifdef FRDMKL25Z
TheChrisyd 4:aa6866d2014a 24 nRF2401A rf2(PTD0, PTD5, PTA13, PTC12, PTC13);
TheChrisyd 4:aa6866d2014a 25 #else
TheChrisyd 4:aa6866d2014a 26 nRF2401A rf2(p10, p11, p12, p13, p14);
TheChrisyd 4:aa6866d2014a 27 #endif
TheChrisyd 0:8fcb46c5fa63 28
TheChrisyd 2:440c95f796ac 29 bool rx_recieved = false;
TheChrisyd 3:34ae527e9d41 30
TheChrisyd 3:34ae527e9d41 31 void nRF2401A_rx (void *arg)
TheChrisyd 2:440c95f796ac 32 {
TheChrisyd 2:440c95f796ac 33 rx_recieved = true;
TheChrisyd 2:440c95f796ac 34 }
TheChrisyd 2:440c95f796ac 35 #endif
TheChrisyd 3:34ae527e9d41 36 int main()
TheChrisyd 3:34ae527e9d41 37 {
TheChrisyd 0:8fcb46c5fa63 38 wait(0.005);
TheChrisyd 0:8fcb46c5fa63 39 pc.printf("Hello nRF2401A\n\r");
TheChrisyd 3:34ae527e9d41 40
TheChrisyd 4:aa6866d2014a 41 #ifdef TX
TheChrisyd 4:aa6866d2014a 42 /* initialise the nRF2401A with payload size, address, CRC, bit rate and channel */
TheChrisyd 0:8fcb46c5fa63 43 rf1.setDataPayloadLength(4 << 3)
TheChrisyd 0:8fcb46c5fa63 44 .setAddress(0x0, 0x0, 0xa6, 0xa6, 0xa6, 3 << 3)
TheChrisyd 0:8fcb46c5fa63 45 .setCRCMode(nRF2401A::NO_CRC)
TheChrisyd 0:8fcb46c5fa63 46 .setDataRate(nRF2401A::BIT_RATE_250KBITS)
TheChrisyd 0:8fcb46c5fa63 47 .setChannel(0x02);
TheChrisyd 0:8fcb46c5fa63 48
TheChrisyd 3:34ae527e9d41 49 rf1.printControlPacket(pc);
TheChrisyd 3:34ae527e9d41 50 rf1.flushControlPacket();
TheChrisyd 3:34ae527e9d41 51
TheChrisyd 4:aa6866d2014a 52 /* initialise variables to use for tranmission */
TheChrisyd 3:34ae527e9d41 53 nRF2401A::address_t rf2_addr = {0x0, 0x0, 0x53, 0x53, 0x53};
TheChrisyd 3:34ae527e9d41 54 uint8_t msg[] = {0x01, 0x01, 0x01, 0x01};
TheChrisyd 3:34ae527e9d41 55 uint32_t *msg32 = (uint32_t *) msg;
TheChrisyd 3:34ae527e9d41 56 #endif
TheChrisyd 0:8fcb46c5fa63 57
TheChrisyd 0:8fcb46c5fa63 58 #ifdef RX
TheChrisyd 4:aa6866d2014a 59 /* initialise the nRF2401A with payload size, address, CRC, bit rate and channel */
TheChrisyd 0:8fcb46c5fa63 60 rf2.setDataPayloadLength(4 << 3)
TheChrisyd 0:8fcb46c5fa63 61 .setAddress(0x0, 0x0, 0x53, 0x53, 0x53, 3 << 3)
TheChrisyd 0:8fcb46c5fa63 62 .setCRCMode(nRF2401A::NO_CRC)
TheChrisyd 0:8fcb46c5fa63 63 .setDataRate(nRF2401A::BIT_RATE_250KBITS)
TheChrisyd 0:8fcb46c5fa63 64 .setChannel(0x02);
TheChrisyd 0:8fcb46c5fa63 65
TheChrisyd 3:34ae527e9d41 66 rf2.printControlPacket(pc);
TheChrisyd 3:34ae527e9d41 67 rf2.flushControlPacket();
TheChrisyd 4:aa6866d2014a 68
TheChrisyd 4:aa6866d2014a 69 /* attach receive callback */
TheChrisyd 3:34ae527e9d41 70 rf2.attachRXHandler(&nRF2401A_rx, 0);
TheChrisyd 0:8fcb46c5fa63 71 #endif
TheChrisyd 0:8fcb46c5fa63 72
TheChrisyd 3:34ae527e9d41 73 while(1)
TheChrisyd 3:34ae527e9d41 74 {
TheChrisyd 3:34ae527e9d41 75
TheChrisyd 4:aa6866d2014a 76 #ifdef TX
TheChrisyd 4:aa6866d2014a 77 myled = 0;
TheChrisyd 4:aa6866d2014a 78 wait(0.25);
TheChrisyd 4:aa6866d2014a 79
TheChrisyd 4:aa6866d2014a 80 /* send the message to the nRF2401A */
TheChrisyd 0:8fcb46c5fa63 81 rf1.sendMsg(rf2_addr, 3 << 3, msg, 4 << 3);
TheChrisyd 0:8fcb46c5fa63 82 *msg32 += 1;
TheChrisyd 4:aa6866d2014a 83
TheChrisyd 4:aa6866d2014a 84 myled = 1;
TheChrisyd 4:aa6866d2014a 85 wait(0.25);
TheChrisyd 0:8fcb46c5fa63 86 #endif
TheChrisyd 3:34ae527e9d41 87
TheChrisyd 3:34ae527e9d41 88
TheChrisyd 2:440c95f796ac 89 #ifdef RX
TheChrisyd 2:440c95f796ac 90 if (rx_recieved)
TheChrisyd 2:440c95f796ac 91 {
TheChrisyd 4:aa6866d2014a 92 /* send the read buffer directly to the serial port */
TheChrisyd 2:440c95f796ac 93 rf2.printDataPacket(pc);
TheChrisyd 4:aa6866d2014a 94
TheChrisyd 4:aa6866d2014a 95 /* send a single byte from the read buffer to the serial port */
TheChrisyd 4:aa6866d2014a 96 uint8_t rx_msg = 0;
TheChrisyd 4:aa6866d2014a 97 rf2.readMsg_byte(&rx_msg, 0 );
TheChrisyd 4:aa6866d2014a 98 pc.printf("\n\r%d\n\r", rx_msg);
TheChrisyd 4:aa6866d2014a 99
TheChrisyd 4:aa6866d2014a 100 /* read the read buffer , then send to the serial port */
TheChrisyd 4:aa6866d2014a 101 uint8_t rx_buffer[32] = {0};
TheChrisyd 4:aa6866d2014a 102 rf2.readMsg( &rx_buffer[0], 32);
TheChrisyd 4:aa6866d2014a 103 for(int i = 0; i < sizeof(rx_buffer); i++)
TheChrisyd 4:aa6866d2014a 104 {
TheChrisyd 4:aa6866d2014a 105 pc.printf("%02x ", rx_buffer[i]);
TheChrisyd 4:aa6866d2014a 106 }
TheChrisyd 4:aa6866d2014a 107 pc.printf("\r\n");
TheChrisyd 4:aa6866d2014a 108
TheChrisyd 4:aa6866d2014a 109 /* clear flags and flash the led */
TheChrisyd 2:440c95f796ac 110 rx_recieved = false;
TheChrisyd 4:aa6866d2014a 111 myled = !myled;
TheChrisyd 2:440c95f796ac 112 }
TheChrisyd 3:34ae527e9d41 113 #endif
TheChrisyd 3:34ae527e9d41 114
TheChrisyd 0:8fcb46c5fa63 115 }
TheChrisyd 0:8fcb46c5fa63 116 }