lcd.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "lcd.h"
  2. #include "font.h"
  3. #define DRV_DEBUG
  4. #define LOG_TAG "spi.lcd"
  5. #include <drv_log.h>
  6. static int32_t lcd_init(void);
  7. static int32_t lcd_writereg(uint8_t reg,uint8_t* pdata,uint32_t length);
  8. static int32_t lcd_readreg(uint8_t reg,uint8_t* pdata);
  9. static int32_t lcd_senddata(uint8_t* pdata,uint32_t length);
  10. static int32_t lcd_recvdata(uint8_t* pdata,uint32_t length);
  11. ST7735_IO_t st7735_pIO = {
  12. lcd_init,
  13. RT_NULL,
  14. RT_NULL,
  15. lcd_writereg,
  16. lcd_readreg,
  17. lcd_senddata,
  18. lcd_recvdata,
  19. RT_NULL
  20. };
  21. ST7735_Object_t st7735_pObj;
  22. uint32_t st7735_id;
  23. static struct rt_spi_device *spi_dev_lcd;
  24. #ifdef LCD_BACKLIGHT_USING_PWM
  25. static struct rt_device_pwm *lcd_pwm_dev;
  26. static uint32_t NowBrightness;
  27. #endif
  28. extern unsigned char WeActStudiologo[];
  29. //LCD_RS
  30. #define LCD_RS_SET rt_pin_write(WR_RS_PIN, PIN_HIGH)
  31. #define LCD_RS_RESET rt_pin_write(WR_RS_PIN, PIN_LOW)
  32. //LCD_CS
  33. #define LCD_CS_SET rt_pin_write(CS_PIN, PIN_HIGH)
  34. #define LCD_CS_RESET rt_pin_write(CS_PIN, PIN_LOW)
  35. void LCD_SetBrightness(uint32_t Brightness)
  36. {
  37. #ifdef LCD_BACKLIGHT_USING_PWM
  38. Brightness = ((Brightness >= MAX_BRIGHTNESS)?(MAX_BRIGHTNESS-1):Brightness);
  39. rt_pwm_set(lcd_pwm_dev, LCD_PWM_DEV_CHANNEL, MAX_BRIGHTNESS, Brightness);
  40. NowBrightness = Brightness;
  41. #else
  42. rt_pin_mode(LCD_DISP_GPIO_NUM, PIN_MODE_OUTPUT);
  43. rt_pin_write(LCD_DISP_GPIO_NUM, PIN_LOW);
  44. #endif
  45. }
  46. uint32_t LCD_GetBrightness(void)
  47. {
  48. #ifdef LCD_BACKLIGHT_USING_PWM
  49. return NowBrightness;
  50. #else
  51. return 0;
  52. #endif
  53. }
  54. //在指定位置显示一个字符
  55. //x,y:起始坐标
  56. //num:要显示的字符:" "--->"~"
  57. //size:字体大小 12/16
  58. //mode:叠加方式(1)还是非叠加方式(0)
  59. void LCD_ShowChar(uint16_t x,uint16_t y,uint8_t num,uint8_t size,uint8_t mode)
  60. {
  61. uint8_t temp,t1,t;
  62. uint16_t y0=y;
  63. uint16_t x0=x;
  64. uint32_t h,w;
  65. uint16_t write[size][size==12?6:8];
  66. uint16_t count;
  67. ST7735_GetXSize(&st7735_pObj,&w);
  68. ST7735_GetYSize(&st7735_pObj,&h);
  69. //设置窗口
  70. num=num-' ';//得到偏移后的值
  71. count = 0;
  72. if(!mode) //非叠加方式
  73. {
  74. for(t=0;t<size;t++)
  75. {
  76. if(size==12)temp=asc2_1206[num][t]; //调用1206字体
  77. else temp=asc2_1608[num][t]; //调用1608字体
  78. for(t1=0;t1<8;t1++)
  79. {
  80. if(temp&0x80)
  81. write[count][t/2]=(BRUSH_POINT_COLOR&0xFF)<<8|BRUSH_POINT_COLOR>>8;
  82. else
  83. write[count][t/2]=(BRUSH_BACK_COLOR&0xFF)<<8|BRUSH_BACK_COLOR>>8;
  84. count ++;
  85. if(count >= size) count =0;
  86. temp<<=1;
  87. y++;
  88. if(y>=h){return;}//超区域了
  89. if((y-y0)==size)
  90. {
  91. y=y0;
  92. x++;
  93. if(x>=w){return;}//超区域了
  94. break;
  95. }
  96. }
  97. }
  98. }
  99. else//叠加方式
  100. {
  101. for(t=0;t<size;t++)
  102. {
  103. if(size==12)temp=asc2_1206[num][t]; //调用1206字体
  104. else temp=asc2_1608[num][t]; //调用1608字体
  105. for(t1=0;t1<8;t1++)
  106. {
  107. if(temp&0x80)
  108. write[count][t/2]=(BRUSH_POINT_COLOR&0xFF)<<8|BRUSH_POINT_COLOR>>8;
  109. count ++;
  110. if(count >= size) count =0;
  111. temp<<=1;
  112. y++;
  113. if(y>=h){return;}//超区域了
  114. if((y-y0)==size)
  115. {
  116. y=y0;
  117. x++;
  118. if(x>=w){return;}//超区域了
  119. break;
  120. }
  121. }
  122. }
  123. }
  124. ST7735_FillRGBRect(&st7735_pObj,x0,y0,(uint8_t *)&write,size==12?6:8,size);
  125. }
  126. //显示字符串
  127. //x,y:起点坐标
  128. //width,height:区域大小
  129. //size:字体大小
  130. //*p:字符串起始地址
  131. void LCD_ShowString(uint16_t x,uint16_t y,uint16_t width,uint16_t height,uint8_t size,uint8_t *p)
  132. {
  133. uint8_t x0=x;
  134. width+=x;
  135. height+=y;
  136. while((*p<='~')&&(*p>=' '))//判断是不是非法字符!
  137. {
  138. if(x>=width){x=x0;y+=size;}
  139. if(y>=height)break;//退出
  140. LCD_ShowChar(x,y,*p,size,0);
  141. x+=size/2;
  142. p++;
  143. }
  144. }
  145. void LCD_FillRGBRect(uint32_t Xpos, uint32_t Ypos, uint8_t *pData, uint32_t Width, uint32_t Height)
  146. {
  147. ST7735_LCD_Driver.FillRGBRect(&st7735_pObj, Xpos, Ypos, pData, Width, Height);
  148. }
  149. static int32_t lcd_init(void)
  150. {
  151. return ST7735_OK;
  152. }
  153. static int32_t lcd_writereg(uint8_t reg,uint8_t* pdata,uint32_t length)
  154. {
  155. int32_t result;
  156. LCD_CS_RESET;
  157. LCD_RS_RESET;
  158. result = rt_spi_send(spi_dev_lcd, &reg, 1);
  159. LCD_RS_SET;
  160. if(length > 0)
  161. result += rt_spi_send(spi_dev_lcd, pdata, length);
  162. LCD_CS_SET;
  163. return ((result == length+1)?0:-1);
  164. }
  165. static int32_t lcd_readreg(uint8_t reg,uint8_t* pdata)
  166. {
  167. int32_t result;
  168. LCD_CS_RESET;
  169. LCD_RS_RESET;
  170. result = rt_spi_send(spi_dev_lcd, &reg, 1);
  171. LCD_RS_SET;
  172. result += rt_spi_recv(spi_dev_lcd, pdata, 1);
  173. LCD_CS_SET;
  174. return ((result == 2)?0:-1);
  175. }
  176. static int32_t lcd_senddata(uint8_t* pdata,uint32_t length)
  177. {
  178. int32_t result;
  179. LCD_CS_RESET;
  180. //LCD_RS_SET;
  181. result =rt_spi_send(spi_dev_lcd, pdata, length);
  182. LCD_CS_SET;
  183. return ((result == length)?0:-1);
  184. }
  185. static int32_t lcd_recvdata(uint8_t* pdata,uint32_t length)
  186. {
  187. int32_t result;
  188. LCD_CS_RESET;
  189. //LCD_RS_SET;
  190. result = rt_spi_recv(spi_dev_lcd, pdata, length);
  191. LCD_CS_SET;
  192. return ((result == length)?0:-1);
  193. }
  194. static int LCD_Init(void)
  195. {
  196. rt_pin_mode(WR_RS_PIN, PIN_MODE_OUTPUT);
  197. rt_pin_mode(CS_PIN, PIN_MODE_OUTPUT);
  198. spi_dev_lcd = (struct rt_spi_device *)rt_device_find(LCD_SPI_DEVICE_NAME);
  199. if (!spi_dev_lcd)
  200. {
  201. LOG_E("tft-lcd init failed! can't find %s device!\n", LCD_SPI_DEVICE_NAME);
  202. return -RT_ERROR;
  203. }
  204. ST7735_RegisterBusIO(&st7735_pObj,&st7735_pIO);
  205. if(ST7735_ERROR == ST7735_LCD_Driver.Init(&st7735_pObj,ST7735_FORMAT_RBG565,ST7735_ORIENTATION_LANDSCAPE_ROT180))
  206. {
  207. LOG_E("st7735 init failed!");
  208. // return ;
  209. }
  210. ST7735_LCD_Driver.FillRect(&st7735_pObj,0,0,160,80,BLACK);
  211. ST7735_LCD_Driver.ReadID(&st7735_pObj,&st7735_id);
  212. ST7735_LCD_Driver.DisplayOn(&st7735_pObj);
  213. LOG_D("lcd id:0X%08X", st7735_id);
  214. LOG_D("chip id:0X%08X", HAL_GetDEVID());
  215. #ifdef LCD_BACKLIGHT_USING_PWM
  216. /* turn on the LCD backlight */
  217. lcd_pwm_dev = (struct rt_device_pwm *)rt_device_find(LCD_PWM_DEV_NAME);
  218. if (!lcd_pwm_dev)
  219. {
  220. LOG_E("lcd pwm pin init failed! can't find %s device!\n", LCD_SPI_DEVICE_NAME);
  221. return -RT_ERROR;
  222. }
  223. /* pwm frequency:100K = 10000ns */
  224. rt_pwm_set(lcd_pwm_dev, LCD_PWM_DEV_CHANNEL, LCD_PWM_DEV_TIME, 5000);
  225. rt_pwm_enable(lcd_pwm_dev, LCD_PWM_DEV_CHANNEL);
  226. #endif
  227. LCD_SetBrightness(MAX_BRIGHTNESS);
  228. ST7735_LCD_Driver.DrawBitmap(&st7735_pObj,0,0,WeActStudiologo);
  229. return RT_EOK;
  230. }
  231. INIT_COMPONENT_EXPORT(LCD_Init);
  232. #ifdef DRV_DEBUG
  233. #ifdef FINSH_USING_MSH
  234. static int show_logo(int argc, char **argv)
  235. {
  236. uint8_t text[20];
  237. LCD_SetBrightness(MAX_BRIGHTNESS);
  238. ST7735_LCD_Driver.DrawBitmap(&st7735_pObj,0,0,WeActStudiologo);
  239. rt_thread_mdelay(1000);
  240. ST7735_LCD_Driver.FillRect(&st7735_pObj,0,0,160,80,BLACK);
  241. sprintf((char *)&text,"WeAct Studio");
  242. LCD_ShowString(4,4,160,16,16,text);
  243. sprintf((char *)&text,"STM32H7xx 0x%X",HAL_GetDEVID());
  244. LCD_ShowString(4,22,160,16,16,text);
  245. sprintf((char *)&text,"LCD ID: 0x%X",st7735_id);
  246. LCD_ShowString(4,40,160,16,16,text);
  247. return 0;
  248. }
  249. MSH_CMD_EXPORT(show_logo, show logo);
  250. #endif /* FINSH_USING_MSH */
  251. #endif /* DRV_DEBUG */