lcd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "stm32f10x.h"
  2. #include "rtthread.h"
  3. #include "fmt0371/FMT0371.h"
  4. #include <rtgui/rtgui.h>
  5. #include <rtgui/driver.h>
  6. void rt_hw_lcd_update(rtgui_rect_t *rect);
  7. rt_uint8_t * rt_hw_lcd_get_framebuffer(void);
  8. void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y);
  9. void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y);
  10. void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y);
  11. void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t y2);
  12. struct rtgui_graphic_driver _rtgui_lcd_driver =
  13. {
  14. "lcd",
  15. 2,
  16. 240,
  17. 320,
  18. rt_hw_lcd_update,
  19. rt_hw_lcd_get_framebuffer,
  20. rt_hw_lcd_set_pixel,
  21. rt_hw_lcd_get_pixel,
  22. rt_hw_lcd_draw_hline,
  23. rt_hw_lcd_draw_vline
  24. };
  25. void rt_hw_lcd_update(rtgui_rect_t *rect)
  26. {
  27. /* nothing for none-DMA mode driver */
  28. }
  29. rt_uint8_t * rt_hw_lcd_get_framebuffer(void)
  30. {
  31. return RT_NULL; /* no framebuffer driver */
  32. }
  33. void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  34. {
  35. unsigned short p;
  36. /* get color pixel */
  37. p = rtgui_color_to_565(*c);
  38. /* set X point */
  39. LCD_ADDR = 0x02;
  40. LCD_DATA = x;
  41. /* set Y point */
  42. LCD_ADDR = 0x03;
  43. LCD_DATA16(y);
  44. /* write pixel */
  45. LCD_ADDR = 0x0E;
  46. LCD_DATA16(p);
  47. }
  48. void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  49. {
  50. /* set X point */
  51. LCD_ADDR = 0x02;
  52. LCD_DATA = x;
  53. /* set Y point */
  54. LCD_ADDR = 0x03;
  55. LCD_DATA16( y );
  56. /* read pixel */
  57. LCD_ADDR = 0x0F;
  58. LCD_DATA16_READ(*c);
  59. }
  60. void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  61. {
  62. unsigned short p;
  63. /* get color pixel */
  64. p = rtgui_color_to_565p(*c);
  65. /* set X point */
  66. LCD_ADDR = 0x02;
  67. LCD_DATA = x1;
  68. /* set Y point */
  69. LCD_ADDR = 0x03;
  70. LCD_DATA16( y );
  71. /* write pixel */
  72. LCD_ADDR = 0x0E;
  73. while (x1 < x2)
  74. {
  75. LCD_DATA16(p);
  76. x1 ++;
  77. }
  78. }
  79. void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t y2)
  80. {
  81. unsigned short p;
  82. /* get color pixel */
  83. p = rtgui_color_to_565p(*c);
  84. /* set X point */
  85. LCD_ADDR = 0x02;
  86. LCD_DATA = x;
  87. while(y1 < y2)
  88. {
  89. /* set Y point */
  90. LCD_ADDR = 0x03;
  91. LCD_DATA16( y1 );
  92. /* write pixel */
  93. LCD_ADDR = 0x0E;
  94. LCD_DATA16(p);
  95. y1 ++;
  96. }
  97. }
  98. rt_err_t rt_hw_lcd_init(void)
  99. {
  100. GPIO_InitTypeDef GPIO_InitStructure;
  101. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
  102. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  103. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  104. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  105. GPIO_Init(GPIOF,&GPIO_InitStructure);
  106. GPIO_SetBits(GPIOF,GPIO_Pin_9);
  107. ftm0371_port_init();
  108. ftm0371_init();
  109. #ifndef DRIVER_TEST
  110. /* add lcd driver into graphic driver */
  111. rtgui_graphic_driver_add(&_rtgui_lcd_driver);
  112. #endif
  113. return RT_EOK;
  114. }
  115. #include <finsh.h>
  116. void hline(rt_base_t x1, rt_base_t x2, rt_base_t y, rt_uint32_t pixel)
  117. {
  118. rt_hw_lcd_draw_hline(&pixel, x1, x2, y);
  119. }
  120. FINSH_FUNCTION_EXPORT(hline, draw a hline);
  121. void vline(int x, int y1, int y2, rt_uint32_t pixel)
  122. {
  123. rt_hw_lcd_draw_vline(&pixel, x, y1, y2);
  124. }
  125. FINSH_FUNCTION_EXPORT(vline, draw a vline);
  126. void cls()
  127. {
  128. rt_size_t index;
  129. rtgui_color_t white = RTGUI_RGB(0xff, 0xff, 0xff);
  130. for(index = 0; index < 320; index ++)
  131. rt_hw_lcd_draw_hline(&white, 0, 240, index);
  132. }
  133. FINSH_FUNCTION_EXPORT(cls, clear screen);