使用这段代码还需要使用SDRAM模块
工程代码可以参考正点原子的例程 例程下载
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);
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);
}
``
|