TMRh20 ported to MBED

Fork of TMRh20 by BME SmartLab

Revision:
3:13e43d3101a5
Parent:
2:3332510eebba
Child:
5:836f5c6da243
--- a/RF24.cpp	Wed Mar 16 15:53:01 2016 +0000
+++ b/RF24.cpp	Thu Mar 17 07:19:37 2016 +0000
@@ -12,36 +12,38 @@
 
 /****************************************************************************/
 
-void RF24::csn(bool mode)
+void RF24::csn(int mode)
 {
     csn_pin = mode;
-    wait_us(5);
+    //wait_us(5);
 }
 
 /****************************************************************************/
 
-void RF24::ce(bool level)
+void RF24::ce(int level)
 {
     ce_pin = level;
 }
 
 /****************************************************************************/
 
-inline void RF24::beginTransaction()
+void RF24::beginTransaction()
 {
     //#if defined (RF24_SPI_TRANSACTIONS)
     //_SPI.beginTransaction(SPISettings(RF24_SPI_SPEED, MSBFIRST, SPI_MODE0));
     //#endif
+        
     csn(LOW);
-    //wait_us(1);
+    wait_us(5);
 }
-
 /****************************************************************************/
 
-inline void RF24::endTransaction()
+void RF24::endTransaction()
 {
-    wait_us(2+5); // This is STRANGE here!!!
+
+    wait_us(5);
     csn(HIGH);
+
     //#if defined (RF24_SPI_TRANSACTIONS)
     //_SPI.endTransaction();
     //#endif
@@ -268,15 +270,16 @@
 /****************************************************************************/
 
 RF24::RF24(PinName mosi, PinName miso, PinName sck, PinName _cspin, PinName _cepin):
-    spi(mosi, miso, sck), ce_pin(_cepin), csn_pin(_cspin), p_variant(false),
-    payload_size(32), dynamic_payloads_enabled(false), addr_width(5)//,pipe0_reading_address(0)
+    ce_pin(_cepin), csn_pin(_cspin), p_variant(false),
+    payload_size(32), dynamic_payloads_enabled(false), addr_width(5), //,pipe0_reading_address(0)
+    spi(mosi, miso, sck)
 {
-    pipe0_reading_address[0]=0;
-
     //_SPI.begin(csn_pin);
     spi.frequency(RF24_SPI_SPEED);
     spi.format(8,0);
 
+    pipe0_reading_address[0]=0;
+
     mainTimer.start();
 
 }
@@ -381,7 +384,7 @@
     ce(LOW);
     csn(HIGH);  // extra
 
-    wait_ms(100);
+    //wait_ms(100);
 
     // Must allow the radio time to settle else configuration bits will not necessarily stick.
     // This is actually only required following power up but some settling time also appears to
@@ -448,6 +451,72 @@
     return ( setup != 0 && setup != 0xff );
 }
 
+void RF24::begin_MB(void)
+{
+  // Initialize pins
+//  pinMode(ce_pin,OUTPUT);
+//  pinMode(csn_pin,OUTPUT);
+
+  // Initialize spi bus
+  //spi.begin();
+  mainTimer.start();
+
+  ce(LOW);
+  csn(HIGH);
+
+  // Must allow the radio time to settle else configuration bits will not necessarily stick.
+  // This is actually only required following power up but some settling time also appears to
+  // be required after resets too. For full coverage, we'll always assume the worst.
+  // Enabling 16b CRC is by far the most obvious case if the wrong timing is used - or skipped.
+  // Technically we require 4.5ms + 14us as a worst case. We'll just call it 5ms for good measure.
+  // WARNING: wait_ms is based on P-variant whereby non-P *may* require different timing.
+  wait_ms( 5 ) ;
+
+  // Set 1500uS (minimum for 32B payload in ESB@250KBPS) timeouts, to make testing a little easier
+  // WARNING: If this is ever lowered, either 250KBS mode with AA is broken or maximum packet
+  // sizes must never be used. See documentation for a more complete explanation.
+  write_register(SETUP_RETR,(4 << ARD) | (15 << ARC));
+
+  // Restore our default PA level
+  setPALevel( RF24_PA_MAX ) ;
+
+  // Determine if this is a p or non-p RF24 module and then
+  // reset our data rate back to default value. This works
+  // because a non-P variant won't allow the data rate to
+  // be set to 250Kbps.
+  if( setDataRate( RF24_250KBPS ) )
+  {
+    p_variant = true ;
+  }
+  
+  // Then set the data rate to the slowest (and most reliable) speed supported by all
+  // hardware.
+  setDataRate( RF24_1MBPS ) ;
+
+  // Initialize CRC and request 2-byte (16bit) CRC
+  setCRCLength( RF24_CRC_16 ) ;
+  
+  // Disable dynamic payloads, to match dynamic_payloads_enabled setting
+  write_register(DYNPD,0);
+
+  // Reset current status
+  // Notice reset and flush is the last thing we do
+  write_register(NRF_STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) );
+
+  // Set up default configuration.  Callers can always change it later.
+  // This channel should be universally safe and not bleed over into adjacent
+  // spectrum.
+  setChannel(76);
+
+  // Flush buffers
+  flush_rx();
+  flush_tx();
+  
+  // set EN_RXADDRR to 0 to fix pipe 0 from receiving
+  write_register(EN_RXADDR, 0);
+}
+
+
 
 /****************************************************************************/