SD card example for Wio cellular targets - Seeed Wio 3G and Wio LTE-M1/NB1(BG96)

Committer:
MACRUM
Date:
Tue Feb 12 08:04:50 2019 +0000
Revision:
0:5e920a8fb894
Child:
1:bda3045adb2f
Add WIO_BG96 support;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:5e920a8fb894 1 #include "mbed.h"
MACRUM 0:5e920a8fb894 2 #include "FATFileSystem.h"
MACRUM 0:5e920a8fb894 3 #include "SDBlockDevice.h"
MACRUM 0:5e920a8fb894 4 #include <stdio.h>
MACRUM 0:5e920a8fb894 5 #include <errno.h>
MACRUM 0:5e920a8fb894 6 /* mbed_retarget.h is included after errno.h so symbols are mapped to
MACRUM 0:5e920a8fb894 7 * consistent values for all toolchains */
MACRUM 0:5e920a8fb894 8 #include "platform/mbed_retarget.h"
MACRUM 0:5e920a8fb894 9
MACRUM 0:5e920a8fb894 10 #if defined(TARGET_WIO_3G) || defined(TARGET_WIO_BG96)
MACRUM 0:5e920a8fb894 11 DigitalOut SD_POWER(PA_15, 1);
MACRUM 0:5e920a8fb894 12 #endif
MACRUM 0:5e920a8fb894 13
MACRUM 0:5e920a8fb894 14 SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI,
MACRUM 0:5e920a8fb894 15 MBED_CONF_SD_SPI_MISO,
MACRUM 0:5e920a8fb894 16 MBED_CONF_SD_SPI_CLK,
MACRUM 0:5e920a8fb894 17 MBED_CONF_SD_SPI_CS);
MACRUM 0:5e920a8fb894 18 FATFileSystem fs("sd", &sd);
MACRUM 0:5e920a8fb894 19
MACRUM 0:5e920a8fb894 20 void return_error(int ret_val){
MACRUM 0:5e920a8fb894 21 if (ret_val)
MACRUM 0:5e920a8fb894 22 printf("Failure. %d\n", ret_val);
MACRUM 0:5e920a8fb894 23 else
MACRUM 0:5e920a8fb894 24 printf("done.\n");
MACRUM 0:5e920a8fb894 25 }
MACRUM 0:5e920a8fb894 26
MACRUM 0:5e920a8fb894 27 void errno_error(void* ret_val){
MACRUM 0:5e920a8fb894 28 if (ret_val == NULL) {
MACRUM 0:5e920a8fb894 29 printf(" Failure. %d \n", errno);
MACRUM 0:5e920a8fb894 30 while (true) __WFI();
MACRUM 0:5e920a8fb894 31 } else
MACRUM 0:5e920a8fb894 32 printf(" done.\n");
MACRUM 0:5e920a8fb894 33 }
MACRUM 0:5e920a8fb894 34
MACRUM 0:5e920a8fb894 35 int main()
MACRUM 0:5e920a8fb894 36 {
MACRUM 0:5e920a8fb894 37 int error = 0;
MACRUM 0:5e920a8fb894 38 printf("Welcome to the filesystem example.\n");
MACRUM 0:5e920a8fb894 39
MACRUM 0:5e920a8fb894 40 printf("Opening a new file, numbers.txt.");
MACRUM 0:5e920a8fb894 41 FILE* fd = fopen("/sd/numbers.txt", "w+");
MACRUM 0:5e920a8fb894 42 errno_error(fd);
MACRUM 0:5e920a8fb894 43
MACRUM 0:5e920a8fb894 44 for (int i = 0; i < 20; i++){
MACRUM 0:5e920a8fb894 45 printf("Writing decimal numbers to a file (%d/20)\r", i);
MACRUM 0:5e920a8fb894 46 fprintf(fd, "%d\n", i);
MACRUM 0:5e920a8fb894 47 }
MACRUM 0:5e920a8fb894 48 printf("Writing decimal numbers to a file (20/20) done.\n");
MACRUM 0:5e920a8fb894 49
MACRUM 0:5e920a8fb894 50 printf("Closing file.");
MACRUM 0:5e920a8fb894 51 fclose(fd);
MACRUM 0:5e920a8fb894 52 printf(" done.\n");
MACRUM 0:5e920a8fb894 53
MACRUM 0:5e920a8fb894 54 printf("Re-opening file read-only.");
MACRUM 0:5e920a8fb894 55 fd = fopen("/sd/numbers.txt", "r");
MACRUM 0:5e920a8fb894 56 errno_error(fd);
MACRUM 0:5e920a8fb894 57
MACRUM 0:5e920a8fb894 58 printf("Dumping file to screen.\n");
MACRUM 0:5e920a8fb894 59 char buff[16] = {0};
MACRUM 0:5e920a8fb894 60 while (!feof(fd)){
MACRUM 0:5e920a8fb894 61 int size = fread(&buff[0], 1, 15, fd);
MACRUM 0:5e920a8fb894 62 fwrite(&buff[0], 1, size, stdout);
MACRUM 0:5e920a8fb894 63 }
MACRUM 0:5e920a8fb894 64 printf("EOF.\n");
MACRUM 0:5e920a8fb894 65
MACRUM 0:5e920a8fb894 66 printf("Closing file.");
MACRUM 0:5e920a8fb894 67 fclose(fd);
MACRUM 0:5e920a8fb894 68 printf(" done.\n");
MACRUM 0:5e920a8fb894 69
MACRUM 0:5e920a8fb894 70 printf("Opening root directory.");
MACRUM 0:5e920a8fb894 71 DIR* dir = opendir("/sd/");
MACRUM 0:5e920a8fb894 72 errno_error(fd);
MACRUM 0:5e920a8fb894 73
MACRUM 0:5e920a8fb894 74 struct dirent* de;
MACRUM 0:5e920a8fb894 75 printf("Printing all filenames:\n");
MACRUM 0:5e920a8fb894 76 while((de = readdir(dir)) != NULL){
MACRUM 0:5e920a8fb894 77 printf(" %s\n", &(de->d_name)[0]);
MACRUM 0:5e920a8fb894 78 }
MACRUM 0:5e920a8fb894 79
MACRUM 0:5e920a8fb894 80 printf("Closeing root directory. ");
MACRUM 0:5e920a8fb894 81 error = closedir(dir);
MACRUM 0:5e920a8fb894 82 return_error(error);
MACRUM 0:5e920a8fb894 83 printf("Filesystem Demo complete.\n");
MACRUM 0:5e920a8fb894 84
MACRUM 0:5e920a8fb894 85 while (true) {}
MACRUM 0:5e920a8fb894 86 }
MACRUM 0:5e920a8fb894 87