This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Radio communication between nRF52840 Dongle and a Arduino device

So, I need to send and receive a data frame with 39 Bytes with a system Dongle-Arduino(with a nRF24L01 module).

My problem is, I tried to configure Dongle to communicate with the Arduino but I couldn't, a Arduino couldn't to interpret nothing from Dongle, and Dongle neither.

When I tried communicate a Arduino with a another one, I got. I tried to configure a Arduino with the same configuration of a default nrf_radio library. There are the Arduino and Dongle code:

 

(In that example, I tried send and receive a 4 bytes only)

(The  pin Map on Arduino module it is ok)

Arduino Code (Rx mode):

#include<Arduino.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <SPI.h>
#include <printf.h>
#define TAMANNHO_FRAME 4
#define chNrf 9
bool radioNumber = 1;                                                         // Set this radio as radio number 0 or 1 - TX
bool role = radioNumber;  
const uint64_t addresses[2]= {0x01,0x00}; // Used to control whether this node is sending or receiving


uint8_t dataFrame[TAMANNHO_FRAME] = {0xFF,0xFF,0xFF,0xFF};

RF24 radio(7,8);   

void radioIni(); 
void txFrame();
void rxFrame();                                                          // Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 

void setup()
{
  pinMode(9,OUTPUT);
  digitalWrite(9,LOW); 
  Serial.begin(115200);
  printf_begin();
  radioIni();  
 
    
radio.printDetails();                                                     // Dump the configuration of the rf unit for debugging
if(radioNumber)
{                                                                         // Open a writing and reading pipe on each radio, with opposite addresses
	radio.openWritingPipe(addresses[1]);
  radio.openReadingPipe(1,addresses[0]);
}
  else
    {
      radio.openWritingPipe(addresses[0]);
      radio.openReadingPipe(1,addresses[1]);
    }
  radio.startListening();                                                     // Start the radio listening for data
  
for(int i = 0; i < 4 ; i++){
      Serial.println(dataFrame[i]);
    }
  
}//setup 


void  loop(){

  rxFrame();
  _delay_ms(10);


}

void txFrame()
{
    radio.stopListening();                                                // First, stop listening so we can talk.
    unsigned long time = micros();                                        // Take the time, and send it.  This will block until complete
    Serial.println("Start Tx"); 
    radio.write( &dataFrame, 4 );

    Serial.println("Successful Tx");  
    radio.startListening(); 
    unsigned long started_waiting_at = micros();                          // Set up a timeout period, get the current microseconds
    boolean timeout = false;                                              // Set up a variable to indicate if a response was received or not
  /*while ( ! radio.available() && ! timeout) NOT USED
    {                                                                     // While nothing is received
      if (micros() - started_waiting_at > 200000 )  
      {                                                                   // If waited longer than 200ms, indicate timeout and exit while loop
        timeout = true;
        break;
      }      
    }*/
    if ( timeout ) // NOT USED
    {                                                                   // Describe the results
      Serial.println(F("time out."));
    }
    else
      {
        unsigned long got_time;                                         // Grab the response, compare, and send to debugging spew
        radio.read( dataFrame, sizeof(byte) );
        //unsigned long time = micros();
      }                                                                 // Try again 1s later
    delay(10);
}//txFrame  
 

void rxFrame()
{
 
  unsigned long got_time;
  if(radio.available()) { // 
    //while (radio.available()) 
    //{                                                                   // While there is data ready
      radio.read( &dataFrame, 4);             // Get the payload
    //}
    radio.stopListening();                                              // First, stop listening so we can talk   
    radio.write( &dataFrame, 4);              // Send the final one back.      
    for(int i = 0; i < 4 ; i++){
      Serial.println(dataFrame[i],HEX);
    }
    radio.startListening();                                             // Now, resume listening so we catch the next packets.     
    Serial.println(F(" TX OK"));
  }
}//rxFrame


void radioIni()
{
    radio.begin();                                                            // Inicializa Radio
    radio.powerUp();                                                          // NRF24 sai do sleep mode
    radio.disableCRC();
    radio.setAutoAck(1);                                                      // Ensure autoACK is enabled
    radio.enableAckPayload();                                                 // Allow optional ack payloads
    radio.setRetries(5,5);                                                    // Smallest time between retries, max no. of retries
    radio.setPayloadSize(10);                                                 // Here we are sending 1-byte payloads to test the call-response speed
    radio.setDataRate(RF24_1MBPS);                                          // Set Radio para 250Kbps
    radio.setPALevel(RF24_PA_MIN);                                            // Set the PA Level high to prevent power supply related issues since this is a
                                                                              // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default. 
    radio.openWritingPipe(addresses[0]);                                      // Both radios listen on the same pipes by default, and switch when writing
    radio.openReadingPipe(1,addresses[1]);
    radio.getCRCLength();
    radio.startListening(); 

}//radioIni

Dongle Code (Tx Mode) (There is a SDK example, but I forced up a package with a static value )

/**
 * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
/** @file
*
* @defgroup nrf_dev_button_radio_tx_example_main main.c
* @{
* @ingroup nrf_dev_button_radio_tx_example
*
* @brief Radio Transceiver Example Application main file.
*
* This file contains the source code for a sample application using the NRF_RADIO peripheral.
*
*/

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "radio_config.h"
#include "nrf_gpio.h"
#include "app_timer.h"
#include "boards.h"
#include "bsp.h"
#include "nordic_common.h"
#include "nrf_error.h"
#include "nrf_delay.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

static uint32_t                   packet;                    /**< Packet to transmit. */

/**@brief Function for sending packet.
 */
void send_packet()
{
    // send the packet:
    NRF_RADIO->EVENTS_READY = 0U;
    NRF_RADIO->TASKS_TXEN   = 1;

    while (NRF_RADIO->EVENTS_READY == 0U)
    {
        // wait
    }
    NRF_RADIO->EVENTS_END  = 0U;
    NRF_RADIO->TASKS_START = 1U;

    while (NRF_RADIO->EVENTS_END == 0U)
    {
        // wait
    }

    uint32_t err_code = bsp_indication_set(BSP_INDICATE_SENT_OK);
    NRF_LOG_INFO("The packet was sent");
    APP_ERROR_CHECK(err_code);

    NRF_RADIO->EVENTS_DISABLED = 0U;
    // Disable radio
    NRF_RADIO->TASKS_DISABLE = 1U;

    while (NRF_RADIO->EVENTS_DISABLED == 0U)
    {
        // wait
    }
}


/**@brief Function for handling bsp events.
 */
void bsp_evt_handler(bsp_event_t evt)
{
    uint32_t prep_packet = 0;
    switch (evt)
    {
        case BSP_EVENT_KEY_0:
            /* Fall through. */
        case BSP_EVENT_KEY_1:
            /* Fall through. */
        case BSP_EVENT_KEY_2:
            /* Fall through. */
        case BSP_EVENT_KEY_3:
            /* Fall through. */
        case BSP_EVENT_KEY_4:
            /* Fall through. */
        case BSP_EVENT_KEY_5:
            /* Fall through. */
        case BSP_EVENT_KEY_6:
            /* Fall through. */
        case BSP_EVENT_KEY_7:
            /* Get actual button state. */
            for (int i = 0; i < BUTTONS_NUMBER; i++)
            {
                prep_packet |= (bsp_board_button_state_get(i));
            }
            break;
        default:
            /* No implementation needed. */
            break;
    }
    packet = 0xAA550110;
}


/**@brief Function for initialization oscillators.
 */
void clock_initialization()
{
    /* Start 16 MHz crystal oscillator */
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART    = 1;

    /* Wait for the external oscillator to start up */
    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
    {
        // Do nothing.
    }

    /* Start low frequency crystal oscillator for app_timer(used by bsp)*/
    NRF_CLOCK->LFCLKSRC            = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_LFCLKSTART    = 1;

    while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
    {
        // Do nothing.
    }
}


/**
 * @brief Function for application main entry.
 * @return 0. int return type required by ANSI/ISO standard.
 */
int main(void)
{
    
    uint32_t err_code = NRF_SUCCESS;

    clock_initialization();

    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();

    err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_evt_handler);
    APP_ERROR_CHECK(err_code);

    // Set radio configuration parameters
    radio_configure();

    // Set payload pointer
    NRF_RADIO->PACKETPTR = (uint32_t)&packet;

    err_code = bsp_indication_set(BSP_INDICATE_USER_STATE_OFF);
    NRF_LOG_INFO("Radio transmitter example started.");
    NRF_LOG_INFO("Press Any Button");
    APP_ERROR_CHECK(err_code);

   packet = 0xAA551001;
    while (true)
    {
 

        if (packet != 0)
        {
            send_packet();
            NRF_LOG_INFO("The contents of the package was %u", (unsigned int)packet);
   
        }
        NRF_LOG_FLUSH();
        __WFE();
        nrf_delay_ms(1);
    }
}


/**
 *@}
 **/

I'm try to use a default radio_config on dongle.

Related