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

Changing the content of nRF ESB payload

Hello,

After decided and tried porting nrf_esb from the example code to my project, now I am stuck with changing the content of the payload.

I changed the payload using:

for (uint8_t i = 0; i < tx_payload.length; i++) {
			tx_payload.data[i] += 1;
//			printf("tx_payload.data[%d]: %x\r\n", i, tx_payload.data[i]);
		}

And here's my handler:

void nrf_esb_event_handler(nrf_esb_evt_t const * p_event) {
	nrf_gpio_pin_toggle(LED_2);
	printf("handler\r\n");
//	NRF_LOG_INFO("Inside handler!");
//	NRF_LOG_FLUSH();
	switch (p_event->evt_id) {
	case NRF_ESB_EVENT_TX_SUCCESS:
//		NRF_LOG_INFO("Packet sending succeeded!");
		printf("sent? PID: %x\r\n", tx_payload.pid);
		nrf_esb_pop_tx();
		break;
	case NRF_ESB_EVENT_TX_FAILED:
		(void) nrf_esb_flush_tx();
		(void) nrf_esb_start_tx();
		//NRF_LOG_INFO("Packet sending failed!");
		break;
	case NRF_ESB_EVENT_RX_RECEIVED:
		// Get the most recent element from the RX FIFO.
		while (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS) {
			printf("Received a packet. ID: %x:\r\n", rx_payload.pid);
			for (uint8_t i = 0; i < rx_payload.length; i++) {
				printf("rx_payload.data[%d]: %x\r\n", i, rx_payload.data[i]);
			}
			// Set LEDs identical to the ones on the PTX.
//			NRF_LOG_INFO("Packet Received!");
		}
		//nrf_esb_flush_rx();
		break;

	}
//	NRF_LOG_INFO("Exit!");
//	NRF_LOG_FLUSH();
	//nrf_gpio_pin_toggle(LED_2);
}

Every time a "send packet command" is fired, it should increment the content inside tx_payload by 1 then send it.

Upon printing data, it always print the old data. Printing on the receiver will not happen if I did not fire sending on sender. The only way to print a new data is to reset it first.

Is this the correct way to use nrf_esb? or is it not?

Thank you,
Winz

Parents
  • Hi Winz

    If this problem persists we can continue the discussion in this case, to avoid cluttering the other one too much. 

    Can you attach your main.c file, so I can have a look at the rest of the code also?

    Best regards
    Torbjørn

  • Hi Torbjørn,

    I found a workaround for this: either I don't print it (possible problem with printf? I can't use NRF_LOG in my application, so I use printf) OR flush, stop, then start the rx. Is it how it supposed to be processed?

    *EDIT* Not really, the old data still resides in the Rx buffer..

    Also, I would like to ask another question: upon writing payload using `nrf_esb_write_payload`, I sometimes receive NRF_SUCCESS on the return, but some other numbers like 4 and 9. May I know if it's documented somewhere?

    Thank you,
    Winz

Reply
  • Hi Torbjørn,

    I found a workaround for this: either I don't print it (possible problem with printf? I can't use NRF_LOG in my application, so I use printf) OR flush, stop, then start the rx. Is it how it supposed to be processed?

    *EDIT* Not really, the old data still resides in the Rx buffer..

    Also, I would like to ask another question: upon writing payload using `nrf_esb_write_payload`, I sometimes receive NRF_SUCCESS on the return, but some other numbers like 4 and 9. May I know if it's documented somewhere?

    Thank you,
    Winz

Children
  • Hi Winz

    What does the printf retarget to?

    If you are using the UART interface it could be that you are spending too much time pushing bytes to the UART interface, and that you are stuck in the interrupt for too long. 
    An alternative solution to this should be to set a flag in the ESB event, rather than run this code, and check the flag in your main loop. Then you can run the code from there in thread context, where it should not affect other parts of the system, and hopefully you can use printf without any issues.  

    The error codes you are referring to are defined in nrf_error.h

    4 corresponds to NRF_ERROR_NO_MEM, which most likely means the internal buffers are full, and 9 corresponds to NRF_ERROR_INVALID_LENGTH. 

    Best regards
    Torbjørn

Related