esp32: Add board config to support MCP2515

This commit is contained in:
Alan C. Assis 2021-03-03 16:47:24 -03:00 committed by Abdelatif Guettouche
parent 296d94b5cb
commit 29af35faef
8 changed files with 440 additions and 2 deletions

View File

@ -0,0 +1,83 @@
/****************************************************************************
* boards/xtensa/esp32/common/src/esp32_mcp2515.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_XTENSA_ESP32_COMMON_INCLUDE_BOARD_MCP2515_H
#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_MCP2515_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Type Definitions
****************************************************************************/
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Inline Functions
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: board_mcp2515_initialize
*
* Description:
* Initialize and register the MCP2515 CAN driver.
*
* Input Parameters:
* devpath - The full path to the driver to register. E.g., "/dev/can0"
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int board_mcp2515_initialize(int devno);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_BOARD_BMP180_H */

View File

@ -34,6 +34,10 @@ ifeq ($(CONFIG_SENSORS_BMP180),y)
CSRCS += esp32_bmp180.c
endif
ifeq ($(CONFIG_CAN_MCP2515),y)
CSRCS += esp32_mcp2515.c
endif
DEPPATH += --dep-path src
VPATH += :src
CFLAGS += $(shell $(INCDIR) "$(CC)" $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src)
CFLAGS += $(shell $(INCDIR) "$(CC)" $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src)

View File

@ -0,0 +1,256 @@
/****************************************************************************
* boards/xtensa/esp32/esp32devkitc/src/esp32_mcp2515.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 <stdio.h>
#include <debug.h>
#include <nuttx/spi/spi.h>
#include <nuttx/can/mcp2515.h>
#include "esp32_spi.h"
#include "esp32-devkitc.h"
#include "esp32_gpio.h"
#include "hardware/esp32_gpio_sigmap.h"
#if defined(CONFIG_SPI) && defined(CONFIG_ESP32_SPI3) && \
defined(CONFIG_CAN_MCP2515)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MCP2515_SPI_PORTNO 3 /* On SPI3 */
#if !defined(CONFIG_ESP32_GPIO_IRQ)
# error "GPIO interrupts aren't enabled and it is required"
#endif
/****************************************************************************
* Private Types
****************************************************************************/
struct esp32_mcp2515config_s
{
/* Configuration structure as seen by the MCP2515 driver */
struct mcp2515_config_s config;
/* Additional private definitions only known to this driver */
FAR struct mcp2515_can_s *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 esp32_mcp2515config_s g_mcp2515config =
{
.config =
{
.spi = NULL,
.baud = 0,
.btp = 0,
.devid = 0,
.mode = 0,
.nfilters = 6,
#ifdef MCP2515_LOOPBACK
.loopback = false;
#endif
.attach = mcp2515_attach,
},
};
/****************************************************************************
* Private Functions
****************************************************************************/
/* This is the MCP2515 Interrupt handler */
int mcp2515_interrupt(int irq, FAR void *context, FAR void *arg)
{
FAR struct esp32_mcp2515config_s *priv =
(FAR struct esp32_mcp2515config_s *)arg;
DEBUGASSERT(priv != NULL);
/* Verify that we have a handler attached */
if (priv->handler)
{
/* Yes.. forward with interrupt along with its argument */
priv->handler(&priv->config, priv->arg);
}
return OK;
}
static int mcp2515_attach(FAR struct mcp2515_config_s *state,
mcp2515_handler_t handler, FAR void *arg)
{
FAR struct esp32_mcp2515config_s *priv =
(FAR struct esp32_mcp2515config_s *)state;
irqstate_t flags;
int irq = ESP32_PIN2IRQ(GPIO_MCP2515_IRQ);
int ret;
caninfo("Saving handler %p\n", handler);
flags = enter_critical_section();
priv->handler = handler;
priv->arg = arg;
/* Configure the interrupt */
esp32_gpioirqdisable(irq);
ret = irq_attach(irq, mcp2515_interrupt, priv);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: gpint_attach() failed: %d\n", ret);
return ret;
}
leave_critical_section(flags);
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_mcp2515_initialize
*
* Description:
* Initialize and register the MCP2515 CAN driver.
*
* Input Parameters:
* devpath - The full path to the driver to register. E.g., "/dev/can0"
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int board_mcp2515_initialize(int devno)
{
FAR struct spi_dev_s *spi;
FAR struct can_dev_s *can;
FAR struct mcp2515_can_s *mcp2515;
char devpath[10];
int ret;
/* Check if we are already initialized */
if (!g_mcp2515config.handle)
{
sninfo("Initializing\n");
/* Configure the MCP2515 interrupt pin as an input */
esp32_configgpio(GPIO_MCP2515_IRQ, INPUT_FUNCTION_3 | PULLDOWN);
spi = esp32_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;
/* 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;
}
/* Create the '/dev/canX' name based on 'devno' */
snprintf(devpath, 10, "/dev/can%d", devno);
/* 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 */

View File

@ -796,6 +796,17 @@ NOTES:
crash in the waitpid() test. At the time of the crash, there is
extensive memory corruption and a user exception occurs (cause=28).
mcp2515:
This config is used to communicate with MCP2515 CAN over SPI chip.
SPI3 is used and kept with the default IOMUX pins, i.e.:
CS --> 5
SCK --> 18
MOSI --> 23
MISO --> 19
The MCP2515 interrupt (INT) pin is connected to the pin 22 of the
ESP32-Devkit.
mmcsdspi:
This config tests the SPI driver by connecting an SD Card reader over SPI.

View File

@ -0,0 +1,55 @@
#
# 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_LEDS is not set
# CONFIG_ESP32_SPI3_DMA is not set
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
# CONFIG_NSH_CMDPARMS is not set
CONFIG_ARCH="xtensa"
CONFIG_ARCH_BOARD="esp32-devkitc"
CONFIG_ARCH_BOARD_ESP32_DEVKITC=y
CONFIG_ARCH_CHIP="esp32"
CONFIG_ARCH_CHIP_ESP32=y
CONFIG_ARCH_CHIP_ESP32WROVER=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_CAN=y
CONFIG_CAN_MCP2515=y
CONFIG_CAN_TXREADY=y
CONFIG_ESP32_SPI3=y
CONFIG_ESP32_UART0=y
CONFIG_EXAMPLES_CAN=y
CONFIG_EXAMPLES_CAN_READ=y
CONFIG_FS_PROCFS=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INTELHEX_BINARY=y
CONFIG_MAX_TASKS=16
CONFIG_MM_REGIONS=2
CONFIG_NFILE_DESCRIPTORS=8
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=114688
CONFIG_RAM_START=0x20000000
CONFIG_RAW_BINARY=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_WAITPID=y
CONFIG_SDCLONE_DISABLE=y
CONFIG_START_DAY=6
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2011
CONFIG_SYSTEM_NSH=y
CONFIG_UART0_SERIAL_CONSOLE=y
CONFIG_USER_ENTRYPOINT="nsh_main"

View File

@ -55,7 +55,11 @@
* This is an externally connected LED used for testing.
*/
#define GPIO_LED1 2
#define GPIO_LED1 2
/* MCP2515 Interrupt pin */
#define GPIO_MCP2515_IRQ 22
/****************************************************************************
* Public Types

View File

@ -62,6 +62,10 @@
# include <nuttx/leds/userled.h>
#endif
#ifdef CONFIG_CAN_MCP2515
# include "esp32_mcp2515.h"
#endif
#ifdef CONFIG_TIMER
# include "esp32_board_tim.h"
#endif
@ -247,6 +251,16 @@ int esp32_bringup(void)
}
#endif
#ifdef CONFIG_CAN_MCP2515
/* Configure and initialize the MCP2515 CAN device */
ret = board_mcp2515_initialize(0);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: board_mcp2515_initialize() failed: %d\n", ret);
}
#endif
#ifdef CONFIG_WATCHDOG
/* Configure watchdog timer */

View File

@ -54,3 +54,14 @@ uint8_t esp32_spi2_status(FAR struct spi_dev_s *dev, uint32_t devid)
return status;
}
/****************************************************************************
* Name: esp32_spi3_status
****************************************************************************/
uint8_t esp32_spi3_status(FAR struct spi_dev_s *dev, uint32_t devid)
{
uint8_t status = 0;
return status;
}