Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DISCO-F746NG_BLOCK_DEVICE_WITH_FAT_FILESYSTEM_ON_SDCARD SDRAM_DISCO_F746NG BSP_DISCO_F746NG BD_SD_DISCO_F746NG AUDIO_DISCO_F746NG
Diff: main.cpp
- Revision:
- 2:993735af824b
- Parent:
- 1:ffacc9d0b308
- Child:
- 3:7f991a3d4e71
diff -r ffacc9d0b308 -r 993735af824b main.cpp
--- a/main.cpp Sat Mar 24 19:13:39 2018 +0000
+++ b/main.cpp Sat Mar 24 19:27:59 2018 +0000
@@ -16,13 +16,32 @@
*/
#include "mbed.h"
+#include "FATFileSystem.h"
#include "BD_SD_DISCO_F746NG.h"
+#include <stdio.h>
+#include <errno.h>
DigitalOut led (LED1);
// Instantiate the Block Device for sd card on DISCO-F746NG
BD_SD_DISCO_F746NG bd;
-uint8_t block[512] = "Hello World!\n";
+FATFileSystem fs ("fs");
+
+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");
+}
int
main () {
@@ -30,40 +49,60 @@
pc.baud (115200);
printf ("Start\n");
- // Call the BD_SD_DISCO_F746NG instance initialisation method.
- printf ("sd card init...\n");
- if (0 != bd.init ()) {
- printf ("Init failed \n");
- return -1;
+ int error = 0;
+ printf ("Welcome to the filesystem example.\r\n"
+ "Formatting a FAT, RAM-backed filesystem. ");
+ error = 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", 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 ("sd size: %llu\n", bd.size ());
- printf ("sd read size: %llu\n", bd.get_read_size ());
- printf ("sd program size: %llu\n", bd.get_program_size ());
- printf ("sd erase size: %llu\n\n", bd.get_erase_size ());
+ 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 ("sd erase...\n");
- if (0 != bd.erase (0, bd.get_erase_size ())) {
- printf ("Error Erasing block \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]);
}
- // Write some the data block to the device
- printf ("sd write: %s\n", block);
- if (0 == bd.program (block, 0, 512)) {
- // read the data block from the device
- printf ("sd read: ");
- if (0 == bd.read (block, 0, 512)) {
- // print the contents of the block
- printf ("%s", block);
- }
- }
-
- // Call the BD_SD_DISCO_F746NG instance de-initialisation method.
- printf ("sd card deinit...\n");
- if (0 != bd.deinit ()) {
- printf ("Deinit failed \n");
- return -1;
- }
+ printf ("Closeing root directory. ");
+ error = closedir (dir);
+ return_error (error);
+ printf ("Filesystem Demo complete.\r\n");
// Blink led with 2 Hz
while (true) {