Re: Adding Adafruit readGPIO and writeGPIO functions in your PN532 driver library

135 views
Skip to first unread message

Philippe Teuwen

unread,
Oct 8, 2013, 4:55:29 PM10/8/13
to jfgau...@altima.net, rom...@libnfc.org, nfc-too...@googlegroups.com
On 10/08/2013 09:25 PM, Jean-Francois Gauthier wrote:
> Hi Gentlemen,
>
> I have installed successfully your libnfc 1.7.0 library (with the
> PN532 UART driver) and i am pleased to say it performs beautifully :-)
> I would need however to be able to use the GPIO pins on the Adafruit
> Breakout shield to drive a buzzer and a led , as i can with the
> Arduino platform and their PN532 library. I've cut and pasted the two
> functions below in this email , but lack the internal knowledge of
> your libnfc PN532 driver in order to successfully implement these two
> useful functions.
>
> Can you guide me in what should be done in order to add these two
> functions to libnfc 1.7.0?

Hello Jean-Fran�ois,

The nfc-tools-devel (in CC:) is probably a better place to discuss it
and share with the community.
Right now the functions to deal with GPIO are not exposed out of libnfc.
But they're present :-)

You can write your application including the internal headers of libnfc
#include "chips/pn53x.h"
and use pn53x_write_register(pnd, PN53X_SFR_P3, mask, value)

For example, to toggle the LED of SCL3711 reader, you can use this code:

/**
* @file quick_start_example3.c
* @brief Quick start example that presents how to use libnfc internals
*/

#include <stdlib.h>
#include <nfc/nfc.h>
#include "chips/pn53x.h"

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;
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);
}
exit1:
// Close NFC device
nfc_close(pnd);
exit2:
// Release the context
nfc_exit(context);
exit3:
exit(0);
}


Drop it along the other examples in the source in examples/doc/
and compile it with

gcc -o quick_start_example3 -lnfc -I../../libnfc quick_start_example3.c


Best regards
Phil
PS: I'm leaving the rest of your email for reference for nfc-tools-devel ML.

>
> Cheers!
> Jean-Francois.
>
> BELOW IS THE ORIGINAL C CODE FROM ADAFRUIT:
>
> /**************************************************************************/
>
> /*!
> Writes an 8-bit value that sets the state of the PN532's GPIO pins
>
> @warning This function is provided exclusively for board testing and
> is dangerous since it will throw an error if any pin other
> than the ones marked "Can be used as GPIO" are modified!
> All
> pins that can not be used as GPIO should ALWAYS be left high
> (value = 1) or the system will become unstable and a HW
> reset
> will be required to recover the PN532.
>
> pinState[0] = P30 Can be used as GPIO
> pinState[1] = P31 Can be used as GPIO
> pinState[2] = P32 *** RESERVED (Must be 1!) ***
> pinState[3] = P33 Can be used as GPIO
> pinState[4] = P34 *** RESERVED (Must be 1!) ***
> pinState[5] = P35 Can be used as GPIO
>
> @returns 1 if everything executed properly, 0 for an error
> */
> /**************************************************************************/
>
> boolean Adafruit_NFCShield_I2C::writeGPIO(uint8_t pinstate) {
> uint8_t errorbit;
>
> // Make sure pinstate does not try to toggle P32 or P34
> pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34);
>
> // Fill command buffer
> pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO;
> pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate; // P3
> Pins
> pn532_packetbuffer[2] = 0x00; // P7 GPIO Pins (not used ... taken
> by I2C)
>
> #ifdef PN532DEBUG
> Serial.print("Writing P3 GPIO: ");
> Serial.println(pn532_packetbuffer[1], HEX);
> #endif
>
> // Send the WRITEGPIO command (0x0E)
> if (! sendCommandCheckAck(pn532_packetbuffer, 3))
> return 0x0;
>
> // Read response packet (00 00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0F)
> DATACHECKSUM)
> wirereaddata(pn532_packetbuffer, 8);
>
> #ifdef PN532DEBUG
> Serial.print("Received: ");
> PrintHex(pn532_packetbuffer, 8);
> Serial.println("");
> #endif
>
> return (pn532_packetbuffer[6] == 0x0F);
> }
>
> /**************************************************************************/
>
> /*!
> Reads the state of the PN532's GPIO pins
>
> @returns An 8-bit value containing the pin state where:
>
> pinState[0] = P30
> pinState[1] = P31
> pinState[2] = P32
> pinState[3] = P33
> pinState[4] = P34
> pinState[5] = P35
> */
> /**************************************************************************/
>
> uint8_t Adafruit_NFCShield_I2C::readGPIO(void) {
> pn532_packetbuffer[0] = PN532_COMMAND_READGPIO;
>
> // Send the READGPIO command (0x0C)
> if (! sendCommandCheckAck(pn532_packetbuffer, 1))
> return 0x0;
>
> // Read response packet (00 00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0D)
> P3 P7 IO1 DATACHECKSUM)
> wirereaddata(pn532_packetbuffer, 11);
>
> /* READGPIO response should be in the following format:
>
> byte Description
> ------------- ------------------------------------------
> b0..6 Frame header and preamble
> b7 P3 GPIO Pins
> b8 P7 GPIO Pins (not used ... taken by I2C)
> b9 Interface Mode Pins (not used ... bus select pins)
> b10 checksum */
>
> // #ifdef PN532DEBUG
> Serial.print("Received: ");
> PrintHex(pn532_packetbuffer, 11);
> Serial.println("");
> Serial.print("P3 GPIO: 0x"); Serial.println(pn532_packetbuffer[7],
> HEX);
> Serial.print("P7 GPIO: 0x"); Serial.println(pn532_packetbuffer[8],
> HEX);
> Serial.print("IO GPIO: 0x"); Serial.println(pn532_packetbuffer[9],
> HEX);
> // Note: You can use the IO GPIO value to detect the serial bus
> being used
> switch(pn532_packetbuffer[9])
> {
> case 0x00: // Using UART
> Serial.println("Using UART (IO = 0x00)");
> break;
> case 0x01: // Using I2C
> Serial.println("Using I2C (IO = 0x01)");
> break;
> case 0x02: // Using I2C
> Serial.println("Using I2C (IO = 0x02)");
> break;
> }
> // #endif
>
> return pn532_packetbuffer[6];
> }
>
>

Reply all
Reply to author
Forward
0 new messages