Skip to content

Commit

Permalink
nfc: Fix NFC IRQ context switching
Browse files Browse the repository at this point in the history
Fixed issue where NFC events data could be missed in
case when NFC interrupts would be executed faster than
the NFC workqueue or the NFC thread.

NCSDK-22681

Signed-off-by: Kamil Gawor <Kamil.Gawor@nordicsemi.no>
  • Loading branch information
KAGA164 authored and rlubos committed Aug 4, 2023
1 parent f9c54d7 commit 117a3ed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ Libraries for networking
Libraries for NFC
-----------------

|no_changes_yet_note|
* Fixed the potential issue where the NFC interrupt context switching could loose interrupts data.
This could happen if interrupts would be executed much faster than the NFC workqueue or thread.

Nordic Security Module
----------------------
Expand Down
22 changes: 21 additions & 1 deletion subsys/nfc/lib/platform_internal_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,30 @@ struct nfc_item_header {
};

#ifdef CONFIG_NFC_OWN_THREAD
static K_SEM_DEFINE(cb_sem, 0, 1);
/* By using a counting semaphore, NFC events interrupt can go faster than the processing thread.
* All events must be processed.
*/
static K_SEM_DEFINE(cb_sem, 0, K_SEM_MAX_LIMIT);
#endif /* CONFIG_NFC_OWN_THREAD */

static bool buf_full;
static nfc_lib_cb_resolve_t nfc_cb_resolve;
RING_BUF_DECLARE(nfc_cb_ring, CONFIG_NFC_RING_SIZE);

static void work_resubmit(struct k_work *work, struct ring_buf *buf)
{
if (!IS_ENABLED(CONFIG_NFC_OWN_THREAD)) {
if (!ring_buf_is_empty(buf)) {
int ret = k_work_submit(work);
/* In this case, work can be scheduled either from IRQ or to the queue
* running work items during the run.
*/
__ASSERT_NO_MSG(((ret == 0) || (ret == 2)));
ARG_UNUSED(ret);
}
}
}

static int ring_buf_get_data(struct ring_buf *buf, uint8_t **data, uint32_t size, bool *is_alloc)
{
uint32_t tmp;
Expand Down Expand Up @@ -120,6 +137,7 @@ static void cb_work(struct k_work *work)
nfc_cb_resolve(ctx, data);

if (header.data_size == 0) {
work_resubmit(work, &nfc_cb_ring);
return;
}

Expand All @@ -133,6 +151,8 @@ static void cb_work(struct k_work *work)
}
}

work_resubmit(work, &nfc_cb_ring);

return;

error:
Expand Down

0 comments on commit 117a3ed

Please sign in to comment.