1 #if AOS_COMP_CLI
2 #include "aos/cli.h"
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <fcntl.h>
7 #include <aos/errno.h>
8 #include <aos/kernel.h>
9 #include <aos/vfs.h>
10 #include "aos/init.h"
11 #include "board.h"
12 #include <k_api.h>
13
14
fatfs_comp_example(int argc,char ** argv)15 static void fatfs_comp_example(int argc, char **argv)
16 {
17 char buff[512] = {0};
18 int ret=0;
19 printf("fatfs component example start !\r\n");
20
21 int fd1 = aos_open("/sdcard/test5.txt", O_CREAT | O_RDWR | O_APPEND);
22 if(fd1 > 0){
23 printf("aos_open /sdcard/test5.txt fd1=%d\n", fd1);
24 }else{
25 printf("fatfs comp test fail! aos_open ret:%d\r\n", fd1);
26 return;
27 }
28
29 ret = aos_write(fd1, "hello world1\n", 13);
30 if(ret > 0){
31 printf("aos_write /sdcard/test5.txt num=%d\n", ret);
32 }else{
33 printf("fatfs comp test fail! aos_write ret:%d\r\n", fd1);
34 return;
35 }
36
37 ret = aos_sync(fd1);
38 if(ret){
39 printf("fatfs comp test fail! aos_sync ret:%d\r\n", ret);
40 return;
41 }
42
43 int fd2 = aos_open("/sdcard/test5.txt", O_RDWR);
44 if(fd2 > 0){
45 printf("aos_open /sdcard/test5.txt fd2=%d\n", fd2);
46 }else{
47 printf("fatfs comp test fail! aos_open ret:%d\r\n", fd2);
48 return;
49 }
50 ret = aos_lseek(fd2, 12, 0);
51 if(ret>0){
52 printf("aos_lseek /sdcard/test5.txt position=%d\n", ret);
53 }else{
54 printf("fatfs comp test fail! aos_lseek ret:%d\r\n", ret);
55 return;
56 }
57
58 ret = aos_read(fd2, buff, sizeof(buff) - 1);
59 if (ret > 0) {
60 printf("aos_read: %s\n", buff);
61 }else{
62 printf("fatfs comp test fail! aos_read ret:%d\r\n", ret);
63 return;
64 }
65
66 aos_close(fd1);
67 aos_close(fd2);
68
69 aos_dir_t *dir = aos_opendir("/sdcard");
70
71 aos_dirent_t *dp = NULL;
72 do {
73 dp = aos_readdir(dir);
74 if (dp)
75 printf("readdir: %s\n", dp->d_name);
76 } while (dp != NULL);
77
78 aos_closedir(dir);
79
80 printf("fatfs component example end !\r\n");
81 }
82
83 #if AOS_COMP_CLI
84 /* reg args: fun, cmd, description*/
85 ALIOS_CLI_CMD_REGISTER(fatfs_comp_example, fatfs_example, fatfs component base example)
86 #endif
87