1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
31
32
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
61
62
63
64
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
102
103
104
109
147
148
153
157
158
163
164
165
166
167
168
174
180
181
182
183
188
189
190
191
192
193
194
199
204
208
212
213
219
235
236
241
252
253
259
260
261
262
263
264
265
266
267
273
274
275
276
280
281
282
283
289
290
291
292
293
294
295
296
297
302
303
304
305
309
310
311
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
344
345
346
347
352
353
354
357
358
359
360
361
362
363
364
365
366
388
410
411
433
434
435
436
437
438
443
447
448
452
453
454
459
460
461
462
474
478
482
483
484
485
486
487
488
489
490
491
492
495
496
/* ... */
#include "k_storage.h"
/* ... */
/* ... */
Includes
static struct {
U32 Mask;
char c;
...} _aAttrib[] = {
{ AM_RDO, 'R' },
{ AM_HID, 'H' },
{ AM_SYS, 'S' },
{ AM_DIR, 'D' },
{ AM_ARC, 'A' },
...};Private typedef
FATFS USBDISK_FatFs;
FATFS mSDDISK_FatFs;
char USBDISK_Drive[4];
char mSDDISK_Drive[4];
USBH_HandleTypeDef hUSB_Host;
osMessageQId StorageEvent;
DIR dir;
static char acAttrib[10];
static char acExt[FILEMGR_MAX_EXT_SIZE];
static uint32_t StorageStatus[NUM_DISK_UNITS];Private variables
static void StorageThread(void const * argument);
static void USBH_UserProcess (USBH_HandleTypeDef *phost, uint8_t id);
static void GetExt(char * pFile, char * pExt);Private function prototypes
/* ... */
void k_StorageInit(void)
{
FATFS_LinkDriver(&USBH_Driver, USBDISK_Drive);
FATFS_LinkDriver(&SD_Driver, mSDDISK_Drive);
osThreadDef(STORAGE_Thread, StorageThread, osPriorityBelowNormal, 0, 256);
osThreadCreate (osThread(STORAGE_Thread), NULL);
osMessageQDef(osqueue, 10, uint16_t);
StorageEvent = osMessageCreate (osMessageQ(osqueue), NULL);
USBH_Init(&hUSB_Host, USBH_UserProcess, 0);
USBH_RegisterClass(&hUSB_Host, USBH_MSC_CLASS);
USBH_Start(&hUSB_Host);
BSP_SD_Init();
BSP_SD_ITConfig();
if(BSP_SD_IsDetected())
{
osMessagePut ( StorageEvent, MSDDISK_CONNECTION_EVENT, 0);
}if (BSP_SD_IsDetected()) { ... }
}{ ... }
/* ... */
static void StorageThread(void const * argument)
{
osEvent event;
for( ;; )
{
event = osMessageGet( StorageEvent, osWaitForever );
if( event.status == osEventMessage )
{
switch(event.value.v)
{
case USBDISK_CONNECTION_EVENT:
f_mount(&USBDISK_FatFs,USBDISK_Drive, 0);
StorageStatus[USB_DISK_UNIT] = 1;
break;
case USBDISK_CONNECTION_EVENT:
case USBDISK_DISCONNECTION_EVENT:
f_mount(0, USBDISK_Drive, 0);
StorageStatus[USB_DISK_UNIT] = 0;
break;
case USBDISK_DISCONNECTION_EVENT:
case MSDDISK_CONNECTION_EVENT:
BSP_SD_Init();
f_mount(&mSDDISK_FatFs, mSDDISK_Drive, 0);
StorageStatus[MSD_DISK_UNIT] = 1;
break;
case MSDDISK_CONNECTION_EVENT:
case MSDDISK_DISCONNECTION_EVENT:
f_mount(0, mSDDISK_Drive, 0);
StorageStatus[MSD_DISK_UNIT] = 0;
break;
case MSDDISK_DISCONNECTION_EVENT:
}switch (event.value.v) { ... }
}if (event.status == osEventMessage) { ... }
}for (;;) { ... }
}{ ... }
/* ... */
uint8_t k_StorageGetStatus (uint8_t unit)
{
return StorageStatus[unit];
}{ ... }
/* ... */
uint32_t k_StorageGetCapacity (uint8_t unit)
{
uint32_t tot_sect = 0;
FATFS *fs;
if(unit == 0)
{
fs = &USBDISK_FatFs;
tot_sect = (fs->n_fatent - 2) * fs->csize;
}if (unit == 0) { ... }
else if (unit == 1)
{
fs = &mSDDISK_FatFs;
tot_sect = (fs->n_fatent - 2) * fs->csize;
}else if (unit == 1) { ... }
return (tot_sect);
}{ ... }
/* ... */
uint32_t k_StorageGetFree (uint8_t unit)
{
uint32_t fre_clust = 0;
FATFS *fs;
FRESULT res = FR_INT_ERR;
if(unit == 0)
{
fs = &USBDISK_FatFs;
res = f_getfree("0:", (DWORD *)&fre_clust, &fs);
}if (unit == 0) { ... }
else if (unit == 1)
{
fs = &mSDDISK_FatFs;
res = f_getfree("1:", (DWORD *)&fre_clust, &fs);
}else if (unit == 1) { ... }
if(res == FR_OK)
{
return (fre_clust * fs->csize);
}if (res == FR_OK) { ... }
else
{
return 0;
}else { ... }
}{ ... }
/* ... */
static void USBH_UserProcess (USBH_HandleTypeDef *phost, uint8_t id)
{
switch (id)
{
case HOST_USER_SELECT_CONFIGURATION:
break;
case HOST_USER_SELECT_CONFIGURATION:
case HOST_USER_DISCONNECTION:
osMessagePut ( StorageEvent, USBDISK_DISCONNECTION_EVENT, 0);
break;
case HOST_USER_DISCONNECTION:
case HOST_USER_CLASS_ACTIVE:
osMessagePut ( StorageEvent, USBDISK_CONNECTION_EVENT, 0);
break;case HOST_USER_CLASS_ACTIVE:
}switch (id) { ... }
}{ ... }
/* ... */
void BSP_SD_DetectCallback(void)
{
if((BSP_SD_IsDetected()))
{
osMessagePut ( StorageEvent, MSDDISK_CONNECTION_EVENT, 0);
}if ((BSP_SD_IsDetected())) { ... }
else
{
osMessagePut ( StorageEvent, MSDDISK_DISCONNECTION_EVENT, 0);
}else { ... }
}{ ... }
/* ... */
static void GetExt(char * pFile, char * pExt)
{
int Len;
int i;
int j;
Len = strlen(pFile);
for (i = Len; i > 0; i--) {
if (*(pFile + i) == '.') {
*(pFile + i) = '\0';
break;
}if (*(pFile + i) == '.') { ... }
}for (i = Len; i > 0; i--) { ... }
j = 0;
while (*(pFile + ++i) != '\0') {
*(pExt + j) = *(pFile + i);
j++;
}while (*(pFile + ++i) != '\0') { ... }
*(pExt + j) = '\0';
}{ ... }
/* ... */
void k_GetExtOnly(char * pFile, char * pExt)
{
int Len;
int i;
int j;
Len = strlen(pFile);
for (i = Len; i > 0; i--) {
if (*(pFile + i) == '.') {
break;
}if (*(pFile + i) == '.') { ... }
}for (i = Len; i > 0; i--) { ... }
j = 0;
while (*(pFile + ++i) != '\0') {
*(pExt + j) = *(pFile + i);
j++;
}while (*(pFile + ++i) != '\0') { ... }
*(pExt + j) = '\0';
}{ ... }
/* ... */
int k_GetData(CHOOSEFILE_INFO * pInfo)
{
char c;
int i = 0;
char tmp[CHOOSEFILE_MAXLEN];
static char fn[CHOOSEFILE_MAXLEN];
FRESULT res = FR_INT_ERR;
FILINFO fno;
switch (pInfo->Cmd)
{
case CHOOSEFILE_FINDFIRST:
f_closedir(&dir);
memset(tmp, 0, CHOOSEFILE_MAXLEN);
strcpy(tmp, pInfo->pRoot);
for(i= CHOOSEFILE_MAXLEN; i > 0; i--)
{
if(tmp[i] == '/')
{
tmp[i] = 0;
break;
}if (tmp[i] == '/') { ... }
}for (i= CHOOSEFILE_MAXLEN; i > 0; i--) { ... }
res = f_opendir(&dir, tmp);
if (res == FR_OK)
{
res = f_readdir(&dir, &fno);
}if (res == FR_OK) { ... }
break;
case CHOOSEFILE_FINDFIRST:
case CHOOSEFILE_FINDNEXT:
res = f_readdir(&dir, &fno);
break;case CHOOSEFILE_FINDNEXT:
}switch (pInfo->Cmd) { ... }
if (res == FR_OK)
{
strcpy(fn, fno.fname);
while (((fno.fattrib & AM_DIR) == 0) && (res == FR_OK))
{
if((strstr(pInfo->pMask, ".img")))
{
if((strstr(fn, ".bmp")) || (strstr(fn, ".jpg")) || (strstr(fn, ".BMP")) || (strstr(fn, ".JPG")))
{
break;
}if ((strstr(fn, ".bmp")) || (strstr(fn, ".jpg")) || (strstr(fn, ".BMP")) || (strstr(fn, ".JPG"))) { ... }
else
{
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0)
{
f_closedir(&dir);
return 1;
}if (res != FR_OK || fno.fname[0] == 0) { ... }
else
{
strcpy(fn, fno.fname);
}else { ... }
}else { ... }
}if ((strstr(pInfo->pMask, ".img"))) { ... }
else if((strstr(pInfo->pMask, ".audio")))
{
if((strstr(fn, ".wav")) || (strstr(fn, ".WAV")))
{
break;
}if ((strstr(fn, ".wav")) || (strstr(fn, ".WAV"))) { ... }
else
{
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0)
{
f_closedir(&dir);
return 1;
}if (res != FR_OK || fno.fname[0] == 0) { ... }
else
{
strcpy(fn, fno.fname);
}else { ... }
}else { ... }
}else if ((strstr(pInfo->pMask, ".audio"))) { ... }
else if((strstr(pInfo->pMask, ".video")))
{
if((strstr(fn, ".emf")) || (strstr(fn, ".EMF")))
{
break;
}if ((strstr(fn, ".emf")) || (strstr(fn, ".EMF"))) { ... }
else
{
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0)
{
f_closedir(&dir);
return 1;
}if (res != FR_OK || fno.fname[0] == 0) { ... }
else
{
strcpy(fn, fno.fname);
}else { ... }
}else { ... }
}else if ((strstr(pInfo->pMask, ".video"))) { ... }
else if(strstr(fn, pInfo->pMask) == NULL)
{
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0)
{
f_closedir(&dir);
return 1;
}if (res != FR_OK || fno.fname[0] == 0) { ... }
else
{
strcpy(fn, fno.fname);
}else { ... }
}else if (strstr(fn, pInfo->pMask) == NULL) { ... }
else
{
break;
}else { ... }
}while (((fno.fattrib & AM_DIR) == 0) && (res == FR_OK)) { ... }
if(fn[0] == 0)
{
f_closedir(&dir);
return 1;
}if (fn[0] == 0) { ... }
pInfo->Flags = ((fno.fattrib & AM_DIR) == AM_DIR) ? CHOOSEFILE_FLAG_DIRECTORY : 0;
for (i = 0; i < GUI_COUNTOF(_aAttrib); i++)
{
if (fno.fattrib & _aAttrib[i].Mask)
{
c = _aAttrib[i].c;
}if (fno.fattrib & _aAttrib[i].Mask) { ... }
else
{
c = '-';
}else { ... }
acAttrib[i] = c;
}for (i = 0; i < GUI_COUNTOF(_aAttrib); i++) { ... }
if((fno.fattrib & AM_DIR) == AM_DIR)
{
acExt[0] = 0;
}if ((fno.fattrib & AM_DIR) == AM_DIR) { ... }
else
{
GetExt(fn, acExt);
}else { ... }
pInfo->pAttrib = acAttrib;
pInfo->pName = fn;
pInfo->pExt = acExt;
pInfo->SizeL = fno.fsize;
pInfo->SizeH = 0;
}if (res == FR_OK) { ... }
return res;
}{ ... }
/* ... */
/* ... */