|
| 1 | +// Released to the piublic domain by Earle F. Philhower, III in 2024 |
| 2 | + |
| 3 | +#include <LittleFS.h> |
| 4 | +#include <VFS.h> |
| 5 | +#include <SPI.h> |
| 6 | +#include <SDFS.h> |
| 7 | + |
| 8 | +// This are GP pins for SPI0 on the Raspberry Pi Pico board, and connect |
| 9 | +// to different *board* level pinouts. Check the PCB while wiring. |
| 10 | +// Only certain pins can be used by the SPI hardware, so if you change |
| 11 | +// these be sure they are legal or the program will crash. |
| 12 | +// See: https://datasheets.raspberrypi.com/picow/PicoW-A4-Pinout.pdf |
| 13 | +const int _MISO = 4; // AKA SPI RX |
| 14 | +const int _MOSI = 7; // AKA SPI TX |
| 15 | +const int _CS = 5; |
| 16 | +const int _SCK = 6; |
| 17 | +// SPI1 |
| 18 | +//const int _MISO = 8; // AKA SPI RX |
| 19 | +//const int _MOSI = 11; // AKA SPI TX |
| 20 | +//const int _CS = 9; |
| 21 | +//const int _SCK = 10; |
| 22 | + |
| 23 | +void setup() { |
| 24 | + delay(5000); |
| 25 | + if (!LittleFS.begin()) { |
| 26 | + Serial.printf("ERROR: Unable to start LittleFS. Did you select a filesystem size in the menus?\n"); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + SDFSConfig cfg; |
| 31 | + bool sd = false; |
| 32 | + if (_MISO == 0 || _MISO == 4 || _MISO == 16) { |
| 33 | + SPI.setRX(_MISO); |
| 34 | + SPI.setTX(_MOSI); |
| 35 | + SPI.setSCK(_SCK); |
| 36 | + SDFS.setConfig(SDFSConfig(_CS, SPI_HALF_SPEED, SPI)); |
| 37 | + sd = SDFS.begin(); |
| 38 | + } else if (_MISO == 8 || _MISO == 12) { |
| 39 | + SPI1.setRX(_MISO); |
| 40 | + SPI1.setTX(_MOSI); |
| 41 | + SPI1.setSCK(_SCK); |
| 42 | + SDFS.setConfig(SDFSConfig(_CS, SPI_HALF_SPEED, SPI1)); |
| 43 | + sd = SDFS.begin(); |
| 44 | + } else { |
| 45 | + Serial.println(F("ERROR: Unknown SPI Configuration")); |
| 46 | + } |
| 47 | + |
| 48 | + VFS.map("/lfs", LittleFS); // Onboard flash at /lfs |
| 49 | + if (sd) { |
| 50 | + VFS.map("/sd", SDFS); // SD card mapped to /sd |
| 51 | + } |
| 52 | + VFS.root(LittleFS); // Anything w/o a prefix maps to LittleFS |
| 53 | + |
| 54 | + Serial.printf("Writing to /lfs/text.txt\n"); |
| 55 | + FILE *f = fopen("/lfs/text.txt", "wb"); |
| 56 | + fwrite("hello littlefs", 14, 1, f); |
| 57 | + fclose(f); |
| 58 | + |
| 59 | + if (sd) { |
| 60 | + Serial.printf("Writing to /sd/test.txt, should not overwrite /lfs/text.txt!\n"); |
| 61 | + f = fopen("/sd/text.txt", "wb"); |
| 62 | + fwrite("hello sdfs", 10, 1, f); |
| 63 | + fclose(f); |
| 64 | + } |
| 65 | + |
| 66 | + f = fopen("/lfs/text.txt", "rb"); |
| 67 | + char buff[33]; |
| 68 | + bzero(buff, 33); |
| 69 | + fread(buff, 1, 32, f); |
| 70 | + fclose(f); |
| 71 | + Serial.printf("READ LFS> '%s'\n", buff); |
| 72 | + |
| 73 | + if (sd) { |
| 74 | + f = fopen("/sd/text.txt", "rb"); |
| 75 | + bzero(buff, 33); |
| 76 | + fread(buff, 1, 32, f); |
| 77 | + fclose(f); |
| 78 | + Serial.printf("READ SDFS> '%s'\n", buff); |
| 79 | + } |
| 80 | + |
| 81 | + f = fopen("/text.txt", "rb"); |
| 82 | + bzero(buff, 33); |
| 83 | + fread(buff, 1, 32, f); |
| 84 | + fclose(f); |
| 85 | + Serial.printf("READ default FS (LittleFS)> '%s'\n", buff); |
| 86 | + |
| 87 | + Serial.printf("\nTesting seeking within a file\n"); |
| 88 | + f = fopen("/lfs/text.txt", "rb"); |
| 89 | + for (int i = 0; i < 10; i ++) { |
| 90 | + fseek(f, i, SEEK_SET); |
| 91 | + bzero(buff, 33); |
| 92 | + fread(buff, 1, 32, f); |
| 93 | + Serial.printf("LFS SEEK %d> '%s'\n", i, buff); |
| 94 | + } |
| 95 | + fclose(f); |
| 96 | + |
| 97 | + Serial.printf("\nTesting fprintf and fgetc from LFS\n"); |
| 98 | + f = fopen("/lfs/printout.txt", "w"); |
| 99 | + for (int i = 0; i < 10; i++) { |
| 100 | + fprintf(f, "INT: %d\n", i); |
| 101 | + } |
| 102 | + fclose(f); |
| 103 | + |
| 104 | + Serial.printf("----\n"); |
| 105 | + f = fopen("/printout.txt", "r"); |
| 106 | + int x; |
| 107 | + while ((x = fgetc(f)) >= 0) { |
| 108 | + Serial.printf("%c", x); |
| 109 | + } |
| 110 | + Serial.printf("----\n"); |
| 111 | +} |
| 112 | + |
| 113 | +void loop() { |
| 114 | +} |
0 commit comments