NFC interrupt for detecting read/write from a reader

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

NFC interrupt for detecting read/write from a reader

Jump to solution
434 Views
Nobby
Contributor II

Hello,

I am developping an application using an NFC chip and a reader.
And I want to turn on a LED on the NFC chip when the reader is writing on the shared memory.

I am using the NHS3152 chip and the RC522 as a reader.
I am aware that you do not recommande the use of the RC522 reader,
But it works for now, I can write into a specific page of the shared memory using the RC522 reader.
Later, we will use the PN7642.

What works?
Writing / Reading using NFC,
The NHS3152 detects if an NFC is available
I can turn on the LED if an NFC is available

What does not work?
I want to turn one the LED only when the reader is writing on the shared memory
I want to access the value inserted into the shared memory

The code on arduino I use for writing into a specific page using the RC522 is here:
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Write personal data NHS3152"));
}
void loop() {
if ( ! mfrc522.PICC_IsNewCardPresent()) {return;}
if ( ! mfrc522.PICC_ReadCardSerial()) {return;}
Serial.setTimeout(20000L);
byte pageNumber = 4;
byte buffr[] = {0x6,0x6,0x6,0x6};
mfrc522.MIFARE_Write(pageNumber, buffr, 16);
Serial.println(" ");
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}

I found the code for the NHS3152 I use for detecting if a reader is writing is here:
.../release_mra2_12_5_nhs3152/docs/firmware/a00450.html
/NHS31xx SW API/Modules/Drivers/NFC: Near field communication driver

#include "board.h"
int main(void){
Chip_NFC_Init(NSS_NFC);
NVIC_EnableIRQ(NFC_IRQn);

Chip_NFC_SetTargetAddress(NSS_NFC, 4); /* 32 bit aligned offset of location to be monitored. I changed it with 0, 3 and 4 */
Chip_NFC_Int_SetEnabledMask(NSS_NFC, NFC_INT_TARGETWRITE | NFC_INT_TARGETREAD);

while (true){
NFC_INT_T status = Chip_NFC_Int_GetRawStatus(NSS_NFC);
if (status & NFC_INT_TARGETWRITE) {
LED_Toggle(LED_RED);
Chip_Clock_System_BusyWait_ms(500);
}
Chip_NFC_Int_ClearRawStatus(NSS_NFC, status);
}
return 0;
}

I wrote on the 4th page using the reader,
And I test on the NSH3152 to initiate the Chip_NFC_SetTargetAddress by 0, 3 and by 4
The three values do not work.

In the condition statement of the code on the NSH3152: if (status & NFC_INT_TARGETWRITE) {...},
If I only use the status, I can detect the presence of an NFC

These are my questions:
1) Should I initiate the Chip_NFC_SetTargetAddress by 0, 3 or 4,
If I wrote on the 4th page of the shared memory?

2) Do I need to initiate the Chip_NFC_SetTargetAddress, if I want only to detect a reader wanted to read?
If yes, by what value?

3) If the problems is resolved, how can I get access to the memory on the NSH3152, after a reader wrote on it?

I really appreciate your help
Nobby

0 Kudos
1 Solution
401 Views
patrickgeens
NXP Apps Support
NXP Apps Support

Hello Nobby,

 

I have adapted the demo code in the following way:

  • it uses the interrupt handler to catch reads and writes to the target page and transfers this info to the main loop via global variables
  • the main loop blinks the led if read or write is detected, timing depends on direction
  • the main loop reads and writes to the NFC shared memory buffer to report data from and to the NFC reader
  • shows the conversion between NFC page space and shared memory buffer: there is an offset of 4

At the NFC reader side:

  • writing a value to page "myNfcPage" stores this value in the "myData" variable
  • every read of page "myNfcPage' increments "myData" and stores the incremented value in NFC memory
  • to demo: write page once with random value, read multiple times to check if value appears and is incremented

 

Due to the led blinking the main loop is slow, so if you NFC read or write too fast, the global variables set in the interrupt handler will be overwritten by the main loop and you will miss some transfers.

#include "board.h"

#define NFC_SHARED_MEM_PAGE_OFFSET 4 // address 0 in shared memory corresponds to page 4 in NFC space

volatile bool readDetected = false;
volatile bool writeDetected = false;

void NFC_IRQHandler(void)
{
NFC_INT_T status = Chip_NFC_Int_GetRawStatus(NSS_NFC);
readDetected = status & NFC_INT_TARGETREAD;
writeDetected = status & NFC_INT_TARGETWRITE;
// clear interrupt flags
Chip_NFC_Int_ClearRawStatus(NSS_NFC, status);
}

int main(void)
{
uint32_t myData = 0;

Board_Init();

readDetected = false;

writeDetected = false;

uint32_t myNfcPage = 4;
Chip_NFC_Init(NSS_NFC);
Chip_NFC_SetTargetAddress(NSS_NFC, myNfcPage - NFC_SHARED_MEM_PAGE_OFFSET);
Chip_NFC_Int_SetEnabledMask(NSS_NFC, NFC_INT_TARGETWRITE | NFC_INT_TARGETREAD);
NVIC_EnableIRQ(NFC_IRQn);

while (true)
{
if(readDetected)
{
LED_On(LED_RED);
Chip_Clock_System_BusyWait_ms(50); // short blink for READ
LED_Off(LED_RED);
Chip_Clock_System_BusyWait_ms(50);
NSS_NFC->BUF[myNfcPage-NFC_SHARED_MEM_PAGE_OFFSET] = ++myData; // increment after every read
readDetected = false;
}

if(writeDetected)
{
LED_On(LED_RED);
Chip_Clock_System_BusyWait_ms(300); // long blink for WRITE
LED_Off(LED_RED);
Chip_Clock_System_BusyWait_ms(50);
myData = NSS_NFC->BUF[myNfcPage-NFC_SHARED_MEM_PAGE_OFFSET]; // store received data
writeDetected = false;
}
}
return 0;
}

 

You can also use the more general interrupts MEMREAD and MEMWRITE, no target address to be set.

Calling the function "Chip_NFC_GetLastAccessInfo(...)" returns the direction (read or write)

and start and end of the pages accessed (f.i. an NFC read can read 4 pages at once).

 

Kind regards,

Patrick

 

 

View solution in original post

2 Replies
389 Views
Nobby
Contributor II

Hi Patrik,

Thank you very much.

The code works as expected.

Kind regards

Nobby

0 Kudos
402 Views
patrickgeens
NXP Apps Support
NXP Apps Support

Hello Nobby,

 

I have adapted the demo code in the following way:

  • it uses the interrupt handler to catch reads and writes to the target page and transfers this info to the main loop via global variables
  • the main loop blinks the led if read or write is detected, timing depends on direction
  • the main loop reads and writes to the NFC shared memory buffer to report data from and to the NFC reader
  • shows the conversion between NFC page space and shared memory buffer: there is an offset of 4

At the NFC reader side:

  • writing a value to page "myNfcPage" stores this value in the "myData" variable
  • every read of page "myNfcPage' increments "myData" and stores the incremented value in NFC memory
  • to demo: write page once with random value, read multiple times to check if value appears and is incremented

 

Due to the led blinking the main loop is slow, so if you NFC read or write too fast, the global variables set in the interrupt handler will be overwritten by the main loop and you will miss some transfers.

#include "board.h"

#define NFC_SHARED_MEM_PAGE_OFFSET 4 // address 0 in shared memory corresponds to page 4 in NFC space

volatile bool readDetected = false;
volatile bool writeDetected = false;

void NFC_IRQHandler(void)
{
NFC_INT_T status = Chip_NFC_Int_GetRawStatus(NSS_NFC);
readDetected = status & NFC_INT_TARGETREAD;
writeDetected = status & NFC_INT_TARGETWRITE;
// clear interrupt flags
Chip_NFC_Int_ClearRawStatus(NSS_NFC, status);
}

int main(void)
{
uint32_t myData = 0;

Board_Init();

readDetected = false;

writeDetected = false;

uint32_t myNfcPage = 4;
Chip_NFC_Init(NSS_NFC);
Chip_NFC_SetTargetAddress(NSS_NFC, myNfcPage - NFC_SHARED_MEM_PAGE_OFFSET);
Chip_NFC_Int_SetEnabledMask(NSS_NFC, NFC_INT_TARGETWRITE | NFC_INT_TARGETREAD);
NVIC_EnableIRQ(NFC_IRQn);

while (true)
{
if(readDetected)
{
LED_On(LED_RED);
Chip_Clock_System_BusyWait_ms(50); // short blink for READ
LED_Off(LED_RED);
Chip_Clock_System_BusyWait_ms(50);
NSS_NFC->BUF[myNfcPage-NFC_SHARED_MEM_PAGE_OFFSET] = ++myData; // increment after every read
readDetected = false;
}

if(writeDetected)
{
LED_On(LED_RED);
Chip_Clock_System_BusyWait_ms(300); // long blink for WRITE
LED_Off(LED_RED);
Chip_Clock_System_BusyWait_ms(50);
myData = NSS_NFC->BUF[myNfcPage-NFC_SHARED_MEM_PAGE_OFFSET]; // store received data
writeDetected = false;
}
}
return 0;
}

 

You can also use the more general interrupts MEMREAD and MEMWRITE, no target address to be set.

Calling the function "Chip_NFC_GetLastAccessInfo(...)" returns the direction (read or write)

and start and end of the pages accessed (f.i. an NFC read can read 4 pages at once).

 

Kind regards,

Patrick