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

How to change RX to TX and back on the go on NRF52 esb

I want to change rx to tx mode and back after recertion my master NRF24l01 packet on NRF52 with esb, how to do it? I tried this but it doesnt works:

void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
{
    switch (p_event->evt_id)
    {
        case NRF_ESB_EVENT_TX_SUCCESS:
            NRF_LOG("TX SUCCESS EVENT\r\n");
				    // Toggle one of the LEDs.
						nrf_gpio_pin_toggle(LED_2);
            break;
        case NRF_ESB_EVENT_TX_FAILED:
            NRF_LOG("TX FAILED EVENT\r\n");
						nrf_gpio_pin_toggle(LED_3);
						nrf_esb_flush_tx();
            break;
        case NRF_ESB_EVENT_RX_RECEIVED:
            NRF_LOG("RX RECEIVED EVENT\r\n");
            if (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS)
            {
							// Set LEDs identical to the ones on the PTX.
							nrf_gpio_pin_toggle(LED_4);
            }
						nrf_esb_flush_rx();
						TX_MODE_FLAG = 1;
            break;
    }
}

uint32_t esb_init( uint8_t state )
{
    uint32_t err_code;
    uint8_t base_addr_0[4] = {0xE7, 0xE7, 0xE7, 0xE7};
    uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2};
    uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8 };
			
    nrf_esb_config_t nrf_esb_config         = NRF_ESB_DEFAULT_CONFIG;
    nrf_esb_config.payload_length           = 32;
		nrf_esb_config.retransmit_delay         = 1000;
		nrf_esb_config.retransmit_count					= 2;
    nrf_esb_config.protocol                 = NRF_ESB_PROTOCOL_ESB;
    nrf_esb_config.bitrate                  = NRF_ESB_BITRATE_2MBPS;
		switch(state){
			case RX:
						nrf_esb_config.mode             = NRF_ESB_MODE_PRX;
			break;
			case TX:
						nrf_esb_config.mode             = NRF_ESB_MODE_PTX;
			break;	
		}
    nrf_esb_config.event_handler            = nrf_esb_event_handler;
		nrf_esb_config.crc											= NRF_ESB_CRC_16BIT;	
    nrf_esb_config.selective_auto_ack       = true;
		
		err_code = nrf_esb_set_rf_channel(14);
		VERIFY_SUCCESS(err_code);
		
    err_code = nrf_esb_init(&nrf_esb_config);
    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_base_address_0(base_addr_0);
    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_base_address_1(base_addr_1);
    VERIFY_SUCCESS(err_code);

    err_code = nrf_esb_set_prefixes(addr_prefix, 8);
    VERIFY_SUCCESS(err_code);

    return err_code;
}

int main(void)
{
    uint32_t err_code;

    gpio_init();

    err_code = NRF_LOG_INIT();
    APP_ERROR_CHECK(err_code);

    clocks_start();

    err_code = esb_init(RX);
    APP_ERROR_CHECK(err_code);

    NRF_LOG("Enhanced ShockBurst Receiver Example running.\r\n");
	
	  err_code = nrf_esb_start_rx();
    //APP_ERROR_CHECK(err_code);
    while (true)
    {
			
			if(TX_MODE_FLAG)
			{
				//err_code = nrf_esb_stop_rx();
				
				err_code = esb_init(TX);
				
			  tx_payload.length = 32;
				tx_payload.pipe = 0;
				tx_payload.noack = true;
				tx_payload.data[1]++;
				
				
        if (nrf_esb_write_payload(&tx_payload) == NRF_SUCCESS)
        {
						nrf_gpio_pin_toggle(LED_1);	
				}	
				
				err_code = nrf_esb_start_tx();
				
				TX_MODE_FLAG = 0;
				
				err_code = esb_init(RX);
				//err_code = nrf_esb_start_rx();
			}
        __WFE();
    }
}

NRF52 RX and TX modes works well separately with NRF24. I need to unite these modes)

  • I've sovlved it!!! i take a look at an example here The error was here: nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG; i preinitialised ESB as TX. After correction this line like nrf_esb_config_t nrf_esb_config; program starts to work nice.

    /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
     *
     * The information contained herein is property of Nordic Semiconductor ASA.
     * Terms and conditions of usage are described in detail in NORDIC
     * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
     *
     * Licensees are granted free, non-transferable use of the information. NO
     * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
     * the file.
     *
     */
    
    #include "nrf_esb.h"
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "sdk_common.h"
    #include "nrf.h"
    #include "nrf_esb_error_codes.h"
    #include "nrf_delay.h"
    #include "nrf_gpio.h"
    #include "nrf_error.h"
    #include "nrf_log.h"
    #include "boards.h"
    #include "nrf_log.h"
    
    #define RX 0 
    #define TX 1 
    
    uint8_t RX_MODE_FLAG = 0;
    uint8_t TX_MODE_FLAG = 0;
    
    const uint8_t leds_list[LEDS_NUMBER] = LEDS_LIST;
    uint8_t led_nr;
    
    nrf_esb_payload_t 				rx_payload;
    nrf_esb_payload_t        	tx_payload = NRF_ESB_CREATE_PAYLOAD(0, 0xC1, 0x00, 0x05);
    
    nrf_esb_config_t nrf_esb_config;//  = NRF_ESB_DEFAULT_CONFIG;
    
    /************************Functions prototypes*************************************/
    void nrf_esb_error_handler(uint32_t err_code, uint32_t line)
    {
        NRF_LOG_PRINTF("App failed at line %d with error code: 0x%08x\r\n",
                       line, err_code);
    #if DEBUG //lint -e553
        while(true);
    #else
        NVIC_SystemReset();
    #endif
    }
    
    #define APP_ERROR_CHECK(err_code) if(err_code) nrf_esb_error_handler(err_code, __LINE__);
    /*********************************************************************************/
    void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
    {
        switch (p_event->evt_id)
        {
            case NRF_ESB_EVENT_TX_SUCCESS:
    						// Toggle one of the LEDs.
    						nrf_gpio_pin_toggle(LED_2);
    						RX_MODE_FLAG = 1;
                break;
            case NRF_ESB_EVENT_TX_FAILED:
    						nrf_gpio_pin_toggle(LED_3);
    						nrf_esb_flush_tx();
    						RX_MODE_FLAG = 1;
                break;
            case NRF_ESB_EVENT_RX_RECEIVED:
                if (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS)
                {
    							// Set LEDs identical to the ones on the PTX.
    							nrf_gpio_pin_toggle(LED_4);
                }
    						nrf_esb_flush_rx();
    						TX_MODE_FLAG = 1;
                break;
        }
    }
    /*********************************************************************************/
    void clocks_start( void )
    {
        NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
        NRF_CLOCK->TASKS_HFCLKSTART = 1;
        while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
    }
    /*********************************************************************************/
    void gpio_init( void )
    {
        LEDS_CONFIGURE(LEDS_MASK);
    }
    /*********************************************************************************/
    uint32_t esb_init( uint8_t state )
    {
        uint32_t err_code;
        uint8_t base_addr_0[4] = {0xE7, 0xE7, 0xE7, 0xE7};
        uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2};
        uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8 };
    			
    		nrf_esb_config.tx_mode									= NRF_ESB_TXMODE_AUTO;
        nrf_esb_config.payload_length           = 32;
    		nrf_esb_config.retransmit_delay         = 1000;
    		nrf_esb_config.retransmit_count					= 6;
        nrf_esb_config.protocol                 = NRF_ESB_PROTOCOL_ESB;
        nrf_esb_config.bitrate                  = NRF_ESB_BITRATE_2MBPS;
    		switch(state){
    			case RX:
    						nrf_esb_config.mode             = NRF_ESB_MODE_PRX;
    			break;
    			case TX:
    						nrf_esb_config.mode             = NRF_ESB_MODE_PTX;
    			break;	
    		}
        nrf_esb_config.event_handler            = nrf_esb_event_handler;
    		nrf_esb_config.crc											= NRF_ESB_CRC_16BIT;	
        nrf_esb_config.selective_auto_ack       = true;
    		
    		err_code = nrf_esb_set_rf_channel(14);
    		VERIFY_SUCCESS(err_code);
    		
        err_code = nrf_esb_init(&nrf_esb_config);
        VERIFY_SUCCESS(err_code);
    
        err_code = nrf_esb_set_base_address_0(base_addr_0);
        VERIFY_SUCCESS(err_code);
    
        err_code = nrf_esb_set_base_address_1(base_addr_1);
        VERIFY_SUCCESS(err_code);
    
        err_code = nrf_esb_set_prefixes(addr_prefix, 8);
        VERIFY_SUCCESS(err_code);
    
        return err_code;
    }
    /*********************************************************************************/
    int main(void)
    {
        uint32_t err_code;
    
        gpio_init();
        clocks_start();
    		esb_init(RX);
    		nrf_esb_start_rx();
    
    		while (true)
        {
    			if(RX_MODE_FLAG==1)
    			{				
    				RX_MODE_FLAG = 0;
    				// Back to RX
    				nrf_esb_disable();
    				nrf_esb_config.mode = NRF_ESB_MODE_PRX;
    				nrf_esb_init(&nrf_esb_config);
    				nrf_esb_start_rx();
    			}
    			else if(TX_MODE_FLAG==1)
    			{     
    				TX_MODE_FLAG = 0;	
    				
    				nrf_esb_stop_rx();					
    				// Back to TX
    				nrf_esb_disable();
    				nrf_esb_config.mode = NRF_ESB_MODE_PTX;
    				nrf_esb_init(&nrf_esb_config);
    				
    				
    				
    			  tx_payload.length = 32;
    				tx_payload.pipe = 0;
    				tx_payload.noack = true;
    				tx_payload.data[1]++;
    				
    				nrf_esb_start_tx();
    				
            if (nrf_esb_write_payload(&tx_payload) == NRF_SUCCESS)
            {
    						nrf_gpio_pin_toggle(LED_1);		
    				}	
    			}
          //  __WFE();
        }
    }
    /*********************************************************************************/
    
Related