Libnfc:example pn53x

From NFC Tools
(Difference between revisions)
Jump to: navigation, search
(To compile it)
 
Line 63: Line 63:
 
</source>
 
</source>
  
[[Category:Libnfc]]
+
[[Category:Libnfc]][[Category:Libnfc:Examples]]

Latest revision as of 02:44, 1 January 2014

Even if not advertised in the public include file, some functions specific to the pn53x chipsets are available to the developer using libnfc...

[edit] Short example showing how to manipulate PN53x GPIO pins


It can be downloaded here as example_scl3711_blink.c.
It will blink the SCL3711 LED.
WARNING!! GPIO usages are very specific from reader to reader, don't mess up with GPIOs if you don't know exactly what you're doing!
This example is safe to be used with SCL3711 and ONLY with SCL3711.

[edit] example_scl3711_blink.c

Note that the code here is quite minimal concerning error handling and does not test if you're using really a SCL3711

#include <stdlib.h>
#include <nfc/nfc.h>

Because chips/pn53x.h is not installed with libnfc.h, you've to make it available somehow to the code.
Here we assume we're compiling this program beside libnfc sources

#include "chips/pn53x.h"

Then classical start

int
main(int argc, const char *argv[])
{
  nfc_device *pnd;
  nfc_target nt;
  nfc_context *context;
  nfc_init(&context);
  if (context == NULL) goto exit3;
  pnd = nfc_open(context, NULL);
  if (pnd == NULL) goto exit2;

Now the core of this example: P32 is toggled ON/OFF repetitively

  int i, res;
  for (i = 0; i < 10; i++) {
    if ((res = pn53x_write_register(pnd, PN53X_SFR_P3, _BV(P32), 0)) < 0) goto exit1;
    sleep(1);
    if ((res = pn53x_write_register(pnd, PN53X_SFR_P3, _BV(P32), _BV(P32))) < 0) goto exit1;
    sleep(1);
  }

Some cleaning

exit1:
  nfc_close(pnd);
exit2:
  nfc_exit(context);
exit3:
  exit(0);
}

[edit] To compile it

It assumes libnfc is installed into the system and the program is somewhere aside the libnfc sources.
Adapt "-I../../libnfc" to your setup so that chips/pn53x.h can be found by gcc.

gcc -o example_scl3711_blink -lnfc -I../../libnfc example_scl3711_blink.c
Personal tools
Namespaces

Variants
Actions
Navigation
Documentation
Misc
Toolbox
Partners