diff --git a/platformio.ini b/platformio.ini index fc5ee5d..e7f9658 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,7 +31,8 @@ lib_deps = https://github.com/tuniii/LogRingBuffer.git#89d7d3e https://github.com/tueddy/PN5180-Library.git#0353104 platform_packages = - platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#1.0.6 + platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#1.0.6 + ;platformio/framework-arduinoespressif32 @ https://github.com/tuniii/arduino-esp32-v1.0.6-wt#v1.0.6-patched [env:esp32-a1s] diff --git a/src/AudioPlayer.cpp b/src/AudioPlayer.cpp index 3b15c3f..fc8498b 100644 --- a/src/AudioPlayer.cpp +++ b/src/AudioPlayer.cpp @@ -687,7 +687,8 @@ void AudioPlayer_Task(void *parameter) { } } - esp_task_wdt_reset(); // Don't forget to feed the dog! + vTaskDelay(portTICK_PERIOD_MS * 3); + //esp_task_wdt_reset(); // Don't forget to feed the dog! } vTaskDelete(NULL); } diff --git a/src/Cmd.cpp b/src/Cmd.cpp index f55ecf6..6844f3f 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -328,6 +328,13 @@ void Cmd_Action(const uint16_t mod) { break; } + #ifdef ENABLE_ESPUINO_DEBUG + case PRINT_TASK_STATS: { + System_esp_print_tasks(); + break; + } + #endif + default: { snprintf(Log_Buffer, Log_BufferLength, "%s %d !", (char *) FPSTR(modificatorDoesNotExist), mod); Log_Println(Log_Buffer, LOGLEVEL_ERROR); diff --git a/src/Led.cpp b/src/Led.cpp index 46ffeec..cecf8ef 100644 --- a/src/Led.cpp +++ b/src/Led.cpp @@ -64,7 +64,7 @@ void Led_Init(void) { xTaskCreatePinnedToCore( Led_Task, /* Function to implement the task */ "Led_Task", /* Name of the task */ - 2000, /* Stack size in words */ + 1512, /* Stack size in words */ NULL, /* Task input parameter */ 1, /* Priority of the task */ NULL, /* Task handle. */ @@ -191,7 +191,6 @@ static void Led_Task(void *parameter) { FastLED.show(); showEvenError = !showEvenError; vTaskDelay(portTICK_RATE_MS * 500); - esp_task_wdt_reset(); continue; } @@ -203,6 +202,8 @@ static void Led_Task(void *parameter) { // Multi-LED: growing red as long button for sleepmode is pressed. // Single-LED: red when pressed and flashing red when long interval-duration is reached if (gShutdownButton < (sizeof(gButtons) / sizeof(gButtons[0])) - 1) { // Only show animation, if CMD_SLEEPMODE was assigned to BUTTON_n_LONG + button is pressed + //snprintf(Log_Buffer, Log_BufferLength, "%u", uxTaskGetStackHighWaterMark(NULL)); + //Log_Println(Log_Buffer, LOGLEVEL_DEBUG); if (!gButtons[gShutdownButton].currentState && (millis() - gButtons[gShutdownButton].firstPressedTimestamp >= 150) && gButtonInitComplete) { if (NUM_LEDS == 1) { FastLED.clear(); @@ -636,8 +637,8 @@ static void Led_Task(void *parameter) { vTaskDelay(portTICK_RATE_MS * 5); } } - //vTaskDelay(portTICK_RATE_MS * 10); - esp_task_wdt_reset(); + vTaskDelay(portTICK_RATE_MS * 1); + //esp_task_wdt_reset(); } vTaskDelete(NULL); #endif diff --git a/src/RfidMfrc522.cpp b/src/RfidMfrc522.cpp index c07980c..8de9d7f 100644 --- a/src/RfidMfrc522.cpp +++ b/src/RfidMfrc522.cpp @@ -60,9 +60,9 @@ for (;;) { if (RFID_SCAN_INTERVAL/2 >= 20) { - vTaskDelay(RFID_SCAN_INTERVAL/2); + vTaskDelay(portTICK_RATE_MS * (RFID_SCAN_INTERVAL/2)); } else { - vTaskDelay(20); + vTaskDelay(portTICK_RATE_MS * 20); } byte cardId[cardIdSize]; String cardIdString; @@ -133,9 +133,9 @@ // https://github.com/miguelbalboa/rfid/issues/188; voodoo! :-) while (true) { if (RFID_SCAN_INTERVAL/2 >= 20) { - vTaskDelay(RFID_SCAN_INTERVAL/2); + vTaskDelay(portTICK_RATE_MS * (RFID_SCAN_INTERVAL/2)); } else { - vTaskDelay(20); + vTaskDelay(portTICK_RATE_MS * 20); } control=0; for (uint8_t i=0u; i<3; i++) { diff --git a/src/RfidPn5180.cpp b/src/RfidPn5180.cpp index 3bc1cd4..af9ebaa 100644 --- a/src/RfidPn5180.cpp +++ b/src/RfidPn5180.cpp @@ -8,6 +8,7 @@ #include "Queues.h" #include "System.h" #include "Port.h" +#include #include "AudioPlayer.h" #ifdef RFID_READER_TYPE_PN5180 @@ -32,7 +33,7 @@ extern unsigned long Rfid_LastRfidCheckTimestamp; #ifdef RFID_READER_TYPE_PN5180 - static void Rfid_Read(void); + static void Rfid_Task(void *parameter); void Rfid_Init(void) { #ifdef PN5180_ENABLE_LPCD @@ -42,184 +43,203 @@ extern unsigned long Rfid_LastRfidCheckTimestamp; gpio_hold_dis(gpio_num_t(RFID_RST)); // RST pinMode(RFID_IRQ, INPUT); #endif + + xTaskCreatePinnedToCore( + Rfid_Task, /* Function to implement the task */ + "rfid", /* Name of the task */ + 1536, /* Stack size in words */ + NULL, /* Task input parameter */ + 2 | portPRIVILEGE_BIT, /* Priority of the task */ + NULL, /* Task handle. */ + 0 /* Core where the task should run */ + ); } void Rfid_Cyclic(void) { - Rfid_Read(); - vTaskDelay(5u); + // Not necessary as cyclic stuff performed by task Rfid_Task() } - void Rfid_Read(void) { + void Rfid_Task(void *parameter) { static PN5180ISO14443 nfc14443(RFID_CS, RFID_BUSY, RFID_RST); static PN5180ISO15693 nfc15693(RFID_CS, RFID_BUSY, RFID_RST); - static uint32_t lastTimeDetected14443 = 0; - static uint32_t lastTimeDetected15693 = 0; + uint32_t lastTimeDetected14443 = 0; + uint32_t lastTimeDetected15693 = 0; #ifdef PAUSE_WHEN_RFID_REMOVED - static byte lastValidcardId[cardIdSize]; - static bool cardAppliedCurrentRun = false; - static bool cardAppliedLastRun = false; - bool sameCardReapplied = false; + byte lastValidcardId[cardIdSize]; + bool cardAppliedCurrentRun = false; + bool cardAppliedLastRun = false; #endif - static uint8_t stateMachine = RFID_PN5180_STATE_INIT; + uint8_t stateMachine = RFID_PN5180_STATE_INIT; static byte cardId[cardIdSize], lastCardId[cardIdSize]; uint8_t uid[10]; - String cardIdString; - bool cardReceived = false; - if (RFID_PN5180_STATE_INIT == stateMachine) { - nfc14443.begin(); - nfc14443.reset(); - // show PN5180 reader version - uint8_t firmwareVersion[2]; - nfc14443.readEEprom(FIRMWARE_VERSION, firmwareVersion, sizeof(firmwareVersion)); - Serial.print(F("Firmware version=")); - Serial.print(firmwareVersion[1]); - Serial.print("."); - Serial.println(firmwareVersion[0]); + for (;;) { + vTaskDelay(portTICK_RATE_MS * 10u); + String cardIdString; + bool cardReceived = false; + #ifdef PAUSE_WHEN_RFID_REMOVED + bool sameCardReapplied = false; + #endif - // activate RF field - delay(4u); - Log_Println((char *) FPSTR(rfidScannerReady), LOGLEVEL_DEBUG); + if (RFID_PN5180_STATE_INIT == stateMachine) { + nfc14443.begin(); + nfc14443.reset(); + // show PN5180 reader version + uint8_t firmwareVersion[2]; + nfc14443.readEEprom(FIRMWARE_VERSION, firmwareVersion, sizeof(firmwareVersion)); + Serial.print(F("Firmware version=")); + Serial.print(firmwareVersion[1]); + Serial.print("."); + Serial.println(firmwareVersion[0]); - // 1. check for an ISO-14443 card - } else if (RFID_PN5180_NFC14443_STATE_RESET == stateMachine) { - nfc14443.reset(); - } else if (RFID_PN5180_NFC14443_STATE_SETUPRF == stateMachine) { - nfc14443.setupRF(); - } else if (RFID_PN5180_NFC14443_STATE_READCARD == stateMachine) { - if (nfc14443.readCardSerial(uid) >= 4u) { - cardReceived = true; - stateMachine = RFID_PN5180_NFC14443_STATE_ACTIVE; - lastTimeDetected14443 = millis(); - #ifdef PAUSE_WHEN_RFID_REMOVED - cardAppliedCurrentRun = true; - #endif - } else { - // Reset to dummy-value if no card is there - // Necessary to differentiate between "card is still applied" and "card is re-applied again after removal" - // lastTimeDetected14443 is used to prevent "new card detection with old card" with single events where no card was detected - if (!lastTimeDetected14443 || (millis() - lastTimeDetected14443 >= 400)) { - lastTimeDetected14443 = 0; + // activate RF field + delay(4u); + Log_Println((char *) FPSTR(rfidScannerReady), LOGLEVEL_DEBUG); + + // 1. check for an ISO-14443 card + } else if (RFID_PN5180_NFC14443_STATE_RESET == stateMachine) { + nfc14443.reset(); + //snprintf(Log_Buffer, Log_BufferLength, "%u", uxTaskGetStackHighWaterMark(NULL)); + //Log_Println(Log_Buffer, LOGLEVEL_DEBUG); + } else if (RFID_PN5180_NFC14443_STATE_SETUPRF == stateMachine) { + nfc14443.setupRF(); + } else if (RFID_PN5180_NFC14443_STATE_READCARD == stateMachine) { + if (nfc14443.readCardSerial(uid) >= 4u) { + cardReceived = true; + stateMachine = RFID_PN5180_NFC14443_STATE_ACTIVE; + lastTimeDetected14443 = millis(); #ifdef PAUSE_WHEN_RFID_REMOVED - cardAppliedCurrentRun = false; + cardAppliedCurrentRun = true; #endif - for (uint8_t i=0; i= 400)) { + lastTimeDetected14443 = 0; + #ifdef PAUSE_WHEN_RFID_REMOVED + cardAppliedCurrentRun = false; + #endif + for (uint8_t i=0; i= 400)) { - lastTimeDetected15693 = 0; + // 2. check for an ISO-15693 card + } else if (RFID_PN5180_NFC15693_STATE_RESET == stateMachine) { + nfc15693.reset(); + } else if (RFID_PN5180_NFC15693_STATE_SETUPRF == stateMachine) { + nfc15693.setupRF(); + } else if (RFID_PN5180_NFC15693_STATE_DISABLEPRIVACYMODE == stateMachine) { + // check for ICODE-SLIX2 password protected tag + // put your privacy password here, e.g.: + // https://de.ifixit.com/Antworten/Ansehen/513422/nfc+Chips+f%C3%BCr+tonies+kaufen + uint8_t password[] = {0x01, 0x02, 0x03, 0x04}; + ISO15693ErrorCode myrc = nfc15693.disablePrivacyMode(password); + if (ISO15693_EC_OK == myrc) { + Serial.println(F("disabling privacy-mode successful")); + } + } else if (RFID_PN5180_NFC15693_STATE_GETINVENTORY == stateMachine) { + // try to read ISO15693 inventory + ISO15693ErrorCode rc = nfc15693.getInventory(uid); + if (rc == ISO15693_EC_OK) { + cardReceived = true; + stateMachine = RFID_PN5180_NFC15693_STATE_ACTIVE; + lastTimeDetected15693 = millis(); #ifdef PAUSE_WHEN_RFID_REMOVED - cardAppliedCurrentRun = false; + cardAppliedCurrentRun = true; #endif - for (uint8_t i=0; i= 400)) { + lastTimeDetected15693 = 0; + #ifdef PAUSE_WHEN_RFID_REMOVED + cardAppliedCurrentRun = false; + #endif + for (uint8_t i=0; i pause - AudioPlayer_TrackControlToQueueSender(PAUSEPLAY); - } - cardAppliedLastRun = cardAppliedCurrentRun; - #endif - - // send card to queue - if (cardReceived) { - memcpy(cardId, uid, cardIdSize); - // check for different card id - if (memcmp((const void *)cardId, (const void *)lastCardId, sizeof(cardId)) == 0) { - // reset state machine - if (RFID_PN5180_NFC14443_STATE_ACTIVE == stateMachine) { - stateMachine = RFID_PN5180_NFC14443_STATE_RESET; - return; - } else if (RFID_PN5180_NFC15693_STATE_ACTIVE == stateMachine) { - stateMachine = RFID_PN5180_NFC15693_STATE_RESET; - return; + #ifdef PAUSE_WHEN_RFID_REMOVED + if (!cardAppliedCurrentRun && cardAppliedLastRun && !gPlayProperties.pausePlay && System_GetOperationMode() != OPMODE_BLUETOOTH) { // Card removed => pause + AudioPlayer_TrackControlToQueueSender(PAUSEPLAY); + Log_Println((char *) FPSTR(rfidTagRemoved), LOGLEVEL_NOTICE); } - } + cardAppliedLastRun = cardAppliedCurrentRun; + #endif - memcpy(lastCardId, cardId, cardIdSize); + // send card to queue + if (cardReceived) { + memcpy(cardId, uid, cardIdSize); - #ifdef PAUSE_WHEN_RFID_REMOVED - if (memcmp((const void *)lastValidcardId, (const void *)cardId, sizeof(cardId)) == 0) { - sameCardReapplied = true; + // check for different card id + if (memcmp((const void *)cardId, (const void *)lastCardId, sizeof(cardId)) == 0) { + // reset state machine + if (RFID_PN5180_NFC14443_STATE_ACTIVE == stateMachine) { + stateMachine = RFID_PN5180_NFC14443_STATE_RESET; + continue; + } else if (RFID_PN5180_NFC15693_STATE_ACTIVE == stateMachine) { + stateMachine = RFID_PN5180_NFC15693_STATE_RESET; + continue; + } } - #endif - Log_Print((char *) FPSTR(rfidTagDetected), LOGLEVEL_NOTICE); - snprintf(Log_Buffer, Log_BufferLength, "(%s) ID: ", (RFID_PN5180_NFC14443_STATE_ACTIVE == stateMachine) ? "ISO-14443" : "ISO-15693"); - Log_Print(Log_Buffer, LOGLEVEL_NOTICE); + memcpy(lastCardId, cardId, cardIdSize); + + #ifdef PAUSE_WHEN_RFID_REMOVED + if (memcmp((const void *)lastValidcardId, (const void *)cardId, sizeof(cardId)) == 0) { + sameCardReapplied = true; + } + #endif - for (uint8_t i = 0u; i < cardIdSize; i++) { - snprintf(Log_Buffer, Log_BufferLength, "%02x%s", cardId[i], (i < cardIdSize - 1u) ? "-" : "\n"); + Log_Print((char *) FPSTR(rfidTagDetected), LOGLEVEL_NOTICE); + snprintf(Log_Buffer, Log_BufferLength, "(%s) ID: ", (RFID_PN5180_NFC14443_STATE_ACTIVE == stateMachine) ? "ISO-14443" : "ISO-15693"); Log_Print(Log_Buffer, LOGLEVEL_NOTICE); - } - for (uint8_t i = 0u; i < cardIdSize; i++) { - char num[4]; - snprintf(num, sizeof(num), "%03d", cardId[i]); - cardIdString += num; - } + for (uint8_t i = 0u; i < cardIdSize; i++) { + snprintf(Log_Buffer, Log_BufferLength, "%02x%s", cardId[i], (i < cardIdSize - 1u) ? "-" : "\n"); + Log_Print(Log_Buffer, LOGLEVEL_NOTICE); + } - #ifdef PAUSE_WHEN_RFID_REMOVED - if (!sameCardReapplied) { // Don't allow to send card to queue if it's the same card again... - xQueueSend(gRfidCardQueue, cardIdString.c_str(), 0); - } else { - // If pause-button was pressed while card was not applied, playback could be active. If so: don't pause when card is reapplied again as the desired functionality would be reversed in this case. - if (gPlayProperties.pausePlay) { - AudioPlayer_TrackControlToQueueSender(PAUSEPLAY); // ... play/pause instead - } + for (uint8_t i = 0u; i < cardIdSize; i++) { + char num[4]; + snprintf(num, sizeof(num), "%03d", cardId[i]); + cardIdString += num; } - memcpy(lastValidcardId, uid, cardIdSize); - #else - xQueueSend(gRfidCardQueue, cardIdString.c_str(), 0); // If PAUSE_WHEN_RFID_REMOVED isn't active, every card-apply leads to new playlist-generation - #endif - } - if (stateMachine == RFID_PN5180_NFC14443_STATE_ACTIVE) { // If 14443 is active, bypass 15693 as next check (performance) - stateMachine = RFID_PN5180_NFC14443_STATE_RESET; - } else if (stateMachine == RFID_PN5180_NFC15693_STATE_ACTIVE) { // If 15693 is active, bypass 14443 as next check (performance) - stateMachine = RFID_PN5180_NFC15693_STATE_RESET; - } else { - stateMachine++; - if (stateMachine > RFID_PN5180_NFC15693_STATE_GETINVENTORY) { + #ifdef PAUSE_WHEN_RFID_REMOVED + if (!sameCardReapplied) { // Don't allow to send card to queue if it's the same card again... + xQueueSend(gRfidCardQueue, cardIdString.c_str(), 0); + } else { + // If pause-button was pressed while card was not applied, playback could be active. If so: don't pause when card is reapplied again as the desired functionality would be reversed in this case. + if (gPlayProperties.pausePlay && System_GetOperationMode() != OPMODE_BLUETOOTH) { + AudioPlayer_TrackControlToQueueSender(PAUSEPLAY); // ... play/pause instead + Log_Println((char *) FPSTR(rfidTagReapplied), LOGLEVEL_NOTICE); + } + } + memcpy(lastValidcardId, uid, cardIdSize); + #else + xQueueSend(gRfidCardQueue, cardIdString.c_str(), 0); // If PAUSE_WHEN_RFID_REMOVED isn't active, every card-apply leads to new playlist-generation + #endif + } + + if (RFID_PN5180_NFC14443_STATE_ACTIVE == stateMachine) { // If 14443 is active, bypass 15693 as next check (performance) stateMachine = RFID_PN5180_NFC14443_STATE_RESET; + } else if (RFID_PN5180_NFC15693_STATE_ACTIVE == stateMachine) { // If 15693 is active, bypass 14443 as next check (performance) + stateMachine = RFID_PN5180_NFC15693_STATE_RESET; + } else { + stateMachine++; + if (stateMachine > RFID_PN5180_NFC15693_STATE_GETINVENTORY) { + stateMachine = RFID_PN5180_NFC14443_STATE_RESET; + } } } } @@ -292,5 +312,4 @@ extern unsigned long Rfid_LastRfidCheckTimestamp; } #endif } - #endif diff --git a/src/System.cpp b/src/System.cpp index b04e556..03082bf 100644 --- a/src/System.cpp +++ b/src/System.cpp @@ -8,6 +8,9 @@ #include "Mqtt.h" #include "SdCard.h" #include "Port.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_system.h" constexpr const char prefsRfidNamespace[] PROGMEM = "rfidTags"; // Namespace used to save IDs of rfid-tags constexpr const char prefsSettingsNamespace[] PROGMEM = "settings"; // Namespace used for generic settings @@ -226,4 +229,13 @@ void System_ShowUpgradeWarning(void) { gPrefsSettings.putUInt("wcountrefact", ++nvsShowUpgradeWarningCount); Log_Println((char *) FPSTR(warningRefactoring), LOGLEVEL_ERROR); } -} \ No newline at end of file +} + +#ifdef ENABLE_ESPUINO_DEBUG + void System_esp_print_tasks(void) { + char *pbuffer = (char *)calloc(2048, 1); + vTaskGetRunTimeStats(pbuffer); + Serial.printf("=====\n%s\n=====", pbuffer); + free(pbuffer); + } +#endif \ No newline at end of file diff --git a/src/System.h b/src/System.h index ff8e3b6..dd8d497 100644 --- a/src/System.h +++ b/src/System.h @@ -24,3 +24,4 @@ void System_SetOperationMode(uint8_t opMode); uint8_t System_GetOperationMode(void); uint8_t System_GetOperationModeFromNvs(void); void System_ShowUpgradeWarning(void); +void System_esp_print_tasks(void); \ No newline at end of file diff --git a/src/Web.cpp b/src/Web.cpp index d1bb8bf..7abe1e7 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -609,13 +609,14 @@ void explorerHandleFileUpload(AsyncWebServerRequest *request, String filename, s } // Create Task for handling the storage of the data - xTaskCreate( + xTaskCreatePinnedToCore( explorerHandleFileStorageTask, /* Function to implement the task */ "fileStorageTask", /* Name of the task */ 4000, /* Stack size in words */ filePath, /* Task input parameter */ 2 | portPRIVILEGE_BIT, /* Priority of the task */ - &fileStorageTaskHandle /* Task handle. */ + &fileStorageTaskHandle, /* Task handle. */ + 1 /* Core where the task should run */ ); } @@ -674,7 +675,7 @@ void explorerHandleFileStorageTask(void *parameter) { // done exit loop to terminate break; } - vTaskDelay(portTICK_PERIOD_MS * 100); + vTaskDelay(portTICK_PERIOD_MS * 20); } #ifdef SD_MMC_1BIT_MODE vTaskDelay(portTICK_PERIOD_MS * 1); @@ -737,7 +738,12 @@ void explorerHandleListRequest(AsyncWebServerRequest *request) { } file = root.openNextFile(); - esp_task_wdt_reset(); + // If playback is active this can (at least sometimes) prevent scattering + if (!gPlayProperties.pausePlay) { + vTaskDelay(portTICK_PERIOD_MS * 5); + } else { + vTaskDelay(portTICK_PERIOD_MS * 1); + } } serializeJson(obj, serializedJsonString); diff --git a/src/main.cpp b/src/main.cpp index 163532e..103ab47 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -273,6 +273,5 @@ void loop() { #endif IrReceiver_Cyclic(); - - vTaskDelay(5u); + vTaskDelay(portTICK_RATE_MS * 5u); } diff --git a/src/revision.h b/src/revision.h index 22b5b45..8d6ac6b 100644 --- a/src/revision.h +++ b/src/revision.h @@ -1,4 +1,4 @@ #ifndef __REVISION_H__ #define __REVISION_H__ - constexpr const char softwareRevision[] PROGMEM = "Software-revision: 20210930-1"; + constexpr const char softwareRevision[] PROGMEM = "Software-revision: 20211002-1"; #endif \ No newline at end of file diff --git a/src/settings.h b/src/settings.h index f2b52bd..5e8c798 100644 --- a/src/settings.h +++ b/src/settings.h @@ -238,4 +238,6 @@ #include "settings-custom.h" // Contains all user-relevant settings custom-board #endif + //#define ENABLE_ESPUINO_DEBUG // Needs modification of platformio.ini (https://forum.espuino.de/t/rfid-mit-oder-ohne-task/353/21); better don't enable unless you know what you're doing :-) + #endif diff --git a/src/values.h b/src/values.h index 9eeaa86..e31d44a 100644 --- a/src/values.h +++ b/src/values.h @@ -74,4 +74,6 @@ #define DE 1 #define EN 2 + // Debug + #define PRINT_TASK_STATS 900 // Prints task stats (only debugging; needs modification of platformio.ini (https://forum.espuino.de/t/rfid-mit-oder-ohne-task/353/21)) #endif \ No newline at end of file