1
0

stm3210e_eval_lcd.c 14 KB

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