ili_lcd_general.c 21 KB

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