From 3def3ab00095467a05f5f0b05841d528a058327c Mon Sep 17 00:00:00 2001 From: Bill2462 Date: Sun, 10 Sep 2017 13:58:49 +0200 Subject: [PATCH] Add power control functions (#334) * Update MFRC522.h Add new power control functions : PCD_SoftPowerDown PCD_SoftPowerUp --- keywords.txt | 6 +++++- src/MFRC522.cpp | 29 +++++++++++++++++++++++++++++ src/MFRC522.h | 6 ++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/keywords.txt b/keywords.txt index 46363a7..c3c1405 100644 --- a/keywords.txt +++ b/keywords.txt @@ -43,6 +43,10 @@ PCD_GetAntennaGain KEYWORD2 PCD_SetAntennaGain KEYWORD2 PCD_PerformSelfTest KEYWORD2 +# Power control functions MFRC522 +PCD_SoftPowerDown KEYWORD2 +PCD_SoftPowerUp KEYWORD2 + # Functions for communicating with PICCs PCD_TransceiveData KEYWORD2 PCD_CommunicateWithPICC KEYWORD2 @@ -216,4 +220,4 @@ FIFO_SIZE LITERAL1 BITRATE_106KBITS LITERAL1 BITRATE_212KBITS LITERAL1 BITRATE_424KBITS LITERAL1 -BITRATE_848KBITS LITERAL1 \ No newline at end of file +BITRATE_848KBITS LITERAL1 diff --git a/src/MFRC522.cpp b/src/MFRC522.cpp index 95bf498..4287a2b 100644 --- a/src/MFRC522.cpp +++ b/src/MFRC522.cpp @@ -363,6 +363,35 @@ bool MFRC522::PCD_PerformSelfTest() { return true; } // End PCD_PerformSelfTest() +///////////////////////////////////////////////////////////////////////////////////// +// Power control +///////////////////////////////////////////////////////////////////////////////////// + +//IMPORTANT NOTE!!!! +//Calling any other function that uses CommandReg will disable soft power down mode !!! +//For more details about power control, refer to the datasheet - page 33 (8.6) + +void MFRC522::PCD_SoftPowerDown(){//Note : Only soft power down mode is available throught software + byte val = PCD_ReadRegister(CommandReg); // Read state of the command register + val |= (1<<4);// set PowerDown bit ( bit 4 ) to 1 + PCD_WriteRegister(CommandReg, val);//write new value to the command register +} + +void MFRC522::PCD_SoftPowerUp(){ + byte val = PCD_ReadRegister(CommandReg); // Read state of the command register + val &= ~(1<<4);// set PowerDown bit ( bit 4 ) to 0 + PCD_WriteRegister(CommandReg, val);//write new value to the command register + // wait until PowerDown bit is cleared (this indicates end of wake up procedure) + const uint32_t timeout = (uint32_t)millis() + 500;// create timer for timeout (just in case) + + while(millis()<=timeout){ // set timeout to 500 ms + val = PCD_ReadRegister(CommandReg);// Read state of the command register + if(!(val & (1<<4))){ // if powerdown bit is 0 + break;// wake up procedure is finished + } + } +} + ///////////////////////////////////////////////////////////////////////////////////// // Functions for communicating with PICCs ///////////////////////////////////////////////////////////////////////////////////// diff --git a/src/MFRC522.h b/src/MFRC522.h index a0e5f9f..6a3a54f 100644 --- a/src/MFRC522.h +++ b/src/MFRC522.h @@ -359,6 +359,12 @@ public: void PCD_SetAntennaGain(byte mask); bool PCD_PerformSelfTest(); + ///////////////////////////////////////////////////////////////////////////////////// + // Power control functions + ///////////////////////////////////////////////////////////////////////////////////// + void PCD_SoftPowerDown(); + void PCD_SoftPowerUp(); + ///////////////////////////////////////////////////////////////////////////////////// // Functions for communicating with PICCs /////////////////////////////////////////////////////////////////////////////////////