In the process of practical application, customers often need the combination of ble + NFC. At present, our IOT-DK006 is the only development board with NFC module. But the NFC example is not perfect. So we porting the library of NFC reader- PN7150, to support KW series microcomputer so that KW series can handle the demand of ble + NFC function.
Now I will introduce you how to port the NFC lib to KW.
1 PN7150 Introduction
PN7150 is the high-performance version of PN7120, the plug’n play NFC solution for easy integration into any OS environment, reducing Bill of Material (BOM) size and cost. PN71xx controllers are ideal for home-automation applications such as gateways and work seamlessly with NFC connected tags.
2 Tools
hardware:FRDM-KW36,PN7150 , some wire
software:mcuxpresso11.3
package:NXP-NCI MCUXpresso example Project
This package contains the nfc library and example that we need. We will refer the ‘NXPNCI-K64F_example’ firstly.
Sdk version: 2.2.8,
Example: frdmkw36_rtos_examples_freertos_i2c
3 Steps
Hardware part:We need connect the PN7150 to KW36 like the picture. Although we can connect the PN7150 to board through the ardunio connector, the pin’s voltage is not enough to drive the PN7150. So we need a wire connected to U1 to get 3.3V.
PN7150 |
FRDM-KW36 |
VBAT/PVDD |
3.3V |
VANT |
5V |
GND |
GND |
IRQ |
PTA16 |
VEN |
PTC15 |
SCL |
PTB0,I2C0 |
SDA |
PTB1,I2C0 |
Software part:We should add the nfc library and directory into our project. You can check the following picture to know what file is necessary. If you want to know how to add directory into our project, you can refer this link.
The red line shows what file we need.
Please notice that when we add file path into the mcuxpresso configuration, we also need add the path into ‘Path and Symbols’ .
We need add some macro into ‘Preprocessor’.
We copy the NXPNCI-K64F_example’s main file content into our ‘freertos_i2c.c’.
Next, we need modify the file pin_mux.c, tml.c and board.h
In file board.h,add the following macro. Don't forget to enable the pin clock.
/* NXPNCI NFC related declaration */
#define BOARD_NXPNCI_I2C_INSTANCE I2C0
#define BOARD_NXPNCI_I2C_BAUDRATE (100000)
#define BOARD_NXPNCI_I2C_ADDR (0x28)
#define BOARD_NXPNCI_IRQ_PORTIRQn PORTA_IRQn
#define BOARD_NXPNCI_IRQ_GPIO (GPIOA)
#define BOARD_NXPNCI_IRQ_PORT (PORTA)
#define BOARD_NXPNCI_IRQ_PIN (16U)
#define BOARD_NXPNCI_VEN_GPIO (GPIOC)
#define BOARD_NXPNCI_VEN_PORT (PORTC)
#define NXPNCI_VEN_PIN (5U)
In file pin_mux.c, add head file ‘board.h’. Add the following code in function
’ BOARD_InitPins’. The step is to configure the VEN, IRQ and I2C0. This example contains the I2C1’s code, you can comment them.
/* Initialize NXPNCI GPIO pins below */
/* IRQ and VEN PIN_MUX Configuration */
PORT_SetPinMux(BOARD_NXPNCI_IRQ_PORT, BOARD_NXPNCI_IRQ_PIN, kPORT_MuxAsGpio);
PORT_SetPinMux(BOARD_NXPNCI_VEN_PORT, NXPNCI_VEN_PIN, kPORT_MuxAsGpio);
/* IRQ interrupt Configuration */
NVIC_SetPriority(BOARD_NXPNCI_IRQ_PORTIRQn, 6);
EnableIRQ(BOARD_NXPNCI_IRQ_PORTIRQn);
PORT_SetPinInterruptConfig(BOARD_NXPNCI_IRQ_PORT, BOARD_NXPNCI_IRQ_PIN, kPORT_InterruptRisingEdge);
Finally, in file tml.c, modify PORTC_IRQHandler as PORTA_IRQHandler
We finished all steps.
4 Results
We use ntag to test the reading and writing operation.
When the tag is closed to the PN7150, we will get the following message.
The text recording is ‘VER=03’.
Next, we will modify the text recording
We need add the new macro to preprocessor.
We can modify the variable NDEF_MESSAGE in function task_nfc_reader to modify the text recording.
Then we download the program again. We will see the original text ‘VER=03’ and the text has been modified. Then we read the tag again. We will see the new text.
If we want to send the larger text, what should we do? We need modify the macro ‘ADD’.
When only 4 characters are sent, ‘ADD’ is 0. And every additional character is added, the ‘ADD’ will add. We modify the tag as ‘Ver=03’, and we have two more characters. So ‘ADD’ needs to be defined as 2
It firstly shows the text ‘Test’. Then it will show the new text ‘Ver=03’.
Other tags’ reading and writing operation can be enabled by defining some macro.