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 26 21:51:30 2013 +0000
Parent:
6:ad8242a1379a
Child:
8:fb7cb88e80a4
Commit message:
updated state machine to allow standby to wait until transmission is finished. created private functions to change modes.

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	Sat Oct 19 11:29:23 2013 +0000
+++ b/nRF2401A.cpp	Sat Oct 26 21:51:30 2013 +0000
@@ -37,7 +37,7 @@
         _dr1(DigitalIn(dr1)),
         _clk1(DigitalOut(clk1)),
         _data(DigitalInOut(data)),
-        _state(nRF2401A::UNDEF),
+        _state(nRF2401A::STANDBY),
         _rx_handler((nRF2401A_rx_handler_t) 0),
         _rx_handler_arg((void *) 0),
         _dr1_isr(InterruptIn(dr1))  {
@@ -60,12 +60,15 @@
     _ctrl_packet_buf.rf_power = 0x3;                    // 0 dBm (1 mW) output power
     // ...start in RX mode
     _ctrl_packet_buf.txr_switch = nRF2401A::RX_MODE;
+    _state = nRF2401A::RX ;
     // assure minimum wake up time while assuming tranceiver powers up with uP
     wait_ms(Tpd2cfgm);
 
     return;
 }
 
+// Public functions
+
 void nRF2401A::printControlPacket(Serial& port)
 {
     for(int i = 0; i < sizeof(_ctrl_packet_buf); i++)
@@ -82,12 +85,33 @@
     return;
 }
 
+nRF2401A& nRF2401A::flushControlPacket() {
+    switch (_state) {
+        case nRF2401A::RX:
+            pushCtrl(_ctrl_packet, 15 << 3 );
+            _state = nRF2401A::RX;
+            _ce = 1;
+            _cs = 0;
+            break;
+        case nRF2401A::STANDBY:
+            pushCtrl(_ctrl_packet, 15 << 3 );
+            _state = nRF2401A::STANDBY;
+            _ce = 0;
+            _cs = 0;
+            break;
+        case nRF2401A::TX:
+        default:
+            _ce = 0;
+            _cs = 0;
+    }
+
+    return *this;
+}
+
 nRF2401A& nRF2401A::attachRXHandler(nRF2401A_rx_handler_t handler, void *arg) 
 {
-
     _rx_handler = handler;
     _rx_handler_arg = arg;
-
     return *this;
 }
 
@@ -105,22 +129,22 @@
 }
 
 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
     uint8_t *aligned_addr = &addr[sizeof(address_t) - (addr_len / 8)];
     // wait for tx completion
     int Toa = (_ctrl_packet_buf.rf_data_rate == nRF2401A::BIT_RATE_1MBITS ? 1 : 4) * (addr_len + msg_len + 1 + 16);
 
     switch (_state) {
+        case nRF2401A::STANDBY:
+            //come out of standby into RX mode
+            standby_mode(true);
+        case nRF2401A::TX:
+            //add a wait while in tx mode
+            while (_state == nRF2401A::TX ) {
+            }
         case nRF2401A::RX:
             // switch to transmit
-            _ce = 0;
-            _cs = 0;
-            wait_us(Td);
-            // assert CS/CE and wait Tcs2data
-            _ce = 0;
-            _cs = 1;
-            wait_us(Tcs2data);
+            transmit_mode();
             // push out the bits
             _data = nRF2401A::TX_MODE;
             wait_us(Ts);
@@ -164,35 +188,118 @@
             wait_us(Tsby2txSB + Toa);
 
             // switch back to receive
-            wait_us(Td);
-            // assert CS/CE and wait Tcs2data
-            _cs = 1;
-            wait_us(Tcs2data);
-            // push out the bits
-            _data = nRF2401A::RX_MODE;
-            wait_us(Ts);
-            _clk1 = 1;
-            wait_us(Th);
-            _clk1 = 0;
-            // wait Td
-            wait_us(Td);
-            _data = 0;
-            // deassert CS/CE and done...
-            _cs = 0;
-            // wait Td to avoid simultaineous control high
-            wait_us(Td);
-            _ce = 1;
-            // done
+            receive_mode();
+            break;
+    }
+    return *this;
+}
+
+
+
+nRF2401A& nRF2401A::standby_mode(bool active) {
+    switch (_state) {
+        case nRF2401A::TX:
+            //wait while in tx mode
+            while (_state == nRF2401A::TX ) {
+            }
+        case nRF2401A::RX:
+            if (!active) {
+                _state = nRF2401A::STANDBY;
+                _ce = 0;
+                _cs = 0;
+            }
             break;
         case nRF2401A::STANDBY:
-        case nRF2401A::TX:
-        case nRF2401A::UNDEF:
-        default:
-            // can only send in RX mode
+            if (active) {
+                _state = nRF2401A::RX;
+                _ce = 1;
+                _cs = 0;
+            }
             break;
     }
+    return *this;
+}
 
-    return *this;
+// Private functions
+
+void nRF2401A::transmit_mode( void ) {        
+    _ce = 0;
+    _cs = 0;
+    wait_us(Td);
+    // assert CS/CE and wait Tcs2data
+    _ce = 0;
+    _cs = 1;
+    wait_us(Tcs2data);
+    _state = nRF2401A::TX;
+}
+
+void nRF2401A::receive_mode( void ) {
+    wait_us(Td);
+    // assert CS/CE and wait Tcs2data
+    _cs = 1;
+    wait_us(Tcs2data);
+    // push out the bits
+    _data = nRF2401A::RX_MODE;
+    wait_us(Ts);
+    _clk1 = 1;
+    wait_us(Th);
+    _clk1 = 0;
+    // wait Td
+    wait_us(Td);
+    _data = 0;
+    // deassert CS/CE and done...
+    _cs = 0;
+    // wait Td to avoid simultaineous control high
+    wait_us(Td);
+    _ce = 1;
+    // done
+    _state = nRF2401A::RX;
+}
+
+
+void nRF2401A::dataReadyHandler(void) {
+    switch (_state) {
+        case nRF2401A::RX:
+            pull(_data_buf);
+            if (_rx_handler != (nRF2401A_rx_handler_t) 0)
+                _rx_handler(_rx_handler_arg);
+            break;
+        default:
+            // todo: error msg
+            break;
+    }
+    return;
+}
+
+int nRF2401A::pull(uint8_t *buf) {
+    int n = 0;
+    
+    // read from data pin
+    _data.input();
+    // init signals, go to standby
+    _ce = 1;
+    _cs = 0;
+    _clk1 = 0;
+    // ensure time from DR
+    wait_us(Td);
+    
+    while (_dr1 == 1) {
+        _clk1 = 1;
+        wait_us(Thmin);
+        if(_data.read())
+            buf[n / 8] |= (0x80 >> (n % 8));
+        else
+            buf[n / 8] &= ~(0x80 >> (n % 8));
+        n++;
+        _clk1 = 0;
+        wait_us(Thmin);
+    }
+    // return to active
+    _ce = 1;
+    // reset data pin direction
+    _data.output();
+    
+    return n;
 }
 
 void nRF2401A::pushCtrl(uint8_t *buf, uint8_t n_bits, bool is_ctrl) {
@@ -227,97 +334,3 @@
 
     return;
 }
-
-int nRF2401A::pull(uint8_t *buf) {
-    int n = 0;
-    
-    // read from data pin
-    _data.input();
-    // init signals, go to standby
-    _ce = 1;
-    _cs = 0;
-    _clk1 = 0;
-    // ensure time from DR
-    wait_us(Td);
-    
-    while (_dr1 == 1) {
-        _clk1 = 1;
-        wait_us(Thmin);
-        if(_data.read())
-            buf[n / 8] |= (0x80 >> (n % 8));
-        else
-            buf[n / 8] &= ~(0x80 >> (n % 8));
-        n++;
-        _clk1 = 0;
-        wait_us(Thmin);
-    }
-    // return to active
-    _ce = 1;
-    // reset data pin direction
-    _data.output();
-    
-    return n;
-}
-
-void nRF2401A::activate(bool active) {
-    switch (_state) {
-        case nRF2401A::RX:
-            if (!active) {
-                _state = nRF2401A::STANDBY;
-                _ce = 0;
-                _cs = 0;
-            }
-            break;
-        case nRF2401A::STANDBY:
-            if (active) {
-                _state = nRF2401A::RX;
-                _ce = 1;
-                _cs = 0;
-            }
-            break;
-        case nRF2401A::TX:
-        case nRF2401A::UNDEF:
-        default:
-            break;
-    }
-
-    return;
-}
-
-void nRF2401A::dataReadyHandler(void) {
-    switch (_state) {
-        case nRF2401A::RX:
-            pull(_data_buf);
-            if (_rx_handler != (nRF2401A_rx_handler_t) 0)
-                _rx_handler(_rx_handler_arg);
-            break;
-        default:
-            // todo: error msg
-            break;
-    }
-    return;
-}
-
-nRF2401A& nRF2401A::flushControlPacket() {
-    switch (_state) {
-        case nRF2401A::UNDEF:
-        case nRF2401A::RX:
-            pushCtrl(_ctrl_packet, 15 << 3 );
-            _state = nRF2401A::RX;
-            _ce = 1;
-            _cs = 0;
-            break;
-        case nRF2401A::STANDBY:
-            pushCtrl(_ctrl_packet, 15 << 3 );
-            _state = nRF2401A::STANDBY;
-            _ce = 0;
-            _cs = 0;
-            break;
-        case nRF2401A::TX:
-        default:
-            _ce = 0;
-            _cs = 0;
-    }
-
-    return *this;
-}
\ No newline at end of file
--- a/nRF2401A.h	Sat Oct 19 11:29:23 2013 +0000
+++ b/nRF2401A.h	Sat Oct 26 21:51:30 2013 +0000
@@ -291,10 +291,11 @@
  */
         nRF2401A& flushControlPacket();
         
-/**
- *
+/** Put the tranceiver into, or bring out of standby
+ * Tx mode 10.5mA, RX mode 18mA, Standby 400nA.
+ * @param active set standby state
  */
-        void activate(bool active = true);
+        nRF2401A& standby_mode(bool active = true);
         
 /**
  *
@@ -356,8 +357,7 @@
  */
         typedef enum 
         { 
-            UNDEF,      /**< The start state. */
-            RX,         /**< The tranceiver is in receive mode. */
+            RX,         /**< The tranceiver is in receive mode. Default mode. */
             TX,         /**< The tranceiver is transmitting. */
             STANDBY     /**< The tranceiver goes into stanby mode. */
         } STATE_T;
@@ -426,6 +426,12 @@
  * @return Number of bits read.
  */
         int pull(uint8_t *buf);
+ /** Set the tranceiver into transmit mode
+ */ 
+        void transmit_mode( void );
+ /** Set the tranceiver into receive mode
+ */       
+        void receive_mode( void );
 };
 
 #endif
\ No newline at end of file