summaryrefslogtreecommitdiff
path: root/bsp/yichip/yc3122-pos/applications/main.c
blob: 503513d1633c224d89330f71e7915c092542e88e (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
120
/*
 * Copyright (c) 2006-2021, YICHIP Technology Co.,Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-09-09     WSY          first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <rtdbg.h>

/* defined the LED pin: PA12 */
#define LED_PIN (51)
#define FS_PARTITION_NAME "filesystem"

#ifdef BSP_USING_INTER_FLASH
#include <dfs_elm.h>
#include <dfs_file.h>
#include <unistd.h>
#include <dfs_fs.h>
#include <fal.h>
static void elmfs_sample(void)
{
    fal_init();

    struct rt_device *flash_dev = fal_blk_device_create(FS_PARTITION_NAME);
    if (flash_dev == NULL)
    {
        LOG_E("Can't create a block device on '%s' partition.", FS_PARTITION_NAME);
    }
    else
    {
        LOG_I("Create a block device on the %s partition of flash successful...", FS_PARTITION_NAME);
    }
    if (dfs_mkfs("elm", flash_dev->parent.name) == 0)
    {
        LOG_I("dfs_mkfs ok!\n");
    }
    else
    {
        LOG_E("dfs_mkfs err!\n");
    }

    if (dfs_mount(flash_dev->parent.name, "/", "elm", 0, 0) == 0)
    {
        LOG_I("Filesystem initialized!");
    }
    else
    {
        LOG_E("Failed to initialize filesystem!");
        LOG_D("You should create a filesystem on the block device first!");
    }
    struct statfs elm_stat;
    if (statfs("/", &elm_stat) == 0)
    {
        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);
    }

    if (mkdir("/user", 0x777) == 0)
    {
        LOG_I("make a directory: '/user'.\n");
    }

    LOG_I("open file\n");
    int fd = open("/user/test.txt", O_WRONLY | O_CREAT);
    LOG_I("open file ok\n");
    char str[] = "elmfat mount";
    if (fd >= 0)
    {
        LOG_I("write file\n");
        if (write(fd, str, sizeof(str)) == sizeof(str))
            LOG_I("write data done.\n");
        close(fd);
    }
    int size;
    char buf[20];
    fd = open("/user/test.txt", O_RDONLY);
    if (fd >= 0)
    {
        LOG_I("read file\n");
        size = read(fd, buf, sizeof(buf));
        close(fd);
        if (size == sizeof(str))
        {
            LOG_I("Read data from file test.txt(size:%d):%s\n", size, buf);
        }
    }
    else
    {
        LOG_E("open err\n");
    }

    if (statfs("/", &elm_stat) == 0)
    {
        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);
    }
}
#endif
int main(void)
{
#ifdef BSP_USING_INTER_FLASH
    elmfs_sample();
#endif
    int count = 1;
    /* set LED4 pin mode to output */
    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);

    while (count++)
    {
        rt_pin_write(LED_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED_PIN, PIN_LOW);
        rt_thread_mdelay(500);
    }

    return RT_EOK;
}