st7789h2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /**
  2. ******************************************************************************
  3. * @file st7789h2.c
  4. * @author MCD Application Team
  5. * @brief This file includes the LCD driver for st7789h2 LCD.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "st7789h2.h"
  21. /** @addtogroup BSP
  22. * @{
  23. */
  24. /** @addtogroup Components
  25. * @{
  26. */
  27. /** @defgroup ST7789H2
  28. * @brief This file provides a set of functions needed to drive the
  29. * FRIDA FRD154BP2901 LCD.
  30. * @{
  31. */
  32. /** @defgroup ST7789H2_Private_TypesDefinitions ST7789H2 Private TypesDefinitions
  33. * @{
  34. */
  35. typedef struct {
  36. uint8_t red;
  37. uint8_t green;
  38. uint8_t blue;
  39. } ST7789H2_Rgb888;
  40. /**
  41. * @}
  42. */
  43. /** @defgroup ST7789H2_Private_Defines ST7789H2 Private Defines
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @defgroup ST7789H2_Private_Macros ST7789H2 Private Macros
  50. * @{
  51. */
  52. /**
  53. * @}
  54. */
  55. /** @defgroup ST7789H2_Private_Variables ST7789H2 Private Variables
  56. * @{
  57. */
  58. LCD_DrvTypeDef ST7789H2_drv =
  59. {
  60. ST7789H2_Init,
  61. ST7789H2_ReadID,
  62. ST7789H2_DisplayOn,
  63. ST7789H2_DisplayOff,
  64. ST7789H2_SetCursor,
  65. ST7789H2_WritePixel,
  66. ST7789H2_ReadPixel,
  67. ST7789H2_SetDisplayWindow,
  68. ST7789H2_DrawHLine,
  69. ST7789H2_DrawVLine,
  70. ST7789H2_GetLcdPixelWidth,
  71. ST7789H2_GetLcdPixelHeight,
  72. ST7789H2_DrawBitmap,
  73. ST7789H2_DrawRGBImage,
  74. };
  75. static uint16_t WindowsXstart = 0;
  76. static uint16_t WindowsYstart = 0;
  77. static uint16_t WindowsXend = ST7789H2_LCD_PIXEL_WIDTH-1;
  78. static uint16_t WindowsYend = ST7789H2_LCD_PIXEL_HEIGHT-1;
  79. /**
  80. * @}
  81. */
  82. /** @defgroup ST7789H2_Private_FunctionPrototypes ST7789H2 Private FunctionPrototypes
  83. * @{
  84. */
  85. static ST7789H2_Rgb888 ST7789H2_ReadPixel_rgb888(uint16_t Xpos, uint16_t Ypos);
  86. static void ST7789H2_DrawRGBHLine(uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint8_t *pdata);
  87. /**
  88. * @}
  89. */
  90. /** @defgroup ST7789H2_Private_Functions ST7789H2 Private Functions
  91. * @{
  92. */
  93. /**
  94. * @brief Initialize the st7789h2 LCD Component.
  95. * @param None
  96. * @retval None
  97. */
  98. void ST7789H2_Init(void)
  99. {
  100. uint8_t parameter[14];
  101. /* Initialize st7789h2 low level bus layer ----------------------------------*/
  102. LCD_IO_Init();
  103. /* Sleep In Command */
  104. ST7789H2_WriteReg(ST7789H2_SLEEP_IN, (uint8_t*)NULL, 0);
  105. /* Wait for 10ms */
  106. LCD_IO_Delay(10);
  107. /* SW Reset Command */
  108. ST7789H2_WriteReg(0x01, (uint8_t*)NULL, 0);
  109. /* Wait for 200ms */
  110. LCD_IO_Delay(200);
  111. /* Sleep Out Command */
  112. ST7789H2_WriteReg(ST7789H2_SLEEP_OUT, (uint8_t*)NULL, 0);
  113. /* Wait for 120ms */
  114. LCD_IO_Delay(120);
  115. /* Normal display for Driver Down side */
  116. parameter[0] = 0x00;
  117. ST7789H2_WriteReg(ST7789H2_NORMAL_DISPLAY, parameter, 1);
  118. /* Color mode 16bits/pixel */
  119. parameter[0] = 0x05;
  120. ST7789H2_WriteReg(ST7789H2_COLOR_MODE, parameter, 1);
  121. /* Display inversion On */
  122. ST7789H2_WriteReg(ST7789H2_DISPLAY_INVERSION, (uint8_t*)NULL, 0);
  123. /* Set Column address CASET */
  124. parameter[0] = 0x00;
  125. parameter[1] = 0x00;
  126. parameter[2] = 0x00;
  127. parameter[3] = 0xEF;
  128. ST7789H2_WriteReg(ST7789H2_CASET, parameter, 4);
  129. /* Set Row address RASET */
  130. parameter[0] = 0x00;
  131. parameter[1] = 0x00;
  132. parameter[2] = 0x00;
  133. parameter[3] = 0xEF;
  134. ST7789H2_WriteReg(ST7789H2_RASET, parameter, 4);
  135. /*--------------- ST7789H2 Frame rate setting -------------------------------*/
  136. /* PORCH control setting */
  137. parameter[0] = 0x0C;
  138. parameter[1] = 0x0C;
  139. parameter[2] = 0x00;
  140. parameter[3] = 0x33;
  141. parameter[4] = 0x33;
  142. ST7789H2_WriteReg(ST7789H2_PORCH_CTRL, parameter, 5);
  143. /* GATE control setting */
  144. parameter[0] = 0x35;
  145. ST7789H2_WriteReg(ST7789H2_GATE_CTRL, parameter, 1);
  146. /*--------------- ST7789H2 Power setting ------------------------------------*/
  147. /* VCOM setting */
  148. parameter[0] = 0x1F;
  149. ST7789H2_WriteReg(ST7789H2_VCOM_SET, parameter, 1);
  150. /* LCM Control setting */
  151. parameter[0] = 0x2C;
  152. ST7789H2_WriteReg(ST7789H2_LCM_CTRL, parameter, 1);
  153. /* VDV and VRH Command Enable */
  154. parameter[0] = 0x01;
  155. parameter[1] = 0xC3;
  156. ST7789H2_WriteReg(ST7789H2_VDV_VRH_EN, parameter, 2);
  157. /* VDV Set */
  158. parameter[0] = 0x20;
  159. ST7789H2_WriteReg(ST7789H2_VDV_SET, parameter, 1);
  160. /* Frame Rate Control in normal mode */
  161. parameter[0] = 0x0F;
  162. ST7789H2_WriteReg(ST7789H2_FR_CTRL, parameter, 1);
  163. /* Power Control */
  164. parameter[0] = 0xA4;
  165. parameter[1] = 0xA1;
  166. ST7789H2_WriteReg(ST7789H2_POWER_CTRL, parameter, 2);
  167. /*--------------- ST7789H2 Gamma setting ------------------------------------*/
  168. /* Positive Voltage Gamma Control */
  169. parameter[0] = 0xD0;
  170. parameter[1] = 0x08;
  171. parameter[2] = 0x11;
  172. parameter[3] = 0x08;
  173. parameter[4] = 0x0C;
  174. parameter[5] = 0x15;
  175. parameter[6] = 0x39;
  176. parameter[7] = 0x33;
  177. parameter[8] = 0x50;
  178. parameter[9] = 0x36;
  179. parameter[10] = 0x13;
  180. parameter[11] = 0x14;
  181. parameter[12] = 0x29;
  182. parameter[13] = 0x2D;
  183. ST7789H2_WriteReg(ST7789H2_PV_GAMMA_CTRL, parameter, 14);
  184. /* Negative Voltage Gamma Control */
  185. parameter[0] = 0xD0;
  186. parameter[1] = 0x08;
  187. parameter[2] = 0x10;
  188. parameter[3] = 0x08;
  189. parameter[4] = 0x06;
  190. parameter[5] = 0x06;
  191. parameter[6] = 0x39;
  192. parameter[7] = 0x44;
  193. parameter[8] = 0x51;
  194. parameter[9] = 0x0B;
  195. parameter[10] = 0x16;
  196. parameter[11] = 0x14;
  197. parameter[12] = 0x2F;
  198. parameter[13] = 0x31;
  199. ST7789H2_WriteReg(ST7789H2_NV_GAMMA_CTRL, parameter, 14);
  200. /* Display ON command */
  201. ST7789H2_DisplayOn();
  202. /* Tearing Effect Line On: Option (00h:VSYNC Interface OFF, 01h:VSYNC Interface ON) */
  203. parameter[0] = 0x00;
  204. ST7789H2_WriteReg(ST7789H2_TEARING_EFFECT, parameter, 1);
  205. }
  206. /**
  207. * @brief Set the Display Orientation.
  208. * @param orientation: ST7789H2_ORIENTATION_PORTRAIT, ST7789H2_ORIENTATION_LANDSCAPE
  209. * or ST7789H2_ORIENTATION_LANDSCAPE_ROT180
  210. * @retval None
  211. */
  212. void ST7789H2_SetOrientation(uint32_t orientation)
  213. {
  214. uint8_t parameter[6];
  215. if(orientation == ST7789H2_ORIENTATION_LANDSCAPE)
  216. {
  217. parameter[0] = 0x00;
  218. }
  219. else if(orientation == ST7789H2_ORIENTATION_LANDSCAPE_ROT180)
  220. {
  221. /* Vertical Scrolling Definition */
  222. /* TFA describes the Top Fixed Area */
  223. parameter[0] = 0x00;
  224. parameter[1] = 0x00;
  225. /* VSA describes the height of the Vertical Scrolling Area */
  226. parameter[2] = 0x01;
  227. parameter[3] = 0xF0;
  228. /* BFA describes the Bottom Fixed Area */
  229. parameter[4] = 0x00;
  230. parameter[5] = 0x00;
  231. ST7789H2_WriteReg(ST7789H2_VSCRDEF, parameter, 6);
  232. /* Vertical Scroll Start Address of RAM */
  233. /* GRAM row nbr (320) - Display row nbr (240) = 80 = 0x50 */
  234. parameter[0] = 0x00;
  235. parameter[1] = 0x50;
  236. ST7789H2_WriteReg(ST7789H2_VSCSAD, parameter, 2);
  237. parameter[0] = 0xC0;
  238. }
  239. else
  240. {
  241. parameter[0] = 0x60;
  242. }
  243. ST7789H2_WriteReg(ST7789H2_NORMAL_DISPLAY, parameter, 1);
  244. }
  245. /**
  246. * @brief Enables the Display.
  247. * @param None
  248. * @retval None
  249. */
  250. void ST7789H2_DisplayOn(void)
  251. {
  252. /* Display ON command */
  253. ST7789H2_WriteReg(ST7789H2_DISPLAY_ON, (uint8_t*)NULL, 0);
  254. /* Sleep Out command */
  255. ST7789H2_WriteReg(ST7789H2_SLEEP_OUT, (uint8_t*)NULL, 0);
  256. }
  257. /**
  258. * @brief Disables the Display.
  259. * @param None
  260. * @retval None
  261. */
  262. void ST7789H2_DisplayOff(void)
  263. {
  264. uint8_t parameter[1];
  265. parameter[0] = 0xFE;
  266. /* Display OFF command */
  267. ST7789H2_WriteReg(ST7789H2_DISPLAY_OFF, parameter, 1);
  268. /* Sleep In Command */
  269. ST7789H2_WriteReg(ST7789H2_SLEEP_IN, (uint8_t*)NULL, 0);
  270. /* Wait for 10ms */
  271. LCD_IO_Delay(10);
  272. }
  273. /**
  274. * @brief Get the LCD pixel Width.
  275. * @param None
  276. * @retval The Lcd Pixel Width
  277. */
  278. uint16_t ST7789H2_GetLcdPixelWidth(void)
  279. {
  280. return (uint16_t)ST7789H2_LCD_PIXEL_WIDTH;
  281. }
  282. /**
  283. * @brief Get the LCD pixel Height.
  284. * @param None
  285. * @retval The Lcd Pixel Height
  286. */
  287. uint16_t ST7789H2_GetLcdPixelHeight(void)
  288. {
  289. return (uint16_t)ST7789H2_LCD_PIXEL_HEIGHT;
  290. }
  291. /**
  292. * @brief Get the st7789h2 ID.
  293. * @param None
  294. * @retval The st7789h2 ID
  295. */
  296. uint16_t ST7789H2_ReadID(void)
  297. {
  298. LCD_IO_Init();
  299. return ST7789H2_ReadReg(ST7789H2_LCD_ID);
  300. }
  301. /**
  302. * @brief Set Cursor position.
  303. * @param Xpos: specifies the X position.
  304. * @param Ypos: specifies the Y position.
  305. * @retval None
  306. */
  307. void ST7789H2_SetCursor(uint16_t Xpos, uint16_t Ypos)
  308. {
  309. uint8_t parameter[4];
  310. /* CASET: Comumn Addrses Set */
  311. parameter[0] = 0x00;
  312. parameter[1] = 0x00 + Xpos;
  313. parameter[2] = 0x00;
  314. parameter[3] = 0xEF + Xpos;
  315. ST7789H2_WriteReg(ST7789H2_CASET, parameter, 4);
  316. /* RASET: Row Addrses Set */
  317. parameter[0] = 0x00;
  318. parameter[1] = 0x00 + Ypos;
  319. parameter[2] = 0x00;
  320. parameter[3] = 0xEF + Ypos;
  321. ST7789H2_WriteReg(ST7789H2_RASET, parameter, 4);
  322. }
  323. /**
  324. * @brief Write pixel.
  325. * @param Xpos: specifies the X position.
  326. * @param Ypos: specifies the Y position.
  327. * @param RGBCode: the RGB pixel color in RGB565 format
  328. * @retval None
  329. */
  330. void ST7789H2_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode)
  331. {
  332. /* Set Cursor */
  333. ST7789H2_SetCursor(Xpos, Ypos);
  334. /* Prepare to write to LCD RAM */
  335. ST7789H2_WriteReg(ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0); /* RAM write data command */
  336. /* Write RAM data */
  337. LCD_IO_WriteData(RGBCode);
  338. }
  339. /**
  340. * @brief Read pixel.
  341. * @param Xpos: specifies the X position.
  342. * @param Ypos: specifies the Y position.
  343. * @retval The RGB pixel color in RGB565 format
  344. */
  345. uint16_t ST7789H2_ReadPixel(uint16_t Xpos, uint16_t Ypos)
  346. {
  347. ST7789H2_Rgb888 rgb888;
  348. uint8_t r, g, b;
  349. uint16_t rgb565;
  350. /* Set Cursor */
  351. ST7789H2_SetCursor(Xpos, Ypos);
  352. /* Read RGB888 data from LCD RAM */
  353. rgb888 = ST7789H2_ReadPixel_rgb888(Xpos, Ypos);
  354. /* Convert RGB888 to RGB565 */
  355. r = ((rgb888.red & 0xF8) >> 3); /* Extract the red component 5 most significant bits */
  356. g = ((rgb888.green & 0xFC) >> 2); /* Extract the green component 6 most significant bits */
  357. b = ((rgb888.blue & 0xF8) >> 3); /* Extract the blue component 5 most significant bits */
  358. rgb565 = ((uint16_t)(r) << 11) + ((uint16_t)(g) << 5) + ((uint16_t)(b) << 0);
  359. return (rgb565);
  360. }
  361. /**
  362. * @brief Writes to the selected LCD register.
  363. * @param Command: command value (or register address as named in st7789h2 doc).
  364. * @param Parameters: pointer on parameters value (if command uses one or several parameters).
  365. * @param NbParameters: number of command parameters (0 if no parameter)
  366. * @retval None
  367. */
  368. void ST7789H2_WriteReg(uint8_t Command, uint8_t *Parameters, uint8_t NbParameters)
  369. {
  370. uint8_t i;
  371. /* Send command */
  372. LCD_IO_WriteReg(Command);
  373. /* Send command's parameters if any */
  374. for (i=0; i<NbParameters; i++)
  375. {
  376. LCD_IO_WriteData(Parameters[i]);
  377. }
  378. }
  379. /**
  380. * @brief Reads the selected LCD Register.
  381. * @param Command: command value (or register address as named in st7789h2 doc).
  382. * @retval Register Value.
  383. */
  384. uint8_t ST7789H2_ReadReg(uint8_t Command)
  385. {
  386. /* Send command */
  387. LCD_IO_WriteReg(Command);
  388. /* Read dummy data */
  389. LCD_IO_ReadData();
  390. /* Read register value */
  391. return (LCD_IO_ReadData());
  392. }
  393. /**
  394. * @brief Sets a display window
  395. * @param Xpos: specifies the X bottom left position.
  396. * @param Ypos: specifies the Y bottom left position.
  397. * @param Height: display window height.
  398. * @param Width: display window width.
  399. * @retval None
  400. */
  401. void ST7789H2_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  402. {
  403. if (Xpos < ST7789H2_LCD_PIXEL_WIDTH)
  404. {
  405. WindowsXstart = Xpos;
  406. }
  407. else
  408. {
  409. WindowsXstart = 0;
  410. }
  411. if (Ypos < ST7789H2_LCD_PIXEL_HEIGHT)
  412. {
  413. WindowsYstart = Ypos;
  414. }
  415. else
  416. {
  417. WindowsYstart = 0;
  418. }
  419. if (Width + Xpos <= ST7789H2_LCD_PIXEL_WIDTH)
  420. {
  421. WindowsXend = Width + Xpos - 1;
  422. }
  423. else
  424. {
  425. WindowsXend = ST7789H2_LCD_PIXEL_WIDTH - 1;
  426. }
  427. if (Height + Ypos <= ST7789H2_LCD_PIXEL_HEIGHT)
  428. {
  429. WindowsYend = Height + Ypos - 1;
  430. }
  431. else
  432. {
  433. WindowsYend = ST7789H2_LCD_PIXEL_HEIGHT-1;
  434. }
  435. }
  436. /**
  437. * @brief Draw vertical line.
  438. * @param RGBCode: Specifies the RGB color in RGB565 format
  439. * @param Xpos: specifies the X position.
  440. * @param Ypos: specifies the Y position.
  441. * @param Length: specifies the Line length.
  442. * @retval None
  443. */
  444. void ST7789H2_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  445. {
  446. uint16_t counter;
  447. /* Set Cursor */
  448. ST7789H2_SetCursor(Xpos, Ypos);
  449. /* Prepare to write to LCD RAM */
  450. ST7789H2_WriteReg(ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0); /* RAM write data command */
  451. /* Sent a complete line */
  452. for(counter = 0; counter < Length; counter++)
  453. {
  454. LCD_IO_WriteData(RGBCode);
  455. }
  456. }
  457. /**
  458. * @brief Draw vertical line.
  459. * @param RGBCode: Specifies the RGB color
  460. * @param Xpos: specifies the X position.
  461. * @param Ypos: specifies the Y position.
  462. * @param Length: specifies the Line length.
  463. * @retval None
  464. */
  465. void ST7789H2_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  466. {
  467. uint16_t counter;
  468. /* Set Cursor */
  469. ST7789H2_SetCursor(Xpos, Ypos);
  470. /* Prepare to write to LCD RAM */
  471. ST7789H2_WriteReg(ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0); /* RAM write data command */
  472. /* Fill a complete vertical line */
  473. for(counter = 0; counter < Length; counter++)
  474. {
  475. ST7789H2_WritePixel(Xpos, Ypos + counter, RGBCode);
  476. }
  477. }
  478. /**
  479. * @brief Displays a bitmap picture.
  480. * @param BmpAddress: Bmp picture address.
  481. * @param Xpos: Bmp X position in the LCD
  482. * @param Ypos: Bmp Y position in the LCD
  483. * @retval None
  484. */
  485. void ST7789H2_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
  486. {
  487. uint32_t index = 0, size = 0;
  488. uint32_t posY;
  489. uint32_t nb_line = 0;
  490. uint16_t Xsize = WindowsXend - WindowsXstart + 1;
  491. uint16_t Ysize = WindowsYend - WindowsYstart + 1;
  492. /* Read bitmap size */
  493. size = *(volatile uint16_t *) (pbmp + 2);
  494. size |= (*(volatile uint16_t *) (pbmp + 4)) << 16;
  495. /* Get bitmap data address offset */
  496. index = *(volatile uint16_t *) (pbmp + 10);
  497. index |= (*(volatile uint16_t *) (pbmp + 12)) << 16;
  498. size = (size - index)/2;
  499. pbmp += index;
  500. for (posY = (Ypos + Ysize); posY > Ypos; posY--) /* In BMP files the line order is inverted */
  501. {
  502. /* Set Cursor */
  503. ST7789H2_SetCursor(Xpos, posY - 1);
  504. /* Draw one line of the picture */
  505. ST7789H2_DrawRGBHLine(Xpos, posY - 1, Xsize, (pbmp + (nb_line * Xsize * 2)));
  506. nb_line++;
  507. }
  508. }
  509. /**
  510. * @brief Displays picture.
  511. * @param pdata: picture address.
  512. * @param Xpos: Image X position in the LCD
  513. * @param Ypos: Image Y position in the LCD
  514. * @param Xsize: Image X size in the LCD
  515. * @param Ysize: Image Y size in the LCD
  516. * @retval None
  517. */
  518. void ST7789H2_DrawRGBImage(uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint16_t Ysize, uint8_t *pdata)
  519. {
  520. uint32_t posY;
  521. uint32_t nb_line = 0;
  522. for (posY = Ypos; posY < (Ypos + Ysize); posY ++)
  523. {
  524. /* Set Cursor */
  525. ST7789H2_SetCursor(Xpos, posY);
  526. /* Draw one line of the picture */
  527. ST7789H2_DrawRGBHLine(Xpos, posY, Xsize, (pdata + (nb_line * Xsize * 2)));
  528. nb_line++;
  529. }
  530. }
  531. /******************************************************************************
  532. Static Functions
  533. *******************************************************************************/
  534. /**
  535. * @brief Read pixel from LCD RAM in RGB888 format
  536. * @param Xpos: specifies the X position.
  537. * @param Ypos: specifies the Y position.
  538. * @retval Each RGB pixel color components in a structure
  539. */
  540. static ST7789H2_Rgb888 ST7789H2_ReadPixel_rgb888(uint16_t Xpos, uint16_t Ypos)
  541. {
  542. ST7789H2_Rgb888 rgb888;
  543. uint16_t rgb888_part1, rgb888_part2;
  544. /* In LCD RAM, pixels are 24 bits packed and read with 16 bits access
  545. * Here is the pixels components arrangement in memory :
  546. * bits: 15 14 13 12 11 10 09 08 | 07 06 05 04 03 02 01 00
  547. * address 0 : red pixel 0 X X | green pixel 0 X X
  548. * address 1 : blue pixel 0 X X | red pixel 1 X X
  549. * address 2 : green pixel 1 X X | blue pixel 1 X X
  550. */
  551. /* Set Cursor */
  552. ST7789H2_SetCursor(Xpos, Ypos);
  553. /* Prepare to read LCD RAM */
  554. ST7789H2_WriteReg(ST7789H2_READ_RAM, (uint8_t*)NULL, 0); /* RAM read data command */
  555. /* Dummy read */
  556. LCD_IO_ReadData();
  557. /* Read first part of the RGB888 data */
  558. rgb888_part1 = LCD_IO_ReadData();
  559. /* Read first part of the RGB888 data */
  560. rgb888_part2 = LCD_IO_ReadData();
  561. /* red component */
  562. rgb888.red = (rgb888_part1 & 0xFC00) >> 8;
  563. /* green component */
  564. rgb888.green = (rgb888_part1 & 0x00FC) >> 0;
  565. /* blue component */
  566. rgb888.blue = (rgb888_part2 & 0xFC00) >> 8;
  567. return rgb888;
  568. }
  569. /**
  570. * @brief Displays a single picture line.
  571. * @param pdata: picture address.
  572. * @param Xpos: Image X position in the LCD
  573. * @param Ypos: Image Y position in the LCD
  574. * @param Xsize: Image X size in the LCD
  575. * @retval None
  576. */
  577. static void ST7789H2_DrawRGBHLine(uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint8_t *pdata)
  578. {
  579. uint32_t i = 0;
  580. uint32_t posX;
  581. uint16_t *rgb565 = (uint16_t*)pdata;
  582. /* Prepare to write to LCD RAM */
  583. ST7789H2_WriteReg(ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0); /* RAM write data command */
  584. for (posX = Xpos; posX < (Xsize + Xpos); posX++)
  585. {
  586. if ((posX >= WindowsXstart) && (Ypos >= WindowsYstart) && /* Check we are in the defined window */
  587. (posX <= WindowsXend) && (Ypos <= WindowsYend))
  588. {
  589. if (posX != (Xsize + Xpos)) /* When writing last pixel when size is odd, the third part is not written */
  590. {
  591. LCD_IO_WriteData(rgb565[i]);
  592. }
  593. i++;
  594. }
  595. }
  596. }
  597. /**
  598. * @}
  599. */
  600. /**
  601. * @}
  602. */
  603. /**
  604. * @}
  605. */
  606. /**
  607. * @}
  608. */
  609. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/