Skip to content

Instantly share code, notes, and snippets.

@geky
Created June 13, 2017 21:31
Show Gist options
  • Save geky/55d96b7c857fbe2932784ac4e9de4e29 to your computer and use it in GitHub Desktop.
Save geky/55d96b7c857fbe2932784ac4e9de4e29 to your computer and use it in GitHub Desktop.
#include "mbed.h"
#include "MBRBlockDevice.h"
#include "SDBlockDevice.h"
#include "FATFileSystem.h"
#include <errno.h>
SDBlockDevice sd(D11, D12, D13, D10);
MBRBlockDevice part1(&sd, 1);
MBRBlockDevice part2(&sd, 2);
FATFileSystem fs1("fs1");
FATFileSystem fs2("fs2");
int destroy() {
static const uint8_t zeros[512] = {0};
int err = sd.init();
if (err) {
return err;
}
err = sd.erase(0, sd.get_erase_size());
if (err) {
return err;
}
err = sd.program(zeros, 0, sizeof(zeros));
if (err) {
return err;
}
return 0;
}
int mountboth() {
int err = part1.init();
if (err) {
return err;
}
err = fs1.mount(&part1);
if (err) {
part1.deinit();
return err;
}
err = part2.init();
if (err) {
fs1.unmount();
part1.deinit();
return err;
}
err = fs2.mount(&part2);
if (err) {
part2.deinit();
fs1.unmount();
part1.deinit();
return err;
}
return 0;
}
int formatboth() {
int err = MBRBlockDevice::partition(&sd, 1, 0x83, 0, 512*1024);
if (err) {
return err;
}
err = MBRBlockDevice::partition(&sd, 2, 0x83, 512*1024);
if (err) {
return err;
}
err = FATFileSystem::format(&part1);
if (err) {
return err;
}
err = FATFileSystem::format(&part2);
if (err) {
return err;
}
return 0;
}
int forcemountboth() {
int err = mountboth();
if (err) {
err = formatboth();
if (err) {
return err;
}
return mountboth();
}
return 0;
}
int unmountboth() {
int err = fs1.unmount();
if (err) {
return err;
}
err = fs2.unmount();
if (err) {
return err;
}
err = part1.deinit();
if (err) {
return err;
}
err = part2.deinit();
if (err) {
return err;
}
return 0;
}
int main() {
printf("go\n");
int err = destroy();
printf("destroy %d\n", err);
err = forcemountboth();
printf("forcemountboth %d\n", err);
FILE *f = fopen("/fs1/hi", "w");
printf("fopen fs1 %p %d\n", f, errno);
err = fwrite("hello", 1, 5, f);
printf("fwrite fs1 %d\n", err);
err = fclose(f);
printf("fclose fs1 %d\n", err);
f = fopen("/fs2/hi", "w");
printf("fopen fs2 %p %d\n", f, errno);
err = fwrite("hello", 1, 5, f);
printf("fwrite fs2 %d\n", err);
err = fclose(f);
printf("fclose fs2 %d\n", err);
err = unmountboth();
printf("unmountboth %d\n", err);
err = forcemountboth();
printf("forcemountboth %d\n", err);
f = fopen("/fs1/hi", "w");
printf("fopen fs1 %p %d\n", f, errno);
err = fwrite("hello", 1, 5, f);
printf("fwrite fs1 %d\n", err);
err = fclose(f);
printf("fclose fs1 %d\n", err);
f = fopen("/fs2/hi", "w");
printf("fopen fs2 %p %d\n", f, errno);
err = fwrite("hello", 1, 5, f);
printf("fwrite fs2 %d\n", err);
err = fclose(f);
printf("fclose fs2 %d\n", err);
err = unmountboth();
printf("unmountboth %d\n", err);
printf("done\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment