Code Monkey home page Code Monkey logo

khoih-prog / littlefs_mbed_rp2040 Goto Github PK

View Code? Open in Web Editor NEW
20.0 2.0 4.0 5.54 MB

Wrapper of LittleFS for Arduino MBED RP2040 boards. This library facilitates your usage of LittleFS for the onboard flash. LittleFS supports power fail safety and high performance

License: GNU General Public License v3.0

C++ 51.12% C 46.91% Shell 1.96%
rp2040 nano-rp2040-connect storage data-storage raspberry-pi-pico littlefs littlefs-mbed mbed flash-storage posix

littlefs_mbed_rp2040's Introduction

LittleFS_Mbed_RP2040 Library

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

Donate to my libraries using BuyMeACoffee



Table of Contents



Important Notes

The LittleFS of the new Nano_RP2040_Connect board (see picture below), using ISSI Flash chip, is currently not working with Arduino mbed_rp2040 core 2.4.1+. Please downgrade to Arduino mbed_rp2040 core 2.3.1

Check RP2040 Connect board has faulty components in newest purchase #318 for more information of when and how the issue will be fixed.

The old board (see picture below), using Adesto Flash chip, is currently working with Arduino mbed_rp2040 core 2.4.1+.



Why do we need this LittleFS_Mbed_RP2040 library

Features

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

The filesystem access uses normal POSIX APIs or mbed FileSystem APIs


Currently supported Boards

  1. RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc. using Arduino-mbed RP2040 core


Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino
  2. Arduino mbed_rp2040 core 2.3.1- for RP2040-based boards using ISSI Flash chip, see New board, such as Arduino Nano RP2040 Connect, RASPBERRY_PI_PICO, etc.. GitHub release
  3. Arduino mbed_rp2040 core 2.4.1+ for Arduino (Use Arduino Board Manager) RP2040-based boards using Adesto Flash chip, see Old board, such as Arduino Nano RP2040 Connect, RASPBERRY_PI_PICO, etc.. Latest release is GitHub release


Installation

Use Arduino Library Manager

The best and easiest way is to use Arduino Library Manager. Search for LittleFS_Mbed_RP2040, 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 LittleFS_Mbed_RP2040 page.
  2. Download the latest release LittleFS_Mbed_RP2040-main.zip.
  3. Extract the zip file to LittleFS_Mbed_RP2040-main directory
  4. Copy whole LittleFS_Mbed_RP2040-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO

  1. Install VS Code
  2. Install PlatformIO
  3. Install LittleFS_Mbed_RP2040 library by using Library Manager. Search for LittleFS_Mbed_RP2040 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. LittleFS_Counting
  2. LittleFS_test


#define LFS_MBED_RP2040_VERSION_MIN_TARGET "LittleFS_Mbed_RP2040 v1.1.0"
#define LFS_MBED_RP2040_VERSION_MIN 1001000
#define _LFS_LOGLEVEL_ 1
#define RP2040_FS_SIZE_KB 64
#define FORCE_REFORMAT false
#include <LittleFS_Mbed_RP2040.h>
LittleFS_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 RP2040_FS_SIZE_KB
for (i = 0; i < RP2040_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 RP2040_FS_SIZE_KB
for (i = 0; i < RP2040_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)
delay(1000);
Serial.print("\nStart LittleFS_Test on ");
Serial.println(BOARD_NAME);
Serial.println(LFS_MBED_RP2040_VERSION);
#if defined(LFS_MBED_RP2040_VERSION_MIN)
if (LFS_MBED_RP2040_VERSION_INT < LFS_MBED_RP2040_VERSION_MIN)
{
Serial.print("Warning. Must use this example on Version equal or later than : ");
Serial.println(LFS_MBED_RP2040_VERSION_MIN_TARGET);
}
#endif
myFS = new LittleFS_MBED();
if (!myFS->init())
{
Serial.println("LITTLEFS Mount Failed");
return;
}
char fileName1[] = MBED_LITTLEFS_FILE_PREFIX "/hello1.txt";
char fileName2[] = MBED_LITTLEFS_FILE_PREFIX "/hello2.txt";
char message[] = "Hello from " BOARD_NAME "\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. LittleFS_Counting on RaspberryPi Pico

The following is the sample terminal output when running example LittleFS_Counting on MBED RaspberryPi Pico

Start LittleFS_Counting on RaspberryPi Pico
LittleFS_Mbed_RP2040 v1.1.0
[LFS] LittleFS size (KB) = 256
[LFS] LittleFS Mount OK
Deleting file: /littlefs/counts.txt => OK
Times have been run = 1
 => Open to write OK

Start LittleFS_Counting on RaspberryPi Pico
LittleFS_Mbed_RP2040 v1.1.0
[LFS] LittleFS size (KB) = 256
[LFS] LittleFS Mount OK
 => Open to read OK
Times have been run = 2
 => Open to write OK

Start LittleFS_Counting on RaspberryPi Pico
LittleFS_Mbed_RP2040 v1.1.0
[LFS] LittleFS size (KB) = 256
[LFS] LittleFS Mount OK
 => Open to read OK
Times have been run = 3
 => Open to write OK

2. LittleFS_Test on RaspberryPi Pico

The following is the sample terminal output when running example LittleFS_Test on MBED RaspberryPi Pico

Start LittleFS_Test on RaspberryPi Pico
LittleFS_Mbed_RP2040 v1.1.0
[LFS] LittleFS size (KB) = 256
[LFS] LittleFS Mount OK
====================================================
Writing file: /littlefs/hello1.txt => Open OK
* Writing OK
====================================================
Reading file: /littlefs/hello1.txt => Open OK
Hello from RaspberryPi Pico
====================================================
Appending file: /littlefs/hello1.txt => Open OK
* Appending OK
====================================================
Reading file: /littlefs/hello1.txt => Open OK
Hello from RaspberryPi Pico
Hello from RaspberryPi Pico
====================================================
Renaming file: /littlefs/hello1.txt to: /littlefs/hello2.txt => OK
====================================================
readCharsFromFile: /littlefs/hello2.txt => Open OK
Hello from RaspberryPi Pico
Hello from RaspberryPi Pico
====================================================
Deleting file: /littlefs/hello2.txt => OK
====================================================
Reading file: /littlefs/hello2.txt => Open Failed
====================================================
Testing file I/O with: /littlefs/hello1.txt => Open OK
- writing

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

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

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

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

Test complete


Debug

Debug is enabled by default on Serial.

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

#define LFS_DEBUG_OUTPUT    Serial

// These define's must be placed at the beginning before #include "LittleFS_Mbed_RP2040.h"
// _LFS_LOGLEVEL_ from 0 to 4
#define _LFS_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: LittleFS_Mbed_RP2040 issues


TO DO

  1. Search for bug and improvement.

DONE

  1. Basic LittleFS wrapper for RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, using Arduino-mbed RP2040 core
  2. Add Version String
  3. Add Table of Contents
  4. Add astyle using allman style. Restyle the library


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 Maximilian Gerhardt to create bug report RP2040_RTC_Time crashes Pico, does not work #3 and help investigate and fix the bug, leading to v1.0.3
maxgerhardt
Maximilian Gerhardt


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

littlefs_mbed_rp2040's People

Contributors

khoih-prog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

littlefs_mbed_rp2040's Issues

MBED crash - RP2040 rebooting

This is the output from Serial (it happens with both examples):
Start LittleFS_Counting on RaspberryPi Pico
LittleFS_Mbed_RP2040 v1.0.0
[LFS] LittleFS size (KB) = 256
[LFS] LittleFS Mount Fail
[LFS] Formatting...

You then see the standard LED blink (5 short and some long blinks) which is associated with an MBED failure.

If you then open up a separate Serial output on Serial1 you get the mbed debug info:

++ MbedOS Fault Handler ++

FaultType: HardFault

Context:
R0 : 20041F00
R1 : 2000BEEC
R2 : 100183E0
R3 : 4B32B500
R4 : 20041F00
R5 : FFFFFF76
R6 : 2000D630
R7 : 10016ED1
R8 : 00000000
R9 : 00000000
R10 : 00000000
R11 : 00000000
R12 : 00000004
SP : 2000B8E8
LR : 1000069F
PC : 1000480C
xPSR : 01000000
PSP : 2000B8C8
MSP : 2003FFC0
CPUID: 410CC601
Mode : Thread
Priv : Privileged
Stack: PSP

-- MbedOS Fault Handler --

++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x1000480C
Error Value: 0x2000CF8C
Current Thread: main Id: 0x2000D144 Entry: 0x100040C5 StackSize: 0x8000 StackMem: 0x20003998 SP: 0x2000B8E8
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&osver=61300&core=0x410CC601&comp=2&ver=110100&tgt=RASPBERRY_PI...

-- MbedOS Error Info --

What exactly is this?

Hello what does this mean?

char fileName[] = MBED_LITTLEFS_FILE_PREFIX "/counts.txt";

Is that C++ or C code? I mean you are constructing array of chars from define which is itself array of chars as here:

#define MBED_LITTLEFS_FILE_NAME "littlefs" #define MBED_LITTLEFS_FILE_PREFIX "/" MBED_LITTLEFS_FILE_NAME

and then you are using that path inside functions such as:

void writeFile(const char * path, const char * message, size_t messageSize)

which are pointers to chars... this is C right?

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.