Hello world example for the nRF2401A Library

Dependencies:   mbed nRF2401A

Committer:
TheChrisyd
Date:
Sun Mar 09 11:58:07 2014 +0000
Revision:
7:202814542566
Parent:
6:f43bf63489bb
Changes to match updated library

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 7:202814542566 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 6:f43bf63489bb 16 nRF2401A rf1(PTD0, PTD5, PTA13, PTC12, PTC13); //ce, cs, dr1, clk1, data
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 7:202814542566 26 nRF2401A rf2(p21, p22, p23, p24, p25);
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 7:202814542566 42 /* initialise the nRF2401A with payload size and address */
TheChrisyd 7:202814542566 43 rf1.setAddress(0x0, 0x0, 0xa6, 0xa6, 0xa6, 3 << 3);
TheChrisyd 7:202814542566 44
TheChrisyd 3:34ae527e9d41 45 rf1.printControlPacket(pc);
TheChrisyd 3:34ae527e9d41 46 rf1.flushControlPacket();
TheChrisyd 3:34ae527e9d41 47
TheChrisyd 4:aa6866d2014a 48 /* initialise variables to use for tranmission */
TheChrisyd 3:34ae527e9d41 49 nRF2401A::address_t rf2_addr = {0x0, 0x0, 0x53, 0x53, 0x53};
TheChrisyd 7:202814542566 50
TheChrisyd 3:34ae527e9d41 51 uint8_t msg[] = {0x01, 0x01, 0x01, 0x01};
TheChrisyd 3:34ae527e9d41 52 uint32_t *msg32 = (uint32_t *) msg;
TheChrisyd 3:34ae527e9d41 53 #endif
TheChrisyd 0:8fcb46c5fa63 54
TheChrisyd 0:8fcb46c5fa63 55 #ifdef RX
TheChrisyd 7:202814542566 56 /* initialise the nRF2401A with payload size and address */
TheChrisyd 7:202814542566 57 rf2.setAddress(0x0, 0x0, 0x53, 0x53, 0x53, 3 << 3);
TheChrisyd 0:8fcb46c5fa63 58
TheChrisyd 3:34ae527e9d41 59 rf2.printControlPacket(pc);
TheChrisyd 3:34ae527e9d41 60 rf2.flushControlPacket();
TheChrisyd 4:aa6866d2014a 61
TheChrisyd 4:aa6866d2014a 62 /* attach receive callback */
TheChrisyd 3:34ae527e9d41 63 rf2.attachRXHandler(&nRF2401A_rx, 0);
TheChrisyd 0:8fcb46c5fa63 64 #endif
TheChrisyd 0:8fcb46c5fa63 65
TheChrisyd 3:34ae527e9d41 66 while(1)
TheChrisyd 3:34ae527e9d41 67 {
TheChrisyd 3:34ae527e9d41 68
TheChrisyd 4:aa6866d2014a 69 #ifdef TX
TheChrisyd 4:aa6866d2014a 70 myled = 0;
TheChrisyd 4:aa6866d2014a 71 wait(0.25);
TheChrisyd 4:aa6866d2014a 72
TheChrisyd 4:aa6866d2014a 73 /* send the message to the nRF2401A */
TheChrisyd 0:8fcb46c5fa63 74 rf1.sendMsg(rf2_addr, 3 << 3, msg, 4 << 3);
TheChrisyd 0:8fcb46c5fa63 75 *msg32 += 1;
TheChrisyd 4:aa6866d2014a 76
TheChrisyd 4:aa6866d2014a 77 myled = 1;
TheChrisyd 4:aa6866d2014a 78 wait(0.25);
TheChrisyd 0:8fcb46c5fa63 79 #endif
TheChrisyd 3:34ae527e9d41 80
TheChrisyd 3:34ae527e9d41 81
TheChrisyd 2:440c95f796ac 82 #ifdef RX
TheChrisyd 2:440c95f796ac 83 if (rx_recieved)
TheChrisyd 2:440c95f796ac 84 {
TheChrisyd 4:aa6866d2014a 85 /* send the read buffer directly to the serial port */
TheChrisyd 2:440c95f796ac 86 rf2.printDataPacket(pc);
TheChrisyd 4:aa6866d2014a 87
TheChrisyd 4:aa6866d2014a 88 /* send a single byte from the read buffer to the serial port */
TheChrisyd 4:aa6866d2014a 89 uint8_t rx_msg = 0;
TheChrisyd 7:202814542566 90 rx_msg = rf2.readMsg_byte( 0 );
TheChrisyd 4:aa6866d2014a 91 pc.printf("\n\r%d\n\r", rx_msg);
TheChrisyd 4:aa6866d2014a 92
TheChrisyd 4:aa6866d2014a 93 /* read the read buffer , then send to the serial port */
TheChrisyd 4:aa6866d2014a 94 uint8_t rx_buffer[32] = {0};
TheChrisyd 4:aa6866d2014a 95 rf2.readMsg( &rx_buffer[0], 32);
TheChrisyd 4:aa6866d2014a 96 for(int i = 0; i < sizeof(rx_buffer); i++)
TheChrisyd 4:aa6866d2014a 97 {
TheChrisyd 4:aa6866d2014a 98 pc.printf("%02x ", rx_buffer[i]);
TheChrisyd 4:aa6866d2014a 99 }
TheChrisyd 4:aa6866d2014a 100 pc.printf("\r\n");
TheChrisyd 4:aa6866d2014a 101
TheChrisyd 4:aa6866d2014a 102 /* clear flags and flash the led */
TheChrisyd 2:440c95f796ac 103 rx_recieved = false;
TheChrisyd 4:aa6866d2014a 104 myled = !myled;
TheChrisyd 2:440c95f796ac 105 }
TheChrisyd 3:34ae527e9d41 106 #endif
TheChrisyd 3:34ae527e9d41 107
TheChrisyd 0:8fcb46c5fa63 108 }
TheChrisyd 0:8fcb46c5fa63 109 }