Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: Teensy 3.1 nrf24l01+ issue

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4

    Unhappy Teensy 3.1 nrf24l01+ issue

    Code written for communication using nrf24l01+ works if I use two arduino UNOs.
    But when I try to upload the same code for receiver to teensy3.1 using the same nrf24l01+ board, it does not work.
    All I can see on serial monitor is "tick". No data is received.
    "Sender" runs on Arduino UNO.
    SPI library is standard arduino library.

    Pin connection for arduino and teensy is the same:
    controller ->nrf
    gnd -> 1
    vcc -> 2
    9 -> 3
    10 -> 4
    11 -> 6
    12 ->7
    13 ->5
    irq(8) is not connected(black wire)
    All connections are tested.

    Arduino IDE version 1.0.5
    Teensy version 1.18rc2
    Code and photos of connections are attached.
    Click image for larger version. 

Name:	2014-01-21 23.00.11.jpg 
Views:	137 
Size:	130.7 KB 
ID:	1310Click image for larger version. 

Name:	2014-01-21 23.03.32.jpg 
Views:	75 
Size:	121.0 KB 
ID:	1311

    Code for receiver:
    Code:
    #include <SPI.h>
    #include "RF24.h"
    /********** radio ************/
    
    RF24 radio(9,10); // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
    
    const uint64_t reading_pipe = 0xF0F0F0F0E2LL;
    
    void init_radio(void){
      radio.begin();
      radio.setRetries(1,0);
      radio.setCRCLength(RF24_CRC_16);
      radio.setAutoAck(false);
      radio.setPayloadSize(sizeof(int));
      radio.openReadingPipe(1,reading_pipe);
      radio.startListening();
    }
    
    #define RECEIVE_DELAY_MICROS 3300
    
    void transfer_all_payloads(void){
      int data = 0;
      radio.read(& data,sizeof(int));
      Serial.println(data);
    }
    
    void setup(void)
    { 
      Serial.begin(115200);
      init_radio();
    }
    
    void loop(void)
    {
      Serial.println("tick");
      if(radio.available()){
        transfer_all_payloads();
      }
      delay(1000);
    }
    Code for transmitter:
    Code:
    #include <SPI.h>
    #include "RF24.h"
    
    /************************* radio *****************/
    RF24 radio(9,10); // Set up nRF24L01 radio on SPI bus plus pins 9 & 10
    const uint64_t writing_pipe = 0xF0F0F0F0E2LL;
    
    void init_radio(void){
      radio.begin();
      radio.setRetries(1,0);
      radio.setAutoAck(false);
      radio.setCRCLength(RF24_CRC_16);
      radio.setPayloadSize(sizeof(int));
      radio.openWritingPipe(writing_pipe);
    }
    
    void setup(void)
    {
      Serial.begin(115200);
      init_radio();
    }
    
    void loop(void)
    { 
      for(int i=0;i<1000;i++){
        Serial.print("sending ");
        Serial.println(i);
        radio.startWrite(& i,sizeof(int));
        delay(1000);
      }
    }
    RF24 library:
    nRF24L01.h
    RF24_config.h
    RF24.cpp
    RF24.h

  2. #2
    Senior Member Wozzy's Avatar
    Join Date
    Jan 2013
    Location
    Philadelphia, Pennsylvania USA
    Posts
    111

    Try this

    Follow the instructions on DC740's thread here: nRF24L01+ working in Teensy++ 2.0 and Teensy 3.0

    This worked for me between a T3.1 and a T3.0 the on first try.
    Thanks DC740
    Last edited by Wozzy; 01-22-2014 at 09:56 PM.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    I tried to use libraries and sketches mentioned above.
    Still does not work on Teensy 3.1.
    I tried two nrf modules and two teensy 3.1, still no luck.

  4. #4
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    1,478
    I've long wondered... those nF24L01's are low bit rate but very low power. At 2.4GHz, and with a crude on-PCB antenna, do these things achieve range such that a pair can deal with, say, 100 ft. range and 2 or so drywall-walls in the path?

    The on-board PCB antennas can tend to be orientation sensitive due to their pattern.

    This, versus a sub-GHz radio.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Hooray! I have finally found the error!
    The problem is struct byte alignment is different on arduino and on teensy3. Consider structure
    Code:
    typedef struct{ //5 bytes
      unsigned long timestamp; //4 bytes
      uint8_t crc; //1 byte
    } snapshot_t;
    Since teensy is 32bit,
    Code:
    Serial.println(sizeof(snapshot_t));
    returns 8 on teensy.
    Arduinos are 8 bit, therefore
    Code:
    Serial.println(sizeof(snapshot_t));
    returns 5 on arduino.
    Solution:
    Code:
    #pragma pack(1)
    typedef struct{ //5 bytes
      unsigned long timestamp; //4 bytes
      uint8_t crc; //1 byte
    } snapshot_t;
    Last edited by renatyv; 02-12-2014 at 05:57 PM.

  6. #6
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    1,478
    Yes... common mistake when moving from 8 bit micros.

    #pragma pack(1), on other C compilers, often has an equivalent but different syntax.

    uint8_t long timestamp; //4 bytes
    shouldn't that be: unsigned long timestamp;
    Last edited by stevech; 02-12-2014 at 04:45 PM.

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    You are wright. I fixed the code in the post.

  8. #8
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    1,478
    Quote Originally Posted by renatyv View Post
    You are wright. I fixed the code in the post.
    I wonder why the compiler doesn't error on: uint8_t long timestamp;
    It seems to ignore uint8_t and use long, making this a signed rather than unsigned. That can bite you later.

  9. #9
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    wondering if anyone managed to get this library working on the Teensy 3.1 ?

    https://github.com/TMRh20/RF24

  10. #10
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    1,478
    You know about this library that supports the Nordic and many other radios?
    http://www.airspayce.com/mikem/arduino/RadioHead/

  11. #11
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    wow, nice library, thanks so much!

  12. #12
    One gotcha I noticed with the #pragma pack(1) solution is that duff2013's enormously useful LowPower_Teensy3 lib defines a structure that works only because it isn't normally packed. Ye olde 32 bit processor and addressing word alignment complement to the structure packing ying-yang. You can fix it by adding padding 3 padding bytes in LowPower_Teensy3.h thusly:

    Code:
    typedef struct sleep_block_struct {
        /* Structure wake source */
        volatile uint32_t wake_source;
        /* Structure RTC Config */
        unsigned long rtc_alarm;
        /* Module Wake Type */
        uint32_t modules;
        /* Structure GPIO Config */
        uint16_t gpio_pin;
        uint16_t gpio_mode;
        /* Structure LPTMR Config */
        uint16_t lptmr_timeout;
        /* Structure TSI Config */
        uint16_t tsi_threshold;
        uint8_t tsi_pin;
        uint8_t pad1;
        uint8_t pad2;
        uint8_t pad3;
        /* pointer to callback function */
        void (*callback)();
        sleep_block_struct() : wake_source(0), rtc_alarm(0), modules(0),
                               gpio_pin(0), gpio_mode(0), lptmr_timeout(0),
                               tsi_threshold(0), tsi_pin(0), callback(NULL) {};
    } sleep_block_t;
    Or by moving elements in the structure around to word-align. I didn't play around with that.

    This got me up and running with my nrf24 hibernating, waking, flinging packets, hibernating and whatnot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •