Merged in offcode/nuttx/pn532 (pull request #47)

add NXP pn532 NFC-chip support
This commit is contained in:
Gregory Nutt 2016-01-17 14:25:22 -06:00
commit 81fe265502
5 changed files with 1423 additions and 0 deletions

View File

@ -61,3 +61,40 @@ config WL_NRF24L01_RXFIFO_LEN
endif
endif
config WL_PN532
bool "pn532 NFC-chip support"
default n
select SPI
---help---
This options adds driver support for the PN532 NFC chip.
if WL_PN532
config PN532_SPI_FREQ
int "SPI frequency for PN532"
default 1000000
depends on WL_PN532
config WL_PN532_DEBUG
bool "Enable PN532 debug"
default n
depends on WL_PN532
config WL_PN532_DEBUG_TX
bool "trace TX frames"
default n
depends on WL_PN532_DEBUG
config WL_PN532_DEBUG_RX
bool "trace RX frames"
default n
depends on WL_PN532_DEBUG
endif

View File

@ -49,6 +49,10 @@ ifeq ($(CONFIG_WL_CC3000),y)
include wireless$(DELIM)cc3000$(DELIM)Make.defs
endif
ifeq ($(CONFIG_WL_PN532),y)
CSRCS += pn532.c
endif
# Include wireless devices build support
DEPPATH += --dep-path wireless

1068
drivers/wireless/pn532.c Normal file

File diff suppressed because it is too large Load Diff

155
drivers/wireless/pn532.h Normal file
View File

@ -0,0 +1,155 @@
/****************************************************************************
* drivers/wireless/pn532.h
*
* Copyright(C) 2012,2013,2016 Offcode Ltd. All rights reserved.
* Authors: Janne Rosberg <janne@offcode.fi>
* Teemu Pirinen <teemu@offcode.fi>
* Juho Grundström <juho@offcode.fi>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <nuttx/spi/spi.h>
#include <nuttx/wqueue.h>
#include <nuttx/wireless/pn532.h>
/****************************************************************************
* Pre-Processor Definitions
****************************************************************************/
#define PN532_PREAMBLE 0x00
#define PN532_STARTCODE1 0x00
#define PN532_STARTCODE2 0xFF
#define PN532_POSTAMBLE 0x00
#define PN532_SOF 0xFF00
#define PN532_HOSTTOPN532 0xD4
#define PN532_PN532TOHOST 0xD5
#define PN532_SPI_STATREAD 0x02
#define PN532_SPI_DATAWRITE 0x01
#define PN532_SPI_DATAREAD 0x03
#define PN532_SPI_READY 0x01
/* PN532 Commands */
#define PN532_COMMAND_DIAGNOSE 0x00
#define PN532_COMMAND_GETFIRMWAREVERSION 0x02
#define PN532_COMMAND_GETGENERALSTATUS 0x04
#define PN532_COMMAND_READREGISTER 0x06
#define PN532_COMMAND_WRITEREGISTER 0x08
#define PN532_COMMAND_READGPIO 0x0C
#define PN532_COMMAND_WRITEGPIO 0x0E
#define PN532_COMMAND_SETSERIALBAUDRATE 0x10
#define PN532_COMMAND_SETPARAMETERS 0x12
#define PN532_COMMAND_SAMCONFIGURATION 0x14
#define PN532_COMMAND_POWERDOWN 0x16
#define PN532_COMMAND_RFCONFIGURATION 0x32
#define PN532_COMMAND_RFREGULATIONTEST 0x58
#define PN532_COMMAND_INJUMPFORDEP 0x56
#define PN532_COMMAND_INJUMPFORPSL 0x46
#define PN532_COMMAND_INLISTPASSIVETARGET 0x4A
#define PN532_COMMAND_INATR 0x50
#define PN532_COMMAND_INPSL 0x4E
#define PN532_COMMAND_INDATAEXCHANGE 0x40
#define PN532_COMMAND_INCOMMUNICATETHRU 0x42
#define PN532_COMMAND_INDESELECT 0x44
#define PN532_COMMAND_INRELEASE 0x52
#define PN532_COMMAND_INSELECT 0x54
#define PN532_COMMAND_INAUTOPOLL 0x60
#define PN532_COMMAND_TGINITASTARGET 0x8C
#define PN532_COMMAND_TGSETGENERALBYTES 0x92
#define PN532_COMMAND_TGGETDATA 0x86
#define PN532_COMMAND_TGSETDATA 0x8E
#define PN532_COMMAND_TGSETMETADATA 0x94
#define PN532_COMMAND_TGGETINITIATORCOMMAND 0x88
#define PN532_COMMAND_TGRESPONSETOINITIATOR 0x90
#define PN532_COMMAND_TGGETTARGETSTATUS 0x8A
#define PN532_WAKEUP 0x55
#define PN532_SAM_NORMAL_MODE 0x01
#define PN532_SAM_VIRTUAL_CARD 0x02
#define PN532_SAM_WIRED_CARD 0x03
#define PN532_SAM_DUAL_CARD 0x04
struct pn532_frame {
uint8_t preamble; /* 0x00 */
uint16_t start_code; /* 0x00FF (BE) -> 0xFF00 (LE) */
uint8_t len; /* 1 byte indicating the number of bytes in
* the data field */
uint8_t lcs; /* 1 Packet Length Checksum LCS byte that satisfies
* the relation: Lower byte of [LEN + LCS] = 00h */
uint8_t tfi; /* Frame idenfifier 0xD4, 0xD5 */
uint8_t data[]; /* LEN-1 bytes of Packet Data Information.
* The first byte PD0 is the Command Code */
} packed_struct;
struct pn_poll_response {
uint8_t nbtg;
uint8_t tg;
uint8_t target_data[];
} packed_struct;
struct pn_target_type_a {
uint16_t sens_res;
uint8_t sel_res;
uint8_t nfcid_len;
uint8_t nfcid_data[];
} packed_struct;
struct pn_firmware_version {
uint8_t ic;
uint8_t ver;
uint8_t rev;
uint8_t support;
};
struct pn532_dev_s
{
uint8_t state;
FAR struct spi_dev_s *spi; /* SPI interface */
FAR struct pn532_config_s *config; /* Board configuration data */
};
#ifndef CONFIG_PN532_SPI_FREQ
#define CONFIG_PN532_SPI_FREQ (5000000)
#endif
bool pn532_set_config(struct pn532_dev_s *dev, uint8_t flags);

View File

@ -0,0 +1,159 @@
/****************************************************************************
* include/wireless/nfc.h
*
* Copyright(C) 2012,2013,2016 Offcode Ltd. All rights reserved.
* Authors: Janne Rosberg <janne@offcode.fi>
* Teemu Pirinen <teemu@offcode.fi>
* Juho Grundström <juho@offcode.fi>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __NUTTX_WIRELESS_PN532_H
#define __NUTTX_WIRELESS_PN532_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/spi/spi.h>
#include <nuttx/irq.h>
#include <sys/ioctl.h>
#include <nuttx/wireless/wireless.h>
/****************************************************************************
* Pre-Processor Definitions
****************************************************************************/
#define PN532_MIFARE_ISO14443A (0x00)
/* IOCTL Commands ***********************************************************/
#define PN532IOC_SET_SAM_CONF _WLIOC_USER(0x0001)
#define PN532IOC_READ_PASSIVE _WLIOC_USER(0x0002)
#define PN532IOC_SET_RF_CONF _WLIOC_USER(0x0003)
#define PN532IOC_SEND_CMD_READ_PASSIVE _WLIOC_USER(0x0004)
#define PN532IOC_GET_DATA_READY _WLIOC_USER(0x0005)
#define PN532IOC_GET_TAG_ID _WLIOC_USER(0x0006)
#define PN532IOC_GET_STATE _WLIOC_USER(0x0007)
#define PN532IOC_READ_TAG_DATA _WLIOC_USER(0x0008)
#define PN532IOC_WRITE_TAG_DATA _WLIOC_USER(0x0009)
enum pn532_state_E
{
PN532_STATE_NOT_INIT,
PN532_STATE_IDLE,
PN532_STATE_CMD_SENT,
PN532_STATE_DATA_READY,
};
/****************************************************************************
* Global Data
****************************************************************************/
struct pn532_dev_s;
struct pn532_config_s
{
int (*reset)(uint8_t enable);
/* external CS, if NULL then SPIDEV_WIRELESS CS is used */
int (*select)(struct pn532_dev_s *dev, bool sel);
int (*irqattach)(void* dev, xcpt_t isr);
};
enum PN_SAM_MODE {
PN_SAM_NORMAL_MODE = 0x01,
PN_SAM_VIRTUAL_CARD,
PN_SAM_WIRED_CARD,
SAM_DUAL_CARD
};
struct pn_sam_settings_s
{
enum PN_SAM_MODE mode; /* Mode */
uint8_t timeout; /* Timeout: LSB=50ms 0x14*50ms = 1sec */
uint8_t irq_en; /* If 1 - enable P-70, IRQ */
};
enum PN_RF_CONFIG_ITEM {
PN_RF_CONFIG_RF_FIELD = 0x01,
PN_RF_CONFIG_VARIOUS_TIMINGS = 0x02,
PN_RF_CONFIG_ITEM_ANALOG_106A = 0x0A,
PN_RF_CONFIG_ITEM_ANALOG_212 = 0x0B,
};
struct pn_rf_config_s
{
uint8_t cfg_item; /* Item */
uint8_t data_size; /* number of config items */
uint8_t config[11]; /* Item config data */
};
struct pn_mifare_tag_data_s
{
uint32_t data;
uint8_t address;
};
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C" {
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: pn532_register
*
* Description:
* Register the PN532 character device as 'devpath'
*
* Input Parameters:
* devpath - The full path to the driver to register. E.g., "/dev/nfc0"
* spi - An instance of the SPI interface to use to communicate with PN532
* config - Device persistent board data
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
EXTERN int pn532_register(FAR const char *devpath, FAR struct spi_dev_s *spi,
FAR struct pn532_config_s *config);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __NUTTX_WIRELESS_PN532_H */