Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
Private typedef
MSC_ApplicationTypeDef
Private define
#define IMAGE_BUFFER_SIZE
#define USH_USR_FS_INIT
#define USH_USR_FS_READLIST
#define USH_USR_FS_WRITEFILE
#define USH_USR_FS_DRAW
Private variables
hUSBHost
USBDISK_FatFs
USBDISK_Driver_Num
file
Image_Buf
BytesRead
Appli_state
USBH_USR_ApplicationState
line_idx
Private function prototypes
main()
LCD_Config()
USBH_UserProcess(USBH_HandleTypeDef *, uint8_t)
MSC_Application()
Explore_Disk(char *, uint8_t)
Image_Browser(char *)
Show_Image()
Storage_OpenReadFile(uint32_t)
Toggle_Leds()
SystemClock_Config()
Error_Handler()
Files
loading...
CodeScopeSTM32 Libraries and SamplesLTDC_AnimatedPictureFromUSBSrc/main.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file Display/LTDC_AnimatedPictureFromUSB/Src/main.c * @author MCD Application Team * @brief Main program body ****************************************************************************** * @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 "main.h" /** @addtogroup STM32F4xx_HAL_Applications * @{ *//* ... */ /** @addtogroup LTDC_AnimatedPictureFromUSB * @{ *//* ... */ Includes /* Private typedef -----------------------------------------------------------*/ typedef enum { APPLICATION_IDLE = 0, APPLICATION_START ...} MSC_ApplicationTypeDef; Private typedef /* Private define ------------------------------------------------------------*/ #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/* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ USBH_HandleTypeDef hUSBHost; FATFS USBDISK_FatFs; uint8_t USBDISK_Driver_Num; /* USB Host logical drive number */ 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 /* Private function prototypes -----------------------------------------------*/ 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 /* Private functions ---------------------------------------------------------*/ /** * @brief Main program * @param None * @retval None *//* ... */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization *//* ... */ HAL_Init(); /* Configure the system clock to 168 MHz */ SystemClock_Config(); /* Configure LED3 and LED4 */ BSP_LED_Init(LED3); BSP_LED_Init(LED4); /* Configure USER Button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* Initialize LCD driver */ LCD_Config(); /* Link the USB Host disk I/O driver */ USBDISK_Driver_Num = FATFS_LinkDriver(&USBH_Driver, ""); /* Init Host Library */ if (USBH_Init(&hUSBHost, USBH_UserProcess, 0) != USBH_OK) { /* USB Initialization Error */ Error_Handler(); }if (USBH_Init(&hUSBHost, USBH_UserProcess, 0) != USBH_OK) { ... } /* Add Supported Class */ USBH_RegisterClass(&hUSBHost, USBH_MSC_CLASS); /* Start Host Process */ if (USBH_Start(&hUSBHost) != USBH_OK) { /* USB Initialization Error */ Error_Handler(); }if (USBH_Start(&hUSBHost) != USBH_OK) { ... } /* Infinite loop */ while (1) { if (Appli_state == APPLICATION_START) { MSC_Application(); }if (Appli_state == APPLICATION_START) { ... } Toggle_Leds(); USBH_Process(&hUSBHost); }while (1) { ... } }{ ... } /** * @brief LCD configuration. * @param None * @retval None *//* ... */ static void LCD_Config(void) { /* LCD Initialization */ BSP_LCD_Init(); /* LCD Layers Initialization */ BSP_LCD_LayerDefaultInit(LCD_FOREGROUND_LAYER, (LCD_FRAME_BUFFER + BUFFER_OFFSET)); /* Configure the transparency for foreground : Increase the transparency */ BSP_LCD_SetTransparency(LCD_BACKGROUND_LAYER, 0); BSP_LCD_SelectLayer(LCD_FOREGROUND_LAYER); /* LCD Log initialization */ 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" ); }{ ... } /** * @brief User Process * @param None * @retval None *//* ... */ 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) { /* FatFs Initialization Error */ 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) { ... } }{ ... } /** * @brief MSC class application * @param None * @retval None *//* ... */ static void MSC_Application(void) { switch(USBH_USR_ApplicationState) { case USH_USR_FS_INIT: /* Register work area for logical drives */ 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: /* USER Button in polling */ 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; }{ ... } /** * @brief Displays disk content * @param path: pointer to root path * @param recu_level: explorer level * @retval None *//* ... */ 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) { /* USER Button in polling */ 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"); /* USER Button in polling */ 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; }{ ... } /** * @brief Image browser * @param path: pointer to root path * @retval None *//* ... */ 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 initialization */ 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; }{ ... } /** * @brief Displays Image * @param None * @retval None *//* ... */ static void Show_Image(void) { Storage_OpenReadFile(LCD_FRAME_BUFFER); BSP_LCD_DrawBitmap(0, 0, (uint8_t*)LCD_FRAME_BUFFER); }{ ... } /** * @brief Open a file and copy its content to a buffer * @param Address: the Directory name to open * @retval err: Error status (0=> success, 1=> fail) *//* ... */ 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; /* Read bitmap size */ size = *(uint16_t *) (BmpAddress + 2); size |= (*(uint16_t *) (BmpAddress + 4)) << 16; /* Get bitmap data address offset */ 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; }{ ... } /** * @brief Toggle LEDs to show user input state. * @param None * @retval None *//* ... */ 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) { ... } }{ ... } /** * @brief System Clock Configuration * The system Clock is configured as follow : * System Clock source = PLL (HSE) * SYSCLK(Hz) = 168000000 * HCLK(Hz) = 168000000 * AHB Prescaler = 1 * APB1 Prescaler = 4 * APB2 Prescaler = 2 * HSE Frequency(Hz) = 8000000 * PLL_M = 8 * PLL_N = 336 * PLL_P = 2 * PLL_Q = 7 * VDD(V) = 3.3 * Main regulator output voltage = Scale1 mode * Flash Latency(WS) = 5 * @param None * @retval None *//* ... */ static void SystemClock_Config(void) { RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; /* Enable Power Control clock */ __HAL_RCC_PWR_CLK_ENABLE(); /* The voltage scaling allows optimizing the power consumption when the device is clocked below the maximum system frequency, to update the voltage scaling value regarding system frequency refer to product datasheet. *//* ... */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /* Enable HSE Oscillator and activate PLL with HSE as source */ 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); /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers *//* ... */ 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); }{ ... } /** * @brief This function is executed in case of error occurrence. * @param None * @retval None *//* ... */ static void Error_Handler(void) { /* Turn LED4 on */ BSP_LED_On(LED4); while(1) { }while (1) { ... } }{ ... } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None *//* ... */ void assert_failed(uint8_t* file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* ... */ /* Infinite loop */ while (1) { }while (1) { ... } }assert_failed (uint8_t* file, uint32_t line) { ... } /* ... */#endif /** * @} *//* ... */ /** * @} *//* ... */