Commit 1b114503 authored by Laurent Latil's avatar Laurent Latil Committed by Philippe Teuwen

Add I2C protocol support for PN532.

parent 6038aca7
......@@ -46,6 +46,7 @@ AC_HEADER_STDC
AC_HEADER_STDBOOL
AC_CHECK_HEADERS([fcntl.h limits.h stdio.h stdlib.h stdint.h stddef.h stdbool.h sys/ioctl.h sys/param.h sys/time.h termios.h])
AC_CHECK_HEADERS([linux/spi/spidev.h], [spi_available="yes"])
AC_CHECK_HEADERS([linux/i2c-dev.h], [i2c_available="yes"])
AC_CHECK_FUNCS([memmove memset select strdup strerror strstr strtol usleep],
[AC_DEFINE([_XOPEN_SOURCE], [600], [Enable POSIX extensions if present])])
......@@ -122,6 +123,9 @@ AM_CONDITIONAL(UART_ENABLED, [test x"$uart_required" = x"yes"])
# Enable SPI if
AM_CONDITIONAL(SPI_ENABLED, [test x"$spi_required" = x"yes"])
# Enable I2C if
AM_CONDITIONAL(I2C_ENABLED, [test x"$i2c_required" = x"yes"])
# Documentation (default: no)
AC_ARG_ENABLE([doc],AS_HELP_STRING([--enable-doc],[Enable documentation generation.]),[enable_doc=$enableval],[enable_doc="no"])
......
......@@ -28,3 +28,10 @@ if LIBUSB_ENABLED
libnfcbuses_la_LIBADD += @libusb_LIBS@
endif
EXTRA_DIST += usbbus.c usbbus.h
if I2C_ENABLED
libnfcbuses_la_SOURCES += i2c.c i2c.h
libnfcbuses_la_CFLAGS +=
libnfcbuses_la_LIBADD +=
endif
EXTRA_DIST += i2c.c i2c.h
/*-
* Free/Libre Near Field Communication (NFC) library
*
* Libnfc historical contributors:
* Copyright (C) 2009 Roel Verdult
* Copyright (C) 2009-2013 Romuald Conty
* Copyright (C) 2010-2012 Romain Tarti?re
* Copyright (C) 2010-2013 Philippe Teuwen
* Copyright (C) 2012-2013 Ludovic Rousseau
* Additional contributors of this file:
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
/**
* @file i2c.c
* @brief I2C driver (implemented / tested for Linux only currently)
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif // HAVE_CONFIG_H
#include "i2c.h"
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <nfc/nfc.h>
#include "nfc-internal.h"
#define LINUX_I2C_DRIVER_NAME "linux_i2c"
#define LOG_GROUP NFC_LOG_GROUP_COM
#define LOG_CATEGORY "libnfc.bus.i2c"
# if defined (__linux__)
const char *i2c_ports_device_radix[] =
{ "i2c-", NULL };
# else
# error "Can't determine I2C devices standard names for your system"
# endif
struct i2c_device
{
int fd; // I2C device file descriptor
};
#define I2C_DATA( X ) ((struct i2c_device *) X)
i2c_device
i2c_open(const char *pcI2C_busName, uint32_t devAddr)
{
struct i2c_device *id = malloc(sizeof(struct i2c_device));
if (id == 0)
return INVALID_I2C_BUS ;
id->fd = open(pcI2C_busName, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (id->fd == -1) {
perror("Cannot open I2C bus");
i2c_close(id);
return INVALID_I2C_BUS ;
}
if (ioctl(id->fd, I2C_SLAVE, devAddr) < 0) {
perror("Cannot select I2C device");
i2c_close(id);
return INVALID_I2C_ADDRESS ;
}
return id;
}
void
i2c_close(const i2c_device id)
{
if (I2C_DATA(id) ->fd >= 0) {
close(I2C_DATA(id) ->fd);
}
free(id);
}
/**
* @brief Read a frame from the I2C device and copy data to \a pbtRx
*
* @param timeout Time out for data read (in milliseconds). 0 for not timeout.
* @return 0 on success, otherwise driver error code
*/
int
i2c_read(i2c_device id, uint8_t *pbtRx, const size_t szRx, void *abort_p,
int timeout)
{
int iAbortFd = abort_p ? *((int *) abort_p) : 0;
int res;
int done = 0;
struct timeval start_tv, cur_tv;
long long duration;
ssize_t recCount = read(I2C_DATA(id) ->fd, pbtRx, szRx);
if (recCount < 0) {
res = NFC_EIO;
} else {
if (recCount < (ssize_t)szRx) {
res = NFC_EINVARG;
} else {
res = recCount;
}
}
return res;
}
/**
* @brief Write a frame to I2C device containing \a pbtTx content
*
* @param timeout Time out for data read (in milliseconds). 0 for not timeout.
* @return 0 on success, otherwise a driver error is returned
*/
int
i2c_write(i2c_device id, const uint8_t *pbtTx, const size_t szTx, int timeout)
{
LOG_HEX(LOG_GROUP, "TX", pbtTx, szTx);
ssize_t writeCount;
writeCount = write(I2C_DATA(id) ->fd, pbtTx, szTx);
if ((const ssize_t) szTx == writeCount) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG,
"wrote %d bytes successfully.", szTx);
return NFC_SUCCESS;
} else {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR,
"Error: wrote only %d bytes (%d expected).", writeCount, (int) szTx);
return NFC_EIO;
}
}
char **
i2c_list_ports(void)
{
char **res = malloc(sizeof(char *));
if (!res) {
perror("malloc");
return res;
}
size_t szRes = 1;
res[0] = NULL;
DIR *dir;
if ((dir = opendir("/dev")) == NULL ) {
perror("opendir error: /dev");
return res;
}
struct dirent entry;
struct dirent *result;
while ((readdir_r(dir, &entry, &result) == 0) && (result != NULL )) {
const char **p = i2c_ports_device_radix;
while (*p) {
if (!strncmp(entry.d_name, *p, strlen(*p))) {
char **res2 = realloc(res, (szRes + 1) * sizeof(char *));
if (!res2) {
perror("malloc");
goto oom;
}
res = res2;
if (!(res[szRes - 1] = malloc(6 + strlen(entry.d_name)))) {
perror("malloc");
goto oom;
}
sprintf(res[szRes - 1], "/dev/%s", entry.d_name);
szRes++;
res[szRes - 1] = NULL;
}
p++;
}
}
oom:
closedir(dir);
return res;
}
/*-
* Free/Libre Near Field Communication (NFC) library
*
* Libnfc historical contributors:
* Copyright (C) 2009 Roel Verdult
* Copyright (C) 2009-2013 Romuald Conty
* Copyright (C) 2010-2012 Romain Tarti?re
* Copyright (C) 2010-2013 Philippe Teuwen
* Copyright (C) 2012-2013 Ludovic Rousseau
* Additional contributors of this file:
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
/**
* @file i2c.h
* @brief I2C driver header
*/
#ifndef __NFC_BUS_I2C_H__
# define __NFC_BUS_I2C_H__
# include <sys/time.h>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <linux/i2c-dev.h>
# include <nfc/nfc-types.h>
// extern const struct i2c_driver linux_i2c_driver;
typedef void *i2c_device;
# define INVALID_I2C_BUS (void*)(~1)
# define INVALID_I2C_ADDRESS (void*)(~2)
i2c_device i2c_open(const char *pcI2C_busName, uint32_t devAddr);
void i2c_close(const i2c_device id);
int i2c_read(i2c_device id, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout);
int i2c_write(i2c_device id, const uint8_t *pbtTx, const size_t szTx, int timeout);
char ** i2c_list_ports(void);
#endif // __NFC_BUS_I2C_H__
......@@ -35,6 +35,10 @@ if DRIVER_PN532_SPI_ENABLED
libnfcdrivers_la_SOURCES += pn532_spi.c pn532_spi.h
endif
if DRIVER_PN532_I2C_ENABLED
libnfcdrivers_la_SOURCES += pn532_i2c.c pn532_i2c.h
endif
if PCSC_ENABLED
libnfcdrivers_la_CFLAGS += @libpcsclite_CFLAGS@
libnfcdrivers_la_LIBADD += @libpcsclite_LIBS@
......
This diff is collapsed.
/*-
* Free/Libre Near Field Communication (NFC) library
*
* Libnfc historical contributors:
* Copyright (C) 2009 Roel Verdult
* Copyright (C) 2009-2013 Romuald Conty
* Copyright (C) 2010-2012 Romain Tarti?re
* Copyright (C) 2010-2013 Philippe Teuwen
* Copyright (C) 2012-2013 Ludovic Rousseau
* Additional contributors of this file:
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
/**
* @file pn532_i2c.h
* @brief Driver for PN532 connected through I2C bus
*/
#ifndef __NFC_DRIVER_PN532_I2C_H__
#define __NFC_DRIVER_PN532_I2C_H__
#include <nfc/nfc-types.h>
# define PN53X_MAX_FRAME_LENGTH 265
/* Reference to the I2C driver structure */
extern const struct nfc_driver pn532_i2c_driver;
#endif // ! __NFC_DRIVER_I2C_H__
......@@ -113,6 +113,10 @@
# include "drivers/pn532_spi.h"
#endif /* DRIVER_PN532_SPI_ENABLED */
#if defined (DRIVER_PN532_I2C_ENABLED)
# include "drivers/pn532_i2c.h"
#endif /* DRIVER_PN532_I2C_ENABLED */
#define LOG_CATEGORY "libnfc.general"
#define LOG_GROUP NFC_LOG_GROUP_GENERAL
......@@ -145,11 +149,15 @@ nfc_drivers_init(void)
#if defined (DRIVER_PN532_SPI_ENABLED)
nfc_register_driver(&pn532_spi_driver);
#endif /* DRIVER_PN532_SPI_ENABLED */
#if defined (DRIVER_PN532_I2C_ENABLED)
nfc_register_driver(&pn532_i2c_driver);
#endif /* DRIVER_PN532_I2C_ENABLED */
#if defined (DRIVER_ARYGON_ENABLED)
nfc_register_driver(&arygon_driver);
#endif /* DRIVER_ARYGON_ENABLED */
}
/** @ingroup lib
* @brief Register an NFC device driver with libnfc.
* This function registers a driver with libnfc, the caller is responsible of managing the lifetime of the
......@@ -189,6 +197,11 @@ nfc_init(nfc_context **context)
}
if (!nfc_drivers)
nfc_drivers_init();
//#if defined (I2C_DRIVERS_ENABLED)
// if (!i2c_drivers)
// i2c_drivers_init();
//#endif
}
/** @ingroup lib
......
......@@ -4,7 +4,7 @@ AC_DEFUN([LIBNFC_ARG_WITH_DRIVERS],
[
AC_MSG_CHECKING(which drivers to build)
AC_ARG_WITH(drivers,
AS_HELP_STRING([--with-drivers=DRIVERS], [Use a custom driver set, where DRIVERS is a comma-separated list of drivers to build support for. Available drivers are: 'acr122_pcsc', 'acr122_usb', 'acr122s', 'arygon', 'pn532_spi', 'pn532_uart' and 'pn53x_usb'. Default drivers set is 'acr122_usb,acr122s,arygon,pn532_spi,pn532_uart,pn53x_usb'. The special driver set 'all' compile all available drivers.]),
AS_HELP_STRING([--with-drivers=DRIVERS], [Use a custom driver set, where DRIVERS is a coma-separated list of drivers to build support for. Available drivers are: 'acr122_pcsc', 'acr122_usb', 'acr122s', 'arygon', 'pn532_spi', 'pn532_uart', 'pn532_i2c' and 'pn53x_usb'. Default drivers set is 'acr122_usb,acr122s,arygon,pn532_spi,pn532_i2c,pn532_uart,pn53x_usb'. The special driver set 'all' compile all available drivers.]),
[ case "${withval}" in
yes | no)
dnl ignore calls without any arguments
......@@ -30,6 +30,10 @@ AC_DEFUN([LIBNFC_ARG_WITH_DRIVERS],
then
DRIVER_BUILD_LIST="$DRIVER_BUILD_LIST pn532_spi"
fi
if test x"$i2c_available" = x"yes"
then
DRIVER_BUILD_LIST="$DRIVER_BUILD_LIST pn532_i2c"
fi
;;
all)
DRIVER_BUILD_LIST="acr122_pcsc acr122_usb acr122s arygon pn53x_usb pn532_uart"
......@@ -37,6 +41,10 @@ AC_DEFUN([LIBNFC_ARG_WITH_DRIVERS],
then
DRIVER_BUILD_LIST="$DRIVER_BUILD_LIST pn532_spi"
fi
if test x"$i2c_available" = x"yes"
then
DRIVER_BUILD_LIST="$DRIVER_BUILD_LIST pn532_i2c"
fi
;;
esac
......@@ -49,6 +57,7 @@ AC_DEFUN([LIBNFC_ARG_WITH_DRIVERS],
driver_arygon_enabled="no"
driver_pn532_uart_enabled="no"
driver_pn532_spi_enabled="no"
driver_pn532_i2c_enabled="no"
for driver in ${DRIVER_BUILD_LIST}
do
......@@ -88,6 +97,11 @@ AC_DEFUN([LIBNFC_ARG_WITH_DRIVERS],
driver_pn532_spi_enabled="yes"
DRIVERS_CFLAGS="$DRIVERS_CFLAGS -DDRIVER_PN532_SPI_ENABLED"
;;
pn532_i2c)
i2c_required="yes"
driver_pn532_i2c_enabled="yes"
DRIVERS_CFLAGS="$DRIVERS_CFLAGS -DDRIVER_PN532_I2C_ENABLED -DI2C_DRIVERS_ENABLED"
;;
*)
AC_MSG_ERROR([Unknow driver: $driver])
;;
......@@ -101,6 +115,9 @@ AC_DEFUN([LIBNFC_ARG_WITH_DRIVERS],
AM_CONDITIONAL(DRIVER_ARYGON_ENABLED, [test x"$driver_arygon_enabled" = xyes])
AM_CONDITIONAL(DRIVER_PN532_UART_ENABLED, [test x"$driver_pn532_uart_enabled" = xyes])
AM_CONDITIONAL(DRIVER_PN532_SPI_ENABLED, [test x"$driver_pn532_spi_enabled" = xyes])
AM_CONDITIONAL(DRIVER_PN532_I2C_ENABLED, [test x"$driver_pn532_i2c_enabled" = xyes])
AM_CONDITIONAL(I2C_DRIVERS_ENABLED, [test x"$i2c_required" = xyes])
])
AC_DEFUN([LIBNFC_DRIVERS_SUMMARY],[
......@@ -113,4 +130,5 @@ echo " arygon........... $driver_arygon_enabled"
echo " pn53x_usb........ $driver_pn53x_usb_enabled"
echo " pn532_uart....... $driver_pn532_uart_enabled"
echo " pn532_spi....... $driver_pn532_spi_enabled"
echo " pn532_i2c........ $driver_pn532_i2c_enabled"
])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment