summaryrefslogtreecommitdiff
path: root/tool/mbed/mbed-sdk/libraries/tests/mbed/dir_sd/main.cpp
blob: f28c45fea347f0583a70e7133386c68738aa33ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "mbed.h"
#include "SDFileSystem.h"

void led_blink(PinName led)
{
    DigitalOut myled(led);

    while (1) {
        myled = !myled;
        wait(1.0);
    }
}

void notify_completion(bool success)
{
    if (success)
        printf("{success}\n");
    else
        printf("{failure}\n");

    printf("{end}\n");
    led_blink(success ? LED1 : LED4);
}

#define TEST_STRING   "Hello World!"

FILE *test_open(char *path, const char *mode)
{
    FILE *f;

    f = fopen(path, mode);
    if (f == NULL) {
        printf("Error opening file\n");
        notify_completion(false);
    }

    return f;
}

void test_write(FILE *f, const char *str)
{
    int n = fprintf(f, str);

    if (n != strlen(str)) {
        printf("Error writing file\n");
        notify_completion(false);
    }
}

void test_close(FILE *f)
{
    int rc = fclose(f);

    if (rc != 0) {
        printf("Error closing file\n");
        notify_completion(false);
    }
}

DigitalOut led2(LED2);

int main()
{
#if defined(TARGET_KL25Z)
    SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
#elif defined(TARGET_nRF51822)
//SDFileSystem sd(p20, p22, p25, p24, "sd");
    SDFileSystem sd(p12, p13, p15, p14, "sd");
#elif defined(TARGET_NUCLEO_F030R8) || \
      defined(TARGET_NUCLEO_F070RB) || \
      defined(TARGET_NUCLEO_F072RB) || \
      defined(TARGET_NUCLEO_F091RC) || \
      defined(TARGET_NUCLEO_F103RB) || \
      defined(TARGET_NUCLEO_F302R8) || \
      defined(TARGET_NUCLEO_F303RE) || \
      defined(TARGET_NUCLEO_F334R8) || \
      defined(TARGET_NUCLEO_F401RE) || \
      defined(TARGET_NUCLEO_F411RE) || \
      defined(TARGET_NUCLEO_L053R8) || \
      defined(TARGET_NUCLEO_L073RZ) || \
      defined(TARGET_NUCLEO_L152RE)
    SDFileSystem sd(D11, D12, D13, D10, "sd");
#elif defined(TARGET_LPC11U37H_401)
    SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL, "sd");
#else
    SDFileSystem sd(p11, p12, p13, p14, "sd");
#endif
    led2 = 1;
    wait(0.5);
    FILE *f;
    char *str = TEST_STRING;
    char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(TEST_STRING));
    int str_len = strlen(TEST_STRING);

    printf("Write files\n");
    char filename[32];
    for (int i = 0; i < 10; i++) {
        sprintf(filename, "/sd/test_%d.txt", i);
        printf("Creating file: %s\n", filename);
        f = test_open(filename, "w");
        led2 = 0;
        test_write(f, str);
        test_close(f);
    }

    printf("List files:\n");
    DIR *d = opendir("/sd");
    if (d == NULL) {
        printf("Error opening directory\n");
        notify_completion(false);
    }

    struct dirent *p;
    while ((p = readdir(d)) != NULL)
        printf("%s\n", p->d_name);
    closedir(d);

    notify_completion(true);
}