Mbed OS 的文件系统

     Arm Mbed OS 的文件系统不仅仅支持SD 卡上的文件系统,而且支持 内部RAM,内部ROM,外接SPI /QSPI NOR Flash ROM等等。 

块设备

文件系统是建立在块设备上的,所以首先要定义块设备(BlockDevice)。

Mbed OS 中有许多种的BlockDevice ,但是最重要的是下面几种。

  1. SDBlockDevice SD 卡
  2. SPI Flash block device 外接的SPI NOR Flash
  3. FlashIAPBlockDevice  处理器内部的FLASH 作为块设备

例如  SD卡设备写成:

SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
 

也可以使用new 的方式

BlockDevice *sd=new SDBlockDevice(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);

文件系统

 Mbed OS 6 的文件系统有两种:

 FATFileSystem

适合SD卡的文件系统。

实例:

#include "mbed.h"
#include "SDBlockDevice.h"

SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
FATFileSystem fs("fs");
main(){
int err = fs.mount(&bd);
 FILE *f = fopen("/fs/hello.txt", "w+");
 fputs ("Hello The world",f);
 fclose(f);
}

LittleFileSystem

适合外接的Flash 存储器件。

实例   在内存建一个文件系统,BlockDevice 类型是heapBlockDevice,文件系统 LittleFileSystem

#include "LittleFileSystem.h"
#include <functional>
#include "HeapBlockDevice.h"
#include <iostream>
using namespace std;
#define BLOCK_SIZE 512

HeapBlockDevice bd(4096,512);
LittleFileSystem fs("fs");
uint8_t block[BLOCK_SIZE] = "Hello World!\n";
int main()
{   

  printf("\rFile System test\n");
  bd.init();
   bd_size_t read_size    = bd.get_read_size();
    bd_size_t program_size = bd.get_program_size();
    bd_size_t erase_size   = bd.get_erase_size();
    bd_size_t size         = bd.size();

    printf("--- Block device geometry ---\n");
    printf("read_size:    %lld B\n", read_size);
    printf("program_size: %lld B\n", program_size);
    printf("erase_size:   %lld B\n", erase_size);
    printf("size:         %lld B\n", size);
    printf("---\n");
   bd.erase(0, bd.size());  
   int  err=fs.reformat(&bd);
    if (err) {
        printf("reformat err \n");
    }
      err = fs.mount(&bd);
    if (err) {
        printf("mount err \n");
    }
    FILE *f = fopen("/fs/hello.txt", "wt");   
   fputs("Hello The world\n",f);
// close file
   fflush(f);
   fclose(f);
// open directory 
    printf("------Opening the root directory... ");
    DIR *d = opendir("/fs/");
        while (true) {
        struct dirent *e = readdir(d);
        if (!e) {
            break;
        }

        printf("    %s\n", e->d_name);
    }
closedir(d);
//Open File again
 printf("-----Opening the file again-----\n");
 // fflush(stdout);
  f = fopen("/fs/hello.txt", "r");
  fseek(f, 0L, SEEK_END);
 int sz = ftell(f);
printf("File Size=%d\n",sz);
 fseek(f,0L,SEEK_SET);
   if (!f) {
 cout <<"file open error"<<endl;
   }
 printf("----File Content-----\n");
    while (!feof(f)) {
        int c = fgetc(f);
        printf("%c", c);
    }
    printf("----Close The File-----");
    err = fclose(f);
 
}

这个程序调试过了,Mbed OS 6.2,搞了好久。遇到一个问题。当heapDevice 的长度设置为2048 的时候,再次打开文件时,文件是空的。后来将长度改为4096 就好了。

FlashIAPBlockDevice 好像在STM32 中不行,下载下去什么都没有

猜你喜欢

转载自blog.csdn.net/yaojiawan/article/details/108623552