Add ENCX24J600 Ethernet driver and support for the ENCX24J600 with the Olimex STM32 P107 board. From Max Holtberg

This commit is contained in:
Gregory Nutt 2013-08-25 11:21:54 -06:00
parent 960227799f
commit ddce6bb638
5 changed files with 456 additions and 4 deletions

View File

@ -37,15 +37,19 @@
CFLAGS += -I$(TOPDIR)/sched
ASRCS =
ASRCS =
AOBJS = $(ASRCS:.S=$(OBJEXT))
CSRCS = up_boot.c
CSRCS = up_boot.c up_spi.c
ifeq ($(CONFIG_CAN),y)
CSRCS += up_can.c
endif
ifeq ($(CONFIG_ENCX24J600),y)
CSRCS += up_encx24j600.c
endif
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)

View File

@ -0,0 +1,88 @@
/******************************************************************************
* configs/olimex-stm32-p107/src/p107-internal.h
*
* Copyright (C) 2013 Max Holtzberg. All rights reserved.
* Author: Max Holtzberg <mholtzberg@uvc-ingenieure.de>
*
* 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 __CONFIGS_OLIMEX_STM32_P107_SRC_INTERNAL_H
#define __CONFIGS_OLIMEX_STM32_P107_SRC_INTERNAL_H
/******************************************************************************
* Included Files
******************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <stdint.h>
/******************************************************************************
* Definitions
******************************************************************************/
/* ENCX24J600
*
* --- ------ -------------- ---------------------------------------------------
* PIN NAME SIGNAL NOTES
* --- ------ -------------- ---------------------------------------------------
*
* 54 PB15 PB15-CS_UEXT ENCX24J600 #CS
* 78 PC10 PC10-SPI3-SCK ENCX24J600 SCK
* 79 PC11 PC11-SPI3-MISO ENCX24J600 MISO
* 80 PC12 PC12-SPI3-MOSI ENCX24J600 MOSI
* 95 PB8 PB8 ENCX24J600 #Interrupt
*/
#ifdef CONFIG_ENCX24J600
# define GPIO_ENCX24J600_CS (GPIO_OUTPUT|GPIO_CNF_OUTPP|GPIO_MODE_50MHz| \
GPIO_OUTPUT_SET|GPIO_PORTB|GPIO_PIN15)
# define GPIO_ENCX24J600_INTR (GPIO_INPUT|GPIO_CNF_INFLOAT|GPIO_MODE_INPUT| \
GPIO_EXTI|GPIO_PORTB|GPIO_PIN8)
#endif
#ifndef __ASSEMBLY__
/************************************************************************************
* Public Functions
************************************************************************************/
/************************************************************************************
* Name: stm32_spiinitialize
*
* Description:
* Called to configure SPI chip select GPIO pins for the M3 Wildfire board.
*
************************************************************************************/
void weak_function stm32_spiinitialize(void);
#endif /* __ASSEMBLY__ */
#endif /* __CONFIGS_OLIMEX_STM32_P107_SRC_INTERNAL_H */

View File

@ -39,12 +39,11 @@
************************************************************************************/
#include <nuttx/config.h>
#include <debug.h>
#include <arch/board/board.h>
#include "up_arch.h"
#include "p107-internal.h"
/************************************************************************************
* Pre-processor Definitions
@ -70,4 +69,15 @@
void stm32_boardinitialize(void)
{
/* Configure SPI chip selects if 1) SPI is not disabled, and 2) the weak function
* stm32_spiinitialize() has been brought into the link.
*/
#if defined(CONFIG_STM32_SPI3)
if (stm32_spiinitialize)
{
stm32_spiinitialize();
}
#endif
}

View File

@ -0,0 +1,196 @@
/****************************************************************************
* configs/olimex-stm32-p107/src/up_encx24j600.c
*
* Copyright (C) 2012 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdio.h>
#include <debug.h>
#include <nuttx/spi/spi.h>
#include <nuttx/net/encx24j600.h>
#include <arch/board/board.h>
#include "chip.h"
#include "up_arch.h"
#include "up_internal.h"
#include "p107-internal.h"
#ifdef CONFIG_ENCX24J600
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* ENCX24J600
*
* --- ------ -------------- ------------------------------------------------
* PIN NAME SIGNAL NOTES
* --- ------ -------------- ------------------------------------------------
*
* 54 PB15 PB15-CS_UEXT ENCX24J600 #CS
* 78 PC10 PC10-SPI3-SCK ENCX24J600 SCK
* 79 PC11 PC11-SPI3-MISO ENCX24J600 MISO
* 80 PC12 PC12-SPI3-MOSI ENCX24J600 MOSI
* 95 PB8 PB8 ENCX24J600 #Interrupt
*/
/* ENCX24J600 is on SPI3 */
#ifndef CONFIG_STM32_SPI3
# error "Need CONFIG_STM32_SPI3 in the configuration"
#endif
/* SPI Assumptions **********************************************************/
#define ENCX24J600_SPI_PORTNO 3 /* On SPI1 */
#define ENCX24J600_DEVNO 0 /* Only one ENCX24J600 */
/****************************************************************************
* Private Types
****************************************************************************/
struct stm32_lower_s
{
const struct enc_lower_s lower; /* Low-level MCU interface */
xcpt_t handler; /* ENCX24J600 interrupt handler */
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int up_attach(FAR const struct enc_lower_s *lower, xcpt_t handler);
static void up_enable(FAR const struct enc_lower_s *lower);
static void up_disable(FAR const struct enc_lower_s *lower);
/****************************************************************************
* Private Data
****************************************************************************/
/* The ENCX24J600 normal provides interrupts to the MCU via a GPIO pin. The
* following structure provides an MCU-independent mechanixm for controlling
* the ENCX24J600 GPIO interrupt.
*/
static struct stm32_lower_s g_enclower =
{
.lower =
{
.attach = up_attach,
.enable = up_enable,
.disable = up_disable
},
.handler = NULL,
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: struct enc_lower_s methods
****************************************************************************/
static int up_attach(FAR const struct enc_lower_s *lower, xcpt_t handler)
{
FAR struct stm32_lower_s *priv = (FAR struct stm32_lower_s *)lower;
/* Just save the handler for use when the interrupt is enabled */
priv->handler = handler;
return OK;
}
static void up_enable(FAR const struct enc_lower_s *lower)
{
FAR struct stm32_lower_s *priv = (FAR struct stm32_lower_s *)lower;
DEBUGASSERT(priv->handler);
(void)stm32_gpiosetevent(GPIO_ENCX24J600_INTR, false, true, true, priv->handler);
}
static void up_disable(FAR const struct enc_lower_s *lower)
{
(void)stm32_gpiosetevent(GPIO_ENCX24J600_INTR, false, true, true, NULL);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_netinitialize
****************************************************************************/
void up_netinitialize(void)
{
FAR struct spi_dev_s *spi;
int ret;
/* Assumptions:
* 1) ENCX24J600 pins were configured in up_spi.c early in the boot-up phase.
* 2) Clocking for the SPI1 peripheral was also provided earlier in boot-up.
*/
spi = up_spiinitialize(ENCX24J600_SPI_PORTNO);
if (!spi)
{
nlldbg("Failed to initialize SPI port %d\n", ENCX24J600_SPI_PORTNO);
return;
}
/* Bind the SPI port to the ENCX24J600 driver */
ret = enc_initialize(spi, &g_enclower.lower, ENCX24J600_DEVNO);
if (ret < 0)
{
nlldbg("Failed to bind SPI port %d ENCX24J600 device %d: %d\n",
ENCX24J600_SPI_PORTNO, ENCX24J600_DEVNO, ret);
return;
}
nllvdbg("Bound SPI port %d to ENCX24J600 device %d\n",
ENCX24J600_SPI_PORTNO, ENCX24J600_DEVNO);
}
#endif /* CONFIG_ENCX24J600 */

View File

@ -0,0 +1,154 @@
/************************************************************************************
* configs/olimex-stm32-p107/src/up_spi.c
*
* Copyright (C) 2013 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.
*
************************************************************************************/
/************************************************************************************
* Included Files
************************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <debug.h>
#include <nuttx/spi/spi.h>
#include <arch/board/board.h>
#include "up_arch.h"
#include "chip.h"
#include "stm32.h"
#include "p107-internal.h"
#if defined(CONFIG_STM32_SPI3)
/************************************************************************************
* Definitions
************************************************************************************/
/* Enables debug output from this file (needs CONFIG_DEBUG too) */
#undef SPI_DEBUG /* Define to enable debug */
#undef SPI_VERBOSE /* Define to enable verbose debug */
#ifdef SPI_DEBUG
# define spidbg lldbg
# ifdef SPI_VERBOSE
# define spivdbg lldbg
# else
# define spivdbg(x...)
# endif
#else
# undef SPI_VERBOSE
# define spidbg(x...)
# define spivdbg(x...)
#endif
/************************************************************************************
* Private Functions
************************************************************************************/
/************************************************************************************
* Public Functions
************************************************************************************/
/************************************************************************************
* Name: stm32_spiinitialize
*
* Description:
* Called to configure SPI chip select GPIO pins for the Olimex stm32-p107 board.
*
************************************************************************************/
void weak_function stm32_spiinitialize(void)
{
/* NOTE: Clocking for SPI3 was already provided in stm32_rcc.c.
* Configurations of SPI pins is performed in stm32_spi.c.
* Here, we only initialize chip select pins unique to the board
* architecture.
*/
/* Configure ENCX24J600 SPI1 CS (also RESET and interrupt pins) */
#if defined(CONFIG_ENCX24J600) && defined(CONFIG_STM32_SPI3)
stm32_configgpio(GPIO_ENCX24J600_CS);
stm32_configgpio(GPIO_ENCX24J600_INTR);
#endif
}
/****************************************************************************
* Name: stm32_spi1/2/3select and stm32_spi1/2/3status
*
* Description:
* The external functions, stm32_spi1/2/3select and stm32_spi1/2/3status must be
* provided by board-specific logic. They are implementations of the select
* and status methods of the SPI interface defined by struct spi_ops_s (see
* include/nuttx/spi/spi.h). All other methods (including up_spiinitialize())
* are provided by common STM32 logic. To use this common SPI logic on your
* board:
*
* 1. Provide logic in stm32_boardinitialize() to configure SPI chip select
* pins.
* 2. Provide stm32_spi1/2/3select() and stm32_spi1/2/3status() functions in your
* board-specific logic. These functions will perform chip selection and
* status operations using GPIOs in the way your board is configured.
* 3. Add a calls to up_spiinitialize() in your low level application
* initialization logic
* 4. The handle returned by up_spiinitialize() may then be used to bind the
* SPI driver to higher level logic (e.g., calling
* mmcsd_spislotinitialize(), for example, will bind the SPI driver to
* the SPI MMC/SD driver).
*
****************************************************************************/
#ifdef CONFIG_STM32_SPI3
void stm32_spi3select(FAR struct spi_dev_s *dev, enum spi_dev_e devid, bool selected)
{
spidbg("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert");
if (devid == SPIDEV_ETHERNET)
{
/* Set the GPIO low to select and high to de-select */
stm32_gpiowrite(GPIO_ENCX24J600_CS, !selected);
}
}
uint8_t stm32_spi3status(FAR struct spi_dev_s *dev, enum spi_dev_e devid)
{
return SPI_STATUS_PRESENT;
}
#endif
#endif /* CONFIG_STM32_SPI3 */