Server FTP su STM32 con W5500, ENC28J60, scheda SD e memoria flash SPI


Infine, vado ad aggiornare il SimpleFTPServer con supporto completo e testato per i dispositivi STM32. Per i test, userò un dispositivo w5500 e un enc28j60. Per la memorizzazione, utilizzerò un semplice adattatore per schede SD e una memoria flash SPI esterna.

Server FTP su STM32 con scheda ethernet e scheda SD o flash SPI
Server FTP su STM32 con scheda ethernet e scheda SD o flash SPI

Puoi fare riferimento all’articolo “Come utilizzare la scheda SD con l’stm32 e la libreria SdFat” per il cablaggio e la configurazione della SD, all’articolo “STM32: memoria flash SPI FAT FS” per il Flash SPI, e agli articoli “STM32: ethernet w5500 standard (HTTP) e SSL (HTTPS)” e “STM32: ethernet enc28j60 standard (HTTP) e SSL (HTTPS)” per la configurazione dei dispositivi Ethernet.

Ecco una selezione di dispositivi STM32 STM32F103C8T6 STM32F401 STM32F411 ST-Link v2 ST-Link v2 official

Libreria

La libreria è disponibile direttamente dal gestore di librerie dell’IDE Arduino.

O il codice sorgente da GitHub.

Configurazione

Per STM32 ho già testato 2 tipi di rete, uno con w5500 e uno con enc28j60, e 2 soluzioni di memorizzazione, la SD e la SPIFlash esterna.

// STM32 configuration
#ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32
	#define DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32 		NETWORK_W5100
	#define DEFAULT_STORAGE_TYPE_STM32 					STORAGE_SDFAT2
#endif

Come puoi vedere, la configurazione predefinita richiede un adattatore Ethernet come w5500 (libreria Ethernet Arduino standard) e una SD gestita dalla libreria SdFat 2.x.

Ma puoi scegliere tra una vasta varietà di dispositivi di rete:

#define NETWORK_ESP8266_ASYNC 	(1)
#define NETWORK_ESP8266 		(2) 	// Standard ESP8266WiFi
#define NETWORK_ESP8266_242 	(3) 	// ESP8266WiFi before 2.4.2 core
#define NETWORK_W5100 			(4)		// Standard Arduino Ethernet library
#define NETWORK_ENC28J60 		(5) 	// UIPEthernet library
#define NETWORK_ESP32 			(6) 	// Standard WiFi library
#define NETWORK_ESP32_ETH 		(7)		// Standard ETH library
#define NETWORK_WiFiNINA 		(8)		// Standard WiFiNINA library
#define NETWORK_SEEED_RTL8720DN (9) 	// Standard SEED WiFi library
#define NETWORK_ETHERNET_LARGE 	(10)
#define NETWORK_ETHERNET_ENC 	(11)	// EthernetENC library (evolution of UIPEthernet
#define NETWORK_ETHERNET_STM 	(12)
#define NETWORK_UIPETHERNET 	(13)	// UIPEthernet library same of NETWORK_ENC28J60

In particolare, vedremo la NETWORK_W5100 e la NETWORK_ETHERNET_ENC che è un’evoluzione della libreria UIPEthernet.

Ecco i dispositivi di memorizzazione:

#define STORAGE_SDFAT1 		1 	// Library SdFat version 1.4.x
#define STORAGE_SDFAT2 		2 	// Library SdFat version >= 2.0.2
#define STORAGE_SPIFM  		3 	// Libraries Adafruit_SPIFlash and SdFat-Adafruit-Fork
#define STORAGE_FATFS  		4 	// Library FatFs
#define STORAGE_SD 			5 	// Standard SD library (suitable for Arduino esp8266 and esp32
#define STORAGE_SPIFFS 		6 	// SPIFFS
#define STORAGE_LITTLEFS 	7 	// LITTLEFS
#define STORAGE_SEEED_SD 	8 	// Seeed_SD library
#define STORAGE_FFAT  		9 	// ESP32 FFAT

Ho utilizzato e testato STORAGE_SDFAT2 per la SD e STORAGE_SPIFM per la flash SPI esterna.

Scheda SD ed ethernet w5500

Ecco la mia selezione di dispositivi STM32 STM32F103C8T6 STM32F401 STM32F411 ST-Link v2 ST-Link v2 official

w5500 è uno dei dispositivi ethernet più efficienti, iniziamo con questo.

STM32F4 con w5500 e SD sull’interfaccia SPI secondaria

Questo è il primo esempio, penso che sia la situazione più comune con un controller ethernet w5500 sulla SPI primaria e la SD sull’interfaccia SPI secondaria dedicata.

Ecco i miei dispositivi ethernet testati w5500 lite - w5500 - enc26j60 mini - enc26j60 - lan8720

STM32 w5500 SPI1
PA4CS
PA5SCK
PA6MISO
PA7MOSI
3.3v3.3v
GNDGND

Adattatori per schede SD AliExpress

STM32Scheda SD SPI2
PA12CS
PA13SCK
PA14MISO
PA15MOSI
3.3vVCC
GNDGND

Esempio di sketch

Creeremo un esempio primario con w5500 collegato all’interfaccia SPI primaria e SS standard, e la scheda SD sull’interfaccia SPI secondaria.

Configurazione SimpleFTPServer

Come descritto per la scheda SD utilizziamo la libreria SdFat 2.x e la libreria Ethernet Arduino standard per w5500.

#ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32
	#define DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32 		NETWORK_W5100
	#define DEFAULT_STORAGE_TYPE_STM32 					STORAGE_SDFAT2
#endif

Codice completo

/**
 * SimpleFTPServer ^1.3.0 on STM32 (need FLASH > 64K) 
 * and ethernet w5500
 * SD connected on secondary SPI or primary
 * 

#ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32
	#define DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32 	NETWORK_W5100
	#define DEFAULT_STORAGE_TYPE_STM32 STORAGE_SDFAT2
#endif

 * 
 * @author Renzo Mischianti <www.mischianti.org>
 * @details www.mischianti.org
 * @version 0.1
 * @date 2022-03-22
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include <SdFat.h>
#include <sdios.h>
#include <Ethernet.h>

#include <SimpleFtpServer.h>

// To use SD with primary SPI
// #define SD_CS_PIN PA4

// To use SD with secondary SPI
#define SD_CS_PIN PB12
static SPIClass mySPI2(PB15, PB14, PB13, SD_CS_PIN);
#define SD2_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(18), &mySPI2)

SdFat sd;

FtpServer ftpSrv;   //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Set the static IP address to use if the DHCP fails to assign
#define MYIPADDR 192,168,1,28
#define MYIPMASK 255,255,255,0
#define MYDNS 192,168,1,1
#define MYGW 192,168,1,1

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  Serial.begin( 115200 );

  while (!Serial) { delay(100); }

  Serial.print("\nInitializing SD card...");

  // Secondary SPI for SD
  if (!sd.begin(SD2_CONFIG)) {
  // Primary SPI for SD
  // if (!SD.begin(SD_CS_PIN)) {
    Serial.println(F("initialization failed. Things to check:"));
    Serial.println(F("* is a card inserted?"));
    Serial.println(F("* is your wiring correct?"));
    Serial.println(F("* did you change the chipSelect pin to match your shield or module?"));
    while (1);
  } else {
    Serial.println(F("Wiring is correct and a card is present."));
  }

  // Show capacity and free space of SD card
  Serial.print(F("Capacity of card:   ")); Serial.print(long( sd.card()->sectorCount() >> 1 )); Serial.println(F(" kBytes"));

    // You can use Ethernet.init(pin) to configure the CS pin
    Ethernet.init(PA4);

    if (Ethernet.begin(mac)) { // Dynamic IP setup
        Serial.println(F("DHCP OK!"));
    }else{
        Serial.println(F("Failed to configure Ethernet using DHCP"));
        // Check for Ethernet hardware present
        if (Ethernet.hardwareStatus() == EthernetNoHardware) {
          Serial.println(F("Ethernet shield was not found.  Sorry, can't run without hardware. :("));
          while (true) {
            delay(1); // do nothing, no point running without Ethernet hardware
          }
        }
        if (Ethernet.linkStatus() == LinkOFF) {
          Serial.println(F("Ethernet cable is not connected."));
        }

    	  IPAddress ip(MYIPADDR);
    	  IPAddress dns(MYDNS);
    	  IPAddress gw(MYGW);
    	  IPAddress sn(MYIPMASK);
    	  Ethernet.begin(mac, ip, dns, gw, sn);
		  Serial.println("STATIC OK!");
    }
    delay(5000);


    Serial.print("Local IP : ");
    Serial.println(Ethernet.localIP());
    Serial.print("Subnet Mask : ");
    Serial.println(Ethernet.subnetMask());
    Serial.print("Gateway IP : ");
    Serial.println(Ethernet.gatewayIP());
    Serial.print("DNS Server : ");
    Serial.println(Ethernet.dnsServerIP());

    Serial.println("Ethernet Successfully Initialized");
    Serial.println();


  // Initialize the FTP server
  ftpSrv.begin("user","password");
  Serial.println("Ftp server started!");
}

void loop()
{
  ftpSrv.handleFTP();

  // more processes...
}

Puoi anche usare il login anonimo con

  ftpSrv.begin();

Scheda SD ed ethernet enc28j60

Un altro dispositivo ethernet popolare è l’enc28j60, molto economico ma con alcuni limiti di memoria, comunque più che sufficiente per il nostro scopo.

STM32F4 con enc28j60 e SD sull’interfaccia SPI primaria

Un’altra situazione classica è l’uso di tutti i dispositivi sulla stessa interfaccia SPI con la selezione di CS. In questa configurazione, userai un numero limitato di pin: 5 per tutti i dispositivi.

Ecco i miei dispositivi ethernet testati w5500 lite - w5500 - enc26j60 mini - enc26j60 - lan8720

Ecco il modulo SD che uso AliExpress

STM32 enc28j60 SPI1
PA3CS
PA5SCK
PA6MISO
PA7MOSI
3.3v3.3v
GNDGND

Adattatori per schede SD AliExpress

STM32Scheda SD SPI2
PA2CS
PA5SCK
PA6MISO
PA7MOSI
3.3vVCC
GNDGND

Esempio di sketch

Utilizzeremo enc28j60 e un adattatore per schede SD sul dispositivo SPI principale, cambiando il CS predefinito di Ethernet in PA2 e il CS della SD in PA3.

Configurazione di SimpleFTPServer

Per le schede SD utilizziamo la libreria SdFat 2.x e EthernetENC per enc28j60, un’evoluzione di UIPEthernet.

#ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32
	#define DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32 		NETWORK_ETHERNET_ENC
	#define DEFAULT_STORAGE_TYPE_STM32 					STORAGE_SDFAT2
#endif

Codice completo

/**
 * SimpleFTPServer ^1.3.0 on STM32 (need FLASH > 64K) 
 * and ethernet enc28j60
 * SD connected on secondary SPI or primary
 * 

#ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32
	#define DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32 	NETWORK_W5100
	#define DEFAULT_STORAGE_TYPE_STM32 STORAGE_SDFAT2
#endif

 * 
 * @author Renzo Mischianti <www.mischianti.org>
 * @details www.mischianti.org
 * @version 0.1
 * @date 2022-03-22
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include <SdFat.h>
#include <sdios.h>
#include <SimpleFtpServer.h>
#include <EthernetEnc.h>

// Ethernet CS
#define ETHERNET_CS_PIN PA3

// To use SD with primary SPI
#define SD_CS_PIN PA2

// To use SD with secondary SPI
// #define SD_CS_PIN PB12
// static SPIClass mySPI2(PB15, PB14, PB13, SD_CS_PIN);
// #define SD2_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(18), &mySPI2)

SdFat sd;

FtpServer ftpSrv;   //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Set the static IP address to use if the DHCP fails to assign
#define MYIPADDR 192,168,1,28
#define MYIPMASK 255,255,255,0
#define MYDNS 192,168,1,1
#define MYGW 192,168,1,1

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  Serial.begin( 115200 );

  while (!Serial) { delay(100); }

  pinMode( SD_CS_PIN, OUTPUT );
  digitalWrite( SD_CS_PIN, HIGH );
  pinMode( ETHERNET_CS_PIN, OUTPUT );
  digitalWrite( ETHERNET_CS_PIN, HIGH );

  Serial.print("\nInitializing SD card...");

  // To use SD with secondary SPI
  // if (!sd.begin(SD2_CONFIG)) {
  // To use SD with primary SPI
  if (!sd.begin(SD_CS_PIN)) {
    Serial.println(F("initialization failed. Things to check:"));
    Serial.println(F("* is a card inserted?"));
    Serial.println(F("* is your wiring correct?"));
    Serial.println(F("* did you change the chipSelect pin to match your shield or module?"));
    while (1);
  } else {
    Serial.println(F("Wiring is correct and a card is present."));
  }

  // Show capacity and free space of SD card
  Serial.print(F("Capacity of card:   ")); Serial.print(long( sd.card()->sectorCount() >> 1 )); Serial.println(F(" kBytes"));

    // You can use Ethernet.init(pin) to configure the CS pin
    Ethernet.init(ETHERNET_CS_PIN);

    if (Ethernet.begin(mac)) { // Dynamic IP setup
        Serial.println(F("DHCP OK!"));
    }else{
        Serial.println(F("Failed to configure Ethernet using DHCP"));
        // Check for Ethernet hardware present
        if (Ethernet.hardwareStatus() == EthernetNoHardware) {
          Serial.println(F("Ethernet shield was not found.  Sorry, can't run without hardware. :("));
          while (true) {
            delay(1); // do nothing, no point running without Ethernet hardware
          }
        }
        if (Ethernet.linkStatus() == LinkOFF) {
          Serial.println(F("Ethernet cable is not connected."));
        }

    	  IPAddress ip(MYIPADDR);
    	  IPAddress dns(MYDNS);
    	  IPAddress gw(MYGW);
    	  IPAddress sn(MYIPMASK);
    	  Ethernet.begin(mac, ip, dns, gw, sn);
		  Serial.println("STATIC OK!");
    }
    delay(5000);


    Serial.print("Local IP : ");
    Serial.println(Ethernet.localIP());
    Serial.print("Subnet Mask : ");
    Serial.println(Ethernet.subnetMask());
    Serial.print("Gateway IP : ");
    Serial.println(Ethernet.gatewayIP());
    Serial.print("DNS Server : ");
    Serial.println(Ethernet.dnsServerIP());

    Serial.println("Ethernet Successfully Initialized");
    Serial.println();


  // Initialize the FTP server
  ftpSrv.begin("user","password");
  Serial.println("Ftp server started!");
}

void loop()
{
  ftpSrv.handleFTP();

  // more processes...
}

SPI Flash ed Ethernet w5500

Per questi test, puoi seguire l’articolo relativo alla Flash SPI “STM32: memoria flash SPI FAT FS”, e utilizziamo l’impronta integrata della Flash SPI della STM32F4 Black-pill.

Ecco un set di Flash SPI testate w25q16 SMD 2Mb - w25q16 Discrete 2Mb - w25q32 SMD 4Mb - w25q32 Discrete 4Mb - w25q64 SMD 8Mb - w25q64 Discrete 8Mb - w25q128 SMD 16Mb - w25q128 Discrete 16Mb W25Q32 W25Q64 w25q128 module 4Mb 8Mb 16Mb

Cablaggio w5500

Ora usiamo solo l’Ethernet w5500 sul SPI principale, ma fai attenzione a non usare il pin PA4 come nello schema, perché è utilizzato dalla Flash SPI sull’impronta.

Ecco i miei dispositivi ethernet testati w5500 lite - w5500 - enc26j60 mini - enc26j60 - lan8720

STM32 w5500 SPI1
PA3CS
PA5SCK
PA6MISO
PA7MOSI
3.3v3.3v
GNDGND
STM32SPI Flash
PA4CS
PA5SCK
PA6MISO
PA7MOSI
3.3vVCC
GNDGND

Esempio di sketch

Fai attenzione ai pin CS. PA4 per la Flash SPI e PA3 su Ethernet w5500.

Configurazione di SimpleFTPServer

Per la Flash SPI, come descritto nell’articolo relativo alla Flash SPI, utilizziamo la libreria Adafruit_SPIFlash e SdFat-Adafruit-Fork e la libreria Arduino Ethernet standard per w5500.

#ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32
	#define DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32 		NETWORK_W5100
	#define DEFAULT_STORAGE_TYPE_STM32 					STORAGE_SPIFM
#endif

Codice completo

/**
 * SimpleFTPServer ^1.3.0 on STM32 (need FLASH > 64K) 
 * and ethernet w5500
 * SPI Flash with Adafruit_SPIFlash and SdFat-Adafruit-Fork library
 * 

#ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32
	#define DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32 	NETWORK_W5100
	#define DEFAULT_STORAGE_TYPE_STM32 STORAGE_SPIFM
#endif

 * 
 * @author Renzo Mischianti <www.mischianti.org>
 * @details www.mischianti.org
 * @version 0.1
 * @date 2022-03-22
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#include <Arduino.h>

#include <Ethernet.h>

#include "SdFat.h"
#include "Adafruit_SPIFlash.h"

#include <SimpleFTPServer.h>

Adafruit_FlashTransport_SPI flashTransport(SS, SPI); // Set CS and SPI interface
Adafruit_SPIFlash flash(&flashTransport);

// file system object from SdFat
FatFileSystem fatfs;

#define ETHERNET_CS_PIN PA3

FtpServer ftpSrv;   //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Set the static IP address to use if the DHCP fails to assign
#define MYIPADDR 192,168,1,28
#define MYIPMASK 255,255,255,0
#define MYDNS 192,168,1,1
#define MYGW 192,168,1,1

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
	  // Initialize serial port and wait for it to open before continuing.
	  Serial.begin(115200);
	  while (!Serial) {
	    delay(100);
	  }
	  Serial.println("Adafruit SPI Flash FatFs Full Usage Example");

	  // Initialize flash library and check its chip ID.
	  if (!flash.begin()) {
	    Serial.println("Error, failed to initialize flash chip!");
	    while(1) yield();
	  }

	  Serial.print("JEDEC ID: "); Serial.println(flash.getJEDECID(), HEX);
	  Serial.print("Flash size: "); Serial.println(flash.size());
	  Serial.flush();

	  // First call begin to mount the filesystem.  Check that it returns true
	  // to make sure the filesystem was mounted.
	  if (!fatfs.begin(&flash)) {
	    Serial.println("Error, failed to mount newly formatted filesystem!");
	    Serial.println("Was the flash chip formatted with the SdFat_format example?");
	    while(1) yield();
	  }
	  Serial.println("Mounted filesystem!");
	  Serial.println();


    // You can use Ethernet.init(pin) to configure the CS pin
    Ethernet.init(ETHERNET_CS_PIN);

    if (Ethernet.begin(mac)) { // Dynamic IP setup
        Serial.println(F("DHCP OK!"));
    }else{
        Serial.println(F("Failed to configure Ethernet using DHCP"));
        // Check for Ethernet hardware present
        if (Ethernet.hardwareStatus() == EthernetNoHardware) {
          Serial.println(F("Ethernet shield was not found.  Sorry, can't run without hardware. :("));
          while (true) {
            delay(1); // do nothing, no point running without Ethernet hardware
          }
        }
        if (Ethernet.linkStatus() == LinkOFF) {
          Serial.println(F("Ethernet cable is not connected."));
        }

    	  IPAddress ip(MYIPADDR);
    	  IPAddress dns(MYDNS);
    	  IPAddress gw(MYGW);
    	  IPAddress sn(MYIPMASK);
    	  Ethernet.begin(mac, ip, dns, gw, sn);
		  Serial.println("STATIC OK!");
    }
    delay(5000);


    Serial.print("Local IP : ");
    Serial.println(Ethernet.localIP());
    Serial.print("Subnet Mask : ");
    Serial.println(Ethernet.subnetMask());
    Serial.print("Gateway IP : ");
    Serial.println(Ethernet.gatewayIP());
    Serial.print("DNS Server : ");
    Serial.println(Ethernet.dnsServerIP());

    Serial.println("Ethernet Successfully Initialized");
    Serial.println();


  // Initialize the FTP server
  ftpSrv.begin("user","password");
  Serial.println("Ftp server started!");
}

void loop()
{
  ftpSrv.handleFTP();

  // more processes...
}

SPI Flash ed Ethernet enc28j60

Cablaggio enc28j60

Utilizziamo lo stesso cablaggio del w5500 per SPI e CS.

STM32 enc28j60 SPI1
PA3CS
PA5SCK
PA6MISO
PA7MOSI
3.3v3.3v
GNDGND
STM32SPI Flash
PA4CS
PA5SCK
PA6MISO
PA7MOSI
3.3vVCC
GNDGND

Esempio di sketch

Fai attenzione ai pin CS. PA4 per la Flash SPI e PA3 su Ethernet enc28j60.

Configurazione di SimpleFTPServer

Per la Flash SPI, come descritto nell’articolo relativo alla Flash SPI, utilizziamo la libreria Adafruit_SPIFlash e SdFat-Adafruit-Fork e EthernetENC per Ethernet enc28j60.

#ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32
	#define DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32 		NETWORK_ETHERNET_ENC
	#define DEFAULT_STORAGE_TYPE_STM32 					STORAGE_SPIFM
#endif

Codice completo

/**
 * SimpleFTPServer ^1.3.0 on STM32 (need FLASH > 64K) 
 * and ethernet enc28j60 with EthernetENC
 * SPI Flash with Adafruit_SPIFlash and SdFat-Adafruit-Fork library
 * 

#ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32
	#define DEFAULT_FTP_SERVER_NETWORK_TYPE_STM32 	NETWORK_ETHERNET_ENC
	#define DEFAULT_STORAGE_TYPE_STM32 STORAGE_SPIFM
#endif

 * 
 * @author Renzo Mischianti <www.mischianti.org>
 * @details www.mischianti.org
 * @version 0.1
 * @date 2022-03-22
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#include <Arduino.h>

#include <EthernetENC.h>

#include "SdFat.h"
#include "Adafruit_SPIFlash.h"

#include <SimpleFTPServer.h>

Adafruit_FlashTransport_SPI flashTransport(SS, SPI); // Set CS and SPI interface
Adafruit_SPIFlash flash(&flashTransport);

// file system object from SdFat
FatFileSystem fatfs;

#define ETHERNET_CS_PIN PA3

FtpServer ftpSrv;   //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Set the static IP address to use if the DHCP fails to assign
#define MYIPADDR 192,168,1,28
#define MYIPMASK 255,255,255,0
#define MYDNS 192,168,1,1
#define MYGW 192,168,1,1

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
	  // Initialize serial port and wait for it to open before continuing.
	  Serial.begin(115200);
	  while (!Serial) {
	    delay(100);
	  }
	  Serial.println("Adafruit SPI Flash FatFs Full Usage Example");

	  // Initialize flash library and check its chip ID.
	  if (!flash.begin()) {
	    Serial.println("Error, failed to initialize flash chip!");
	    while(1) yield();
	  }

	  Serial.print("JEDEC ID: "); Serial.println(flash.getJEDECID(), HEX);
	  Serial.print("Flash size: "); Serial.println(flash.size());
	  Serial.flush();

	  // First call begin to mount the filesystem.  Check that it returns true
	  // to make sure the filesystem was mounted.
	  if (!fatfs.begin(&flash)) {
	    Serial.println("Error, failed to mount newly formatted filesystem!");
	    Serial.println("Was the flash chip formatted with the SdFat_format example?");
	    while(1) yield();
	  }
	  Serial.println("Mounted filesystem!");
	  Serial.println();


    // You can use Ethernet.init(pin) to configure the CS pin
    Ethernet.init(ETHERNET_CS_PIN);

    if (Ethernet.begin(mac)) { // Dynamic IP setup
        Serial.println(F("DHCP OK!"));
    }else{
        Serial.println(F("Failed to configure Ethernet using DHCP"));
        // Check for Ethernet hardware present
        if (Ethernet.hardwareStatus() == EthernetNoHardware) {
          Serial.println(F("Ethernet shield was not found.  Sorry, can't run without hardware. :("));
          while (true) {
            delay(1); // do nothing, no point running without Ethernet hardware
          }
        }
        if (Ethernet.linkStatus() == LinkOFF) {
          Serial.println(F("Ethernet cable is not connected."));
        }

    	  IPAddress ip(MYIPADDR);
    	  IPAddress dns(MYDNS);
    	  IPAddress gw(MYGW);
    	  IPAddress sn(MYIPMASK);
    	  Ethernet.begin(mac, ip, dns, gw, sn);
		  Serial.println("STATIC OK!");
    }
    delay(5000);


    Serial.print("Local IP : ");
    Serial.println(Ethernet.localIP());
    Serial.print("Subnet Mask : ");
    Serial.println(Ethernet.subnetMask());
    Serial.print("Gateway IP : ");
    Serial.println(Ethernet.gatewayIP());
    Serial.print("DNS Server : ");
    Serial.println(Ethernet.dnsServerIP());

    Serial.println("Ethernet Successfully Initialized");
    Serial.println();


  // Initialize the FTP server
  ftpSrv.begin("user","password");
  Serial.println("Ftp server started!");
}

void loop()
{
  ftpSrv.handleFTP();

  // more processes...
}

Grazie

  1. STM32F1 Blue Pill: piedinatura, specifiche e configurazione IDE Arduino (STM32duino e STMicroelectronics)
  2. STM32: programmazione (STM32F1) via USB con bootloader STM32duino
  3. STM32: programmazione (STM32F1 STM32F4) tramite USB con bootloader HID
  4. STM32F4 Black Pill: pinout, specifiche e configurazione IDE Arduino
  5. STM32: ethernet w5500 standard (HTTP) e SSL (HTTPS)
  6. STM32: ethernet enc28j60 standard (HTTP) e SSL (HTTPS)
  7. STM32: WiFiNINA con un ESP32 come WiFi Co-Processor
    1. STM32F1 Blue-pill: shield WiFi (WiFiNINA)
    2. STM32F4 Black-pill: shield WiFi (WiFiNINA)
  8. Come utilizzare la scheda SD con l’stm32 e la libreria SdFat
  9. STM32: memoria flash SPI FAT FS
  10. STM32: RTC interno, sistema orario e backup batteria (VBAT)
  11. STM32 LoRa
  1. STM32 Risparmio energetico
    1. STM32F1 Blue-Pill gestione clock e frequenza
    2. STM32F4 Black-Pill gestione clock e frequenza
    3. Introduzione e framework Arduino vs STM
    4. Libreria LowPower, cablaggio e Idle (STM Sleep).
    5. Sleep, deep sleep, shutdown e consumo energetico
    6. Sveglia da allarme RTC e Seriale
    7. Sveglia da sorgente esterna
    8. Introduzione al dominio di backup e conservazione delle variabili durante il RESET
    9. Registro di backup RTC e conservazione della SRAM
  1. STM32 invia email con allegati e SSL (come Gmail): w5500, enc28j60, SD e SPI Flash
  2. Server FTP su STM32 con W5500, ENC28J60, scheda SD e memoria flash SPI
  3. Collegamento dell’EByte E70 ai dispositivi STM32 (black/blue pill) e un semplice sketch di esempio
  1. Libreria SimpleFTPServer library: guida esp32 e esp8266
  2. Libreria SimpleFTPServer: guida WioTerminal
  3. Server FTP su STM32 con W5500, ENC28J60, scheda SD e memoria flash SPI

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *