lcd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include "stm32f10x.h"
  2. #include "rtthread.h"
  3. #include "board.h"
  4. #include <rtgui/rtgui.h>
  5. #include <rtgui/driver.h>
  6. #include <rtgui/rtgui_server.h>
  7. #include <rtgui/rtgui_system.h>
  8. #if (LCD_VERSION == 1)
  9. #include "fmt0371/FMT0371.h"
  10. #endif
  11. #if (LCD_VERSION == 2)
  12. #include "ili9325/ili9325.h"
  13. #endif
  14. rt_err_t rt_hw_lcd_init(void);
  15. void rt_hw_lcd_update(rtgui_rect_t *rect);
  16. rt_uint8_t * rt_hw_lcd_get_framebuffer(void);
  17. void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y);
  18. void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y);
  19. void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y);
  20. void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t y2);
  21. void rt_hw_lcd_draw_raw_hline(rt_uint8_t *pixels, rt_base_t x1, rt_base_t x2, rt_base_t y);
  22. struct rtgui_graphic_driver _rtgui_lcd_driver =
  23. {
  24. "lcd",
  25. 2,
  26. 240,
  27. 320,
  28. rt_hw_lcd_update,
  29. rt_hw_lcd_get_framebuffer,
  30. rt_hw_lcd_set_pixel,
  31. rt_hw_lcd_get_pixel,
  32. rt_hw_lcd_draw_hline,
  33. rt_hw_lcd_draw_vline,
  34. rt_hw_lcd_draw_raw_hline
  35. };
  36. extern void info_init(void);
  37. extern void player_init(void);
  38. void radio_rtgui_init(void)
  39. {
  40. rtgui_rect_t rect;
  41. rtgui_system_server_init();
  42. /* register dock panel */
  43. rect.x1 = 0;
  44. rect.y1 = 0;
  45. rect.x2 = 240;
  46. rect.y2 = 25;
  47. rtgui_panel_register("info", &rect);
  48. /* register main panel */
  49. rect.x1 = 0;
  50. rect.y1 = 25;
  51. rect.x2 = 320;
  52. rect.y2 = 320;
  53. rtgui_panel_register("main", &rect);
  54. rtgui_panel_set_default_focused("main");
  55. rt_hw_lcd_init();
  56. info_init();
  57. player_init();
  58. }
  59. #if (LCD_VERSION == 1)
  60. void rt_hw_lcd_update(rtgui_rect_t *rect)
  61. {
  62. /* nothing for none-DMA mode driver */
  63. }
  64. rt_uint8_t * rt_hw_lcd_get_framebuffer(void)
  65. {
  66. return RT_NULL; /* no framebuffer driver */
  67. }
  68. void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  69. {
  70. unsigned short p;
  71. /* get color pixel */
  72. p = rtgui_color_to_565p(*c);
  73. /* set X point */
  74. LCD_ADDR = 0x02;
  75. LCD_DATA = x;
  76. /* set Y point */
  77. LCD_ADDR = 0x03;
  78. LCD_DATA16(y);
  79. /* write pixel */
  80. LCD_ADDR = 0x0E;
  81. LCD_DATA16(p);
  82. }
  83. void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  84. {
  85. /* set X point */
  86. LCD_ADDR = 0x02;
  87. LCD_DATA = x;
  88. /* set Y point */
  89. LCD_ADDR = 0x03;
  90. LCD_DATA16( y );
  91. /* read pixel */
  92. LCD_ADDR = 0x0F;
  93. /* dummy read */
  94. x = LCD_DATA;
  95. *c = rtgui_color_from_565p( LCD_DATA16_READ() );
  96. }
  97. void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  98. {
  99. unsigned short p;
  100. /* get color pixel */
  101. p = rtgui_color_to_565p(*c);
  102. /* set X point */
  103. LCD_ADDR = 0x02;
  104. LCD_DATA = x1;
  105. /* set Y point */
  106. LCD_ADDR = 0x03;
  107. LCD_DATA16( y );
  108. /* write pixel */
  109. LCD_ADDR = 0x0E;
  110. while (x1 < x2)
  111. {
  112. LCD_DATA16(p);
  113. x1 ++;
  114. }
  115. }
  116. void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t y2)
  117. {
  118. unsigned short p;
  119. /* get color pixel */
  120. p = rtgui_color_to_565p(*c);
  121. /* set X point */
  122. LCD_ADDR = 0x02;
  123. LCD_DATA = x;
  124. while (y1 < y2)
  125. {
  126. /* set Y point */
  127. LCD_ADDR = 0x03;
  128. LCD_DATA16( y1 );
  129. /* write pixel */
  130. LCD_ADDR = 0x0E;
  131. LCD_DATA16(p);
  132. y1 ++;
  133. }
  134. }
  135. void rt_hw_lcd_draw_raw_hline(rt_uint8_t *pixels, rt_base_t x1, rt_base_t x2, rt_base_t y)
  136. {
  137. rt_uint16_t *ptr;
  138. /* get pixel */
  139. ptr = (rt_uint16_t*) pixels;
  140. /* set X point */
  141. LCD_ADDR = 0x02;
  142. LCD_DATA = x1;
  143. /* set Y point */
  144. LCD_ADDR = 0x03;
  145. LCD_DATA16( y );
  146. /* write pixel */
  147. LCD_ADDR = 0x0E;
  148. while (x1 < x2)
  149. {
  150. LCD_DATA16(*ptr);
  151. x1 ++;
  152. ptr ++;
  153. }
  154. }
  155. rt_err_t rt_hw_lcd_init(void)
  156. {
  157. GPIO_InitTypeDef GPIO_InitStructure;
  158. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
  159. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  160. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  161. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  162. GPIO_Init(GPIOF,&GPIO_InitStructure);
  163. GPIO_SetBits(GPIOF,GPIO_Pin_9);
  164. ftm0371_port_init();
  165. ftm0371_init();
  166. //LCD GRAM test
  167. {
  168. unsigned int test_x;
  169. unsigned int test_y;
  170. unsigned short temp;
  171. rt_kprintf("\r\nLCD GRAM test....");
  172. //write
  173. temp = 0;
  174. for( test_y=0; test_y<320; test_y++)
  175. {
  176. /* set X point */
  177. LCD_ADDR = 0x02;
  178. LCD_DATA = 0;
  179. /* set Y point */
  180. LCD_ADDR = 0x03;
  181. LCD_DATA16( test_y );
  182. /* write pixel */
  183. LCD_ADDR = 0x0E;
  184. for(test_x=0; test_x<240; test_x++)
  185. {
  186. LCD_DATA16(temp++);
  187. }
  188. }
  189. temp = 0;
  190. for( test_y=0; test_y<320; test_y++)
  191. {
  192. /* set X point */
  193. LCD_ADDR = 0x02;
  194. LCD_DATA = 0;
  195. /* set Y point */
  196. LCD_ADDR = 0x03;
  197. LCD_DATA16( test_y );
  198. /* write pixel */
  199. LCD_ADDR = 0x0f;
  200. /* dummy read */
  201. test_x = LCD_DATA;
  202. for(test_x=0; test_x<240; test_x++)
  203. {
  204. if ( LCD_DATA16_READ() != temp++)
  205. {
  206. rt_kprintf(" LCD GRAM ERR!!");
  207. while(1);
  208. }
  209. }
  210. }
  211. rt_kprintf(" TEST PASS!\r\n");
  212. }//LCD GRAM TEST
  213. #ifndef DRIVER_TEST
  214. /* add lcd driver into graphic driver */
  215. rtgui_graphic_driver_add(&_rtgui_lcd_driver);
  216. #endif
  217. return RT_EOK;
  218. }
  219. #include <finsh.h>
  220. void hline(rt_base_t x1, rt_base_t x2, rt_base_t y, rt_uint32_t pixel)
  221. {
  222. rt_hw_lcd_draw_hline(&pixel, x1, x2, y);
  223. }
  224. FINSH_FUNCTION_EXPORT(hline, draw a hline);
  225. void vline(int x, int y1, int y2, rt_uint32_t pixel)
  226. {
  227. rt_hw_lcd_draw_vline(&pixel, x, y1, y2);
  228. }
  229. FINSH_FUNCTION_EXPORT(vline, draw a vline);
  230. void cls()
  231. {
  232. rt_size_t index;
  233. rtgui_color_t white = RTGUI_RGB(0xff, 0xff, 0xff);
  234. for (index = 0; index < 320; index ++)
  235. rt_hw_lcd_draw_hline(&white, 0, 240, index);
  236. }
  237. FINSH_FUNCTION_EXPORT(cls, clear screen);
  238. #endif
  239. #if (LCD_VERSION == 2)
  240. void rt_hw_lcd_update(rtgui_rect_t *rect)
  241. {
  242. /* nothing for none-DMA mode driver */
  243. }
  244. rt_uint8_t * rt_hw_lcd_get_framebuffer(void)
  245. {
  246. return RT_NULL; /* no framebuffer driver */
  247. }
  248. /* 设置像素点 颜色,X,Y */
  249. void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  250. {
  251. unsigned short p;
  252. /* get color pixel */
  253. p = rtgui_color_to_565p(*c);
  254. ili9325_SetCursor(x,y);
  255. ili9325_WriteRAM_Prepare();
  256. ili9325_RAM = p ;
  257. }
  258. /* 获取像素点颜色 */
  259. void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  260. {
  261. unsigned short p;
  262. p = ili9325_BGR2RGB( ili9325_ReadGRAM(x,y) );
  263. *c = rtgui_color_from_565p(p);
  264. }
  265. /* 画水平线 */
  266. void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  267. {
  268. unsigned short p;
  269. /* get color pixel */
  270. p = rtgui_color_to_565p(*c);
  271. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  272. ili9325_WriteReg(0x0003,(1<<12)|(1<<5)|(1<<4) | (0<<3) );
  273. ili9325_SetCursor(x1, y);
  274. ili9325_WriteRAM_Prepare(); /* Prepare to write GRAM */
  275. while (x1 < x2)
  276. {
  277. ili9325_RAM = p ;
  278. x1++;
  279. }
  280. }
  281. /* 垂直线 */
  282. void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t y2)
  283. {
  284. unsigned short p;
  285. /* get color pixel */
  286. p = rtgui_color_to_565p(*c);
  287. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  288. ili9325_WriteReg(0x0003,(1<<12)|(1<<5)|(0<<4) | (1<<3) );
  289. ili9325_SetCursor(x, y1);
  290. ili9325_WriteRAM_Prepare(); /* Prepare to write GRAM */
  291. while (y1 < y2)
  292. {
  293. ili9325_RAM = p ;
  294. y1++;
  295. }
  296. }
  297. /* ?? */
  298. void rt_hw_lcd_draw_raw_hline(rt_uint8_t *pixels, rt_base_t x1, rt_base_t x2, rt_base_t y)
  299. {
  300. rt_uint16_t *ptr;
  301. /* get pixel */
  302. ptr = (rt_uint16_t*) pixels;
  303. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  304. ili9325_WriteReg(0x0003,(1<<12)|(1<<5)|(1<<4) | (0<<3) );
  305. ili9325_SetCursor(x1, y);
  306. ili9325_WriteRAM_Prepare(); /* Prepare to write GRAM */
  307. while (x1 < x2)
  308. {
  309. ili9325_RAM = *ptr ;
  310. x1 ++;
  311. ptr ++;
  312. }
  313. }
  314. rt_err_t rt_hw_lcd_init(void)
  315. {
  316. GPIO_InitTypeDef GPIO_InitStructure;
  317. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
  318. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  319. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  320. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  321. GPIO_Init(GPIOF,&GPIO_InitStructure);
  322. GPIO_SetBits(GPIOF,GPIO_Pin_9);
  323. ili9325_Initializtion();
  324. /* LCD GRAM TEST */
  325. {
  326. unsigned short temp;
  327. unsigned int test_x;
  328. unsigned int test_y;
  329. rt_kprintf("\r\nLCD GRAM test....");
  330. /* write */
  331. temp=0;
  332. /* [5:4]-ID~ID0 [3]-AM-1垂直-0水平 */
  333. ili9325_WriteReg(0x0003,(1<<12)|(1<<5)|(1<<4) | (0<<3) );
  334. ili9325_SetCursor(0,0);
  335. ili9325_WriteRAM_Prepare();
  336. for(test_y=0; test_y<76800; test_y++)
  337. {
  338. ili9325_RAM = temp++ ;
  339. }
  340. /* read */
  341. temp=0;
  342. for(test_y=0; test_y<320; test_y++)
  343. {
  344. for(test_x=0; test_x<240; test_x++)
  345. {
  346. if( ili9325_BGR2RGB( ili9325_ReadGRAM(test_x,test_y) ) != temp++)
  347. {
  348. rt_kprintf(" LCD GRAM ERR!!");
  349. while(1);
  350. }
  351. }
  352. }
  353. rt_kprintf(" TEST PASS!\r\n");
  354. }/* LCD GRAM TEST */
  355. #ifndef DRIVER_TEST
  356. /* add lcd driver into graphic driver */
  357. rtgui_graphic_driver_add(&_rtgui_lcd_driver);
  358. #endif
  359. return RT_EOK;
  360. }
  361. #include <finsh.h>
  362. void hline(rt_base_t x1, rt_base_t x2, rt_base_t y, rt_uint32_t pixel)
  363. {
  364. rt_hw_lcd_draw_hline(&pixel, x1, x2, y);
  365. }
  366. FINSH_FUNCTION_EXPORT(hline, draw a hline);
  367. void vline(int x, int y1, int y2, rt_uint32_t pixel)
  368. {
  369. rt_hw_lcd_draw_vline(&pixel, x, y1, y2);
  370. }
  371. FINSH_FUNCTION_EXPORT(vline, draw a vline);
  372. void cls()
  373. {
  374. ili9325_Clear(0x051F);
  375. }
  376. FINSH_FUNCTION_EXPORT(cls, clear screen);
  377. #endif