Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define LX_SOURCE_CODE
#define LX_DISABLE_ERROR_CHECKING
#include "lx_api.h"
...
...
_lx_nor_flash_logical_sector_find(LX_NOR_FLASH *, ULONG, ULONG, ULONG **, ULONG **)
Files
loading...
CodeScopeSTM32 Libraries and Sampleslevelxcommon/src/lx_nor_flash_logical_sector_find.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** LevelX Component */ /** */ /** NOR Flash */ /** */... /**************************************************************************/ /**************************************************************************/ #define LX_SOURCE_CODE /* Disable ThreadX error checking. */ #ifndef LX_DISABLE_ERROR_CHECKING #define LX_DISABLE_ERROR_CHECKING #endif /* Include necessary system files. */ #include "lx_api.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _lx_nor_flash_logical_sector_find PORTABLE C */ /* 6.1.7 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function attempts to find the specified logical sector in */ /* the NOR flash. */ /* */ /* INPUT */ /* */ /* nor_flash NOR flash instance */ /* logical_sector Logical sector number */ /* superceded_check Check for sector being */ /* superceded (can happen if */ /* on interruptions of sector */ /* write) */ /* physical_sector_map_entry Destination for physical */ /* sector map entry address */ /* physical_sector_address Destination for physical */ /* sector data */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _lx_nor_flash_driver_read Driver flash sector read */ /* _lx_nor_flash_system_error Internal system error handler */ /* */ /* CALLED BY */ /* */ /* Internal LevelX */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 William E. Lamie Modified comment(s), */ /* resulting in version 6.1 */ /* 06-02-2021 Bhupendra Naphade Modified comment(s), */ /* resulting in version 6.1.7 */ /* */... /**************************************************************************/ UINT _lx_nor_flash_logical_sector_find(LX_NOR_FLASH *nor_flash, ULONG logical_sector, ULONG superceded_check, ULONG **physical_sector_map_entry, ULONG **physical_sector_address) { ULONG *block_word_ptr; ULONG *list_word_ptr; ULONG list_word; ULONG min_logical_sector; ULONG max_logical_sector; ULONG mapped_sectors; ULONG total_blocks; ULONG total_sectors; ULONG i, j; ULONG search_start; LX_NOR_SECTOR_MAPPING_CACHE_ENTRY *sector_mapping_cache_entry_ptr = LX_NULL; LX_NOR_SECTOR_MAPPING_CACHE_ENTRY temp_sector_mapping_cache_entry; #ifndef LX_DIRECT_READ UINT status; #endif /* Initialize the return parameters. */ *physical_sector_map_entry = (ULONG *) 0; *physical_sector_address = (ULONG *) 0; /* Determine if there are any mapped physical sectors. */ if (nor_flash -> lx_nor_flash_mapped_physical_sectors == 0) { /* No mapped sector so nothing can be found!. */ return(LX_SECTOR_NOT_FOUND); }if (nor_flash -> lx_nor_flash_mapped_physical_sectors == 0) { ... } /* Determine if the sector mapping cache is enabled. */ if (nor_flash -> lx_nor_flash_sector_mapping_cache_enabled) { /* Calculate the starting index of the sector cache for this sector entry. */ i = (logical_sector & LX_NOR_SECTOR_MAPPING_CACHE_HASH_MASK) * LX_NOR_SECTOR_MAPPING_CACHE_DEPTH; /* Build a pointer to the cache entry. */ sector_mapping_cache_entry_ptr = &nor_flash -> lx_nor_flash_sector_mapping_cache[i]; /* Determine if the sector is in the sector mapping cache - assuming the depth of the sector mapping cache is LX_NOR_SECTOR_MAPPING_CACHE_DEPTH entries. *//* ... */ if ((sector_mapping_cache_entry_ptr -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID)) { /* Increment the sector mapping cache hit counter. */ nor_flash -> lx_nor_flash_sector_mapping_cache_hits++; /* Yes, return the cached values associated with the sector. */ *physical_sector_map_entry = sector_mapping_cache_entry_ptr -> lx_nor_sector_mapping_cache_physical_sector_map_entry; *physical_sector_address = sector_mapping_cache_entry_ptr -> lx_nor_sector_mapping_cache_physical_sector_address; /* Don't move anything since we found the entry at the top. */ /* Return a successful status. */ return(LX_SUCCESS); }if ((sector_mapping_cache_entry_ptr -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID)) { ... } else if (((sector_mapping_cache_entry_ptr + 1) -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID)) { /* Increment the sector mapping cache hit counter. */ nor_flash -> lx_nor_flash_sector_mapping_cache_hits++; /* Yes, return the cached values associated with the sector. */ *physical_sector_map_entry = (sector_mapping_cache_entry_ptr + 1) -> lx_nor_sector_mapping_cache_physical_sector_map_entry; *physical_sector_address = (sector_mapping_cache_entry_ptr + 1) -> lx_nor_sector_mapping_cache_physical_sector_address; /* Just swap the first and second entry. */ temp_sector_mapping_cache_entry = *(sector_mapping_cache_entry_ptr); *(sector_mapping_cache_entry_ptr) = *(sector_mapping_cache_entry_ptr + 1); *(sector_mapping_cache_entry_ptr + 1) = temp_sector_mapping_cache_entry; /* Return a successful status. */ return(LX_SUCCESS); }else if (((sector_mapping_cache_entry_ptr + 1) -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID)) { ... } else if (((sector_mapping_cache_entry_ptr + 2) -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID)) { /* Increment the sector mapping cache hit counter. */ nor_flash -> lx_nor_flash_sector_mapping_cache_hits++; /* Yes, return the cached value. */ *physical_sector_map_entry = (sector_mapping_cache_entry_ptr + 2) -> lx_nor_sector_mapping_cache_physical_sector_map_entry; *physical_sector_address = (sector_mapping_cache_entry_ptr + 2) -> lx_nor_sector_mapping_cache_physical_sector_address; /* Move the third entry to the top and the first two entries down. */ temp_sector_mapping_cache_entry = *(sector_mapping_cache_entry_ptr); *(sector_mapping_cache_entry_ptr) = *(sector_mapping_cache_entry_ptr + 2); *(sector_mapping_cache_entry_ptr + 2) = *(sector_mapping_cache_entry_ptr + 1); *(sector_mapping_cache_entry_ptr + 1) = temp_sector_mapping_cache_entry; /* Return a successful status. */ return(LX_SUCCESS); }else if (((sector_mapping_cache_entry_ptr + 2) -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID)) { ... } else if (((sector_mapping_cache_entry_ptr + 3) -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID)) { /* Increment the sector mapping cache hit counter. */ nor_flash -> lx_nor_flash_sector_mapping_cache_hits++; /* Yes, return the cached value. */ *physical_sector_map_entry = (sector_mapping_cache_entry_ptr + 3) -> lx_nor_sector_mapping_cache_physical_sector_map_entry; *physical_sector_address = (sector_mapping_cache_entry_ptr + 3) -> lx_nor_sector_mapping_cache_physical_sector_address; /* Move the last entry to the top and the first three entries down. */ temp_sector_mapping_cache_entry = *(sector_mapping_cache_entry_ptr); *(sector_mapping_cache_entry_ptr) = *(sector_mapping_cache_entry_ptr + 3); *(sector_mapping_cache_entry_ptr + 3) = *(sector_mapping_cache_entry_ptr + 2); *(sector_mapping_cache_entry_ptr + 2) = *(sector_mapping_cache_entry_ptr + 1); *(sector_mapping_cache_entry_ptr + 1) = temp_sector_mapping_cache_entry; /* Return a successful status. */ return(LX_SUCCESS); }else if (((sector_mapping_cache_entry_ptr + 3) -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID)) { ... } /* If we get here, we have a cache miss so increment the counter before we fall through the loop. */ nor_flash -> lx_nor_flash_sector_mapping_cache_misses++; }if (nor_flash -> lx_nor_flash_sector_mapping_cache_enabled) { ... } /* Setup the total number of mapped sectors. */ mapped_sectors = nor_flash -> lx_nor_flash_mapped_physical_sectors; /* Start searching from the last found block. */ i = nor_flash -> lx_nor_flash_found_block_search; /* Setup the starting sector to look at. */ j = nor_flash -> lx_nor_flash_found_sector_search; /* Pickup the total number of blocks. */ total_blocks = nor_flash -> lx_nor_flash_total_blocks; /* Loop through the blocks to attempt to find the mapped logical sector. */ while (total_blocks--) { /* Setup the block word pointer to the first word of the search block. */ block_word_ptr = (nor_flash -> lx_nor_flash_base_address + (i * nor_flash -> lx_nor_flash_words_per_block)); /* Determine if the minimum and maximum logical sector values are present in the block header. If these are present, we can quickly skip blocks that don't have our sector. *//* ... */ /* Read the minimum and maximum logical sector values in this block. */ #ifdef LX_DIRECT_READ /* Read the word directly. */ min_logical_sector = *(block_word_ptr + LX_NOR_FLASH_MIN_LOGICAL_SECTOR_OFFSET);/* ... */ #else status = _lx_nor_flash_driver_read(nor_flash, block_word_ptr + LX_NOR_FLASH_MIN_LOGICAL_SECTOR_OFFSET, &min_logical_sector, 1); /* Check for an error from flash driver. Drivers should never return an error.. */ if (status) { /* Call system error handler. */ _lx_nor_flash_system_error(nor_flash, status); /* Return the error. */ return(status); }if (status) { ... } /* ... */#endif #ifdef LX_DIRECT_READ /* Read the word directly. */ max_logical_sector = *(block_word_ptr + LX_NOR_FLASH_MAX_LOGICAL_SECTOR_OFFSET);/* ... */ #else status = _lx_nor_flash_driver_read(nor_flash, block_word_ptr + LX_NOR_FLASH_MAX_LOGICAL_SECTOR_OFFSET, &max_logical_sector, 1); /* Check for an error from flash driver. Drivers should never return an error.. */ if (status) { /* Call system error handler. */ _lx_nor_flash_system_error(nor_flash, status); /* Return the error. */ return(status); }if (status) { ... } /* ... */#endif /* Are the values valid? */ if ((min_logical_sector != LX_ALL_ONES) && (max_logical_sector != LX_ALL_ONES)) { /* Now let's check to see if the search sector is within this range. */ if ((logical_sector < min_logical_sector) || (logical_sector > max_logical_sector)) { /* Move to the next block. */ i++; /* Determine if we have wrapped. */ if (i >= nor_flash -> lx_nor_flash_total_blocks) { /* Yes, we have wrapped, set to block 0. */ i = 0; }if (i >= nor_flash -> lx_nor_flash_total_blocks) { ... } /* Start at the first sector in the next block. */ j = 0; /* No point in looking further into this block, just continue the loop. */ continue; }if ((logical_sector < min_logical_sector) || (logical_sector > max_logical_sector)) { ... } }if ((min_logical_sector != LX_ALL_ONES) && (max_logical_sector != LX_ALL_ONES)) { ... } /* Setup the total number of sectors. */ total_sectors = nor_flash -> lx_nor_flash_physical_sectors_per_block; /* Remember the start of the search. */ search_start = j; /* Now search through the sector list to find a match. */ while (total_sectors--) { /* Setup a pointer to the mapped list. */ list_word_ptr = block_word_ptr + nor_flash -> lx_nor_flash_block_physical_sector_mapping_offset + j; /* Read in the mapped list for this block. */ #ifdef LX_DIRECT_READ /* Read the word directly. */ list_word = *(list_word_ptr);/* ... */ #else status = _lx_nor_flash_driver_read(nor_flash, list_word_ptr, &list_word, 1); /* Check for an error from flash driver. Drivers should never return an error.. */ if (status) { /* Call system error handler. */ _lx_nor_flash_system_error(nor_flash, status); /* Return the error. */ return(status); }if (status) { ... } /* ... */#endif /* Determine if the entry hasn't been used. */ if (list_word == LX_NOR_PHYSICAL_SECTOR_FREE) { /* Since the mapping is done sequentially in the block, we know nothing else exists after this point. *//* ... */ /* Determine if the search started at the beginning of the block. */ if (search_start == 0) { /* Yes, we started at the beginning of the block. We are now done with this block. */ break; }if (search_start == 0) { ... } else { /* Setup the new total to the search start. */ total_sectors = search_start; /* Clear search start. */ search_start = 0; /* Start search over. */ j = 0; continue; }else { ... } }if (list_word == LX_NOR_PHYSICAL_SECTOR_FREE) { ... } /* Is this entry valid? */ if ((list_word & (LX_NOR_PHYSICAL_SECTOR_VALID | LX_NOR_PHYSICAL_SECTOR_MAPPING_NOT_VALID)) == LX_NOR_PHYSICAL_SECTOR_VALID) { /* Decrement the number of mapped sectors. */ mapped_sectors--; /* Do we have a valid sector match? */ if ((list_word & LX_NOR_LOGICAL_SECTOR_MASK) == logical_sector) { /* Determine if we care about the superceded bit. */ if (superceded_check == LX_FALSE) { /* Prepare the return information. */ *physical_sector_map_entry = list_word_ptr; *physical_sector_address = block_word_ptr + nor_flash -> lx_nor_flash_block_physical_sector_offset + (j * LX_NOR_SECTOR_SIZE); /* Determine if the sector mapping cache is enabled. */ if (nor_flash -> lx_nor_flash_sector_mapping_cache_enabled) { /* Yes, update the cache with the sector mapping. */ /* Move all the cache entries down so the oldest is at the bottom. */ *(sector_mapping_cache_entry_ptr + 3) = *(sector_mapping_cache_entry_ptr + 2); *(sector_mapping_cache_entry_ptr + 2) = *(sector_mapping_cache_entry_ptr + 1); *(sector_mapping_cache_entry_ptr + 1) = *(sector_mapping_cache_entry_ptr); /* Setup the new sector information in the cache. */ sector_mapping_cache_entry_ptr -> lx_nor_sector_mapping_cache_logical_sector = (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID); sector_mapping_cache_entry_ptr -> lx_nor_sector_mapping_cache_physical_sector_map_entry = *physical_sector_map_entry; sector_mapping_cache_entry_ptr -> lx_nor_sector_mapping_cache_physical_sector_address = *physical_sector_address; }if (nor_flash -> lx_nor_flash_sector_mapping_cache_enabled) { ... } /* Remember the last found block for next search. */ nor_flash -> lx_nor_flash_found_block_search = i; /* Remember the last found sector. */ nor_flash -> lx_nor_flash_found_sector_search = j+1; /* Has this wrapped around? */ if (nor_flash -> lx_nor_flash_found_sector_search >= nor_flash -> lx_nor_flash_physical_sectors_per_block) { /* Reset to the beginning sector. */ nor_flash -> lx_nor_flash_found_sector_search = 0; }if (nor_flash -> lx_nor_flash_found_sector_search >= nor_flash -> lx_nor_flash_physical_sectors_per_block) { ... } /* Return success! */ return(LX_SUCCESS); }if (superceded_check == LX_FALSE) { ... } /* Check for the superceded bit being clear, which means the sector was superceded. */ else if (list_word & LX_NOR_PHYSICAL_SECTOR_SUPERCEDED) { /* Prepare the return information. */ *physical_sector_map_entry = list_word_ptr; *physical_sector_address = block_word_ptr + nor_flash -> lx_nor_flash_block_physical_sector_offset + (j * LX_NOR_SECTOR_SIZE); /* No need to update the cache here, since this condition only happens during initialization. */ /* Remember the last found block for next search. */ nor_flash -> lx_nor_flash_found_block_search = i; /* Remember the last found sector. */ nor_flash -> lx_nor_flash_found_sector_search = j+1; /* Has this wrapped around? */ if (nor_flash -> lx_nor_flash_found_sector_search >= nor_flash -> lx_nor_flash_physical_sectors_per_block) { /* Reset to the beginning sector. */ nor_flash -> lx_nor_flash_found_sector_search = 0; }if (nor_flash -> lx_nor_flash_found_sector_search >= nor_flash -> lx_nor_flash_physical_sectors_per_block) { ... } /* Return success! */ return(LX_SUCCESS); }else if (list_word & LX_NOR_PHYSICAL_SECTOR_SUPERCEDED) { ... } }if ((list_word & LX_NOR_LOGICAL_SECTOR_MASK) == logical_sector) { ... } }if ((list_word & (LX_NOR_PHYSICAL_SECTOR_VALID | LX_NOR_PHYSICAL_SECTOR_MAPPING_NOT_VALID)) == LX_NOR_PHYSICAL_SECTOR_VALID) { ... } /* Move to the next list entry. */ j++; /* Check for wrap around. */ if (j >= nor_flash -> lx_nor_flash_physical_sectors_per_block) { /* Yes, wrap around, go back to the beginning. */ j = 0; }if (j >= nor_flash -> lx_nor_flash_physical_sectors_per_block) { ... } }while (total_sectors--) { ... } /* Determine if there are any more mapped sectors. */ if (mapped_sectors == 0) break; /* Move to the next block. */ i++; /* Determine if we have wrapped. */ if (i >= nor_flash -> lx_nor_flash_total_blocks) { /* Yes, we have wrapped, set to block 0. */ i = 0; }if (i >= nor_flash -> lx_nor_flash_total_blocks) { ... } /* Start at the first sector in the next block. */ j = 0; }while (total_blocks--) { ... } /* Return sector not found status. */ return(LX_SECTOR_NOT_FOUND); }{ ... }