1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
29
37
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
77
78
79
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
109
110
111
112
113
114
119
120
121
130
131
132
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
162
180
181
186
187
188
189
190
204
205
206
211
212
213
214
215
216
217
218
222
223
224
225
239
241
242
243
244
245
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
270
274
275
276
277
281
285
286
287
288
289
290
291
292
293
294
295
296
301
302
306
310
315
320
321
325
326
327
328
329
330
331
336
337
338
339
340
341
342
343
344
345
349
350
351
352
353
354
355
356
357
358
362
363
364
365
366
367
368
369
370
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
398
403
404
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
433
437
438
439
440
447
448
449
450
451
452
453
454
455
460
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
502
503
504
505
506
507
508
509
510
511
512
513
514
515
517
518
519
520
521
522
523
524
525
530
538
539
540
559
560
563
564
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
typedef enum
{
APPLICATION_IDLE = 0,
APPLICATION_START
...}
MSC_ApplicationTypeDef;
Private typedef
#define IMAGE_BUFFER_SIZE 512
#define USH_USR_FS_INIT 0
#define USH_USR_FS_READLIST 1
#define USH_USR_FS_WRITEFILE 2
#define USH_USR_FS_DRAW 3
5 defines
Private define
USBH_HandleTypeDef hUSBHost;
FATFS USBDISK_FatFs;
uint8_t USBDISK_Driver_Num;
FIL file;
uint8_t Image_Buf[IMAGE_BUFFER_SIZE];
uint32_t BytesRead = 0;
MSC_ApplicationTypeDef Appli_state = APPLICATION_IDLE;
uint8_t USBH_USR_ApplicationState = USH_USR_FS_INIT;
__IO uint8_t line_idx = 0;
Private variables
static void LCD_Config(void);
static void MSC_Application(void);
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
static uint8_t Explore_Disk(char *path , uint8_t recu_level);
static uint8_t Image_Browser(char *path);
static void Show_Image(void);
static uint32_t Storage_OpenReadFile(uint32_t Address);
static void Toggle_Leds(void);
static void SystemClock_Config(void);
static void Error_Handler(void);
Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED3);
BSP_LED_Init(LED4);
BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
LCD_Config();
USBDISK_Driver_Num = FATFS_LinkDriver(&USBH_Driver, "");
if (USBH_Init(&hUSBHost, USBH_UserProcess, 0) != USBH_OK)
{
Error_Handler();
}if (USBH_Init(&hUSBHost, USBH_UserProcess, 0) != USBH_OK) { ... }
USBH_RegisterClass(&hUSBHost, USBH_MSC_CLASS);
if (USBH_Start(&hUSBHost) != USBH_OK)
{
Error_Handler();
}if (USBH_Start(&hUSBHost) != USBH_OK) { ... }
while (1)
{
if (Appli_state == APPLICATION_START)
{
MSC_Application();
}if (Appli_state == APPLICATION_START) { ... }
Toggle_Leds();
USBH_Process(&hUSBHost);
}while (1) { ... }
}{ ... }
/* ... */
static void LCD_Config(void)
{
BSP_LCD_Init();
BSP_LCD_LayerDefaultInit(LCD_FOREGROUND_LAYER, (LCD_FRAME_BUFFER + BUFFER_OFFSET));
BSP_LCD_SetTransparency(LCD_BACKGROUND_LAYER, 0);
BSP_LCD_SelectLayer(LCD_FOREGROUND_LAYER);
LCD_LOG_Init();
LCD_LOG_SetHeader((uint8_t *)"LTDC Application");
LCD_UsrLog("> USB Host library started.\n");
LCD_LOG_SetFooter ((uint8_t *)" USB Host Library V3.2.0" );
}{ ... }
/* ... */
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id)
{
switch (id)
{
case HOST_USER_DISCONNECTION:
Appli_state = APPLICATION_IDLE;
if (f_mount(&USBDISK_FatFs, "", 0) != FR_OK)
{
Error_Handler();
}if (f_mount(&USBDISK_FatFs, "", 0) != FR_OK) { ... }
break;
case HOST_USER_DISCONNECTION:
case HOST_USER_CLASS_ACTIVE:
Appli_state = APPLICATION_START;
break;case HOST_USER_CLASS_ACTIVE:
}switch (id) { ... }
}{ ... }
/* ... */
static void MSC_Application(void)
{
switch(USBH_USR_ApplicationState)
{
case USH_USR_FS_INIT:
if (f_mount(&USBDISK_FatFs, "", 0) != FR_OK)
{
LCD_ErrLog("> File System NOT initialized.\n");
Error_Handler();
}if (f_mount(&USBDISK_FatFs, "", 0) != FR_OK) { ... }
else
{
LCD_UsrLog("> File System initialized.\n");
USBH_USR_ApplicationState = USH_USR_FS_READLIST;
}else { ... }
break;
case USH_USR_FS_INIT:
case USH_USR_FS_READLIST:
LCD_UsrLog("> Exploring disk flash ...\n");
if (Explore_Disk("0:/", 1) != FR_OK)
{
LCD_ErrLog("> File cannot be explored.\n");
Error_Handler();
}if (Explore_Disk("0:/", 1) != FR_OK) { ... }
else
{
line_idx = 0;
USBH_USR_ApplicationState = USH_USR_FS_DRAW;
LCD_UsrLog("To start Image Slideshow\n");
LCD_UsrLog("Press Key\n");
while(BSP_PB_GetState (BUTTON_KEY) != SET)
{
Toggle_Leds();
}while (BSP_PB_GetState (BUTTON_KEY) != SET) { ... }
}else { ... }
break;
case USH_USR_FS_READLIST:
case USH_USR_FS_DRAW:
while(BSP_PB_GetState (BUTTON_KEY) != RESET)
{
Toggle_Leds();
}while (BSP_PB_GetState (BUTTON_KEY) != RESET) { ... }
while(Appli_state == APPLICATION_START)
{
Image_Browser("0:/");
return;
}while (Appli_state == APPLICATION_START) { ... }
break;
case USH_USR_FS_DRAW:
default:
break;default
}switch (USBH_USR_ApplicationState) { ... }
return;
}{ ... }
/* ... */
static uint8_t Explore_Disk(char* path , uint8_t recu_level)
{
FRESULT res;
FILINFO fno;
DIR dir;
char *fn;
char tmp[14];
res = f_opendir(&dir, path);
if (res == FR_OK)
{
LCD_UsrLog("To see the disk root content:\n" );
LCD_UsrLog("Press Key...\n");
while((BSP_PB_GetState (BUTTON_KEY) != SET))
{
Toggle_Leds();
}while ((BSP_PB_GetState (BUTTON_KEY) != SET)) { ... }
while((BSP_PB_GetState (BUTTON_KEY) != RESET))
{
Toggle_Leds();
}while ((BSP_PB_GetState (BUTTON_KEY) != RESET)) { ... }
while(Appli_state == APPLICATION_START)
{
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0)
{
break;
}if (res != FR_OK || fno.fname[0] == 0) { ... }
if (fno.fname[0] == '.')
{
continue;
}if (fno.fname[0] == '.') { ... }
fn = fno.fname;
strcpy(tmp, fn);
line_idx++;
if(line_idx > 12)
{
line_idx = 0;
LCD_UsrLog("Press Key to continue...\n");
while((Appli_state == APPLICATION_START) && \
(BSP_PB_GetState (BUTTON_KEY) != SET))
{
Toggle_Leds();
}while ((Appli_state == APPLICATION_START) && \ (BSP_PB_GetState (BUTTON_KEY) != SET)) { ... }
}if (line_idx > 12) { ... }
if(recu_level == 1)
{
LCD_DbgLog(" |__");
}if (recu_level == 1) { ... }
else if(recu_level == 2)
{
LCD_DbgLog(" | |__");
}else if (recu_level == 2) { ... }
if((fno.fattrib & AM_DIR) == AM_DIR)
{
strcat(tmp, "\n");
LCD_UsrLog((void *)tmp);
}if ((fno.fattrib & AM_DIR) == AM_DIR) { ... }
else
{
strcat(tmp, "\n");
LCD_DbgLog((void *)tmp);
}else { ... }
if(((fno.fattrib & AM_DIR) == AM_DIR)&&(recu_level == 1))
{
Explore_Disk(fn, 2);
}if (((fno.fattrib & AM_DIR) == AM_DIR)&&(recu_level == 1)) { ... }
}while (Appli_state == APPLICATION_START) { ... }
}if (res == FR_OK) { ... }
f_closedir(&dir);
return res;
}{ ... }
/* ... */
static uint8_t Image_Browser(char *path)
{
FRESULT res;
uint8_t ret = 1;
FILINFO fno;
DIR dir;
char *fn;
res = f_opendir(&dir, path);
if (res != FR_OK)
{
Error_Handler();
}if (res != FR_OK) { ... }
else
{
for (;;) {
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0) break;
if (fno.fname[0] == '.') continue;
fn = fno.fname;
if (fno.fattrib & AM_DIR)
{
continue;
}if (fno.fattrib & AM_DIR) { ... }
else
{
if((strstr(fn, "bmp")) || (strstr(fn, "BMP")))
{
res = f_open(&file, fn, FA_OPEN_EXISTING | FA_READ);
Show_Image();
USBH_Delay(100);
ret = 0;
while((Appli_state == APPLICATION_START) && \
(BSP_PB_GetState (BUTTON_KEY) != SET))
{
Toggle_Leds();
}while ((Appli_state == APPLICATION_START) && \ (BSP_PB_GetState (BUTTON_KEY) != SET)) { ... }
f_close(&file);
}if ((strstr(fn, "bmp")) || (strstr(fn, "BMP"))) { ... }
}else { ... }
}for (;;) { ... }
}else { ... }
LCD_LOG_Init();
LCD_LOG_SetHeader((uint8_t *)"LTDC Application");
LCD_LOG_SetFooter ((uint8_t *)" USB Host Library V3.2.0" );
USBH_USR_ApplicationState = USH_USR_FS_READLIST;
f_closedir(&dir);
return ret;
}{ ... }
/* ... */
static void Show_Image(void)
{
Storage_OpenReadFile(LCD_FRAME_BUFFER);
BSP_LCD_DrawBitmap(0, 0, (uint8_t*)LCD_FRAME_BUFFER);
}{ ... }
/* ... */
static uint32_t Storage_OpenReadFile(uint32_t Address)
{
uint32_t index = 0, size = 0, i1 = 0;
uint32_t BmpAddress;
f_read(&file, &Image_Buf, 30, 0);
BmpAddress = (uint32_t)Image_Buf;
size = *(uint16_t *) (BmpAddress + 2);
size |= (*(uint16_t *) (BmpAddress + 4)) << 16;
index = *(uint16_t *) (BmpAddress + 10);
index |= (*(uint16_t *) (BmpAddress + 12)) << 16;
f_lseek (&file, 0);
do
{
if (size < 256*2)
{
i1 = size;
}if (size < 256*2) { ... }
else
{
i1 = 256*2;
}else { ... }
size -= i1;
f_read(&file, &Image_Buf, i1, (UINT *)&BytesRead);
for (index = 0; index < i1; index++)
{
*(__IO uint8_t*) (Address) = *(__IO uint8_t *)BmpAddress;
BmpAddress++;
Address++;
}for (index = 0; index < i1; index++) { ... }
BmpAddress = (uint32_t)Image_Buf;
...}
while (size > 0);
return 1;
}{ ... }
/* ... */
static void Toggle_Leds(void)
{
static uint32_t i;
if (i++ == 0x10000)
{
BSP_LED_Toggle(LED3);
BSP_LED_Toggle(LED4);
i = 0;
}if (i++ == 0x10000) { ... }
}{ ... }
/* ... */
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
/* ... */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* ... */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}{ ... }
/* ... */
static void Error_Handler(void)
{
BSP_LED_On(LED4);
while(1)
{
}while (1) { ... }
}{ ... }
#ifdef USE_FULL_ASSERT
/* ... */
void assert_failed(uint8_t* file, uint32_t line)
{
/* ... */
while (1)
{
}while (1) { ... }
}assert_failed (uint8_t* file, uint32_t line) { ... }
/* ... */#endif
/* ... */
/* ... */