1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <aos/kernel.h>
8 #include <aos/cli.h>
9 #include <aos/mtd.h>
10 
mtd_unit_test(int argc,char * argv[])11 static void mtd_unit_test(int argc, char *argv[])
12 {
13     ssize_t count;
14     aos_mtd_t *mtd;
15     aos_status_t ret;
16     char buf[64] = "mtd test.";
17     int buf_test_len = strlen(buf);
18 
19     mtd = aos_mtd_open(MTD_PART_NAME_KERNEL2);
20     if (mtd == NULL) {
21         printf("%s:%d: aos_mtd_open failed.\n", __func__, __LINE__);
22         return;
23     }
24 
25     ret = aos_mtd_erase(mtd, 0, strlen("mtd test."));
26     if (ret < 0) {
27         printf("%s:%d: aos_mtd_erase failed ret: %d\n", __func__, __LINE__, ret);
28         aos_mtd_close(mtd);
29         return;
30     }
31 
32     count = aos_mtd_write(mtd, 0, buf, strlen(buf));
33     if (count < 0) {
34         printf("%s:%d: aos_mtd_write failed ret: %d\n", __func__, __LINE__, count);
35         aos_mtd_close(mtd);
36         return;
37     }
38     printf("%s:%d:buf\"%s\" was successfully written.\n", __func__, __LINE__, buf);
39 
40     memset(buf, 0, sizeof(buf));
41     count = aos_mtd_read(mtd, 0, buf, buf_test_len);
42     if (count < 0) {
43         printf("%s:%d: aos_mtd_read failed ret: %d\n", __func__, __LINE__, count);
44         aos_mtd_close(mtd);
45         return;
46     }
47     printf("%s:%d:buf\"%s\" was successfully read.\n", __func__, __LINE__, buf);
48 
49     if (strcmp(buf, "mtd test.") != 0) {
50         printf("%s:%d:buf:%s aos_mtd_test failed!\n", __func__, __LINE__, buf);
51     } else {
52         printf("%s:%d: aos_mtd_test success!\n", __func__, __LINE__);
53     }
54 
55     aos_mtd_close(mtd);
56     return;
57 }
58 
59 #ifdef AOS_COMP_CLI
60 /* reg args: fun, cmd, description*/
61 ALIOS_CLI_CMD_REGISTER(mtd_unit_test, mtd_example, aos mtd example)
62 #endif