This example demonstrates the reading of the SD card in the Nucleo.

Dependencies:   mbed SDFileSystem

Committer:
beaglescout007
Date:
Mon Mar 21 08:58:22 2016 +0000
Revision:
0:b0a3ecd53c7d
Child:
1:9d8797b91dda
Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
beaglescout007 0:b0a3ecd53c7d 1 #include "mbed.h"
beaglescout007 0:b0a3ecd53c7d 2 #include "SDFileSystem.h"
beaglescout007 0:b0a3ecd53c7d 3
beaglescout007 0:b0a3ecd53c7d 4 DigitalIn btn(USER_BUTTON);
beaglescout007 0:b0a3ecd53c7d 5
beaglescout007 0:b0a3ecd53c7d 6 // trim '\n'
beaglescout007 0:b0a3ecd53c7d 7 void ntrim(char *str)
beaglescout007 0:b0a3ecd53c7d 8 {
beaglescout007 0:b0a3ecd53c7d 9 int i;
beaglescout007 0:b0a3ecd53c7d 10 for (i = 0; str[i] != 0; ++i);
beaglescout007 0:b0a3ecd53c7d 11
beaglescout007 0:b0a3ecd53c7d 12 if (i > 0 && str[i - 1] == '\n')
beaglescout007 0:b0a3ecd53c7d 13 str[i - 1] = 0;
beaglescout007 0:b0a3ecd53c7d 14 }
beaglescout007 0:b0a3ecd53c7d 15
beaglescout007 0:b0a3ecd53c7d 16 int main()
beaglescout007 0:b0a3ecd53c7d 17 {
beaglescout007 0:b0a3ecd53c7d 18 // SD filesystem
beaglescout007 0:b0a3ecd53c7d 19 SDFileSystem *sd = new SDFileSystem(PB_15, PB_14, PB_13, PA_9, "sd", NC, SDFileSystem::SWITCH_NONE, 20000000); // mosi, miso, sclk, name, card detect, sw type, freq
beaglescout007 0:b0a3ecd53c7d 20
beaglescout007 0:b0a3ecd53c7d 21 while (1)
beaglescout007 0:b0a3ecd53c7d 22 {
beaglescout007 0:b0a3ecd53c7d 23 if (btn) continue;
beaglescout007 0:b0a3ecd53c7d 24
beaglescout007 0:b0a3ecd53c7d 25 // file open
beaglescout007 0:b0a3ecd53c7d 26 FILE *fp = fopen("/sd/test.txt", "r");
beaglescout007 0:b0a3ecd53c7d 27 if (fp == NULL)
beaglescout007 0:b0a3ecd53c7d 28 {
beaglescout007 0:b0a3ecd53c7d 29 printf("open error!!\r\n");
beaglescout007 0:b0a3ecd53c7d 30 while(1);
beaglescout007 0:b0a3ecd53c7d 31 }
beaglescout007 0:b0a3ecd53c7d 32
beaglescout007 0:b0a3ecd53c7d 33 // read text file
beaglescout007 0:b0a3ecd53c7d 34 char buf[1024];
beaglescout007 0:b0a3ecd53c7d 35 while (fgets(buf, sizeof(buf), fp) != NULL)
beaglescout007 0:b0a3ecd53c7d 36 {
beaglescout007 0:b0a3ecd53c7d 37 ntrim(buf);
beaglescout007 0:b0a3ecd53c7d 38 printf("%s\r\n", buf);
beaglescout007 0:b0a3ecd53c7d 39 }
beaglescout007 0:b0a3ecd53c7d 40
beaglescout007 0:b0a3ecd53c7d 41 // file close
beaglescout007 0:b0a3ecd53c7d 42 fclose(fp);
beaglescout007 0:b0a3ecd53c7d 43
beaglescout007 0:b0a3ecd53c7d 44 wait(1);
beaglescout007 0:b0a3ecd53c7d 45 }
beaglescout007 0:b0a3ecd53c7d 46 }