Skip to content

Commit 92e44f7

Browse files
committed
1 parent 8de2b86 commit 92e44f7

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- Wio Terminal (SdFat 2, Seed SD, and native FAT)
1313

1414
#### Changelog
15-
- 2022-01-13 2.1.5 Fix SPIFM external SPI Flash date management
15+
- 2022-01-13 2.1.5 Fix SPIFM external SPI Flash date management (add SPIFM esp32 example)
1616
- 2022-09-21 2.1.4 Add support for Raspberry Pi Pico W and rp2040 boards, Fix SD card config
1717
- 2022-09-20 2.1.3 Soft AP IP management, more disconnect event and SD_MCC
1818
- 2022-05-21 2.1.2 Fix SD path (#19)
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <Arduino.h>
2+
#include "SdFat.h"
3+
#include "Adafruit_SPIFlash.h"
4+
#include <WiFi.h>
5+
#include <SimpleFTPServer.h>
6+
7+
Adafruit_FlashTransport_SPI flashTransport(SS, SPI); // Set CS and SPI interface
8+
Adafruit_SPIFlash flash(&flashTransport);
9+
10+
// file system object from SdFat
11+
FatFileSystem fatfs;
12+
13+
const char* ssid = "<YOUR_SSID>";
14+
const char* password = "<YOUR_PASSWD>";
15+
16+
17+
FtpServer ftpSrv; //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial
18+
19+
void _callback(FtpOperation ftpOperation, unsigned int freeSpace, unsigned int totalSpace){
20+
switch (ftpOperation) {
21+
case FTP_CONNECT:
22+
Serial.println(F("FTP: Connected!"));
23+
break;
24+
case FTP_DISCONNECT:
25+
Serial.println(F("FTP: Disconnected!"));
26+
break;
27+
case FTP_FREE_SPACE_CHANGE:
28+
Serial.print(F("FTP: Free space change, free "));
29+
Serial.print(freeSpace);
30+
Serial.print(F(" of "));
31+
Serial.println(totalSpace);
32+
break;
33+
default:
34+
break;
35+
}
36+
};
37+
void _transferCallback(FtpTransferOperation ftpOperation, const char* name, unsigned int transferredSize){
38+
switch (ftpOperation) {
39+
case FTP_UPLOAD_START:
40+
Serial.println(F("FTP: Upload start!"));
41+
break;
42+
case FTP_UPLOAD:
43+
Serial.print(F("FTP: Upload of file "));
44+
Serial.print(name);
45+
Serial.print(F(" "));
46+
Serial.print(transferredSize);
47+
Serial.println(F("bytes"));
48+
break;
49+
case FTP_TRANSFER_STOP:
50+
Serial.println(F("FTP: Finish transfer!"));
51+
break;
52+
case FTP_TRANSFER_ERROR:
53+
Serial.println(F("FTP: Transfer error!"));
54+
break;
55+
default:
56+
break;
57+
}
58+
59+
/* FTP_UPLOAD_START = 0,
60+
* FTP_UPLOAD = 1,
61+
*
62+
* FTP_DOWNLOAD_START = 2,
63+
* FTP_DOWNLOAD = 3,
64+
*
65+
* FTP_TRANSFER_STOP = 4,
66+
* FTP_DOWNLOAD_STOP = 4,
67+
* FTP_UPLOAD_STOP = 4,
68+
*
69+
* FTP_TRANSFER_ERROR = 5,
70+
* FTP_DOWNLOAD_ERROR = 5,
71+
* FTP_UPLOAD_ERROR = 5
72+
*/
73+
};
74+
75+
void setup()
76+
{
77+
// Initialize serial port and wait for it to open before continuing.
78+
Serial.begin(115200);
79+
while (!Serial) {
80+
delay(100);
81+
}
82+
Serial.println("FTP with external SPIFlash");
83+
84+
WiFi.begin(ssid, password);
85+
Serial.println("");
86+
87+
// Wait for connection
88+
while (WiFi.status() != WL_CONNECTED) {
89+
delay(500);
90+
Serial.print(".");
91+
}
92+
Serial.println("");
93+
Serial.print("Connected to ");
94+
Serial.println(ssid);
95+
Serial.print("IP address: ");
96+
Serial.println(WiFi.localIP());
97+
Serial.print("SN address: ");
98+
Serial.println(WiFi.subnetMask());
99+
100+
if (flash.begin()) {
101+
Serial.println(F("Device finded and supported!"));
102+
} else {
103+
Serial.println(F("Problem to discover and configure device, check wiring also!"));
104+
while(1) yield();
105+
}
106+
// Set 4Mhz SPI speed
107+
flashTransport.setClockSpeed(4000000, 4000000); // added to prevent speed problem
108+
109+
Serial.print("JEDEC ID: "); Serial.println(flash.getJEDECID(), HEX);
110+
Serial.print("Flash size: "); Serial.println(flash.size());
111+
Serial.flush();
112+
113+
// First call begin to mount the filesystem. Check that it returns true
114+
// to make sure the filesystem was mounted.
115+
if (!fatfs.begin(&flash)) {
116+
Serial.println("Error, failed to mount newly formatted filesystem!");
117+
Serial.println("Was the flash chip formatted with the SdFat_format example?");
118+
while(1) yield();
119+
}
120+
Serial.println("Mounted filesystem!");
121+
Serial.println();
122+
123+
ftpSrv.setCallback(_callback);
124+
ftpSrv.setTransferCallback(_transferCallback);
125+
126+
Serial.println("Starting FTP Server!");
127+
ftpSrv.begin("user","password"); //username, password for ftp. (default 21, 50009 for PASV)
128+
// ftpSrv.beginAnonymous();
129+
130+
}
131+
132+
// The loop function is called in an endless loop
133+
void loop()
134+
{
135+
ftpSrv.handleFTP(); //make sure in loop you call handleFTP()!!
136+
}

0 commit comments

Comments
 (0)