stm3210e_eval_lcd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * File : stm3210e_eval_lcd.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-11-01 Bernard the first version
  13. */
  14. #include <rtthread.h>
  15. #include "stm32f10x.h"
  16. #include "stm32f10x_fsmc.h"
  17. #include "stm3210e_eval_lcd.h"
  18. #ifdef RT_USING_RTGUI
  19. #include <rtgui/driver.h>
  20. #include <rtgui/color.h>
  21. /*
  22. * LCD Driver
  23. * RGB mode (5-6-5)
  24. * 240 x 320 pixel LCD
  25. */
  26. /* convert rtgui color to hardware color, rgb 5-6-5 */
  27. typedef struct
  28. {
  29. rt_uint16_t LCD_REG;
  30. rt_uint16_t LCD_RAM;
  31. } LCD_TypeDef;
  32. /* Note: LCD /CS is CE4 - Bank 4 of NOR/SRAM Bank 1~4 */
  33. #define LCD_BASE ((rt_uint32_t)(0x60000000 | 0x0C000000))
  34. #define LCD ((LCD_TypeDef *) LCD_BASE)
  35. /*******************************************************************************
  36. * Function Name : LCD_WriteReg
  37. * Description : Writes to the selected LCD register.
  38. * Input : - LCD_Reg: address of the selected register.
  39. * - LCD_RegValue: value to write to the selected register.
  40. * Output : None
  41. * Return : None
  42. *******************************************************************************/
  43. void LCD_WriteReg(rt_uint8_t LCD_Reg, rt_uint16_t LCD_RegValue)
  44. {
  45. /* Write 16-bit Index, then Write Reg */
  46. LCD->LCD_REG = LCD_Reg;
  47. /* Write 16-bit Reg */
  48. LCD->LCD_RAM = LCD_RegValue;
  49. }
  50. /*******************************************************************************
  51. * Function Name : LCD_ReadReg
  52. * Description : Reads the selected LCD Register.
  53. * Input : None
  54. * Output : None
  55. * Return : LCD Register Value.
  56. *******************************************************************************/
  57. rt_uint16_t LCD_ReadReg(rt_uint8_t LCD_Reg)
  58. {
  59. /* Write 16-bit Index (then Read Reg) */
  60. LCD->LCD_REG = LCD_Reg;
  61. /* Read 16-bit Reg */
  62. return (LCD->LCD_RAM);
  63. }
  64. /*******************************************************************************
  65. * Function Name : LCD_WriteRAM_Prepare
  66. * Description : Prepare to write to the LCD RAM.
  67. * Input : None
  68. * Output : None
  69. * Return : None
  70. *******************************************************************************/
  71. void LCD_WriteRAM_Prepare(void)
  72. {
  73. LCD->LCD_REG = R34;
  74. }
  75. /*******************************************************************************
  76. * Function Name : LCD_WriteRAM
  77. * Description : Writes to the LCD RAM.
  78. * Input : - RGB_Code: the pixel color in RGB mode (5-6-5).
  79. * Output : None
  80. * Return : None
  81. *******************************************************************************/
  82. rt_inline void LCD_WriteRAM(rt_uint16_t RGB_Code)
  83. {
  84. /* Write 16-bit GRAM Reg */
  85. LCD->LCD_RAM = RGB_Code;
  86. }
  87. /*******************************************************************************
  88. * Function Name : LCD_ReadRAM
  89. * Description : Reads the LCD RAM.
  90. * Input : None
  91. * Output : None
  92. * Return : LCD RAM Value.
  93. *******************************************************************************/
  94. rt_inline rt_uint16_t LCD_ReadRAM(void)
  95. {
  96. /* Write 16-bit Index (then Read Reg) */
  97. LCD->LCD_REG = R34; /* Select GRAM Reg */
  98. /* Read 16-bit Reg */
  99. return LCD->LCD_RAM;
  100. }
  101. /*******************************************************************************
  102. * Function Name : LCD_DisplayOn
  103. * Description : Enables the Display.
  104. * Input : None
  105. * Output : None
  106. * Return : None
  107. *******************************************************************************/
  108. void LCD_DisplayOn(void)
  109. {
  110. /* Display On */
  111. LCD_WriteReg(0x26, 0x3C); /* 262K color and display ON */
  112. }
  113. /*******************************************************************************
  114. * Function Name : LCD_DisplayOff
  115. * Description : Disables the Display.
  116. * Input : None
  117. * Output : None
  118. * Return : None
  119. *******************************************************************************/
  120. void LCD_DisplayOff(void)
  121. {
  122. /* Display Off */
  123. LCD_WriteReg(0x26, 0x0);
  124. }
  125. /*******************************************************************************
  126. * Function Name : LCD_SetCursor
  127. * Description : Sets the cursor position.
  128. * Input : - Xpos: specifies the X position.
  129. * - Ypos: specifies the Y position.
  130. * Output : None
  131. * Return : None
  132. *******************************************************************************/
  133. void LCD_SetCursor(rt_uint32_t x, rt_uint32_t y)
  134. {
  135. LCD_WriteReg(0x06, (x & 0xff00) >> 8);
  136. LCD_WriteReg(0x07, (x & 0x00ff));
  137. LCD_WriteReg(0x02, (y & 0xff00) >> 8);
  138. LCD_WriteReg(0x03, (y & 0x00ff));
  139. }
  140. /*******************************************************************************
  141. * Function Name : LCD_CtrlLinesConfig
  142. * Description : Configures LCD Control lines (FSMC Pins) in alternate function
  143. Push-Pull mode.
  144. * Input : None
  145. * Output : None
  146. * Return : None
  147. *******************************************************************************/
  148. void LCD_CtrlLinesConfig(void)
  149. {
  150. GPIO_InitTypeDef GPIO_InitStructure;
  151. /* Enable FSMC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
  152. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  153. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
  154. RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG |
  155. RCC_APB2Periph_AFIO, ENABLE);
  156. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  157. //±³¹â
  158. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  159. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  160. GPIO_Init(GPIOA, &GPIO_InitStructure);
  161. GPIO_ResetBits(GPIOA, GPIO_Pin_8);
  162. //·äÃùÆ÷
  163. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  164. GPIO_Init(GPIOC, &GPIO_InitStructure);
  165. GPIO_SetBits(GPIOC, GPIO_Pin_6);
  166. /* Set PD.00(D2), PD.01(D3), PD.04(NOE), PD.05(NWE), PD.08(D13), PD.09(D14),
  167. PD.10(D15), PD.14(D0), PD.15(D1) as alternate
  168. function push pull */
  169. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
  170. GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 |
  171. GPIO_Pin_15;
  172. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  173. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  174. GPIO_Init(GPIOD, &GPIO_InitStructure);
  175. /* Set PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
  176. PE.14(D11), PE.15(D12) as alternate function push pull */
  177. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
  178. GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
  179. GPIO_Pin_15;
  180. GPIO_Init(GPIOE, &GPIO_InitStructure);
  181. // GPIO_WriteBit(GPIOE, GPIO_Pin_6, Bit_SET);
  182. /* Set PF.00(A0 (RS)) as alternate function push pull */
  183. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  184. GPIO_Init(GPIOF, &GPIO_InitStructure);
  185. /* Set PG.12(NE4 (LCD/CS)) as alternate function push pull - CE3(LCD /CS) */
  186. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  187. GPIO_Init(GPIOG, &GPIO_InitStructure);
  188. }
  189. /*******************************************************************************
  190. * Function Name : LCD_FSMCConfig
  191. * Description : Configures the Parallel interface (FSMC) for LCD(Parallel mode)
  192. * Input : None
  193. * Output : None
  194. * Return : None
  195. *******************************************************************************/
  196. void LCD_FSMCConfig(void)
  197. {
  198. FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
  199. FSMC_NORSRAMTimingInitTypeDef p;
  200. /*-- FSMC Configuration ------------------------------------------------------*/
  201. /*----------------------- SRAM Bank 4 ----------------------------------------*/
  202. /* FSMC_Bank1_NORSRAM4 configuration */
  203. p.FSMC_AddressSetupTime = 0;
  204. p.FSMC_AddressHoldTime = 0;
  205. p.FSMC_DataSetupTime = 2;
  206. p.FSMC_BusTurnAroundDuration = 0;
  207. p.FSMC_CLKDivision = 0;
  208. p.FSMC_DataLatency = 0;
  209. p.FSMC_AccessMode = FSMC_AccessMode_A;
  210. /* Color LCD configuration ------------------------------------
  211. LCD configured as follow:
  212. - Data/Address MUX = Disable
  213. - Memory Type = SRAM
  214. - Data Width = 16bit
  215. - Write Operation = Enable
  216. - Extended Mode = Enable
  217. - Asynchronous Wait = Disable */
  218. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;
  219. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  220. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  221. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  222. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  223. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  224. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  225. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  226. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  227. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  228. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  229. // FSMC_NORSRAMInitStructure.FSMC_AsyncWait = FSMC_AsyncWait_Disable;
  230. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  231. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  232. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  233. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  234. /* BANK 4 (of NOR/SRAM Bank 1~4) is enabled */
  235. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE);
  236. }
  237. void rt_hw_lcd_update(rtgui_rect_t *rect)
  238. {
  239. /* nothing */
  240. }
  241. rt_uint8_t * rt_hw_lcd_get_framebuffer(void)
  242. {
  243. return RT_NULL;
  244. }
  245. void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  246. {
  247. unsigned short p;
  248. /* get color pixel */
  249. p = rtgui_color_to_565p(*c);
  250. LCD_SetCursor(y, x);
  251. /* Prepare to write GRAM */
  252. LCD_WriteRAM_Prepare();
  253. LCD_WriteRAM(p);
  254. }
  255. void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  256. {
  257. rt_uint16_t hc;
  258. LCD_SetCursor(y, x);
  259. hc = LCD_ReadRAM();
  260. *c = rtgui_color_from_565p(hc);
  261. }
  262. void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  263. {
  264. rt_uint16_t hc;
  265. hc = rtgui_color_to_565p(*c);
  266. LCD_SetCursor(y, x1);
  267. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  268. while (x1 < x2)
  269. {
  270. LCD_WriteRAM(hc);
  271. x1 ++;
  272. }
  273. }
  274. void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t y2)
  275. {
  276. rt_uint16_t hc;
  277. hc = rtgui_color_to_565p(*c);
  278. while (y1 < y2)
  279. {
  280. LCD_SetCursor(y1, x); y1 ++;
  281. /* Prepare to write GRAM */
  282. LCD_WriteRAM_Prepare();
  283. LCD_WriteRAM(hc);
  284. }
  285. }
  286. void rt_hw_lcd_draw_raw_hline(rt_uint8_t *pixels, rt_base_t x1, rt_base_t x2, rt_base_t y)
  287. {
  288. rt_uint16_t *ptr;
  289. /* get pixel */
  290. ptr = (rt_uint16_t*) pixels;
  291. LCD_SetCursor(y, x1);
  292. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  293. while (x1 < x2)
  294. {
  295. LCD_WriteRAM(*ptr);
  296. x1 ++; ptr ++;
  297. }
  298. }
  299. struct rtgui_graphic_driver _rtgui_lcd_driver =
  300. {
  301. "lcd",
  302. 2,
  303. 320,
  304. 240,
  305. rt_hw_lcd_update,
  306. rt_hw_lcd_get_framebuffer,
  307. rt_hw_lcd_set_pixel,
  308. rt_hw_lcd_get_pixel,
  309. rt_hw_lcd_draw_hline,
  310. rt_hw_lcd_draw_vline,
  311. rt_hw_lcd_draw_raw_hline
  312. };
  313. #define Delay(v) \
  314. { \
  315. volatile rt_uint32_t index; \
  316. for (index = 0; index < v * 100; index ++) \
  317. ; \
  318. }
  319. void rt_hw_lcd_init()
  320. {
  321. /* Configure the LCD Control pins --------------------------------------------*/
  322. LCD_CtrlLinesConfig();
  323. /* Configure the FSMC Parallel interface -------------------------------------*/
  324. LCD_FSMCConfig();
  325. Delay(5); /* delay 50 ms */
  326. // Gamma for CMO 3.2¡±
  327. LCD_WriteReg(0x46,0x94);
  328. LCD_WriteReg(0x47,0x41);
  329. LCD_WriteReg(0x48,0x00);
  330. LCD_WriteReg(0x49,0x33);
  331. LCD_WriteReg(0x4a,0x23);
  332. LCD_WriteReg(0x4b,0x45);
  333. LCD_WriteReg(0x4c,0x44);
  334. LCD_WriteReg(0x4d,0x77);
  335. LCD_WriteReg(0x4e,0x12);
  336. LCD_WriteReg(0x4f,0xcc);
  337. LCD_WriteReg(0x50,0x46);
  338. LCD_WriteReg(0x51,0x82);
  339. //240x320 window setting
  340. LCD_WriteReg(0x02,0x00);
  341. LCD_WriteReg(0x03,0x00);
  342. LCD_WriteReg(0x04,0x01);
  343. LCD_WriteReg(0x05,0x3f);
  344. LCD_WriteReg(0x06,0x00);
  345. LCD_WriteReg(0x07,0x00);
  346. LCD_WriteReg(0x08,0x00);
  347. LCD_WriteReg(0x09,0xef);
  348. // Display Setting
  349. LCD_WriteReg(0x01,0x06);
  350. LCD_WriteReg(0x16,0x68);
  351. LCD_WriteReg(0x23,0x95);
  352. LCD_WriteReg(0x24,0x95);
  353. LCD_WriteReg(0x25,0xff);
  354. LCD_WriteReg(0x27,0x02);
  355. LCD_WriteReg(0x28,0x02);
  356. LCD_WriteReg(0x29,0x02);
  357. LCD_WriteReg(0x2a,0x02);
  358. LCD_WriteReg(0x2c,0x02);
  359. LCD_WriteReg(0x2d,0x02);
  360. LCD_WriteReg(0x3a,0x01);///*******************
  361. LCD_WriteReg(0x3b,0x01);
  362. LCD_WriteReg(0x3c,0xf0);
  363. LCD_WriteReg(0x3d,0x00);
  364. Delay(2);
  365. LCD_WriteReg(0x35,0x38);
  366. LCD_WriteReg(0x36,0x78);
  367. LCD_WriteReg(0x3e,0x38);
  368. LCD_WriteReg(0x40,0x0f);
  369. LCD_WriteReg(0x41,0xf0);
  370. // Power Supply Setting
  371. LCD_WriteReg(0x19,0x49);//********
  372. LCD_WriteReg(0x93,0x0f);//*******
  373. Delay(1);
  374. LCD_WriteReg(0x20,0x30);
  375. LCD_WriteReg(0x1d,0x07);
  376. LCD_WriteReg(0x1e,0x00);
  377. LCD_WriteReg(0x1f,0x07);
  378. // VCOM Setting for CMO 3.2¡± Panel
  379. LCD_WriteReg(0x44,0x4d);//4d***************4f
  380. LCD_WriteReg(0x45,0x13);//0x0a);
  381. Delay(1);
  382. LCD_WriteReg(0x1c,0x04);
  383. Delay(2);
  384. LCD_WriteReg(0x43,0x80);
  385. Delay(5);
  386. LCD_WriteReg(0x1b,0x08);
  387. Delay(4);
  388. LCD_WriteReg(0x1b,0x10);
  389. Delay(4);
  390. // Display ON Setting
  391. LCD_WriteReg(0x90,0x7f);
  392. LCD_WriteReg(0x26,0x04);
  393. Delay(4);
  394. LCD_WriteReg(0x26,0x24);
  395. LCD_WriteReg(0x26,0x2c);
  396. Delay(4);
  397. LCD_WriteReg(0x26,0x3c);
  398. // Set internal VDDD voltage
  399. LCD_WriteReg(0x57,0x02);
  400. LCD_WriteReg(0x55,0x00);
  401. LCD_WriteReg(0x57,0x00);
  402. /* add lcd driver into graphic driver */
  403. rtgui_list_init(&_rtgui_lcd_driver.list);
  404. rtgui_graphic_driver_add(&_rtgui_lcd_driver);
  405. }
  406. #endif
  407. void stm3210e_rtgui_init()
  408. {
  409. rtgui_rect_t rect;
  410. rtgui_system_server_init();
  411. /* register dock panel */
  412. rect.x1 = 0;
  413. rect.y1 = 0;
  414. rect.x2 = 320;
  415. rect.y2 = 25;
  416. rtgui_panel_register("info", &rect);
  417. /* register main panel */
  418. rect.x1 = 0;
  419. rect.y1 = 25;
  420. rect.x2 = 320;
  421. rect.y2 = 240;
  422. rtgui_panel_register("main", &rect);
  423. rtgui_panel_set_default_focused("main");
  424. rt_hw_lcd_init();
  425. info_init();
  426. today_init();
  427. }