Example of using block device class for creating a FAT file system via block device with is used to access a SD/MMC card via SD/MMC interface on DISCO_F746NG

Dependencies:   BD_SD_DISCO_F746NG

Dependents:   ItWorks

Fork of DISCO-F746NG_BLOCK_DEVICE_SDCARD by Roy Krikke

Example of using block device class for creating a FAT file system via block device to access a SD/MMC card via SD/MMC interface on DISCO_F746NG development board. This block device API provides an interface for access to block-based storage. You can use a block device to back a full file system or write to it directly. More about Block Devices

http://www.st.com/content/ccc/fragment/product_related/rpn_information/board_photo/group0/ea/c4/6d/73/c3/f5/46/e2/stm32f746g-disco/files/stm32f746g-disco.jpg/_jcr_content/translations/en.stm32f746g-disco.jpg

Files at this revision

API Documentation at this revision

Comitter:
roykrikke
Date:
Wed Apr 04 19:49:22 2018 +0000
Parent:
2:993735af824b
Commit message:
Code Style & Formatting

Changed in this revision

BD_SD_DISCO_F746NG.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/BD_SD_DISCO_F746NG.lib	Sat Mar 24 19:27:59 2018 +0000
+++ b/BD_SD_DISCO_F746NG.lib	Wed Apr 04 19:49:22 2018 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/roykrikke/code/BD_SD_DISCO_F746NG/#131c3e1d831e
+https://os.mbed.com/users/roykrikke/code/BD_SD_DISCO_F746NG/#d1d99cfe13df
--- a/main.cpp	Sat Mar 24 19:27:59 2018 +0000
+++ b/main.cpp	Wed Apr 04 19:49:22 2018 +0000
@@ -1,12 +1,12 @@
 /* Example file of using SD/MMC Block device Library for MBED-OS
  * Copyright 2017 Roy Krikke
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *     http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,96 +17,99 @@
 
 #include "mbed.h"
 #include "FATFileSystem.h"
-#include "BD_SD_DISCO_F746NG.h"
+#include "SDBlockDeviceDISCOF746NG.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;
+SDBlockDeviceDISCOF746NG bd;
 FATFileSystem fs ("fs");
 
 void
-return_error (int ret_val) {
-  if (ret_val)
-    printf ("Failure. %d\r\n", ret_val);
-  else
-    printf ("done.\r\n");
+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");
+errno_error (void* ret_val)
+{
+    if(ret_val == NULL)
+        printf (" Failure. %d \r\n", errno);
+    else
+        printf (" done.\r\n");
 }
 
 int
-main () {
-  Serial pc (SERIAL_TX, SERIAL_RX);
-  pc.baud (115200);
-  printf ("Start\n");
+main ()
+{
+    Serial pc (SERIAL_TX, SERIAL_RX);
+    pc.baud(115200);
+    printf("Start\n");
 
-  int error = 0;
-  printf ("Welcome to the filesystem example.\r\n"
-          "Formatting a FAT, RAM-backed filesystem. ");
-  error = FATFileSystem::format (&bd);
-  return_error (error);
+    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("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);
+    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");
+    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("Closing file.");
+    fclose(fd);
+    printf(" done.\r\n");
 
-  printf ("Re-opening file read-only.");
-  fd = fopen ("/fs/numbers.txt", "r");
-  errno_error (fd);
+    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("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("Closing file.");
+    fclose(fd);
+    printf(" done.\r\n");
 
-  printf ("Opening root directory.");
-  DIR* dir = opendir ("/fs/");
-  errno_error (fd);
+    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]);
-  }
+    struct dirent* de;
+    printf("Printing all filenames:\r\n");
+    while((de = readdir (dir)) != NULL) {
+        printf("  %s\r\n", &(de->d_name)[0]);
+    }
 
-  printf ("Closeing root directory. ");
-  error = closedir (dir);
-  return_error (error);
-  printf ("Filesystem Demo complete.\r\n");
+    printf("Closeing root directory. ");
+    error = closedir(dir);
+    return_error(error);
+    printf("Filesystem Demo complete.\r\n");
 
-  // Blink led with 2 Hz   
-  while (true) {
-    led = !led;
-    wait (0.5);
-  }
+    // Blink led with 2 Hz
+    while(true) {
+        led = !led;
+        wait (0.5);
+    }
 }