1
10
13
14
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
132
138
139
140
141
142
143
144
145
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
208
209
210
216
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
...
...
...
#define LX_SOURCE_CODE
#ifndef LX_DISABLE_ERROR_CHECKING
#define LX_DISABLE_ERROR_CHECKING
#endif
#include "lx_api.h"
...
...
UINT _lx_nor_flash_next_block_to_erase_find(LX_NOR_FLASH *nor_flash, ULONG *return_erase_block, ULONG *return_erase_count, ULONG *return_mapped_sectors, ULONG *return_obsolete_sectors)
{
ULONG *block_word_ptr;
ULONG *list_word_ptr;
ULONG list_word;
ULONG i, j;
ULONG mapped_sectors;
ULONG erase_count;
ULONG obsolete_sectors;
ULONG min_block_erase = 0;
ULONG min_block_erase_count;
ULONG min_block_obsolete_count = 0;
ULONG min_block_mapped_count = 0;
ULONG max_obsolete_sectors;
ULONG max_obsolete_block = 0;
ULONG max_obsolete_erase_count = 0;
ULONG max_obsolete_mapped_count = 0;
ULONG min_system_block_erase_count;
ULONG max_system_block_erase_count;
ULONG erase_count_threshold;
#ifndef LX_DIRECT_READ
UINT status;
#endif
block_word_ptr = nor_flash -> lx_nor_flash_base_address;
min_block_erase_count = LX_ALL_ONES;
min_system_block_erase_count = LX_ALL_ONES;
max_system_block_erase_count = 0;
max_obsolete_sectors = 0;
if (nor_flash -> lx_nor_flash_free_physical_sectors >= nor_flash -> lx_nor_flash_physical_sectors_per_block)
{
erase_count_threshold = nor_flash -> lx_nor_flash_minimum_erase_count + LX_NOR_FLASH_MAX_ERASE_COUNT_DELTA;
}if (nor_flash -> lx_nor_flash_free_physical_sectors >= nor_flash -> lx_nor_flash_physical_sectors_per_block) { ... }
else
{
erase_count_threshold = LX_ALL_ONES;
}else { ... }
for (i = 0; i < nor_flash -> lx_nor_flash_total_blocks; i++)
{
#ifdef LX_DIRECT_READ
erase_count = *(block_word_ptr);/* ... */
#else
status = _lx_nor_flash_driver_read(nor_flash, block_word_ptr, &erase_count, 1);
if (status)
{
_lx_nor_flash_system_error(nor_flash, status);
return(status);
}if (status) { ... }
/* ... */#endif
if (erase_count < min_system_block_erase_count)
min_system_block_erase_count = erase_count;
if (erase_count > max_system_block_erase_count)
max_system_block_erase_count = erase_count;
obsolete_sectors = 0;
mapped_sectors = 0;
list_word_ptr = block_word_ptr + nor_flash -> lx_nor_flash_block_physical_sector_mapping_offset;
for (j = 0; j < nor_flash -> lx_nor_flash_physical_sectors_per_block; j++)
{
#ifdef LX_DIRECT_READ
list_word = *(list_word_ptr);/* ... */
#else
status = _lx_nor_flash_driver_read(nor_flash, list_word_ptr, &list_word, 1);
if (status)
{
_lx_nor_flash_system_error(nor_flash, status);
return(status);
}if (status) { ... }
/* ... */#endif
if (list_word == LX_NOR_PHYSICAL_SECTOR_FREE)
{
/* ... */
break;
}if (list_word == LX_NOR_PHYSICAL_SECTOR_FREE) { ... }
if ((list_word & LX_NOR_PHYSICAL_SECTOR_VALID) == 0)
{
obsolete_sectors++;
}if ((list_word & LX_NOR_PHYSICAL_SECTOR_VALID) == 0) { ... }
else
{
mapped_sectors++;
}else { ... }
list_word_ptr++;
}for (j = 0; j < nor_flash -> lx_nor_flash_physical_sectors_per_block; j++) { ... }
if ((obsolete_sectors > max_obsolete_sectors) && (erase_count <= erase_count_threshold))
{
max_obsolete_sectors = obsolete_sectors;
max_obsolete_block = i;
max_obsolete_erase_count = erase_count;
max_obsolete_mapped_count = mapped_sectors;
}if ((obsolete_sectors > max_obsolete_sectors) && (erase_count <= erase_count_threshold)) { ... }
else if ((max_obsolete_sectors) && (obsolete_sectors == max_obsolete_sectors) && (erase_count <= erase_count_threshold))
{
/* ... */
if (erase_count < max_obsolete_erase_count)
{
max_obsolete_sectors = obsolete_sectors;
max_obsolete_block = i;
max_obsolete_erase_count = erase_count;
max_obsolete_mapped_count = mapped_sectors;
}if (erase_count < max_obsolete_erase_count) { ... }
}else if ((max_obsolete_sectors) && (obsolete_sectors == max_obsolete_sectors) && (erase_count <= erase_count_threshold)) { ... }
if (erase_count < min_block_erase_count)
{
min_block_erase_count = erase_count;
min_block_erase = i;
min_block_obsolete_count = obsolete_sectors;
min_block_mapped_count = mapped_sectors;
}if (erase_count < min_block_erase_count) { ... }
block_word_ptr = block_word_ptr + nor_flash -> lx_nor_flash_words_per_block;
}for (i = 0; i < nor_flash -> lx_nor_flash_total_blocks; i++) { ... }
if (max_obsolete_sectors)
{
*return_erase_block = max_obsolete_block;
*return_erase_count = max_obsolete_erase_count;
*return_obsolete_sectors = max_obsolete_sectors;
*return_mapped_sectors = max_obsolete_mapped_count;
}if (max_obsolete_sectors) { ... }
else
{
*return_erase_block = min_block_erase;
*return_erase_count = min_block_erase_count;
*return_obsolete_sectors = min_block_obsolete_count;
*return_mapped_sectors = min_block_mapped_count;
}else { ... }
nor_flash -> lx_nor_flash_minimum_erase_count = min_system_block_erase_count;
nor_flash -> lx_nor_flash_maximum_erase_count = max_system_block_erase_count;
return(LX_SUCCESS);
}{ ... }