1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
31
37
43
44
51
52
53
54
55
56
57
58
59
60
61
62
63
68
69
70
76
77
78
79
80
81
85
86
87
88
89
90
91
92
93
94
95
96
100
101
102
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
138
139
140
141
142
143
144
145
146
147
148
149
150
154
155
157
158
159
160
161
162
166
167
168
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
220
221
222
223
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
299
300
301
302
303
304
305
306
307
308
309
310
311
316
317
318
319
320
321
322
323
324
329
334
335
336
340
341
342
343
344
345
346
347
351
352
353
354
355
356
357
358
363
368
369
370
371
372
373
374
378
379
380
381
382
383
384
389
394
395
396
397
398
399
400
401
402
407
408
409
410
411
412
413
414
415
416
417
421
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
462
463
464
465
466
467
468
469
470
473
474
475
476
477
478
479
480
481
484
485
486
487
488
489
490
491
492
495
496
497
498
503
504
505
506
507
508
509
510
511
512
513
514
515
537
538
539
544
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
581
585
586
587
592
598
599
600
601
602
603
604
605
606
607
617
618
619
622
623
/* ... */
#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
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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 0x10;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
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-F411RE ");
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++;
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)
{
Storage_OpenReadFile(127, 159, (const char*)str);
}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 = filesnumbers;
}if ((bmpcounter + 1) > filesnumbers) { ... }
else
{
bmpcounter++;
sprintf ((char*)str, "%-11.11s", pDirectoryFiles[bmpcounter - 1]);
checkstatus = Storage_CheckBitmapFile((const char*)str, &bmplen);
if(checkstatus == 0)
{
Storage_OpenReadFile(127, 159, (const char*)str);
}if (checkstatus == 0) { ... }
else
{
TFT_DisplayErrorMessage(SD_CARD_FILE_NOT_SUPPORTED);
}else { ... }
joystickstatus = JOY_NONE;
JoystickValue = 2;
}else { ... }
}if (joystickstatus == JOY_RIGHT) { ... }
else if(joystickstatus == JOY_LEFT )
{
if((bmpcounter - 1) <= 1)
{
bmpcounter = 1;
}if ((bmpcounter - 1) <= 1) { ... }
else
{
bmpcounter--;
sprintf ((char*)str, "%-11.11s", pDirectoryFiles[bmpcounter - 1]);
checkstatus = Storage_CheckBitmapFile((const char*)str, &bmplen);
if(checkstatus == 0)
{
Storage_OpenReadFile(127, 159, (const char*)str);
}if (checkstatus == 0) { ... }
else if (checkstatus == 1)
{
TFT_DisplayErrorMessage(SD_CARD_FILE_NOT_SUPPORTED);
}else if (checkstatus == 1) { ... }
}else { ... }
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(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) { ... }
}{ ... }
/* ... */
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_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) != 0)
{
return SHIELD_DETECTED;
}if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) != 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
/* ... */
/* ... */