qspi_stream_ip驱动st77916的c程序 - minichao9901/TangNano-20k-Zynq-7020 GitHub Wiki

说明

  • st77916是command mode
  • 使用的仍然是st77903中用的qspi_ip,在byte排序上仍然是用的rgb88的排序(按byte序),其实不符合rgb565的需求(按half_word反序)。为了解决这个问题,我们在c程序中对1920x1080的原始图像做了rgb888->rgb565的处理,并对byte顺序做了调整,以适应这种情况(其实就是w16的msb/lsb byte交换)。程序中有说明。
  • st77916的模组引脚如下:
  • 注意这个图中LEDA与LEDK错了,是反的。

image image image image image

bd说明

《axi4_stream_qspi3_st77916》

image image

刷单色图程序

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xparameters.h"
#include "xil_io.h"
#include "sleep.h"
//#include "ff.h"
#include "xil_cache.h"
#include "xtime_l.h"
#include "xgpiops.h"


#define PS_KEY  (54 + 0)  //EMIO0,对应的GPIO编号为54+0=54
#define FULL_PIN  (54 + 1)    //EMIO1,对应的GPIO编号为54+1=55
#define EMPTY_PIN  (54 + 2)    //EMIO2,对应的GPIO编号为54+2=56

#define INPUT		1
#define OUTPUT		0
XGpioPs GpioPs;	//GPIO实例对象
void PS_GPIO_Init()
{
	XGpioPs_Config *ConfigPtr;
	ConfigPtr = XGpioPs_LookupConfig(XPAR_XGPIOPS_0_DEVICE_ID);
	XGpioPs_CfgInitialize(&GpioPs, ConfigPtr, ConfigPtr->BaseAddr);
}

/**
  *****************************************************
  * @brief	初始化特定GPIO口的模式与状态
  * @param	GPIO_Num	GPIO编号(MIO为0~53,EMIO从54开始)
  * @param	Dir			输入/输出:OUTPUT为输出,INPUT为输入
  * @param	Data		输出电平高低:0为低,1为高(若设置为输入则此处数据无影响)
  * @usage	//设置GPIO46为输出模式,输出为高电平
  * 		GPIO_SetMode(46, OUTPUT, 1);
  *****************************************************
**/
void PS_GPIO_SetMode(uint8_t GPIO_Num, uint8_t Dir, uint8_t Data)
{
	if(Dir == INPUT) {
		XGpioPs_SetDirectionPin(&GpioPs, GPIO_Num, 0);
	} else if(Dir == OUTPUT){
		XGpioPs_SetDirectionPin(&GpioPs, GPIO_Num, 1);
		XGpioPs_SetOutputEnablePin(&GpioPs, GPIO_Num, 1);
		XGpioPs_WritePin(&GpioPs, GPIO_Num, Data);
	}
}

/**
  *****************************************************
  * @brief	控制特定GPIO口的输出高/低电平,使用前必须先用GPIO_SetMode设置该GPIO口为"输出"模式
  * @param	GPIO_Num	GPIO编号(MIO为0~53,EMIO从54开始)
  * @param	Data		输出电平高低:0为低,1为高
  * @usage	//控制GPIO46输出高电平
  * 		GPIO_SetPort(46, 1);
  *****************************************************
**/
void PS_GPIO_SetPort(uint8_t GPIO_Num, uint8_t Data)
{
	XGpioPs_WritePin(&GpioPs, GPIO_Num, Data);	//输出高/低电平,0为低,1为高
}

/**
  *****************************************************
  * @brief	读取特定GPIO口的输入电平,使用前必须先用GPIO_SetMode设置该GPIO口为"输入"模式
  * @param	GPIO_Num	GPIO编号(MIO为0~53,EMIO从54开始)
  * @return	返回采集的GPIO电平值,0为低,1为高
  * @usage	//采集GPIO46的电平,存放在state
  * 		state = GPIO_GetPort(46, state);
  *****************************************************
**/
uint8_t PS_GPIO_GetPort(uint8_t GPIO_Num)
{
	return XGpioPs_ReadPin(&GpioPs, GPIO_Num);		//输出高/低电平,0为低,1为高
}


void gpio_init()
{
    PS_GPIO_Init(); //初始化PS端MIO和EMIO
    PS_GPIO_SetMode(PS_KEY, OUTPUT, 0);
    PS_GPIO_SetMode(FULL_PIN, INPUT, 0);
    PS_GPIO_SetMode(EMPTY_PIN, INPUT, 1);
}

void qspi_mode_set(u8 mode)
{
	PS_GPIO_SetPort(PS_KEY, mode);
}

u32 read_full()
{
	return PS_GPIO_GetPort(FULL_PIN);
}

u32 read_empty()
{
	return PS_GPIO_GetPort(EMPTY_PIN);
}

/***********************************************************************************/

#define  RED        0xF800
#define  ORANGE     0xFC00
#define  YELLOW     0xFFE0
#define  GREEN      0x07E0
#define  CYAN       0x07FF
#define  BLUE       0x001F
#define  PURPPLE    0xF81F
#define  BLACK      0x0000
#define  WHITE      0xFFFF
#define  GRAY       0xD69A

const u16 colors[] = {
	RED,
	GREEN,
	BLUE,
	ORANGE,
	YELLOW,
	CYAN,
	PURPPLE,
	BLACK,
	WHITE,
	GRAY
};

typedef struct{
	u8 txbuf[50];
	u8 rxbuf[50];
	u8 length;
}t_buf;

//#define BIST_MODE
//t_buf WriteBuffer[]={
//		{{0xf0, 0xc3}, {0x00},2},
//		{{0xf0, 0x96}, {0x00},2},
//		{{0xf0, 0xa5}, {0x00},2},
//		{{0xe9, 0x20}, {0x00},2},
//		{{0xe7,  0x80, 0x77, 0x1f, 0xcc}, {0x00}, 5},
//		{{0xc1,  0x77, 0x07, 0xcf, 0x16}, {0x00},5},//Normal:VGHS/VGLS/VSP/VSN电压
//		{{0xc2,  0x77, 0x07, 0xcf, 0x16}, {0x00},5},//Idle: VGHS/VGLS/VSP/VSN电压
//		{{0xc3,  0x22, 0x02, 0x22, 0x04}, {0x00},5},//Normal: VGH/VGL/VSP/VSN Clk
//		{{0xc4,  0x22, 0x02, 0x22, 0x04}, {0x00},5},//Idle: VGH/VGL/VSP/VSN Clk
//		{{0xc5,  0xed},{0x00},2}, //VCOM
//		{{0xe0, 0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b, 0x17, 0x2a, 0x2f},{0x00},15},
//		{{0xe1, 0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b, 0x17, 0x2a, 0x2f},{0x00},15},
//		{{0xe5, 0xbe, 0xf5, 0xb1, 0x22, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22},{0x00},15},
//		{{0xe6, 0xbe, 0xf5, 0xb1, 0x22, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22},{0x00},15},
//		{{0xec, 0x40, 0x03},{0x00},3},
//		{{0x36,  0x00},{0x00},2},
//		{{0x3a,  0x55},{0x00},2},
//		{{0xb2,  0x00},{0x00},2},//GIP pattern
//		{{0xb3,  0x01},{0x00},2},//video mode dot-inversion
//		{{0xb4,  0x01},{0x00},2},
//		{{0xb5,  0x00, 0x08, 0x00, 0x08},{0x00},5}, //vfp
//		{{0xb6,  0xc7, 0x31},{0x00},3},
//		{{0xa5,  0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x2a, 0x8a, 0x02},{0x00},10},
//		{{0xa6,  0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x2a, 0x8a, 0x02},{0x00},10},
//		{{0xba,  0x0a, 0x5a, 0x23, 0x10, 0x25, 0x02, 0x00},{0x00},8},
//		{{0xbb,  0x00, 0x30, 0x00, 0x2c, 0x82, 0x87, 0x18, 0x00},{0x00},9},
//		{{0xbc,  0x00, 0x30, 0x00, 0x2c, 0x82, 0x87, 0x18, 0x00},{0x00},9},
//		{{0xbd, 0xa1, 0xb2, 0x2b, 0x1a, 0x56, 0x43, 0x34, 0x65, 0xff, 0xff, 0x0f},{0x00},12},
//		{{0x35,  0x00},{0x00},2},
//		{{0x21},{0x00},1},
//		{{0x11},{0x00},1},
//		{{0xff},{0x00},0},
//		{{0x29},{0x00},1},
//		{{0xff},{0x00},0},
//		{{0x2c},{0x00},1},
		#ifdef BIST_MODE
		{{0xb0,  0xa5},{0x00},2},
		{{0xcc,  0x40, 0x00, 0x3f, 0x00, 0x14, 0x14, 0x20, 0x20, 0x03},{0x00},10},
		#endif
//};

t_buf WriteBuffer[]={
	{{0x3A,0x55},{0x00},2},
	{{0xF0,0x08},{0x00},2},
	{{0xF2,0x08},{0x00},2},
	{{0x9B,0x51},{0x00},2},
	{{0x86,0x53},{0x00},2},
	{{0xF2,0x80},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0xF0,0x01},{0x00},2},
	{{0xF1,0x01},{0x00},2},
	{{0xB0,0x54},{0x00},2},
	{{0xB1,0x3F},{0x00},2},
	{{0xB2,0x2A},{0x00},2},
	{{0xB4,0x46},{0x00},2},
	{{0xB5,0x34},{0x00},2},
	{{0xB6,0xD5},{0x00},2},
	{{0xB7,0x30},{0x00},2},
	{{0xBA,0x00},{0x00},2},
	{{0xBB,0x08},{0x00},2},
	{{0xBC,0x08},{0x00},2},
	{{0xBD,0x00},{0x00},2},
	{{0xC0,0x80},{0x00},2},
	{{0xC1,0x10},{0x00},2},
	{{0xC2,0x37},{0x00},2},
	{{0xC3,0x80},{0x00},2},
	{{0xC4,0x10},{0x00},2},
	{{0xC5,0x37},{0x00},2},
	{{0xC6,0xA9},{0x00},2},
	{{0xC7,0x41},{0x00},2},
	{{0xC8,0x51},{0x00},2},
	{{0xC9,0xA9},{0x00},2},
	{{0xCA,0x41},{0x00},2},
	{{0xCB,0x51},{0x00},2},
	{{0xD0,0x91},{0x00},2},
	{{0xD1,0x68},{0x00},2},
	{{0xD2,0x69},{0x00},2},
	{{0xF5,0x00,0xA5},{0x00},3},
	{{0xDD,0x3F},{0x00},2},
	{{0xDE,0x3F},{0x00},2},
	{{0xF1,0x10},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0xF0,0x02},{0x00},2},
	{{0xE0,0xF0,0x06,0x0B,0x09,0x09,0x16,0x32,0x44,0x4A,0x37,0x13,0x13,0x2E,0x34},{0x00},15},
	{{0xE1,0xF0,0x06,0x0B,0x09,0x08,0x05,0x32,0x33,0x49,0x17,0x13,0x13,0x2E,0x34},{0x00},15},
	{{0xF0,0x10},{0x00},2},
	{{0xF3,0x10},{0x00},2},
	{{0xE0,0x0A},{0x00},2},
	{{0xE1,0x00},{0x00},2},
	{{0xE2,0x00},{0x00},2},
	{{0xE3,0x00},{0x00},2},
	{{0xE4,0xE0},{0x00},2},
	{{0xE5,0x06},{0x00},2},
	{{0xE6,0x21},{0x00},2},
	{{0xE7,0x00},{0x00},2},
	{{0xE8,0x05},{0x00},2},
	{{0xE9,0x82},{0x00},2},
	{{0xEA,0xDF},{0x00},2},
	{{0xEB,0x89},{0x00},2},
	{{0xEC,0x20},{0x00},2},
	{{0xED,0x14},{0x00},2},
	{{0xEE,0xFF},{0x00},2},
	{{0xEF,0x00},{0x00},2},
	{{0xF8,0xFF},{0x00},2},
	{{0xF9,0x00},{0x00},2},
	{{0xFA,0x00},{0x00},2},
	{{0xFB,0x30},{0x00},2},
	{{0xFC,0x00},{0x00},2},
	{{0xFD,0x00},{0x00},2},
	{{0xFE,0x00},{0x00},2},
	{{0xFF,0x00},{0x00},2},
	{{0x60,0x42},{0x00},2},
	{{0x61,0xE0},{0x00},2},
	{{0x62,0x40},{0x00},2},
	{{0x63,0x40},{0x00},2},
	{{0x64,0x02},{0x00},2},
	{{0x65,0x00},{0x00},2},
	{{0x66,0x40},{0x00},2},
	{{0x67,0x03},{0x00},2},
	{{0x68,0x00},{0x00},2},
	{{0x69,0x00},{0x00},2},
	{{0x6A,0x00},{0x00},2},
	{{0x6B,0x00},{0x00},2},
	{{0x70,0x42},{0x00},2},
	{{0x71,0xE0},{0x00},2},
	{{0x72,0x40},{0x00},2},
	{{0x73,0x40},{0x00},2},
	{{0x74,0x02},{0x00},2},
	{{0x75,0x00},{0x00},2},
	{{0x76,0x40},{0x00},2},
	{{0x77,0x03},{0x00},2},
	{{0x78,0x00},{0x00},2},
	{{0x79,0x00},{0x00},2},
	{{0x7A,0x00},{0x00},2},
	{{0x7B,0x00},{0x00},2},
	{{0x80,0x48},{0x00},2},
	{{0x81,0x00},{0x00},2},
	{{0x82,0x05},{0x00},2},
	{{0x83,0x02},{0x00},2},
	{{0x84,0xDD},{0x00},2},
	{{0x85,0x00},{0x00},2},
	{{0x86,0x00},{0x00},2},
	{{0x87,0x00},{0x00},2},
	{{0x88,0x48},{0x00},2},
	{{0x89,0x00},{0x00},2},
	{{0x8A,0x07},{0x00},2},
	{{0x8B,0x02},{0x00},2},
	{{0x8C,0xDF},{0x00},2},
	{{0x8D,0x00},{0x00},2},
	{{0x8E,0x00},{0x00},2},
	{{0x8F,0x00},{0x00},2},
	{{0x90,0x48},{0x00},2},
	{{0x91,0x00},{0x00},2},
	{{0x92,0x09},{0x00},2},
	{{0x93,0x02},{0x00},2},
	{{0x94,0xE1},{0x00},2},
	{{0x95,0x00},{0x00},2},
	{{0x96,0x00},{0x00},2},
	{{0x97,0x00},{0x00},2},
	{{0x98,0x48},{0x00},2},
	{{0x99,0x00},{0x00},2},
	{{0x9A,0x0B},{0x00},2},
	{{0x9B,0x02},{0x00},2},
	{{0x9C,0xE3},{0x00},2},
	{{0x9D,0x00},{0x00},2},
	{{0x9E,0x00},{0x00},2},
	{{0x9F,0x00},{0x00},2},
	{{0xA0,0x48},{0x00},2},
	{{0xA1,0x00},{0x00},2},
	{{0xA2,0x04},{0x00},2},
	{{0xA3,0x02},{0x00},2},
	{{0xA4,0xDC},{0x00},2},
	{{0xA5,0x00},{0x00},2},
	{{0xA6,0x00},{0x00},2},
	{{0xA7,0x00},{0x00},2},
	{{0xA8,0x48},{0x00},2},
	{{0xA9,0x00},{0x00},2},
	{{0xAA,0x06},{0x00},2},
	{{0xAB,0x02},{0x00},2},
	{{0xAC,0xDE},{0x00},2},
	{{0xAD,0x00},{0x00},2},
	{{0xAE,0x00},{0x00},2},
	{{0xAF,0x00},{0x00},2},
	{{0xB0,0x48},{0x00},2},
	{{0xB1,0x00},{0x00},2},
	{{0xB2,0x08},{0x00},2},
	{{0xB3,0x02},{0x00},2},
	{{0xB4,0xE0},{0x00},2},
	{{0xB5,0x00},{0x00},2},
	{{0xB6,0x00},{0x00},2},
	{{0xB7,0x00},{0x00},2},
	{{0xB8,0x48},{0x00},2},
	{{0xB9,0x00},{0x00},2},
	{{0xBA,0x0A},{0x00},2},
	{{0xBB,0x02},{0x00},2},
	{{0xBC,0xE2},{0x00},2},
	{{0xBD,0x00},{0x00},2},
	{{0xBE,0x00},{0x00},2},
	{{0xBF,0x00},{0x00},2},
	{{0xC0,0x12},{0x00},2},
	{{0xC1,0xAA},{0x00},2},
	{{0xC2,0x65},{0x00},2},
	{{0xC3,0x74},{0x00},2},
	{{0xC4,0x47},{0x00},2},
	{{0xC5,0x56},{0x00},2},
	{{0xC6,0x00},{0x00},2},
	{{0xC7,0x88},{0x00},2},
	{{0xC8,0x99},{0x00},2},
	{{0xC9,0x33},{0x00},2},
	{{0xD0,0x21},{0x00},2},
	{{0xD1,0xAA},{0x00},2},
	{{0xD2,0x65},{0x00},2},
	{{0xD3,0x74},{0x00},2},
	{{0xD4,0x47},{0x00},2},
	{{0xD5,0x56},{0x00},2},
	{{0xD6,0x00},{0x00},2},
	{{0xD7,0x88},{0x00},2},
	{{0xD8,0x99},{0x00},2},
	{{0xD9,0x33},{0x00},2},
	{{0xF3,0x01},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0xF0,0x01},{0x00},2},
	{{0xF1,0x01},{0x00},2},
	{{0xA0,0x0B},{0x00},2},
	{{0xA3,0x2A},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2B},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2C},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2D},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2E},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2F},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x30},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x31},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x32},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x33},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA0,0x09},{0x00},2},
	{{0xF1,0x10},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0x2A,0x00,0x00,0x01,0x67},{0x00},5},
	{{0x2B,0x00,0x00,0x01,0x68},{0x00},5},
	{{0x4D,0x00},{0x00},2},
	{{0x4E,0x00},{0x00},2},
	{{0x4F,0x00},{0x00},2},
	{{0x4C,0x01},{0x00},2},
	{{0x4C,0x00},{0x00},2},
	{{0x2A,0x00,0x00,0x01,0x67},{0x00},5},
	{{0x2B,0x00,0x00,0x01,0x67},{0x00},5},
	{{0x21},{0x00},1},
	{{0x11},{0x00},1},
	{{0xff},{0x00},0},
	{{0x29},{0x00},1},
	{{0x2c},{0x00},1}
};

/*************************************************
 * reg rw functions
 ************************************************/
#define DMA_BASE_ADRR  0x40000000
#define DDR_BASE_ADDR  0x10000000

void wait_for_idle()
{
	u32 tmp;
	while(1){
		tmp=Xil_In32(DMA_BASE_ADRR+04);
		if(((tmp>>1)&0x1) == 1){
			break;
		}
	}
}

void dma_xfer(u32 mem_addr, u32 length)
{/*用于初始化阶段,需要刷新Cache,确保初始化成功*/
	//Xil_DCacheFlush();
	Xil_DCacheFlushRange(mem_addr, length);


	Xil_Out32(DMA_BASE_ADRR,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x30,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x18,  mem_addr);
	Xil_Out32(DMA_BASE_ADRR+0x28,  length);	 /*length is bytes*/
	wait_for_idle();
	//usleep(10);
}

void dma_xfer2(u32 mem_addr, u32 length)
{/*发送数据阶段,可以不用Cache*/
	//Xil_DCacheFlush();
	Xil_DCacheFlushRange(mem_addr, length);

	Xil_Out32(DMA_BASE_ADRR,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x30,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x18,  mem_addr);
	Xil_Out32(DMA_BASE_ADRR+0x28,  length);	 /*length is bytes*/
	wait_for_idle();
	//usleep(10);
}

void set_cfg(u32 mem_addr, u8 cmd, u8 addr, u8 mode, u8 work_mode, u16 length)
{
	u32 tmp=cmd;
	tmp|=addr<<8;
	tmp|=mode<<16;
	tmp|=length<<20;
//	tmp|=work_mode<<30;
	Xil_Out32(mem_addr,  tmp);
}

u32 get_cfg(u8 cmd, u8 addr, u8 mode, u8 work_mode, u16 length)
{
	u32 tmp=cmd;
	tmp|=addr<<8;
	tmp|=mode<<16;
	tmp|=length<<20;
//	tmp|=work_mode<<30;
	return tmp;
}

void set_npdata8(u32 mem_addr, u8 *pdata, u32 length){
	u32 addr=mem_addr;
	while(length--){
		Xil_Out8(addr++, *pdata++);
	}
}

/*************************************************
 * basic xfer functions
 ************************************************/
void xfer_cmd_pdata8(u32 mem_addr, u8 cmd, u8* pdata, u32 length){
	if(length==0)
		set_cfg(mem_addr, 0x02, cmd, 0b0101, 0b00, length);  /*byte_mode=0, cs_mode=1, data_mode=0b01*/
	else
		set_cfg(mem_addr, 0x02, cmd, 0b0110, 0b00, length); /*byte_mode=0, cs_mode=1, data_mode=0b10*/

	set_npdata8(mem_addr+4, pdata,length);
	dma_xfer(mem_addr, 4+length);
}


void xfer_cmd_pdata16_dma(u32 mem_addr, u32 length){
	dma_xfer2(mem_addr, length);
}


void seqs_init() {

	for (int i = 0; i < sizeof(WriteBuffer) / sizeof(*WriteBuffer); i++) {
		if (WriteBuffer[i].length == 0) {
			usleep(WriteBuffer[i].txbuf[0] * 1000);
			continue;
		}

		u8 cmd=WriteBuffer[i].txbuf[0];
		u8 *pdata=WriteBuffer[i].txbuf+1;
		u16 length=WriteBuffer[i].length-1;

		xfer_cmd_pdata8(DDR_BASE_ADDR, cmd, pdata, length);
		usleep(10); //this delay is need
		//xil_printf("Exec CMD=%x\r\n\r\n", cmd);
	}
}

void xfer_win(u16 xstart, u16 ystart, u16 width, u16 height)
{
    u8 cmd_list[2][5]={
    		{0x2a, xstart>>8, xstart&0xff, (xstart+width-1)>>8, (xstart+width-1)&0xff},
    		{0x2b, ystart>>8, ystart&0xff, (ystart+height-1)>>8, (ystart+height-1)&0xff},
    };

    xfer_cmd_pdata8(DDR_BASE_ADDR, cmd_list[0][0], &(cmd_list[0][1]),4);
    xfer_cmd_pdata8(DDR_BASE_ADDR, cmd_list[1][0], &(cmd_list[1][1]),4);
}

//void xfer_color(u16 color, u16 width, u16 height){
//	u32 length=width*height;
//	xfer_cmd_cdata16(0x2C, color, length);
//}

/*************************************************
 * application functions
 ************************************************/

#define LCD_BPP             (16)

#define LCD_X_SIZE          (360U)                      /* available x pixel size */
#define LCD_Y_SIZE          (360U)		                /* available y pixle size */
#define LCD_PBYTE           ((LCD_BPP + 0) / 8)         /* bytes in pixel unit */
#define LCD_HBYTE           (LCD_X_SIZE * LCD_PBYTE)    /* bytes in horizontal line */

#define LCD_VSW             (1U)
#define LCD_HFP             (8U)
#define LCD_HBP             (8U)

#define LCD_TE_OFT          (25U)


typedef u8 (*ArrayPtr)[360][12*64];
ArrayPtr ptr1=(ArrayPtr)0x10100000;
ArrayPtr ptr2=(ArrayPtr)0x10200000;
ArrayPtr ptr3=(ArrayPtr)0x10300000;
ArrayPtr ptr4=(ArrayPtr)0x10400000;
ArrayPtr ptr5=(ArrayPtr)0x10500000;
ArrayPtr ptr6=(ArrayPtr)0x10600000;
ArrayPtr ptr7=(ArrayPtr)0x10700000;
ArrayPtr ptr8=(ArrayPtr)0x10800000;
ArrayPtr ptr9=(ArrayPtr)0x10900000;

void fill_data(ArrayPtr ptr, u8 r, u8 g, u8 b) {
    u32 cmd1 = get_cfg(0x32, 0x2c, 0b0011, 0b00, 60);
    u32 cmd2 = get_cfg(0x32, 0x2c, 0b0111, 0b00, 60);
    u32 cmd3 = get_cfg(0x32, 0x3c, 0b0011, 0b00, 60);
    u32 cmd4 = get_cfg(0x32, 0x3c, 0b0111, 0b00, 60);

    // 遍历数组的每一行
    for (int i = 0; i < 360; ++i) {
        // 遍历每一行的每一个packet
        for (int j = 0; j < 12; ++j) {
            // 计算当前packet的起始地址
        	u8 *packet = (u8 *)(ptr + i) + j * 64; // ptr指向整个二维数组,ptr+i指向第i行,然后加上j个packet的偏移

            // 填充cmd部分
            u32 *cmd_ptr = (u32 *)packet;
            if(i==0){
            	*cmd_ptr = (j == 11) ? cmd2 : cmd1; // 第一行,第12个packet使用cmd2,其余使用cmd1
            }
            else{
            	*cmd_ptr = (j == 11) ? cmd4 : cmd3; // 其余行,第12个packet使用cmd4,其余使用cmd3
            }

            // 移动到color数据部分的起始地址
            packet += sizeof(cmd1);

            // 填充color数据部分
            for (int k = 0; k < 60; k += 2) {
//            	  u16 tmp=((b&0x1f)<<11)+((r&0x3f)<<5)+(g&0x1f);
//                packet[k] = tmp&0xff;
//                packet[k + 1] = tmp>>8;

            	//注意我们的qspi_ip目前不满足rgb565的顺序,因此需要手动进行转换
            	u16 tmp=(((r>>3)&0x1f)<<11)+(((g>>2)&0x3f)<<5)+((b>>3)&0x1f);
                packet[k+1] = tmp&0xff;
                packet[k] = tmp>>8;
            }
        }
    }
}

void fill_color()
{
	for(int i=0; i<9; i++){
			switch(i){
			case 0: fill_data(ptr1, 0xff,0x00,0x00); break;
			case 1: fill_data(ptr2, 0x00,0xff,0x00); break;
			case 2: fill_data(ptr3, 0x00,0x00,0xff); break;
			case 3: fill_data(ptr4, 0xff,0xff,0x00); break;
			case 4: fill_data(ptr5, 0xff,0x00,0xff); break;
			case 5: fill_data(ptr6, 0x00,0xff,0xff); break;
			case 6: fill_data(ptr7, 0xff,0xff,0xff); break;
			case 7: fill_data(ptr8, 0x00,0x00,0x00); break;
			case 8: fill_data(ptr8, 0x80,0x80,0x80); break;
			}
	}
}

ArrayPtr get_color_ptr(u32 index)
{
	ArrayPtr tmp;
	switch(index){
		case 0: tmp=ptr1; break;
		case 1: tmp=ptr2; break;
		case 2: tmp=ptr3; break;
		case 3: tmp=ptr4; break;
		case 4: tmp=ptr5; break;
		case 5: tmp=ptr6; break;
		case 6: tmp=ptr7; break;
		case 7: tmp=ptr8; break;
		case 8: tmp=ptr9; break;
	}

	return tmp;
}

void dram_eight_colors()
{
	u32 loop_cnt=0;
	u8 color_index=0;
	fill_color();

    while (1)
    {
		ArrayPtr my_ptr=get_color_ptr(color_index);
		xfer_win(0,0,360,360);
		for (int i = 0; i < LCD_Y_SIZE; i+=1)
		{
		   xfer_cmd_pdata16_dma((u32)my_ptr[i],12*64);
		   //usleep(15);
		}
        loop_cnt++;
        if(loop_cnt%60==0){
			color_index++;
			if(color_index==8)
				color_index=0;
        }
    }

}




int main()
{
    init_platform();
    //Xil_DCacheDisable();

    print("Hello World\n\r");

    gpio_init();
    qspi_mode_set(0);
	seqs_init();
	dram_eight_colors();

    //draw_bmp();
    //draw_movie();

    cleanup_platform();
    return 0;
}

显示图片程序

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xparameters.h"
#include "xil_io.h"
#include "sleep.h"
#include "ff.h"
#include "xil_cache.h"
#include "xtime_l.h"
#include <string.h>
#include "xgpiops.h"


#define PS_KEY  (54 + 0)    //PS_KEY为EMIO0,对应的GPIO编号为54+0=54
#define INPUT		1
#define OUTPUT		0
XGpioPs GpioPs;	//GPIO实例对象
void PS_GPIO_Init()
{
	XGpioPs_Config *ConfigPtr;
	ConfigPtr = XGpioPs_LookupConfig(XPAR_XGPIOPS_0_DEVICE_ID);
	XGpioPs_CfgInitialize(&GpioPs, ConfigPtr, ConfigPtr->BaseAddr);
}

/**
  *****************************************************
  * @brief	初始化特定GPIO口的模式与状态
  * @param	GPIO_Num	GPIO编号(MIO为0~53,EMIO从54开始)
  * @param	Dir			输入/输出:OUTPUT为输出,INPUT为输入
  * @param	Data		输出电平高低:0为低,1为高(若设置为输入则此处数据无影响)
  * @usage	//设置GPIO46为输出模式,输出为高电平
  * 		GPIO_SetMode(46, OUTPUT, 1);
  *****************************************************
**/
void PS_GPIO_SetMode(uint8_t GPIO_Num, uint8_t Dir, uint8_t Data)
{
	if(Dir == INPUT) {
		XGpioPs_SetDirectionPin(&GpioPs, GPIO_Num, 0);
	} else if(Dir == OUTPUT){
		XGpioPs_SetDirectionPin(&GpioPs, GPIO_Num, 1);
		XGpioPs_SetOutputEnablePin(&GpioPs, GPIO_Num, 1);
		XGpioPs_WritePin(&GpioPs, GPIO_Num, Data);
	}
}

/**
  *****************************************************
  * @brief	控制特定GPIO口的输出高/低电平,使用前必须先用GPIO_SetMode设置该GPIO口为"输出"模式
  * @param	GPIO_Num	GPIO编号(MIO为0~53,EMIO从54开始)
  * @param	Data		输出电平高低:0为低,1为高
  * @usage	//控制GPIO46输出高电平
  * 		GPIO_SetPort(46, 1);
  *****************************************************
**/
void PS_GPIO_SetPort(uint8_t GPIO_Num, uint8_t Data)
{
	XGpioPs_WritePin(&GpioPs, GPIO_Num, Data);	//输出高/低电平,0为低,1为高
}

/**
  *****************************************************
  * @brief	读取特定GPIO口的输入电平,使用前必须先用GPIO_SetMode设置该GPIO口为"输入"模式
  * @param	GPIO_Num	GPIO编号(MIO为0~53,EMIO从54开始)
  * @return	返回采集的GPIO电平值,0为低,1为高
  * @usage	//采集GPIO46的电平,存放在state
  * 		state = GPIO_GetPort(46, state);
  *****************************************************
**/
uint8_t PS_GPIO_GetPort(uint8_t GPIO_Num)
{
	return XGpioPs_ReadPin(&GpioPs, GPIO_Num);		//输出高/低电平,0为低,1为高
}


//void qspi_mode_set(u8 mode)
//{
//    PS_GPIO_Init(); //初始化PS端MIO和EMIO
//    PS_GPIO_SetMode(PS_KEY, OUTPUT, mode);
//}

void gpio_init()
{
    PS_GPIO_Init(); //初始化PS端MIO和EMIO
    PS_GPIO_SetMode(PS_KEY, OUTPUT, 0);
}

void qspi_mode_set(u8 mode)
{
	PS_GPIO_SetPort(PS_KEY, mode);
}
/***********************************************************************************/



#define  RED        0xF800
#define  ORANGE     0xFC00
#define  YELLOW     0xFFE0
#define  GREEN      0x07E0
#define  CYAN       0x07FF
#define  BLUE       0x001F
#define  PURPPLE    0xF81F
#define  BLACK      0x0000
#define  WHITE      0xFFFF
#define  GRAY       0xD69A

const u16 colors[] = {
	RED,
	GREEN,
	BLUE,
	ORANGE,
	YELLOW,
	CYAN,
	PURPPLE,
	BLACK,
	WHITE,
	GRAY
};

typedef struct{
	u8 txbuf[50];
	u8 rxbuf[50];
	u8 length;
}t_buf;

//#define BIST_MODE
//t_buf WriteBuffer[]={
//		{{0xf0, 0xc3}, {0x00},2},
//		{{0xf0, 0x96}, {0x00},2},
//		{{0xf0, 0xa5}, {0x00},2},
//		{{0xe9, 0x20}, {0x00},2},
//		{{0xe7,  0x80, 0x77, 0x1f, 0xcc}, {0x00}, 5},
//		{{0xc1,  0x77, 0x07, 0xcf, 0x16}, {0x00},5},//Normal:VGHS/VGLS/VSP/VSN电压
//		{{0xc2,  0x77, 0x07, 0xcf, 0x16}, {0x00},5},//Idle: VGHS/VGLS/VSP/VSN电压
//		{{0xc3,  0x22, 0x02, 0x22, 0x04}, {0x00},5},//Normal: VGH/VGL/VSP/VSN Clk
//		{{0xc4,  0x22, 0x02, 0x22, 0x04}, {0x00},5},//Idle: VGH/VGL/VSP/VSN Clk
//		{{0xc5,  0xed},{0x00},2}, //VCOM
//		{{0xe0, 0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b, 0x17, 0x2a, 0x2f},{0x00},15},
//		{{0xe1, 0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b, 0x17, 0x2a, 0x2f},{0x00},15},
//		{{0xe5, 0xbe, 0xf5, 0xb1, 0x22, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22},{0x00},15},
//		{{0xe6, 0xbe, 0xf5, 0xb1, 0x22, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22},{0x00},15},
//		{{0xec, 0x40, 0x03},{0x00},3},
//		//{{0x36,  0x0c},{0x00},2},
//		{{0x36,  0x04},{0x00},2},
//		{{0x3a,  0x07},{0x00},2},
//		{{0xb2,  0x00},{0x00},2},//GIP pattern
//		{{0xb3,  0x01},{0x00},2},//video mode dot-inversion
//		{{0xb4,  0x01},{0x00},2},
//		{{0xb5,  0x00, 0x08, 0x00, 0x08},{0x00},5}, //vfp
//		{{0xb6,  0xc7, 0x31},{0x00},3},
//		{{0xa5,  0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x2a, 0x8a, 0x02},{0x00},10},
//		{{0xa6,  0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x2a, 0x8a, 0x02},{0x00},10},
//		{{0xba,  0x0a, 0x5a, 0x23, 0x10, 0x25, 0x02, 0x00},{0x00},8},
//		{{0xbb,  0x00, 0x30, 0x00, 0x2c, 0x82, 0x87, 0x18, 0x00},{0x00},9},
//		{{0xbc,  0x00, 0x30, 0x00, 0x2c, 0x82, 0x87, 0x18, 0x00},{0x00},9},
//		{{0xbd, 0xa1, 0xb2, 0x2b, 0x1a, 0x56, 0x43, 0x34, 0x65, 0xff, 0xff, 0x0f},{0x00},12},
//		{{0x35,  0x00},{0x00},2},
//		{{0x21},{0x00},1},
//		{{0x11},{0x00},1},
//		{{0xff},{0x00},0},
//		{{0x29},{0x00},1},
//		{{0xff},{0x00},0},
//		#ifdef BIST_MODE
//		{{0xb0,  0xa5},{0x00},2},
//		{{0xcc,  0x40, 0x00, 0x3f, 0x00, 0x14, 0x14, 0x20, 0x20, 0x03},{0x00},10},
//		#endif
//};

t_buf WriteBuffer[]={
	{{0x3A,0x55},{0x00},2},
	{{0xF0,0x08},{0x00},2},
	{{0xF2,0x08},{0x00},2},
	{{0x9B,0x51},{0x00},2},
	{{0x86,0x53},{0x00},2},
	{{0xF2,0x80},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0xF0,0x01},{0x00},2},
	{{0xF1,0x01},{0x00},2},
	{{0xB0,0x54},{0x00},2},
	{{0xB1,0x3F},{0x00},2},
	{{0xB2,0x2A},{0x00},2},
	{{0xB4,0x46},{0x00},2},
	{{0xB5,0x34},{0x00},2},
	{{0xB6,0xD5},{0x00},2},
	{{0xB7,0x30},{0x00},2},
	{{0xBA,0x00},{0x00},2},
	{{0xBB,0x08},{0x00},2},
	{{0xBC,0x08},{0x00},2},
	{{0xBD,0x00},{0x00},2},
	{{0xC0,0x80},{0x00},2},
	{{0xC1,0x10},{0x00},2},
	{{0xC2,0x37},{0x00},2},
	{{0xC3,0x80},{0x00},2},
	{{0xC4,0x10},{0x00},2},
	{{0xC5,0x37},{0x00},2},
	{{0xC6,0xA9},{0x00},2},
	{{0xC7,0x41},{0x00},2},
	{{0xC8,0x51},{0x00},2},
	{{0xC9,0xA9},{0x00},2},
	{{0xCA,0x41},{0x00},2},
	{{0xCB,0x51},{0x00},2},
	{{0xD0,0x91},{0x00},2},
	{{0xD1,0x68},{0x00},2},
	{{0xD2,0x69},{0x00},2},
	{{0xF5,0x00,0xA5},{0x00},3},
	{{0xDD,0x3F},{0x00},2},
	{{0xDE,0x3F},{0x00},2},
	{{0xF1,0x10},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0xF0,0x02},{0x00},2},
	{{0xE0,0xF0,0x06,0x0B,0x09,0x09,0x16,0x32,0x44,0x4A,0x37,0x13,0x13,0x2E,0x34},{0x00},15},
	{{0xE1,0xF0,0x06,0x0B,0x09,0x08,0x05,0x32,0x33,0x49,0x17,0x13,0x13,0x2E,0x34},{0x00},15},
	{{0xF0,0x10},{0x00},2},
	{{0xF3,0x10},{0x00},2},
	{{0xE0,0x0A},{0x00},2},
	{{0xE1,0x00},{0x00},2},
	{{0xE2,0x00},{0x00},2},
	{{0xE3,0x00},{0x00},2},
	{{0xE4,0xE0},{0x00},2},
	{{0xE5,0x06},{0x00},2},
	{{0xE6,0x21},{0x00},2},
	{{0xE7,0x00},{0x00},2},
	{{0xE8,0x05},{0x00},2},
	{{0xE9,0x82},{0x00},2},
	{{0xEA,0xDF},{0x00},2},
	{{0xEB,0x89},{0x00},2},
	{{0xEC,0x20},{0x00},2},
	{{0xED,0x14},{0x00},2},
	{{0xEE,0xFF},{0x00},2},
	{{0xEF,0x00},{0x00},2},
	{{0xF8,0xFF},{0x00},2},
	{{0xF9,0x00},{0x00},2},
	{{0xFA,0x00},{0x00},2},
	{{0xFB,0x30},{0x00},2},
	{{0xFC,0x00},{0x00},2},
	{{0xFD,0x00},{0x00},2},
	{{0xFE,0x00},{0x00},2},
	{{0xFF,0x00},{0x00},2},
	{{0x60,0x42},{0x00},2},
	{{0x61,0xE0},{0x00},2},
	{{0x62,0x40},{0x00},2},
	{{0x63,0x40},{0x00},2},
	{{0x64,0x02},{0x00},2},
	{{0x65,0x00},{0x00},2},
	{{0x66,0x40},{0x00},2},
	{{0x67,0x03},{0x00},2},
	{{0x68,0x00},{0x00},2},
	{{0x69,0x00},{0x00},2},
	{{0x6A,0x00},{0x00},2},
	{{0x6B,0x00},{0x00},2},
	{{0x70,0x42},{0x00},2},
	{{0x71,0xE0},{0x00},2},
	{{0x72,0x40},{0x00},2},
	{{0x73,0x40},{0x00},2},
	{{0x74,0x02},{0x00},2},
	{{0x75,0x00},{0x00},2},
	{{0x76,0x40},{0x00},2},
	{{0x77,0x03},{0x00},2},
	{{0x78,0x00},{0x00},2},
	{{0x79,0x00},{0x00},2},
	{{0x7A,0x00},{0x00},2},
	{{0x7B,0x00},{0x00},2},
	{{0x80,0x48},{0x00},2},
	{{0x81,0x00},{0x00},2},
	{{0x82,0x05},{0x00},2},
	{{0x83,0x02},{0x00},2},
	{{0x84,0xDD},{0x00},2},
	{{0x85,0x00},{0x00},2},
	{{0x86,0x00},{0x00},2},
	{{0x87,0x00},{0x00},2},
	{{0x88,0x48},{0x00},2},
	{{0x89,0x00},{0x00},2},
	{{0x8A,0x07},{0x00},2},
	{{0x8B,0x02},{0x00},2},
	{{0x8C,0xDF},{0x00},2},
	{{0x8D,0x00},{0x00},2},
	{{0x8E,0x00},{0x00},2},
	{{0x8F,0x00},{0x00},2},
	{{0x90,0x48},{0x00},2},
	{{0x91,0x00},{0x00},2},
	{{0x92,0x09},{0x00},2},
	{{0x93,0x02},{0x00},2},
	{{0x94,0xE1},{0x00},2},
	{{0x95,0x00},{0x00},2},
	{{0x96,0x00},{0x00},2},
	{{0x97,0x00},{0x00},2},
	{{0x98,0x48},{0x00},2},
	{{0x99,0x00},{0x00},2},
	{{0x9A,0x0B},{0x00},2},
	{{0x9B,0x02},{0x00},2},
	{{0x9C,0xE3},{0x00},2},
	{{0x9D,0x00},{0x00},2},
	{{0x9E,0x00},{0x00},2},
	{{0x9F,0x00},{0x00},2},
	{{0xA0,0x48},{0x00},2},
	{{0xA1,0x00},{0x00},2},
	{{0xA2,0x04},{0x00},2},
	{{0xA3,0x02},{0x00},2},
	{{0xA4,0xDC},{0x00},2},
	{{0xA5,0x00},{0x00},2},
	{{0xA6,0x00},{0x00},2},
	{{0xA7,0x00},{0x00},2},
	{{0xA8,0x48},{0x00},2},
	{{0xA9,0x00},{0x00},2},
	{{0xAA,0x06},{0x00},2},
	{{0xAB,0x02},{0x00},2},
	{{0xAC,0xDE},{0x00},2},
	{{0xAD,0x00},{0x00},2},
	{{0xAE,0x00},{0x00},2},
	{{0xAF,0x00},{0x00},2},
	{{0xB0,0x48},{0x00},2},
	{{0xB1,0x00},{0x00},2},
	{{0xB2,0x08},{0x00},2},
	{{0xB3,0x02},{0x00},2},
	{{0xB4,0xE0},{0x00},2},
	{{0xB5,0x00},{0x00},2},
	{{0xB6,0x00},{0x00},2},
	{{0xB7,0x00},{0x00},2},
	{{0xB8,0x48},{0x00},2},
	{{0xB9,0x00},{0x00},2},
	{{0xBA,0x0A},{0x00},2},
	{{0xBB,0x02},{0x00},2},
	{{0xBC,0xE2},{0x00},2},
	{{0xBD,0x00},{0x00},2},
	{{0xBE,0x00},{0x00},2},
	{{0xBF,0x00},{0x00},2},
	{{0xC0,0x12},{0x00},2},
	{{0xC1,0xAA},{0x00},2},
	{{0xC2,0x65},{0x00},2},
	{{0xC3,0x74},{0x00},2},
	{{0xC4,0x47},{0x00},2},
	{{0xC5,0x56},{0x00},2},
	{{0xC6,0x00},{0x00},2},
	{{0xC7,0x88},{0x00},2},
	{{0xC8,0x99},{0x00},2},
	{{0xC9,0x33},{0x00},2},
	{{0xD0,0x21},{0x00},2},
	{{0xD1,0xAA},{0x00},2},
	{{0xD2,0x65},{0x00},2},
	{{0xD3,0x74},{0x00},2},
	{{0xD4,0x47},{0x00},2},
	{{0xD5,0x56},{0x00},2},
	{{0xD6,0x00},{0x00},2},
	{{0xD7,0x88},{0x00},2},
	{{0xD8,0x99},{0x00},2},
	{{0xD9,0x33},{0x00},2},
	{{0xF3,0x01},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0xF0,0x01},{0x00},2},
	{{0xF1,0x01},{0x00},2},
	{{0xA0,0x0B},{0x00},2},
	{{0xA3,0x2A},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2B},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2C},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2D},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2E},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2F},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x30},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x31},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x32},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x33},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA0,0x09},{0x00},2},
	{{0xF1,0x10},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0x2A,0x00,0x00,0x01,0x67},{0x00},5},
	{{0x2B,0x00,0x00,0x01,0x68},{0x00},5},
	{{0x4D,0x00},{0x00},2},
	{{0x4E,0x00},{0x00},2},
	{{0x4F,0x00},{0x00},2},
	{{0x4C,0x01},{0x00},2},
	{{0x4C,0x00},{0x00},2},
	{{0x2A,0x00,0x00,0x01,0x67},{0x00},5},
	{{0x2B,0x00,0x00,0x01,0x67},{0x00},5},
	{{0x21},{0x00},1},
	{{0x11},{0x00},1},
	{{0xff},{0x00},0},
	{{0x29},{0x00},1},
	{{0x2c},{0x00},1},
};

/*************************************************
 * reg rw functions
 ************************************************/
#define DMA_BASE_ADRR  0x40000000
#define DDR_BASE_ADDR  0x10000000

void wait_for_idle()
{
	u32 tmp;
	while(1){
		tmp=Xil_In32(DMA_BASE_ADRR+04);
		if(((tmp>>1)&0x1) == 1){
			break;
		}
	}
}

void dma_xfer(u32 mem_addr, u32 length)
{/*用于初始化阶段,需要刷新Cache,确保初始化成功*/
	//Xil_DCacheFlush();
	Xil_DCacheFlushRange(mem_addr, length);

	Xil_Out32(DMA_BASE_ADRR,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x30,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x18,  mem_addr);
	Xil_Out32(DMA_BASE_ADRR+0x28,  length);	 /*length is bytes*/
	//wait_for_idle();
	//usleep(10);
}

void dma_xfer2(u32 mem_addr, u32 length)
{/*发送数据阶段,可以不用Cache*/
	//Xil_DCacheFlush();
	Xil_DCacheFlushRange(mem_addr, length);

	Xil_Out32(DMA_BASE_ADRR,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x30,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x18,  mem_addr);
	Xil_Out32(DMA_BASE_ADRR+0x28,  length);	 /*length is bytes*/
	wait_for_idle();
	//usleep(10);
}

void set_cfg(u32 mem_addr, u8 cmd, u8 addr, u8 mode, u8 work_mode, u16 length)
{
	u32 tmp=cmd;
	tmp|=addr<<8;
	tmp|=mode<<16;
	tmp|=length<<20;
	tmp|=work_mode<<30;
	Xil_Out32(mem_addr,  tmp);
}

u32 get_cfg(u8 cmd, u8 addr, u8 mode, u8 work_mode, u16 length)
{
	u32 tmp=cmd;
	tmp|=addr<<8;
	tmp|=mode<<16;
	tmp|=length<<20;
	tmp|=work_mode<<30;
	return tmp;
}


void set_npdata8(u32 mem_addr, u8 *pdata, u32 length){
	u32 addr=mem_addr;
	while(length--){
		Xil_Out8(addr++, *pdata++);
	}
}


/*************************************************
 * basic xfer functions
 ************************************************/
void xfer_cmd_pdata8(u32 mem_addr, u8 cmd, u8* pdata, u32 length){
	if(length==0)
		set_cfg(mem_addr, 0x02, cmd, 0b0101,  0b00, length);  /*byte_mode=0, cs_mode=1, data_mode=0b01*/
	else
		set_cfg(mem_addr, 0x02, cmd, 0b0110,  0b00, length); /*byte_mode=0, cs_mode=1, data_mode=0b10*/

	set_npdata8(mem_addr+4, pdata,length);
	dma_xfer(mem_addr, 4+length);
}


void xfer_cmd_pdata16_dma(u32 mem_addr, u32 length){
	//dma_xfer(mem_addr, 20*64);
	dma_xfer2(mem_addr, length);
}


void seqs_init() {

	for (int i = 0; i < sizeof(WriteBuffer) / sizeof(*WriteBuffer); i++) {
		if (WriteBuffer[i].length == 0) {
			usleep(WriteBuffer[i].txbuf[0] * 1000);
			continue;
		}

		u8 cmd=WriteBuffer[i].txbuf[0];
		u8 *pdata=WriteBuffer[i].txbuf+1;
		u16 length=WriteBuffer[i].length-1;

		xfer_cmd_pdata8(DDR_BASE_ADDR, cmd, pdata, length);
		usleep(10); //this delay is need
		//xil_printf("Exec CMD=%x\r\n\r\n", cmd);
	}
}

void xfer_win(u16 xstart, u16 ystart, u16 width, u16 height)
{
    u8 cmd_list[2][5]={
    		{0x2a, xstart>>8, xstart&0xff, (xstart+width-1)>>8, (xstart+width-1)&0xff},
    		{0x2b, ystart>>8, ystart&0xff, (ystart+height-1)>>8, (ystart+height-1)&0xff},
    };

    xfer_cmd_pdata8(DDR_BASE_ADDR, cmd_list[0][0], &(cmd_list[0][1]),4);
    xfer_cmd_pdata8(DDR_BASE_ADDR, cmd_list[1][0], &(cmd_list[1][1]),4);
}

/*************************************************
 * application functions
 ************************************************/
u8 frame_src[1920*1080*3];
u16 frame_target[360*360];

void load_sd_bmp(u8 *frame, const char *bmp_name);
void convertRGB888toRGB888(
		const uint8_t *input,
		uint8_t *output,
		int inputWidth, int inputHeight,
		int startX, int startY,
		int outputWidth, int outputHeight
		) ;

void convertRGB888toRGB565(
		const uint8_t *input,
		uint16_t *output,
		int inputWidth, int inputHeight,
		int startX, int startY,
		int outputWidth, int outputHeight
		) ;


#define LCD_BPP             (16)

#define LCD_X_SIZE          (360U)                      /* available x pixel size */
#define LCD_Y_SIZE          (360U)		                /* available y pixle size */
#define LCD_PBYTE           ((LCD_BPP + 0) / 8)         /* bytes in pixel unit */
#define LCD_HBYTE           (LCD_X_SIZE * LCD_PBYTE)    /* bytes in horizontal line */

#define LCD_VSW             (1U)
#define LCD_HFP             (8U)
#define LCD_HBP             (8U)

#define LCD_TE_OFT          (25U)


typedef u8 (*ArrayPtr)[360][12 * 64];
ArrayPtr ptr1=(ArrayPtr)0x10100000;


void fill_pic(ArrayPtr ptr, u16 *pcolor) {
    u32 cmd1 = get_cfg(0x32, 0x2c, 0b0011, 0b00, 60);
    u32 cmd2 = get_cfg(0x32, 0x2c, 0b0111, 0b00, 60);
    u32 cmd3 = get_cfg(0x32, 0x3c, 0b0011, 0b00, 60);
    u32 cmd4 = get_cfg(0x32, 0x3c, 0b0111, 0b00, 60);

//    u32 cmd1 = get_cfg(0x02, 0x2c, 0b0010, 0b00, 60);
//    u32 cmd2 = get_cfg(0x02, 0x2c, 0b0110, 0b00, 60);
//    u32 cmd3 = get_cfg(0x02, 0x3c, 0b0010, 0b00, 60);
//    u32 cmd4 = get_cfg(0x02, 0x3c, 0b0110, 0b00, 60);

    // 遍历数组的每一行
    for (int i = 0; i < 360; ++i) {
        // 遍历每一行的每一个packet
        for (int j = 0; j < 12; ++j) {
            // 计算当前packet的起始地址
        	u8 *packet = (u8 *)(ptr + i) + j * 64; // ptr指向整个二维数组,ptr+i指向第i行,然后加上j个packet的偏移

            // 填充cmd部分
            u32 *cmd_ptr = (u32 *)packet;
            if(i==0){
            	*cmd_ptr = (j == 11) ? cmd2 : cmd1; // 第一行,第12个packet使用cmd2,其余使用cmd1
            }
            else{
            	*cmd_ptr = (j == 11) ? cmd4 : cmd3; // 其余行,第12个packet使用cmd4,其余使用cmd3
            }

            // 移动到color数据部分的起始地址
            packet += sizeof(cmd1);

            // 填充color数据部分
			memcpy(packet, pcolor, 60);
			pcolor+=30;
        }
    }
}

XTime t1,t2,t3,t4;
void draw_bmp()
{
//	XTime_GetTime(&t1);
	load_sd_bmp(frame_src, "shatan.bmp");	//1920x1080
//	XTime_GetTime(&t2);
	convertRGB888toRGB565(
			frame_src,
			frame_target,
			1920,1080,
			500,500,
			360,360
			);
//	XTime_GetTime(&t3);
	fill_pic(ptr1,frame_target);
//	XTime_GetTime(&t4);
//
//	XTime dt21 = ((t2-t1) * 1000000) / (COUNTS_PER_SECOND);
//	XTime dt32 = ((t3-t2) * 1000000) / (COUNTS_PER_SECOND);
//	XTime dt43 = ((t4-t3) * 1000000) / (COUNTS_PER_SECOND);
//	xil_printf("dt21=%d, dt32=%d, dt43=%d\r\n", dt21,dt32,dt43);

    while (1)
    {
		xfer_win(0,0,360,360);
		for (int i = 0; i < LCD_Y_SIZE; i+=1)
		{
		   xfer_cmd_pdata16_dma((u32)ptr1[i],12*64);
		   usleep(50);
		}
    }

}

int main()
{
    //init_platform();
    //Xil_DCacheDisable();
    print("Hello World\n\r");

    gpio_init();
    qspi_mode_set(0);
	seqs_init();
    draw_bmp();
    //draw_movie();
    return 0;
}


//从SD卡中读取BMP图片
void load_sd_bmp(u8 *frame, const char *bmp_name)
{
	static 	FATFS fatfs;
	FIL 	fil;
	u8		bmp_head[54];
	UINT 	bmp_width,bmp_height,bmp_size;
	UINT 	br;

	f_mount(&fatfs,"",1);//挂载文件系统

	f_open(&fil, bmp_name,FA_READ);	//打开文件, 注意是bmp_24bits格式
	xil_printf("open bmp\n\r");
	f_lseek(&fil,0);//移动文件读写指针到文件开头

	f_read(&fil,bmp_head,54,&br);//读取BMP文件头

	//BMP图片的分辨率和大小
	bmp_width  = *(UINT *)(bmp_head + 0x12);
	bmp_height = *(UINT *)(bmp_head + 0x16);
	bmp_size   = *(UINT *)(bmp_head + 0x22);
	xil_printf("bmp information:\n\r");
	xil_printf(" width  = %d,\n\r height = %d,\n\r size   = %d bytes \n\r",
			bmp_width,bmp_height,bmp_size);

	//读出图片,写入DDR
	f_read(&fil, frame, bmp_width*bmp_height*3,&br);
	xil_printf("br=%d\r\n", br);

	for(int i=0; i<20; i++){
		xil_printf("%x\r\n", frame[i]);
	}

	//关闭文件
	f_close(&fil);

	Xil_DCacheFlush();     //刷新Cache,将数据更新至DDR3中
	xil_printf("display bmp\n\r");
}


void convertRGB888toRGB888(const uint8_t *input, uint8_t *output, int inputWidth, int inputHeight, int startX, int startY, int outputWidth, int outputHeight)
{
    int i, j;

    for (i = 0; i < outputHeight; i++) {
        int inputIndex = ((startY + i) * inputWidth + startX + 0) * 3;
        int outputIndex=(i * outputWidth + 0)*3;
    	memcpy(&output[outputIndex], &input[inputIndex],outputWidth*3);

//        for (j = 0; j < outputWidth; j++) {
//            // Calculate the index in the RGB888 array
//            int inputIndex = ((startY + i) * inputWidth + startX + j) * 3;
//
//            // Extract RGB888 components
//            uint8_t b = input[inputIndex];
//            uint8_t g = input[inputIndex + 1];
//            uint8_t r = input[inputIndex + 2];
//
//            int outputIndex=(i * outputWidth + j)*3;
//            // Store in the output array
//            output[outputIndex] = r;
//            output[outputIndex+1] = g;
//            output[outputIndex+2] = b;
//        }
    }
}

void convertRGB888toRGB565(const uint8_t *input, uint16_t *output, int inputWidth, int inputHeight, int startX, int startY, int outputWidth, int outputHeight)
{
    int i, j;

    for (i = 0; i < outputHeight; i++) {
        for (j = 0; j < outputWidth; j++) {
            // Calculate the index in the RGB888 array
            int inputIndex = ((startY + i) * inputWidth + startX + j) * 3;

            // Extract RGB888 components
            uint8_t b = input[inputIndex];
            uint8_t g = input[inputIndex + 1];
            uint8_t r = input[inputIndex + 2];

            // Convert to RGB565 format
            uint16_t rgb565 = ((r>>3) << 11) | ((g>>2) << 5) | (b>>3);
            uint16_t rgb565_new=((rgb565&0xff)<<8)+(rgb565>>8);  //注意我们的qspi_ip的发送顺序,跟rgb565不匹配,要手动调整顺序

            // Store in the output array
            output[i * outputWidth + j] = rgb565_new;
        }
    }
}

显示动画程序(等待te信号发图像数据,与te同步)

  • 本来想利用te的中断,结果发现好像不行。开启te中断后,程序就卡死了,原因不清楚。于是我们用等待te信号高电平的方式来决定发数据,一样的实现了同步。
  • 另外发现,printf函数执行时间非常长,有40-50ms。所以主循环中一定不能使用printf。
#include <stdio.h>
#include "xil_printf.h"
#include "xparameters.h"
#include "xil_io.h"
#include "sleep.h"
#include "ff.h"
#include "xil_cache.h"
#include "xtime_l.h"
#include <string.h>
#include "xgpiops.h"
#include "xiao_mei_ge/COMMON.h"


#define PS_KEY  (54 + 0)  //EMIO0,对应的GPIO编号为54+0=54
#define PS_EMIO_KEY (54+1)
#define FULL_PIN  (54 + 1)    //EMIO1,对应的GPIO编号为54+1=55
#define EMPTY_PIN  (54 + 2)    //EMIO2,对应的GPIO编号为54+2=56

#define INPUT		1
#define OUTPUT		0
XGpioPs GpioPs;	//GPIO实例对象

void gpio_init()
{
    PS_GPIO_Init(); //初始化PS端MIO和EMIO
    PS_GPIO_SetMode(PS_KEY, OUTPUT, 0);
//    PS_GPIO_SetMode(FULL_PIN, INPUT, 0);
//    PS_GPIO_SetMode(EMPTY_PIN, INPUT, 1);
}

void qspi_mode_set(u8 mode)
{
	PS_GPIO_SetPort(PS_KEY, mode);
}

u32 read_full()
{
	return PS_GPIO_GetPort(FULL_PIN);
}

u32 read_empty()
{
	return PS_GPIO_GetPort(EMPTY_PIN);
}

u32 read_te()
{
	return PS_GPIO_GetPort(PS_EMIO_KEY);
}


u32 Refresh_flag=0;
void PS_GPIO_IRQ_Handler(void *CallBackRef, uint32_t Bank, uint32_t Status)
{
    if(XGpioPs_IntrGetStatusPin(&GpioPs, PS_EMIO_KEY)) {
    	//printf("ps_emio_key pressed!\r\n");
    	Refresh_flag=1;
    	XGpioPs_IntrClearPin(&GpioPs, PS_EMIO_KEY);

    }
}



int main_gpio_irq(void)
{
	//GIC通用中断控制器初始化
	ScuGic_Init();

	{
		//初始化PS端GPIO
		//PS_GPIO_Init();

		//设置PS_KEY(MIO47)方向为输入
		PS_GPIO_SetMode(PS_EMIO_KEY, INPUT, 1);
		//设置按键PS_KEY的中断为边沿敏感
		PS_GPIO_SetInt(PS_EMIO_KEY,XGPIOPS_IRQ_TYPE_EDGE_RISING);

		//初始化PS GPIO中断
		//PS_GPIO_Int_Init(PS_GPIO_IRQ_Handler);   //说明,打开中断后,会卡死。原因不清楚。
	}

	return 0;
}

/***********************************************************************************/


#define  RED        0xF800
#define  ORANGE     0xFC00
#define  YELLOW     0xFFE0
#define  GREEN      0x07E0
#define  CYAN       0x07FF
#define  BLUE       0x001F
#define  PURPPLE    0xF81F
#define  BLACK      0x0000
#define  WHITE      0xFFFF
#define  GRAY       0xD69A

const u16 colors[] = {
	RED,
	GREEN,
	BLUE,
	ORANGE,
	YELLOW,
	CYAN,
	PURPPLE,
	BLACK,
	WHITE,
	GRAY
};

typedef struct{
	u8 txbuf[50];
	u8 rxbuf[50];
	u8 length;
}t_buf;

//#define BIST_MODE
//t_buf WriteBuffer[]={
//		{{0xf0, 0xc3}, {0x00},2},
//		{{0xf0, 0x96}, {0x00},2},
//		{{0xf0, 0xa5}, {0x00},2},
//		{{0xe9, 0x20}, {0x00},2},
//		{{0xe7,  0x80, 0x77, 0x1f, 0xcc}, {0x00}, 5},
//		{{0xc1,  0x77, 0x07, 0xcf, 0x16}, {0x00},5},//Normal:VGHS/VGLS/VSP/VSN电压
//		{{0xc2,  0x77, 0x07, 0xcf, 0x16}, {0x00},5},//Idle: VGHS/VGLS/VSP/VSN电压
//		{{0xc3,  0x22, 0x02, 0x22, 0x04}, {0x00},5},//Normal: VGH/VGL/VSP/VSN Clk
//		{{0xc4,  0x22, 0x02, 0x22, 0x04}, {0x00},5},//Idle: VGH/VGL/VSP/VSN Clk
//		{{0xc5,  0xed},{0x00},2}, //VCOM
//		{{0xe0, 0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b, 0x17, 0x2a, 0x2f},{0x00},15},
//		{{0xe1, 0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b, 0x17, 0x2a, 0x2f},{0x00},15},
//		{{0xe5, 0xbe, 0xf5, 0xb1, 0x22, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22},{0x00},15},
//		{{0xe6, 0xbe, 0xf5, 0xb1, 0x22, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22},{0x00},15},
//		{{0xec, 0x40, 0x03},{0x00},3},
//		//{{0x36,  0x0c},{0x00},2},
//		{{0x36,  0x04},{0x00},2},
//		{{0x3a,  0x07},{0x00},2},
//		{{0xb2,  0x00},{0x00},2},//GIP pattern
//		{{0xb3,  0x01},{0x00},2},//video mode dot-inversion
//		{{0xb4,  0x01},{0x00},2},
//		{{0xb5,  0x00, 0x08, 0x00, 0x08},{0x00},5}, //vfp
//		{{0xb6,  0xc7, 0x31},{0x00},3},
//		{{0xa5,  0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x2a, 0x8a, 0x02},{0x00},10},
//		{{0xa6,  0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x2a, 0x8a, 0x02},{0x00},10},
//		{{0xba,  0x0a, 0x5a, 0x23, 0x10, 0x25, 0x02, 0x00},{0x00},8},
//		{{0xbb,  0x00, 0x30, 0x00, 0x2c, 0x82, 0x87, 0x18, 0x00},{0x00},9},
//		{{0xbc,  0x00, 0x30, 0x00, 0x2c, 0x82, 0x87, 0x18, 0x00},{0x00},9},
//		{{0xbd, 0xa1, 0xb2, 0x2b, 0x1a, 0x56, 0x43, 0x34, 0x65, 0xff, 0xff, 0x0f},{0x00},12},
//		{{0x35,  0x00},{0x00},2},
//		{{0x21},{0x00},1},
//		{{0x11},{0x00},1},
//		{{0xff},{0x00},0},
//		{{0x29},{0x00},1},
//		{{0xff},{0x00},0},
//		#ifdef BIST_MODE
//		{{0xb0,  0xa5},{0x00},2},
//		{{0xcc,  0x40, 0x00, 0x3f, 0x00, 0x14, 0x14, 0x20, 0x20, 0x03},{0x00},10},
//		#endif
//};

t_buf WriteBuffer[]={
	{{0x35,0x00},{0x00},2}, //set TE=ON, vs-only
	{{0x44,0x00,100},{0x00},3}, //set TE_Lines

	{{0x3A,0x55},{0x00},2},
	{{0xF0,0x08},{0x00},2},
	{{0xF2,0x08},{0x00},2},
	{{0x9B,0x51},{0x00},2},
	{{0x86,0x53},{0x00},2},
	{{0xF2,0x80},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0xF0,0x01},{0x00},2},
	{{0xF1,0x01},{0x00},2},
	{{0xB0,0x54},{0x00},2},
	{{0xB1,0x3F},{0x00},2},
	{{0xB2,0x2A},{0x00},2},
	{{0xB4,0x46},{0x00},2},
	{{0xB5,0x34},{0x00},2},
	{{0xB6,0xD5},{0x00},2},
	{{0xB7,0x30},{0x00},2},
	{{0xBA,0x00},{0x00},2},
	{{0xBB,0x08},{0x00},2},
	{{0xBC,0x08},{0x00},2},
	{{0xBD,0x00},{0x00},2},
	{{0xC0,0x80},{0x00},2},
	{{0xC1,0x10},{0x00},2},
	{{0xC2,0x37},{0x00},2},
	{{0xC3,0x80},{0x00},2},
	{{0xC4,0x10},{0x00},2},
	{{0xC5,0x37},{0x00},2},
	{{0xC6,0xA9},{0x00},2},
	{{0xC7,0x41},{0x00},2},
	{{0xC8,0x51},{0x00},2},
	{{0xC9,0xA9},{0x00},2},
	{{0xCA,0x41},{0x00},2},
	{{0xCB,0x51},{0x00},2},
	{{0xD0,0x91},{0x00},2},
	{{0xD1,0x68},{0x00},2},
	{{0xD2,0x69},{0x00},2},
	{{0xF5,0x00,0xA5},{0x00},3},
	{{0xDD,0x3F},{0x00},2},
	{{0xDE,0x3F},{0x00},2},
	{{0xF1,0x10},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0xF0,0x02},{0x00},2},
	{{0xE0,0xF0,0x06,0x0B,0x09,0x09,0x16,0x32,0x44,0x4A,0x37,0x13,0x13,0x2E,0x34},{0x00},15},
	{{0xE1,0xF0,0x06,0x0B,0x09,0x08,0x05,0x32,0x33,0x49,0x17,0x13,0x13,0x2E,0x34},{0x00},15},
	{{0xF0,0x10},{0x00},2},
	{{0xF3,0x10},{0x00},2},
	{{0xE0,0x0A},{0x00},2},
	{{0xE1,0x00},{0x00},2},
	{{0xE2,0x00},{0x00},2},
	{{0xE3,0x00},{0x00},2},
	{{0xE4,0xE0},{0x00},2},
	{{0xE5,0x06},{0x00},2},
	{{0xE6,0x21},{0x00},2},
	{{0xE7,0x00},{0x00},2},
	{{0xE8,0x05},{0x00},2},
	{{0xE9,0x82},{0x00},2},
	{{0xEA,0xDF},{0x00},2},
	{{0xEB,0x89},{0x00},2},
	{{0xEC,0x20},{0x00},2},
	{{0xED,0x14},{0x00},2},
	{{0xEE,0xFF},{0x00},2},
	{{0xEF,0x00},{0x00},2},
	{{0xF8,0xFF},{0x00},2},
	{{0xF9,0x00},{0x00},2},
	{{0xFA,0x00},{0x00},2},
	{{0xFB,0x30},{0x00},2},
	{{0xFC,0x00},{0x00},2},
	{{0xFD,0x00},{0x00},2},
	{{0xFE,0x00},{0x00},2},
	{{0xFF,0x00},{0x00},2},
	{{0x60,0x42},{0x00},2},
	{{0x61,0xE0},{0x00},2},
	{{0x62,0x40},{0x00},2},
	{{0x63,0x40},{0x00},2},
	{{0x64,0x02},{0x00},2},
	{{0x65,0x00},{0x00},2},
	{{0x66,0x40},{0x00},2},
	{{0x67,0x03},{0x00},2},
	{{0x68,0x00},{0x00},2},
	{{0x69,0x00},{0x00},2},
	{{0x6A,0x00},{0x00},2},
	{{0x6B,0x00},{0x00},2},
	{{0x70,0x42},{0x00},2},
	{{0x71,0xE0},{0x00},2},
	{{0x72,0x40},{0x00},2},
	{{0x73,0x40},{0x00},2},
	{{0x74,0x02},{0x00},2},
	{{0x75,0x00},{0x00},2},
	{{0x76,0x40},{0x00},2},
	{{0x77,0x03},{0x00},2},
	{{0x78,0x00},{0x00},2},
	{{0x79,0x00},{0x00},2},
	{{0x7A,0x00},{0x00},2},
	{{0x7B,0x00},{0x00},2},
	{{0x80,0x48},{0x00},2},
	{{0x81,0x00},{0x00},2},
	{{0x82,0x05},{0x00},2},
	{{0x83,0x02},{0x00},2},
	{{0x84,0xDD},{0x00},2},
	{{0x85,0x00},{0x00},2},
	{{0x86,0x00},{0x00},2},
	{{0x87,0x00},{0x00},2},
	{{0x88,0x48},{0x00},2},
	{{0x89,0x00},{0x00},2},
	{{0x8A,0x07},{0x00},2},
	{{0x8B,0x02},{0x00},2},
	{{0x8C,0xDF},{0x00},2},
	{{0x8D,0x00},{0x00},2},
	{{0x8E,0x00},{0x00},2},
	{{0x8F,0x00},{0x00},2},
	{{0x90,0x48},{0x00},2},
	{{0x91,0x00},{0x00},2},
	{{0x92,0x09},{0x00},2},
	{{0x93,0x02},{0x00},2},
	{{0x94,0xE1},{0x00},2},
	{{0x95,0x00},{0x00},2},
	{{0x96,0x00},{0x00},2},
	{{0x97,0x00},{0x00},2},
	{{0x98,0x48},{0x00},2},
	{{0x99,0x00},{0x00},2},
	{{0x9A,0x0B},{0x00},2},
	{{0x9B,0x02},{0x00},2},
	{{0x9C,0xE3},{0x00},2},
	{{0x9D,0x00},{0x00},2},
	{{0x9E,0x00},{0x00},2},
	{{0x9F,0x00},{0x00},2},
	{{0xA0,0x48},{0x00},2},
	{{0xA1,0x00},{0x00},2},
	{{0xA2,0x04},{0x00},2},
	{{0xA3,0x02},{0x00},2},
	{{0xA4,0xDC},{0x00},2},
	{{0xA5,0x00},{0x00},2},
	{{0xA6,0x00},{0x00},2},
	{{0xA7,0x00},{0x00},2},
	{{0xA8,0x48},{0x00},2},
	{{0xA9,0x00},{0x00},2},
	{{0xAA,0x06},{0x00},2},
	{{0xAB,0x02},{0x00},2},
	{{0xAC,0xDE},{0x00},2},
	{{0xAD,0x00},{0x00},2},
	{{0xAE,0x00},{0x00},2},
	{{0xAF,0x00},{0x00},2},
	{{0xB0,0x48},{0x00},2},
	{{0xB1,0x00},{0x00},2},
	{{0xB2,0x08},{0x00},2},
	{{0xB3,0x02},{0x00},2},
	{{0xB4,0xE0},{0x00},2},
	{{0xB5,0x00},{0x00},2},
	{{0xB6,0x00},{0x00},2},
	{{0xB7,0x00},{0x00},2},
	{{0xB8,0x48},{0x00},2},
	{{0xB9,0x00},{0x00},2},
	{{0xBA,0x0A},{0x00},2},
	{{0xBB,0x02},{0x00},2},
	{{0xBC,0xE2},{0x00},2},
	{{0xBD,0x00},{0x00},2},
	{{0xBE,0x00},{0x00},2},
	{{0xBF,0x00},{0x00},2},
	{{0xC0,0x12},{0x00},2},
	{{0xC1,0xAA},{0x00},2},
	{{0xC2,0x65},{0x00},2},
	{{0xC3,0x74},{0x00},2},
	{{0xC4,0x47},{0x00},2},
	{{0xC5,0x56},{0x00},2},
	{{0xC6,0x00},{0x00},2},
	{{0xC7,0x88},{0x00},2},
	{{0xC8,0x99},{0x00},2},
	{{0xC9,0x33},{0x00},2},
	{{0xD0,0x21},{0x00},2},
	{{0xD1,0xAA},{0x00},2},
	{{0xD2,0x65},{0x00},2},
	{{0xD3,0x74},{0x00},2},
	{{0xD4,0x47},{0x00},2},
	{{0xD5,0x56},{0x00},2},
	{{0xD6,0x00},{0x00},2},
	{{0xD7,0x88},{0x00},2},
	{{0xD8,0x99},{0x00},2},
	{{0xD9,0x33},{0x00},2},
	{{0xF3,0x01},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0xF0,0x01},{0x00},2},
	{{0xF1,0x01},{0x00},2},
	{{0xA0,0x0B},{0x00},2},
	{{0xA3,0x2A},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2B},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2C},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2D},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2E},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x2F},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x30},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x31},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x32},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA3,0x33},{0x00},2},
	{{0xA5,0xC3},{0x00},2},
	{{0xA0,0x09},{0x00},2},
	{{0xF1,0x10},{0x00},2},
	{{0xF0,0x00},{0x00},2},
	{{0x2A,0x00,0x00,0x01,0x67},{0x00},5},
	{{0x2B,0x00,0x00,0x01,0x68},{0x00},5},
	{{0x4D,0x00},{0x00},2},
	{{0x4E,0x00},{0x00},2},
	{{0x4F,0x00},{0x00},2},
	{{0x4C,0x01},{0x00},2},
	{{0x4C,0x00},{0x00},2},
	{{0x2A,0x00,0x00,0x01,0x67},{0x00},5},
	{{0x2B,0x00,0x00,0x01,0x67},{0x00},5},
	{{0x21},{0x00},1},
	{{0x11},{0x00},1},
	{{0xff},{0x00},0},
	{{0x29},{0x00},1},
	{{0x2c},{0x00},1},
};

/*************************************************
 * reg rw functions
 ************************************************/
#define DMA_BASE_ADRR  0x40000000
#define DDR_BASE_ADDR  0x10000000

void wait_for_idle()
{
	u32 tmp;
	while(1){
		tmp=Xil_In32(DMA_BASE_ADRR+04);
		if(((tmp>>1)&0x1) == 1){
			break;
		}
	}
}

void dma_xfer(u32 mem_addr, u32 length)
{/*用于初始化阶段,需要刷新Cache,确保初始化成功*/
	//Xil_DCacheFlush();
	Xil_DCacheFlushRange(mem_addr, length);

	Xil_Out32(DMA_BASE_ADRR,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x30,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x18,  mem_addr);
	Xil_Out32(DMA_BASE_ADRR+0x28,  length);	 /*length is bytes*/
	wait_for_idle();
	//usleep(10);
}

void dma_xfer2(u32 mem_addr, u32 length)
{/*发送数据阶段,可以不用Cache*/
	//Xil_DCacheFlush();
	Xil_DCacheFlushRange(mem_addr, length);

	Xil_Out32(DMA_BASE_ADRR,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x30,  0x00010003);
	Xil_Out32(DMA_BASE_ADRR+0x18,  mem_addr);
	Xil_Out32(DMA_BASE_ADRR+0x28,  length);	 /*length is bytes*/
	wait_for_idle();
	//usleep(10);
}
void set_cfg(u32 mem_addr, u8 cmd, u8 addr, u8 mode, u16 length)
{
	u32 tmp=cmd;
	tmp|=addr<<8;
	tmp|=mode<<16;
	tmp|=length<<20;
	Xil_Out32(mem_addr,  tmp);
}

u32 get_cfg(u8 cmd, u8 addr, u8 mode, u8 work_mode, u16 length)
{
	u32 tmp=cmd;
	tmp|=addr<<8;
	tmp|=mode<<16;
	tmp|=length<<20;
	tmp|=work_mode<<30;
	return tmp;
}

void set_npdata8(u32 mem_addr, u8 *pdata, u32 length){
	u32 addr=mem_addr;
	while(length--){
		Xil_Out8(addr++, *pdata++);
	}
}


/*************************************************
 * basic xfer functions
 ************************************************/
void xfer_cmd_pdata8(u32 mem_addr, u8 cmd, u8* pdata, u32 length){
	if(length==0)
		set_cfg(mem_addr, 0x02, cmd, 0b0101, length);  /*byte_mode=0, cs_mode=1, data_mode=0b01*/
	else
		set_cfg(mem_addr, 0x02, cmd, 0b0110, length); /*byte_mode=0, cs_mode=1, data_mode=0b10*/

	set_npdata8(mem_addr+4, pdata,length);
	dma_xfer(mem_addr, 4+length);
}


void xfer_cmd_pdata16_dma(u32 mem_addr, u32 length){
	//dma_xfer(mem_addr, 20*64);
	dma_xfer2(mem_addr, length);
}


void seqs_init() {

	for (int i = 0; i < sizeof(WriteBuffer) / sizeof(*WriteBuffer); i++) {
		if (WriteBuffer[i].length == 0) {
			usleep(WriteBuffer[i].txbuf[0] * 1000);
			continue;
		}

		u8 cmd=WriteBuffer[i].txbuf[0];
		u8 *pdata=WriteBuffer[i].txbuf+1;
		u16 length=WriteBuffer[i].length-1;

		xfer_cmd_pdata8(DDR_BASE_ADDR, cmd, pdata, length);
		usleep(10); //this delay is need
		//xil_printf("Exec CMD=%x\r\n\r\n", cmd);
	}
}

void xfer_win(u16 xstart, u16 ystart, u16 width, u16 height)
{
    u8 cmd_list[2][5]={
    		{0x2a, xstart>>8, xstart&0xff, (xstart+width-1)>>8, (xstart+width-1)&0xff},
    		{0x2b, ystart>>8, ystart&0xff, (ystart+height-1)>>8, (ystart+height-1)&0xff},
    };

    xfer_cmd_pdata8(DDR_BASE_ADDR, cmd_list[0][0], &(cmd_list[0][1]),4);
    xfer_cmd_pdata8(DDR_BASE_ADDR, cmd_list[1][0], &(cmd_list[1][1]),4);
}

/*************************************************
 * application functions
 ************************************************/
void load_sd_bmp(u8 *frame, const char *bmp_name);
//void convertRGB888toRGB888(
//		const uint8_t *input,
//		uint8_t *output,
//		int inputWidth, int inputHeight,
//		int startX, int startY,
//		int outputWidth, int outputHeight
//		) ;
void convertRGB888toRGB565(
		const uint8_t *input,
		uint16_t *output,
		int inputWidth, int inputHeight,
		int startX, int startY,
		int outputWidth, int outputHeight
		) ;

void convertRGB565toRGB565(
		const uint16_t *input,
		uint16_t *output,
		int inputWidth, int inputHeight,
		int startX, int startY,
		int outputWidth, int outputHeight
		) ;


#define LCD_BPP             (16)

#define LCD_X_SIZE          (360U)                      /* available x pixel size */
#define LCD_Y_SIZE          (360U)		                /* available y pixle size */
#define LCD_PBYTE           ((LCD_BPP + 0) / 8)         /* bytes in pixel unit */
#define LCD_HBYTE           (LCD_X_SIZE * LCD_PBYTE)    /* bytes in horizontal line */

#define LCD_VSW             (1U)
#define LCD_HFP             (8U)
#define LCD_HBP             (8U)

#define LCD_TE_OFT          (25U)

//#define NFRAMES   1
//typedef u8 (*ArrayPtr)[400][20 * 60];
//ArrayPtr ptr[NFRAMES];

//u8 frame_src[1920*1080*3];
//u8 frame_target[400*400*3];

typedef u8 (*frame_src_type)[1920*1080*3]; /*一定要地址对齐,否则dma工作异常*/
frame_src_type frame_src=(frame_src_type)0x11000000;

typedef u16 (*frame_dest_type)[1920*1080]; /*一定要地址对齐,否则dma工作异常*/
frame_dest_type frame_dest=(frame_dest_type)0x12000000;

typedef u16 (*frame_target_type)[360*360];
frame_target_type frame_target=(frame_target_type)0x14000000;

//void init_Arrayptr()
//{
//	for(int i=0; i<NFRAMES; i++){
//		ptr[i]=(ArrayPtr)(0x13000000+400*20*60*i);
//	}
//}


//void fill_pic(ArrayPtr ptr, u8 *pcolor) {
//    u32 cmd1 = get_cfg(0xde, 0x60, 0b0011, 60);
//    u32 cmd2 = get_cfg(0xde, 0x60, 0b0111, 60);
//
//    // 遍历数组的每一行
//    for (int i = 0; i < 400; ++i) {
//        // 遍历每一行的每一个packet
//        for (int j = 0; j < 20; ++j) {
//            // 计算当前packet的起始地址
//        	u8 *packet = (u8 *)(ptr + i) + j * 64; // ptr指向整个二维数组,ptr+i指向第i行,然后加上j个packet的偏移
//
//            // 填充cmd部分
//            u32 *cmd_ptr = (u32 *)packet;
//            *cmd_ptr = (j == 19) ? cmd2 : cmd1; // 第20个packet使用cmd2,其余使用cmd1
//
//            // 移动到color数据部分的起始地址
//            packet += sizeof(cmd1);
//
//            // 填充color数据部分
//			memcpy(packet, pcolor, 60); /*库函数速度更快*/
//			pcolor+=60;
////            for (int k = 0; k < 60; k += 3) {
////            	u8 r=*pcolor++;
////            	u8 g=*pcolor++;
////            	u8 b=*pcolor++;
////
////                packet[k] = r;
////                packet[k + 1] = g;
////                packet[k + 2] = b;
////            }
//        }
//    }
//}

//void fill_pic_new(
//		const uint8_t *input, ArrayPtr ptr,
//		int inputWidth, int inputHeight,
//		int startX, int startY,
//		int outputWidth, int outputHeight
//)
//{
////    u32 cmd1 = get_cfg(0xde, 0x60, 0b0011, 60);
////    u32 cmd2 = get_cfg(0xde, 0x60, 0b0111, 60);
//
//    // 遍历数组的每一行
//    for (int i = 0; i < outputHeight; ++i) {
//        // 遍历每一行的每一个packet
//        for (int j = 0; j < outputWidth*3/60; ++j) {
//            // 计算当前packet的起始地址
//        	u8 *packet = (u8 *)(ptr + i) + j * 60; // ptr指向整个二维数组,ptr+i指向第i行,然后加上j个packet的偏移
//
////            // 填充cmd部分
////            u32 *cmd_ptr = (u32 *)packet;
////            *cmd_ptr = (j == 19) ? cmd2 : cmd1; // 第20个packet使用cmd2,其余使用cmd1
////
////            // 移动到color数据部分的起始地址
////            packet += sizeof(cmd1);
//
//            // 填充color数据部分
//            int inputIndex = ((startY + i) * inputWidth + startX + j*20) * 3;
//			memcpy(packet,  &input[inputIndex], 60); /*库函数速度更快*/
//        }
//    }
//}

//u32 get_line_ptr(
//	const uint8_t *input,
//	int inputWidth,
//	int startX, int startY,
//	int line_num
//)
//{
//	int inputIndex = ((startY + line_num) * inputWidth + startX + 0*20) * 3;
//	return (u32)&input[inputIndex];
//}


typedef u8 (*ArrayPtr)[360][12 * 64];
ArrayPtr ptr1=(ArrayPtr)0x10100000;


void fill_pic(ArrayPtr ptr, u16 *pcolor) {
    u32 cmd1 = get_cfg(0x32, 0x2c, 0b0011, 0b00, 60);
    u32 cmd2 = get_cfg(0x32, 0x2c, 0b0111, 0b00, 60);
    u32 cmd3 = get_cfg(0x32, 0x3c, 0b0011, 0b00, 60);
    u32 cmd4 = get_cfg(0x32, 0x3c, 0b0111, 0b00, 60);

//    u32 cmd1 = get_cfg(0x02, 0x2c, 0b0010, 0b00, 60);
//    u32 cmd2 = get_cfg(0x02, 0x2c, 0b0110, 0b00, 60);
//    u32 cmd3 = get_cfg(0x02, 0x3c, 0b0010, 0b00, 60);
//    u32 cmd4 = get_cfg(0x02, 0x3c, 0b0110, 0b00, 60);

    // 遍历数组的每一行
    for (int i = 0; i < 360; ++i) {
        // 遍历每一行的每一个packet
        for (int j = 0; j < 12; ++j) {
            // 计算当前packet的起始地址
        	u8 *packet = (u8 *)(ptr + i) + j * 64; // ptr指向整个二维数组,ptr+i指向第i行,然后加上j个packet的偏移

            // 填充cmd部分
            u32 *cmd_ptr = (u32 *)packet;
            if(i==0){
            	*cmd_ptr = (j == 11) ? cmd2 : cmd1; // 第一行,第12个packet使用cmd2,其余使用cmd1
            }
            else{
            	*cmd_ptr = (j == 11) ? cmd4 : cmd3; // 其余行,第12个packet使用cmd4,其余使用cmd3
            }

            // 移动到color数据部分的起始地址
            packet += sizeof(cmd1);

            // 填充color数据部分
			memcpy(packet, pcolor, 60);
			pcolor+=30;
        }
    }
}

void show_bmp();

XTime t1,t2,t3,t4,t5,t6;
void draw_bmp()
{
	int x=500,y=500;
	int dir_x=1, dir_y=1;

	XTime_GetTime(&t1);
	load_sd_bmp(*frame_src, "shatan.bmp");	//1920x1080

	convertRGB888toRGB565(
			*frame_src,
			*frame_dest,
			1920,1080,
			0,0,
			1920,1080
			);
//	convertRGB888toRGB565(
//			*frame_src,
//			*frame_target,
//			1920,1080,
//			500,500,
//			360,360
//			);

	main_gpio_irq();

	int i=0;
	while(1){
		while(read_te()==0);

		if(1){
			if(dir_x) x+=1; else x-=1;
			if(dir_y) y+=1; else y-=1;

			if(x+360>1920) {x=1920-360; dir_x=0;} else if(x<0) {x=0; dir_x=1;}
			if(y+360>1080) {y=1080-360; dir_y=0;} else if(y<0) {y=0; dir_y=1;}

	//		XTime_GetTime(&t2);
	//		convertRGB888toRGB888(
	//				frame_src,
	//				(u8 *)frame_target,
	//				1920,1080,
	//				x,y,
	//				400,400
	//				);
	//		XTime_GetTime(&t3);
	//		fill_pic(ptr[i],(u8 *)frame_target);
	//		fill_pic_new(
	//				frame_src,
	//				ptr[i],
	//				1920,1080,
	//				x,y,
	//				400,400
	//				);
	//		XTime_GetTime(&t4);
	//
	//		int dt32 = (u32)(t3-t2)*(1000000.0/COUNTS_PER_SECOND);
	//		int dt43 = (u32)(t4-t3)*(1000000.0/COUNTS_PER_SECOND);
	//		printf("ptr=%p, x=%d, y=%d, dt32=%d, dt43=%d\r\n", ptr[i], x,y,dt32,dt43);

//			XTime_GetTime(&t2);
			convertRGB565toRGB565(
					*frame_dest,
					*frame_target,
					1920,1080,
					x,y,
					360,360
					);
//			XTime_GetTime(&t3);
			fill_pic(ptr1,*frame_target);
//			XTime_GetTime(&t4);

			xfer_win(0,0,360,360);
			for (int i = 0; i < LCD_Y_SIZE; i+=1)
			{
			   xfer_cmd_pdata16_dma((u32)ptr1[i],12*64);
			   //usleep(30);
			}
//			XTime_GetTime(&t5);

//			int dt32 = (u32)(t3-t2)*(1000000.0/COUNTS_PER_SECOND);
//			int dt43 = (u32)(t4-t3)*(1000000.0/COUNTS_PER_SECOND);
//			int dt54 = (u32)(t5-t4)*(1000000.0/COUNTS_PER_SECOND);
//			xil_printf("dt32=%d, dt43=%d, dt54=%d\r\n", dt32, dt43, dt54);
//
//			XTime_GetTime(&t6);
//			int dt65 = (u32)(t6-t5)*(1000000.0/COUNTS_PER_SECOND);
//			xil_printf("dt65=%d\r\n", dt65);

			//Refresh_flag=0;
		}
		//show_bmp(x,y);
	}
}

//void show_bmp(int x, int y)
//{
//	u32 frame_index=0;
//    do
//    {
//        /* vs(0x61) packet */
//        for (int i = 0; i < LCD_VSW; i++)
//        {
//        	xfer_cmd_pdata8(DDR_BASE_ADDR, 0x61,NULL,0);
//            usleep(40);
//        }
//
//        /* hbp(0x60) packet */
//        for (int i = 0; i < LCD_HBP; i++)
//        {
//        	xfer_cmd_pdata8(DDR_BASE_ADDR, 0x60,NULL,0);
//            usleep(40);
//        }
//
//        /* transmit display cache data to lcd line by line */
//        qspi_mode_set(1);
//        for (int i = 0; i < LCD_Y_SIZE; i+=1)
//        {
//        	/*每次获得一行图像数据的指针,直接在原始图像frame_buffer中取到,避免了复制*/
//    		u32 addr=get_line_ptr(
//    				*frame_src,
//    				1920,
//    				x,y,
//					i
//    				);
//    		//printf("%x\r\n", addr);
//        	//xfer_cmd_pdata16_dma((u32)ptr[frame_index][i],20*60);
//    		xfer_cmd_pdata16_dma(addr,20*60);
//            usleep(45);  //这个时间设置非常关键,如果小了就不行
//        }
//        qspi_mode_set(0);
//
//
//        /* hfp(0x60) packet */
//        for (int i = 0; i < LCD_HFP; i++)
//        {
//        	xfer_cmd_pdata8(DDR_BASE_ADDR, 0x60,NULL,0);
//            usleep(40);
//        }
//
////        frame_index++;
////        if(frame_index==NFRAMES){
////        	frame_index=0;
////        }
//
//    }
//    while(0);
//}

int main()
{
/*
 *	在 void dma_xfer(u32 mem_addr, u32 length)中
 *  Xil_DCacheFlush();  //加上这一句话,就可以运行!这里最关键!
 *  其它地方都不需要加
 */

   //Xil_DCacheDisable();
	gpio_init();
	//init_Arrayptr();
	qspi_mode_set(0);
    seqs_init();
    draw_bmp();

    return 0;
}


//从SD卡中读取BMP图片
void load_sd_bmp(u8 *frame, const char *bmp_name)
{
	static 	FATFS fatfs;
	FIL 	fil;
	u8		bmp_head[54];
	UINT 	bmp_width,bmp_height,bmp_size;
	UINT 	br;

	f_mount(&fatfs,"",1);//挂载文件系统

	f_open(&fil, bmp_name,FA_READ);	//打开文件, 注意是bmp_24bits格式
	xil_printf("open bmp\n\r");
	f_lseek(&fil,0);//移动文件读写指针到文件开头

	f_read(&fil,bmp_head,54,&br);//读取BMP文件头

	//BMP图片的分辨率和大小
	bmp_width  = *(UINT *)(bmp_head + 0x12);
	bmp_height = *(UINT *)(bmp_head + 0x16);
	bmp_size   = *(UINT *)(bmp_head + 0x22);
	xil_printf("bmp information:\n\r");
	xil_printf(" width  = %d,\n\r height = %d,\n\r size   = %d bytes \n\r",
			bmp_width,bmp_height,bmp_size);

	//读出图片,写入DDR
	f_read(&fil, frame, bmp_width*bmp_height*3,&br);
	xil_printf("br=%d\r\n", br);

	for(int i=0; i<20; i++){
		xil_printf("%x\r\n", frame[i]);
	}

	//关闭文件
	f_close(&fil);

	Xil_DCacheFlush();     //刷新Cache,将数据更新至DDR3中
	xil_printf("display bmp\n\r");
}


//void convertRGB888toRGB888(const uint8_t *input, uint8_t *output, int inputWidth, int inputHeight, int startX, int startY, int outputWidth, int outputHeight)
//{
//    int i, j;
//
//    for (i = 0; i < outputHeight; i++) {
//        int inputIndex = ((startY + i) * inputWidth + startX + 0) * 3;
//        int outputIndex=(i * outputWidth + 0)*3;
//    	memcpy(&output[outputIndex], &input[inputIndex],outputWidth*3); /*库函数速度更快*/
//
////        for (j = 0; j < outputWidth; j++) {
////            // Calculate the index in the RGB888 array
////            int inputIndex = ((startY + i) * inputWidth + startX + j) * 3;
////
////            // Extract RGB888 components
////            uint8_t b = input[inputIndex];
////            uint8_t g = input[inputIndex + 1];
////            uint8_t r = input[inputIndex + 2];
////
////            int outputIndex=(i * outputWidth + j)*3;
////            // Store in the output array
////            output[outputIndex] = r;
////            output[outputIndex+1] = g;
////            output[outputIndex+2] = b;
////        }
//    }
//}

void convertRGB888toRGB565(const uint8_t *input, uint16_t *output, int inputWidth, int inputHeight, int startX, int startY, int outputWidth, int outputHeight)
{
    int i, j;

    for (i = 0; i < outputHeight; i++) {
        for (j = 0; j < outputWidth; j++) {
            // Calculate the index in the RGB888 array
            int inputIndex = ((startY + i) * inputWidth + startX + j) * 3;

            // Extract RGB888 components
            uint8_t b = input[inputIndex];
            uint8_t g = input[inputIndex + 1];
            uint8_t r = input[inputIndex + 2];

            // Convert to RGB565 format
            uint16_t rgb565 = ((r>>3) << 11) | ((g>>2) << 5) | (b>>3);
            uint16_t rgb565_new=((rgb565&0xff)<<8)+(rgb565>>8);  //注意我们的qspi_ip的发送顺序,跟rgb565不匹配,要手动调整顺序

            // Store in the output array
            output[i * outputWidth + j] = rgb565_new;
        }
    }
}

void convertRGB565toRGB565(const uint16_t *input, uint16_t *output, int inputWidth, int inputHeight, int startX, int startY, int outputWidth, int outputHeight)
{
    int i, j;

    for (i = 0; i < outputHeight; i++) {
        int inputIndex = ((startY + i) * inputWidth + startX + 0) * 1;
        int outputIndex=(i * outputWidth + 0)*1;
    	memcpy(&output[outputIndex], &input[inputIndex],outputWidth*2); /*库函数速度更快*/
    }
}

效果

  • 显示动画非常丝滑,但是图像中始终有一根亮线。刷单色图没有问题,因此可以排除玻璃的影响,可能是程序的问题。可能是程序遇到的边界问题?(后来找到原因了,0x13000000这个地址段ddr有坏道,换一个地址就可以了。)
typedef u16 (*frame_target_type)[360*360];
frame_target_type frame_target=(frame_target_type)0x14000000;
  • 逻辑分析仪抓到的波形可以看到,发送数据确实与te同步。

image

			xfer_win(0,0,360,360);
			for (int i = 0; i < LCD_Y_SIZE; i+=1)
			{
			   xfer_cmd_pdata16_dma((u32)ptr1[i],12*64);
			   usleep(30);
			}

image

			xfer_win(0,0,360,360);
			for (int i = 0; i < LCD_Y_SIZE; i+=1)
			{
			   xfer_cmd_pdata16_dma((u32)ptr1[i],12*64);
			   //usleep(30);
			}
⚠️ **GitHub.com Fallback** ⚠️