// 9G-STM32 EWARM开发过程简介之四--移植FSMC的T6963C驱动

/*
一,把VDT6与T6963C连接
 NOE -> /RD  ;
 NWE -> /WR  ;
 NE1 -> /CE  ;
 A16 -> C/D  ; A16    最近,接线最方便。
 PC4 -> /RST ;
 D8  -> DB0  ;   D8-D15 最近, 接线最方便。
 ...
 D15 -> DB7  ;
 
二,把IO配置成FSMC模式
*/
 void LCD_CtrlLinesConfig(void)
 {
   GPIO_InitTypeDef GPIO_InitStructure;
 
   /* Enable FSMC, GPIOC, GPIOD, GPIOE clocks */
   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
 
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE);
 
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 
   /* Set PC.04(LCDRST) as alternate
      function push pull */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
   GPIO_Init(GPIOC, &GPIO_InitStructure);
  
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 
   /* Set PD.04(NOE), PD.05(NWE), PD.07(NE1), PD.08(D13), PD.09(D14), PD.10(D15), PD.11(A16) as alternate
      function push pull */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 ;
   GPIO_Init(GPIOD, &GPIO_InitStructure);
 
   /* Set PE.11(D8), PE.12(D9), PE.13(D10), PE.14(D11), PE.15(D12) as alternate function push pull */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
   GPIO_Init(GPIOE, &GPIO_InitStructure);
 }

/*       
三,配置FSMC工作模式
*/
 void LCD_FSMCConfig(void)
 {
   FSMC_NORSRAMInitTypeDef  FSMC_NORSRAMInitStructure;
   FSMC_NORSRAMTimingInitTypeDef  p;
 
 /*-- FSMC Configuration ------------------------------------------------------*/
 /*----------------------- SRAM Bank 1 ----------------------------------------*/
   /* FSMC_Bank1_NORSRAM1 configuration */
   p.FSMC_AddressSetupTime = 1;
   p.FSMC_AddressHoldTime = 1;
   p.FSMC_DataSetupTime = 6;
   p.FSMC_BusTurnAroundDuration = 1;
   p.FSMC_CLKDivision = 1;
   p.FSMC_DataLatency = 2;
   p.FSMC_AccessMode = FSMC_AccessMode_A;
 
   /* Color LCD configuration ------------------------------------
      LCD configured as follow:
         - Data/Address MUX = Disable
         - Memory Type = SRAM
         - Data Width = 16bit
         - Write Operation = Enable
         - Extended Mode = Enable
         - Asynchronous Wait = Disable */
   FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
   FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
   FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
   FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
   FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
   FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
   FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
   FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
   FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
   FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
   FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable;
   FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
   FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
   FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
 
   FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); 
 
   /* Enable FSMC_Bank1_NORSRAM1 */
   FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
 }
/*
四,定义物理访问地址
*/ //TXEC 240128BDLNW-EY
 // T6963C
 
 /*A16=0 -- dat*/
 #define LCD_DAT_ADDR (*((volatile unsigned char *) 0x60000001))
 /*A16=1 -- cmd*/
 #define LCD_CMD_ADDR (*((volatile unsigned char *) 0x60020001))
 
 #define IMAGE     1
 #define TEXT     0
 #define VISION_PIXEL_LENGTH  240
 #define VISION_PIXEL_WIDTH  128
 #define FRAME_BUFFER_LENGTH  256
 #define FRAME_BUFFER_WIDTH  128
/*
五,定义指令访问接口
*/ //指令&数据读写状态
 static void RWCheck(void)
 {
  while((LCD_CMD_ADDR & 0x03) != 0x03);      //低电平忙
 }
 //数据自动写状态
 void CheckAutoWrite(void)
 {
  while((LCD_CMD_ADDR & 0x08) != 0x08);      //低电平忙
 }
 //屏读/屏考贝出错状态
 void CheckScreen(void)
 {     
  while(LCD_CMD_ADDR & 0x40);      //1出错,0正确
 }
 static void WriteDATA(unsigned char Data)                       
 { 
  RWCheck();
  LCD_DAT_ADDR = Data;
 }
 static unsigned char ReadDATA(void)
 {
  RWCheck();
  return LCD_DAT_ADDR;
 }
 static void WriteCMD0(unsigned char Comd)                 
 {
  RWCheck();
  LCD_CMD_ADDR = Comd;
 }
 static void WriteCMD1(unsigned char Para,unsigned char Comd)
 {
  WriteDATA(Para);
     WriteCMD0(Comd);
 }
 static void WriteCMD2(unsigned char Par1,unsigned char Par2,unsigned char Comd)
 {
  WriteDATA(Par1);
  WriteDATA(Par2); 
     WriteCMD0(Comd);
 }
/*
六,定义API访问接口
*/
 void LCD_Reset(void)
 {
  GPIO_SetBits(GPIOC, GPIO_Pin_4); //置1  LCDRST = 1;
  OSTimeDlyHMSM(0, 0, 0, 10);
     GPIO_ResetBits(GPIOC, GPIO_Pin_4); //清0  LCDRST = 0;
  OSTimeDlyHMSM(0, 0, 0, 50);
     GPIO_SetBits(GPIOC, GPIO_Pin_4); //置1  LCDRST = 1;
  OSTimeDlyHMSM(0, 0, 0, 50);
 }
 
 void STM32_T6963C_Init(void)
 {
   unsigned int i;
 /* Configure the LCD Control pins --------------------------------------------*/
   LCD_CtrlLinesConfig();
 
 /* Configure the FSMC Parallel interface -------------------------------------*/
   LCD_FSMCConfig();
  
 /* initialize sequence  -------------------------------*/
   LCD_Reset();

   WriteCMD2(0x00,0x08,0x42); //设置显示图形首地址
   WriteCMD2((FRAME_BUFFER_LENGTH/8),0x00,0x43); //设置图形显示区域宽度
   WriteCMD0(0x98);   //图形显示开
   WriteCMD0(0x88);   //设定显示方式,图形/文本或的方式
 }
 
 void STM32_T6963C_Clear(void)
 {
  unsigned int i;
  
  WriteCMD2(0x00,0x00,0x24);
  WriteCMD0(0xb0);
   
   for(i=0;i<0x2000;i++)
  {
   WriteCMD1(0x00,0xc0);
  }
  WriteCMD0(0xb2);
 }
 
 //设定显示地址函数
 static void LCD_Set_x_y(unsigned char x,unsigned char y,unsigned char mode)
 {
  unsigned int  temp;
  
  temp=(FRAME_BUFFER_LENGTH/8)*y + x/8;
  if(mode==IMAGE)
  {                              //如果图形模式要加上图形区首地址0x0800
   temp+=0x0800;
  }
  WriteCMD2(temp&0xff,temp/256,0x24);
 }
 
 void LCD_Paint_dot(unsigned char x,unsigned char y)
 {
  LCD_Set_x_y(x,y,IMAGE);
  WriteCMD0(0xf8|(0x07&(~(x%8))));
 }
 void LCD_Clear_dot(unsigned char x,unsigned char y)
 {
  LCD_Set_x_y(x,y,IMAGE);
  WriteCMD0(0xf0|(0x07&(~(x%8))));
 }
 void LCD_Paint_line_x(unsigned char y) //图形方式画一条线
 {
  unsigned char i;
 
  LCD_Set_x_y(0,y,IMAGE);
  for(i=0;i<(VISION_PIXEL_LENGTH/8);i++)
  {
   WriteCMD1(0xFF, 0xc0);     //数据写地址不变
  }
 }
 
 void LCD_Paint_line_y(unsigned char x)
 {
  unsigned char i;
 
  for(i=0;i<VISION_PIXEL_WIDTH;i++)
  {
   LCD_Paint_dot(x,i);
  }
 }

/*
七,其它接口自行处理
*/