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
159
160
165
166
168
169
170
171
172
173
178
179
180
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
232
233
234
235
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
311
312
313
314
315
316
317
318
319
320
321
322
323
332
333
334
335
336
337
338
339
340
346
351
352
353
357
358
359
360
361
362
363
364
368
372
373
374
375
376
381
382
387
388
389
390
391
392
396
400
401
402
403
408
409
414
415
416
417
418
419
420
421
426
427
428
429
430
431
432
433
437
438
439
443
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
484
485
486
487
488
489
490
491
492
495
496
497
498
499
500
501
502
503
506
507
508
509
510
511
512
513
514
517
518
519
520
521
522
523
524
525
528
529
530
531
536
537
538
539
540
541
542
543
544
545
546
547
548
570
571
572
577
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
616
620
621
622
627
633
634
635
636
637
638
639
640
641
642
652
653
654
657
658
/* ... */
#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 = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { ... }
if(HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}if (HAL_PWREx_EnableOverDrive() != 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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != 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-F429ZI ");
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_USER, 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;
NUCLEO_ADCx_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = NUCLEO_ADCx_GPIO_PIN ;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(NUCLEO_ADCx_GPIO_PORT , &GPIO_InitStruct);
if(HAL_GPIO_ReadPin(NUCLEO_ADCx_GPIO_PORT , NUCLEO_ADCx_GPIO_PIN) != 0)
{
return SHIELD_DETECTED;
}if (HAL_GPIO_ReadPin(NUCLEO_ADCx_GPIO_PORT , NUCLEO_ADCx_GPIO_PIN) != 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
/* ... */
/* ... */