Hi everyone, I hope you are well.
I was trying to study the existing code of how to write/read, format/mount internal flash of Nano 33 BLE using FAT FILESYSTEM the origin code is as follows:
#include "FlashIAPBlockDevice.h"
#include "FATFileSystem.h"
REDIRECT_STDOUT_TO(Serial)
static mbed::FATFileSystem fs("fs");
static FlashIAPBlockDevice bd(0x80000, 0x80000);
void return_error(int ret_val) {
if (ret_val)
printf("Failure. %d\r\n", ret_val);
else
printf("done.\r\n");
}
void errno_error(void* ret_val) {
if (ret_val == NULL)
printf(" Failure. %d \r\n", errno);
else
printf(" done.\r\n");
}
void setup() {
Serial.begin(115200);
while (!Serial);
// put your setup code here, to run once:
int error = 0;
printf("Welcome to the filesystem example.\r\n"
"Formatting a FAT, RAM-backed filesystem. ");
error = mbed::FATFileSystem::format(&bd);
return_error(error);
printf("Mounting the filesystem on \"/fs\". ");
error = fs.mount(&bd);
return_error(error);
printf("Opening a new file, numbers.txt.");
FILE* fd = fopen("/fs/numbers.txt", "w");
errno_error(fd);
for (int i = 0; i < 20; i++) {
printf("Writing decimal numbers to a file (%d/20)\r\n", i);
fprintf(fd, "%d\r\n", i);
}
printf("Writing decimal numbers to a file (20/20) done.\r\n");
printf("Closing file.");
fclose(fd);
printf(" done.\r\n");
printf("Re-opening file read-only.");
fd = fopen("/fs/numbers.txt", "r");
errno_error(fd);
printf("Dumping file to screen.\r\n");
char buff[16] = {0};
while (!feof(fd)) {
int size = fread(&buff[0], 1, 15, fd);
fwrite(&buff[0], 1, size, stdout);
}
printf("EOF.\r\n");
printf("Closing file.");
fclose(fd);
printf(" done.\r\n");
printf("Opening root directory.");
DIR* dir = opendir("/fs/");
errno_error(fd);
struct dirent* de;
printf("Printing all filenames:\r\n");
while ((de = readdir(dir)) != NULL) {
printf(" %s\r\n", &(de->d_name)[0]);
}
printf("Closing root directory. ");
error = closedir(dir);
return_error(error);
printf("Filesystem Demo complete.\r\n");
}
void loop() {
// put your main code here, to run repeatedly:
}
This code was working perfectly without any issue.
Then I compiled and uploaded the following code
PLEASE DO NOT RUN IT AND UPLOAD IT IN YOUR DEVICE MIGHT DAMAGE YOUR NANO 33
#include "FlashIAPBlockDevice.h"
#include "FATFileSystem.h"
REDIRECT_STDOUT_TO(Serial)
static mbed::FATFileSystem fs("fs");
static FlashIAPBlockDevice bd(0x8000, 0x8000); // origin ( bd(0x80000, 0x80000)) // change here
void return_error(int ret_val) {
if (ret_val)
printf("Failure. %d\r\n", ret_val);
else
printf("done.\r\n");
}
void errno_error(void* ret_val) {
if (ret_val == NULL)
printf(" Failure. %d \r\n", errno);
else
printf(" done.\r\n");
}
void setup() {
Serial.begin(115200);
while (!Serial);
// put your setup code here, to run once:
int error = 0;
printf("Welcome to the filesystem example.\r\n"
"Formatting a FAT, RAM-backed filesystem. ");
error = mbed::FATFileSystem::format(&bd);
return_error(error);
//printf("Mounting the filesystem on \"/fs\". "); // comment out to see what will happen
//error = fs.mount(&bd); //comment out to see what will happen
//return_error(error); //comment out to see what will happen
printf("Opening a new file, numbers.txt.");
FILE* fd = fopen("/fs/numbers.txt", "w+");
errno_error(fd);
for (int i = 0; i < 20; i++) {
printf("Writing decimal numbers to a file (%d/20)\r\n", i);
fprintf(fd, "%d\r\n", i);
}
printf("Writing decimal numbers to a file (20/20) done.\r\n");
printf("Closing file.");
fclose(fd);
printf(" done.\r\n");
printf("Re-opening file read-only.");
fd = fopen("/fs/numbers.txt", "r+");
errno_error(fd);
printf("Dumping file to screen.\r\n");
char buff[16] = {0};
while (!feof(fd)) {
int size = fread(&buff[0], 1, 15, fd);
fwrite(&buff[0], 1, size, stdout);
}
printf("EOF.\r\n");
printf("Closing file.");
fclose(fd);
printf(" done.\r\n");
printf("Opening root directory.");
DIR* dir = opendir("/fs/");
errno_error(fd);
struct dirent* de;
printf("Printing all filenames:\r\n");
while ((de = readdir(dir)) != NULL) {
printf(" %s\r\n", &(de->d_name)[0]);
}
printf("Closing root directory. ");
error = closedir(dir);
return_error(error);
printf("Filesystem Demo complete.\r\n");
}
void loop() {
// put your main code here, to run repeatedly:
}
When I uploaded the above code, I lost the serial port, and I couldn't connect it. I try to reset it using the RST push Button, and nothing happen, I applied a HIGH signal to RST pin and also nothing happens.
I tried twice push of reset button to run the bootloader, but nothing works. No LEDs are blinking.
I am afraid that I damaged the internet flash where the bootloader stored.
Could you please review the second code. I just comment out the fs.mount() function and changed the amount of bytes used to store data
static FlashIAPBlockDevice bd(0x8000, 0x8000); // origin ( bd(0x80000, 0x80000)) // change here
//printf("Mounting the filesystem on \"/fs\". "); // comment out to see what will happen
//error = fs.mount(&bd); //comment out to see what will happen
//return_error(error); //comment out to see what will happen
Thanks in advance for your time and concern