Skip to content

Commit d6488f9

Browse files
committed
Merge remote-tracking branch 'esp8266/esp8266' into esp8266
2 parents c0c31f0 + 799f680 commit d6488f9

File tree

13 files changed

+421
-249
lines changed

13 files changed

+421
-249
lines changed

hardware/esp8266com/esp8266/cores/esp8266/FS.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ String Dir::fileName() {
144144
return _impl->fileName();
145145
}
146146

147+
size_t Dir::fileSize() {
148+
if (!_impl) {
149+
return 0;
150+
}
151+
152+
return _impl->fileSize();
153+
}
154+
147155
bool Dir::next() {
148156
if (!_impl) {
149157
return false;

hardware/esp8266com/esp8266/cores/esp8266/FS.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Dir {
7878

7979
File openFile(const char* mode);
8080
String fileName();
81+
size_t fileSize();
8182
bool next();
8283

8384
protected:

hardware/esp8266com/esp8266/cores/esp8266/FSImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class DirImpl {
5656
virtual ~DirImpl() { }
5757
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
5858
virtual const char* fileName() = 0;
59+
virtual size_t fileSize() = 0;
5960
virtual bool next() = 0;
6061
};
6162

hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_noniso.c

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ char* ultoa(unsigned long value, char* result, int base) {
148148
}
149149

150150
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
151-
151+
bool negative = false;
152+
152153
if (isnan(number)) {
153154
strcpy(s, "nan");
154155
return s;
@@ -158,50 +159,65 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
158159
return s;
159160
}
160161

161-
if (number > 4294967040.0 || number < -4294967040.0) {
162-
strcpy(s, "ovf");
163-
return s;
164-
}
165-
166162
char* out = s;
167-
int signInt_Part = 1;
168-
163+
164+
int fillme = width; // how many cells to fill for the integer part
165+
if (prec > 0) {
166+
fillme -= (prec+1);
167+
}
168+
169169
// Handle negative numbers
170170
if (number < 0.0) {
171-
signInt_Part = -1;
171+
negative = true;
172+
fillme--;
172173
number = -number;
173174
}
174175

175-
// calc left over digits
176-
if (prec > 0)
177-
{
178-
width -= (prec + 1);
179-
}
180-
181176
// Round correctly so that print(1.999, 2) prints as "2.00"
182-
double rounding = 0.5;
177+
// I optimized out most of the divisions
178+
double rounding = 2.0;
183179
for (uint8_t i = 0; i < prec; ++i)
184-
rounding /= 10.0;
180+
rounding *= 10.0;
181+
rounding = 1.0 / rounding;
185182

186183
number += rounding;
187-
188-
// Extract the integer part of the number and print it
189-
unsigned long int_part = (unsigned long)number;
190-
double remainder = number - (double)int_part;
191-
out += sprintf(out, "%*ld", width, int_part * signInt_Part);
192-
193-
// Print the decimal point, but only if there are digits beyond
194-
if (prec > 0) {
195-
*out = '.';
196-
++out;
197-
198-
199-
for (unsigned char decShift = prec; decShift > 0; decShift--) {
200-
remainder *= 10.0;
201-
}
202-
sprintf(out, "%0*d", prec, (int)remainder);
184+
185+
// Figure out how big our number really is
186+
double tenpow = 1.0;
187+
int digitcount = 1;
188+
while (number >= 10.0 * tenpow) {
189+
tenpow *= 10.0;
190+
digitcount++;
191+
}
192+
193+
number /= tenpow;
194+
fillme -= digitcount;
195+
196+
// Pad unused cells with spaces
197+
while (fillme-- > 0) {
198+
*out++ = ' ';
199+
}
200+
201+
// Handle negative sign
202+
if (negative) *out++ = '-';
203+
204+
// Print the digits, and if necessary, the decimal point
205+
digitcount += prec;
206+
int8_t digit = 0;
207+
while (digitcount-- > 0) {
208+
digit = (int8_t)number;
209+
if (digit > 9) digit = 9; // insurance
210+
*out++ = (char)('0' | digit);
211+
if ((digitcount == prec) && (prec > 0)) {
212+
*out++ = '.';
213+
}
214+
number -= digit;
215+
number *= 10.0;
203216
}
204217

218+
// make sure the string is terminated
219+
*out = 0;
205220
return s;
206221
}
207222

223+

hardware/esp8266com/esp8266/cores/esp8266/spiffs/README

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SPIFFS (SPI Flash File System)
2-
V0.3.0
2+
V0.3.2
33

44
Copyright (c) 2013-2015 Peter Andersson (pelleplutt1976<at>gmail.com)
55

@@ -58,6 +58,30 @@ For testing and contributions, see the docs/IMPLEMENTING file.
5858

5959
* HISTORY
6060

61+
0.3.2
62+
Limit cache size if too much cache is given (thanks pgeiem)
63+
New feature - Controlled erase. #23
64+
SPIFFS_rename leaks file descriptors #28 (thanks benpicco)
65+
moved dbg print defines in test framework to params_test.h
66+
lseek should return the resulting offset (thanks hefloryd)
67+
fixed type on dbg ifdefs
68+
silence warning about signed/unsigned comparison when spiffs_obj_id is 32 bit (thanks benpicco)
69+
Possible error in test_spiffs.c #21 (thanks yihcdaso-yeskela)
70+
Cache might writethrough too often #16
71+
even moar testrunner updates
72+
Test framework update and some added tests
73+
Some thoughts for next gen
74+
Test sigsevs when having too many sectors #13 (thanks alonewolfx2)
75+
GC might be suboptimal #11
76+
Fix eternal readdir when objheader at last block, last entry
77+
78+
New API functions:
79+
SPIFFS_gc_quick - call a nonintrusive gc
80+
SPIFFS_gc - call a full-scale intrusive gc
81+
82+
0.3.1
83+
Removed two return warnings, was too triggerhappy on release
84+
6185
0.3.0
6286
Added existing namecheck when creating files
6387
Lots of static analysis bugs #6

hardware/esp8266com/esp8266/cores/esp8266/spiffs/spiffs.h

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#ifndef SPIFFS_H_
1111
#define SPIFFS_H_
12-
#ifdef __cplusplus
12+
#if defined(__cplusplus)
1313
extern "C" {
1414
#endif
1515

@@ -47,6 +47,9 @@ extern "C" {
4747
#define SPIFFS_ERR_ERASE_FAIL -10027
4848
#define SPIFFS_ERR_MAGIC_NOT_POSSIBLE -10028
4949

50+
#define SPIFFS_ERR_NO_DELETED_BLOCKS -10029
51+
52+
#define SPIFFS_ERR_FILE_EXISTS -10030
5053

5154
#define SPIFFS_ERR_INTERNAL -10050
5255

@@ -62,12 +65,25 @@ typedef u16_t spiffs_mode;
6265
// object type
6366
typedef u8_t spiffs_obj_type;
6467

68+
#if SPIFFS_HAL_CALLBACK_EXTRA
69+
struct spiffs_t;
70+
71+
/* spi read call function type */
72+
typedef s32_t (*spiffs_read)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *dst);
73+
/* spi write call function type */
74+
typedef s32_t (*spiffs_write)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *src);
75+
/* spi erase call function type */
76+
typedef s32_t (*spiffs_erase)(struct spiffs_t *fs, u32_t addr, u32_t size);
77+
78+
#else // SPIFFS_HAL_CALLBACK_EXTRA
79+
6580
/* spi read call function type */
6681
typedef s32_t (*spiffs_read)(u32_t addr, u32_t size, u8_t *dst);
6782
/* spi write call function type */
6883
typedef s32_t (*spiffs_write)(u32_t addr, u32_t size, u8_t *src);
6984
/* spi erase call function type */
7085
typedef s32_t (*spiffs_erase)(u32_t addr, u32_t size);
86+
#endif // SPIFFS_HAL_CALLBACK_EXTRA
7187

7288
/* file system check callback report operation */
7389
typedef enum {
@@ -88,21 +104,26 @@ typedef enum {
88104
} spiffs_check_report;
89105

90106
/* file system check callback function */
107+
#if SPIFFS_HAL_CALLBACK_EXTRA
108+
typedef void (*spiffs_check_callback)(struct spiffs_t *fs, spiffs_check_type type, spiffs_check_report report,
109+
u32_t arg1, u32_t arg2);
110+
#else // SPIFFS_HAL_CALLBACK_EXTRA
91111
typedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_report report,
92112
u32_t arg1, u32_t arg2);
113+
#endif // SPIFFS_HAL_CALLBACK_EXTRA
93114

94115
#ifndef SPIFFS_DBG
95116
#define SPIFFS_DBG(...) \
96117
print(__VA_ARGS__)
97118
#endif
98119
#ifndef SPIFFS_GC_DBG
99-
#define SPIFFS_GC_DBG(...) c_printf(__VA_ARGS__)
120+
#define SPIFFS_GC_DBG(...) printf(__VA_ARGS__)
100121
#endif
101122
#ifndef SPIFFS_CACHE_DBG
102-
#define SPIFFS_CACHE_DBG(...) c_printf(__VA_ARGS__)
123+
#define SPIFFS_CACHE_DBG(...) printf(__VA_ARGS__)
103124
#endif
104125
#ifndef SPIFFS_CHECK_DBG
105-
#define SPIFFS_CHECK_DBG(...) c_printf(__VA_ARGS__)
126+
#define SPIFFS_CHECK_DBG(...) printf(__VA_ARGS__)
106127
#endif
107128

108129
/* Any write to the filehandle is appended to end of the file */
@@ -119,6 +140,8 @@ typedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_repor
119140
#define SPIFFS_RDWR (SPIFFS_RDONLY | SPIFFS_WRONLY)
120141
/* Any writes to the filehandle will never be cached */
121142
#define SPIFFS_DIRECT (1<<5)
143+
/* If SPIFFS_CREAT and SPIFFS_EXCL are set, SPIFFS_open() shall fail if the file exists */
144+
#define SPIFFS_EXCL (1<<6)
122145

123146
#define SPIFFS_SEEK_SET (0)
124147
#define SPIFFS_SEEK_CUR (1)
@@ -166,7 +189,7 @@ typedef struct {
166189
#endif
167190
} spiffs_config;
168191

169-
typedef struct {
192+
typedef struct spiffs_t {
170193
// file system configuration
171194
spiffs_config cfg;
172195
// number of logical blocks
@@ -224,6 +247,8 @@ typedef struct {
224247

225248
// mounted flag
226249
u8_t mounted;
250+
// user data
251+
void *user_data;
227252
// config magic
228253
u32_t config_magic;
229254
} spiffs;
@@ -387,7 +412,7 @@ s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh);
387412
* @param fs the file system struct
388413
* @param fh the filehandle of the file to close
389414
*/
390-
void SPIFFS_close(spiffs *fs, spiffs_file fh);
415+
s32_t SPIFFS_close(spiffs *fs, spiffs_file fh);
391416

392417
/**
393418
* Renames a file
@@ -440,7 +465,6 @@ struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e);
440465
*/
441466
s32_t SPIFFS_check(spiffs *fs);
442467

443-
444468
/**
445469
* Returns number of total bytes available and number of used bytes.
446470
* This is an estimation, and depends on if there a many files with little
@@ -465,27 +489,60 @@ s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used);
465489
* SPIFFS_format.
466490
* If SPIFFS_mount fails, SPIFFS_format can be called directly without calling
467491
* SPIFFS_unmount first.
492+
*
493+
* @param fs the file system struct
468494
*/
469495
s32_t SPIFFS_format(spiffs *fs);
470496

471497
/**
472498
* Returns nonzero if spiffs is mounted, or zero if unmounted.
499+
* @param fs the file system struct
473500
*/
474501
u8_t SPIFFS_mounted(spiffs *fs);
475502

476503
/**
477-
* Check if EOF reached.
478-
* @param fs the file system struct
479-
* @param fh the filehandle of the file to check
504+
* Tries to find a block where most or all pages are deleted, and erase that
505+
* block if found. Does not care for wear levelling. Will not move pages
506+
* around.
507+
* If parameter max_free_pages are set to 0, only blocks with only deleted
508+
* pages will be selected.
509+
*
510+
* NB: the garbage collector is automatically called when spiffs needs free
511+
* pages. The reason for this function is to give possibility to do background
512+
* tidying when user knows the system is idle.
513+
*
514+
* Use with care.
515+
*
516+
* Setting max_free_pages to anything larger than zero will eventually wear
517+
* flash more as a block containing free pages can be erased.
518+
*
519+
* Will set err_no to SPIFFS_OK if a block was found and erased,
520+
* SPIFFS_ERR_NO_DELETED_BLOCK if no matching block was found,
521+
* or other error.
522+
*
523+
* @param fs the file system struct
524+
* @param max_free_pages maximum number allowed free pages in block
480525
*/
481-
s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);
526+
s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages);
482527

483528
/**
484-
* Get the current position of the data pointer.
529+
* Will try to make room for given amount of bytes in the filesystem by moving
530+
* pages and erasing blocks.
531+
* If it is physically impossible, err_no will be set to SPIFFS_ERR_FULL. If
532+
* there already is this amount (or more) of free space, SPIFFS_gc will
533+
* silently return. It is recommended to call SPIFFS_info before invoking
534+
* this method in order to determine what amount of bytes to give.
535+
*
536+
* NB: the garbage collector is automatically called when spiffs needs free
537+
* pages. The reason for this function is to give possibility to do background
538+
* tidying when user knows the system is idle.
539+
*
540+
* Use with care.
541+
*
485542
* @param fs the file system struct
486-
* @param fh the filehandle of the open file
543+
* @param size amount of bytes that should be freed
487544
*/
488-
s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);
545+
s32_t SPIFFS_gc(spiffs *fs, u32_t size);
489546

490547
#if SPIFFS_TEST_VISUALISATION
491548
/**
@@ -511,8 +568,9 @@ u32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages);
511568
#endif
512569
#endif
513570

514-
515-
#ifdef __cplusplus
571+
#if SPIFFS_CACHE
572+
#endif
573+
#if defined(__cplusplus)
516574
}
517575
#endif
518576

0 commit comments

Comments
 (0)