Library for the nRF2401A Transceiver

Dependents:   nRF2401A_Hello_World nRF2401A_Wireless_Accelerometer_joypad nRF2401A_Gameduino_Invaders

Files at this revision

API Documentation at this revision

Comitter:
TheChrisyd
Date:
Sat Oct 19 11:10:47 2013 +0000
Parent:
4:e8523ef6e472
Child:
6:ad8242a1379a
Commit message:
Added functions readMsg and readMsg_byte

Changed in this revision

nRF2401A.cpp Show annotated file Show diff for this revision Revisions of this file
nRF2401A.h Show annotated file Show diff for this revision Revisions of this file
--- a/nRF2401A.cpp	Sun Oct 06 16:17:32 2013 +0000
+++ b/nRF2401A.cpp	Sat Oct 19 11:10:47 2013 +0000
@@ -40,7 +40,7 @@
         _state(nRF2401A::UNDEF),
         _rx_handler((nRF2401A_rx_handler_t) 0),
         _rx_handler_arg((void *) 0),
-        _dr1_isr(InterruptIn(dr1)) {
+        _dr1_isr(InterruptIn(dr1))  {
 
     // init member variables
     _data.output();
@@ -91,6 +91,19 @@
     return *this;
 }
 
+nRF2401A& nRF2401A::readMsg( uint8_t *msg_buf, uint8_t msg_len ) {
+    for(int i = 0; i < msg_len; i++)
+    {
+        msg_buf[i] = _data_buf[i];
+    }
+    return *this;
+}
+
+nRF2401A& nRF2401A::readMsg_byte( uint8_t *msg_buf, uint8_t buf_index ) { 
+    *msg_buf = _data_buf[buf_index];
+    return *this;
+}
+
 nRF2401A& nRF2401A::sendMsg(nRF2401A::address_t addr, uint8_t addr_len, uint8_t *msg_buf, uint8_t msg_len) {
 
     // point to start of address byte in address
--- a/nRF2401A.h	Sun Oct 06 16:17:32 2013 +0000
+++ b/nRF2401A.h	Sat Oct 19 11:10:47 2013 +0000
@@ -53,19 +53,30 @@
  * #include "mbed.h"
  * #include "nRF2401A.h"
  * 
- * // comment these out depending on the job of the mbed. If your only using one mbed leave both uncommented.
- * #define TX
+ * /* comment these out depending on the job of the mbed. If your only using one mbed leave both uncommented. */
+ * //#define TX
  * #define RX
  * 
+ * /* If using the FRDM-KL25Z uncomment this line */
+ * //#define FRDMKL25Z
+ * 
  * Serial pc(USBTX, USBRX);
  * DigitalOut  myled(LED1);
  * 
  * #ifdef TX
+ * #ifdef FRDMKL25Z
+ * nRF2401A    rf1(PTD0, PTD5, PTA13, PTC12, PTC13);
+ * #else
  * nRF2401A    rf1(p10, p11, p12, p13, p14);
  * #endif
+ * #endif
  * 
  * #ifdef RX
- * nRF2401A    rf2(p21, p22, p23, p24, p25);
+ * #ifdef FRDMKL25Z
+ * nRF2401A    rf2(PTD0, PTD5, PTA13, PTC12, PTC13);
+ * #else
+ * nRF2401A    rf2(p10, p11, p12, p13, p14);
+ * #endif
  * 
  * bool rx_recieved = false;
  * 
@@ -79,7 +90,8 @@
  *     wait(0.005);
  *     pc.printf("Hello nRF2401A\n\r");
  *     
- * #ifdef TX    
+ * #ifdef TX  
+ *     /* initialise the nRF2401A with payload size, address, CRC, bit rate and channel */   
  *     rf1.setDataPayloadLength(4 << 3)
  *        .setAddress(0x0, 0x0, 0xa6, 0xa6, 0xa6, 3 << 3)
  *        .setCRCMode(nRF2401A::NO_CRC)
@@ -89,12 +101,14 @@
  *     rf1.printControlPacket(pc);
  *     rf1.flushControlPacket();
  *     
+ *     /* initialise variables to use for tranmission */
  *     nRF2401A::address_t rf2_addr = {0x0, 0x0, 0x53, 0x53, 0x53};
  *     uint8_t msg[] = {0x01, 0x01, 0x01, 0x01};
  *     uint32_t *msg32 = (uint32_t *) msg;
  * #endif
  * 
  * #ifdef RX   
+ *     /* initialise the nRF2401A with payload size, address, CRC, bit rate and channel */   
  *     rf2.setDataPayloadLength(4 << 3)
  *        .setAddress(0x0, 0x0, 0x53, 0x53, 0x53, 3 << 3)
  *        .setCRCMode(nRF2401A::NO_CRC)
@@ -103,34 +117,56 @@
  *        
  *     rf2.printControlPacket(pc);
  *     rf2.flushControlPacket();   
+ *     
+ *     /* attach receive callback */
  *     rf2.attachRXHandler(&nRF2401A_rx, 0);
  * #endif
- *      
+ *       
  *     while(1) 
  *     {
  *     
- * #ifdef TX             
+ * #ifdef TX  
+ *         myled = 0;
+ *         wait(0.25);  
+ *         
+ *         /* send the message to the nRF2401A */         
  *         rf1.sendMsg(rf2_addr, 3 << 3, msg, 4 << 3);
  *         *msg32 += 1;
+ *         
+ *         myled = 1;
+ *         wait(0.25);
  * #endif
  * 
- *         myled = 1;
- *         wait(0.25);
  *         
  * #ifdef RX  
  *         if (rx_recieved)
  *         {      
+ *             /* send the read buffer directly to the serial port */
  *             rf2.printDataPacket(pc);
+ *             
+ *             /* send a single byte from the read buffer to the serial port */
+ *             uint8_t rx_msg = 0;
+ *             rf2.readMsg_byte(&rx_msg,  0 );
+ *             pc.printf("\n\r%d\n\r", rx_msg);
+ *             
+ *             /* read the read buffer , then send to the serial port */
+ *             uint8_t rx_buffer[32] = {0};
+ *             rf2.readMsg( &rx_buffer[0], 32);
+ *             for(int i = 0; i < sizeof(rx_buffer); i++)
+ *             {
+ *                 pc.printf("%02x ", rx_buffer[i]);
+ *             }
+ *             pc.printf("\r\n");
+ *             
+ *             /* clear flags and flash the led */
  *             rx_recieved = false;
+ *             myled = !myled;
  *         }
  * #endif 
  *       
- *         myled = 0;
- *         wait(0.25);
  *     }
  * }
  * 
- * 
  * @endcode
  */
 
@@ -145,7 +181,7 @@
 /** Class constructor.
  * The constructor assigns the specified pinout, attatch the
  * DR1 to a pin interrupt and sets up inmutable control packet
- * fields.
+ * fields. for the KL25Z, the Data Ready pin must be on ports A or C
  * @param ce Chip Enable (CE) pin of the nRF2401A.
  * @param c2 Chip Select (CS) pin of the nRF2401A.
  * @param dr1 Data Ready 1 (DR1) pin of the nRF2401A.
@@ -205,7 +241,7 @@
             CRC_8 = 0x1,    /**< Use a 8-bit CRC. */
             CRC_16 = 0x3    /**< Use a 16-bit CRC. */
         } CRC_T;
-        
+        typedef uint8_t read_msg;
 /** Set CRC use.
  * Set the CRC mode field of the control packet.
  * @param mode The CRC mode of choise.
@@ -264,7 +300,25 @@
  *
  */
         typedef uint8_t address_t[5];
-        
+
+/** Read a message.
+ * This routine will transfer the data from the receive buffer to the buffer
+ *  supplied. It will transfer a number of Bytes equal to the specified length.
+ * @param msg_buf Message buffer.
+ * @param msg_len Length of message,  in bits.
+ * @return Reference to the invoked object (for chaining operations).
+ */
+        nRF2401A& readMsg( uint8_t *msg_buf, uint8_t msg_len );
+
+/** Read a byte from message.
+ * This routine will transfer the data from the receive buffer to the buffer
+ *  supplied. It will transfer one Bytes at index buf_index.
+ * @param msg_buf Message body.
+ * @param buf_index index of byte to be read.
+ * @return Reference to the invoked object (for chaining operations).
+ */
+        nRF2401A& readMsg_byte( uint8_t *msg_buf, uint8_t buf_index );
+         
 /** Send a message.
  * This routine will transfer the data from the supplied buffer and send
  * it to the specified address using the current control packet settings.
@@ -336,7 +390,7 @@
         
         uint8_t         *_ctrl_packet;  /**< */
         
-        uint8_t         _data_buf[32];  /**< */        
+        uint8_t         _data_buf[32];  /**< */       
         
         nRF2401A_rx_handler_t   _rx_handler;        /**< */
         void                    *_rx_handler_arg;   /**< */