Please apply attached patch add support to ENC28J60 in the
STM32F4Discovery board.

BR,

Alan
From a68a785cb0d4914c75a702704e8060c0cfb4210b Mon Sep 17 00:00:00 2001
From: Alan Carvalho de Assis <engenhari...@siam.ind.br>
Date: Sun, 9 Feb 2020 10:16:37 -0300
Subject: [PATCH] Add Ethernet SPI ENC28J60 board support to STM32F4Discovery

---
 boards/arm/stm32/stm32f4discovery/README.txt  |  18 ++
 .../arm/stm32/stm32f4discovery/src/Makefile   |   4 +
 .../stm32f4discovery/src/stm32_enc28j60.c     | 225 ++++++++++++++++++
 .../stm32/stm32f4discovery/src/stm32_spi.c    |  15 ++
 .../stm32f4discovery/src/stm32f4discovery.h   |   9 +
 5 files changed, 271 insertions(+)
 create mode 100644 boards/arm/stm32/stm32f4discovery/src/stm32_enc28j60.c

diff --git a/boards/arm/stm32/stm32f4discovery/README.txt b/boards/arm/stm32/stm32f4discovery/README.txt
index 9022220390..0d4e1cf932 100644
--- a/boards/arm/stm32/stm32f4discovery/README.txt
+++ b/boards/arm/stm32/stm32f4discovery/README.txt
@@ -42,6 +42,7 @@ Contents
   - SSD1289
   - UG-2864AMBAG01 / UG-2864HSWEG01
   - NiceRF LoRa (2AD66-LoRa V2)
+  - Ethernet SPI Module ENC28J60
   - HCI UART
   - STM32F4Discovery-specific Configuration Options
   - BASIC
@@ -652,6 +653,23 @@ First connect the GND and VCC (to 3.3V) and then connect the SCK label to PA5,
 connnect the MISO to PA6, connect the MOSI to PA7, connect the NSS to PD8,
 connect DIO0 to PD0 and finally connect NRESET to PD4.
 
+Ethernet SPI Module ENC28J60
+============================
+
+You can use an external Ethernet SPI Module ENC28J60 with STM32F4Discovery board.
+
+First connect the GND and VCC (to 3.3V). Note: according with ENC28J60 datasheet
+the Operating Voltage should be between 3.1V to 3.6V, but STM32F4Discover only
+supply 3.0V. You can modify your board to supply 3.3V: just remove the D3 diode
+and short-circuit the board pads where it was soldered).
+
+Connect the SCK label to PA5, connnect the SO to PA6, connect the SI to PA7,
+connect the CS to PA4, connect RST to PE1 and finally connect INT to PE4.
+
+The next step is to enable the ENC28J60 in the menuconfig ("make menuconfig")
+and the necessary Network configuration, you can use the
+boards/arm/stm32/fire-stm32v2/configs/nsh/defconfig as reference.
+
 HCI UART
 ========
 
diff --git a/boards/arm/stm32/stm32f4discovery/src/Makefile b/boards/arm/stm32/stm32f4discovery/src/Makefile
index d242420ba3..86840217b9 100644
--- a/boards/arm/stm32/stm32f4discovery/src/Makefile
+++ b/boards/arm/stm32/stm32f4discovery/src/Makefile
@@ -81,6 +81,10 @@ ifeq ($(CONFIG_LCD_ST7567),y)
 CSRCS += stm32_st7567.c
 endif
 
+ifeq ($(CONFIG_ENC28J60),y)
+CSRCS += stm32_enc28j60.c
+endif
+
 ifeq ($(CONFIG_LPWAN_SX127X),y)
 CSRCS += stm32_sx127x.c
 endif
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_enc28j60.c b/boards/arm/stm32/stm32f4discovery/src/stm32_enc28j60.c
new file mode 100644
index 0000000000..cc14d75e13
--- /dev/null
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32_enc28j60.c
@@ -0,0 +1,225 @@
+/****************************************************************************
+ * boards/arm/stm32/fire-stm32v2/src/stm32_enc28j60.c
+ *
+ *   Copyright (C) 2012, 2017 Gregory Nutt. All rights reserved.
+ *   Author: Gregory Nutt <gn...@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.
+ *
+ ****************************************************************************/
+
+/* 2MBit SPI FLASH OR ENC28J60
+ *
+ * --- ------ -------------- -----------------------------------------------------
+ * PIN NAME   SIGNAL         NOTES
+ * --- ------ -------------- -----------------------------------------------------
+ *
+ * 29  PA4    PA4-SPI1-NSS   10Mbit ENC28J60, SPI 2M FLASH
+ * 30  PA5    PA5-SPI1-SCK   2.4" TFT + Touchscreen, 10Mbit ENC28J60, SPI 2M FLASH
+ * 31  PA6    PA6-SPI1-MISO  2.4" TFT + Touchscreen, 10Mbit ENC28J60, SPI 2M FLASH
+ * 32  PA7    PA7-SPI1-MOSI  2.4" TFT + Touchscreen, 10Mbit ENC28J60, SPI 2M FLASH
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <stdio.h>
+#include <debug.h>
+
+#include <nuttx/spi/spi.h>
+#include <nuttx/net/enc28j60.h>
+
+#include <arch/board/board.h>
+
+#include "chip.h"
+#include "up_arch.h"
+#include "up_internal.h"
+#include "stm32_spi.h"
+
+#include "stm32f4discovery.h"
+
+#ifdef CONFIG_ENC28J60
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+/* Configuration ************************************************************/
+/* ENC28J60
+ *
+ * --- ------ -------------- -----------------------------------------------------
+ * PIN NAME   SIGNAL         NOTES
+ * --- ------ -------------- -----------------------------------------------------
+ *
+ * 29  PA4    PA4-SPI1-NSS   10Mbit ENC28J60, SPI 2M FLASH
+ * 30  PA5    PA5-SPI1-SCK   2.4" TFT + Touchscreen, 10Mbit ENC28J60, SPI 2M FLASH
+ * 31  PA6    PA6-SPI1-MISO  2.4" TFT + Touchscreen, 10Mbit ENC28J60, SPI 2M FLASH
+ * 32  PA7    PA7-SPI1-MOSI  2.4" TFT + Touchscreen, 10Mbit ENC28J60, SPI 2M FLASH
+ * 98  PE1    PE1-FSMC_NBL1  2.4" TFT + Touchscreen, 10Mbit EN28J60 Reset
+ * 4   PE5    (no name)      10Mbps ENC28J60 Interrupt
+ */
+
+/* ENC28J60 is on SPI1 */
+
+#ifndef CONFIG_STM32_SPI1
+# error "Need CONFIG_STM32_SPI1 in the configuration"
+#endif
+
+/* SPI Assumptions **********************************************************/
+
+#define ENC28J60_SPI_PORTNO 1   /* On SPI1 */
+#define ENC28J60_DEVNO      0   /* Only one ENC28J60 */
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct stm32_lower_s
+{
+  const struct enc_lower_s lower;    /* Low-level MCU interface */
+  xcpt_t                   handler;  /* ENC28J60 interrupt handler */
+  FAR void                *arg;      /* Argument that accompanies the interrupt */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int  up_attach(FAR const struct enc_lower_s *lower, xcpt_t handler,
+                      FAR void *arg);
+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 ENC28J60 normal provides interrupts to the MCU via a GPIO pin.  The
+ * following structure provides an MCU-independent mechanixm for controlling
+ * the ENC28J60 GPIO interrupt.
+ */
+
+static struct stm32_lower_s g_enclower =
+{
+  .lower =
+  {
+    .attach  = up_attach,
+    .enable  = up_enable,
+    .disable = up_disable
+  },
+  .handler = NULL,
+  .arg     = NULL
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: struct enc_lower_s methods
+ ****************************************************************************/
+
+static int up_attach(FAR const struct enc_lower_s *lower, xcpt_t handler,
+                     FAR void *arg)
+{
+  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;
+  priv->arg     = arg;
+  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);
+  stm32_gpiosetevent(GPIO_ENC28J60_INTR, false, true, true,
+                     priv->handler, priv->arg);
+}
+
+/* REVISIT:  Since the interrupt is completely torn down, not just disabled,
+ * in interrupt requests that occurs while the interrupt is disabled will be
+ * lost.
+ */
+
+static void up_disable(FAR const struct enc_lower_s *lower)
+{
+  stm32_gpiosetevent(GPIO_ENC28J60_INTR, false, true, true,
+                     NULL, NULL);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: up_netinitialize
+ ****************************************************************************/
+
+void up_netinitialize(void)
+{
+  FAR struct spi_dev_s *spi;
+  int ret;
+
+  /* Assumptions:
+   * 1) ENC28J60 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 = stm32_spibus_initialize(ENC28J60_SPI_PORTNO);
+  if (!spi)
+    {
+      nerr("ERROR: Failed to initialize SPI port %d\n", ENC28J60_SPI_PORTNO);
+      return;
+    }
+
+  /* Take ENC28J60 out of reset (active low)*/
+
+  stm32_gpiowrite(GPIO_ENC28J60_RESET, true);
+
+  /* Bind the SPI port to the ENC28J60 driver */
+
+  ret = enc_initialize(spi, &g_enclower.lower, ENC28J60_DEVNO);
+  if (ret < 0)
+    {
+      nerr("ERROR: Failed to bind SPI port %d ENC28J60 device %d: %d\n",
+           ENC28J60_SPI_PORTNO, ENC28J60_DEVNO, ret);
+      return;
+    }
+
+  ninfo("Bound SPI port %d to ENC28J60 device %d\n",
+        ENC28J60_SPI_PORTNO, ENC28J60_DEVNO);
+}
+
+#endif /* CONFIG_ENC28J60 */
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_spi.c b/boards/arm/stm32/stm32f4discovery/src/stm32_spi.c
index cbdf86ab3d..0688f3570e 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32_spi.c
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32_spi.c
@@ -69,6 +69,12 @@
 
 void weak_function stm32_spidev_initialize(void)
 {
+#ifdef CONFIG_ENC28J60
+  stm32_configgpio(GPIO_ENC28J60_CS);
+  stm32_configgpio(GPIO_ENC28J60_RESET);
+  stm32_configgpio(GPIO_ENC28J60_INTR);
+#endif
+
 #ifdef CONFIG_STM32_SPI1
   stm32_configgpio(GPIO_CS_MEMS);    /* MEMS chip select */
 #endif
@@ -130,6 +136,15 @@ void stm32_spi1select(FAR struct spi_dev_s *dev, uint32_t devid, bool selected)
 {
   spiinfo("devid: %d CS: %s\n", (int)devid, selected ? "assert" : "de-assert");
 
+#ifdef CONFIG_ENC28J60
+  if (devid == SPIDEV_ETHERNET(0))
+    {
+      /* Set the GPIO low to select and high to de-select */
+
+      stm32_gpiowrite(GPIO_ENC28J60_CS, !selected);
+    }
+#endif
+
 #ifdef CONFIG_LPWAN_SX127X
   if (devid == SPIDEV_LPWAN(0))
     {
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h b/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
index 0d34a67ea6..2177d4344d 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
@@ -286,6 +286,15 @@
 #define GPIO_CS_XEN1210   (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
                            GPIO_OUTPUT_SET|GPIO_PORTA|GPIO_PIN4)
 
+#define GPIO_ENC28J60_CS    (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
+                             GPIO_OUTPUT_SET|GPIO_PORTA|GPIO_PIN4)
+
+#define GPIO_ENC28J60_RESET (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
+                             GPIO_OUTPUT_CLEAR|GPIO_PORTE|GPIO_PIN1)
+
+#define GPIO_ENC28J60_INTR  (GPIO_INPUT|GPIO_FLOAT|GPIO_EXTI|\
+                             GPIO_OPENDRAIN|GPIO_PORTE|GPIO_PIN4)
+
 /* USB OTG FS
  *
  * PA9  OTG_FS_VBUS VBUS sensing (also connected to the green LED)
-- 
2.20.1

Reply via email to