Blame view

RIOT/tests/pkg_fatfs/main.c 9.86 KB
a752c7ab   elopes   add first test an...
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  /*
   * Copyright (C) 2016 Michel Rottleuthner <michel.rottleuthner@haw-hamburg.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     tests
   * @{
   *
   * @file
   * @brief       Test application for the fatfs package.
   *
   * @author      Michel Rottleuthner <michel.rottleuthner@haw-hamburg.de>
   *
   * @}
   */
  
  #if FATFS_FFCONF_OPT_FS_NORTC == 0
  #include "periph/rtc.h"
  #endif
  #include "fatfs_diskio_common.h"
  #include "fatfs/ff.h"
  #include "shell.h"
  #include <string.h>
  #include <stdlib.h>
  #include <stdio.h>
  #include <inttypes.h>
  #include <stdbool.h>
  
  #define TEST_FATFS_READ_BUFFER_SIZE 64
  #define TEST_FATFS_MAX_LBL_SIZE 64
  #define TEST_FATFS_MAX_VOL_STR_LEN 8
  #define TEST_FATFS_FIXED_SECTOR_SIZE 512
  #define TEST_FATFS_FATENT_OFFSET 2
  #define TEST_FATFS_SHIFT_B_TO_GIB 30
  #define TEST_FATFS_SHIFT_B_TO_MIB 20
  #define TEST_FATFS_RTC_MON_OFFSET 1
  #define TEST_FATFS_RTC_YEAR 2000
  #define TEST_FATFS_RTC_MON  1
  #define TEST_FATFS_RTC_DAY  1
  #define TEST_FATFS_RTC_H    0
  #define TEST_FATFS_RTC_M    0
  #define TEST_FATFS_RTC_S    0
  #define IEC_KIBI 1024
  #define SI_KILO 1000
  
  FATFS fat_fs;        /* FatFs work area needed for each volume */
  
  static int _mount(int argc, char **argv)
  {
      int vol_idx;
  
      if (argc != 2) {
          printf("usage: %s <volume_idx>\n", argv[0]);
          return -1;
      }
  
      vol_idx = (int)atoi(argv[1]);
  
      char volume_str[TEST_FATFS_MAX_VOL_STR_LEN];
      sprintf(volume_str, "%d:/", vol_idx);
  
      puts("mounting file system image...");
  
      /* "0:/" points to the root dir of drive 0 */
      FRESULT mountresu = f_mount(&fat_fs, volume_str, 1);
      TCHAR label[TEST_FATFS_MAX_LBL_SIZE];
  
      if (mountresu == FR_OK) {
          puts("[OK]");
          if (f_getlabel("", label, NULL) == FR_OK) {
              printf("Volume name: %s\n", label);
          }
  
          FATFS *fs;
          DWORD fre_clust;
  
          /* Get volume information and free clusters of selected drive */
          if (f_getfree(volume_str, &fre_clust, &fs) != FR_OK) {
              puts("wasn't able to get volume size info!");
          }
          else {
  
              #if _MAX_SS == _MIN_SS
              uint16_t sector_size = TEST_FATFS_FIXED_SECTOR_SIZE;
              #else
              uint16_t sector_size = fs->ssize;
              #endif
  
              uint64_t total_bytes = (fs->n_fatent - TEST_FATFS_FATENT_OFFSET) * fs->csize;
              total_bytes *= sector_size;
              uint64_t free_bytes = fre_clust * fs->csize;
              free_bytes *= sector_size;
  
              uint32_t to_gib_i = total_bytes >> TEST_FATFS_SHIFT_B_TO_GIB;
              uint32_t to_gib_f = ((((total_bytes >> TEST_FATFS_SHIFT_B_TO_MIB) - to_gib_i * IEC_KIBI)
                                    * SI_KILO) / IEC_KIBI);
  
              uint32_t fr_gib_i = free_bytes >> TEST_FATFS_SHIFT_B_TO_GIB;
              uint32_t fr_gib_f = ((((free_bytes >> TEST_FATFS_SHIFT_B_TO_MIB) - fr_gib_i * IEC_KIBI)
                                    * SI_KILO) / IEC_KIBI);
  
              printf("%" PRIu32 ",%03" PRIu32 " GiB of %" PRIu32 ",%03" PRIu32
                     " GiB available\n", fr_gib_i, fr_gib_f, to_gib_i, to_gib_f);
          }
      }
      else {
          puts("[FAILED]");
          switch (mountresu) {
              case FR_NO_FILESYSTEM:
                  puts("no filesystem -> you need to format the card to FAT");
                  break;
              case FR_DISK_ERR:
                  puts("error in the low-level disk driver!");
                  break;
              default:
                  printf("error %d -> see ff.h of fatfs package for "
                         "further details\n", mountresu);
          }
          return -1;
      }
      return 0;
  }
  
  static int _touch(int argc, char **argv)
  {
      FIL fd;
  
      if (argc != 2) {
          printf("usage: %s <filename>\n", argv[0]);
          return -1;
      }
  
      FRESULT open_resu = f_open(&fd, argv[1], FA_WRITE | FA_CREATE_ALWAYS);
      if (open_resu == FR_OK) {
          FRESULT close_resu = f_close(&fd);
          if (close_resu == FR_OK) {
              puts("[OK]");
              return 0;
          }
  
          printf("[FAILED] (f_close error %d)\n", close_resu);
          return -2;
      }
  
      printf("[FAILED] (f_open error %d)\n", open_resu);
      return -3;
  }
  
  static int _read(int argc, char **argv)
  {
      FIL fd;
      int resu = 0;
  
      if ((argc < 2) || (argc > 3)) {
          printf("usage: %s <filename> [<len>]\n", argv[0]);
          return -1;
      }
  
      FRESULT open_resu = f_open(&fd, argv[1], FA_READ | FA_OPEN_EXISTING);
      if (open_resu == FR_OK) {
          UINT read_chunk;
          uint32_t len = ((argc == 3) ? (uint32_t)atoi(argv[2]) : f_size(&fd));
  
          char buffer[TEST_FATFS_READ_BUFFER_SIZE];
  
          for (uint32_t read = 0; read < len; read += read_chunk) {
              uint32_t to_read = len - read;
  
              if (to_read > sizeof(buffer)) {
                  to_read = sizeof(buffer);
              }
  
              FRESULT lseek_resu = f_lseek(&fd, read);
  
              if (lseek_resu != FR_OK) {
                  printf("[FAILED] f_lseek error %d\n", lseek_resu);
                  resu = -3;
                  break;
              }
  
              FRESULT read_resu = f_read(&fd, buffer, to_read, &read_chunk);
  
              if (read_resu != FR_OK) {
                  printf("[FAILED] (f_read error %d)\n", read_resu);
                  resu = -4;
                  break;
              }
  
              for (uint32_t i = 0; i < read_chunk; i++) {
                  printf("%c", buffer[i]);
              }
          }
          puts("");
  
          FRESULT close_resu = f_close(&fd);
  
          if (close_resu == FR_OK) {
              puts("[OK]");
              resu = 0;
          }
          else {
              printf("[FAILED] (f_close error %d)\n", open_resu);
              resu = -5;
          }
  
      }
      else {
          printf("[FAILED] (f_open error %d)\n", open_resu);
          resu = -2;
      }
  
      return resu;
  }
  
  static int _write(int argc, char **argv)
  {
      FIL fd;
      UINT bw;
  
      if (argc != 3) {
          printf("usage: %s <filename> <string>\n", argv[0]);
          return -1;
      }
  
      uint32_t len = strlen(argv[2]);
      FRESULT open_resu = f_open(&fd, argv[1], FA_WRITE | FA_OPEN_APPEND);
  
      if (open_resu == FR_OK) {
          printf("writing %" PRId32 " bytes to %s ...", len, argv[1]);
          FRESULT write_resu = f_write(&fd, argv[2], len, &bw);
  
          if ((write_resu != FR_OK) || (bw < len)) {
              printf("[FAILED] (f_write error %d)\n", write_resu);
              return -2;
          }
          else {
              FRESULT close_resu = f_close(&fd);
  
              if (close_resu == FR_OK) {
                  puts("[OK]");
                  return 0;
              }
  
              printf("[FAILED] (f_close error %d)\n", open_resu);
              return -3;
          }
      }
  
      printf("[FAILED] (f_open error %d)\n", open_resu);
      return -1;
  }
  
  static int _ls(int argc, char **argv)
  {
      char *path;
      FRESULT res;
      DIR dir;
      static FILINFO fno;
  
      if (argc == 2) {
          path = argv[1];
      }
      else {
          path = "/";
      }
  
      res = f_opendir(&dir, path);/* Open the directory */
  
      if (res == FR_OK) {
          while (true) {
              res = f_readdir(&dir, &fno);    /* Read a directory item */
  
              if ((res != FR_OK) || fno.fname[0] == 0) {
                  break;                      /* Break on error or end of dir */
              }
  
              if (fno.fattrib & AM_DIR) {     /* if this element is a directory */
                  printf("%s%s/\n", path, fno.fname);
              }
              else {
                  printf("%s/%s\n", path, fno.fname);
              }
          }
  
          f_closedir(&dir);
          return 0;
      }
  
      printf("[FAILED] error %d\n", res);
      return -1;
  }
  
  static int _mkfs(int argc, char **argv)
  {
      int vol_idx;
      BYTE opt;
  
      if (argc == 3) {
          vol_idx = (int)atoi(argv[1]);
  
          if (strcmp(argv[2], "fat") == 0) {
              opt = FM_FAT;
          }
          else if (strcmp(argv[2], "fat32") == 0) {
              opt = FM_FAT32;
          }
          else if (strcmp(argv[2], "exfat") == 0) {
              opt = FM_EXFAT;
          }
          else {
              opt = FM_ANY;
          }
      }
      else {
          printf("usage: %s <volume_idx> <fat|fat32|exfat|any>\n", argv[0]);
          return -1;
      }
  
      char volume_str[TEST_FATFS_MAX_VOL_STR_LEN];
      sprintf(volume_str, "%d:/", vol_idx);
      BYTE work[_MAX_SS];
  
      puts("formatting media...");
  
      /* au = 0: use default allocation unit size depending on volume size */
      FRESULT mkfs_resu = f_mkfs(volume_str, opt, 0,  work, sizeof(work));
  
      if (mkfs_resu == FR_OK) {
          puts("[OK]");
          return 0;
      }
  
      printf("[FAILED] error %d\n", mkfs_resu);
      return -1;
  }
  
  static const shell_command_t shell_commands[] = {
      { "mount", "mount file system", _mount },
      { "mkfs", "format volume", _mkfs },
      { "touch", "create file", _touch },
      { "read", "print file content to console", _read },
      { "write", "append string to file", _write },
      { "ls", "list files", _ls },
      { NULL, NULL, NULL }
  };
  
  int main(void)
  {
      #if FATFS_FFCONF_OPT_FS_NORTC == 0
      /* the rtc is used in diskio.c for timestamps of files */
      puts("Initializing the RTC driver");
      rtc_poweron();
      rtc_init();
  
      struct tm time;
      time.tm_year = TEST_FATFS_RTC_YEAR - RTC_YEAR_OFFSET;  /* years are counted from 1900 */
      time.tm_mon  = TEST_FATFS_RTC_MON;                     /* 0 = January, 11 = December */
      time.tm_mday = TEST_FATFS_RTC_DAY;
      time.tm_hour = TEST_FATFS_RTC_H;
      time.tm_min  = TEST_FATFS_RTC_M;
      time.tm_sec  = TEST_FATFS_RTC_S;
  
      printf("Setting RTC to %04d-%02d-%02d %02d:%02d:%02d\n",
             time.tm_year + RTC_YEAR_OFFSET,
             time.tm_mon + TEST_FATFS_RTC_MON_OFFSET,
             time.tm_mday,
             time.tm_hour,
             time.tm_min,
             time.tm_sec);
      rtc_set_time(&time);
      #endif
  
      char line_buf[SHELL_DEFAULT_BUFSIZE];
      shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
  
      return 0;
  }