Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional task mutex #69

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.2.1"
version: "3.2.2"
description: "Library for communication with RFID / NFC cards using MFRC522 module"
url: "https://github.com/abobija/esp-idf-rc522"
repository: "https://github.com/abobija/esp-idf-rc522.git"
Expand Down
8 changes: 5 additions & 3 deletions include/rc522_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <esp_err.h>
#include <esp_event.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "rc522_driver.h"
#include "rc522_picc.h"

Expand Down Expand Up @@ -33,9 +34,10 @@ typedef struct rc522 *rc522_handle_t;
typedef struct
{
rc522_driver_handle_t driver;
uint16_t poll_interval_ms; /*<! Delay (in milliseconds) between polls */
size_t task_stack_size; /*<! Stack size of rc522 task */
uint8_t task_priority; /*<! Priority of rc522 task */
uint16_t poll_interval_ms; /*<! Delay (in milliseconds) between polls */
size_t task_stack_size; /*<! Stack size of rc522 task */
uint8_t task_priority; /*<! Priority of rc522 task */
SemaphoreHandle_t task_mutex; /*<! Mutex for rc522 task */
} rc522_config_t;

typedef enum
Expand Down
21 changes: 21 additions & 0 deletions src/rc522.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,21 @@ void rc522_task(void *arg)
const uint32_t task_delay_ms = 50;
const uint32_t picc_heartbeat_failure_threshold_ms = (2 * task_delay_ms);
uint32_t picc_heartbeat_failure_at_ms = 0;
bool mutex_taken = false;
const uint16_t mutex_take_timeout_ms = 4000;

xEventGroupClearBits(rc522->bits, RC522_TASK_STOPPED_BIT);

while (!rc522->exit_requested) {
if (mutex_taken && rc522->config->task_mutex != NULL) {
if (xSemaphoreGive(rc522->config->task_mutex) == pdTRUE) {
mutex_taken = false;
}
else {
RC522_LOGW("failed to give mutex");
}
}

if (rc522->state != RC522_STATE_POLLING) {
// waiting for state change to polling
rc522_delay_ms(100);
Expand All @@ -224,6 +235,16 @@ void rc522_task(void *arg)

rc522_delay_ms(task_delay_ms);

if (rc522->config->task_mutex != NULL) {
if (xSemaphoreTake(rc522->config->task_mutex, pdMS_TO_TICKS(mutex_take_timeout_ms)) == pdTRUE) {
mutex_taken = true;
}
else {
RC522_LOGW("failed to take mutex in %d ms", mutex_take_timeout_ms);
continue;
}
}

bool should_poll = (rc522_millis() - last_poll_ms) > rc522->config->poll_interval_ms;

if (rc522->picc.state == RC522_PICC_STATE_IDLE || rc522->picc.state == RC522_PICC_STATE_HALT) {
Expand Down