Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
/ FS_Nano33BLE Public archive

Wrapper of FS (LittleFS or not-advisable FATFS) for Arduino MBED nRF52840-based boards, such as Nano_33_BLE boards. This library facilitates your usage of FS (LittleFS or FATFS) for the onboard flash. FS supports power fail safety and high performance

License

Notifications You must be signed in to change notification settings

khoih-prog/FS_Nano33BLE

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FS_Nano33BLE Library

arduino-library-badge GitHub release GitHub contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Why do we need this FS_Nano33BLE library

Important Notes

Avoid using FATFS because the somehow (issue with the core ???) it's OK to use only with 512KB. Please use the better LittleFS, where you can select the size anywhere from 64KB to 512KB.

Features

This library is just a simple LittleFS wrapper to facilitate your usage of LittleFS for the onboard flash on MBED nRF52840-based boards such as Nano_33_BLE, Nano_33_BLE_Sense, using Arduino-mbed mbed_nano core

The filesystem access uses normal POSIX APIs or mbed FileSystem APIs


Currently supported Boards

  1. MBED nRF52840-based boards such as Nano_33_BLE, Nano_33_BLE_Sense, etc. using Arduino-mbed mbed_nano core
  2. Seeeduino nRF52840-based boards such as SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE, etc. using Seeeduino mbed core


Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. Arduino mbed_nano core 3.4.1+ for Arduino (Use Arduino Board Manager) MBED nRF52840-based boards such as Nano_33_BLE, Nano_33_BLE_Sense. GitHub release
  3. Seeeduino mbed core 2.7.2+ for Seeeduino nRF52840-based boards such as SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE


Installation

Use Arduino Library Manager

The best and easiest way is to use Arduino Library Manager. Search for FS_Nano33BLE, then select / install the latest version. You can also use this link arduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

  1. Navigate to FS_Nano33BLE page.
  2. Download the latest release FS_Nano33BLE-main.zip.
  3. Extract the zip file to FS_Nano33BLE-main directory
  4. Copy whole FS_Nano33BLE-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO

  1. Install VS Code
  2. Install PlatformIO
  3. Install FS_Nano33BLE library by using Library Manager. Search for FS_Nano33BLE in Platform.io Author's Libraries
  4. Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File


Examples

  1. FS_Counting
  2. FS_Test


Example FS_Test

#define FS_NANO33BLE_VERSION_MIN_TARGET "FS_Nano33BLE v1.2.1"
#define FS_NANO33BLE_VERSION_MIN 1002001
#define _FS_LOGLEVEL_ 1
// Min NANO33BLE_FS_SIZE_KB must be 64KB. If defined smalller => auto adjust to 64KB
// Max NANO33BLE_FS_SIZE_KB must be 512KB. If defined larger => auto adjust to 512KB
#define NANO33BLE_FS_SIZE_KB 256
#define FORCE_REFORMAT false
// Default USING_LITTLEFS. Uncomment to not USING_LITTLEFS => USING_FATFS.
// It's advisable not to use FATFS, as the NANO33BLE_FS_SIZE_KB must be auto-adjusted to 512KB
//#define USING_LITTLEFS false
#include <FS_Nano33BLE.h>
FileSystem_MBED *myFS;
void readCharsFromFile(const char * path)
{
Serial.print("readCharsFromFile: ");
Serial.print(path);
FILE *file = fopen(path, "r");
if (file)
{
Serial.println(" => Open OK");
}
else
{
Serial.println(" => Open Failed");
return;
}
char c;
while (true)
{
c = fgetc(file);
if ( feof(file) )
{
break;
}
else
Serial.print(c);
}
fclose(file);
}
void readFile(const char * path)
{
Serial.print("Reading file: ");
Serial.print(path);
FILE *file = fopen(path, "r");
if (file)
{
Serial.println(" => Open OK");
}
else
{
Serial.println(" => Open Failed");
return;
}
char c;
uint32_t numRead = 1;
while (numRead)
{
numRead = fread((uint8_t *) &c, sizeof(c), 1, file);
if (numRead)
Serial.print(c);
}
fclose(file);
}
void writeFile(const char * path, const char * message, size_t messageSize)
{
Serial.print("Writing file: ");
Serial.print(path);
FILE *file = fopen(path, "w");
if (file)
{
Serial.println(" => Open OK");
}
else
{
Serial.println(" => Open Failed");
return;
}
if (fwrite((uint8_t *) message, 1, messageSize, file))
{
Serial.println("* Writing OK");
}
else
{
Serial.println("* Writing failed");
}
fclose(file);
}
void appendFile(const char * path, const char * message, size_t messageSize)
{
Serial.print("Appending file: ");
Serial.print(path);
FILE *file = fopen(path, "a");
if (file)
{
Serial.println(" => Open OK");
}
else
{
Serial.println(" => Open Failed");
return;
}
if (fwrite((uint8_t *) message, 1, messageSize, file))
{
Serial.println("* Appending OK");
}
else
{
Serial.println("* Appending failed");
}
fclose(file);
}
void deleteFile(const char * path)
{
Serial.print("Deleting file: ");
Serial.print(path);
if (remove(path) == 0)
{
Serial.println(" => OK");
}
else
{
Serial.println(" => Failed");
return;
}
}
void renameFile(const char * path1, const char * path2)
{
Serial.print("Renaming file: ");
Serial.print(path1);
Serial.print(" to: ");
Serial.print(path2);
if (rename(path1, path2) == 0)
{
Serial.println(" => OK");
}
else
{
Serial.println(" => Failed");
return;
}
}
void testFileIO(const char * path)
{
Serial.print("Testing file I/O with: ");
Serial.print(path);
#define BUFF_SIZE 512
static uint8_t buf[BUFF_SIZE];
FILE *file = fopen(path, "w");
if (file)
{
Serial.println(" => Open OK");
}
else
{
Serial.println(" => Open Failed");
return;
}
size_t i;
Serial.println("- writing" );
uint32_t start = millis();
size_t result = 0;
// Write a file only 1/4 of NANO33BLE_FS_SIZE_KB
for (i = 0; i < NANO33BLE_FS_SIZE_KB / 2; i++)
{
result = fwrite(buf, BUFF_SIZE, 1, file);
if ( result != 1)
{
Serial.print("Write result = ");
Serial.println(result);
Serial.print("Write error, i = ");
Serial.println(i);
break;
}
}
Serial.println("");
uint32_t end = millis() - start;
Serial.print(i / 2);
Serial.print(" Kbytes written in (ms) ");
Serial.println(end);
fclose(file);
printLine();
/////////////////////////////////
file = fopen(path, "r");
start = millis();
end = start;
i = 0;
if (file)
{
start = millis();
Serial.println("- reading" );
result = 0;
fseek(file, 0, SEEK_SET);
// Read file only 1/4 of NANO33BLE_FS_SIZE_KB
for (i = 0; i < NANO33BLE_FS_SIZE_KB / 2; i++)
{
result = fread(buf, BUFF_SIZE, 1, file);
if ( result != 1 )
{
Serial.print("Read result = ");
Serial.println(result);
Serial.print("Read error, i = ");
Serial.println(i);
break;
}
}
Serial.println("");
end = millis() - start;
Serial.print((i * BUFF_SIZE) / 1024);
Serial.print(" Kbytes read in (ms) ");
Serial.println(end);
fclose(file);
}
else
{
Serial.println("- failed to open file for reading");
}
}
void printLine()
{
Serial.println("****************************************************");
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(1000);
Serial.print("\nStart FS_Test on ");
Serial.println(BOARD_NAME);
Serial.println(FS_NANO33BLE_VERSION);
#if defined(FS_NANO33BLE_VERSION_MIN)
if (FS_NANO33BLE_VERSION_INT < FS_NANO33BLE_VERSION_MIN)
{
Serial.print("Warning. Must use this example on Version equal or later than : ");
Serial.println(FS_NANO33BLE_VERSION_MIN_TARGET);
}
#endif
Serial.print("FS_size (KB) = ");
Serial.println(NANO33BLE_FS_SIZE_KB);
Serial.print("FS_ Start Address = 0x");
Serial.println(NANO33BLE_FS_START, HEX);
myFS = new FileSystem_MBED();
if (!myFS->init())
{
Serial.println("FS Mount Failed");
return;
}
char fileName1[] = MBED_FS_FILE_PREFIX "/hello1.txt";
char fileName2[] = MBED_FS_FILE_PREFIX "/hello2.txt";
char message[] = "Hello from Nano_33_BLE\n";
printLine();
writeFile(fileName1, message, sizeof(message));
printLine();
readFile(fileName1);
printLine();
appendFile(fileName1, message, sizeof(message));
printLine();
readFile(fileName1);
printLine();
renameFile(fileName1, fileName2);
printLine();
readCharsFromFile(fileName2);
printLine();
deleteFile(fileName2);
printLine();
readFile(fileName2);
printLine();
testFileIO(fileName1);
printLine();
testFileIO(fileName2);
printLine();
deleteFile(fileName1);
printLine();
deleteFile(fileName2);
printLine();
Serial.println( "\nTest complete" );
}
void loop()
{
}



Debug Terminal Output Samples

1. FS_Counting on Nano 33 BLE with LittleFS size 256KB

The following is the sample terminal output when running example FS_Counting on MBED Nano_33_BLE using LittleFS

Start FS_Test on Nano 33 BLE
LittleFS_Nano33BLE v1.2.1
FS_size (KB) = 256
FS_ Start Address = 0xC0000
[FS] LittleFS size (KB) = 256
[LFS] LittleFS Mount OK
Deleting file: /littlefs/counts.txt => OK
Times have been run = 1
 => Open to write OK

Start FS_Test on Nano 33 BLE
LittleFS_Nano33BLE v1.2.1
[LFS] LittleFS size (KB) = 256
[LFS] LittleFS Mount OK
 => Open to read OK
Times have been run = 2
 => Open to write OK

Start FS_Test on Nano 33 BLE
LittleFS_Nano33BLE v1.2.1
[LFS] LittleFS size (KB) = 256
[LFS] LittleFS Mount OK
 => Open to read OK
Times have been run = 3
 => Open to write OK

2. FS_Test on Nano 33 BLE with LittleFS size 256KB

The following is the sample terminal output when running example FS_Test on MBED Nano_33_BLE using LittleFS

Start FS_Test on Nano 33 BLE
LittleFS_Nano33BLE v1.2.1
FS_size (KB) = 256
FS_ Start Address = 0xC0000
[FS] LittleFS size (KB) = 256
[LFS] LittleFS Mount Fail
[LFS] Formatting... 
[LFS] 
FS Format OK. Mounting
====================================================
Writing file: /fs/hello1.txt => Open OK
* Writing OK
====================================================
Reading file: /fs/hello1.txt => Open OK
Hello from Nano_33_BLE
====================================================
Appending file: /fs/hello1.txt => Open OK
* Appending OK
====================================================
Reading file: /fs/hello1.txt => Open OK
Hello from Nano_33_BLE
Hello from Nano_33_BLE
====================================================
Renaming file: /fs/hello1.txt to: /fs/hello2.txt => OK
====================================================
readCharsFromFile: /fs/hello2.txt => Open OK
Hello from Nano_33_BLE
Hello from Nano_33_BLE
====================================================
Deleting file: /fs/hello2.txt => OK
====================================================
Reading file: /fs/hello2.txt => Open Failed
====================================================
Testing file I/O with: /fs/hello1.txt => Open OK
- writing

64 Kbytes written in (ms) 2461
====================================================
- reading

64 Kbytes read in (ms) 7
====================================================
Testing file I/O with: /fs/hello2.txt => Open OK
- writing

64 Kbytes written in (ms) 2460
====================================================
- reading

64 Kbytes read in (ms) 7
====================================================
Deleting file: /fs/hello1.txt => OK
====================================================
Deleting file: /fs/hello2.txt => OK
====================================================

Test complete

3. FS_Test on Nano 33 BLE with FATFS size 512KB

The following is the sample terminal output when running example FS_Test on MBED Nano_33_BLE using FATFS

Start FS_Test on Nano 33 BLE
FATFS_Nano33BLE v1.2.1
FS_size (KB) = 512
FS_ Start Address = 0x80000
[FS] LittleFS size (KB) = 512
[LFS] FATFS Mount OK
====================================================
Writing file: /fs/hello1.txt => Open OK
* Writing OK
====================================================
Reading file: /fs/hello1.txt => Open OK
Hello from Nano_33_BLE
====================================================
Appending file: /fs/hello1.txt => Open OK
* Appending OK
====================================================
Reading file: /fs/hello1.txt => Open OK
Hello from Nano_33_BLE
Hello from Nano_33_BLE
====================================================
Renaming file: /fs/hello1.txt to: /fs/hello2.txt => OK
====================================================
readCharsFromFile: /fs/hello2.txt => Open OK
Hello from Nano_33_BLE
Hello from Nano_33_BLE
====================================================
Deleting file: /fs/hello2.txt => OK
====================================================
Reading file: /fs/hello2.txt => Open Failed
====================================================
Testing file I/O with: /fs/hello1.txt => Open OK
- writing

64 Kbytes written in (ms) 4374
====================================================
- reading

64 Kbytes read in (ms) 15
====================================================
Testing file I/O with: /fs/hello2.txt => Open OK
- writing

64 Kbytes written in (ms) 4374
====================================================
- reading

64 Kbytes read in (ms) 15
====================================================
Deleting file: /fs/hello1.txt => OK
====================================================
Deleting file: /fs/hello2.txt => OK
====================================================

Test complete


Debug

Debug is enabled by default on Serial.

You can also change the debugging level (FS_LOGLEVEL) from 0 to 4

#define FS_DEBUG_OUTPUT    Serial

// These define's must be placed at the beginning before #include "FS_Nano33BLE.h"
// _FS_LOGLEVEL_ from 0 to 4
#define _FS_LOGLEVEL_      0

Troubleshooting

If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.

Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.



Issues

Submit issues to: FS_Nano33BLE issues


TO DO

  1. Search for bug and improvement.

DONE

  1. Basic LittleFS wrapper for MBED nRF52840-based boards such as Nano_33_BLE, Nano_33_BLE_Sense, using Arduino-mbed mbed_nano core
  2. Add Version String
  3. Add Table of Contents
  4. Fix multiple-definitions linker error
  5. Use correct NANO33BLE_FS_START address for LittleFS without wasting flash space. Check Half size of flash #2
  6. Add support to Seeeduino nRF52840-based boards such as SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE, etc. using Seeeduino mbed core
  7. Add astyle using allman style. Restyle the library
  8. Display compile warning only when _FS_LOGLEVEL_ > 3


Contributions and Thanks

Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.

  1. Thanks to Rob Probin to report issue Half size of flash #2 leading to v1.2.0
robzed
Rob Probin


Contributing

If you want to contribute to this project:

  • Report bugs and errors
  • Ask for enhancements
  • Create issues and pull requests
  • Tell other people about this library

License

  • The library is licensed under GPLv3

Copyright

Copyright (c) 2021- Khoi Hoang