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
59
60
61
62
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
95
120
121
126
130
131
136
137
138
139
140
141
146
147
148
149
154
155
156
157
158
159
160
165
166
170
174
175
181
197
198
204
205
206
207
208
209
210
211
212
218
219
220
221
225
226
227
228
234
235
236
237
238
239
240
241
242
247
248
249
250
254
255
256
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
287
288
289
290
295
296
297
300
301
302
303
304
305
306
307
308
309
331
353
354
376
377
378
379
380
381
386
390
391
395
396
397
402
403
404
405
417
421
425
426
427
428
429
430
431
432
433
434
435
438
439
/* ... */
#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;
char USBDISK_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);
USBH_Init(&hUSB_Host, USBH_UserProcess, 0);
osThreadDef(STORAGE_Thread, StorageThread, osPriorityLow, 0, 64);
osThreadCreate (osThread(STORAGE_Thread), NULL);
osMessageQDef(osqueue, 10, uint16_t);
StorageEvent = osMessageCreate (osMessageQ(osqueue), NULL);
USBH_RegisterClass(&hUSB_Host, USBH_MSC_CLASS);
USBH_Start(&hUSB_Host);
}{ ... }
/* ... */
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:
}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 == USB_DISK_UNIT)
{
fs = &USBDISK_FatFs;
tot_sect = (fs->n_fatent - 2) * fs->csize;
}if (unit == USB_DISK_UNIT) { ... }
return (tot_sect);
}{ ... }
/* ... */
uint32_t k_StorageGetFree (uint8_t unit)
{
uint32_t fre_clust = 0;
FATFS *fs ;
FRESULT res = FR_INT_ERR;
if(unit == USB_DISK_UNIT)
{
fs = &USBDISK_FatFs;
res = f_getfree("0:", (DWORD *)&fre_clust, &fs);
}if (unit == USB_DISK_UNIT) { ... }
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) { ... }
}{ ... }
/* ... */
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;
char tmp[CHOOSEFILE_MAXLEN];
FRESULT res = FR_INT_ERR;
char *fn;
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)
{
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
{
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
{
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
{
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
{
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;
}{ ... }
/* ... */
/* ... */