summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/examples/spi_sfud_sample.c
blob: ed9d0423ac34d5b20224d9490460467560b36681 (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
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Email: [email protected]
 *
 * Change Logs:
 * Date        Author       Notes
 * 2022-11-20  liqiaozhong  first commit
 * 2022-03-08  liqiaozhong  add format function and mount table
 */
#include <rtthread.h>
#include <string.h>

#if defined (RT_USING_SFUD) && defined(RT_USING_DFS)

#include <dfs_fs.h>
#include <dfs_file.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include "spi_flash.h"
#include "spi_flash_sfud.h"

#include "fdebug.h"
#include "fparameters_comm.h"

#include "fspim.h"
/************************** Variable Definitions *****************************/
sfud_flash_t spim_flash = RT_NULL;
const struct dfs_mount_tbl mount_table[] =
{
    { "flash2", "/", "elm", 0, RT_NULL },
    {0},
};
/***************** Macros (Inline Fungoctions) Definitions *********************/
#define FSPIM_DEBUG_TAG "SPIM"
#define FSPIM_ERROR(format, ...)   FT_DEBUG_PRINT_E(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPIM_WARN(format, ...)   FT_DEBUG_PRINT_W(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPIM_INFO(format, ...)    FT_DEBUG_PRINT_I(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPIM_DEBUG(format, ...)   FT_DEBUG_PRINT_D(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)

/*******************************Api Functions*********************************/
static int spi_flash_sfud_init(void)
{
    if (RT_NULL == rt_sfud_flash_probe("flash2", "spi02"))
    {
        rt_kprintf("rt_sfud_flash_probe failed\n");
        return RT_ERROR;
    }
    spim_flash = rt_sfud_flash_find_by_dev_name("flash2");
    if (RT_NULL == spim_flash)
    {
        rt_kprintf("Flash init failed -> can't find flash2 device!\n");
        return RT_ERROR;
    }
    rt_kprintf("Spi flash device flash2 init\n");
    rt_kprintf("Flash device: flash2 info\nmf_id: 0x%x\ntype_id: 0x%x\ncapacity_id: 0x%x\nerase granularity: %lu\n",
    spim_flash->chip.mf_id,
    spim_flash->chip.type_id,
    spim_flash->chip.capacity_id,
    spim_flash->chip.erase_gran);

    return RT_EOK;
}
INIT_DEVICE_EXPORT(spi_flash_sfud_init);

/* format the flash with elm environment */
static int flash_format_operation(void)
{
    int result = RT_EOK;
    result = dfs_mkfs("elm", "flash2");
    return result;
}
INIT_ENV_EXPORT(flash_format_operation);

#endif /* RT_USING_SFUD || RT_USING_DFS */