2020-10-24 文件系统

测试代码


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

#ifdef RT_USING_DFS
    #include <dfs_fs.h>
#endif

#define DBG_SECTION_NAME  "MAIN"
#define DBG_COLOR
#define DBG_LEVEL DBG_INFO
#include <rtdbg.h>

int main(int argc, char *argv[])
{
    return 0;
}

/* Example: nand flash mount fatfs */
#ifdef RT_USING_DFS

#define NAND_FLASH_NAME  "nand0"
#define TEST_DATA_LEN (512*1024)

void thread1_entry(void *pra)
{
    int fd, size;
    rt_uint8_t *data_ptr;
    rt_uint8_t *recv_ptr;

    rt_uint32_t index = 0;
    rt_uint32_t j = 0;
    rt_uint32_t file_size = 0;
    rt_uint32_t datasize = 0;
    
    char *file_name;
    int i = 0;

    file_name = rt_malloc(220);
    for(i=0;i<2;i++)
    {
        sprintf(file_name,"/file%03d.txt",i);

        data_ptr = rt_malloc(TEST_DATA_LEN);
        if (data_ptr == RT_NULL)
        {
            rt_kprintf("data ptr out of memory!\n");
            return;
        }

        /* prepare write data */
        for (index = 0; index < TEST_DATA_LEN; index ++)
        {
            data_ptr[index] = '0' + j;
            j++;
            if (j > 9)
            {
                j = 0;
            }
        }

        /* open "/test.txt" file with (O_WRONLY | O_CREAT | O_APPEND) mode */
        fd = open(file_name, O_WRONLY | O_CREAT | O_APPEND);
        if (fd >= 0)
        {
            
            datasize = write(fd, data_ptr, TEST_DATA_LEN);
            if(datasize != TEST_DATA_LEN)
            {
                rt_kprintf("Write done %s.\n\n",file_name);
                rt_kprintf("Write over.\n");
                return;
            }
            datasize = write(fd, data_ptr, TEST_DATA_LEN);
            if(datasize != TEST_DATA_LEN)
            {
                rt_kprintf("Write done %s.\n\n",file_name);
                rt_kprintf("Write over.\n");
                return;
            }
            file_size = lseek(fd, 0, SEEK_END);
            rt_kprintf("**************************file size is %d.\n", file_size);
            close(fd);

            rt_kprintf("Write done %s.\n\n",file_name);
        }
        rt_free(data_ptr);

    }
}

int mnt_init(void)
{
    rt_thread_t tid1 = RT_NULL;

    /* mount fatfs */
    if (dfs_mount(NAND_FLASH_NAME, "/", "elm", 0, 0) == 0)
    {
        rt_kprintf("Mount FatFs file system to root, Done!\n");
    }
    else
    {
        rt_kprintf("Mount FatFs file system failed.\n");
        return 0;
    }

    /* Test: read and write file in thread1 */
    tid1 = rt_thread_create("test_thread1", thread1_entry, 0, 4096, 25, 20);
    if (tid1 != RT_NULL)
    {
        rt_thread_startup(tid1);
    }
    return 0;
}
INIT_APP_EXPORT(mnt_init);
#endif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nameless-Y

文章帮助到我了!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值