4444#include "init.h"
4545#include "bluetooth_core.h"
4646#include "pin_interrupt.h"
47- #include "nrf_nfc_handler.h"
47+ #include "nfc.h"
48+ #include "nfc_t2t_lib.h"
4849
4950#include "ruuvi_endpoints.h"
5051
@@ -132,10 +133,41 @@ ret_code_t button_press_handler(const ruuvi_standard_message_t message)
132133/**
133134 * Callback for NFC event
134135 */
135- ret_code_t nfc_detected_handler (const ruuvi_standard_message_t message )
136+ static void nfc_detected_handler (void * data , uint16_t length )
136137{
137138 nrf_ble_es_on_start_connectable_advertising ();
138- return NRF_SUCCESS ;
139+ }
140+
141+ /**
142+ * Work around NFC data corruption bug by reinitializing NFC data after field has been lost.
143+ * Call this function outside of interrupt context.
144+ */
145+ static void reinit_nfc (void * data , uint16_t length )
146+ {
147+ init_nfc ();
148+ }
149+
150+ /**@brief Function for handling NFC events.
151+ * Schedulers call to handler.
152+ */
153+ void app_nfc_callback (void * p_context , nfc_t2t_event_t event , const uint8_t * p_data , size_t data_length )
154+ {
155+ NRF_LOG_INFO ("NFC\r\n" );
156+ switch (event )
157+ {
158+ case NFC_T2T_EVENT_FIELD_ON :
159+ NRF_LOG_INFO ("NFC Field detected \r\n" );
160+ app_sched_event_put (NULL , 0 , nfc_detected_handler );
161+ break ;
162+ case NFC_T2T_EVENT_FIELD_OFF :
163+ NRF_LOG_INFO ("NFC Field lost \r\n" );
164+ app_sched_event_put (NULL , 0 , reinit_nfc );
165+ break ;
166+ case NFC_T2T_EVENT_DATA_READ :
167+ NRF_LOG_INFO ("Data read\r\n" );
168+ default :
169+ break ;
170+ }
139171}
140172
141173/**
@@ -155,9 +187,8 @@ int main(void)
155187
156188 NRF_LOG_DEBUG ("BLE init status: %d\r\n" , err_code );
157189
158- //init_handler must be called before init_nfc, as init_nfc passes function pointer set by init_handler to NFC stack
159- nfc_init_handler ();
160- nfc_connected_handler_set (nfc_detected_handler );
190+ //setup callback before initializing NFC.
191+ set_nfc_callback (app_nfc_callback );
161192 err_code |= init_nfc ();
162193
163194
0 commit comments