Giter Club home page Giter Club logo

khoih-prog / fs_nano33ble Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 1.0 84 KB

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: GNU General Public License v3.0

C++ 63.35% C 35.19% Shell 1.45%
littlefs littlefs-mbed fatfs fatfs-mbed nrf52840 nano-33-ble nano-33-ble-sense mbed mbed-nano file-system

fs_nano33ble's Introduction

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

fs_nano33ble's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

kazukihiraizumi

fs_nano33ble's Issues

Freeze up when attempting to open a file.

Describe the bug

FILE *file = fopen(path, "w");

Stops all execution on nano 3.3 ble.

Steps to Reproduce

void Storage::WriteFile(const char * path, const char * message, size_t messageSize){
    bool isOpen = false;
    /* char fullPath[sizeof(MBED_FS_FILE_PREFIX) + sizeof(path)];
    strcpy(fullPath,MBED_FS_FILE_PREFIX);
    strcat(fullPath, path); */
    if(*_debug) _communication->Send("Writing file: ");
    if(*_debug) _communication->Send(path);

    FILE *file = fopen(path, "w");

    if (file)
    {
        isOpen = true;
        if(*_debug) _communication->Send(" => Open OK");
    }
    else
    {
        if(*_debug) _communication->Send(" => Open Failed");
    }

    if(isOpen){
        if (fwrite((uint8_t *) message, 1, messageSize, file))
        {
            if(*_debug) _communication->Send("* Writing OK");
        }
        else
        {
            if(*_debug) _communication->Send("* Writing failed");
        }

        fclose(file);
    }

}

Expected behavior

the file should open or error out.

Actual behavior

nothing happens, code stop executing

Please ensure to specify the following:

  • Arduino IDE version (e.g. 1.8.19) or Platform.io version
    Visual Studio Code
  • nRF52840 mbed Core Version (e.g. Arduino mbed_nano core v3.4.1, Seeeduino mbed core v2.7.2)
    BN: Arduino Nano 33 BLE
    VID: 2341
    PID: 805a
    SN: C0FE91FA29BB3E26

Updated to Core 3.5.4

Impossibile to create a class

Hi, I'm using a Seeeduino XIAO BLE board (it mounts nRF52840).
I edited the ".h" file to bypass the board check, given the fact that it is not listed in it.

However, when I run the example code, everything works as expected.

Then I decided to try to make a class out of it (ProjectA), in order to make my work easier.
So I begin to copy/paste block of code from the example to a blank project.

I managed to implement also ArduinoJson and vector.
It seemed to work fine till the end.

THAN the bad moment kicks in...
I put the .hpp and .cpp files in another project (ProjectB) to use the "library" I just made and BOOM nothing works anymore! Exact code.

So I tried to flash back the ProjectA it was working a while ago => it stopped working too O_o

So... I tried to upload your example code and worked. At this point I'm confused.

I attach the ProjectA that was actually working (I made several intermediate tests ArduinoJson was working good)
XiaoFS_Example.zip

Suppress warnings in FSNano33BLE.h

Is your feature request related to a problem? Please describe.

I like the library, and it's exactly what I needed so far, great work!
It is, though, a bit annoying seeing warnings like this when compiling the code (and can also make "real" warnings harder to find):

#warning Using default NANO33BLE_FS_SIZE_KB == 64KB

Describe the solution you'd like

Guarding these defines with something optional like "#ifdef FS_VERBOSE" would be sufficient.

Describe alternatives you've considered

Could also use something like "FS_WARN_SUPPRESS" to invert the logic from the above solution (ioptional flag to suppress the warnings explicitly).

Additional context

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.