lcd.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_565p(*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. unsigned short p;
  51. /* set X point */
  52. LCD_ADDR = 0x02;
  53. LCD_DATA = x;
  54. /* set Y point */
  55. LCD_ADDR = 0x03;
  56. LCD_DATA16( y );
  57. /* read pixel */
  58. LCD_ADDR = 0x0F;
  59. LCD_DATA16_READ(p);
  60. *c = rtgui_color_from_565p(p);
  61. }
  62. void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  63. {
  64. unsigned short p;
  65. /* get color pixel */
  66. p = rtgui_color_to_565p(*c);
  67. /* set X point */
  68. LCD_ADDR = 0x02;
  69. LCD_DATA = x1;
  70. /* set Y point */
  71. LCD_ADDR = 0x03;
  72. LCD_DATA16( y );
  73. /* write pixel */
  74. LCD_ADDR = 0x0E;
  75. while (x1 < x2)
  76. {
  77. LCD_DATA16(p);
  78. x1 ++;
  79. }
  80. }
  81. void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t y2)
  82. {
  83. unsigned short p;
  84. /* get color pixel */
  85. p = rtgui_color_to_565p(*c);
  86. /* set X point */
  87. LCD_ADDR = 0x02;
  88. LCD_DATA = x;
  89. while(y1 < y2)
  90. {
  91. /* set Y point */
  92. LCD_ADDR = 0x03;
  93. LCD_DATA16( y1 );
  94. /* write pixel */
  95. LCD_ADDR = 0x0E;
  96. LCD_DATA16(p);
  97. y1 ++;
  98. }
  99. }
  100. rt_err_t rt_hw_lcd_init(void)
  101. {
  102. GPIO_InitTypeDef GPIO_InitStructure;
  103. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
  104. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  105. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  106. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  107. GPIO_Init(GPIOF,&GPIO_InitStructure);
  108. GPIO_SetBits(GPIOF,GPIO_Pin_9);
  109. ftm0371_port_init();
  110. ftm0371_init();
  111. #ifndef DRIVER_TEST
  112. /* add lcd driver into graphic driver */
  113. rtgui_graphic_driver_add(&_rtgui_lcd_driver);
  114. #endif
  115. return RT_EOK;
  116. }
  117. #include <finsh.h>
  118. void hline(rt_base_t x1, rt_base_t x2, rt_base_t y, rt_uint32_t pixel)
  119. {
  120. rt_hw_lcd_draw_hline(&pixel, x1, x2, y);
  121. }
  122. FINSH_FUNCTION_EXPORT(hline, draw a hline);
  123. void vline(int x, int y1, int y2, rt_uint32_t pixel)
  124. {
  125. rt_hw_lcd_draw_vline(&pixel, x, y1, y2);
  126. }
  127. FINSH_FUNCTION_EXPORT(vline, draw a vline);
  128. void cls()
  129. {
  130. rt_size_t index;
  131. rtgui_color_t white = RTGUI_RGB(0xff, 0xff, 0xff);
  132. for(index = 0; index < 320; index ++)
  133. rt_hw_lcd_draw_hline(&white, 0, 240, index);
  134. }
  135. FINSH_FUNCTION_EXPORT(cls, clear screen);