- 1.MCU GD32F470及其液晶RGB接口
STM32F429 支持LCD RGB硬件接口,但是其存在供货 价格问题,暂时不选,选择国产替代方案。
国产MCU中支持LCD RGB 硬件接口的有GD32F450 GD32F470.根据GD FAE最新消息450不在新推客户,推GD32F470. 2.不同的分辨率涉及不同RAM需求,如内置RAM不足,需要外扩。 LCD.h
#ifndef __LCD_H
#define __LCD_H
#include "sys.h"
#include "stdlib.h"
#include "delay.h"
typedef struct
{
uint16_t width;
uint16_t height;
uint16_t id;
uint8_t dir;
uint16_t wramcmd;
uint16_t setxcmd;
uint16_t setycmd;
}_lcd_dev;
extern _lcd_dev lcddev;
#define DEFAULT_POINT_COLOR YELLOW;
#define DEFAULT_BACK_COLOR BLACK;
extern uint32_t PointColor;
extern uint32_t BackColor;
typedef struct
{
vuint16_t LCD_REG;
vuint16_t LCD_RAM;
} LCD_TypeDef;
#define LCD_BASE ((uint32_t)(0x60000000 | 0x0007FFFE))
#define LCD ((LCD_TypeDef *) LCD_BASE)
#define L2R_U2D 0
#define L2R_D2U 1
#define R2L_U2D 2
#define R2L_D2U 3
#define U2D_L2R 4
#define U2D_R2L 5
#define D2U_L2R 6
#define D2U_R2L 7
#define DFT_SCAN_DIR L2R_U2D
#define WHITE 0x0000FFFF
#define BLACK 0x00000000
#define BLUE 0x0000001F
#define RED 0x0000F800
#define GREEN 0x000007E0
#define YELLOW 0x0000FFE0
#define BROWN 0X0000BC40
#define BRRED 0X0000FC07
#define GRAY 0X00008430
#define LEFT 0
#define RIGHT 1
void LCD_Init(void);
void LCD_DisplayOn(void);
void LCD_DisplayOff(void);
void LCD_BlackLightOn(void);
void LCD_BlackLightOff(void);
void LCD_Clear(uint32_t Color);
void LCD_DrawPoint(uint16_t x,uint16_t y);
void LCD_Fast_DrawPoint(uint16_t x,uint16_t y,uint32_t color);
uint32_t LCD_ReadPoint(uint16_t x,uint16_t y);
void LCD_Draw_Circle(uint16_t x0,uint16_t y0,uint8_t r);
void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
void LCD_DrawCheckBox(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2);
void LCD_FillCheckBox(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint32_t color);
void LCD_DrawTriangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t x,uint16_t y);
void LCD_DrawTriangleRE(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t x,uint16_t y);
void LCD_DrawHistogram(uint16_t x,uint16_t y,uint16_t w);
void LCD_FillHistogram(uint16_t x,uint16_t y,uint16_t w,uint16_t value,uint16_t range);
void LCD_DrawHistogramLimit(uint16_t x,uint16_t y,uint16_t w,uint16_t limit,uint16_t range);
void LCD_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint32_t color);
void LCD_Color_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t *color);
void LCD_ShowChar(uint16_t x,uint16_t y,char num,uint8_t size,uint8_t mode);
void LCD_ShowString(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint8_t size,volatile char *p);
void Show_Font(uint16_t x,uint16_t y,uint8_t size,const volatile char *p, uint8_t mode);
void LCD_ShowPicture(uint16_t x,uint16_t y,uint16_t width,uint16_t heigth, const uint8_t *data,uint8_t mode);
void LCD_ShowText(uint16_t x,uint16_t y,uint16_t width,uint16_t height,volatile char*str,uint8_t size,uint8_t mode);
char* FloatAsc2Right(float value, uint8_t dot);
char* FloatAsc2Left(float value, uint8_t dot);
void LCD_WriteReg(uint16_t LCD_Reg, uint16_t LCD_RegValue);
uint16_t LCD_ReadReg(uint16_t LCD_Reg);
void LCD_WriteRAM_Prepare(void);
void LCD_WriteRAM(uint16_t RGB_Code);
void LCD_Display_Dir(uint8_t dir);
void LCD_Set_Window(uint16_t sx,uint16_t sy,uint16_t width,uint16_t height);
#define LCD_HOR_RESOLUTION 640
#define LCD_VER_RESOLUTION 480
#define LCD_HOR_PULSE_WIDTH 1
#define LCD_HOR_BACK_PORCH 144
#define LCD_HOR_FRONT_PORCH 16
#define LCD_VER_PULSE_WIDTH 1
#define LCD_VER_BACK_PORCH 35
#define LCD_VER_FRONT_PORCH 10
#define LCD_HT (LCD_HOR_RESOLUTION+LCD_HOR_BACK_PORCH+LCD_HOR_FRONT_PORCH)
#define LCD_HPS (LCD_HOR_BACK_PORCH)
#define LCD_VT (LCD_VER_RESOLUTION+LCD_VER_BACK_PORCH+LCD_VER_FRONT_PORCH)
#define LCD_VPS (LCD_VER_BACK_PORCH)
#endif
LCD.c
#include "lcd.h"
#include "stdlib.h"
#include "font.h"
#include "usart.h"
#include "delay.h"
#include "ltdc.h"
uint32_t PointColor=DEFAULT_POINT_COLOR;
uint32_t BackColor =DEFAULT_BACK_COLOR;
_lcd_dev lcddev;
void LCD_WR_REG(vuint16_t regval)
{
regval=regval;
LCD->LCD_REG=regval;
}
void LCD_WR_DATA(vuint16_t data)
{
data=data;
LCD->LCD_RAM=data;
}
uint16_t LCD_RD_DATA(void)
{
vuint16_t ram;
ram=LCD->LCD_RAM;
return ram;
}
void LCD_WriteReg(uint16_t LCD_Reg,uint16_t LCD_RegValue)
{
LCD->LCD_REG = LCD_Reg;
LCD->LCD_RAM = LCD_RegValue;
}
uint16_t LCD_ReadReg(uint16_t LCD_Reg)
{
LCD_WR_REG(LCD_Reg);
delay_us(5);
return LCD_RD_DATA();
}
void LCD_WriteRAM_Prepare(void)
{
LCD->LCD_REG=lcddev.wramcmd;
}
void LCD_WriteRAM(uint16_t RGB_Code)
{
LCD->LCD_RAM = RGB_Code;
}
void opt_delay(uint8_t i)
{
while(i--);
}
uint32_t LCD_ReadPoint(uint16_t x,uint16_t y)
{
if(x>=lcddev.width||y>=lcddev.height)return 0;
return LTDC_Read_Point(x,y);
}
void LCD_DisplayOn(void)
{
LTDC_Switch(1);
}
void LCD_DisplayOff(void)
{
LTDC_Switch(0);
}
void LCD_BlackLightOn(void)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET);
}
void LCD_BlackLightOff(void)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_RESET);
}
void LCD_DrawPoint(uint16_t x,uint16_t y)
{
LTDC_Draw_Point(x,y,PointColor);
}
void LCD_Fast_DrawPoint(uint16_t x,uint16_t y,uint32_t color)
{
LTDC_Draw_Point(x,y,color);
return;
}
void LCD_Display_Dir(uint8_t dir)
{
LTDC_Display_Dir(dir);
return;
}
void LCD_Init(void)
{
LTDC_Init();
}
void LCD_Clear(uint32_t color)
{
LTDC_Clear(color);
delay_ms(5);
}
void LCD_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint32_t color)
{
LTDC_Fill(sx,sy,ex,ey,color);
delay_ms(5);
}
void LCD_Color_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t *color)
{
LTDC_Color_Fill(sx,sy,ex,ey,color);
}
void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
uint16_t t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
delta_x=x2-x1;
delta_y=y2-y1;
uRow=x1;
uCol=y1;
if(delta_x>0)incx=1;
else if(delta_x==0)incx=0;
else {incx=-1;delta_x=-delta_x;}
if(delta_y>0)incy=1;
else if(delta_y==0)incy=0;
else{incy=-1;delta_y=-delta_y;}
if( delta_x>delta_y)distance=delta_x;
else distance=delta_y;
for(t=0;t<=distance+1;t++ )
{
LCD_DrawPoint(uRow,uCol);
xerr+=delta_x ;
yerr+=delta_y ;
if(xerr>distance)
{
xerr-=distance;
uRow+=incx;
}
if(yerr>distance)
{
yerr-=distance;
uCol+=incy;
}
}
}
void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
LCD_DrawLine(x1,y1,x2,y1);
LCD_DrawLine(x1,y1,x1,y2);
LCD_DrawLine(x1,y2,x2,y2);
LCD_DrawLine(x2,y1,x2,y2);
}
void LCD_DrawCheckBox(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
{
LCD_DrawRectangle(x1,y1,x2,y2);
LCD_DrawRectangle(x1+1,y1+1,x2-1,y2-1);
}
void LCD_FillCheckBox(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint32_t color)
{
LCD_Fill(x1+2,y1+2,x2-2,y2-2,color);
PointColor = DEFAULT_POINT_COLOR;
}
void LCD_DrawTriangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t x,uint16_t y)
{
LCD_Fill(x1,y1,x2,y2,BackColor);
LCD_DrawLine(x, y-10,x , y+10);
LCD_DrawLine(x+1, y-9, x+1, y+9);
LCD_DrawLine(x+2, y-8, x+2, y+8);
LCD_DrawLine(x+3, y-7, x+3, y+7);
LCD_DrawLine(x+4, y-6, x+4, y+6);
LCD_DrawLine(x+5, y-5, x+5, y+5);
LCD_DrawLine(x+6, y-4, x+6, y+4);
LCD_DrawLine(x+7, y-3, x+7, y+3);
LCD_DrawLine(x+8, y-2, x+8, y+2);
LCD_DrawLine(x+9, y-1, x+9, y+1);
LCD_DrawLine(x+10,y , x+10,y );
}
void LCD_DrawTriangleRE(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t x,uint16_t y)
{
LCD_Fill(x1,y1,x2,y2,BackColor);
LCD_DrawLine(x, y-10,x , y+10);
LCD_DrawLine(x-1, y-9, x-1, y+9);
LCD_DrawLine(x-2, y-8, x-2, y+8);
LCD_DrawLine(x-3, y-7, x-3, y+7);
LCD_DrawLine(x-4, y-6, x-4, y+6);
LCD_DrawLine(x-5, y-5, x-5, y+5);
LCD_DrawLine(x-6, y-4, x-6, y+4);
LCD_DrawLine(x-7, y-3, x-7, y+3);
LCD_DrawLine(x-8, y-2, x-8, y+2);
LCD_DrawLine(x-9, y-1, x-9, y+1);
LCD_DrawLine(x-10,y , x-10,y );
}
void LCD_DrawHistogram(uint16_t x,uint16_t y,uint16_t w)
{
LCD_DrawLine(x,y,x+w,y);
LCD_DrawLine(x,y,x,y+280);
LCD_DrawLine(x+w,y,x+w,y+280);
LCD_DrawLine(x,y+280,x+w,y+280);
LCD_DrawLine(x-10,y,x-2,y);
LCD_DrawLine(x-6 ,y+35,x-2,y+35);
LCD_DrawLine(x-10,y+70,x-2,y+70);
LCD_DrawLine(x-6,y+105,x-2,y+105);
LCD_DrawLine(x-10,y+140,x-2,y+140);
LCD_DrawLine(x-6,y+175,x-2,y+175);
LCD_DrawLine(x-10,y+210,x-2,y+210);
LCD_DrawLine(x-6,y+245,x-2,y+245);
LCD_DrawLine(x-10,y+280,x-2,y+280);
LCD_DrawLine(x-2,y,x-2,y+280);
}
void LCD_FillHistogram(uint16_t x,uint16_t y,uint16_t w,uint16_t value,uint16_t range)
{
LCD_Fill(x+1,y+1,x+w-1,y-1+((range-value)*279)/range,BLACK);
LCD_Fill(x+1,y+((range-value)*279)/range,x+w-1,y+279,YELLOW);
}
void LCD_DrawHistogramLimit(uint16_t x,uint16_t y,uint16_t w,uint16_t limit,uint16_t range)
{
LCD_Fill(x+w+2,y,x+w+2,y-1+((range-limit)*279)/range,RED);
LCD_Fill(x+w+2,y+((range-limit)*279)/range,x+w+2,y+279,BLACK);
}
void LCD_Draw_Circle(uint16_t x0,uint16_t y0,uint8_t r)
{
int a,b;
int di;
a=0;b=r;
di=3-(r<<1);
while(a<=b)
{
LCD_DrawPoint(x0+a,y0-b);
LCD_DrawPoint(x0+b,y0-a);
LCD_DrawPoint(x0+b,y0+a);
LCD_DrawPoint(x0+a,y0+b);
LCD_DrawPoint(x0-a,y0+b);
LCD_DrawPoint(x0-b,y0+a);
LCD_DrawPoint(x0-a,y0-b);
LCD_DrawPoint(x0-b,y0-a);
a++;
if(di<0)di +=4*a+6;
else
{
di+=10+4*(a-b);
b--;
}
}
}
void LCD_ShowChar(uint16_t x,uint16_t y,char num,uint8_t size,uint8_t mode)
{
uint8_t temp,t1,t;
uint16_t y0=y;
uint8_t csize=(size/8+((size%8)?1:0))*(size/2);
num=num-' ';
for(t=0;t<csize;t++)
{
if(size==12)temp=asc2_1206[num][t];
else if(size==16)temp=asc2_1608[num][t];
else if(size==24)temp=asc2_2412[num][t];
else if(size==32)temp=asc2_3216[num][t];
else return;
for(t1=0;t1<8;t1++)
{
if(temp&0x80)LCD_Fast_DrawPoint(x,y,PointColor);
else if(mode==0)LCD_Fast_DrawPoint(x,y,BackColor);
temp<<=1;
y++;
if(y>=lcddev.height)return;
if((y-y0)==size)
{
y=y0;
x++;
if(x>=lcddev.width)return;
break;
}
}
}
}
char* FloatAsc2Right(float value, uint8_t dot)
{
float temp_val, compensate;
uint16_t n, int_num, int_temp;
uint8_t i,j,k;
static char temp_char[9] = {0};
temp_char[8] = 0;
i = 2 - dot;
compensate = 0.51;
for(j = 0;j < 8;j++)
{
temp_char[j] = ' ';
}
for(k = 0; k < dot; k++)
{
compensate = compensate / 10;
}
temp_val = value + compensate;
int_num = (uint16_t)temp_val;
int_temp = int_num;
if(dot == 0)
{
i++;
}
n = 10000;
if(int_num == 0)
{
i = i+4;
}else
{
while(n > 0)
{
if(int_num >= n)
{
temp_char[i] = (char)(int_temp / n) + 48;
int_temp = int_temp % n;
}
i += 1;
n /= 10;
}
}
if(temp_val < 1)
{
temp_char[i] = 48;
i +=1;
}
int_temp = (uint16_t)((temp_val - int_num) * 10000);
if(dot > 0)
{
temp_char[i] = 46;
i += 1;
temp_char[i] = (char)(int_temp / 1000) + 48;
i += 1;
}
if(dot > 1)
{
int_temp = int_temp % 1000;
temp_char[i] = (char)(int_temp / 100) + 48;
i += 1;
}
return temp_char;
}
char* FloatAsc2Left(float value, uint8_t dot)
{
float temp_val, compensate;
uint16_t n, int_num, int_temp;
uint8_t i,j,k;
static char temp_char[8] = {0};
i = 0;
compensate = 0.51;
for(j = 0;j < 8;j++)
{
temp_char[j] = 0;
}
for(k = 0; k < dot; k++)
{
compensate = compensate / 10;
}
temp_val = value + compensate;
int_num = (uint16_t)temp_val;
int_temp = int_num;
n = 10000;
while(n > 0)
{
if(int_num >= n)
{
temp_char[i] = (char)(int_temp / n) + 48;
int_temp = int_temp % n;
i += 1;
}
n /= 10;
}
if(temp_val < 1)
{
temp_char[i] = 48;
i +=1;
}
int_temp = (uint16_t)((temp_val - int_num) * 10000);
if(dot > 0)
{
temp_char[i] = 46;
i += 1;
temp_char[i] = (char)(int_temp / 1000) + 48;
i += 1;
}
if(dot > 1)
{
int_temp = int_temp % 1000;
temp_char[i] = (char)(int_temp / 100) + 48;
i += 1;
}
return temp_char;
}
void LCD_ShowString(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint8_t size,volatile char *p)
{
uint8_t x0=x;
width+=x;
height+=y;
while((*p<='~')&&(*p>=' '))
{
if(x>=width){x=x0;y+=size;}
if(y>=height)break;
LCD_ShowChar(x,y,*p,size,0);
x+=size/2;
p++;
}
}
void LCD_ShowFont(uint16_t x,uint16_t y,uint8_t size,const volatile char *p,uint8_t mode)
{
uint16_t i=0,j=0,k=0;
uint16_t num_font;
uint16_t x0=x;
if(size == 24)
{
num_font = sizeof(GB24_Code) / sizeof(typFNT_GB24);
for(i=0;i<num_font;i++)
{
if((*p==GB24_Code[i].Index[0]) && (*(p+1)==GB24_Code[i].Index[1]))
{
for(j=0;j<MSKNUM24;j++)
{
unsigned short word=GB24_Code[i].Msk[j];
for(k=0;k<8;k++)
{
if(word&0x80)LCD_Fast_DrawPoint(x,y,PointColor);
else if(mode==0)LCD_Fast_DrawPoint(x,y,BackColor);
word<<=1;
x++;
if((x-x0)==size)
{
x=x0;
y++;
break;
}
}
}
}
}
}else if(size == 32)
{
num_font = sizeof(GB32_Code) / sizeof(typFNT_GB32);
for(i=0;i<num_font;i++)
{
if((*p==GB32_Code[i].Index[0]) && (*(p+1)==GB32_Code[i].Index[1]))
{
for(j=0;j<MSKNUM32;j++)
{
unsigned short word=GB32_Code[i].Msk[j];
for(k=0;k<8;k++)
{
if(word&0x80)LCD_Fast_DrawPoint(x,y,PointColor);
else if(mode==0)LCD_Fast_DrawPoint(x,y,BackColor);
word<<=1;
x++;
if((x-x0)==size)
{
x=x0;
y++;
break;
}
}
}
}
}
}else
{
;
}
}
void LCD_ShowPicture(uint16_t x,uint16_t y,uint16_t width,uint16_t heigth, const uint8_t *data,uint8_t mode)
{
uint16_t i=0,j=0;
uint16_t num_font;
uint16_t x0=x;
num_font = width * heigth / 8;
for(i=0;i<num_font;i++)
{
uint8_t word=data[i];
for(j=0;j<8;j++)
{
if(word&0x80)LCD_Fast_DrawPoint(x,y,WHITE);
else if(mode==0)LCD_Fast_DrawPoint(x,y,BackColor);
word<<=1;
x++;
if((x-x0)==width)
{
x=x0;
y++;
break;
}
}
}
}
void LCD_ShowText(uint16_t x,uint16_t y,uint16_t width,uint16_t height,volatile char*str,uint8_t size,uint8_t mode)
{
while(*str!=0)
{
if((uint8_t)*str<0x81)
{
LCD_ShowChar(x,y,*str,size,mode);
str++;
x+=size/2;
}
else
{
LCD_ShowFont(x,y,size,str,mode);
str+=2;
x+=size;
}
}
}
LTDC.H
```c
#ifndef _LCD_H
#define _LCD_H
#include "sys.h"
typedef struct
{
uint32_t pwidth;
uint32_t pheight;
uint16_t hsw;
uint16_t vsw;
uint16_t hbp;
uint16_t vbp;
uint16_t hfp;
uint16_t vfp;
uint8_t activelayer;
uint8_t dir;
uint16_t width;
uint16_t height;
uint32_t pixsize;
}_ltdc_dev;
extern _ltdc_dev lcdltdc;
extern LTDC_HandleTypeDef LTDC_Handler;
extern DMA2D_HandleTypeDef DMA2D_Handler;
#define LCD_PIXEL_FORMAT_ARGB8888 0X00
#define LCD_PIXEL_FORMAT_RGB888 0X01
#define LCD_PIXEL_FORMAT_RGB565 0X02
#define LCD_PIXEL_FORMAT_ARGB1555 0X03
#define LCD_PIXEL_FORMAT_ARGB4444 0X04
#define LCD_PIXEL_FORMAT_L8 0X05
#define LCD_PIXEL_FORMAT_AL44 0X06
#define LCD_PIXEL_FORMAT_AL88 0X07
#define LCD_PIXFORMAT LCD_PIXEL_FORMAT_RGB565
#define LTDC_BACKLAYERCOLOR BLACK
#define LCD_FRAME_BUF_ADDR 0XC0000000
void LTDC_Switch(uint8_t sw);
void LTDC_Layer_Switch(uint8_t layerx,uint8_t sw);
void LTDC_Select_Layer(uint8_t layerx);
void LTDC_Display_Dir(uint8_t dir);
void LTDC_Draw_Point(uint16_t x,uint16_t y,uint32_t color);
uint32_t LTDC_Read_Point(uint16_t x,uint16_t y);
void LTDC_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint32_t color);
void LTDC_Color_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t *color);
void LTDC_Clear(uint32_t color);
uint8_t LTDC_Clk_Set(uint32_t pllsain,uint32_t pllsair,uint32_t pllsaidivr);
void LTDC_Layer_Window_Config(uint8_t layerx,uint16_t sx,uint16_t sy,uint16_t width,uint16_t height);
void LTDC_Layer_Parameter_Config(uint8_t layerx,uint32_t bufaddr,uint8_t pixformat,uint8_t alpha,uint8_t alpha0,uint8_t bfac1,uint8_t bfac2,uint32_t bkcolor);
uint16_t LTDC_PanelID_Read(void);
void LTDC_Init(void);
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
LTDC.c
#include "ltdc.h"
#include "lcd.h"
LTDC_HandleTypeDef LTDC_Handler;
DMA2D_HandleTypeDef DMA2D_Handler;
#if LCD_PIXFORMAT==LCD_PIXFORMAT_ARGB8888||LCD_PIXFORMAT==LCD_PIXFORMAT_RGB888
uint32_t ltdc_lcd_framebuf[1280][800] __attribute__((at(LCD_FRAME_BUF_ADDR)));
#else
uint16_t ltdc_lcd_framebuf[1280][800] __attribute__((at(LCD_FRAME_BUF_ADDR)));
#endif
uint32_t *ltdc_framebuf[2];
_ltdc_dev lcdltdc;
void LTDC_Switch(uint8_t sw)
{
if(sw==1) __HAL_LTDC_ENABLE(<DC_Handler);
else if(sw==0)__HAL_LTDC_DISABLE(<DC_Handler);
}
void LTDC_Layer_Switch(uint8_t layerx,uint8_t sw)
{
if(sw==1) __HAL_LTDC_LAYER_ENABLE(<DC_Handler,layerx);
else if(sw==0) __HAL_LTDC_LAYER_DISABLE(<DC_Handler,layerx);
__HAL_LTDC_RELOAD_CONFIG(<DC_Handler);
}
void LTDC_Select_Layer(uint8_t layerx)
{
lcdltdc.activelayer=layerx;
}
void LTDC_Display_Dir(uint8_t dir)
{
lcdltdc.dir=dir;
if(dir==0)
{
lcdltdc.width=lcdltdc.pheight;
lcdltdc.height=lcdltdc.pwidth;
}else if(dir==1)
{
lcdltdc.width=lcdltdc.pwidth;
lcdltdc.height=lcdltdc.pheight;
}
}
void LTDC_Draw_Point(uint16_t x,uint16_t y,uint32_t color)
{
#if LCD_PIXFORMAT==LCD_PIXFORMAT_ARGB8888||LCD_PIXFORMAT==LCD_PIXFORMAT_RGB888
if(lcdltdc.dir)
{
*(uint32_t*)((uint32_t)ltdc_framebuf[lcdltdc.activelayer]+lcdltdc.pixsize*(lcdltdc.pwidth*y+x))=color;
}else
{
*(uint32_t*)((uint32_t)ltdc_framebuf[lcdltdc.activelayer]+lcdltdc.pixsize*(lcdltdc.pwidth*(lcdltdc.pheight-x)+y))=color;
}
#else
if(lcdltdc.dir)
{
*(uint16_t*)((uint32_t)ltdc_framebuf[lcdltdc.activelayer]+lcdltdc.pixsize*(lcdltdc.pwidth*y+x))=color;
}else
{
*(uint16_t*)((uint32_t)ltdc_framebuf[lcdltdc.activelayer]+lcdltdc.pixsize*(lcdltdc.pwidth*(lcdltdc.pheight-x-1)+y))=color;
}
#endif
}
uint32_t LTDC_Read_Point(uint16_t x,uint16_t y)
{
#if LCD_PIXFORMAT==LCD_PIXFORMAT_ARGB8888||LCD_PIXFORMAT==LCD_PIXFORMAT_RGB888
if(lcdltdc.dir)
{
return *(uint32_t*)((uint32_t)ltdc_framebuf[lcdltdc.activelayer]+lcdltdc.pixsize*(lcdltdc.pwidth*y+x));
}else
{
return *(uint32_t*)((uint32_t)ltdc_framebuf[lcdltdc.activelayer]+lcdltdc.pixsize*(lcdltdc.pwidth*(lcdltdc.pheight-x)+y));
}
#else
if(lcdltdc.dir)
{
return *(uint16_t*)((uint32_t)ltdc_framebuf[lcdltdc.activelayer]+lcdltdc.pixsize*(lcdltdc.pwidth*y+x));
}else
{
return *(uint16_t*)((uint32_t)ltdc_framebuf[lcdltdc.activelayer]+lcdltdc.pixsize*(lcdltdc.pwidth*(lcdltdc.pheight-x-1)+y));
}
#endif
}
void LTDC_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint32_t color)
{
uint32_t psx,psy,pex,pey;
uint32_t timeout=0;
uint16_t offline;
uint32_t addr;
if(lcdltdc.dir)
{
psx=sx;psy=sy;
pex=ex;pey=ey;
}else
{
psx=sy;psy=lcdltdc.pheight-ex-1;
pex=ey;pey=lcdltdc.pheight-sx-1;
}
offline=lcdltdc.pwidth-(pex-psx+1);
addr=((uint32_t)ltdc_framebuf[lcdltdc.activelayer]+lcdltdc.pixsize*(lcdltdc.pwidth*psy+psx));
__HAL_RCC_DMA2D_CLK_ENABLE();
DMA2D->CR&=~(DMA2D_CR_START);
DMA2D->CR=DMA2D_R2M;
DMA2D->OPFCCR=LCD_PIXFORMAT;
DMA2D->OOR=offline;
DMA2D->OMAR=addr;
DMA2D->NLR=(pey-psy+1)|((pex-psx+1)<<16);
DMA2D->OCOLR=color;
DMA2D->CR|=DMA2D_CR_START;
while((DMA2D->ISR&(DMA2D_FLAG_TC))==0)
{
timeout++;
if(timeout>0X1FFFFF)break;
}
DMA2D->IFCR|=DMA2D_FLAG_TC;
}
void LTDC_Color_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint16_t *color)
{
uint32_t psx,psy,pex,pey;
uint32_t timeout=0;
uint16_t offline;
uint32_t addr;
if(lcdltdc.dir)
{
psx=sx;psy=sy;
pex=ex;pey=ey;
}else
{
psx=sy;psy=lcdltdc.pheight-ex-1;
pex=ey;pey=lcdltdc.pheight-sx-1;
}
offline=lcdltdc.pwidth-(pex-psx+1);
addr=((uint32_t)ltdc_framebuf[lcdltdc.activelayer]+lcdltdc.pixsize*(lcdltdc.pwidth*psy+psx));
__HAL_RCC_DMA2D_CLK_ENABLE();
DMA2D->CR&=~(DMA2D_CR_START);
DMA2D->CR=DMA2D_M2M;
DMA2D->FGPFCCR=LCD_PIXFORMAT;
DMA2D->FGOR=0;
DMA2D->OOR=offline;
DMA2D->FGMAR=(uint32_t)color;
DMA2D->OMAR=addr;
DMA2D->NLR=(pey-psy+1)|((pex-psx+1)<<16);
DMA2D->CR|=DMA2D_CR_START;
while((DMA2D->ISR&(DMA2D_FLAG_TC))==0)
{
timeout++;
if(timeout>0X1FFFFF)break;
}
DMA2D->IFCR|=DMA2D_FLAG_TC;
}
void LTDC_Clear(uint32_t color)
{
LTDC_Fill(0,0,lcdltdc.width-1,lcdltdc.height-1,color);
}
uint8_t LTDC_Clk_Set(uint32_t pllsain,uint32_t pllsair,uint32_t pllsaidivr)
{
RCC_PeriphCLKInitTypeDef PeriphClkIniture;
PeriphClkIniture.PeriphClockSelection=RCC_PERIPHCLK_LTDC;
PeriphClkIniture.PLLSAI.PLLSAIN=pllsain;
PeriphClkIniture.PLLSAI.PLLSAIR=pllsair;
PeriphClkIniture.PLLSAIDivR=pllsaidivr;
if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkIniture)==HAL_OK)
{
return 0;
}
else return 1;
}
void LTDC_Layer_Window_Config(uint8_t layerx,uint16_t sx,uint16_t sy,uint16_t width,uint16_t height)
{
HAL_LTDC_SetWindowPosition(<DC_Handler,sx,sy,layerx);
HAL_LTDC_SetWindowSize(<DC_Handler,width,height,layerx);
}
void LTDC_Layer_Parameter_Config(uint8_t layerx,uint32_t bufaddr,uint8_t pixformat,uint8_t alpha,uint8_t alpha0,uint8_t bfac1,uint8_t bfac2,uint32_t bkcolor)
{
LTDC_LayerCfgTypeDef pLayerCfg;
pLayerCfg.WindowX0=0;
pLayerCfg.WindowY0=0;
pLayerCfg.WindowX1=lcdltdc.pwidth;
pLayerCfg.WindowY1=lcdltdc.pheight;
pLayerCfg.PixelFormat=pixformat;
pLayerCfg.Alpha=alpha;
pLayerCfg.Alpha0=alpha0;
pLayerCfg.BlendingFactor1=(uint32_t)bfac1<<8;
pLayerCfg.BlendingFactor2=(uint32_t)bfac2<<8;
pLayerCfg.FBStartAdress=bufaddr;
pLayerCfg.ImageWidth=lcdltdc.pwidth;
pLayerCfg.ImageHeight=lcdltdc.pheight;
pLayerCfg.Backcolor.Red=(uint8_t)(bkcolor&0X00FF0000)>>16;
pLayerCfg.Backcolor.Green=(uint8_t)(bkcolor&0X0000FF00)>>8;
pLayerCfg.Backcolor.Blue=(uint8_t)bkcolor&0X000000FF;
HAL_LTDC_ConfigLayer(<DC_Handler,&pLayerCfg,layerx);
}
void LTDC_Init(void)
{
lcdltdc.pwidth=LCD_HOR_RESOLUTION;
lcdltdc.pheight=LCD_VER_RESOLUTION;
lcdltdc.hsw=LCD_HOR_PULSE_WIDTH;
lcdltdc.vsw=LCD_VER_PULSE_WIDTH;
lcdltdc.hbp=LCD_HOR_BACK_PORCH;
lcdltdc.vbp=LCD_VER_BACK_PORCH;
lcdltdc.hfp=LCD_HOR_FRONT_PORCH;
lcdltdc.vfp=LCD_VER_FRONT_PORCH;
LTDC_Clk_Set(400,4,RCC_PLLSAIDIVR_4);
lcddev.width=lcdltdc.pwidth;
lcddev.height=lcdltdc.pheight;
#if LCD_PIXFORMAT==LCD_PIXFORMAT_ARGB8888||LCD_PIXFORMAT==LCD_PIXFORMAT_RGB888
ltdc_framebuf[0]=(uint32_t*)<dc_lcd_framebuf;
lcdltdc.pixsize=4;
#else
lcdltdc.pixsize=2;
ltdc_framebuf[0]=(uint32_t*)<dc_lcd_framebuf;
#endif
LTDC_Handler.Instance=LTDC;
LTDC_Handler.Init.HSPolarity=LTDC_HSPOLARITY_AL;
LTDC_Handler.Init.VSPolarity=LTDC_VSPOLARITY_AL;
LTDC_Handler.Init.DEPolarity=LTDC_DEPOLARITY_AL;
LTDC_Handler.Init.PCPolarity=LTDC_PCPOLARITY_IPC;
LTDC_Handler.Init.HorizontalSync=lcdltdc.hsw-1;
LTDC_Handler.Init.VerticalSync=lcdltdc.vsw-1;
LTDC_Handler.Init.AccumulatedHBP=lcdltdc.hsw+lcdltdc.hbp-1;
LTDC_Handler.Init.AccumulatedVBP=lcdltdc.vsw+lcdltdc.vbp-1;
LTDC_Handler.Init.AccumulatedActiveW=lcdltdc.hsw+lcdltdc.hbp+lcdltdc.pwidth-1;
LTDC_Handler.Init.AccumulatedActiveH=lcdltdc.vsw+lcdltdc.vbp+lcdltdc.pheight-1;
LTDC_Handler.Init.TotalWidth=lcdltdc.hsw+lcdltdc.hbp+lcdltdc.pwidth+lcdltdc.hfp-1;
LTDC_Handler.Init.TotalHeigh=lcdltdc.vsw+lcdltdc.vbp+lcdltdc.pheight+lcdltdc.vfp-1;
LTDC_Handler.Init.Backcolor.Red=0XFF;
LTDC_Handler.Init.Backcolor.Green=0XFF;
LTDC_Handler.Init.Backcolor.Blue=0XFF;
HAL_LTDC_Init(<DC_Handler);
LTDC_Layer_Parameter_Config(0,(uint32_t)ltdc_framebuf[0],LCD_PIXFORMAT,255,0,6,7,0X000000);
LTDC_Layer_Window_Config(0,0,0,lcdltdc.pwidth,lcdltdc.pheight);
LTDC_Display_Dir(1);
LTDC_Select_Layer(0);
LCD_BlackLightOn();
LTDC_Clear(BLACK);
}
void HAL_LTDC_MspInit(LTDC_HandleTypeDef* hltdc)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_LTDC_CLK_ENABLE();
__HAL_RCC_DMA2D_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
GPIO_Initure.Pin=GPIO_PIN_10;
GPIO_Initure.Mode=GPIO_MODE_AF_PP;
GPIO_Initure.Pull=GPIO_NOPULL;
GPIO_Initure.Speed=GPIO_SPEED_HIGH;
GPIO_Initure.Alternate=GPIO_AF14_LTDC;
HAL_GPIO_Init(GPIOF,&GPIO_Initure);
GPIO_Initure.Pin=GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_11;
HAL_GPIO_Init(GPIOG,&GPIO_Initure);
GPIO_Initure.Pin=GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|\
GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
HAL_GPIO_Init(GPIOH,&GPIO_Initure);
GPIO_Initure.Pin=GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_5|\
GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_9|GPIO_PIN_10;
HAL_GPIO_Init(GPIOI,&GPIO_Initure);
}
参考:https://blog.csdn.net/weixin_48375064/article/details/120009393
|