1 /*
2 * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2024-12-30 CDT first version
9 */
10
11 /*
12 * 程序清单: FAL使用例程
13 * 例程导出了 fal_sample 命令到控制终端
14 * 命令调用格式:fal_sample
15 * 1)配置RTT工程
16 * menuconfig:
17 * RT-Thread Components ---> FAL: flash abstraction layer
18 * ---> Device Drivers ---> Using SPI Bus/Device device drivers ---> Using Serial Flash Universal Driver
19 * Hardware Drivers Config ---> Onboard Peripheral Drivers ----> Enable on-chip FLASH
20 */
21 #include <rtthread.h>
22 #include <rtdevice.h>
23
24 #if defined(RT_USING_FAL) && defined(BSP_USING_ON_CHIP_FLASH)
25
26 #include "board.h"
27 #include <fal.h>
28
29 #define FAL_PART_NAME "app"
30 #define TEST_BUF_SIZE 1024UL
31 #define TEST_RW_CNT 32UL
32 #define TEST_RW_START_ADDR HC32_FLASH_END_ADDRESS - (TEST_BUF_SIZE * TEST_RW_CNT)
33
34
35 static uint8_t write_buffer[TEST_BUF_SIZE] = {0};
36 static uint8_t read_buffer[TEST_BUF_SIZE] = {0};
37
38
fal_sample(int argc,char ** argv)39 static int fal_sample(int argc, char **argv)
40 {
41 const struct fal_partition *param;
42 int ret;
43 uint32_t Address;
44 uint8_t errFlag = 0;
45
46 fal_init(); //抽象层初始化
47 /* Set write buffer, clear read buffer */
48 for (uint32_t index = 0; index < TEST_BUF_SIZE; index++)
49 {
50 write_buffer[index] = index;
51 }
52 param = fal_partition_find(FAL_PART_NAME);
53 if (param == RT_NULL)
54 {
55 rt_kprintf("not find partition app!\r\n");
56 return -1;
57 }
58 for (int j = 0; j < TEST_RW_CNT; j++)
59 {
60 errFlag = 0;
61 Address = TEST_RW_START_ADDR + j * TEST_BUF_SIZE;
62 rt_kprintf("........test %d address 0x%08x........\r\n", j + 1, Address);
63 /* erase process */
64
65 if (j == 31)
66 {
67 rt_kprintf(".......");
68 }
69
70 ret = fal_partition_erase(param, Address, TEST_BUF_SIZE);
71 if (ret >= 0)
72 {
73 rt_kprintf("Erase succeeded!\r\n");
74 }
75 else
76 {
77 rt_kprintf("Erase failed!\r\n");
78 return ret;
79 }
80 /* write process */
81 ret = fal_partition_write(param, Address, write_buffer, TEST_BUF_SIZE);
82 if (ret >= 0)
83 {
84 rt_kprintf("Write succeeded!\r\n");
85 }
86 else
87 {
88 rt_kprintf("Write failed!\r\n");
89 return ret;
90 }
91 /* read process */
92 for (uint32_t index = 0; index < TEST_BUF_SIZE; index++)
93 {
94 read_buffer[index] = 0;
95 }
96 ret = fal_partition_read(param, Address, read_buffer, TEST_BUF_SIZE);
97 if (ret >= 0)
98 {
99 rt_kprintf("Read succeeded!\r\n");
100 }
101 else
102 {
103 rt_kprintf("Read failed!\r\n");
104 return ret;
105 }
106 /* compare process */
107 for (int i = 0; i < TEST_BUF_SIZE; i++)
108 {
109 #if defined(HC32F460)
110 if ((j == (TEST_RW_CNT - 1)) && (i >= (TEST_BUF_SIZE - 32)) ?
111 (read_buffer[i] != 0xFF) : (read_buffer[i] != write_buffer[i]))
112 #else
113 if (read_buffer[i] != write_buffer[i])
114 #endif
115 {
116 rt_kprintf("Data verification failed:\r\n");
117 rt_kprintf("NUM: %d Write: %x Read: %x \r\n", i, write_buffer[i], read_buffer[i]);
118 errFlag = 1;
119 ret = -1;
120 }
121 }
122 if (0 == errFlag)
123 {
124 rt_kprintf("Data verification OK!\r\n");
125 }
126 }
127
128 return ret;
129 }
130
131 /* 导出到 msh 命令列表中 */
132 MSH_CMD_EXPORT(fal_sample, fal sample);
133
134 #endif
135