XFusion API
v1.3.0
载入中...
搜索中...
未找到
xf_main.c
浏览该文件的文档.
1
17
/* ==================== [Includes] ========================================== */
18
19
#include "xf_utils.h"
20
#include "xf_fal.h"
21
22
/* ==================== [Defines] =========================================== */
23
24
/* ==================== [Typedefs] ========================================== */
25
26
#define TAG "xf_fal_example"
27
28
/* ==================== [Static Prototypes] ================================= */
29
30
/* ==================== [Static Variables] ================================== */
31
32
/* ==================== [Macros] ============================================ */
33
34
/* ==================== [Global Functions] ================================== */
35
36
void
xf_main
(
void
)
37
{
38
uint8_t read_buf[16] = {0};
39
uint8_t write_buf[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
40
xf_err_t
xf_ret;
41
42
/*
43
注册 xf_fal
44
用户可以忽略
45
已经在对接层做了,这里无需操作,
46
对接部分可见 @ref ports/espressif/esp32/port_fal.c
47
*/
48
49
/* 初始化 FAL */
50
xf_ret =
xf_fal_init
();
51
if
(xf_ret !=
XF_OK
) {
52
XF_LOGI
(
TAG
,
"FAL init failed: %d"
, xf_ret);
53
return
;
54
}
55
56
/* 打印分区表 */
57
xf_fal_show_part_table
();
58
59
/* 获取分区 */
60
const
xf_fal_partition_t
*part =
xf_fal_partition_find
(
XF_FAL_DEFAULT_PARTITION_NAME
);
61
if
(!part) {
62
XF_LOGI
(
TAG
,
"Partition not found!"
);
63
return
;
64
}
65
66
/* 写入之前要擦除数据 */
67
xf_ret =
xf_fal_partition_erase
(part, 0, part->
len
);
68
if
(xf_ret !=
XF_OK
) {
69
XF_LOGI
(
TAG
,
"Partition erase failed: %d"
, xf_ret);
70
return
;
71
}
72
73
/* 读取数据并打印 */
74
xf_ret =
xf_fal_partition_read
(part, 0, read_buf,
sizeof
(read_buf));
75
if
(xf_ret ==
XF_OK
) {
76
XF_LOGI
(
TAG
,
"The partition was successfully. Data: "
);
77
for
(
size_t
i = 0; i <
sizeof
(read_buf); i++) {
78
xf_log_printf
(
"%02X "
, read_buf[i]);
79
}
80
xf_log_printf
(
"\n"
);
81
}
else
{
82
XF_LOGI
(
TAG
,
"Partition read after erase failed: %d"
, xf_ret);
83
}
84
85
/* 写入数据到分区 */
86
xf_ret =
xf_fal_partition_write
(part, 0, write_buf,
sizeof
(write_buf));
87
if
(xf_ret !=
XF_OK
) {
88
XF_LOGI
(
TAG
,
"Partition write failed: %d"
, xf_ret);
89
return
;
90
}
91
92
/* 读取数据并打印 */
93
xf_ret =
xf_fal_partition_read
(part, 0, read_buf,
sizeof
(read_buf));
94
if
(xf_ret ==
XF_OK
) {
95
XF_LOGI
(
TAG
,
"Partition read successful! Data: "
);
96
for
(
size_t
i = 0; i <
sizeof
(read_buf); i++) {
97
xf_log_printf
(
"%02X "
, read_buf[i]);
98
}
99
xf_log_printf
(
"\n"
);
100
}
else
{
101
XF_LOGI
(
TAG
,
"Partition read failed: %d"
, xf_ret);
102
}
103
104
/*
105
擦除数据
106
数据只能以扇区为单位擦除,此处先获取 flash 设备查询扇区大小
107
*/
108
const
xf_fal_flash_dev_t
*flash_dev =
xf_fal_flash_device_find_by_part
(part);
109
if
(NULL == flash_dev) {
110
XF_LOGI
(
TAG
,
"flash device not found."
);
111
return
;
112
}
113
114
xf_ret =
xf_fal_partition_erase
(part, 0, flash_dev->
sector_size
);
115
if
(xf_ret !=
XF_OK
) {
116
XF_LOGI
(
TAG
,
"Partition erase failed: %d"
, xf_ret);
117
return
;
118
}
119
120
/* 再次读取数据并打印 */
121
xf_ret =
xf_fal_partition_read
(part, 0, read_buf,
sizeof
(read_buf));
122
if
(xf_ret ==
XF_OK
) {
123
XF_LOGI
(
TAG
,
"After erase, data: "
);
124
for
(
size_t
i = 0; i <
sizeof
(read_buf); i++) {
125
xf_log_printf
(
"%02X "
, read_buf[i]);
126
}
127
xf_log_printf
(
"\n"
);
128
}
else
{
129
XF_LOGI
(
TAG
,
"Partition read after erase failed: %d"
, xf_ret);
130
}
131
XF_LOGI
(
TAG
,
"Done!"
);
132
}
133
134
/* ==================== [Static Functions] ================================== */
xf_main
void xf_main(void)
Definition
xf_main.c:28
xf_fal_partition_read
xf_err_t xf_fal_partition_read(const xf_fal_partition_t *part, size_t src_offset, void *dst, size_t size)
从指定分区读取数据。
Definition
xf_fal.c:404
xf_fal_partition_erase
xf_err_t xf_fal_partition_erase(const xf_fal_partition_t *part, size_t offset, size_t size)
擦除指定分区数据。
Definition
xf_fal.c:482
xf_fal_init
xf_err_t xf_fal_init(void)
初始化 FAL.
Definition
xf_fal.c:259
xf_fal_partition_write
xf_err_t xf_fal_partition_write(const xf_fal_partition_t *part, size_t dst_offset, const void *src, size_t size)
将数据写入指定分区。
Definition
xf_fal.c:443
xf_fal_show_part_table
void xf_fal_show_part_table(void)
打印分区表信息。
Definition
xf_fal.c:525
xf_fal_partition_find
const xf_fal_partition_t * xf_fal_partition_find(const char *name)
根据分区名称查找分区句柄。
Definition
xf_fal.c:370
xf_fal_flash_device_find_by_part
const xf_fal_flash_dev_t * xf_fal_flash_device_find_by_part(const xf_fal_partition_t *part)
通过给定分区查找 flash 设备。
Definition
xf_fal.c:342
xf_err_t
int32_t xf_err_t
整形错误类型。 错误码具体值见 xf_err_code_t.
Definition
xf_err.h:69
XF_OK
@ XF_OK
Definition
xf_err.h:43
TAG
#define TAG
Definition
xf_main.c:24
_xf_fal_flash_dev_t
flash 设备结构体。
Definition
xf_fal_types.h:124
_xf_fal_flash_dev_t::sector_size
size_t sector_size
扇区大小。扇区大小是最小擦除大小。单位: byte.
Definition
xf_fal_types.h:131
_xf_fal_partition_t
flash 分区结构体。
Definition
xf_fal_types.h:155
_xf_fal_partition_t::len
size_t len
flash 设备上的该分区长度。
Definition
xf_fal_types.h:173
XF_FAL_DEFAULT_PARTITION_NAME
#define XF_FAL_DEFAULT_PARTITION_NAME
Definition
xf_fal_config_internal.h:54
XF_LOGI
#define XF_LOGI(tag, format,...)
Definition
xf_log_uitls.h:44
xf_log_printf
#define xf_log_printf(format,...)
Definition
xf_utils_log_config.h:51
examples
storage
fal
fal_base
main
xf_main.c
生成于 2025年 一月 21日 星期二 17:25:09 , 为 XFusion API使用
1.9.8