-
Notifications
You must be signed in to change notification settings - Fork 8
/
32f429_lcd.c
1626 lines (1428 loc) · 50.5 KB
/
32f429_lcd.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
******************************************************************************
* @file stm32f429i_discovery_lcd.c
* @author MCD Application Team
* @version V1.0.1
* @date 28-October-2013
* @brief This file includes the LCD driver for ILI9341 Liquid Crystal
* Display Modules of STM32F429I-DISCO kit (MB1075).
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "32f429_lcd.h"
#include "../Common/fonts.c"
/** @addtogroup Utilities
* @{
*/
/** @addtogroup STM32F4_DISCOVERY
* @{
*/
/** @addtogroup STM32F429I_DISCOVERY
* @{
*/
/** @defgroup STM32F429I_DISCOVERY_LCD
* @brief This file includes the LCD driver for (ILI9341)
* @{
*/
/** @defgroup STM32F429I_DISCOVERY_LCD_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup STM32F429I_DISCOVERY_LCD_Private_Defines
* @{
*/
#define POLY_Y(Z) ((int32_t)((Points + Z)->X))
#define POLY_X(Z) ((int32_t)((Points + Z)->Y))
/**
* @}
*/
/** @defgroup STM32F429I_DISCOVERY_LCD_Private_Macros
* @{
*/
#define ABS(X) ((X) > 0 ? (X) : -(X))
/**
* @}
*/
/** @defgroup STM32F429I_DISCOVERY_LCD_Private_Variables
* @{
*/
static sFONT *LCD_Currentfonts;
/* Global variables to set the written text color */
static uint16_t CurrentTextColor = 0x0000;
static uint16_t CurrentBackColor = 0xFFFF;
/* Default LCD configuration with LCD Layer 1 */
static uint32_t CurrentFrameBuffer = LCD_FRAME_BUFFER;
static uint32_t CurrentLayer = LCD_BACKGROUND_LAYER;
/**
* @}
*/
/** @defgroup STM32F429I_DISCOVERY_LCD_Private_FunctionPrototypes
* @{
*/
#ifndef USE_Delay
static void delay(__IO uint32_t nCount);
#endif /* USE_Delay*/
static void PutPixel(int16_t x, int16_t y);
static void LCD_PolyLineRelativeClosed(pPoint Points, uint16_t PointCount, uint16_t Closed);
static void LCD_AF_GPIOConfig(void);
/**
* @}
*/
/** @defgroup STM32F429I_DISCOVERY_LCD_Private_Functions
* @{
*/
/**
* @brief DeInitializes the LCD.
* @param None
* @retval None
*/
void LCD_DeInit(void)
{
}
/**
* @brief Initializes the LCD.
* @param None
* @retval None
*/
void LCD_Init(void)
{
LTDC_InitTypeDef LTDC_InitStruct;
/* Configure the LCD Control pins ------------------------------------------*/
// LCD_CtrlLinesConfig();
// LCD_ChipSelect(DISABLE);
// LCD_ChipSelect(ENABLE);
/* Enable the LTDC Clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_LTDC, ENABLE);
/* Enable the DMA2D Clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2D, ENABLE);
/* Configure the LCD Control pins */
LCD_AF_GPIOConfig();
/* Configure the FMC Parallel interface : SDRAM is used as Frame Buffer for LCD */
SDRAM_Init();
/* LTDC Configuration *********************************************************/
/* Polarity configuration */
/* Initialize the horizontal synchronization polarity as active low */
LTDC_InitStruct.LTDC_HSPolarity = LTDC_HSPolarity_AL;
/* Initialize the vertical synchronization polarity as active low */
LTDC_InitStruct.LTDC_VSPolarity = LTDC_VSPolarity_AL;
/* Initialize the data enable polarity as active low */
LTDC_InitStruct.LTDC_DEPolarity = LTDC_DEPolarity_AL;
/* Initialize the pixel clock polarity as input pixel clock */
LTDC_InitStruct.LTDC_PCPolarity = LTDC_PCPolarity_IPC;
/* Configure R,G,B component values for LCD background color */
LTDC_InitStruct.LTDC_BackgroundRedValue = 0;
LTDC_InitStruct.LTDC_BackgroundGreenValue = 0;
LTDC_InitStruct.LTDC_BackgroundBlueValue = 0;
/* Configure PLLSAI prescalers for LCD */
/* Enable Pixel Clock */
/* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
/* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAI_N = 192 Mhz */
/* PLLLCDCLK = PLLSAI_VCO Output/PLLSAI_R = 192/4 = 48 Mhz */
/* LTDC clock frequency = PLLLCDCLK / RCC_PLLSAIDivR = 48/8 = 6 Mhz */
RCC_PLLSAIConfig(192, 7, 4);
RCC_LTDCCLKDivConfig(RCC_PLLSAIDivR_Div2);
/* Enable PLLSAI Clock */
RCC_PLLSAICmd(ENABLE);
/* Wait for PLLSAI activation */
while(RCC_GetFlagStatus(RCC_FLAG_PLLSAIRDY) == RESET)
{
}
// /* Timing configuration */
// /* Configure horizontal synchronization width */
// LTDC_InitStruct.LTDC_HorizontalSync = 9;
// /* Configure vertical synchronization height */
// LTDC_InitStruct.LTDC_VerticalSync = 1;
// /* Configure accumulated horizontal back porch */
// LTDC_InitStruct.LTDC_AccumulatedHBP = 29;
// /* Configure accumulated vertical back porch */
// LTDC_InitStruct.LTDC_AccumulatedVBP = 3;
// /* Configure accumulated active width */
// LTDC_InitStruct.LTDC_AccumulatedActiveW = 269;
// /* Configure accumulated active height */
// LTDC_InitStruct.LTDC_AccumulatedActiveH = 323;
// /* Configure total width */
// LTDC_InitStruct.LTDC_TotalWidth = 279; //240
// /* Configure total height */
// LTDC_InitStruct.LTDC_TotalHeigh = 327; //320
//
// LTDC_Init(<DC_InitStruct);
/* Timing configuration */
/* Configure horizontal synchronization width */
LTDC_InitStruct.LTDC_HorizontalSync = 9;
/* Configure vertical synchronization height */
LTDC_InitStruct.LTDC_VerticalSync = 1;
/* Configure accumulated horizontal back porch */
LTDC_InitStruct.LTDC_AccumulatedHBP = 29;
/* Configure accumulated vertical back porch */
LTDC_InitStruct.LTDC_AccumulatedVBP = 3;
/* Configure accumulated active width */
LTDC_InitStruct.LTDC_AccumulatedActiveW = LCD_PIXEL_WIDTH+29;
/* Configure accumulated active height */
LTDC_InitStruct.LTDC_AccumulatedActiveH =LCD_PIXEL_HEIGHT+3;
/* Configure total width */
LTDC_InitStruct.LTDC_TotalWidth =LCD_PIXEL_WIDTH+39+200;
/* Configure total height */
LTDC_InitStruct.LTDC_TotalHeigh = LCD_PIXEL_HEIGHT+7+50;
LTDC_Init(<DC_InitStruct);
}
/**
* @brief Initializes the LCD Layers.
* @param None
* @retval None
*/
void LCD_LayerInit(void)
{
LTDC_Layer_InitTypeDef LTDC_Layer_InitStruct;
/* Windowing configuration */
/* In this case all the active display area is used to display a picture then :
Horizontal start = horizontal synchronization + Horizontal back porch = 30
Horizontal stop = Horizontal start + window width -1 = 30 + 240 -1
Vertical start = vertical synchronization + vertical back porch = 4
Vertical stop = Vertical start + window height -1 = 4 + 320 -1 */
LTDC_Layer_InitStruct.LTDC_HorizontalStart = 30;
LTDC_Layer_InitStruct.LTDC_HorizontalStop = (LCD_PIXEL_WIDTH + 30 - 1);
LTDC_Layer_InitStruct.LTDC_VerticalStart = 4;
LTDC_Layer_InitStruct.LTDC_VerticalStop = (LCD_PIXEL_HEIGHT + 4 - 1);
/* Pixel Format configuration*/
LTDC_Layer_InitStruct.LTDC_PixelFormat = LTDC_Pixelformat_RGB565;
/* Alpha constant (255 totally opaque) */
LTDC_Layer_InitStruct.LTDC_ConstantAlpha = 255;
/* Default Color configuration (configure A,R,G,B component values) */
LTDC_Layer_InitStruct.LTDC_DefaultColorBlue = 0;
LTDC_Layer_InitStruct.LTDC_DefaultColorGreen = 0;
LTDC_Layer_InitStruct.LTDC_DefaultColorRed = 0;
LTDC_Layer_InitStruct.LTDC_DefaultColorAlpha = 0;
/* Configure blending factors */
// LTDC_Layer_InitStruct.LTDC_BlendingFactor_1 = LTDC_BlendingFactor1_CA;
// LTDC_Layer_InitStruct.LTDC_BlendingFactor_2 = LTDC_BlendingFactor2_CA;
LTDC_Layer_InitStruct.LTDC_BlendingFactor_1 = LTDC_BlendingFactor1_PAxCA;
LTDC_Layer_InitStruct.LTDC_BlendingFactor_2 = LTDC_BlendingFactor2_PAxCA;
/* the length of one line of pixels in bytes + 3 then :
Line Lenth = Active high width x number of bytes per pixel + 3
Active high width = LCD_PIXEL_WIDTH
number of bytes per pixel = 2 (pixel_format : RGB565)
*/
LTDC_Layer_InitStruct.LTDC_CFBLineLength = ((LCD_PIXEL_WIDTH * 2) + 3);
/* the pitch is the increment from the start of one line of pixels to the
start of the next line in bytes, then :
Pitch = Active high width x number of bytes per pixel */
LTDC_Layer_InitStruct.LTDC_CFBPitch = (LCD_PIXEL_WIDTH * 2);
/* Configure the number of lines */
LTDC_Layer_InitStruct.LTDC_CFBLineNumber = LCD_PIXEL_HEIGHT;
/* Start Address configuration : the LCD Frame buffer is defined on SDRAM */
LTDC_Layer_InitStruct.LTDC_CFBStartAdress = LCD_FRAME_BUFFER;
/* Initialize LTDC layer 1 */
LTDC_LayerInit(LTDC_Layer1, <DC_Layer_InitStruct);
/* Configure Layer2 */
/* Start Address configuration : the LCD Frame buffer is defined on SDRAM w/ Offset */
LTDC_Layer_InitStruct.LTDC_CFBStartAdress = LCD_FRAME_BUFFER + BUFFER_OFFSET;
LTDC_Layer_InitStruct.LTDC_HorizontalStart = LCD_LAYER2_X0 + 30;
LTDC_Layer_InitStruct.LTDC_HorizontalStop = (LCD_LAYER2_X0 + LCD_LAYER2_PIXEL_WIDTH + 30 - 1);
LTDC_Layer_InitStruct.LTDC_VerticalStart = LCD_LAYER2_Y0 + 4;
LTDC_Layer_InitStruct.LTDC_VerticalStop = (LCD_LAYER2_Y0 + LCD_LAYER2_PIXEL_HEIGHT + 4 - 1);
LTDC_Layer_InitStruct.LTDC_CFBLineLength = ((LCD_LAYER2_PIXEL_WIDTH * 2) + 3);
LTDC_Layer_InitStruct.LTDC_CFBPitch = (LCD_LAYER2_PIXEL_WIDTH * 2);
LTDC_Layer_InitStruct.LTDC_CFBLineNumber = LCD_LAYER2_PIXEL_HEIGHT;
/* Configure blending factors */
LTDC_Layer_InitStruct.LTDC_BlendingFactor_1 = LTDC_BlendingFactor1_PAxCA;
LTDC_Layer_InitStruct.LTDC_BlendingFactor_2 = LTDC_BlendingFactor2_PAxCA;
/* Initialize LTDC layer 2 */
LTDC_LayerInit(LTDC_Layer2, <DC_Layer_InitStruct);
/* LTDC configuration reload */
LTDC_ReloadConfig(LTDC_IMReload);
/* Enable foreground & background Layers */
LTDC_LayerCmd(LTDC_Layer1, ENABLE);
LTDC_LayerCmd(LTDC_Layer2, ENABLE);
/* LTDC configuration reload */
LTDC_ReloadConfig(LTDC_IMReload);
/* Set default font */
LCD_SetFont(&LCD_DEFAULT_FONT);
/* dithering activation */
LTDC_DitherCmd(ENABLE);
}
/**
* @brief Controls LCD Chip Select (CS) pin
* @param NewState CS pin state
* @retval None
*/
void LCD_ChipSelect(FunctionalState NewState)
{
if (NewState == DISABLE)
{
GPIO_ResetBits(LCD_NCS_GPIO_PORT, LCD_NCS_PIN); /* CS pin low: LCD disabled */
}
else
{
GPIO_SetBits(LCD_NCS_GPIO_PORT, LCD_NCS_PIN); /* CS pin high: LCD enabled */
}
}
/**
* @brief Sets the LCD Layer.
* @param Layerx: specifies the Layer foreground or background.
* @retval None
*/
void LCD_SetLayer(uint32_t Layerx)
{
if (Layerx == LCD_BACKGROUND_LAYER)
{
CurrentFrameBuffer = LCD_FRAME_BUFFER;
CurrentLayer = LCD_BACKGROUND_LAYER;
}
else
{
CurrentFrameBuffer = LCD_FRAME_BUFFER + BUFFER_OFFSET;
CurrentLayer = LCD_FOREGROUND_LAYER;
}
}
/**
* @brief Sets the LCD Text and Background colors.
* @param TextColor: specifies the Text Color.
* @param BackColor: specifies the Background Color.
* @retval None
*/
void LCD_SetColors(uint16_t TextColor, uint16_t BackColor)
{
CurrentTextColor = TextColor;
CurrentBackColor = BackColor;
}
/**
* @brief Gets the LCD Text and Background colors.
* @param TextColor: pointer to the variable that will contain the Text
Color.
* @param BackColor: pointer to the variable that will contain the Background
Color.
* @retval None
*/
void LCD_GetColors(uint16_t *TextColor, uint16_t *BackColor)
{
*TextColor = CurrentTextColor;
*BackColor = CurrentBackColor;
}
/**
* @brief Sets the Text color.
* @param Color: specifies the Text color code RGB(5-6-5).
* @retval None
*/
void LCD_SetTextColor(uint16_t Color)
{
CurrentTextColor = Color;
}
/**
* @brief Sets the Background color.
* @param Color: specifies the Background color code RGB(5-6-5).
* @retval None
*/
void LCD_SetBackColor(uint16_t Color)
{
CurrentBackColor = Color;
}
/**
* @brief Sets the Text Font.
* @param fonts: specifies the font to be used.
* @retval None
*/
void LCD_SetFont(sFONT *fonts)
{
LCD_Currentfonts = fonts;
}
/**
* @brief Configure the transparency.
* @param transparency: specifies the transparency,
* This parameter must range from 0x00 to 0xFF.
* @retval None
*/
void LCD_SetTransparency(uint8_t transparency)
{
if (CurrentLayer == LCD_BACKGROUND_LAYER)
{
LTDC_LayerAlpha(LTDC_Layer1, transparency);
}
else
{
LTDC_LayerAlpha(LTDC_Layer2, transparency);
}
LTDC_ReloadConfig(LTDC_IMReload);
}
/**
* @brief Gets the Text Font.
* @param None.
* @retval the used font.
*/
sFONT *LCD_GetFont(void)
{
return LCD_Currentfonts;
}
/**
* @brief Clears the selected line.
* @param Line: the Line to be cleared.
* This parameter can be one of the following values:
* @arg LCD_LINE_x: where x can be: 0..13 if LCD_Currentfonts is Font16x24
* 0..26 if LCD_Currentfonts is Font12x12 or Font8x12
* 0..39 if LCD_Currentfonts is Font8x8
* @retval None
*/
void LCD_ClearLine(uint16_t Line)
{
uint16_t refcolumn = 0;
/* Send the string character by character on lCD */
while ((refcolumn < LCD_PIXEL_WIDTH) && (((refcolumn + LCD_Currentfonts->Width)& 0xFFFF) >= LCD_Currentfonts->Width))
{
/* Display one character on LCD */
LCD_DisplayChar(Line, refcolumn, ' ');
/* Decrement the column position by 16 */
refcolumn += LCD_Currentfonts->Width;
}
}
/**
* @brief Clears the hole LCD.
* @param Color: the color of the background.
* @retval None
*/
void LCD_Clear(uint16_t Color)
{
uint32_t index = 0;
// for (index = (uint16_t *)CurrentFrameBuffer; index < (uint16_t *)(CurrentFrameBuffer+(BUFFER_OFFSET)); index++)
// {
// *index = Color;
// }
/* erase memory */
for (index = 0x00; index < BUFFER_OFFSET; index++)
{
*(__IO uint16_t*)(CurrentFrameBuffer + (2*index)) = Color;
//delay(100000);
}
}
/**
* @brief Sets the cursor position.
* @param Xpos: specifies the X position.
* @param Ypos: specifies the Y position.
* @retval Display Address
*/
uint32_t LCD_SetCursor(uint16_t Xpos, uint16_t Ypos)
{
return CurrentFrameBuffer + 2*(Xpos + (LCD_PIXEL_WIDTH*Ypos));
}
/**
* @brief Config and Sets the color Keying.
* @param RGBValue: Specifies the Color reference.
* @retval None
*/
void LCD_SetColorKeying(uint32_t RGBValue)
{
LTDC_ColorKeying_InitTypeDef LTDC_colorkeying_InitStruct;
/* configure the color Keying */
LTDC_colorkeying_InitStruct.LTDC_ColorKeyBlue = 0x0000FF & RGBValue;
LTDC_colorkeying_InitStruct.LTDC_ColorKeyGreen = (0x00FF00 & RGBValue) >> 8;
LTDC_colorkeying_InitStruct.LTDC_ColorKeyRed = (0xFF0000 & RGBValue) >> 16;
if (CurrentLayer == LCD_BACKGROUND_LAYER)
{
/* Enable the color Keying for Layer1 */
LTDC_ColorKeyingConfig(LTDC_Layer1, <DC_colorkeying_InitStruct, ENABLE);
LTDC_ReloadConfig(LTDC_IMReload);
}
else
{
/* Enable the color Keying for Layer2 */
LTDC_ColorKeyingConfig(LTDC_Layer2, <DC_colorkeying_InitStruct, ENABLE);
LTDC_ReloadConfig(LTDC_IMReload);
}
}
/**
* @brief Disable the color Keying.
* @param RGBValue: Specifies the Color reference.
* @retval None
*/
void LCD_ReSetColorKeying(void)
{
LTDC_ColorKeying_InitTypeDef LTDC_colorkeying_InitStruct;
if (CurrentLayer == LCD_BACKGROUND_LAYER)
{
/* Disable the color Keying for Layer1 */
LTDC_ColorKeyingConfig(LTDC_Layer1, <DC_colorkeying_InitStruct, DISABLE);
LTDC_ReloadConfig(LTDC_IMReload);
}
else
{
/* Disable the color Keying for Layer2 */
LTDC_ColorKeyingConfig(LTDC_Layer2, <DC_colorkeying_InitStruct, DISABLE);
LTDC_ReloadConfig(LTDC_IMReload);
}
}
/**
* @brief Draws a character on LCD.
* @param Xpos: the Line where to display the character shape.
* @param Ypos: start column address.
* @param c: pointer to the character data.
* @retval None
*/
void LCD_DrawChar(uint16_t Xpos, uint16_t Ypos, const uint16_t *c)
{
uint32_t index = 0, counter = 0, xpos =0;
uint32_t Xaddress = 0;
xpos = Xpos*LCD_PIXEL_WIDTH*2;
Xaddress += Ypos;
for(index = 0; index < LCD_Currentfonts->Height; index++)
{
for(counter = 0; counter < LCD_Currentfonts->Width; counter++)
{
if((((c[index] & ((0x80 << ((LCD_Currentfonts->Width / 12 ) * 8 ) ) >> counter)) == 0x00) &&(LCD_Currentfonts->Width <= 12))||
(((c[index] & (0x1 << counter)) == 0x00)&&(LCD_Currentfonts->Width > 12 )))
{
/* Write data value to all SDRAM memory */
*(__IO uint16_t*) (CurrentFrameBuffer + (2*Xaddress) + xpos) = CurrentBackColor;
}
else
{
/* Write data value to all SDRAM memory */
*(__IO uint16_t*) (CurrentFrameBuffer + (2*Xaddress) + xpos) = CurrentTextColor;
}
Xaddress++;
}
Xaddress += (LCD_PIXEL_WIDTH - LCD_Currentfonts->Width);
}
}
/**
* @brief Displays one character (16dots width, 24dots height).
* @param Line: the Line where to display the character shape .
* This parameter can be one of the following values:
* @arg Linex: where x can be 0..29
* @param Column: start column address.
* @param Ascii: character ascii code, must be between 0x20 and 0x7E.
* @retval None
*/
void LCD_DisplayChar(uint16_t Line, uint16_t Column, uint8_t Ascii)
{
Ascii -= 32;
LCD_DrawChar(Line, Column, &LCD_Currentfonts->table[Ascii * LCD_Currentfonts->Height]);
}
/**
* @brief Displays a maximum of 20 char on the LCD.
* @param Line: the Line where to display the character shape .
* This parameter can be one of the following values:
* @arg Linex: where x can be 0..9
* @param *ptr: pointer to string to display on LCD.
* @retval None
*/
void LCD_DisplayStringLine(uint16_t Line, uint8_t *ptr)
{
uint16_t refcolumn = 0;
/* Send the string character by character on lCD */
while ((refcolumn < LCD_PIXEL_WIDTH) && ((*ptr != 0) & (((refcolumn + LCD_Currentfonts->Width) & 0xFFFF) >= LCD_Currentfonts->Width)))
{
/* Display one character on LCD */
LCD_DisplayChar(Line, refcolumn, *ptr);
/* Decrement the column position by width */
refcolumn += LCD_Currentfonts->Width;
/* Point on the next character */
ptr++;
}
}
/**
* @brief Sets a display window
* @param Xpos: specifies the X bottom left position from 0 to 240.
* @param Ypos: specifies the Y bottom left position from 0 to 320.
* @param Height: display window height, can be a value from 0 to 320.
* @param Width: display window width, can be a value from 0 to 240.
* @retval None
*/
void LCD_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Height, uint16_t Width)
{
if (CurrentLayer == LCD_BACKGROUND_LAYER)
{
/* reconfigure the layer1 position */
LTDC_LayerPosition(LTDC_Layer1, Xpos, Ypos);
LTDC_ReloadConfig(LTDC_IMReload);
/* reconfigure the layer1 size */
LTDC_LayerSize(LTDC_Layer1, Width, Height);
LTDC_ReloadConfig(LTDC_IMReload);
}
else
{
/* reconfigure the layer2 position */
LTDC_LayerPosition(LTDC_Layer2, Xpos, Ypos);
LTDC_ReloadConfig(LTDC_IMReload);
/* reconfigure the layer2 size */
LTDC_LayerSize(LTDC_Layer2, Width, Height);
LTDC_ReloadConfig(LTDC_IMReload);
}
}
/**
* @brief Disables LCD Window mode.
* @param None
* @retval None
*/
void LCD_WindowModeDisable(void)
{
LCD_SetDisplayWindow(0, 0, LCD_PIXEL_HEIGHT, LCD_PIXEL_WIDTH);
}
/**
* @brief Displays a line.
* @param Xpos: specifies the X position, can be a value from 0 to 480.
* @param Ypos: specifies the Y position, can be a value from 0 to 272.
* @param Length: line length.
* @param Direction: line direction.
* This parameter can be one of the following values: LCD_DIR_HORIZONTAL or LCD_DIR_VERTICAL.
* @retval None
*/
void LCD_DrawLine(uint16_t Xpos, uint16_t Ypos, uint16_t Length, uint8_t Direction)
{
DMA2D_InitTypeDef DMA2D_InitStruct;
uint32_t Xaddress = 0;
uint16_t Red_Value = 0, Green_Value = 0, Blue_Value = 0;
Xaddress = CurrentFrameBuffer + 2*(LCD_PIXEL_WIDTH*Ypos + Xpos);
Red_Value = (0xF800 & CurrentTextColor) >> 11;
Blue_Value = 0x001F & CurrentTextColor;
Green_Value = (0x07E0 & CurrentTextColor) >> 5;
/* Configure DMA2D */
DMA2D_DeInit();
DMA2D_InitStruct.DMA2D_Mode = DMA2D_R2M;
DMA2D_InitStruct.DMA2D_CMode = DMA2D_RGB565;
DMA2D_InitStruct.DMA2D_OutputGreen = Green_Value;
DMA2D_InitStruct.DMA2D_OutputBlue = Blue_Value;
DMA2D_InitStruct.DMA2D_OutputRed = Red_Value;
DMA2D_InitStruct.DMA2D_OutputAlpha = 0x0F;
DMA2D_InitStruct.DMA2D_OutputMemoryAdd = Xaddress;
if(Direction == LCD_DIR_HORIZONTAL)
{
DMA2D_InitStruct.DMA2D_OutputOffset = 0;
DMA2D_InitStruct.DMA2D_NumberOfLine = 1;
DMA2D_InitStruct.DMA2D_PixelPerLine = Length;
}
else
{
DMA2D_InitStruct.DMA2D_OutputOffset = LCD_PIXEL_WIDTH - 1;
DMA2D_InitStruct.DMA2D_NumberOfLine = Length;
DMA2D_InitStruct.DMA2D_PixelPerLine = 1;
}
DMA2D_Init(&DMA2D_InitStruct);
/* Start Transfer */
DMA2D_StartTransfer();
/* Wait for CTC Flag activation */
while(DMA2D_GetFlagStatus(DMA2D_FLAG_TC) == RESET)
{
}
}
/**
* @brief Displays a rectangle.
* @param Xpos: specifies the X position, can be a value from 0 to 240.
* @param Ypos: specifies the Y position, can be a value from 0 to 320.
* @param Height: display rectangle height, can be a value from 0 to 320.
* @param Width: display rectangle width, can be a value from 0 to 240.
* @retval None
*/
void LCD_DrawRect(uint16_t Xpos, uint16_t Ypos, uint16_t Height, uint16_t Width)
{
/* draw horizontal lines */
LCD_DrawLine(Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
LCD_DrawLine(Xpos, (Ypos+ Height), Width, LCD_DIR_HORIZONTAL);
/* draw vertical lines */
LCD_DrawLine(Xpos, Ypos, Height, LCD_DIR_VERTICAL);
LCD_DrawLine((Xpos + Width), Ypos, Height, LCD_DIR_VERTICAL);
}
/**
* @brief Draw a circle.
* @param Xpos: specifies the X position, can be a value from 0 to 240.
* @param Ypos: specifies the Y position, can be a value from 0 to 320.
* @param Radius: radius of the circle.
* @retval None
*/
void LCD_DrawCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius)
{
int x = -Radius, y = 0, err = 2-2*Radius, e2;
do {
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos-x) + LCD_PIXEL_WIDTH*(Ypos+y)))) = CurrentTextColor;
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos+x) + LCD_PIXEL_WIDTH*(Ypos+y)))) = CurrentTextColor;
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos+x) + LCD_PIXEL_WIDTH*(Ypos-y)))) = CurrentTextColor;
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos-x) + LCD_PIXEL_WIDTH*(Ypos-y)))) = CurrentTextColor;
e2 = err;
if (e2 <= y) {
err += ++y*2+1;
if (-x == y && e2 <= x) e2 = 0;
}
if (e2 > x) err += ++x*2+1;
}
while (x <= 0);
}
/**
* @brief Draw a full ellipse.
* @param Xpos: specifies the X position, can be a value from 0 to 240.
* @param Ypos: specifies the Y position, can be a value from 0 to 320.
* @param Radius: minor radius of ellipse.
* @param Radius2: major radius of ellipse.
* @retval None
*/
void LCD_DrawFullEllipse(int Xpos, int Ypos, int Radius, int Radius2)
{
int x = -Radius, y = 0, err = 2-2*Radius, e2;
float K = 0, rad1 = 0, rad2 = 0;
rad1 = Radius;
rad2 = Radius2;
if (Radius > Radius2)
{
do
{
K = (float)(rad1/rad2);
LCD_DrawLine((Xpos+x), (Ypos-(uint16_t)(y/K)), (2*(uint16_t)(y/K) + 1), LCD_DIR_VERTICAL);
LCD_DrawLine((Xpos-x), (Ypos-(uint16_t)(y/K)), (2*(uint16_t)(y/K) + 1), LCD_DIR_VERTICAL);
e2 = err;
if (e2 <= y)
{
err += ++y*2+1;
if (-x == y && e2 <= x) e2 = 0;
}
if (e2 > x) err += ++x*2+1;
}
while (x <= 0);
}
else
{
y = -Radius2;
x = 0;
do
{
K = (float)(rad2/rad1);
LCD_DrawLine((Xpos-(uint16_t)(x/K)), (Ypos+y), (2*(uint16_t)(x/K) + 1), LCD_DIR_HORIZONTAL);
LCD_DrawLine((Xpos-(uint16_t)(x/K)), (Ypos-y), (2*(uint16_t)(x/K) + 1), LCD_DIR_HORIZONTAL);
e2 = err;
if (e2 <= x)
{
err += ++x*2+1;
if (-y == x && e2 <= y) e2 = 0;
}
if (e2 > y) err += ++y*2+1;
}
while (y <= 0);
}
}
/**
* @brief Displays an Ellipse.
* @param Xpos: specifies the X position, can be a value from 0 to 240.
* @param Ypos: specifies the Y position, can be a value from 0 to 320.
* @param Radius: specifies Radius.
* @param Radius2: specifies Radius2.
* @retval None
*/
void LCD_DrawEllipse(int Xpos, int Ypos, int Radius, int Radius2)
{
int x = -Radius, y = 0, err = 2-2*Radius, e2;
float K = 0, rad1 = 0, rad2 = 0;
rad1 = Radius;
rad2 = Radius2;
if (Radius > Radius2)
{
do {
K = (float)(rad1/rad2);
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos-x) + LCD_PIXEL_WIDTH*(Ypos+(uint16_t)(y/K))))) = CurrentTextColor;
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos+x) + LCD_PIXEL_WIDTH*(Ypos+(uint16_t)(y/K))))) = CurrentTextColor;
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos+x) + LCD_PIXEL_WIDTH*(Ypos-(uint16_t)(y/K))))) = CurrentTextColor;
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos-x) + LCD_PIXEL_WIDTH*(Ypos-(uint16_t)(y/K))))) = CurrentTextColor;
e2 = err;
if (e2 <= y) {
err += ++y*2+1;
if (-x == y && e2 <= x) e2 = 0;
}
if (e2 > x) err += ++x*2+1;
}
while (x <= 0);
}
else
{
y = -Radius2;
x = 0;
do {
K = (float)(rad2/rad1);
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos-(uint16_t)(x/K)) + LCD_PIXEL_WIDTH*(Ypos+y)))) = CurrentTextColor;
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos+(uint16_t)(x/K)) + LCD_PIXEL_WIDTH*(Ypos+y)))) = CurrentTextColor;
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos+(uint16_t)(x/K)) + LCD_PIXEL_WIDTH*(Ypos-y)))) = CurrentTextColor;
*(__IO uint16_t*) (CurrentFrameBuffer + (2*((Xpos-(uint16_t)(x/K)) + LCD_PIXEL_WIDTH*(Ypos-y)))) = CurrentTextColor;
e2 = err;
if (e2 <= x) {
err += ++x*2+1;
if (-y == x && e2 <= y) e2 = 0;
}
if (e2 > y) err += ++y*2+1;
}
while (y <= 0);
}
}
/**
* @brief Displays a mono-color picture.
* @param Pict: pointer to the picture array.
* @retval None
*/
void LCD_DrawMonoPict(const uint32_t *Pict)
{
uint32_t index = 0, counter = 0;
for(index = 0; index < 2400; index++)
{
for(counter = 0; counter < 32; counter++)
{
if((Pict[index] & (1 << counter)) == 0x00)
{
*(__IO uint16_t*)(CurrentFrameBuffer) = CurrentBackColor;
}
else
{
*(__IO uint16_t*)(CurrentFrameBuffer) = CurrentTextColor;
}
}
}
}
/**
* @brief Displays a bitmap picture loaded in the internal Flash.
* @param BmpAddress: Bmp picture address in the internal Flash.
* @retval None
*/
void LCD_WriteBMP(uint32_t BmpAddress)
{
uint32_t index = 0, size = 0, width = 0, height = 0, bit_pixel = 0;
uint32_t Address;
uint32_t currentline = 0, linenumber = 0;
Address = CurrentFrameBuffer;
/* Read bitmap size */
size = *(__IO uint16_t *) (BmpAddress + 2);
size |= (*(__IO uint16_t *) (BmpAddress + 4)) << 16;
/* Get bitmap data address offset */
index = *(__IO uint16_t *) (BmpAddress + 10);
index |= (*(__IO uint16_t *) (BmpAddress + 12)) << 16;
/* Read bitmap width */
width = *(uint16_t *) (BmpAddress + 18);
width |= (*(uint16_t *) (BmpAddress + 20)) << 16;
/* Read bitmap height */
height = *(uint16_t *) (BmpAddress + 22);
height |= (*(uint16_t *) (BmpAddress + 24)) << 16;
/* Read bit/pixel */
bit_pixel = *(uint16_t *) (BmpAddress + 28);
if (CurrentLayer == LCD_BACKGROUND_LAYER)
{
/* reconfigure layer size in accordance with the picture */
LTDC_LayerSize(LTDC_Layer1, width, height);
LTDC_ReloadConfig(LTDC_VBReload);
/* Reconfigure the Layer pixel format in accordance with the picture */
if ((bit_pixel/8) == 4)
{
LTDC_LayerPixelFormat(LTDC_Layer1, LTDC_Pixelformat_ARGB8888);
LTDC_ReloadConfig(LTDC_VBReload);
}
else if ((bit_pixel/8) == 2)
{
LTDC_LayerPixelFormat(LTDC_Layer1, LTDC_Pixelformat_RGB565);
LTDC_ReloadConfig(LTDC_VBReload);
}
else
{
LTDC_LayerPixelFormat(LTDC_Layer1, LTDC_Pixelformat_RGB888);
LTDC_ReloadConfig(LTDC_VBReload);
}
}
else
{
/* reconfigure layer size in accordance with the picture */
LTDC_LayerSize(LTDC_Layer2, width, height);
LTDC_ReloadConfig(LTDC_VBReload);
/* Reconfigure the Layer pixel format in accordance with the picture */
if ((bit_pixel/8) == 4)
{
LTDC_LayerPixelFormat(LTDC_Layer2, LTDC_Pixelformat_ARGB8888);
LTDC_ReloadConfig(LTDC_VBReload);
}
else if ((bit_pixel/8) == 2)
{
LTDC_LayerPixelFormat(LTDC_Layer2, LTDC_Pixelformat_RGB565);
LTDC_ReloadConfig(LTDC_VBReload);
}
else
{
LTDC_LayerPixelFormat(LTDC_Layer2, LTDC_Pixelformat_RGB888);
LTDC_ReloadConfig(LTDC_VBReload);
}
}
/* compute the real size of the picture (without the header)) */
size = (size - index);
/* bypass the bitmap header */
BmpAddress += index;
/* start copie image from the bottom */
Address += width*(height-1)*(bit_pixel/8);
for(index = 0; index < size; index++)
{
*(__IO uint8_t*) (Address) = *(__IO uint8_t *)BmpAddress;
/*jump on next byte */
BmpAddress++;
Address++;
currentline++;
if((currentline/(bit_pixel/8)) == width)
{
if(linenumber < height)
{
linenumber++;
Address -=(2*width*(bit_pixel/8));
currentline = 0;
}