You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+55-47
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,7 @@
73
73
74
74
The SPIFlashFileSystem (SFFS) library implements a basic [wear leveling](https://en.wikipedia.org/wiki/Wear_leveling) file system intended for use with SPI Flash devices, using either the built-in [hardware.spiflash](https://electricimp.com/docs/api/hardware/spiflash) object on imp003 and above, or an external SPI Flash plus the [SPIFlash library](https://github.com/electricimp/spiflash) on the imp001 and imp002.
75
75
76
-
**To add this library to your project, add `#require "SPIFlashFileSystem.device.lib.nut:2.0.0"` to the top of your device code.**
76
+
**To add this library to your project, add**`#require "SPIFlashFileSystem.device.lib.nut:2.0.0"`**to the top of your device code.**
77
77
78
78
## Overview of the File System
79
79
@@ -151,7 +151,7 @@ Because each sector contains a FileID and SpanID, the sectors can be anywhere in
151
151
152
152
When the SFFS deletes a file, it simply marks all of the pages the file was using as erased. In order to use those pages again in the future, we first need erase the sectors that the file used. This is done automatically through a process called garbage collection.
153
153
154
-
Each time a file is closed, or erased the SFFS determins whether or not it needs to run the garbage collector. It does this by comparing the number of free pages to the *autoGcThreshold*, which can be set with [setAutoGc](#setautogcnumpages) method.
154
+
Each time a file is closed or erased, the SFFS determines whether or not it needs to run the garbage collector. It does this by comparing the number of free pages to the *autoGcThreshold*, which can be set with the [*setAutoGc()*](#setautogcnumpages) method.
155
155
156
156
## Library Classes
157
157
@@ -216,7 +216,7 @@ If the *init()* method is called while the filesystem has files open, a `SPIFlas
216
216
217
217
### getFileList(*[orderByDate]*)
218
218
219
-
The *getFileList()* returns an array of file information identical to that passed into the *init()* callback. It takes an optional parameter: orderByData, a Boolean value that specifies whether the returned files should be sorted into date order for you. The default value is `false`.
219
+
The *getFileList()* returns an array of file information identical to that passed into the *init()* callback. It takes an optional parameter: *orderByDate*, which is a Boolean value that specifies whether the returned files should be sorted into date order for you. The default value is `false`.
This method returns information about the filesystem in the form of a table with the following keys:
258
258
259
-
| Key | Description |
260
-
| -----------|--------------- |
261
-
|*size*| The filesystem size in bytes |
262
-
|*len*| The number of files in the filesystem |
263
-
|*start*| The address of the first byte of SPI flash assigned to the filesystem |
264
-
|*end*| The address of the last byte of SPI flash assigned to the filesystem |
259
+
| Key | Description |
260
+
| --- | --- |
261
+
|*size*| The filesystem size in bytes |
262
+
|*len*| The number of files in the filesystem |
263
+
|*start*| The address of the first byte of SPI flash assigned to the filesystem |
264
+
|*end*| The address of the last byte of SPI flash assigned to the filesystem |
265
265
|*pages*| The number of pages available in the filesystem |
266
266
267
267
```squirrel
@@ -270,26 +270,25 @@ local d = sffs.dimensions();
270
270
271
271
### created(*fileRef*)
272
272
273
-
Gets the creation timestamp for a specified file reference (id or file name).
273
+
Gets the creation timestamp for a specified file reference (ID or file name).
274
274
275
275
```squirrel
276
-
// get creation date by file name
276
+
// Get creation date by file name
277
277
sffs.created("file.txt");
278
278
279
-
// get creation date by id
279
+
// Get creation date by ID
280
280
sffs.created(5);
281
281
```
282
282
283
283
### getFreeSpace()
284
284
285
285
This method returns an estimate of the free space available in the filesystem. Smaller files have more overhead than larger files so it is impossible to know exactly how much space is free. The information is returned as a table with two keys:
286
286
287
-
| Key | Description |
288
-
| -------------|--------------- |
289
-
|*free*| The estimated free space in bytes |
287
+
| Key | Description |
288
+
| --- | --- |
289
+
|*free*| The estimated free space in bytes |
290
290
|*freeable*| The estimated space in bytes that may be freed through further garbage collection |
291
291
292
-
293
292
### isFileOpen(*filename*)
294
293
295
294
Returns `true` or `false` according to whether or not the specified file is currently open.
@@ -352,7 +351,7 @@ This method erases all files within the filesystem. It will return with an error
352
351
353
352
### setAutoGc(*numPages*)
354
353
355
-
The *setAutoGc()* method sets the *autoGcThresgold* property. The default *autoGcThresgold* property is 4. The garbage collector will automatically run when the filesystem has fewer than *autoGcThreshold* pages.
354
+
The *setAutoGc()* method sets the *autoGcThresgold* property. The garbage collector will automatically run when the filesystem has fewer than *autoGcThreshold* pages. The default *autoGcThresgold* value is 4.
356
355
357
356
Setting *numPages* to 0 will turn off automatic garbage collection.
358
357
@@ -366,16 +365,15 @@ sffs.setAutoGc(10);
366
365
367
366
The *gc()* method manually starts the garbage collection process. The SPIFlashFileSystem is designed in such a way that the auto garbage collection *should* be sufficient, and you should never need to manually call the *gc()* method.
368
367
369
-
If the *numPages* parameter is specified, the garbage collector will free up to *numPages* pages and return when it completes (this is what happens when the garbage collector runs because the file system needs a page and none are free). If the *numPages* parameter is ommited, the garbage collector will run asynchronously in the background (this is what happens when the garbage collector runs because free pages drops below the value of *autoGcThreshold*).
370
-
368
+
If the *numPages* parameter is specified, the garbage collector will free up to *numPages* pages and return when it completes (this is what happens when the garbage collector runs because the file system needs a page and none are free). If the *numPages* parameter is omited, the garbage collector will run asynchronously in the background (this is what happens when the garbage collector runs because free pages drops below the value of *autoGcThreshold*).
371
369
372
370
## SPIFlashFileSystem.File
373
371
374
-
A *SPIFlashFileSystem.File* object is returned from the SPIFlashFileSystem each time a file is opened. The *SPIFlashFileSystem.File* object acts as a stream, with an internal pointer which can be manipulated with a variety of methods in the *SPIFlashFileSystem.File* class. Typically, you will not need to instantiate SPIFlashFileSystem.File objects yourself.
372
+
A *SPIFlashFileSystem.File* object is returned from the SPIFlashFileSystem each time a file is opened. The *SPIFlashFileSystem.File* object acts as a stream, with an internal pointer which can be manipulated with a variety of methods in the *SPIFlashFileSystem.File* class. Typically, you will not need to instantiate *SPIFlashFileSystem.File* objects yourself.
The constructor creates a file record. It takes the current SPIFlashFileSystem instance, the file’s unique integer ID, the index of the file in the internal record of open files, the current file’s filename, and its access mode: read or write, represented as the strings `"r"` and `"w"`, respectively.
376
+
The constructor creates a file record. It takes the current *SPIFlashFileSystem* instance, the file’s unique integer ID, the index of the file in the internal record of open files, the current file’s filename, and its access mode: read or write, represented as the strings `"r"` and `"w"`, respectively.
Writes a string or blob to the end of a file's data opened with mode `"w"`. If you attempt to write to a file opened with mode `"r"` a `SPIFlashFileSystem.ERR_WRITE_R_FILE` error will be thrown.
436
434
437
-
**Note:** The page header is not written to the SPI Flash until the entire page is written, or the [close](#close) method is called.
435
+
**Note** The page header is not written to the SPI Flash until the entire page is written, or the [close](#close) method is called.
438
436
439
437
In the following example, we download a file in chunks from the agent:
440
438
@@ -459,7 +457,6 @@ The *close()* method closes a file, and writes data to the SPI Flash if required
459
457
460
458
*See [write()](#writedata) for sample usage.*
461
459
462
-
463
460
## SPIFlashFileSystem.FAT
464
461
465
462
A SPIFlashFileSystem.FAT object is automatically generated when the filesystem is initialized. It records the file allocation table (FAT).
@@ -474,15 +471,15 @@ The constructor takes a master *SPIFlashFileSystem* object and, optionally, the
474
471
475
472
This method scans the filesystem for files and uses them to construct the file allocation table.
476
473
477
-
### get(*fileReference*)
474
+
### get(*fileRef*)
478
475
479
-
This method retrieves information about a specific file from the file allocation table. The value passed into *fileReference* can be either a file’s name (a string) or its ID (an integer). The method returns a table of information containing the following keys:
476
+
This method retrieves information about a specific file from the file allocation table. The value passed into *fileRef* can be either a file’s name (a string) or its ID (an integer). The method returns a table of information containing the following keys:
480
477
481
478
| Key | Description |
482
-
| -----|--------------- |
479
+
| --- | --- |
483
480
|*id*| The file’s ID (integer) |
484
481
|*fname*| The file’s filename |
485
-
|*spans*|???|
482
+
|*spans*|The spans of the file|
486
483
|*pages*| An array of pages in which the file is stored |
487
484
|*pageCount*| The number of pages in which the file is stored |
488
485
|*sizes*| An array of the size (in bytes) of the file chunks each page contains |
@@ -491,81 +488,92 @@ This method retrieves information about a specific file from the file allocation
491
488
492
489
### getFileList(*[orderByDate]*)
493
490
494
-
The *getFileList()* returns an array of file information, one entry per file in the FAT. It takes an optional parameter: *orderByData*, a Boolean value that specifies whether the returned files should be sorted into date order for you. The default value is `false`. The information for each file is a table with the following keys:
491
+
The *getFileList()* returns an array of file information, one entry per file in the FAT. It takes an optional parameter: *orderByDate*, a Boolean value that specifies whether the returned files should be sorted into date order for you. The default value is `false`. The information for each file is a table with the following keys:
495
492
496
493
| Key | Description |
497
-
| -----|--------------- |
494
+
| --- | --- |
498
495
|*id*| The file’s ID (integer) |
499
496
|*fname*| The file’s filename |
500
497
|*size*| The total size of the file |
501
498
|*created*| A timestamp indicating the date and time of the file’s creation |
502
499
503
500
### getFileId(*filename*)
501
+
504
502
This method takes a given file’s name and returns the unique integer by which it is referenced in the file allocation table.
505
503
506
-
### fileExists(*fileReference*)
507
-
This method returns `true` or `false` according to whether or not the specified file exists in the file allocation table. The value passed into fileReference can be either a file’s name (a string) or its ID (an integer).
504
+
### fileExists(*fileRef*)
505
+
506
+
This method returns `true` or `false` according to whether or not the specified file exists in the file allocation table. The value passed into *fileRef* can be either a file’s name (a string) or its ID (an integer).
508
507
509
508
### getFreePage()
510
-
This method returns the address of a random free page in the filesystem. It will return an error, *SPIFlashFileSystem.ERR_NO_FREE_SPACE*, if it is unable to do so because there is no free space left.
509
+
510
+
This method returns the address of a random free page in the filesystem. It will return an error, `SPIFlashFileSystem.ERR_NO_FREE_SPACE`, if it is unable to do so because there is no free space left.
511
511
512
512
### markPage(*address, status*)
513
-
This method sets the status of the page at address. Status values can be any of the constants: *SPIFLASHFILESYSTEM_STATUS_FREE, SPIFLASHFILESYSTEM_STATUS_USED*, *SPIFLASHFILESYSTEM_STATUS_ERASED or SPIFLASHFILESYSTEM_STATUS_BAD*.
513
+
514
+
This method sets the status of the page at address. Status values can be any of the constants: *SPIFLASHFILESYSTEM_STATUS_FREE*, *SPIFLASHFILESYSTEM_STATUS_USED*, *SPIFLASHFILESYSTEM_STATUS_ERASED* or *SPIFLASHFILESYSTEM_STATUS_BAD*.
514
515
515
516
### addPage(*fileId, page*)
517
+
516
518
This method adds the specified page to the file allocation table for the specified file.
517
519
518
-
### getPageCount(*fileReference*)
519
-
This method returns the number of pages the files specified by the parameter fileReference comprises. The value passed into fileReference can be either a file’s name (a string) or its ID (an integer).
520
+
### getPageCount(*fileRef*)
521
+
522
+
This method returns the number of pages the files specified by the parameter fileReference comprises. The value passed into *fileRef* can be either a file’s name (a string) or its ID (an integer).
520
523
521
-
### forEachPage(*fileReference, callback*)
522
-
This method iterates over each page used to record the file specified by the parameter fileReference. For each page, the function passed into the parameter callback is called. This function takes a single parameter: the page it is passed.
524
+
### forEachPage(*fileRef, callback*)
525
+
526
+
This method iterates over each page used to record the file specified by the parameter *fileRef*. For each page, the function passed into the parameter callback is called. This function takes a single parameter: the page it is passed.
523
527
524
528
### pagesOrderedBySpan(*pages*)
529
+
525
530
This method returns an array of pages ordered by the span.
526
531
527
532
### addSizeToLastSpan(*fileId, bytes*)
533
+
528
534
This method updates the size of the last span in a file.
529
535
530
536
### set(*fileId, file*)
537
+
531
538
This method sets the span for the file specified by its ID.
532
539
533
540
### removeFile(*filename*)
541
+
534
542
This method removes the named file from the file allocation table.
535
543
536
544
### getSectorMap()
545
+
537
546
This method returns the file allocation table’s sector map.
538
547
539
548
### getStats()
540
-
This method returns a table of page usage information, specifically the number of pages in the filesystem in each of the four status categories. This information is returned as a table with the keys *free, used, erased and bad*. Each key’s value is an integer: the number of pages in the filesystem with that status.
549
+
550
+
This method returns a table of page usage information, specifically the number of pages in the filesystem in each of the four status categories. This information is returned as a table with the keys *free*, *used*, *erased* and *bad*. Each key’s value is an integer: the number of pages in the filesystem with that status.
541
551
542
552
### describe()
543
-
This method is intended to assist with debugging: it provides a readout of the current number of files in the file allocation table, and lists each file by name, the number of pages it spans and its total size.
544
553
554
+
This method is intended to assist with debugging: it provides a readout of the current number of files in the file allocation table, and lists each file by name, the number of pages it spans and its total size.
545
555
546
556
## Testing
547
557
548
-
Repository contains [impUnit](https://github.com/electricimp/impUnit) tests and a configuration for [impTest](https://github.com/electricimp/impTest).
558
+
The repository contains [impUnit](https://github.com/electricimp/impUnit) tests and a configuration for [impTest](https://github.com/electricimp/impTest).
549
559
550
-
Tests can be launched with:
560
+
The tests can be launched with:
551
561
552
562
```bash
553
563
imptest test
554
564
```
555
565
556
-
By default configuration for the testing is read from [.imptest](https://github.com/electricimp/impTest/blob/develop/docs/imptest-spec.md).
557
-
558
-
To run test with your settings (for example while you are developing), create your copy of **.imptest** file and name it something like **.imptest.local**, then run tests with:
566
+
By default, the configuration for the testing is read from the file [*.imptest*](https://github.com/electricimp/impTest/blob/develop/docs/imptest-spec.md). To run tests with your settings (for example while you are developing), create a copy of the *.imptest* file and rename it (for example, *.imptest.local*), then run the tests with:
559
567
560
568
```bash
561
569
imptest test -c .imptest.local
562
570
```
563
571
564
572
### Hardware
565
573
566
-
Tests require an [Amy](https://electricimp.com/docs/hardware/resources/reference-designs/amy/) board or [imp003 Evaluation Board](https://electricimp.com/docs/hardware/imp003evb/). Any other boards with imp003 and above containing SPI flash with available user space may work too.
574
+
The provided tests require an [imp003 Breakout Board](https://electricimp.com/docs/hardware/resources/reference-designs/imp003breakout/) or [imp003 Evaluation Board](https://electricimp.com/docs/hardware/imp003evb/). Any other boards with imp003 and above containing SPI flash with available user space may work too.
0 commit comments