1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
31
32
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
62
63
64
65
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
98
123
124
129
133
134
139
140
141
142
143
144
150
151
152
153
158
159
160
161
162
163
164
169
173
177
178
184
200
201
207
208
209
210
211
212
213
214
215
221
222
223
224
228
229
230
231
237
238
239
240
241
242
243
244
245
250
251
252
253
257
258
259
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
292
293
294
295
300
301
302
305
306
307
308
309
310
311
312
313
314
336
358
359
360
361
362
363
368
372
373
377
378
379
384
385
386
387
399
403
407
408
409
410
411
412
413
414
415
416
417
420
421
/* ... */
#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);
osThreadDef(STORAGE_Thread, StorageThread, osPriorityLow, 0, 64);
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);
}{ ... }
/* ... */
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 == 0)
{
fs = &USBDISK_FatFs;
tot_sect = (fs->n_fatent - 2) * fs->csize;
}if (unit == 0) { ... }
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) { ... }
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 = 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, ".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;
}{ ... }
/* ... */
/* ... */