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_physical_sector_allocate(LX_NOR_FLASH *, ULONG, ULONG **, ULONG **)
Files
loading...
CodeScopeSTM32 Libraries and Sampleslevelxcommon/src/lx_nor_flash_physical_sector_allocate.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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_physical_sector_allocate PORTABLE C */ /* 6.1.7 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function allocates a free physical sector for mapping to a */ /* logical sector. */ /* */ /* INPUT */ /* */ /* nor_flash NOR flash instance */ /* logical_sector Logical sector number */ /* physical_sector_map_entry Pointer to sector map entry */ /* physical_sector_address Address of physical sector */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _lx_nor_flash_driver_write Driver flash sector write */ /* _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_physical_sector_allocate(LX_NOR_FLASH *nor_flash, ULONG logical_sector, ULONG **physical_sector_map_entry, ULONG **physical_sector_address) { ULONG search_block; ULONG *block_word_ptr; ULONG block_word; ULONG min_logical_sector; ULONG max_logical_sector; ULONG *list_word_ptr; ULONG list_word; ULONG i, j, k, l; UINT status; /* Increment the number of physical sector allocation requests. */ nor_flash -> lx_nor_flash_physical_block_allocates++; /* Initialize the return parameters. */ *physical_sector_map_entry = (ULONG *) 0; *physical_sector_address = (ULONG *) 0; /* Determine if there are any free physical sectors. */ if (nor_flash -> lx_nor_flash_free_physical_sectors == 0) { /* Increment the number of failed allocations. */ nor_flash -> lx_nor_flash_physical_block_allocate_errors++; /* No free physical sectors, return . */ return(LX_NO_SECTORS); }if (nor_flash -> lx_nor_flash_free_physical_sectors == 0) { ... } /* Pickup the search for a free physical sector at the specified block. */ search_block = nor_flash -> lx_nor_flash_free_block_search; /* Loop through the blocks to find a free physical sector. */ for (i = 0; i < nor_flash -> lx_nor_flash_total_blocks; i++) { /* Setup the block word pointer to the first word of the search block. */ block_word_ptr = nor_flash -> lx_nor_flash_base_address + (search_block * nor_flash -> lx_nor_flash_words_per_block); /* Find the first free physical sector from the free sector bit map of this block. */ for (j = 0; j < nor_flash -> lx_nor_flash_block_bit_map_words; j++) { /* Read this word of the free sector bit map. */ #ifdef LX_DIRECT_READ /* Read the word directly. */ block_word = *(block_word_ptr + nor_flash -> lx_nor_flash_block_free_bit_map_offset + j);/* ... */ #else status = _lx_nor_flash_driver_read(nor_flash, (block_word_ptr + nor_flash -> lx_nor_flash_block_free_bit_map_offset + j), &block_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 /* Are there any free sectors in this word? */ if (block_word) { /* Yes, there are free sectors in this word. */ for (k = 0; k < 32; k++) { /* Is this sector free? */ if (block_word & 1) { /* Yes, this sector is free, clear the bit for this sector in the free sector map. */ /* Read this word of the free sector bit map again. */ #ifdef LX_DIRECT_READ /* Read the word directly. */ block_word = *(block_word_ptr + nor_flash -> lx_nor_flash_block_free_bit_map_offset + j);/* ... */ #else status = _lx_nor_flash_driver_read(nor_flash, (block_word_ptr + nor_flash -> lx_nor_flash_block_free_bit_map_offset + j), &block_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 /* Clear the bit associated with the free sector to indicate it is not free. */ block_word = block_word & ~(((ULONG) 1) << k); /* Now write back free bit map word with the bit for this sector cleared. */ status = _lx_nor_flash_driver_write(nor_flash, (block_word_ptr + nor_flash -> lx_nor_flash_block_free_bit_map_offset + j), &block_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) { ... } /* Determine if this is the last entry available in this block. */ if (((block_word >> 1) == 0) && (j == (nor_flash -> lx_nor_flash_block_bit_map_words - 1))) { /* This is the last physical sector in the block. Now we need to calculate the minimum valid logical sector and the maximum valid logical sector. *//* ... */ /* Setup the minimum and maximum logical sectors to the current logical sector. */ min_logical_sector = logical_sector; max_logical_sector = logical_sector; /* Setup a pointer to the mapped list. */ list_word_ptr = block_word_ptr + nor_flash -> lx_nor_flash_block_physical_sector_mapping_offset; /* Loop to search the mapped list. */ for (l = 0; l < nor_flash -> lx_nor_flash_physical_sectors_per_block; l++) { /* Read the mapped sector entry. */ #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 /* Is this entry valid? */ if (list_word & LX_NOR_PHYSICAL_SECTOR_VALID) { /* Isolate the logical sector. */ list_word = list_word & LX_NOR_LOGICAL_SECTOR_MASK; /* Determine if a new minimum has been found. */ if (list_word < min_logical_sector) min_logical_sector = list_word; /* Determine if a new maximum has been found. */ if (list_word != LX_NOR_LOGICAL_SECTOR_MASK) { if (list_word > max_logical_sector) max_logical_sector = list_word; }if (list_word != LX_NOR_LOGICAL_SECTOR_MASK) { ... } }if (list_word & LX_NOR_PHYSICAL_SECTOR_VALID) { ... } /* Move the list pointer ahead. */ list_word_ptr++; }for (l = 0; l < nor_flash -> lx_nor_flash_physical_sectors_per_block; l++) { ... } /* Move the search pointer forward, since we know this block is exhausted. */ search_block++; /* Check for wrap condition on the search block. */ if (search_block >= nor_flash -> lx_nor_flash_total_blocks) { /* Reset search block to the beginning. */ search_block = 0; }if (search_block >= nor_flash -> lx_nor_flash_total_blocks) { ... } /* Now write the minimum and maximum logical sector in this block. */ status = _lx_nor_flash_driver_write(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) { ... } status = _lx_nor_flash_driver_write(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) { ... } }if (((block_word >> 1) == 0) && (j == (nor_flash -> lx_nor_flash_block_bit_map_words - 1))) { ... } /* Remember the block to search. */ nor_flash -> lx_nor_flash_free_block_search = search_block; /* Prepare the return information. */ *physical_sector_map_entry = block_word_ptr + (nor_flash -> lx_nor_flash_block_physical_sector_mapping_offset + (j * 32)) + k; *physical_sector_address = block_word_ptr + (nor_flash -> lx_nor_flash_block_physical_sector_offset) + (((j * 32) + k) * LX_NOR_SECTOR_SIZE); /* Return success! */ return(LX_SUCCESS); }if (block_word & 1) { ... } /* Shift down the bit map. */ block_word = block_word >> 1; /* Determine if there are any more bits set? If not, we can break out of the search of this word. */ if (block_word == 0) break; }for (k = 0; k < 32; k++) { ... } }if (block_word) { ... } }for (j = 0; j < nor_flash -> lx_nor_flash_block_bit_map_words; j++) { ... } /* Move to the next flash block. */ search_block++; /* Determine if we have to wrap the search block. */ if (search_block >= nor_flash -> lx_nor_flash_total_blocks) { /* Set the search block to the beginning. */ search_block = 0; }if (search_block >= nor_flash -> lx_nor_flash_total_blocks) { ... } }for (i = 0; i < nor_flash -> lx_nor_flash_total_blocks; i++) { ... } /* Increment the number of failed allocations. */ nor_flash -> lx_nor_flash_physical_block_allocate_errors++; /* Return no sector completion. */ return(LX_NO_SECTORS); }{ ... }