8000 Merge remote-tracking branch 'upstream/esp8266' · emgab/esp8266-Arduino@ecc3e0a · GitHub
[go: up one dir, main page]

Skip to content

Commit ecc3e0a

Browse files
committed
Merge remote-tracking branch 'upstream/esp8266'
2 parents 2417c15 + 5fc7229 commit ecc3e0a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+9997
-1018
lines changed

esp8266com/esp8266/cores/esp8266/Arduino.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern "C" {
3838
#include "pgmspace.h"
3939
#include "esp8266_peri.h"
4040
#include "twi.h"
41+
#include "spiffs/spiffs.h"
4142

4243
void yield(void);
4344

@@ -172,6 +173,7 @@ int digitalRead(uint8_t);
172173
int analogRead(uint8_t);
173174
void analogReference(uint8_t mode);
174175
void analogWrite(uint8_t, int);
176+
void analogWriteFreq(uint32_t freq);
175177

176178
unsigned long millis(void);
177179
unsigned long micros(void);
@@ -210,7 +212,9 @@ void loop(void);
210212
#include "WString.h"
211213

212214
#include "HardwareSerial.h"
215+
#include "FileSystem.h"
213216
#include "Esp.h"
217+
#include "debug.h"
214218

215219
uint16_t makeWord(uint16_t w);
216220
uint16_t makeWord(byte h, byte l);
@@ -228,6 +232,7 @@ long random(long, long);
228232
void randomSeed(unsigned int);
229233
long map(long, long, long, long, long);
230234

235+
231236
#endif
232237

233238
#include "pins_arduino.h"
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/*
2+
FileSystem.cpp - SPIFS implementation for esp8266
3+
4+
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
#include "FileSystem.h"
22+
#include "Arduino.h"
23+
24+
bool FSClass::mount() {
25+
if (_mounted)
26+
return true;
27+
28+
_mounted = spiffs_mount();
29+
return _mounted;
30+
}
31+
32+
void FSClass::unmount() {
33+
if (!_mounted)
34+
return;
35+
36+
spiffs_unmount();
37+
_mounted = false;
38+
}
39+
40+
bool FSClass::format() {
41+
return spiffs_format();
42+
}
43+
44+
bool FSClass::exists(const char *filename) {
45+
spiffs_stat stat = {0};
46+
if (SPIFFS_stat(&_filesystemStorageHandle, filename, &stat) < 0)
47+
return false;
48+
return stat.name[0] != '\0';
49+
}
50+
51+
bool FSClass::create(const char *filepath){
52+
return SPIFFS_creat(&_filesystemStorageHandle, filepath, 0) == 0;
53+
}
54+
55+
bool FSClass::remove(const char *filepath){
56+
return SPIFFS_remove(&_filesystemStorageHandle, filepath) == 0;
57+
}
58+
59+
bool FSClass::rename(const char *filename, const char *newname) {
60+
return SPIFFS_rename(&_filesystemStorageHandle, filename, newname) == 0;
61+
}
62+
63+
FSFile FSClass::open(const char *filename, uint8_t mode) {
64+
int repeats = 0;
65+
bool notExist;
66+
bool canRecreate = (mode & SPIFFS_CREAT) == SPIFFS_CREAT;
67+
int res;
68+
69+
do{
70+
notExist = false;
71+
res = SPIFFS_open(&_filesystemStorageHandle, filename, (spiffs_flags)mode, 0);
72+
int code = SPIFFS_errno(&_filesystemStorageHandle);
73+
if (res < 0){
74+
debugf("open errno %d\n", code);
75+
notExist = (code == SPIFFS_ERR_NOT_FOUND || code == SPIFFS_ERR_DELETED || code == SPIFFS_ERR_FILE_DELETED || code == SPIFFS_ERR_IS_FREE);
76+
if (notExist && canRecreate)
77+
remove(filename); // fix for deleted files
78+
}
79+
} while (notExist && canRecreate && repeats++ < 3);
80+
81+
if(res){
82+
return FSFile(res);
83+
}
84+
return FSFile();
85+
}
86+
87+
FSClass FS;
88+
89+
FSFile::FSFile() {
90+
_file = 0;
91+
_stats = {0};
92+
}
93+
94+
FSFile::FSFile(file_t f) {
95+
_file = f;
96+
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){
97+
debugf("mount errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
98+
}
99+
}
100+
101+
void FSFile::close() {
102+
if (! _file) return;
103+
SPIFFS_close(&_filesystemStorageHandle, _file);
104+
_file = 0;
105+
}
106+
107+
uint32_t FSFile::size() {
108+
if(! _file) return 0;
109+
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
110+
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END);
111+
uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file);
112+
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
113+
return size;
114+
}
115+
116+
uint32_t FSFile::seek(uint32_t pos) {
117+
if (! _file) return 0;
118+
return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
119+
}
120+
121+
uint32_t FSFile::position() {
122+
if (! _file) return 0;
123+
return SPIFFS_tell(&_filesystemStorageHandle, _file);
124+
}
125+
126+
bool FSFile::eof() {
127+
if (! _file) return 0;
128+
return SPIFFS_eof(&_filesystemStorageHandle, _file);
129+
}
130+
131+
bool FSFile::isDirectory(void) {
132+
return false;
133+
}
134+
135+
int FSFile::read(void *buf, uint16_t nbyte) {
136+
if (! _file) return -1;
137+
return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte);
138+
}
139+
140+
int FSFile::read() {
141+
if (! _file) return -1;
142+
int val;
143+
if(SPIFFS_read(&_filesystemStorageHandle, _file, &val, 1) != 1) return -1;
144+
return val;
145+
}
146+
147+
int FSFile::peek() {
148+
if (! _file) return 0;
149+
int c = read();
150+
SPIFFS_lseek(&_filesystemStorageHandle, _file, -1, SPIFFS_SEEK_CUR);
151+
return c;
152+
}
153+
154+
int FSFile::available() {
155+
if (! _file) return 0;
156+
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
157+
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END);
158+
uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file);
159+
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
160+
return size - pos;
161+
}
162+
163+
size_t FSFile::write(const uint8_t *buf, size_t size){
164+
if (! _file) return 0;
165+
return SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
166+
}
167+
168+
size_t FSFile::write(uint8_t val) {
169+
if (! _file) return 0;
170+
return write(&val, 1);
171+
}
172+
173+
void FSFile::flush(){
174+
if (! _file) return;
175+
SPIFFS_fflush(&_filesystemStorageHandle, _file);
176+
}
177+
178+
uint32_t FSFile::remove(){
179+
if (! _file) return 0;
180+
return SPIFFS_fremove(&_filesystemStorageHandle, _file);
181+
_file = 0;
182+
}
183+
184+
int FSFile::lastError(){
185+
return SPIFFS_errno(&_filesystemStorageHandle);
186+
}
187+
188+
void FSFile::clearError(){
189+
_filesystemStorageHandle.errno = SPIFFS_OK;
190+
}
191+
192+
char * FSFile::name(){
193+
return 0;
194+
}
195+
196+
197+
198+
199+
200+
201+
/*
202+
spiffs_DIR *dirOpen(spiffs_DIR *d){
203+
return SPIFFS_opendir(&_filesystemStorageHandle, 0, d);
204+
}
205+
206+
int dirClose(spiffs_DIR *d){
207+
return SPIFFS_closedir(d);
208+
}
209+
210+
file_t dirOpenFile(spiffs_dirent* entry, uint8_t flags){
211+
return SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)flags, 0);
212+
}
213+
*/
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
FileSystem.h - SPIFS implementation for esp8266
3+
4+
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
#ifndef _SPIFFS_CORE_FILESYSTEM_H_
22+
#define _SPIFFS_CORE_FILESYSTEM_H_
23+
24+
#include "spiffs/spiffs.h"
25+
#include "Arduino.h"
26+
class String;
27+
28+
#define FSFILE_READ SPIFFS_RDONLY
29+
#define FSFILE_WRITE (SPIFFS_RDONLY | SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_APPEND | SPIFFS_TRUNC)
30+
31+
class FSFile : public Stream {
32+
private:
33+
spiffs_stat _stats;
34+
file_t _file;
35+
36+
public:
37+
FSFile(file_t f);
38+
FSFile(void);
39+
virtual size_t write(uint8_t);
40+
virtual size_t write(const uint8_t *buf, size_t size);
41+
virtual int read();
42+
virtual int peek();
43+
virtual int available();
44+
virtual void flush();
45+
int read(void *buf, uint16_t nbyte);
46+
uint32_t seek(uint32_t pos);
47+
uint32_t remove();
48+
uint32_t position();
49+
uint32_t size();
50+
bool eof();
51+
void close();
52+
int lastError();
53+
void clearError();
54+
operator bool() { return _file > 0; }
55+
char * name();
56+
bool isDirectory(void);
57+
58+
template<typename T> size_t write(T &src){
59+
const size_t bufferSize = 64;
60+
uint8_t obuf[bufferSize];
61+
size_t bytesWritten = 0;
62+
while (true){
63+
size_t available = src.available();
64+
size_t willWrite = (available < bufferSize) ? available : bufferSize;
65+
src.read(obuf, willWrite);
66+
size_t cb = write(obuf, willWrite);
67+
bytesWritten += cb;
68+
if (cb != willWrite) {
69+
return bytesWritten;
70+
}
71+
}
72+
return bytesWritten;
73+
}
74+
75+
using Print::write;
76+
};
77+
78+
class FSClass {
79+
80+
private:
81+
bool _mounted;
82+
83+
public:
84+
bool mount();
85+
void unmount();
86+
bool format();
87+
bool exists(const char *filename);
88+
bool create(const char *filepath);
89+
bool remove(const char *filepath);
90+
bool rename(const char *filename, const char *newname);
91+
92+
FSFile open(const char *filename, uint8_t mode = FSFILE_READ);
93+
94+
private:
95+
friend class FSFile;
96+
};
97+
98+
extern FSClass FS;
99+
100+
#endif

0 commit comments

Comments
 (0)
0