1 /*
2 * Copyright (c) 2006-2021, YICHIP Technology Co.,Ltd.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-09-09 WSY first version
9 */
10
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 #include <rtdbg.h>
14
15 /* defined the LED pin: PA12 */
16 #define LED_PIN (51)
17 #define FS_PARTITION_NAME "filesystem"
18
19 #ifdef BSP_USING_INTER_FLASH
20 #include <dfs_elm.h>
21 #include <dfs_file.h>
22 #include <unistd.h>
23 #include <dfs_fs.h>
24 #include <fal.h>
elmfs_sample(void)25 static void elmfs_sample(void)
26 {
27 fal_init();
28
29 struct rt_device *flash_dev = fal_blk_device_create(FS_PARTITION_NAME);
30 if (flash_dev == NULL)
31 {
32 LOG_E("Can't create a block device on '%s' partition.", FS_PARTITION_NAME);
33 }
34 else
35 {
36 LOG_I("Create a block device on the %s partition of flash successful...", FS_PARTITION_NAME);
37 }
38 if (dfs_mkfs("elm", flash_dev->parent.name) == 0)
39 {
40 LOG_I("dfs_mkfs ok!\n");
41 }
42 else
43 {
44 LOG_E("dfs_mkfs err!\n");
45 }
46
47 if (dfs_mount(flash_dev->parent.name, "/", "elm", 0, 0) == 0)
48 {
49 LOG_I("Filesystem initialized!");
50 }
51 else
52 {
53 LOG_E("Failed to initialize filesystem!");
54 LOG_D("You should create a filesystem on the block device first!");
55 }
56 struct statfs elm_stat;
57 if (statfs("/", &elm_stat) == 0)
58 {
59 LOG_I("elmfat filesystem block size:0x%x,total blocks:0x%x,free blocks:0x%x\n", elm_stat.f_bsize, elm_stat.f_blocks, elm_stat.f_bfree);
60 }
61
62 if (mkdir("/user", 0x777) == 0)
63 {
64 LOG_I("make a directory: '/user'.\n");
65 }
66
67 LOG_I("open file\n");
68 int fd = open("/user/test.txt", O_WRONLY | O_CREAT);
69 LOG_I("open file ok\n");
70 char str[] = "elmfat mount";
71 if (fd >= 0)
72 {
73 LOG_I("write file\n");
74 if (write(fd, str, sizeof(str)) == sizeof(str))
75 LOG_I("write data done.\n");
76 close(fd);
77 }
78 int size;
79 char buf[20];
80 fd = open("/user/test.txt", O_RDONLY);
81 if (fd >= 0)
82 {
83 LOG_I("read file\n");
84 size = read(fd, buf, sizeof(buf));
85 close(fd);
86 if (size == sizeof(str))
87 {
88 LOG_I("Read data from file test.txt(size:%d):%s\n", size, buf);
89 }
90 }
91 else
92 {
93 LOG_E("open err\n");
94 }
95
96 if (statfs("/", &elm_stat) == 0)
97 {
98 LOG_I("elmfat filesystem block size:0x%x,total blocks:0x%x,free blocks:0x%x\n", elm_stat.f_bsize, elm_stat.f_blocks, elm_stat.f_bfree);
99 }
100 }
101 #endif
main(void)102 int main(void)
103 {
104 #ifdef BSP_USING_INTER_FLASH
105 elmfs_sample();
106 #endif
107 int count = 1;
108 /* set LED4 pin mode to output */
109 rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
110
111 while (count++)
112 {
113 rt_pin_write(LED_PIN, PIN_HIGH);
114 rt_thread_mdelay(500);
115 rt_pin_write(LED_PIN, PIN_LOW);
116 rt_thread_mdelay(500);
117 }
118
119 return RT_EOK;
120 }
121