Library for SD card

Dependents:   SDFileSystem_HelloWorld Sharp_ce140f_emul

Committer:
ffxx68
Date:
Tue Jul 19 13:49:28 2022 +0000
Revision:
2:02f003d025a7
Parent:
0:3bdfc1556537
SD Card handling added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AlexVC97 0:3bdfc1556537 1 /* mbed Microcontroller Library - MemFileSystem
AlexVC97 0:3bdfc1556537 2 * Copyright (c) 2008, sford
AlexVC97 0:3bdfc1556537 3 */
AlexVC97 0:3bdfc1556537 4
AlexVC97 0:3bdfc1556537 5
AlexVC97 0:3bdfc1556537 6 #ifndef MBED_MEMFILESYSTEM_H
AlexVC97 0:3bdfc1556537 7 #define MBED_MEMFILESYSTEM_H
AlexVC97 0:3bdfc1556537 8
AlexVC97 0:3bdfc1556537 9 #include "FATFileSystem.h"
AlexVC97 0:3bdfc1556537 10
AlexVC97 0:3bdfc1556537 11 namespace mbed
AlexVC97 0:3bdfc1556537 12 {
AlexVC97 0:3bdfc1556537 13
AlexVC97 0:3bdfc1556537 14 class MemFileSystem : public FATFileSystem
AlexVC97 0:3bdfc1556537 15 {
AlexVC97 0:3bdfc1556537 16 public:
AlexVC97 0:3bdfc1556537 17
AlexVC97 0:3bdfc1556537 18 // 2000 sectors, each 512 bytes (malloced as required)
AlexVC97 0:3bdfc1556537 19 char *sectors[2000];
AlexVC97 0:3bdfc1556537 20
AlexVC97 0:3bdfc1556537 21 MemFileSystem(const char* name) : FATFileSystem(name) {
AlexVC97 0:3bdfc1556537 22 memset(sectors, 0, sizeof(sectors));
AlexVC97 0:3bdfc1556537 23 }
AlexVC97 0:3bdfc1556537 24
AlexVC97 0:3bdfc1556537 25 virtual ~MemFileSystem() {
AlexVC97 0:3bdfc1556537 26 for(int i = 0; i < 2000; i++) {
AlexVC97 0:3bdfc1556537 27 if(sectors[i]) {
AlexVC97 0:3bdfc1556537 28 free(sectors[i]);
AlexVC97 0:3bdfc1556537 29 }
AlexVC97 0:3bdfc1556537 30 }
AlexVC97 0:3bdfc1556537 31 }
AlexVC97 0:3bdfc1556537 32
AlexVC97 0:3bdfc1556537 33 // read a sector in to the buffer, return 0 if ok
AlexVC97 0:3bdfc1556537 34 virtual int disk_read(char *buffer, int sector) {
AlexVC97 0:3bdfc1556537 35 if(sectors[sector] == 0) {
AlexVC97 0:3bdfc1556537 36 // nothing allocated means sector is empty
AlexVC97 0:3bdfc1556537 37 memset(buffer, 0, 512);
AlexVC97 0:3bdfc1556537 38 } else {
AlexVC97 0:3bdfc1556537 39 memcpy(buffer, sectors[sector], 512);
AlexVC97 0:3bdfc1556537 40 }
AlexVC97 0:3bdfc1556537 41 return 0;
AlexVC97 0:3bdfc1556537 42 }
AlexVC97 0:3bdfc1556537 43
AlexVC97 0:3bdfc1556537 44 // write a sector from the buffer, return 0 if ok
AlexVC97 0:3bdfc1556537 45 virtual int disk_write(const char *buffer, int sector) {
AlexVC97 0:3bdfc1556537 46 // if buffer is zero deallocate sector
AlexVC97 0:3bdfc1556537 47 char zero[512];
AlexVC97 0:3bdfc1556537 48 memset(zero, 0, 512);
AlexVC97 0:3bdfc1556537 49 if(memcmp(zero, buffer, 512)==0) {
AlexVC97 0:3bdfc1556537 50 if(sectors[sector] != 0) {
AlexVC97 0:3bdfc1556537 51 free(sectors[sector]);
AlexVC97 0:3bdfc1556537 52 sectors[sector] = 0;
AlexVC97 0:3bdfc1556537 53 }
AlexVC97 0:3bdfc1556537 54 return 0;
AlexVC97 0:3bdfc1556537 55 }
AlexVC97 0:3bdfc1556537 56 // else allocate a sector if needed, and write
AlexVC97 0:3bdfc1556537 57 if(sectors[sector] == 0) {
AlexVC97 0:3bdfc1556537 58 char *sec = (char*)malloc(512);
AlexVC97 0:3bdfc1556537 59 if(sec==0) {
AlexVC97 0:3bdfc1556537 60 return 1; // out of memory
AlexVC97 0:3bdfc1556537 61 }
AlexVC97 0:3bdfc1556537 62 sectors[sector] = sec;
AlexVC97 0:3bdfc1556537 63 }
AlexVC97 0:3bdfc1556537 64 memcpy(sectors[sector], buffer, 512);
AlexVC97 0:3bdfc1556537 65 return 0;
AlexVC97 0:3bdfc1556537 66 }
AlexVC97 0:3bdfc1556537 67
AlexVC97 0:3bdfc1556537 68 // return the number of sectors
AlexVC97 0:3bdfc1556537 69 virtual int disk_sectors() {
AlexVC97 0:3bdfc1556537 70 return sizeof(sectors)/sizeof(sectors[0]);
AlexVC97 0:3bdfc1556537 71 }
AlexVC97 0:3bdfc1556537 72
AlexVC97 0:3bdfc1556537 73 };
AlexVC97 0:3bdfc1556537 74
AlexVC97 0:3bdfc1556537 75 }
AlexVC97 0:3bdfc1556537 76
AlexVC97 0:3bdfc1556537 77 #endif