1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
31
38
49
56
57
58
59
60
61
62
63
64
65
66
67
68
73
74
75
81
82
83
84
85
86
90
91
92
93
94
95
96
97
98
99
100
101
105
106
107
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
143
144
145
146
147
148
149
150
151
152
153
154
158
159
161
162
163
164
165
166
170
171
172
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
224
225
226
227
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
303
304
305
306
307
308
309
310
311
312
313
314
315
324
325
326
327
328
329
330
331
332
338
343
344
345
349
350
351
352
353
354
355
356
360
364
365
366
367
368
373
374
379
380
381
382
383
384
388
392
393
394
395
400
401
406
407
408
409
410
411
412
413
418
419
420
421
422
423
424
425
429
430
431
435
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
476
477
478
479
480
481
482
483
484
487
488
489
490
491
492
493
494
495
498
499
500
501
502
503
504
505
506
509
510
511
512
513
514
515
516
517
520
521
522
523
528
529
530
531
532
533
534
535
536
537
538
539
540
562
563
564
569
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
607
611
612
613
618
624
625
626
627
628
629
630
631
632
633
643
644
645
648
649
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
typedef enum
{
SHIELD_NOT_DETECTED = 0,
SHIELD_DETECTED
...}ShieldStatus;
Private typedef
#define SD_CARD_NOT_FORMATTED 0
#define SD_CARD_FILE_NOT_SUPPORTED 1
#define SD_CARD_OPEN_FAIL 2
#define FATFS_NOT_MOUNTED 3
#define BSP_SD_INIT_FAILED 4
#define POSITION_X_BITMAP 0
#define POSITION_Y_BITMAP 0
7 defines
Private define
uint8_t BlinkSpeed = 0, str[20];
__IO uint8_t JoystickValue = 0;
char* pDirectoryFiles[MAX_BMP_FILES];
FATFS SD_FatFs;
char SD_Path[4];
Private variables
static void SystemClock_Config(void);
static void Error_Handler(void);
static void LED2_Blink(void);
static ShieldStatus TFT_ShieldDetect(void);
static void SDCard_Config(void);
static void TFT_DisplayErrorMessage(uint8_t message);
static void TFT_DisplayMenu(void);
static void TFT_DisplayImages(void);
Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
SystemClock_Config();
/* ... */
if(TFT_ShieldDetect() == SHIELD_DETECTED)
{
BSP_LCD_Init();
SDCard_Config();
TFT_DisplayImages();
}if (TFT_ShieldDetect() == SHIELD_DETECTED) { ... }
else
{
LED2_Blink();
}else { ... }
while (1)
{
}while (1) { ... }
}{ ... }
/* ... */
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_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 400;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { ... }
/* ... */
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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) { ... }
}{ ... }
/* ... */
static void TFT_DisplayMenu(void)
{
JOYState_TypeDef tmp;
BSP_LCD_SetFont(&Font12);
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(1, (uint8_t*)" NUCLEO-F413ZH ");
BSP_LCD_DisplayStringAtLine(2, (uint8_t*)" DEMO ");
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayStringAtLine(4, (uint8_t*)"Display images ");
BSP_LCD_DisplayStringAtLine(6, (uint8_t*)"stored under uSD ");
BSP_LCD_DisplayStringAtLine(8, (uint8_t*)"on TFT LCD ");
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_DisplayStringAtLine(11, (uint8_t*)" Press JOY DOWN ");
BSP_LCD_DisplayStringAtLine(12, (uint8_t*)" to continue... ");
while (BSP_JOY_GetState() != JOY_DOWN)
{}while (BSP_JOY_GetState() != JOY_DOWN) { ... }
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_DisplayStringAtLine(4, (uint8_t*)" ");
BSP_LCD_DisplayStringAtLine(6, (uint8_t*)" Press Joystick ");
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayStringAtLine(8, (uint8_t*)" UP for: ");
BSP_LCD_DisplayStringAtLine(9, (uint8_t*)" Manual Mode ");
BSP_LCD_DisplayStringAtLine(11, (uint8_t*)" DOWN for: ");
BSP_LCD_DisplayStringAtLine(12, (uint8_t*)" Automatic Mode ");
while (BSP_JOY_GetState() == JOY_DOWN)
{}while (BSP_JOY_GetState() == JOY_DOWN) { ... }
tmp = JOY_RIGHT;
while ((tmp != JOY_DOWN) && (tmp != JOY_UP))
{
tmp = BSP_JOY_GetState();
}while ((tmp != JOY_DOWN) && (tmp != JOY_UP)) { ... }
BSP_LCD_Clear(LCD_COLOR_WHITE);
if(tmp == JOY_UP )
{
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(3, (uint8_t*)" Manual Mode ");
BSP_LCD_DisplayStringAtLine(5, (uint8_t*)" Selected ");
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
BSP_LCD_DisplayStringAtLine(9, (uint8_t*)"RIGHT: Next image");
BSP_LCD_DisplayStringAtLine(10, (uint8_t*)"LEFT : Previous ");
BSP_LCD_DisplayStringAtLine(11, (uint8_t*)"SEL : Switch to ");
BSP_LCD_DisplayStringAtLine(12, (uint8_t*)"automatic mode ");
JoystickValue = 2;
}if (tmp == JOY_UP) { ... }
else if (tmp == JOY_DOWN)
{
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAtLine(3, (uint8_t*)" Automatic Mode ");
BSP_LCD_DisplayStringAtLine(5, (uint8_t*)" Selected ");
JoystickValue = 1;
HAL_Delay(200);
}else if (tmp == JOY_DOWN) { ... }
}{ ... }
/* ... */
static void TFT_DisplayImages(void)
{
uint32_t bmplen = 0x00;
uint32_t checkstatus = 0x00;
uint32_t filesnumbers = 0x00;
uint32_t joystickstatus = JOY_NONE;
uint32_t bmpcounter = 0x00;
DIR directory;
FRESULT res;
BSP_JOY_Init();
TFT_DisplayMenu();
res = f_opendir(&directory, "/");
if((res != FR_OK))
{
if(res == FR_NO_FILESYSTEM)
{
TFT_DisplayErrorMessage(SD_CARD_NOT_FORMATTED);
}if (res == FR_NO_FILESYSTEM) { ... }
else
{
TFT_DisplayErrorMessage(SD_CARD_OPEN_FAIL);
}else { ... }
}if ((res != FR_OK)) { ... }
filesnumbers = Storage_GetDirectoryBitmapFiles ("/", pDirectoryFiles);
bmpcounter = 1;
while (1)
{
joystickstatus = BSP_JOY_GetState();
if(joystickstatus == JOY_SEL )
{
JoystickValue++;
if (JoystickValue > 2)
{
JoystickValue = 1;
}if (JoystickValue > 2) { ... }
joystickstatus = JOY_NONE;
}if (joystickstatus == JOY_SEL) { ... }
if(JoystickValue == 1)
{
sprintf((char*)str, "%-11.11s", pDirectoryFiles[bmpcounter -1]);
checkstatus = Storage_CheckBitmapFile((const char*)str, &bmplen);
if(checkstatus == 0)
{
checkstatus = Storage_OpenReadFile(POSITION_X_BITMAP, POSITION_Y_BITMAP, (const char*)str);
HAL_Delay(1500);
}if (checkstatus == 0) { ... }
else if (checkstatus == 1)
{
TFT_DisplayErrorMessage(SD_CARD_FILE_NOT_SUPPORTED);
}else if (checkstatus == 1) { ... }
bmpcounter++;
if(bmpcounter > filesnumbers)
{
bmpcounter = 1;
}if (bmpcounter > filesnumbers) { ... }
}if (JoystickValue == 1) { ... }
if(JoystickValue == 2)
{
if( joystickstatus == JOY_RIGHT )
{
if((bmpcounter + 1) > filesnumbers)
{
bmpcounter = 1;
}if ((bmpcounter + 1) > filesnumbers) { ... }
else
{
bmpcounter++;
}else { ... }
sprintf ((char*)str, "%-11.11s", pDirectoryFiles[bmpcounter - 1]);
checkstatus = Storage_CheckBitmapFile((const char*)str, &bmplen);
if(checkstatus == 0)
{
Storage_OpenReadFile(POSITION_X_BITMAP, POSITION_Y_BITMAP, (const char*)str);
}if (checkstatus == 0) { ... }
if(checkstatus == 1)
{
TFT_DisplayErrorMessage(SD_CARD_FILE_NOT_SUPPORTED);
}if (checkstatus == 1) { ... }
joystickstatus = JOY_NONE;
JoystickValue = 2;
}if (joystickstatus == JOY_RIGHT) { ... }
else if(joystickstatus == JOY_LEFT )
{
if((bmpcounter - 1) <= 1)
{
bmpcounter = 1;
}if ((bmpcounter - 1) <= 1) { ... }
else
{
bmpcounter--;
}else { ... }
sprintf ((char*)str, "%-11.11s", pDirectoryFiles[bmpcounter - 1]);
checkstatus = Storage_CheckBitmapFile((const char*)str, &bmplen);
if(checkstatus == 0)
{
Storage_OpenReadFile(POSITION_X_BITMAP, POSITION_Y_BITMAP, (const char*)str);
}if (checkstatus == 0) { ... }
if (checkstatus == 1)
{
TFT_DisplayErrorMessage(SD_CARD_FILE_NOT_SUPPORTED);
}if (checkstatus == 1) { ... }
joystickstatus = JOY_NONE;
JoystickValue = 2;
}else if (joystickstatus == JOY_LEFT) { ... }
}if (JoystickValue == 2) { ... }
}while (1) { ... }
}{ ... }
/* ... */
static void SDCard_Config(void)
{
uint32_t counter = 0;
if(FATFS_LinkDriver(&SD_Driver, SD_Path) == 0)
{
if(BSP_SD_Init() != MSD_OK)
{
TFT_DisplayErrorMessage(BSP_SD_INIT_FAILED);
}if (BSP_SD_Init() != MSD_OK) { ... }
if(f_mount(&SD_FatFs, (TCHAR const*)"/", 0) != FR_OK)
{
TFT_DisplayErrorMessage(FATFS_NOT_MOUNTED);
}if (f_mount(&SD_FatFs, (TCHAR const*)"/", 0) != FR_OK) { ... }
else
{
for (counter = 0; counter < MAX_BMP_FILES; counter++)
{
pDirectoryFiles[counter] = malloc(11);
}for (counter = 0; counter < MAX_BMP_FILES; counter++) { ... }
}else { ... }
}if (FATFS_LinkDriver(&SD_Driver, SD_Path) == 0) { ... }
}{ ... }
/* ... */
static void TFT_DisplayErrorMessage(uint8_t message)
{
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_SetFont(&Font12);
BSP_LCD_SetBackColor(LCD_COLOR_GREY);
BSP_LCD_SetTextColor(LCD_COLOR_RED);
if(message == SD_CARD_NOT_FORMATTED)
{
BSP_LCD_DisplayStringAtLine(5, (uint8_t*)" SD Card is not ");
BSP_LCD_DisplayStringAtLine(6, (uint8_t*)" FAT formatted. ");
BSP_LCD_DisplayStringAtLine(7, (uint8_t*)" Please Format the ");
BSP_LCD_DisplayStringAtLine(8, (uint8_t*)" microSD card. ");
while (1)
{
}while (1) { ... }
}if (message == SD_CARD_NOT_FORMATTED) { ... }
if(message == SD_CARD_FILE_NOT_SUPPORTED)
{
BSP_LCD_DisplayStringAtLine(5, (uint8_t*)" ");
BSP_LCD_DisplayStringAtLine(6, (uint8_t*)" File type is not ");
BSP_LCD_DisplayStringAtLine(7, (uint8_t*)" supported. ");
BSP_LCD_DisplayStringAtLine(8, (uint8_t*)" ");
while(1)
{
}while (1) { ... }
}if (message == SD_CARD_FILE_NOT_SUPPORTED) { ... }
if(message == SD_CARD_OPEN_FAIL)
{
BSP_LCD_DisplayStringAtLine(5, (uint8_t*)" ");
BSP_LCD_DisplayStringAtLine(6, (uint8_t*)" Open directory ");
BSP_LCD_DisplayStringAtLine(7, (uint8_t*)" fails. ");
BSP_LCD_DisplayStringAtLine(8, (uint8_t*)" ");
while(1)
{
}while (1) { ... }
}if (message == SD_CARD_OPEN_FAIL) { ... }
if(message == FATFS_NOT_MOUNTED)
{
BSP_LCD_DisplayStringAtLine(5, (uint8_t*)" ");
BSP_LCD_DisplayStringAtLine(6, (uint8_t*)" Cannot mount ");
BSP_LCD_DisplayStringAtLine(7, (uint8_t*)" FatFs on Drive. ");
BSP_LCD_DisplayStringAtLine(8, (uint8_t*)" ");
while (1)
{
}while (1) { ... }
}if (message == FATFS_NOT_MOUNTED) { ... }
if(message == BSP_SD_INIT_FAILED)
{
BSP_LCD_DisplayStringAtLine(5, (uint8_t*)" ");
BSP_LCD_DisplayStringAtLine(6, (uint8_t*)" SD Init ");
BSP_LCD_DisplayStringAtLine(7, (uint8_t*)" fails. ");
BSP_LCD_DisplayStringAtLine(8, (uint8_t*)" ");
while(1)
{
}while (1) { ... }
}if (message == BSP_SD_INIT_FAILED) { ... }
}{ ... }
/* ... */
static void LED2_Blink(void)
{
BSP_LED_Init(LED2);
BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);
BlinkSpeed = 0;
while(1)
{
if(BlinkSpeed == 0)
{
BSP_LED_Toggle(LED2);
HAL_Delay(500);
}if (BlinkSpeed == 0) { ... }
else if(BlinkSpeed == 1)
{
BSP_LED_Toggle(LED2);
HAL_Delay(100);
}else if (BlinkSpeed == 1) { ... }
else if(BlinkSpeed == 2)
{
BSP_LED_Toggle(LED2);
HAL_Delay(50);
}else if (BlinkSpeed == 2) { ... }
}while (1) { ... }
}{ ... }
/* ... */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(BlinkSpeed == 2)
{
BlinkSpeed = 0;
}if (BlinkSpeed == 2) { ... }
else
{
BlinkSpeed ++;
}else { ... }
}{ ... }
/* ... */
static ShieldStatus TFT_ShieldDetect(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_1) != 0)
{
return SHIELD_DETECTED;
}if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_1) != 0) { ... }
else
{
return SHIELD_NOT_DETECTED;
}else { ... }
}{ ... }
/* ... */
static void Error_Handler(void)
{
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
/* ... */
/* ... */