Add board support to test Microchip MCP2515 driver

This commit is contained in:
Alan Carvalho de Assis
2017-05-23 14:29:36 -03:00
parent eb7373cedf
commit 607b9b5a8c
7 changed files with 1727 additions and 0 deletions
+113
View File
@@ -0,0 +1,113 @@
############################################################################
# configs/stm32f103-minimum/nsh/Make.defs
#
# Copyright (C) 2016 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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.
#
############################################################################
include ${TOPDIR}/.config
include ${TOPDIR}/tools/Config.mk
include ${TOPDIR}/arch/arm/src/armv7-m/Toolchain.defs
LDSCRIPT = ld.script
ifeq ($(WINTOOL),y)
# Windows-native toolchains
DIRLINK = $(TOPDIR)/tools/copydir.sh
DIRUNLINK = $(TOPDIR)/tools/unlink.sh
MKDEP = $(TOPDIR)/tools/mkwindeps.sh
ARCHINCLUDES = -I. -isystem "${shell cygpath -w $(TOPDIR)/include}"
ARCHXXINCLUDES = -I. -isystem "${shell cygpath -w $(TOPDIR)/include}" -isystem "${shell cygpath -w $(TOPDIR)/include/cxx}"
ARCHSCRIPT = -T "${shell cygpath -w $(TOPDIR)/configs/$(CONFIG_ARCH_BOARD)/scripts/$(LDSCRIPT)}"
else
# Linux/Cygwin-native toolchain
MKDEP = $(TOPDIR)/tools/mkdeps$(HOSTEXEEXT)
ARCHINCLUDES = -I. -isystem $(TOPDIR)/include
ARCHXXINCLUDES = -I. -isystem $(TOPDIR)/include -isystem $(TOPDIR)/include/cxx
ARCHSCRIPT = -T$(TOPDIR)/configs/$(CONFIG_ARCH_BOARD)/scripts/$(LDSCRIPT)
endif
CC = $(CROSSDEV)gcc
CXX = $(CROSSDEV)g++
CPP = $(CROSSDEV)gcc -E
LD = $(CROSSDEV)ld
AR = $(CROSSDEV)ar rcs
NM = $(CROSSDEV)nm
OBJCOPY = $(CROSSDEV)objcopy
OBJDUMP = $(CROSSDEV)objdump
ARCHCCVERSION = ${shell $(CC) -v 2>&1 | sed -n '/^gcc version/p' | sed -e 's/^gcc version \([0-9\.]\)/\1/g' -e 's/[-\ ].*//g' -e '1q'}
ARCHCCMAJOR = ${shell echo $(ARCHCCVERSION) | cut -d'.' -f1}
ifeq ($(CONFIG_DEBUG_SYMBOLS),y)
ARCHOPTIMIZATION = -g
endif
ifneq ($(CONFIG_DEBUG_NOOPT),y)
ARCHOPTIMIZATION += $(MAXOPTIMIZATION) -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer
endif
ARCHCFLAGS = -fno-builtin
ARCHCXXFLAGS = -fno-builtin -fno-exceptions -fcheck-new
ARCHWARNINGS = -Wall -Wstrict-prototypes -Wshadow -Wundef
ARCHWARNINGSXX = -Wall -Wshadow -Wundef
ARCHDEFINES =
ARCHPICFLAGS = -fpic -msingle-pic-base -mpic-register=r10
CFLAGS = $(ARCHCFLAGS) $(ARCHWARNINGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRADEFINES) -pipe
CPICFLAGS = $(ARCHPICFLAGS) $(CFLAGS)
CXXFLAGS = $(ARCHCXXFLAGS) $(ARCHWARNINGSXX) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHXXINCLUDES) $(ARCHDEFINES) $(EXTRADEFINES) -pipe
CXXPICFLAGS = $(ARCHPICFLAGS) $(CXXFLAGS)
CPPFLAGS = $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRADEFINES)
AFLAGS = $(CFLAGS) -D__ASSEMBLY__
NXFLATLDFLAGS1 = -r -d -warn-common
NXFLATLDFLAGS2 = $(NXFLATLDFLAGS1) -T$(TOPDIR)/binfmt/libnxflat/gnu-nxflat-pcrel.ld -no-check-sections
LDNXFLATFLAGS = -e main -s 2048
ASMEXT = .S
OBJEXT = .o
LIBEXT = .a
EXEEXT =
ifneq ($(CROSSDEV),arm-nuttx-elf-)
LDFLAGS += -nostartfiles -nodefaultlibs
endif
ifeq ($(CONFIG_DEBUG_SYMBOLS),y)
LDFLAGS += -g
endif
HOSTCC = gcc
HOSTINCLUDES = -I.
HOSTCFLAGS = -Wall -Wstrict-prototypes -Wshadow -Wundef -g -pipe
HOSTLDFLAGS =
File diff suppressed because it is too large Load Diff
+4
View File
@@ -69,6 +69,10 @@ ifeq ($(CONFIG_AUDIO_TONE),y)
CSRCS += stm32_tone.c
endif
ifeq ($(CONFIG_CAN_MCP2515),y)
CSRCS += stm32_mcp2515.c
endif
ifeq ($(CONFIG_CL_MFRC522),y)
CSRCS += stm32_mfrc522.c
endif
@@ -151,6 +151,13 @@ int stm32_bringup(void)
}
#endif
#ifdef CONFIG_CAN_MCP2515
ret = stm32_mcp2515initialize("/dev/can0");
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: stm32_mcp2515initialize() failed: %d\n", ret);
}
#endif
#ifdef CONFIG_CL_MFRC522
ret = stm32_mfrc522initialize("/dev/rfid0");
@@ -0,0 +1,240 @@
/************************************************************************************
* configs/stm32f4discovery/src/stm32_mcp2515.c
*
* Copyright (C) 2015 Alan Carvalho de Assis. All rights reserved.
* Author: Alan Carvalho de Assis <acassis@gmail.com>
*
* 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 <errno.h>
#include <debug.h>
#include <nuttx/spi/spi.h>
#include <nuttx/can/mcp2515.h>
#include "stm32.h"
#include "stm32_spi.h"
#include "stm32f103_minimum.h"
#if defined(CONFIG_SPI) && defined(CONFIG_STM32_SPI1) && defined(CONFIG_CAN_MCP2515)
/************************************************************************************
* Pre-processor Definitions
************************************************************************************/
#define MCP2515_SPI_PORTNO 1 /* On SPI1 */
/****************************************************************************
* Private Types
****************************************************************************/
struct stm32_mcp2515config_s
{
/* Configuration structure as seen by the MCP2515 driver */
struct mcp2515_config_s config;
/* Additional private definitions only known to this driver */
MCP2515_HANDLE handle; /* The MCP2515 driver handle */
mcp2515_handler_t handler; /* The MCP2515 interrupt handler */
FAR void *arg; /* Argument to pass to the interrupt handler */
};
/****************************************************************************
* Static Function Prototypes
****************************************************************************/
/* IRQ/GPIO access callbacks. These operations all hidden behind callbacks
* to isolate the MCP2515 driver from differences in GPIO interrupt handling
* by varying boards and MCUs.
*
* attach - Attach the MCP2515 interrupt handler to the GPIO interrupt
*/
static int mcp2515_attach(FAR struct mcp2515_config_s *state,
mcp2515_handler_t handler, FAR void *arg);
/****************************************************************************
* Private Data
****************************************************************************/
/* A reference to a structure of this type must be passed to the MCP2515
* driver. This structure provides information about the configuration
* of the MCP2515 and provides some board-specific hooks.
*
* Memory for this structure is provided by the caller. It is not copied
* by the driver and is presumed to persist while the driver is active. The
* memory must be writable because, under certain circumstances, the driver
* may modify frequency or X plate resistance values.
*/
static struct stm32_mcp2515config_s g_mcp2515config =
{
.config =
{
.devid = 0,
.nfilters = 6,
.ntxbuffers = 3,
.attach = mcp2515_attach,
},
};
/****************************************************************************
* Private Functions
****************************************************************************/
/* This is the MCP2515 Interupt handler */
int mcp2515_interrupt(int irq, FAR void *context)
{
/* Verify that we have a handler attached */
if (g_mcp2515config.handler)
{
/* Yes.. forward with interrupt along with its argument */
g_mcp2515config.handler(&g_mcp2515config.config, g_mcp2515config.arg);
}
return OK;
}
static int mcp2515_attach(FAR struct mcp2515_config_s *state,
mcp2515_handler_t handler, FAR void *arg)
{
FAR struct stm32_mcp2515config_s *priv =
(FAR struct stm32_mcp2515config_s *) state;
irqstate_t flags;
caninfo("Saving handle %p\n", handler);
flags = enter_critical_section();
priv->handler = handler;
priv->arg = arg;
/* Configure the interrupt for falling edge*/
(void)stm32_gpiosetevent(GPIO_MCP2515_IRQ, false, true, false, mcp2515_interrupt, NULL);
leave_critical_section(flags);
return OK;
}
/************************************************************************************
* Public Functions
************************************************************************************/
/************************************************************************************
* Name: stm32_mcp2515initialize
*
* Description:
* Initialize and register the MCP2515 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_mcp2515initialize(FAR const char *devpath)
{
FAR struct spi_dev_s *spi;
FAR struct can_dev_s *can;
FAR struct mcp2515_can_s *mcp2515;
int ret;
/* Check if we are already initialized */
if (!g_mcp2515config.handle)
{
sninfo("Initializing\n");
/* Configure the MCP2515 interrupt pin as an input */
(void)stm32_configgpio(GPIO_MCP2515_IRQ);
spi = stm32_spibus_initialize(MCP2515_SPI_PORTNO);
if (!spi)
{
return -ENODEV;
}
/* Save the SPI instance in the mcp2515_config_s structure */
g_mcp2515config.config.spi = spi;
/* Instantiate the MCP2515 CAN Driver */
mcp2515 = mcp2515_instantiate(&g_mcp2515config.config);
if (mcp2515 == NULL)
{
canerr("ERROR: Failed to get MCP2515 Driver Loaded\n");
return -ENODEV;
}
/* Save the opaque structure */
g_mcp2515config.handle = (MCP2515_HANDLE) mcp2515;
/* Initialize the CAN Device with the MCP2515 operations */
can = mcp2515_initialize(mcp2515);
if (can == NULL)
{
canerr("ERROR: Failed to get CAN interface\n");
return -ENODEV;
}
/* Register the CAN driver at "/dev/can0" */
ret = can_register(devpath, can);
if (ret < 0)
{
canerr("ERROR: can_register failed: %d\n", ret);
return ret;
}
}
return OK;
}
#endif /* CONFIG_SPI && CONFIG_CAN_MCP2515 */
+11
View File
@@ -74,6 +74,10 @@ void stm32_spidev_initialize(void)
* architecture.
*/
#ifdef CONFIG_CAN_MCP2515
(void)stm32_configgpio(GPIO_MCP2515_CS); /* MCP2515 chip select */
#endif
#ifdef CONFIG_CL_MFRC522
(void)stm32_configgpio(GPIO_CS_MFRC522); /* MFRC522 chip select */
#endif
@@ -120,6 +124,13 @@ void stm32_spidev_initialize(void)
void stm32_spi1select(FAR struct spi_dev_s *dev, uint32_t devid,
bool selected)
{
#if defined(CONFIG_CAN_MCP2515)
if (devid == SPIDEV_CANBUS(0))
{
stm32_gpiowrite(GPIO_MCP2515_CS, !selected);
}
#endif
#if defined(CONFIG_CL_MFRC522)
if (devid == SPIDEV_WIRELESS(0))
{
@@ -86,6 +86,9 @@
#define STM32_LCD_CS (GPIO_OUTPUT|GPIO_CNF_OUTPP|GPIO_MODE_50MHz|\
GPIO_OUTPUT_SET|GPIO_PORTA|GPIO_PIN4)
#define GPIO_MCP2515_CS (GPIO_OUTPUT|GPIO_CNF_OUTPP|GPIO_MODE_50MHz|\
GPIO_OUTPUT_SET|GPIO_PORTA|GPIO_PIN4)
#define GPIO_NRF24L01_CS (GPIO_OUTPUT|GPIO_CNF_OUTPP|GPIO_MODE_50MHz|\
GPIO_OUTPUT_SET|GPIO_PORTA|GPIO_PIN4)
@@ -114,6 +117,10 @@
#define GPIO_NRF24L01_IRQ (GPIO_INPUT|GPIO_CNF_INFLOAT|GPIO_PORTA|GPIO_PIN0)
/* MCP2515 IRQ line: PB.0 */
#define GPIO_MCP2515_IRQ (GPIO_INPUT|GPIO_CNF_INFLOAT|GPIO_PORTB|GPIO_PIN0)
/* USB Soft Connect Pullup: PC.13 */
#define GPIO_USB_PULLUP (GPIO_OUTPUT|GPIO_CNF_OUTPP|GPIO_MODE_50MHz|\