arm/stm32f401rc-rs485: Add support to RFID MFRC522

Signed-off-by: Rodrigo Sim rcsim10@gmail.com
This commit is contained in:
Rodrigo Sim 2024-08-04 16:14:55 -03:00 committed by Alan Carvalho de Assis
parent a36c168e54
commit fddebc267d
10 changed files with 291 additions and 0 deletions

View File

@ -664,3 +664,30 @@ you can configure this using menuconfig::
Number of 8x8 LEDs matrices in the horizontal (width)
Number of 8x8 LEDs matrices in the vertical (height)
mfrc522
-------
Configures the NuttShell (nsh) over USB Serial (check usbserial configuration) and enables RFID driver with
MFRC522::
nsh> rfid_readuid
Trying to READ: Card is not present!
Trying to READ: Card is not present!
Trying to READ: RFID CARD UID = 0x3DB3F169
======= ====
MFRC522 PINS
======= ====
SCK PA5
MISO PA6
MOSI PA7
CS PC5
======= ====
The board used is based on MFRC522 NXP IC that supports contactless communication
at 13.56 MHz and ISO/IEC 14443 A/MIFARE and NTAG.
.. figure:: mfrc522_image.jpg
:align: center

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

View File

@ -0,0 +1,79 @@
/****************************************************************************
* boards/arm/stm32/common/include/stm32_mfrc522.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MFRC522_H
#define __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MFRC522_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Inline Functions
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: stm32_mfrc522initialize
*
* Description:
* Initialize and register the MFRC522 RFID driver.
*
* Input Parameters:
* devpath - The full path to the driver to register. E.g., "/dev/rfid0"
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int stm32_mfrc522initialize(const char *devpath);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MFRC522_H */

View File

@ -120,6 +120,10 @@ if(CONFIG_LCD_MAX7219)
list(APPEND SRCS stm32_max7219_matrix.c)
endif()
if(CONFIG_CL_MFRC522)
list(APPEND SRCS stm32_mfrc522.c)
endif()
if(CONFIG_LIS3DSH)
list(APPEND SRCS stm32_lis3dsh.c)
endif()

View File

@ -128,6 +128,10 @@ ifeq ($(CONFIG_LCD_MAX7219),y)
CSRCS += stm32_max7219_matrix.c
endif
ifeq ($(CONFIG_CL_MFRC522),y)
CSRCS += stm32_mfrc522.c
endif
ifeq ($(CONFIG_LIS3DSH),y)
CSRCS += stm32_lis3dsh.c
endif

View File

@ -0,0 +1,84 @@
/****************************************************************************
* boards/arm/stm32/common/src/stm32_mfrc522.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/board.h>
#include <nuttx/spi/spi.h>
#include <nuttx/contactless/mfrc522.h>
#include "stm32.h"
#include "stm32_spi.h"
#if defined(CONFIG_SPI) && defined(CONFIG_STM32_SPI1) && defined(CONFIG_CL_MFRC522)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MFRC522_SPI_PORTNO 1 /* On SPI1 */
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_mfrc522initialize
*
* Description:
* Initialize and register the MFRC522 RFID driver.
*
* Input Parameters:
* devpath - The full path to the driver to register. E.g., "/dev/rfid0"
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int stm32_mfrc522initialize(const char *devpath)
{
struct spi_dev_s *spi;
int ret;
spi = stm32_spibus_initialize(MFRC522_SPI_PORTNO);
if (!spi)
{
return -ENODEV;
}
/* Then register the MFRC522 */
ret = mfrc522_register(devpath, spi);
if (ret < 0)
{
snerr("ERROR: Error registering MFRC522\n");
}
return ret;
}
#endif /* CONFIG_SPI && CONFIG_MFRC522 */

View File

@ -0,0 +1,66 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_ARCH_FPU is not set
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
# CONFIG_NSH_DISABLE_IFCONFIG is not set
# CONFIG_NSH_DISABLE_PS is not set
CONFIG_ARCH="arm"
CONFIG_ARCH_BOARD="stm32f401rc-rs485"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_STM32F401RC_RS485=y
CONFIG_ARCH_BUTTONS=y
CONFIG_ARCH_CHIP="stm32"
CONFIG_ARCH_CHIP_STM32=y
CONFIG_ARCH_CHIP_STM32F401RC=y
CONFIG_ARCH_INTERRUPTSTACK=2048
CONFIG_ARCH_IRQBUTTONS=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARDCTL_USBDEVCTRL=y
CONFIG_BOARD_LOOPSPERMSEC=8499
CONFIG_BUILTIN=y
CONFIG_CDCACM=y
CONFIG_CDCACM_CONSOLE=y
CONFIG_CL_MFRC522=y
CONFIG_DRIVERS_CONTACTLESS=y
CONFIG_EXAMPLES_BUTTONS=y
CONFIG_EXAMPLES_BUTTONS_NAME0="SW3"
CONFIG_EXAMPLES_BUTTONS_NAME1="SW4"
CONFIG_EXAMPLES_BUTTONS_NAME2="SW5"
CONFIG_EXAMPLES_BUTTONS_NAMES=y
CONFIG_EXAMPLES_BUTTONS_QTD=3
CONFIG_EXAMPLES_RFID_READUID=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INPUT=y
CONFIG_INPUT_BUTTONS=y
CONFIG_INPUT_BUTTONS_LOWER=y
CONFIG_INTELHEX_BINARY=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_LINELEN=64
CONFIG_NSH_READLINE=y
CONFIG_PREALLOC_TIMERS=4
CONFIG_RAM_SIZE=98304
CONFIG_RAM_START=0x20000000
CONFIG_RAW_BINARY=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=5
CONFIG_START_MONTH=5
CONFIG_START_YEAR=2014
CONFIG_STM32_JTAG_SW_ENABLE=y
CONFIG_STM32_OTGFS=y
CONFIG_STM32_PWR=y
CONFIG_STM32_SPI1=y
CONFIG_STM32_USART6=y
CONFIG_SYSTEM_NSH=y
CONFIG_TASK_NAME_SIZE=0
CONFIG_USBDEV=y

View File

@ -340,6 +340,10 @@ extern "C"
#define STM32_LCD_CS (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
GPIO_OUTPUT_SET|GPIO_PORTC|GPIO_PIN4)
/* MFRC522 */
#define GPIO_RFID_CS (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
GPIO_OUTPUT_SET|GPIO_PORTC|GPIO_PIN5)
/* LEDs
*
* The STM32F401RC-RS485 boards provide 4 blue user LEDs. LD1, LD2, LD3

View File

@ -67,6 +67,10 @@
#include "stm32_max7219_matrix.h"
#endif
#ifdef CONFIG_CL_MFRC522
#include "stm32_mfrc522.h"
#endif
#ifdef CONFIG_STEPPER_DRV8825
#include "stm32_drv8266.h"
#endif
@ -286,5 +290,13 @@ int stm32_bringup(void)
}
#endif
#ifdef CONFIG_CL_MFRC522
ret = stm32_mfrc522initialize("/dev/rfid0");
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: stm32_mfrc522initialize() failed: %d\n", ret);
}
#endif
return ret;
}

View File

@ -62,6 +62,10 @@ void weak_function stm32_spidev_initialize(void)
#ifdef CONFIG_LCD_MAX7219
stm32_configgpio(STM32_LCD_CS); /* MAX7219 chip select */
#endif
#ifdef CONFIG_CL_MFRC522
stm32_configgpio(GPIO_RFID_CS); /* MFRC522 chip select */
#endif
}
/****************************************************************************
@ -110,6 +114,13 @@ void stm32_spi1select(struct spi_dev_s *dev,
stm32_gpiowrite(STM32_LCD_CS, !selected);
}
#endif
#if defined(CONFIG_CL_MFRC522)
if (devid == SPIDEV_CONTACTLESS(0))
{
stm32_gpiowrite(GPIO_RFID_CS, !selected);
}
#endif
}
uint8_t stm32_spi1status(struct spi_dev_s *dev, uint32_t devid)