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)