lcd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #include "lcd.h"
  2. #include "finsh.h"
  3. #include "stm32f10x_lib.h"
  4. #include "stm32f10x_rcc.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. #define HW_COLOR_FROM(c) \
  23. (((RTGUI_RGB_R(c) >> 3) << 11) | \
  24. ((RTGUI_RGB_B(c) >> 2) << 5) | \
  25. ((RTGUI_RGB_B(c) >> 3) & 0x1f))
  26. #define HW_COLOR_TO(c) \
  27. ((c & 0x1f) * 255 / 31) | \
  28. (((c >> 5) & 0x3f) * 255 / 63) | \
  29. (((c >> 11) & 0x1f) * 255 / 31)
  30. #ifdef RT_USING_FRAMEBUFFER
  31. rt_uint16_t _rt_hw_framebuffer[320 x 240];
  32. #endif
  33. /*******************************************************************************
  34. * Function Name : LCD_WriteReg
  35. * Description : Writes to the selected LCD register.
  36. * Input : - LCD_Reg: address of the selected register.
  37. * - LCD_RegValue: value to write to the selected register.
  38. * Output : None
  39. * Return : None
  40. *******************************************************************************/
  41. void LCD_WriteReg(rt_uint8_t LCD_Reg, rt_uint16_t LCD_RegValue)
  42. {
  43. /* Write 16-bit Index, then Write Reg */
  44. LCD->LCD_REG = LCD_Reg;
  45. /* Write 16-bit Reg */
  46. LCD->LCD_RAM = LCD_RegValue;
  47. }
  48. /*******************************************************************************
  49. * Function Name : LCD_ReadReg
  50. * Description : Reads the selected LCD Register.
  51. * Input : None
  52. * Output : None
  53. * Return : LCD Register Value.
  54. *******************************************************************************/
  55. rt_uint16_t LCD_ReadReg(rt_uint8_t LCD_Reg)
  56. {
  57. /* Write 16-bit Index (then Read Reg) */
  58. LCD->LCD_REG = LCD_Reg;
  59. /* Read 16-bit Reg */
  60. return (LCD->LCD_RAM);
  61. }
  62. /*******************************************************************************
  63. * Function Name : LCD_WriteRAM_Prepare
  64. * Description : Prepare to write to the LCD RAM.
  65. * Input : None
  66. * Output : None
  67. * Return : None
  68. *******************************************************************************/
  69. void LCD_WriteRAM_Prepare(void)
  70. {
  71. LCD->LCD_REG = R34;
  72. }
  73. /*******************************************************************************
  74. * Function Name : LCD_WriteRAM
  75. * Description : Writes to the LCD RAM.
  76. * Input : - RGB_Code: the pixel color in RGB mode (5-6-5).
  77. * Output : None
  78. * Return : None
  79. *******************************************************************************/
  80. rt_inline void LCD_WriteRAM(rt_uint16_t RGB_Code)
  81. {
  82. /* Write 16-bit GRAM Reg */
  83. LCD->LCD_RAM = RGB_Code;
  84. }
  85. /*******************************************************************************
  86. * Function Name : LCD_ReadRAM
  87. * Description : Reads the LCD RAM.
  88. * Input : None
  89. * Output : None
  90. * Return : LCD RAM Value.
  91. *******************************************************************************/
  92. rt_inline rt_uint16_t LCD_ReadRAM(void)
  93. {
  94. /* Write 16-bit Index (then Read Reg) */
  95. LCD->LCD_REG = R34; /* Select GRAM Reg */
  96. /* Read 16-bit Reg */
  97. return LCD->LCD_RAM;
  98. }
  99. /*******************************************************************************
  100. * Function Name : LCD_DisplayOn
  101. * Description : Enables the Display.
  102. * Input : None
  103. * Output : None
  104. * Return : None
  105. *******************************************************************************/
  106. void LCD_DisplayOn(void)
  107. {
  108. /* Display On */
  109. LCD_WriteReg(0x26, 0x3C); /* 262K color and display ON */
  110. }
  111. /*******************************************************************************
  112. * Function Name : LCD_DisplayOff
  113. * Description : Disables the Display.
  114. * Input : None
  115. * Output : None
  116. * Return : None
  117. *******************************************************************************/
  118. void LCD_DisplayOff(void)
  119. {
  120. /* Display Off */
  121. LCD_WriteReg(0x26, 0x0);
  122. }
  123. /*******************************************************************************
  124. * Function Name : LCD_SetCursor
  125. * Description : Sets the cursor position.
  126. * Input : - Xpos: specifies the X position.
  127. * - Ypos: specifies the Y position.
  128. * Output : None
  129. * Return : None
  130. *******************************************************************************/
  131. void LCD_SetCursor(rt_uint32_t x, rt_uint32_t y)
  132. {
  133. LCD_WriteReg(0x06, (x & 0xff00) >> 8);
  134. LCD_WriteReg(0x07, (x & 0x00ff));
  135. LCD_WriteReg(0x02, (y & 0xff00) >> 8);
  136. LCD_WriteReg(0x03, (y & 0x00ff));
  137. }
  138. /*******************************************************************************
  139. * Function Name : LCD_CtrlLinesConfig
  140. * Description : Configures LCD Control lines (FSMC Pins) in alternate function
  141. Push-Pull mode.
  142. * Input : None
  143. * Output : None
  144. * Return : None
  145. *******************************************************************************/
  146. void LCD_CtrlLinesConfig(void)
  147. {
  148. GPIO_InitTypeDef GPIO_InitStructure;
  149. /* Enable FSMC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
  150. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
  151. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
  152. RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG |
  153. RCC_APB2Periph_AFIO, ENABLE);
  154. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  155. //±³¹â
  156. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  157. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  158. GPIO_Init(GPIOA, &GPIO_InitStructure);
  159. GPIO_ResetBits(GPIOA, GPIO_Pin_8);
  160. //·äÃùÆ÷
  161. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  162. GPIO_Init(GPIOC, &GPIO_InitStructure);
  163. GPIO_SetBits(GPIOC, GPIO_Pin_6);
  164. /* Set PD.00(D2), PD.01(D3), PD.04(NOE), PD.05(NWE), PD.08(D13), PD.09(D14),
  165. PD.10(D15), PD.14(D0), PD.15(D1) as alternate
  166. function push pull */
  167. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
  168. GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 |
  169. GPIO_Pin_15;
  170. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  171. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  172. GPIO_Init(GPIOD, &GPIO_InitStructure);
  173. /* Set PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
  174. PE.14(D11), PE.15(D12) as alternate function push pull */
  175. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
  176. GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
  177. GPIO_Pin_15;
  178. GPIO_Init(GPIOE, &GPIO_InitStructure);
  179. // GPIO_WriteBit(GPIOE, GPIO_Pin_6, Bit_SET);
  180. /* Set PF.00(A0 (RS)) as alternate function push pull */
  181. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  182. GPIO_Init(GPIOF, &GPIO_InitStructure);
  183. /* Set PG.12(NE4 (LCD/CS)) as alternate function push pull - CE3(LCD /CS) */
  184. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  185. GPIO_Init(GPIOG, &GPIO_InitStructure);
  186. }
  187. /*******************************************************************************
  188. * Function Name : LCD_FSMCConfig
  189. * Description : Configures the Parallel interface (FSMC) for LCD(Parallel mode)
  190. * Input : None
  191. * Output : None
  192. * Return : None
  193. *******************************************************************************/
  194. void LCD_FSMCConfig(void)
  195. {
  196. FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
  197. FSMC_NORSRAMTimingInitTypeDef p;
  198. /*-- FSMC Configuration ------------------------------------------------------*/
  199. /*----------------------- SRAM Bank 4 ----------------------------------------*/
  200. /* FSMC_Bank1_NORSRAM4 configuration */
  201. p.FSMC_AddressSetupTime = 0;
  202. p.FSMC_AddressHoldTime = 0;
  203. p.FSMC_DataSetupTime = 2;
  204. p.FSMC_BusTurnAroundDuration = 0;
  205. p.FSMC_CLKDivision = 0;
  206. p.FSMC_DataLatency = 0;
  207. p.FSMC_AccessMode = FSMC_AccessMode_A;
  208. /* Color LCD configuration ------------------------------------
  209. LCD configured as follow:
  210. - Data/Address MUX = Disable
  211. - Memory Type = SRAM
  212. - Data Width = 16bit
  213. - Write Operation = Enable
  214. - Extended Mode = Enable
  215. - Asynchronous Wait = Disable */
  216. FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM4;
  217. FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  218. FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  219. FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  220. FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  221. FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  222. FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
  223. FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  224. FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  225. FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  226. FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  227. // FSMC_NORSRAMInitStructure.FSMC_AsyncWait = FSMC_AsyncWait_Disable;
  228. FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  229. FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  230. FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
  231. FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
  232. /* BANK 4 (of NOR/SRAM Bank 1~4) is enabled */
  233. FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE);
  234. }
  235. void rt_hw_lcd_update(rtgui_rect_t *rect)
  236. {
  237. /* nothing */
  238. }
  239. rt_uint8_t * rt_hw_lcd_get_framebuffer(void)
  240. {
  241. #ifdef RT_USING_FRAMEBUFFER
  242. return (rt_uint8_t *)_rt_hw_framebuffer;
  243. #else
  244. return RT_NULL;
  245. #endif
  246. }
  247. void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  248. {
  249. LCD_SetCursor(x, 319 - y);
  250. /* Prepare to write GRAM */
  251. LCD_WriteRAM_Prepare();
  252. LCD_WriteRAM(HW_COLOR_FROM(*c));
  253. }
  254. void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  255. {
  256. rt_uint16_t hc;
  257. LCD_SetCursor(x, 319 - y);
  258. hc = LCD_ReadRAM();
  259. *c = HW_COLOR_TO(hc);
  260. }
  261. void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  262. {
  263. rt_uint32_t index;
  264. rt_uint16_t hc;
  265. hc = HW_COLOR_FROM(*c);
  266. for (index = x1; index < x2; index ++)
  267. {
  268. LCD_SetCursor(index, 319 - y);
  269. /* Prepare to write GRAM */
  270. LCD_WriteRAM_Prepare();
  271. LCD_WriteRAM(hc);
  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_uint32_t index;
  277. rt_uint16_t hc;
  278. hc = HW_COLOR_FROM(*c);
  279. for (index = y1; index < y2; index ++)
  280. {
  281. LCD_SetCursor(x, 319 - index);
  282. /* Prepare to write GRAM */
  283. LCD_WriteRAM_Prepare();
  284. LCD_WriteRAM(hc);
  285. }
  286. }
  287. struct rtgui_graphic_driver _rtgui_lcd_driver =
  288. {
  289. "lcd",
  290. 2,
  291. 240,
  292. 320,
  293. rt_hw_lcd_update,
  294. rt_hw_lcd_get_framebuffer,
  295. rt_hw_lcd_set_pixel,
  296. rt_hw_lcd_get_pixel,
  297. rt_hw_lcd_draw_hline,
  298. rt_hw_lcd_draw_vline
  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. void hline(rt_uint32_t c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  394. {
  395. rtgui_color_t color = (rtgui_color_t)c;
  396. rt_hw_lcd_draw_hline(&color, x1, x2, y);
  397. }
  398. FINSH_FUNCTION_EXPORT(hline, Horizontal Line)
  399. void vline(rt_uint32_t c, rt_base_t x, rt_base_t y1, rt_base_t y2)
  400. {
  401. rtgui_color_t color = (rtgui_color_t)c;
  402. rt_hw_lcd_draw_vline(&color, x, y1, y2);
  403. }
  404. FINSH_FUNCTION_EXPORT(vline, Vertical Line)
  405. FINSH_FUNCTION_EXPORT(rt_hw_lcd_init, LCD Init)
  406. void clear()
  407. {
  408. rt_uint32_t index;
  409. #if 0
  410. for (index = 0; index < 320; index ++)
  411. {
  412. rt_hw_lcd_draw_hline((rtgui_color_t*)&white, 0, 240, index);
  413. }
  414. #else
  415. for (index = 0; index < 240; index ++)
  416. {
  417. rt_hw_lcd_draw_vline((rtgui_color_t*)&white, index, 0, 320);
  418. }
  419. #endif
  420. }
  421. FINSH_FUNCTION_EXPORT(clear, clear screen)
  422. #endif