1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <string.h>
8 
9 #include "aos/kernel.h"
10 #include "aos/hal/flash.h"
11 #include "flash_test.h"
12 
13 #define TEST_BUFFER_WORDS 512
14 
15 uint16_t flash_test_buffer[TEST_BUFFER_WORDS];
16 
hal_flash_test(void)17 void hal_flash_test(void)
18 {
19     int      i = 0;
20     int      j = 0;
21     int      ret = 0;
22     uint32_t off_set = 0;
23 
24     hal_logic_partition_t * partition_info = NULL;
25 
26     printf("*********** flash test start ! ***********\n");
27 
28     partition_info = hal_flash_get_info(HAL_PARTITION_TEST);
29 
30     if (partition_info == NULL) {
31         printf("flash info get error ! test failed\n");
32         return;
33     } else {
34         printf("test partition is %s\n", partition_info->partition_description);
35         printf("test partition bytes: %d\n", partition_info->partition_length);
36     }
37 
38     printf("step1: erase the flash !\n");
39 
40     ret = hal_flash_erase(HAL_PARTITION_TEST, 0, partition_info->partition_length);
41     if (ret != 0) {
42         printf("flash erase return error ! test failed\n");
43         return;
44     }
45 
46     printf("step2: check if the flash is completely erased!\n");
47 
48     off_set = 0;
49 
50     for (i = 0; i < partition_info->partition_length / (TEST_BUFFER_WORDS * sizeof(uint16_t)); i++)
51     {
52     	ret = hal_flash_read(HAL_PARTITION_TEST, &off_set, flash_test_buffer, (TEST_BUFFER_WORDS * sizeof(uint16_t)));
53 
54         if (ret != 0) {
55             printf("flash read error ! test failed\n");
56             return;
57         }
58 
59         for (j = 0; j < TEST_BUFFER_WORDS; j++) {
60         	if (flash_test_buffer[j] != 0xFFFF) {
61                 printf("flash erase data error ! test failed\n");
62                 return;
63         	}
64         }
65     }
66 
67     printf("step3: write the flash !\n");
68 
69     off_set = 0;
70 
71     for (i = 0; i < partition_info->partition_length / (TEST_BUFFER_WORDS * sizeof(uint16_t)); i++)
72     {
73         for (j = 0; j < TEST_BUFFER_WORDS; j++) {
74         	flash_test_buffer[j] = j + i * TEST_BUFFER_WORDS;
75         }
76 
77     	ret = hal_flash_write(HAL_PARTITION_TEST, &off_set, flash_test_buffer, (TEST_BUFFER_WORDS * sizeof(uint16_t)));
78 
79         if (ret != 0) {
80             printf("flash write error ! test failed\n");
81             return;
82         }
83     }
84 
85     printf("step4: read the flash to check the data in flash !\n");
86 
87     off_set = 0;
88 
89     for (i = 0; i < partition_info->partition_length / (TEST_BUFFER_WORDS * sizeof(uint16_t)); i++)
90     {
91     	ret = hal_flash_read(HAL_PARTITION_TEST, &off_set, flash_test_buffer, (TEST_BUFFER_WORDS * sizeof(uint16_t)));
92 
93         if (ret != 0) {
94             printf("flash read error ! test failed\n");
95             return;
96         }
97 
98         for (j = 0; j < TEST_BUFFER_WORDS; j++) {
99         	if (flash_test_buffer[j] != j + i * TEST_BUFFER_WORDS) {
100                 printf("flash read data error ! test failed\n");
101                 return;
102         	}
103         }
104     }
105 
106     printf("flash test result: succeed !\n");
107 
108     printf("*********** flash test end ! ***********\n");
109 }
110