1
10
11
12
13
14
15
16
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
50
51
57
58
59
60
61
62
63
64
65
67
68
69
77
78
79
81
82
84
85
86
87
88
89
90
91
92
93
94
95
110
111
112
113
114
115
116
117
118
119
120
131
132
133
134
135
136
137
150
163
170
179
180
181
182
183
184
189
190
193
194
195
196
197
198
199
200
206
207
208
209
210
211
216
217
218
219
220
225
226
227
228
229
230
231
232
241
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
299
303
304
305
306
307
308
309
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
363
364
365
366
367
373
374
375
376
377
378
382
386
387
388
389
390
391
392
393
394
399
400
401
402
403
404
405
406
407
408
409
410
411
...
#include "fx_stm32_sd_driver.h"
/* ... */
#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;
}{ ... }
/* ... */
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)
is_initialized = 1;/* ... */
#endif
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)
unaligned_buffer = (UINT)(media_ptr->fx_media_driver_buffer) & 0x3;/* ... */
#else
unaligned_buffer = 0;/* ... */
#endif
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)
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
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
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:
{
media_ptr->fx_media_driver_status = FX_SUCCESS;
break;
...}
case FX_DRIVER_FLUSH:
case FX_DRIVER_ABORT:
{
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:
{
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) { ... }
/* ... */
partition_start = 0;
status = _fx_partition_offset_calculate(media_ptr -> fx_media_driver_buffer, 0,
&partition_start, &partition_size);
if (status)
{
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
break;
}if (status) { ... }
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) { ... }
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) { ... }
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) { ... }
}{ ... }
/* ... */
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;
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++)
{
status = fx_stm32_sd_read_blocks(FX_STM32_SD_INSTANCE, (UINT *)scratch, (UINT)start_sector++, 1);
if (status != 0)
{
FX_STM32_SD_READ_TRANSFER_ERROR(status);
return FX_IO_ERROR;
}if (status != 0) { ... }
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++) { ... }
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)
{
FX_STM32_SD_READ_TRANSFER_ERROR(status);
return FX_IO_ERROR;
}if (status != 0) { ... }
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 { ... }
FX_STM32_SD_POST_READ_TRANSFER(media_ptr);
return status;
}{ ... }
/* ... */
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;
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_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)
{
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) { ... }
FX_STM32_SD_WRITE_CPLT_NOTIFY();
status = FX_SUCCESS;
}else { ... }
FX_STM32_SD_POST_WRITE_TRANSFER(media_ptr);
return status;
}{ ... }