Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "k_storage.h"
Private typedef
_aAttrib
Private variables
USBDISK_FatFs
USBDISK_Drive
hUSB_Host
StorageEvent
dir
acAttrib
acExt
StorageStatus
Private function prototypes
k_StorageInit()
StorageThread(const void *)
k_StorageGetStatus(uint8_t)
k_StorageGetCapacity(uint8_t)
k_StorageGetFree(uint8_t)
USBH_UserProcess(USBH_HandleTypeDef *, uint8_t)
GetExt(char *, char *)
k_GetExtOnly(char *, char *)
k_GetData(CHOOSEFILE_INFO *)
Files
loading...
CodeScopeSTM32 Libraries and SamplesSTemWinCore/Src/k_storage.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file k_storage.c * @author MCD Application Team * @brief This file provides the kernel storage functions ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "k_storage.h" /** @addtogroup CORE * @{ *//* ... */ /** @defgroup KERNEL_STORAGE * @brief Kernel storage routines * @{ *//* ... */ Includes /* External variables --------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/ static struct { U32 Mask; char c; ...} _aAttrib[] = { { AM_RDO, 'R' }, { AM_HID, 'H' }, { AM_SYS, 'S' }, { AM_DIR, 'D' }, { AM_ARC, 'A' }, ...};Private typedef /* Private defines -----------------------------------------------------------*/ /* Private macros ------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ FATFS USBDISK_FatFs; /* File system object for USB disk logical drive */ char USBDISK_Drive[4]; /* USB Host logical drive number */ 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 /* Private function prototypes -----------------------------------------------*/ 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 /* Private functions ---------------------------------------------------------*/ /** * @brief Storage drives initialization * @param None * @retval None *//* ... */ void k_StorageInit(void) { /* Link the USB Host disk I/O driver */ FATFS_LinkDriver(&USBH_Driver, USBDISK_Drive); /* Init Host Library */ USBH_Init(&hUSB_Host, USBH_UserProcess, 0); /* Create USB background task */ osThreadDef(STORAGE_Thread, StorageThread, osPriorityLow, 0, 64); osThreadCreate (osThread(STORAGE_Thread), NULL); /* Create Storage Message Queue */ osMessageQDef(osqueue, 10, uint16_t); StorageEvent = osMessageCreate (osMessageQ(osqueue), NULL); /* Add Supported Class */ USBH_RegisterClass(&hUSB_Host, USBH_MSC_CLASS); /* Start Host Process */ USBH_Start(&hUSB_Host); }{ ... } /** * @brief Storage Thread * @param argument: pointer that is passed to the thread function as start argument. * @retval None *//* ... */ 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 (;;) { ... } }{ ... } /** * @brief Storage get status * @param unit: logical storage unit index. * @retval int *//* ... */ uint8_t k_StorageGetStatus (uint8_t unit) { return StorageStatus[unit]; }{ ... } /** * @brief Storage get capacity * @param unit: logical storage unit index. * @retval int *//* ... */ 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); }{ ... } /** * @brief Storage get free space * @param unit: logical storage unit index. * @retval int *//* ... */ 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 { ... } }{ ... } /** * @brief User Process * @param phost: Host handle * @param id: Host Library user message ID * @retval None *//* ... */ 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) { ... } }{ ... } /** * @brief Return file extension and removed from file name. * @param pFile: pointer to the file name. * @param pExt: pointer to the file extension * @retval None *//* ... */ static void GetExt(char * pFile, char * pExt) { int Len; int i; int j; /* Search beginning of extension */ Len = strlen(pFile); for (i = Len; i > 0; i--) { if (*(pFile + i) == '.') { *(pFile + i) = '\0'; /* Cut extension from filename */ break; }if (*(pFile + i) == '.') { ... } }for (i = Len; i > 0; i--) { ... } /* Copy extension */ j = 0; while (*(pFile + ++i) != '\0') { *(pExt + j) = *(pFile + i); j++; }while (*(pFile + ++i) != '\0') { ... } *(pExt + j) = '\0'; /* Set end of string */ }{ ... } /** * @brief Return the extension Only * @param pFile: pointer to the file name. * @param pExt: pointer to the file extension * @retval None *//* ... */ void k_GetExtOnly(char * pFile, char * pExt) { int Len; int i; int j; /* Search beginning of extension */ Len = strlen(pFile); for (i = Len; i > 0; i--) { if (*(pFile + i) == '.') { break; }if (*(pFile + i) == '.') { ... } }for (i = Len; i > 0; i--) { ... } /* Copy extension */ j = 0; while (*(pFile + ++i) != '\0') { *(pExt + j) = *(pFile + i); j++; }while (*(pFile + ++i) != '\0') { ... } *(pExt + j) = '\0'; /* Set end of string */ }{ ... } /** * @brief This function is responsible to pass information about the requested file * @param pInfo: Pointer to structure which contains all details of the requested file. * @retval None *//* ... */ 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); /* reformat path */ 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; }{ ... } /** * @} *//* ... */ /** * @} *//* ... */