Select one of the symbols to view example projects that use it.
 
Outline
...
#include "fx_stm32_sd_driver.h"
scratch
is_initialized
check_sd_status(uint32_t)
fx_stm32_sd_driver(FX_MEDIA *)
sd_read_data(FX_MEDIA *, ULONG, UINT, UINT)
sd_write_data(FX_MEDIA *, ULONG, UINT, UINT)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/drivers/fx_stm32_sd_driver.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Partial 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. */ /* Partial Copyright (c) STMicroelctronics 2020. All rights reserved */... /**************************************************************************/ /* Include necessary system files. */ #include "fx_stm32_sd_driver.h" /* * the scratch buffer is required when performing DMA transfers using unaligned addresses * When CPU cache is enabled, the scratch buffer should be 32-byte aligned to match a whole cache line * otherwise it is 4-byte aligned to match the DMA alignment constraints *//* ... */ #if (FX_STM32_SD_CACHE_MAINTENANCE == 1) static UCHAR scratch[FX_STM32_SD_DEFAULT_SECTOR_SIZE] __attribute__ ((aligned (32))); #else static UCHAR scratch[FX_STM32_SD_DEFAULT_SECTOR_SIZE] __attribute__ ((aligned (4))); #endif UINT _fx_partition_offset_calculate(void *partition_sector, UINT partition, ULONG *partition_start, ULONG *partition_size); static UINT sd_read_data(FX_MEDIA *media_ptr, ULONG sector, UINT num_sectors, UINT use_scratch_buffer); static UINT sd_write_data(FX_MEDIA *media_ptr, ULONG sector, UINT num_sectors, UINT use_scratch_buffer); static UINT is_initialized = 0; static INT check_sd_status(uint32_t instance) { uint32_t start = FX_STM32_SD_CURRENT_TIME(); while (FX_STM32_SD_CURRENT_TIME() - start < FX_STM32_SD_DEFAULT_TIMEOUT) { if (fx_stm32_sd_get_status(instance) == 0) { return 0; }if (fx_stm32_sd_get_status(instance) == 0) { ... } }while (FX_STM32_SD_CURRENT_TIME() - start < FX_STM32_SD_DEFAULT_TIMEOUT) { ... } return 1; }{ ... } /** * @brief This function is the entry point to the STM32 SD disk driver. * It relies on the STM32 peripheral library from ST. * @param media_ptr: FileX's Media Config Block * @retval None *//* ... */ VOID fx_stm32_sd_driver(FX_MEDIA *media_ptr) { UINT status; UINT unaligned_buffer; ULONG partition_start; ULONG partition_size; #if (FX_STM32_SD_INIT == 0) /* the SD was initialized by the application */ is_initialized = 1;/* ... */ #endif /* before performing any operation, check the status of the SD IP */ if (is_initialized == 1) { if (check_sd_status(FX_STM32_SD_INSTANCE) != 0) { media_ptr->fx_media_driver_status = FX_IO_ERROR; return; }if (check_sd_status(FX_STM32_SD_INSTANCE) != 0) { ... } }if (is_initialized == 1) { ... } #if (FX_STM32_SD_DMA_API == 1) /* the SD DMA requires a 4-byte aligned buffers */ unaligned_buffer = (UINT)(media_ptr->fx_media_driver_buffer) & 0x3;/* ... */ #else /* if the DMA is not used there isn't any constraint on buffer alignment */ unaligned_buffer = 0;/* ... */ #endif /* Process the driver request specified in the media control block. */ switch(media_ptr->fx_media_driver_request) { case FX_DRIVER_INIT: { media_ptr->fx_media_driver_status = FX_SUCCESS; FX_STM32_SD_PRE_INIT(media_ptr); #if (FX_STM32_SD_INIT == 1) /* Initialize the SD instance */ if (is_initialized == 0) { status = fx_stm32_sd_init(FX_STM32_SD_INSTANCE); if (status == 0) { is_initialized = 1; }if (status == 0) { ... } else { media_ptr->fx_media_driver_status = FX_IO_ERROR; }else { ... } }if (is_initialized == 0) { ... } /* ... */#endif /* call post init user macro */ FX_STM32_SD_POST_INIT(media_ptr); break; ...} case FX_DRIVER_INIT: case FX_DRIVER_UNINIT: { media_ptr->fx_media_driver_status = FX_SUCCESS; #if (FX_STM32_SD_INIT == 1) status = fx_stm32_sd_deinit(FX_STM32_SD_INSTANCE); if (status != 0) { media_ptr->fx_media_driver_status = FX_IO_ERROR; }if (status != 0) { ... } else { is_initialized = 0; }else { ... } /* ... */#endif /* call post deinit processing */ FX_STM32_SD_POST_DEINIT(media_ptr); break; ...} case FX_DRIVER_UNINIT: case FX_DRIVER_READ: { media_ptr->fx_media_driver_status = FX_IO_ERROR; if (sd_read_data(media_ptr, media_ptr->fx_media_driver_logical_sector + media_ptr->fx_media_hidden_sectors, media_ptr->fx_media_driver_sectors, unaligned_buffer) == FX_SUCCESS) { media_ptr->fx_media_driver_status = FX_SUCCESS; }if (sd_read_data(media_ptr, media_ptr->fx_media_driver_logical_sector + media_ptr->fx_media_hidden_sectors, media_ptr->fx_media_driver_sectors, unaligned_buffer) == FX_SUCCESS) { ... } break; ...} case FX_DRIVER_READ: case FX_DRIVER_WRITE: { media_ptr->fx_media_driver_status = FX_IO_ERROR; if (sd_write_data(media_ptr, media_ptr->fx_media_driver_logical_sector + media_ptr->fx_media_hidden_sectors, media_ptr->fx_media_driver_sectors, unaligned_buffer) == FX_SUCCESS) { media_ptr->fx_media_driver_status = FX_SUCCESS; }if (sd_write_data(media_ptr, media_ptr->fx_media_driver_logical_sector + media_ptr->fx_media_hidden_sectors, media_ptr->fx_media_driver_sectors, unaligned_buffer) == FX_SUCCESS) { ... } break; ...} case FX_DRIVER_WRITE: case FX_DRIVER_FLUSH: { /* Return driver success. */ media_ptr->fx_media_driver_status = FX_SUCCESS; break; ...} case FX_DRIVER_FLUSH: case FX_DRIVER_ABORT: { /* Return driver success. */ media_ptr->fx_media_driver_status = FX_SUCCESS; FX_STM32_SD_POST_ABORT(media_ptr); break; ...} case FX_DRIVER_ABORT: case FX_DRIVER_BOOT_READ: { /* the boot sector is the sector zero */ status = sd_read_data(media_ptr, 0, media_ptr->fx_media_driver_sectors, unaligned_buffer); if (status != FX_SUCCESS) { media_ptr->fx_media_driver_status = status; break; }if (status != FX_SUCCESS) { ... } /* Check if the sector 0 is the actual boot sector, otherwise calculate the offset into it. Please note that this should belong to higher level of MW to do this check and it is here as a temporary work solution *//* ... */ partition_start = 0; status = _fx_partition_offset_calculate(media_ptr -> fx_media_driver_buffer, 0, &partition_start, &partition_size); /* Check partition read error. */ if (status) { /* Unsuccessful driver request. */ media_ptr -> fx_media_driver_status = FX_IO_ERROR; break; }if (status) { ... } /* Now determine if there is a partition... */ if (partition_start) { if (check_sd_status(FX_STM32_SD_INSTANCE) != 0) { media_ptr->fx_media_driver_status = FX_IO_ERROR; break; }if (check_sd_status(FX_STM32_SD_INSTANCE) != 0) { ... } /* Yes, now lets read the actual boot record. */ status = sd_read_data(media_ptr, partition_start, media_ptr->fx_media_driver_sectors, unaligned_buffer); if (status != FX_SUCCESS) { media_ptr->fx_media_driver_status = status; break; }if (status != FX_SUCCESS) { ... } }if (partition_start) { ... } /* Successful driver request. */ media_ptr -> fx_media_driver_status = FX_SUCCESS; break; ...} case FX_DRIVER_BOOT_READ: case FX_DRIVER_BOOT_WRITE: { status = sd_write_data(media_ptr, 0, media_ptr->fx_media_driver_sectors, unaligned_buffer); media_ptr->fx_media_driver_status = status; break; ...} case FX_DRIVER_BOOT_WRITE: default: { media_ptr->fx_media_driver_status = FX_IO_ERROR; break; ...}default }switch (media_ptr->fx_media_driver_request) { ... } }{ ... } /** * @brief Read data from uSD into destination buffer * @param FX_MEDIA *media_ptr a pointer the main FileX structure * @param ULONG start_sector first sector to start reading from * @param UINT num_sectors number of sectors to be read * @param UINT use_scratch_buffer to enable scratch buffer usage or not. * @retval FX_SUCCESS on success FX_BUFFER_ERROR / FX_ACCESS_ERROR / FX_IO_ERROR otherwise *//* ... */ static UINT sd_read_data(FX_MEDIA *media_ptr, ULONG start_sector, UINT num_sectors, UINT use_scratch_buffer) { INT i = 0; UINT status; UCHAR *read_addr; /* perform the Pre read operations */ FX_STM32_SD_PRE_READ_TRANSFER(media_ptr); if (use_scratch_buffer) { read_addr = media_ptr->fx_media_driver_buffer; for (i = 0; i < num_sectors; i++) { /* Start reading into the scratch buffer */ status = fx_stm32_sd_read_blocks(FX_STM32_SD_INSTANCE, (UINT *)scratch, (UINT)start_sector++, 1); if (status != 0) { /* read error occurred, call the error handler code then return immediately */ FX_STM32_SD_READ_TRANSFER_ERROR(status); return FX_IO_ERROR; }if (status != 0) { ... } /* wait for read transfer notification */ FX_STM32_SD_READ_CPLT_NOTIFY(); #if (FX_STM32_SD_CACHE_MAINTENANCE == 1) invalidate_cache_by_addr((uint32_t*)scratch, FX_STM32_SD_DEFAULT_SECTOR_SIZE); #endif _fx_utility_memory_copy(scratch, read_addr, FX_STM32_SD_DEFAULT_SECTOR_SIZE); read_addr += FX_STM32_SD_DEFAULT_SECTOR_SIZE; }for (i = 0; i < num_sectors; i++) { ... } /* Check if all sectors were read */ if (i == num_sectors) { status = FX_SUCCESS; }if (i == num_sectors) { ... } else { status = FX_BUFFER_ERROR; }else { ... } }if (use_scratch_buffer) { ... } else { status = fx_stm32_sd_read_blocks(FX_STM32_SD_INSTANCE, (UINT *)media_ptr->fx_media_driver_buffer, (UINT)start_sector, num_sectors); if (status != 0) { /* read error occurred, call the error handler code then return immediately */ FX_STM32_SD_READ_TRANSFER_ERROR(status); return FX_IO_ERROR; }if (status != 0) { ... } /* wait for read transfer notification */ FX_STM32_SD_READ_CPLT_NOTIFY(); #if (FX_STM32_SD_CACHE_MAINTENANCE == 1) invalidate_cache_by_addr((uint32_t*)media_ptr->fx_media_driver_buffer, num_sectors * FX_STM32_SD_DEFAULT_SECTOR_SIZE); #endif status = FX_SUCCESS; }else { ... } /* Operation finished, call the post read macro if defined */ FX_STM32_SD_POST_READ_TRANSFER(media_ptr); return status; }{ ... } /** * @brief write data buffer into the uSD * @param FX_MEDIA *media_ptr a pointer the main FileX structure * @param ULONG start_sector first sector to start writing from * @param UINT num_sectors number of sectors to be written * @param UINT use_scratch_buffer to enable scratch buffer usage or not. * @retval FX_SUCCESS on success FX_BUFFER_ERROR / FX_ACCESS_ERROR / FX_IO_ERROR otherwise *//* ... */ static UINT sd_write_data(FX_MEDIA *media_ptr, ULONG start_sector, UINT num_sectors, UINT use_scratch_buffer) { INT i = 0; UINT status; UCHAR *write_addr; /* call Pre write operation macro */ FX_STM32_SD_PRE_WRITE_TRANSFER(media_ptr); if (use_scratch_buffer) { write_addr = media_ptr->fx_media_driver_buffer; for (i = 0; i < num_sectors; i++) { _fx_utility_memory_copy(write_addr, scratch, FX_STM32_SD_DEFAULT_SECTOR_SIZE); write_addr += FX_STM32_SD_DEFAULT_SECTOR_SIZE; #if (FX_STM32_SD_CACHE_MAINTENANCE == 1) /* Clean the DCache to make the SD DMA see the actual content of the scratch buffer */ clean_cache_by_addr((uint32_t*)scratch, FX_STM32_SD_DEFAULT_SECTOR_SIZE);/* ... */ #endif status = fx_stm32_sd_write_blocks(FX_STM32_SD_INSTANCE, (UINT *)scratch, (UINT)start_sector++, 1); if (status != 0) { /* in case of error call the error handling macro */ FX_STM32_SD_WRITE_TRANSFER_ERROR(status); return FX_IO_ERROR; }if (status != 0) { ... } /* */ FX_STM32_SD_WRITE_CPLT_NOTIFY(); }for (i = 0; i < num_sectors; i++) { ... } if (i == num_sectors) { status = FX_SUCCESS; }if (i == num_sectors) { ... } else { status = FX_BUFFER_ERROR; }else { ... } }if (use_scratch_buffer) { ... } else { #if (FX_STM32_SD_CACHE_MAINTENANCE == 1) clean_cache_by_addr((uint32_t*)media_ptr->fx_media_driver_buffer, num_sectors * FX_STM32_SD_DEFAULT_SECTOR_SIZE); #endif status = fx_stm32_sd_write_blocks(FX_STM32_SD_INSTANCE, (UINT *)media_ptr->fx_media_driver_buffer, (UINT)start_sector, num_sectors); if (status != 0) { FX_STM32_SD_WRITE_TRANSFER_ERROR(status); return FX_IO_ERROR; }if (status != 0) { ... } /* when defined, wait for the write notification */ FX_STM32_SD_WRITE_CPLT_NOTIFY(); status = FX_SUCCESS; }else { ... } /* perform post write operations */ FX_STM32_SD_POST_WRITE_TRANSFER(media_ptr); return status; }{ ... }