@@ -360,6 +360,45 @@ void BoxRFID::sendRawSPI(uint8_t* buffer, uint8_t length, bool continuedSend) {
360360
361361 spiDisable ();
362362}
363+ BoxRFID::ISO15693_RESULT BoxRFID::ISO15693_readSingleBlock (uint8_t blockId, uint8_t * blockData) {
364+ uint8_t offset = 0 ;
365+
366+ trfBuffer[offset++] = 0x02 ; // ISO15693 flags - ISO15693_REQ_DATARATE_HIGH
367+ trfBuffer[offset++] = 0x20 ; // Read Single BLock
368+ /*
369+ bool withUid = true;
370+ if (withUid) {
371+ trfBuffer[0] = trfBuffer[0] || 0x20 || 0x10; // ISO15693_REQ_DATARATE_HIGH || ISO15693_REQ_ADDRESS || ISO15693_REQ_OPTION
372+ for (uint8_t i= 0; i<8; i++) {
373+ trfBuffer[offset++] = tagUid[i];
374+ }
375+ }*/
376+ trfBuffer[offset++] = blockId; // BlockId
377+
378+ trfStatus = sendDataTag (&trfBuffer[0 ], offset);
379+ if (trfStatus == TRF_STATUS::RX_COMPLETE) { // If data has been received
380+ if (trfBuffer[0 ] == 0x00 ) { // Confirm "no error" in response flags byte
381+ if (trfRxLength == 5 ) {
382+ // data Starts at the 2rd received byte, length = 4
383+ for (uint8_t i=0 ; i<4 ; i++) {
384+ blockData[i] = trfBuffer[i+1 ];
385+ }
386+ return ISO15693_RESULT::READ_SINGLE_BLOCK_VALID_RESPONSE;
387+ } else {
388+ Log.error (" Received invalid answer. Length should be %i but is %i" , 5 , trfRxLength);
389+ for (uint8_t i=0 ; i<trfRxLength; i++) {
390+ Log.printf (" %x" , trfBuffer[i]);
391+ }
392+ Log.println ();
393+ }
394+ } else {
395+ Log.error (" Error flag=%X while reading" , trfStatus);
396+ }
397+ } else {
398+ Log.error (" Unexpected TRF_STATUS=%X for read single block with id %i" , trfStatus, blockId);
399+ }
400+ return ISO15693_RESULT::READ_SINGLE_BLOCK_INVALID_RESPONSE; // TODO
401+ }
363402
364403BoxRFID::ISO15693_RESULT BoxRFID::ISO15693_sendSingleSlotInventory (uint8_t * uid) {
365404 uint8_t g_ui8TagDetectedCount;
@@ -637,6 +676,9 @@ void BoxRFID::initRFID() {
637676}
638677
639678BoxRFID::TRF_STATUS BoxRFID::sendDataTag (uint8_t *sendBuffer, uint8_t sendLen) {
679+ sendDataTag (sendBuffer, sendLen, 15 , 15 ); // 15, 5 vs. 15, 15 (longer timeout for set password)
680+ }
681+ BoxRFID::TRF_STATUS BoxRFID::sendDataTag (uint8_t *sendBuffer, uint8_t sendLen, uint8_t txTimeout, uint8_t rxTimeout) {
640682 uint8_t buffer[sendLen+5 ];
641683 memcpy (&buffer[5 ], sendBuffer, sendLen);
642684
@@ -656,7 +698,7 @@ BoxRFID::TRF_STATUS BoxRFID::sendDataTag(uint8_t *sendBuffer, uint8_t sendLen) {
656698 Log.println();*/
657699
658700 sendRaw (&buffer[0 ], sendLen+5 );
659- TRF_STATUS status = waitRxData (15 , 15 ); // 15, 5 vs. 15, 15 (longer timeout for set password)
701+ TRF_STATUS status = waitRxData (txTimeout, rxTimeout);
660702 return status;
661703}
662704
@@ -673,4 +715,84 @@ void BoxRFID::logUID() {
673715 uint8_t uid[24 ];
674716 getUID (uid);
675717 Log.info (" RFID UID: %s" , uid);
718+ }
719+
720+ uint8_t BoxRFID::readBlocks (uint8_t * data, uint8_t maxBytes) {
721+ BoxRFID::ISO15693_RESULT result;
722+ uint8_t bytesRead = 0 ;
723+
724+ resetRFID ();
725+ initRFID ();
726+ writeRegister (REGISTER::CHIP_STATUS_CONTROL, 0b00100001 ); // turnRfOn();
727+ Box.delayTask (20 ); // not 1 ms?!
728+
729+ for (uint8_t i=0 ; i<maxBytes/4 ; i++) {
730+ result = ISO15693_readSingleBlock (i, &data[i*4 ]);
731+ if (result != ISO15693_RESULT::READ_SINGLE_BLOCK_VALID_RESPONSE)
732+ break ;
733+ bytesRead += 4 ;
734+ reinitRFID ();
735+ }
736+
737+ writeRegister (REGISTER::CHIP_STATUS_CONTROL, 0b00000001 ); // turnRfOff();
738+ return bytesRead;
739+ }
740+ void BoxRFID::logTagMemory () {
741+ uint8_t data[32 ];
742+ uint8_t bytesRead;
743+ bytesRead = Box.boxRFID .readBlocks (data, 32 );
744+ if (bytesRead == 32 ) {
745+ Log.disableNewline (true );
746+ Log.info (" Reading %i bytes of memory:" );
747+ for (uint8_t i = 0 ; i < bytesRead; i++) {
748+ Log.printf (" %x" , data[i]);
749+ }
750+ Log.println ();
751+ Log.disableNewline (false );
752+ } else {
753+ Log.error (" Expected 32 blocks but got %i..." , bytesRead);
754+ }
755+ }
756+
757+ bool BoxRFID::dumpTagMemory (bool overwrite) {
758+ FileFs dumpFile;
759+ uint8_t data[32 ];
760+ uint8_t bytesRead;
761+ char * path = " rDUMP/0123456789ABCDEF" ;
762+ sprintf (
763+ (char *)path,
764+ " rDUMP/%02x%02x%02x%02x%02x%02x%02x%02x" ,
765+ tagUid[7 ], tagUid[6 ], tagUid[5 ], tagUid[4 ], tagUid[3 ], tagUid[2 ], tagUid[1 ], tagUid[0 ]
766+ );
767+ if (!overwrite && dumpFile.open ((char *)path, FA_OPEN_EXISTING | FA_READ)) {
768+ dumpFile.close ();
769+ Log.info (" Dump %s exists, skip..." , path);
770+ return false ;
771+ }
772+
773+ bytesRead = Box.boxRFID .readBlocks (data, 32 );
774+ if (bytesRead == 32 ) {
775+ Log.disableNewline (true );
776+ Log.info (" Reading %i bytes of memory:" );
777+ for (uint8_t i = 0 ; i < bytesRead; i++) {
778+ Log.printf (" %x" , data[i]);
779+ }
780+ Log.disableNewline (false );
781+ Log.println ();
782+ uint8_t mode = FA_CREATE_NEW | FA_WRITE;
783+ if (overwrite)
784+ mode = FA_CREATE_ALWAYS | FA_WRITE;
785+
786+ if (dumpFile.open ((char *)path, mode)) {
787+ dumpFile.write (data, bytesRead);
788+ dumpFile.close ();
789+ Log.info (" Wrote dump to %s" , path);
790+ return true ;
791+ } else {
792+ Log.error (" Could not open %s for writing" , path);
793+ }
794+ } else {
795+ Log.error (" Expected 32 blocks but got %i..." , bytesRead);
796+ }
797+ return false ;
676798}
0 commit comments