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

Update NFC content on-the-fly

I want update NFC payload on-the-fly.

My test firmware based on nRF5_SDK_14.2.0_17b948a\examples\nfc\record_text (unable to update SDK. Legacy part.)

At firmware start:

/* Set up NFC */
err_code = nfc_t2t_setup(nfc_callback, NULL);
APP_ERROR_CHECK(err_code);

/* Encode welcome message */
err_code = welcome_msg_encode(m_ndef_msg_buf, &len);
APP_ERROR_CHECK(err_code);

/* Set created message as the NFC payload */
err_code = nfc_t2t_payload_set(m_ndef_msg_buf, len);
APP_ERROR_CHECK(err_code);

/* Start sensing NFC field */
err_code = nfc_t2t_emulation_start();
APP_ERROR_CHECK(err_code);

I am try update NFC payload:

err_code = msg_encode(m_ndef_msg_buf, &len);
APP_ERROR_CHECK(err_code);

err_code = nfc_t2t_payload_set(m_ndef_msg_buf, len);
APP_ERROR_CHECK(err_code);

nfc_t2t_payload_set is return NRF_ERROR_INVALID_STATE

Ok. I will call nfc_t2t_emulation_stop before update payload

err_code = msg_encode(m_ndef_msg_buf, &len);
APP_ERROR_CHECK(err_code);

nfc_t2t_emulation_stop();

/* Set created message as the NFC payload */
err_code = nfc_t2t_payload_set(m_ndef_msg_buf, len);
APP_ERROR_CHECK(err_code);

err_code = nfc_t2t_emulation_start();
APP_ERROR_CHECK(err_code);

That's working.

BUT I have next issue:

When I call nfc_t2t_emulation_stop, nfc_t2t_payload_set and nfc_t2t_emulation_start while tag at field(after NFC_T2T_EVENT_FIELD_ON) something went wrong and NFC tag unavailable for reader forever.

Parents
  • Hi,

    Did you check the return code from call to nfc_t2t_emulation_stop? It may be that you cannot call these functions from interrupt context. Can you try to set a flag in the event handler, and do the payload update in the main context?

    Best regards,
    Jørgen

  • I have call nfc_t2t_emulation_stop from application context.

    main.c

    nfc_init("1", "2");
    nrf_delay_ms(5000);
    nfc_start("3");

    nfc.c

    static void nfc_callback(void * p_context, nfc_t2t_event_t event, const uint8_t * p_data, size_t data_length)
    {
        (void)p_context;
    
        switch (event)
        {
            case NFC_T2T_EVENT_FIELD_ON:
                NRF_LOG_DEBUG("NFC_T2T_EVENT_FIELD_ON");
                break;
            case NFC_T2T_EVENT_FIELD_OFF:
                NRF_LOG_DEBUG("NFC_T2T_EVENT_FIELD_OFF");
                break;
            case NFC_T2T_EVENT_DATA_READ:
    			NRF_LOG_DEBUG("NFC_T2T_EVENT_DATA_READ");
    			break;
            case NFC_T2T_EVENT_STOPPED:
    			NRF_LOG_DEBUG("NFC_T2T_EVENT_STOPPED");
    			break;
            default:
                break;
        }
    }
    
    void nfc_init(char *p_fw_ver, char *p_hw_rev)
    {
    	NRF_LOG_DEBUG("NFC init");
    
    	uint32_t  err_code;
    	uint32_t  len = sizeof(m_ndef_msg_buf);
    
    	err_code = nfc_t2t_setup(nfc_callback, NULL);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = msg_encode(m_ndef_msg_buf, &len);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nfc_t2t_payload_set(m_ndef_msg_buf, len);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nfc_t2t_emulation_start();
    	APP_ERROR_CHECK(err_code);
    }
    
    void nfc_start(char *test)
    {
    	NRF_LOG_DEBUG("NFC start");
    
    	uint32_t  err_code;
    	uint32_t  len = sizeof(m_ndef_msg_buf);
    
    	err_code = nfc_t2t_emulation_stop();
    	NRF_LOG_DEBUG("nfc_t2t_emulation_stop: 0x%x", err_code);
    
    	err_code = msg_encode(m_ndef_msg_buf, &len);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nfc_t2t_payload_set(m_ndef_msg_buf, len);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nfc_t2t_emulation_start();
    	APP_ERROR_CHECK(err_code);
    }

    logs with issue:

    00> [00000000] <debug> NFC: NFC init
    00> [00001374] <debug> NFC: NFC_T2T_EVENT_FIELD_ON
    00> [00001491] <debug> NFC: NFC_T2T_EVENT_DATA_READ
    00> [00005110] <debug> NFC: NFC start
    00> [00005110] <debug> NFC: nfc_t2t_emulation_stop: 0x0

    logs without issue:

    00> [00000000] <debug> NFC: NFC init
    00> [00003898] <debug> NFC: NFC_T2T_EVENT_FIELD_ON
    00> [00004017] <debug> NFC: NFC_T2T_EVENT_DATA_READ
    00> [00004402] <debug> NFC: NFC_T2T_EVENT_FIELD_OFF
    00> [00005014] <debug> NFC: NFC start
    00> [00005014] <debug> NFC: nfc_t2t_emulation_stop: 0x0
    00> [00009423] <debug> NFC: NFC_T2T_EVENT_FIELD_ON
    00> [00009541] <debug> NFC: NFC_T2T_EVENT_DATA_READ
    00> [00009806] <debug> NFC: NFC_T2T_EVENT_FIELD_OFF

    Issue: Unable to update NFC payload after NFC_T2T_EVENT_FIELD_ON event. Tag is unavailable for any readers.

Reply
  • I have call nfc_t2t_emulation_stop from application context.

    main.c

    nfc_init("1", "2");
    nrf_delay_ms(5000);
    nfc_start("3");

    nfc.c

    static void nfc_callback(void * p_context, nfc_t2t_event_t event, const uint8_t * p_data, size_t data_length)
    {
        (void)p_context;
    
        switch (event)
        {
            case NFC_T2T_EVENT_FIELD_ON:
                NRF_LOG_DEBUG("NFC_T2T_EVENT_FIELD_ON");
                break;
            case NFC_T2T_EVENT_FIELD_OFF:
                NRF_LOG_DEBUG("NFC_T2T_EVENT_FIELD_OFF");
                break;
            case NFC_T2T_EVENT_DATA_READ:
    			NRF_LOG_DEBUG("NFC_T2T_EVENT_DATA_READ");
    			break;
            case NFC_T2T_EVENT_STOPPED:
    			NRF_LOG_DEBUG("NFC_T2T_EVENT_STOPPED");
    			break;
            default:
                break;
        }
    }
    
    void nfc_init(char *p_fw_ver, char *p_hw_rev)
    {
    	NRF_LOG_DEBUG("NFC init");
    
    	uint32_t  err_code;
    	uint32_t  len = sizeof(m_ndef_msg_buf);
    
    	err_code = nfc_t2t_setup(nfc_callback, NULL);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = msg_encode(m_ndef_msg_buf, &len);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nfc_t2t_payload_set(m_ndef_msg_buf, len);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nfc_t2t_emulation_start();
    	APP_ERROR_CHECK(err_code);
    }
    
    void nfc_start(char *test)
    {
    	NRF_LOG_DEBUG("NFC start");
    
    	uint32_t  err_code;
    	uint32_t  len = sizeof(m_ndef_msg_buf);
    
    	err_code = nfc_t2t_emulation_stop();
    	NRF_LOG_DEBUG("nfc_t2t_emulation_stop: 0x%x", err_code);
    
    	err_code = msg_encode(m_ndef_msg_buf, &len);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nfc_t2t_payload_set(m_ndef_msg_buf, len);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nfc_t2t_emulation_start();
    	APP_ERROR_CHECK(err_code);
    }

    logs with issue:

    00> [00000000] <debug> NFC: NFC init
    00> [00001374] <debug> NFC: NFC_T2T_EVENT_FIELD_ON
    00> [00001491] <debug> NFC: NFC_T2T_EVENT_DATA_READ
    00> [00005110] <debug> NFC: NFC start
    00> [00005110] <debug> NFC: nfc_t2t_emulation_stop: 0x0

    logs without issue:

    00> [00000000] <debug> NFC: NFC init
    00> [00003898] <debug> NFC: NFC_T2T_EVENT_FIELD_ON
    00> [00004017] <debug> NFC: NFC_T2T_EVENT_DATA_READ
    00> [00004402] <debug> NFC: NFC_T2T_EVENT_FIELD_OFF
    00> [00005014] <debug> NFC: NFC start
    00> [00005014] <debug> NFC: nfc_t2t_emulation_stop: 0x0
    00> [00009423] <debug> NFC: NFC_T2T_EVENT_FIELD_ON
    00> [00009541] <debug> NFC: NFC_T2T_EVENT_DATA_READ
    00> [00009806] <debug> NFC: NFC_T2T_EVENT_FIELD_OFF

    Issue: Unable to update NFC payload after NFC_T2T_EVENT_FIELD_ON event. Tag is unavailable for any readers.

Children
No Data
Related