Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_utility.h"
#include "fx_fault_tolerant.h"
...
...
_fx_utility_FAT_entry_write(FX_MEDIA *, ULONG, ULONG)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/src/fx_utility_FAT_entry_write.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** FileX Component */ /** */ /** Utility */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE /* Include necessary system files. */ #include "fx_api.h" #include "fx_system.h" #include "fx_utility.h" #ifdef FX_ENABLE_FAULT_TOLERANT #include "fx_fault_tolerant.h" #endif /* FX_ENABLE_FAULT_TOLERANT */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_utility_FAT_entry_write PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function writes to the supplied FAT entry to all FATs in */ /* the media. 12-bit, 16-bit, and 32-bit FAT writing is supported. */ /* */ /* INPUT */ /* */ /* media_ptr Media control block pointer */ /* cluster Cluster entry number */ /* next_cluster Next cluster integer pointer */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_utility_FAT_flush FLUSH dirty entries in the */ /* FAT cache */ /* _fx_fault_tolerant_add_fat_log Add FAT redo log */ /* */ /* CALLED BY */ /* */ /* FileX System Functions */ /* */ /* 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 */ /* */... /**************************************************************************/ UINT _fx_utility_FAT_entry_write(FX_MEDIA *media_ptr, ULONG cluster, ULONG next_cluster) { UINT status, index, i; FX_FAT_CACHE_ENTRY *cache_entry_ptr; #ifdef FX_ENABLE_FAULT_TOLERANT ULONG FAT_sector; /* While fault_tolerant is enabled, only FAT entries in the same sector are allowed to be cached. */ /* We must flush FAT sectors in the order of FAT chains. */ if (media_ptr -> fx_media_fault_tolerant_enabled && (media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED)) { if (!(media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_SET_FAT_CHAIN)) { /* Redirect this request to log file. */ return(_fx_fault_tolerant_add_FAT_log(media_ptr, cluster, next_cluster)); }if (!(media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_SET_FAT_CHAIN)) { ... } /* Get sector number of the incoming FAT entry. */ FAT_sector = _fx_utility_FAT_sector_get(media_ptr, cluster); /* Is FAT sector changed? */ if (FAT_sector != media_ptr -> fx_media_fault_tolerant_cached_FAT_sector) { /* Are there any cached FAT entries? */ if (media_ptr -> fx_media_fault_tolerant_cached_FAT_sector) { /* Current FAT entry is located in different sector. Force flush. */ /* Flush the cached individual FAT entries */ _fx_utility_FAT_flush(media_ptr); #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Flush exFAT bitmap. */ _fx_utility_exFAT_bitmap_flush(media_ptr); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ }if (media_ptr -> fx_media_fault_tolerant_cached_FAT_sector) { ... } /* Record sector number of current FAT entry. */ media_ptr -> fx_media_fault_tolerant_cached_FAT_sector = FAT_sector; }if (FAT_sector != media_ptr -> fx_media_fault_tolerant_cached_FAT_sector) { ... } }if (media_ptr -> fx_media_fault_tolerant_enabled && (media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED)) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */ #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of FAT entry writes and cache hits. */ media_ptr -> fx_media_fat_entry_writes++; media_ptr -> fx_media_fat_entry_cache_write_hits++;/* ... */ #endif /* Extended port-specific processing macro, which is by default defined to white space. */ FX_UTILITY_FAT_ENTRY_WRITE_EXTENSION /* Calculate the area of the cache for this FAT entry. */ index = (cluster & FX_FAT_CACHE_HASH_MASK) * FX_FAT_CACHE_DEPTH; /* Build a pointer to the cache entry. */ cache_entry_ptr = &media_ptr -> fx_media_fat_cache[index]; /* First search for the entry in the FAT entry cache. */ for (i = 0; i < FX_FAT_CACHE_DEPTH; i++) { /* See if the entry matches the write request. */ if (((cache_entry_ptr + i) -> fx_fat_cache_entry_cluster) == cluster) { /* Yes, we have a matching entry. Save the new information in the FAT cache and mark this entry as dirty. *//* ... */ (cache_entry_ptr + i) -> fx_fat_cache_entry_value = next_cluster; (cache_entry_ptr + i) -> fx_fat_cache_entry_dirty = 1; /* Determine if the driver has requested notification when data sectors in the media become free. This can be useful to FLASH manager software. *//* ... */ if ((media_ptr -> fx_media_driver_free_sector_update) && (next_cluster == FX_FREE_CLUSTER)) { /* Yes, the driver does wish to know that these specific sector(s) are not in use. *//* ... */ #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of driver release sectors requests. */ media_ptr -> fx_media_driver_release_sectors_requests++;/* ... */ #endif /* This cluster is being released so inform the driver that the corresponding sectors are now available. *//* ... */ media_ptr -> fx_media_driver_request = FX_DRIVER_RELEASE_SECTORS; media_ptr -> fx_media_driver_status = FX_IO_ERROR; media_ptr -> fx_media_driver_logical_sector = (media_ptr -> fx_media_data_sector_start + ((cluster - FX_FAT_ENTRY_START) * media_ptr -> fx_media_sectors_per_cluster)); media_ptr -> fx_media_driver_sectors = media_ptr -> fx_media_sectors_per_cluster; /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_RELEASE_SECTORS, media_ptr, media_ptr -> fx_media_driver_logical_sector, media_ptr -> fx_media_driver_sectors, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0) /* Call the driver. */ (media_ptr -> fx_media_driver_entry)(media_ptr); }if ((media_ptr -> fx_media_driver_free_sector_update) && (next_cluster == FX_FREE_CLUSTER)) { ... } /* Done, return successful status. */ return(FX_SUCCESS); }if (((cache_entry_ptr + i) -> fx_fat_cache_entry_cluster) == cluster) { ... } }for (i = 0; i < FX_FAT_CACHE_DEPTH; i++) { ... } /* If we reach this point, we know that the FAT write request is not in the cache. *//* ... */ #ifndef FX_MEDIA_STATISTICS_DISABLE /* Decrement the number of cache hits. */ media_ptr -> fx_media_fat_entry_cache_write_hits--; /* Increment the number of cache misses. */ media_ptr -> fx_media_fat_entry_cache_write_misses++;/* ... */ #endif /* Determine if the oldest entry is dirty and needs to be flushed. */ if (media_ptr -> fx_media_fat_cache[index + 3].fx_fat_cache_entry_dirty == 1) { /* Flush the dirty entry so it can be used to hold the current FAT entry write request. *//* ... */ status = _fx_utility_FAT_flush(media_ptr); /* Determine if the write was successful. */ if (status != FX_SUCCESS) { /* No, return error status to caller. */ return(status); }if (status != FX_SUCCESS) { ... } }if (media_ptr -> fx_media_fat_cache[index + 3].fx_fat_cache_entry_dirty == 1) { ... } /* Move all the cache entries down so the oldest is at the bottom. */ *(cache_entry_ptr + 3) = *(cache_entry_ptr + 2); *(cache_entry_ptr + 2) = *(cache_entry_ptr + 1); *(cache_entry_ptr + 1) = *(cache_entry_ptr); /* Save the current FAT entry write request and mark as dirty. */ cache_entry_ptr -> fx_fat_cache_entry_dirty = 1; cache_entry_ptr -> fx_fat_cache_entry_cluster = cluster; cache_entry_ptr -> fx_fat_cache_entry_value = next_cluster; /* Determine if the driver has requested notification when data sectors in the media become free. This can be useful to FLASH manager software. *//* ... */ if ((media_ptr -> fx_media_driver_free_sector_update) && (next_cluster == FX_FREE_CLUSTER)) { /* Yes, the driver does wish to know that these specific sector(s) are not in use. *//* ... */ #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of driver release sectors requests. */ media_ptr -> fx_media_driver_release_sectors_requests++;/* ... */ #endif /* This cluster is being released so inform the driver that the corresponding sectors are now available. *//* ... */ media_ptr -> fx_media_driver_request = FX_DRIVER_RELEASE_SECTORS; media_ptr -> fx_media_driver_status = FX_IO_ERROR; media_ptr -> fx_media_driver_logical_sector = (media_ptr -> fx_media_data_sector_start + ((cluster - FX_FAT_ENTRY_START) * media_ptr -> fx_media_sectors_per_cluster)); media_ptr -> fx_media_driver_sectors = media_ptr -> fx_media_sectors_per_cluster; /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_RELEASE_SECTORS, media_ptr, media_ptr -> fx_media_driver_logical_sector, media_ptr -> fx_media_driver_sectors, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0) /* Call the driver. */ (media_ptr -> fx_media_driver_entry)(media_ptr); }if ((media_ptr -> fx_media_driver_free_sector_update) && (next_cluster == FX_FREE_CLUSTER)) { ... } /* Return success to caller. */ return(FX_SUCCESS); }{ ... }