ili_lcd_general.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. #include "ili_lcd_general.h"
  2. // Compatible list:
  3. // ili9320 ili9325 ili9328
  4. // LG4531
  5. //内联函数定义,用以提高性能
  6. #ifdef __CC_ARM /* ARM Compiler */
  7. #define lcd_inline static __inline
  8. #elif defined (__ICCARM__) /* for IAR Compiler */
  9. #define lcd_inline inline
  10. #elif defined (__GNUC__) /* GNU GCC Compiler */
  11. #define lcd_inline static __inline
  12. #else
  13. #define lcd_inline static
  14. #endif
  15. #define rw_data_prepare() write_cmd(34)
  16. /********* control ***********/
  17. #include "stm32f10x.h"
  18. #include "board.h"
  19. //输出重定向.当不进行重定向时.
  20. #define printf rt_kprintf //使用rt_kprintf来输出
  21. //#define printf(...) //无输出
  22. /* LCD is connected to the FSMC_Bank1_NOR/SRAM2 and NE2 is used as ship select signal */
  23. /* RS <==> A2 */
  24. #define LCD_REG (*((volatile unsigned short *) 0x64000000)) /* RS = 0 */
  25. #define LCD_RAM (*((volatile unsigned short *) 0x64000008)) /* RS = 1 */
  26. static void LCD_FSMCConfig(void)
  27. {
  28. FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
  29. FSMC_NORSRAMTimingInitTypeDef Timing_read,Timing_write;
  30. /* FSMC GPIO configure */
  31. {
  32. GPIO_InitTypeDef GPIO_InitStructure;
  33. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF
  34. | RCC_APB2Periph_GPIOG, ENABLE);
  35. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  36. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  37. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38. /*
  39. FSMC_D0 ~ FSMC_D3
  40. PD14 FSMC_D0 PD15 FSMC_D1 PD0 FSMC_D2 PD1 FSMC_D3
  41. */
  42. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_14 | GPIO_Pin_15;
  43. GPIO_Init(GPIOD,&GPIO_InitStructure);
  44. /*
  45. FSMC_D4 ~ FSMC_D12
  46. PE7 ~ PE15 FSMC_D4 ~ FSMC_D12
  47. */
  48. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10
  49. | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  50. GPIO_Init(GPIOE,&GPIO_InitStructure);
  51. /* FSMC_D13 ~ FSMC_D15 PD8 ~ PD10 */
  52. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
  53. GPIO_Init(GPIOD,&GPIO_InitStructure);
  54. /*
  55. FSMC_A0 ~ FSMC_A5 FSMC_A6 ~ FSMC_A9
  56. PF0 ~ PF5 PF12 ~ PF15
  57. */
  58. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3
  59. | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  60. GPIO_Init(GPIOF,&GPIO_InitStructure);
  61. /* FSMC_A10 ~ FSMC_A15 PG0 ~ PG5 */
  62. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  63. GPIO_Init(GPIOG,&GPIO_InitStructure);
  64. /* FSMC_A16 ~ FSMC_A18 PD11 ~ PD13 */
  65. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  66. GPIO_Init(GPIOD,&GPIO_InitStructure);
  67. /* RD-PD4 WR-PD5 */
  68. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  69. GPIO_Init(GPIOD,&GPIO_InitStructure);
  70. /* NBL0-PE0 NBL1-PE1 */
  71. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  72. GPIO_Init(GPIOE,&GPIO_InitStructure);
  73. /* NE1/NCE2 */
  74. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  75. GPIO_Init(GPIOD,&GPIO_InitStructure);
  76. /* NE2 */
  77. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  78. GPIO_Init(GPIOG,&GPIO_InitStructure);
  79. /* NE3 */
  80. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  81. GPIO_Init(GPIOG,&GPIO_InitStructure);
  82. /* NE4 */
  83. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  84. GPIO_Init(GPIOG,&GPIO_InitStructure);
  85. }
  86. /* FSMC GPIO configure */
  87. /*-- FSMC Configuration -------------------------------------------------*/
  88. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &Timing_read;
  89. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &Timing_write;
  90. FSMC_NORSRAMStructInit(&FSMC_NORSRAMInitStructure);
  91. Timing_read.FSMC_AddressSetupTime = 3; /* 地址建立时间 */
  92. Timing_read.FSMC_DataSetupTime = 4; /* 数据建立时间 */
  93. Timing_read.FSMC_AccessMode = FSMC_AccessMode_A; /* FSMC 访问模式 */
  94. Timing_write.FSMC_AddressSetupTime = 2; /* 地址建立时间 */
  95. Timing_write.FSMC_DataSetupTime = 3; /* 数据建立时间 */
  96. Timing_write.FSMC_AccessMode = FSMC_AccessMode_A; /* FSMC 访问模式 */
  97. /* Color LCD configuration ------------------------------------
  98. LCD configured as follow:
  99. - Data/Address MUX = Disable
  100. - Memory Type = SRAM
  101. - Data Width = 16bit
  102. - Write Operation = Enable
  103. - Extended Mode = Enable
  104. - Asynchronous Wait = Disable */
  105. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2;
  106. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  107. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  108. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  109. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  110. FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
  111. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  112. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  113. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  114. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  115. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  116. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Enable;
  117. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  118. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  119. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE);
  120. }
  121. static void delay(int cnt)
  122. {
  123. volatile unsigned int dl;
  124. while(cnt--)
  125. {
  126. for(dl=0; dl<500; dl++);
  127. }
  128. }
  129. static void lcd_port_init(void)
  130. {
  131. LCD_FSMCConfig();
  132. }
  133. lcd_inline void write_cmd(unsigned short cmd)
  134. {
  135. LCD_REG = cmd;
  136. }
  137. lcd_inline unsigned short read_data(void)
  138. {
  139. return LCD_RAM;
  140. }
  141. lcd_inline void write_data(unsigned short data_code )
  142. {
  143. LCD_RAM = data_code;
  144. }
  145. lcd_inline void write_reg(unsigned char reg_addr,unsigned short reg_val)
  146. {
  147. write_cmd(reg_addr);
  148. write_data(reg_val);
  149. }
  150. lcd_inline unsigned short read_reg(unsigned char reg_addr)
  151. {
  152. unsigned short val=0;
  153. write_cmd(reg_addr);
  154. val = read_data();
  155. return (val);
  156. }
  157. /********* control <只移植以上函数即可> ***********/
  158. static unsigned short deviceid=0;//设置一个静态变量用来保存LCD的ID
  159. //返回LCD的ID
  160. unsigned int lcd_getdeviceid(void)
  161. {
  162. return deviceid;
  163. }
  164. static unsigned short BGR2RGB(unsigned short c)
  165. {
  166. u16 r, g, b, rgb;
  167. b = (c>>0) & 0x1f;
  168. g = (c>>5) & 0x3f;
  169. r = (c>>11) & 0x1f;
  170. rgb = (b<<11) + (g<<5) + (r<<0);
  171. return( rgb );
  172. }
  173. static void lcd_SetCursor(unsigned int x,unsigned int y)
  174. {
  175. write_reg(32,x); /* 0-239 */
  176. write_reg(33,y); /* 0-319 */
  177. }
  178. /* 读取指定地址的GRAM */
  179. static unsigned short lcd_read_gram(unsigned int x,unsigned int y)
  180. {
  181. unsigned short temp;
  182. lcd_SetCursor(x,y);
  183. rw_data_prepare();
  184. /* dummy read */
  185. temp = read_data();
  186. temp = read_data();
  187. return temp;
  188. }
  189. static void lcd_clear(unsigned short Color)
  190. {
  191. unsigned int index=0;
  192. lcd_SetCursor(0,0);
  193. rw_data_prepare(); /* Prepare to write GRAM */
  194. for (index=0; index<(LCD_WIDTH*LCD_HEIGHT); index++)
  195. {
  196. write_data(Color);
  197. }
  198. }
  199. static void lcd_data_bus_test(void)
  200. {
  201. unsigned short temp1;
  202. unsigned short temp2;
  203. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  204. write_reg(0x0003,(1<<12)|(1<<5)|(1<<4) | (0<<3) );
  205. /* wirte */
  206. lcd_SetCursor(0,0);
  207. rw_data_prepare();
  208. write_data(0x5555);
  209. write_data(0xAAAA);
  210. /* read */
  211. lcd_SetCursor(0,0);
  212. if (
  213. (deviceid ==0x9325)
  214. || (deviceid ==0x9328)
  215. || (deviceid ==0x9320)
  216. )
  217. {
  218. temp1 = BGR2RGB( lcd_read_gram(0,0) );
  219. temp2 = BGR2RGB( lcd_read_gram(1,0) );
  220. }
  221. else if( deviceid ==0x4531 )
  222. {
  223. temp1 = lcd_read_gram(0,0);
  224. temp2 = lcd_read_gram(1,0);
  225. }
  226. if( (temp1 == 0x5555) && (temp2 == 0xAAAA) )
  227. {
  228. printf(" data bus test pass!");
  229. }
  230. else
  231. {
  232. printf(" data bus test error: %04X %04X",temp1,temp2);
  233. }
  234. }
  235. void lcd_Initializtion(void)
  236. {
  237. lcd_port_init();
  238. deviceid = read_reg(0x00);
  239. /* deviceid check */
  240. if(
  241. (deviceid != 0x4531)
  242. && (deviceid != 0x7783)
  243. && (deviceid != 0x9320)
  244. && (deviceid != 0x9325)
  245. && (deviceid != 0x9328)
  246. && (deviceid != 0x9300)
  247. )
  248. {
  249. printf("Invalid LCD ID:%08X\r\n",deviceid);
  250. printf("Please check you hardware and configure.");
  251. //while(1);
  252. return ;
  253. }
  254. else
  255. {
  256. printf("\r\nLCD Device ID : %04X ",deviceid);
  257. }
  258. if (deviceid==0x9325|| deviceid==0x9328)
  259. {
  260. write_reg(0x00e7,0x0010);
  261. write_reg(0x0000,0x0001); //start internal osc
  262. #if defined(_ILI_REVERSE_DIRECTION_)
  263. write_reg(0x0001,0x0000); //Reverse Display
  264. #else
  265. write_reg(0x0001,0x0100); //
  266. #endif
  267. write_reg(0x0002,0x0700); //power on sequence
  268. /* [5:4]-ID1~ID0 [3]-AM-1垂直-0水平 */
  269. write_reg(0x0003,(1<<12)|(1<<5)|(0<<4) | (1<<3) );
  270. write_reg(0x0004,0x0000);
  271. write_reg(0x0008,0x0207);
  272. write_reg(0x0009,0x0000);
  273. write_reg(0x000a,0x0000); //display setting
  274. write_reg(0x000c,0x0001); //display setting
  275. write_reg(0x000d,0x0000); //0f3c
  276. write_reg(0x000f,0x0000);
  277. //Power On sequence //
  278. write_reg(0x0010,0x0000);
  279. write_reg(0x0011,0x0007);
  280. write_reg(0x0012,0x0000);
  281. write_reg(0x0013,0x0000);
  282. delay(15);
  283. write_reg(0x0010,0x1590);
  284. write_reg(0x0011,0x0227);
  285. delay(15);
  286. write_reg(0x0012,0x009c);
  287. delay(15);
  288. write_reg(0x0013,0x1900);
  289. write_reg(0x0029,0x0023);
  290. write_reg(0x002b,0x000e);
  291. delay(15);
  292. write_reg(0x0020,0x0000);
  293. write_reg(0x0021,0x0000);
  294. delay(15);
  295. write_reg(0x0030,0x0007);
  296. write_reg(0x0031,0x0707);
  297. write_reg(0x0032,0x0006);
  298. write_reg(0x0035,0x0704);
  299. write_reg(0x0036,0x1f04);
  300. write_reg(0x0037,0x0004);
  301. write_reg(0x0038,0x0000);
  302. write_reg(0x0039,0x0706);
  303. write_reg(0x003c,0x0701);
  304. write_reg(0x003d,0x000f);
  305. delay(15);
  306. write_reg(0x0050,0x0000);
  307. write_reg(0x0051,0x00ef);
  308. write_reg(0x0052,0x0000);
  309. write_reg(0x0053,0x013f);
  310. #if defined(_ILI_REVERSE_DIRECTION_)
  311. write_reg(0x0060,0x2700);
  312. #else
  313. write_reg(0x0060,0xA700);
  314. #endif
  315. write_reg(0x0061,0x0001);
  316. write_reg(0x006a,0x0000);
  317. write_reg(0x0080,0x0000);
  318. write_reg(0x0081,0x0000);
  319. write_reg(0x0082,0x0000);
  320. write_reg(0x0083,0x0000);
  321. write_reg(0x0084,0x0000);
  322. write_reg(0x0085,0x0000);
  323. write_reg(0x0090,0x0010);
  324. write_reg(0x0092,0x0000);
  325. write_reg(0x0093,0x0003);
  326. write_reg(0x0095,0x0110);
  327. write_reg(0x0097,0x0000);
  328. write_reg(0x0098,0x0000);
  329. //display on sequence
  330. write_reg(0x0007,0x0133);
  331. write_reg(0x0020,0x0000);
  332. write_reg(0x0021,0x0000);
  333. }
  334. else if( deviceid==0x9320|| deviceid==0x9300)
  335. {
  336. write_reg(0x00,0x0000);
  337. #if defined(_ILI_REVERSE_DIRECTION_)
  338. write_reg(0x0001,0x0100); //Reverse Display
  339. #else
  340. write_reg(0x0001,0x0000); // Driver Output Contral.
  341. #endif
  342. write_reg(0x02,0x0700); //LCD Driver Waveform Contral.
  343. // write_reg(0x03,0x1030); //Entry Mode Set.
  344. write_reg(0x03,0x1018); //Entry Mode Set.
  345. write_reg(0x04,0x0000); //Scalling Contral.
  346. write_reg(0x08,0x0202); //Display Contral 2.(0x0207)
  347. write_reg(0x09,0x0000); //Display Contral 3.(0x0000)
  348. write_reg(0x0a,0x0000); //Frame Cycle Contal.(0x0000)
  349. write_reg(0x0c,(1<<0)); //Extern Display Interface Contral 1.(0x0000)
  350. write_reg(0x0d,0x0000); //Frame Maker Position.
  351. write_reg(0x0f,0x0000); //Extern Display Interface Contral 2.
  352. delay(15);
  353. write_reg(0x07,0x0101); //Display Contral.
  354. delay(15);
  355. write_reg(0x10,(1<<12)|(0<<8)|(1<<7)|(1<<6)|(0<<4)); //Power Control 1.(0x16b0)
  356. write_reg(0x11,0x0007); //Power Control 2.(0x0001)
  357. write_reg(0x12,(1<<8)|(1<<4)|(0<<0)); //Power Control 3.(0x0138)
  358. write_reg(0x13,0x0b00); //Power Control 4.
  359. write_reg(0x29,0x0000); //Power Control 7.
  360. write_reg(0x2b,(1<<14)|(1<<4));
  361. write_reg(0x50,0); //Set X Start.
  362. write_reg(0x51,239); //Set X End.
  363. write_reg(0x52,0); //Set Y Start.
  364. write_reg(0x53,319); //Set Y End.
  365. #if defined(_ILI_REVERSE_DIRECTION_)
  366. write_reg(0x0060,0x2700); //Driver Output Control.
  367. #else
  368. write_reg(0x0060,0xA700);
  369. #endif
  370. write_reg(0x61,0x0001); //Driver Output Control.
  371. write_reg(0x6a,0x0000); //Vertical Srcoll Control.
  372. write_reg(0x80,0x0000); //Display Position? Partial Display 1.
  373. write_reg(0x81,0x0000); //RAM Address Start? Partial Display 1.
  374. write_reg(0x82,0x0000); //RAM Address End-Partial Display 1.
  375. write_reg(0x83,0x0000); //Displsy Position? Partial Display 2.
  376. write_reg(0x84,0x0000); //RAM Address Start? Partial Display 2.
  377. write_reg(0x85,0x0000); //RAM Address End? Partial Display 2.
  378. write_reg(0x90,(0<<7)|(16<<0)); //Frame Cycle Contral.(0x0013)
  379. write_reg(0x92,0x0000); //Panel Interface Contral 2.(0x0000)
  380. write_reg(0x93,0x0001); //Panel Interface Contral 3.
  381. write_reg(0x95,0x0110); //Frame Cycle Contral.(0x0110)
  382. write_reg(0x97,(0<<8)); //
  383. write_reg(0x98,0x0000); //Frame Cycle Contral.
  384. write_reg(0x07,0x0173); //(0x0173)
  385. }
  386. else if( deviceid==0x4531 )
  387. {
  388. // Setup display
  389. write_reg(0x00,0x0001);
  390. write_reg(0x10,0x0628);
  391. write_reg(0x12,0x0006);
  392. write_reg(0x13,0x0A32);
  393. write_reg(0x11,0x0040);
  394. write_reg(0x15,0x0050);
  395. write_reg(0x12,0x0016);
  396. delay(15);
  397. write_reg(0x10,0x5660);
  398. delay(15);
  399. write_reg(0x13,0x2A4E);
  400. #if defined(_ILI_REVERSE_DIRECTION_)
  401. write_reg(0x01,0x0100);
  402. #else
  403. write_reg(0x01,0x0000);
  404. #endif
  405. write_reg(0x02,0x0300);
  406. write_reg(0x03,0x1030);
  407. // write_reg(0x03,0x1038);
  408. write_reg(0x08,0x0202);
  409. write_reg(0x0A,0x0000);
  410. write_reg(0x30,0x0000);
  411. write_reg(0x31,0x0402);
  412. write_reg(0x32,0x0106);
  413. write_reg(0x33,0x0700);
  414. write_reg(0x34,0x0104);
  415. write_reg(0x35,0x0301);
  416. write_reg(0x36,0x0707);
  417. write_reg(0x37,0x0305);
  418. write_reg(0x38,0x0208);
  419. write_reg(0x39,0x0F0B);
  420. delay(15);
  421. write_reg(0x41,0x0002);
  422. #if defined(_ILI_REVERSE_DIRECTION_)
  423. write_reg(0x0060,0x2700);
  424. #else
  425. write_reg(0x0060,0xA700);
  426. #endif
  427. write_reg(0x61,0x0001);
  428. write_reg(0x90,0x0119);
  429. write_reg(0x92,0x010A);
  430. write_reg(0x93,0x0004);
  431. write_reg(0xA0,0x0100);
  432. // write_reg(0x07,0x0001);
  433. delay(15);
  434. // write_reg(0x07,0x0021);
  435. delay(15);
  436. // write_reg(0x07,0x0023);
  437. delay(15);
  438. // write_reg(0x07,0x0033);
  439. delay(15);
  440. write_reg(0x07,0x0133);
  441. delay(15);
  442. write_reg(0xA0,0x0000);
  443. delay(20);
  444. }
  445. else if( deviceid ==0x7783)
  446. {
  447. // Start Initial Sequence
  448. write_reg(0x00FF,0x0001);
  449. write_reg(0x00F3,0x0008);
  450. write_reg(0x0001,0x0100);
  451. write_reg(0x0002,0x0700);
  452. write_reg(0x0003,0x1030); //0x1030
  453. write_reg(0x0008,0x0302);
  454. write_reg(0x0008,0x0207);
  455. write_reg(0x0009,0x0000);
  456. write_reg(0x000A,0x0000);
  457. write_reg(0x0010,0x0000); //0x0790
  458. write_reg(0x0011,0x0005);
  459. write_reg(0x0012,0x0000);
  460. write_reg(0x0013,0x0000);
  461. delay(20);
  462. write_reg(0x0010,0x12B0);
  463. delay(20);
  464. write_reg(0x0011,0x0007);
  465. delay(20);
  466. write_reg(0x0012,0x008B);
  467. delay(20);
  468. write_reg(0x0013,0x1700);
  469. delay(20);
  470. write_reg(0x0029,0x0022);
  471. //################# void Gamma_Set(void) ####################//
  472. write_reg(0x0030,0x0000);
  473. write_reg(0x0031,0x0707);
  474. write_reg(0x0032,0x0505);
  475. write_reg(0x0035,0x0107);
  476. write_reg(0x0036,0x0008);
  477. write_reg(0x0037,0x0000);
  478. write_reg(0x0038,0x0202);
  479. write_reg(0x0039,0x0106);
  480. write_reg(0x003C,0x0202);
  481. write_reg(0x003D,0x0408);
  482. delay(20);
  483. write_reg(0x0050,0x0000);
  484. write_reg(0x0051,0x00EF);
  485. write_reg(0x0052,0x0000);
  486. write_reg(0x0053,0x013F);
  487. write_reg(0x0060,0xA700);
  488. write_reg(0x0061,0x0001);
  489. write_reg(0x0090,0x0033);
  490. write_reg(0x002B,0x000B);
  491. write_reg(0x0007,0x0133);
  492. delay(20);
  493. }
  494. //数据总线测试,用于测试硬件连接是否正常.
  495. lcd_data_bus_test();
  496. //清屏
  497. lcd_clear( Blue );
  498. }
  499. /* 设置像素点 颜色,X,Y */
  500. void rt_hw_lcd_set_pixel(const char* pixel, int x, int y)
  501. {
  502. lcd_SetCursor(x,y);
  503. rw_data_prepare();
  504. write_data(*(rt_uint16_t*)pixel);
  505. }
  506. /* 获取像素点颜色 */
  507. void rt_hw_lcd_get_pixel(char* pixel, int x, int y)
  508. {
  509. *(rt_uint16_t*)pixel = BGR2RGB( lcd_read_gram(x,y) );
  510. }
  511. /* 画水平线 */
  512. void rt_hw_lcd_draw_hline(const char* pixel, int x1, int x2, int y)
  513. {
  514. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  515. write_reg(0x0003,(1<<12)|(1<<5)|(1<<4) | (0<<3) );
  516. lcd_SetCursor(x1, y);
  517. rw_data_prepare(); /* Prepare to write GRAM */
  518. while (x1 < x2)
  519. {
  520. write_data( *(rt_uint16_t*)pixel );
  521. x1++;
  522. }
  523. }
  524. /* 垂直线 */
  525. void rt_hw_lcd_draw_vline(const char* pixel, int x, int y1, int y2)
  526. {
  527. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  528. write_reg(0x0003,(1<<12)|(1<<5)|(0<<4) | (1<<3) );
  529. lcd_SetCursor(x, y1);
  530. rw_data_prepare(); /* Prepare to write GRAM */
  531. while (y1 < y2)
  532. {
  533. write_data( *(rt_uint16_t*)pixel );
  534. y1++;
  535. }
  536. }
  537. /* ?? */
  538. void rt_hw_lcd_draw_blit_line(const char* pixels, int x, int y, rt_size_t size)
  539. {
  540. rt_uint16_t *ptr;
  541. ptr = (rt_uint16_t*)pixels;
  542. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  543. write_reg(0x0003,(1<<12)|(1<<5)|(1<<4) | (0<<3) );
  544. lcd_SetCursor(x, y);
  545. rw_data_prepare(); /* Prepare to write GRAM */
  546. while (size)
  547. {
  548. write_data(*ptr ++);
  549. size --;
  550. }
  551. }
  552. struct rt_device_graphic_ops lcd_ili_ops =
  553. {
  554. rt_hw_lcd_set_pixel,
  555. rt_hw_lcd_get_pixel,
  556. rt_hw_lcd_draw_hline,
  557. rt_hw_lcd_draw_vline,
  558. rt_hw_lcd_draw_blit_line
  559. };
  560. struct rt_device _lcd_device;
  561. static rt_err_t lcd_init(rt_device_t dev)
  562. {
  563. return RT_EOK;
  564. }
  565. static rt_err_t lcd_open(rt_device_t dev, rt_uint16_t oflag)
  566. {
  567. return RT_EOK;
  568. }
  569. static rt_err_t lcd_close(rt_device_t dev)
  570. {
  571. return RT_EOK;
  572. }
  573. static rt_err_t lcd_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  574. {
  575. switch (cmd)
  576. {
  577. case RTGRAPHIC_CTRL_GET_INFO:
  578. {
  579. struct rt_device_graphic_info *info;
  580. info = (struct rt_device_graphic_info*) args;
  581. RT_ASSERT(info != RT_NULL);
  582. info->bits_per_pixel = 16;
  583. info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
  584. info->framebuffer = RT_NULL;
  585. info->width = 240;
  586. info->height = 320;
  587. }
  588. break;
  589. case RTGRAPHIC_CTRL_RECT_UPDATE:
  590. /* nothong to be done */
  591. break;
  592. default:
  593. break;
  594. }
  595. return RT_EOK;
  596. }
  597. void rt_hw_lcd_init(void)
  598. {
  599. /* LCD RESET */
  600. /* PF10 : LCD RESET */
  601. {
  602. GPIO_InitTypeDef GPIO_InitStructure;
  603. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
  604. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  605. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  606. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  607. GPIO_Init(GPIOF,&GPIO_InitStructure);
  608. GPIO_ResetBits(GPIOF,GPIO_Pin_10);
  609. GPIO_SetBits(GPIOF,GPIO_Pin_10);
  610. /* wait for lcd reset */
  611. rt_thread_delay(1);
  612. }
  613. /* register lcd device */
  614. _lcd_device.type = RT_Device_Class_Graphic;
  615. _lcd_device.init = lcd_init;
  616. _lcd_device.open = lcd_open;
  617. _lcd_device.close = lcd_close;
  618. _lcd_device.control = lcd_control;
  619. _lcd_device.read = RT_NULL;
  620. _lcd_device.write = RT_NULL;
  621. _lcd_device.user_data = &lcd_ili_ops;
  622. lcd_Initializtion();
  623. /* register graphic device driver */
  624. rt_device_register(&_lcd_device, "lcd",
  625. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  626. }