Does the RF24 library (from TMRh20) work on STM32F103?

Can someone help me port this library?
madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by madias » Fri Mar 11, 2016 10:25 pm

Thank you very much I ll going to try it out on Monday!

madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by madias » Sun Mar 13, 2016 2:13 pm

jbforrer wrote:@madias --- I had good luck with the TMRh20 library (with few changes) using a Blue Pill module.
Attached a working example as used with my home sensor network.
Hope it helps.

Regards.
-JBF.
jbforrer: Ok, thank you, it works!

For all other users:
I've changed the name of the library (and all #includes) to RF24_STM32, so you can put it into the hardware/Arduino_STM32/STM32F1/libraries folder and it will coexist with the standard RF24 library (I think most of us are using different MCU's with one IDE)
Warning: Many examples won't work (like "scanner") because of the lack of printf in STM32duino, but the basic functionality - transfer data between modules - works like a charm! The main advantage of the RF24 lib is, that the most of known dev-boards-MCU's are supported: AVR + tiny, SAM, RPi, ESP8266, teensy
Attachments
RF24_STM32.zip
(33.83 KiB) Downloaded 435 times

User avatar
RogerClark
Posts: 7540
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by RogerClark » Sun Mar 13, 2016 9:42 pm

Thanks Matthias

Perhaps I should add it to the repo

madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by madias » Mon Mar 14, 2016 10:52 am

Dear Roger,
I don't think this library is ready for the main repo, I think in the main repo should be only libs they are 100% tested and proofed.
But (!):
Maybe it would be a good idea to make an folder on github for those "not 100% libs? So we can refer every time to this github link and we can collect all this "unfinished" but very useful libs out there in the forum and/or on different github accounts. Otherwise we would loose many potential codes over the time....
Maybe a good github name would be "User libraries [not verified]" or something. So the user can do a "pull request" and you can just accept it without thinking of compatibility issues or something. If a library is tested by several users it could be dragged into the main repo?

User avatar
mrburnette
Posts: 2013
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by mrburnette » Mon Mar 14, 2016 12:01 pm

Of course, IMO, having the untested code on github is a two-edged sword.
Yes, it permits a holding grounds and archive, but
It also permits an area where the sources are unknown, the working state is unknown, and the quality unknown.

My fear is that traditional 8-bit to 32-bit porting efforts will wind-up in an incomplete state and just become a junkyard of digital goop.

We have always stressed on the forum that the person needing the library should manage their own port and engage the forum for specific issues. Thus, when the library is complete and is working satisfactory, a pull request can be issued to Roger. We now have working code and some issues that can be documented.

Just my thoughts,


Ray

konczakp
Posts: 191
Joined: Thu Jul 14, 2016 4:17 pm

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by konczakp » Mon Oct 03, 2016 10:07 am

@madias

Could You please add an example of two way communication like a ping-pong for tx and rx devices?

Thanks in advance

User avatar
Manny
Posts: 44
Joined: Wed Dec 09, 2015 3:15 pm

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by Manny » Mon Oct 03, 2016 5:20 pm

konczakp wrote:@madias

Could You please add an example of two way communication like a ping-pong for tx and rx devices?

Thanks in advance
This should work and is a better get starting sketch, its not my code, just change the "boolean sender = false;" to "boolean sender = true;" on one of the nrf24l01

Code: Select all

/*
Sending formatted data packet with nRF24L01.
Maximum size of data struct is 32 bytes.


#include <SPI.h>
#include <RF24_STM32.h>

// For printf support
int serial_putc( char c, FILE * )
{
  Serial.write( c );
  return c;
}

const uint64_t pipe = 0xE8E8F0F0E1LL;

RF24 radio(PA14, PA15); 
// The sizeof this struct should not exceed 32 bytes
struct MyData {
  byte byte0;
  byte byte1;
  byte byteArray[2];
  int int0;
  int int1;
  int intArray[2];
  float float0;
  float float1;
  float floatArray[2];
  unsigned long packetId; // 16 month rollover at 100 packets/sec
};

/**************************************************/

// Change this when uploading to each Arduino
boolean sender = false;

// Print some info to serial after this number of packets
unsigned long printRate = 1000;

void setup()
{
   disableDebugPorts();
  Serial.begin(9600);
 // fdevopen( &serial_putc, 0 ); // for printf

  // Set up radio module
  radio.begin();
  radio.setDataRate(RF24_250KBPS); // Both endpoints must have this set the same
  //radio.setAutoAck(false);       // Either endpoint can set to false to disable ACKs
  radio.setPALevel(RF24_PA_MAX); // Power amplifier level. Also LOW,MED,HIGH (default is HIGH)
  radio.printDetails();

  if ( sender )
    radio.openWritingPipe(pipe);
  else {
    radio.openReadingPipe(1,pipe);
    radio.startListening();
  }

  int dataSize = sizeof(MyData);
  Serial.print("Size of MyData: ");
   Serial.println(dataSize);
  if ( dataSize > 32 )
     Serial.println("*** MyData struct is too large ***");

   Serial.print("This Arduino is set to be a ");
    Serial.print(sender);
     Serial.println( "? sender : receiver");
      //Serial.print
}

/**************************************************/

unsigned long nextPacketId = 0;   // Stamp packets with their sequence
float floatSensor = 0;            // A fake 'sensor' so we can see some data changing
unsigned long unAckedPackets = 0; // To estimate how many packets were not ACKed
unsigned long lastPrintTime = 0;  // To measure time between printouts

void sendData()
{
  MyData data;

  // Set properties of your data here...
  data.float0 = floatSensor;
  data.packetId = nextPacketId;

  // Record a before and after time to see how long the write takes
  unsigned long beforeWrite = millis();

  radio.write(&data, sizeof(MyData));

  unsigned long afterWrite = millis();

  // Although the RF24 documentation states that the timeout for
  // receiving an ACK is 60ms, in my experience it seems to wait
  // for only about 2-3ms. The value 2 here was chosen by printing
  // out the time taken for 1000 writes when the receiver was non
  // existent (ie. all packets dropped) and this was around 2788ms
  // between two Arduino UNOs. So I figure that any writes taking
  // 3ms or more have likely been abandoned without receiving any
  // ACK. Note that this does not mean the packet was not actually
  // received at the other end, it just means that the sender could
  // not confirm that the packet arrived.
  if ( afterWrite - beforeWrite > 2)
    unAckedPackets++;

  // Every n packets, print out some info
  if ( data.packetId % printRate == 0 ) {

    if ( data.packetId > 0 ) {
     
      // Use bytes/msec as an approximate kbytes/sec value
      unsigned long now = millis();     
      unsigned long packetsec = ((printRate - unAckedPackets) * 1000) / (now - lastPrintTime);
      int kbsec = (sizeof(MyData) * (printRate - unAckedPackets)) / (now - lastPrintTime);
      lastPrintTime = now;

      // Format the float for printing
      char buf[16];
      dtostrf(data.float0, 0, 6, buf);

      // As mentioned above, the dropped count is only a rough estimate.
      // You may see some non-zero values even when no packets were really
      // dropped, but it's not bad as a rough guide. You may need to alter
      // the value 2 used above depending on the speed of the microprocessors
      // you are using, to get a better estimate.
     // printf("Wrote %ld packets, last id = %ld, float0 = %s, unAcked %ld, %ld packets/sec (%d kbyte/sec)\n", printRate, data.packetId, buf, unAckedPackets, packetsec, kbsec);
            Serial.print("Wrote packets = ");
       Serial.println(printRate);
       Serial.print("Last iD = ");
       Serial.println(data.packetId);
       Serial.print("Float0 = ");
       Serial.println(buf);
        Serial.print("unAcked = ");
         Serial.println(unAckedPackets);
          Serial.print("Packets/sec = ");
          Serial.println(packetsec);
          Serial.print("Kbyte/sec = ");
          Serial.println(kbsec);
          Serial.println("--------");
          Serial.println("");
    }
   
    unAckedPackets = 0;
  }

  // Increment the packet id for next time
  nextPacketId++;

  // Change the 'sensor' value so we can check floats are working
  floatSensor += 0.000001 * random(1000);
}

/**************************************************/

unsigned long packetsRead = 0;      // Counts total packets read
unsigned long expectedPacketId = 0; // To track whether the packet id was what we expected
int missedPackets = 0;              // How many packets missed since last printout

void recvData()
{
  if ( radio.available() ) {
    MyData data;

    radio.read(&data, sizeof(MyData));
    packetsRead++;

    // Check if the packet id was what we expected. Unlike for the sender
    // this is not an estimate, it's a known value because we can look at
    // incoming packet ids to see how many we missed.
    if ( expectedPacketId > 0 )
      missedPackets += data.packetId - expectedPacketId;

    // Set the packet id we will expect next time
    expectedPacketId = data.packetId + 1;

    // Every n packets, print out some info
    if ( data.packetId > 0 && packetsRead % printRate == 0 ) {

      // Use bytes/msec as an approximate kbytes/sec value
      unsigned long now = millis();     
      unsigned long packetsec = ((printRate - unAckedPackets) * 1000) / (now - lastPrintTime);
      int kbsec = (sizeof(MyData) * printRate) / (now - lastPrintTime);
      lastPrintTime = now;

      // Format the float for printing
      char buf[16];
      dtostrf(data.float0, 0, 6, buf);

      Serial.print("Read packets = ");
       Serial.println(packetsRead);
       Serial.print("Last iD = ");
       Serial.println(data.packetId);
       Serial.print("Float0 = ");
       Serial.println(buf);
        Serial.print("Skipped = ");
         Serial.println(missedPackets);
          Serial.print("Packets/sec = ");
          Serial.println(packetsec);
          Serial.print("Kbyte/sec = ");
          Serial.println(kbsec);
          Serial.println("--------");
          Serial.println("");
       
      missedPackets = 0;     
    }
  } 
}

/**************************************************/

void loop()
{
  if ( sender )
    sendData();
  else
    recvData();
}


konczakp
Posts: 191
Joined: Thu Jul 14, 2016 4:17 pm

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by konczakp » Tue Oct 04, 2016 11:25 am

Thanks a lot for that! :) Unfortunately I'm still having some problems with big amount of lost packets. I will have to check it out.

User avatar
zoomx
Posts: 550
Joined: Mon Apr 27, 2015 2:28 pm
Location: Mt.Etna, Italy

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by zoomx » Tue Oct 04, 2016 12:12 pm

It can be a power problem. Try to add a 100uF cap to the power pins.

User avatar
Manny
Posts: 44
Joined: Wed Dec 09, 2015 3:15 pm

Re: Does the RF24 library (from TMRh20) work on STM32F103?

Post by Manny » Tue Oct 04, 2016 3:51 pm

You can also try lowering the PA level and see if it is a problem with the power supply.

Code: Select all

radio.setPALevel(RF24_PA_LOW)

Post Reply