st7789.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /****************************************************************************
  2. * drivers/lcd/st7789.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include <errno.h>
  30. #include <debug.h>
  31. #include <nuttx/arch.h>
  32. #include <nuttx/spi/spi.h>
  33. #include <nuttx/lcd/lcd.h>
  34. #include <nuttx/lcd/st7789.h>
  35. #include "st7789.h"
  36. #ifdef CONFIG_LCD_ST7789
  37. /****************************************************************************
  38. * Pre-processor Definitions
  39. ****************************************************************************/
  40. /* Verify that all configuration requirements have been met */
  41. #ifndef CONFIG_LCD_ST7789_SPIMODE
  42. # define CONFIG_LCD_ST7789_SPIMODE SPIDEV_MODE0
  43. #endif
  44. /* SPI frequency */
  45. #ifndef CONFIG_LCD_ST7789_FREQUENCY
  46. # define CONFIG_LCD_ST7789_FREQUENCY 1000000
  47. #endif
  48. /* Check contrast selection */
  49. #if !defined(CONFIG_LCD_MAXCONTRAST)
  50. # define CONFIG_LCD_MAXCONTRAST 1
  51. #endif
  52. /* Check power setting */
  53. #if !defined(CONFIG_LCD_MAXPOWER) || CONFIG_LCD_MAXPOWER < 1
  54. # define CONFIG_LCD_MAXPOWER 1
  55. #endif
  56. #if CONFIG_LCD_MAXPOWER > 255
  57. # error "CONFIG_LCD_MAXPOWER must be less than 256 to fit in uint8_t"
  58. #endif
  59. /* Check orientation */
  60. #if defined(CONFIG_LCD_PORTRAIT)
  61. # if defined(CONFIG_LCD_LANDSCAPE) || defined(CONFIG_LCD_RLANDSCAPE) ||\
  62. defined(CONFIG_LCD_RPORTRAIT)
  63. # error "Cannot define both portrait and any other orientations"
  64. # endif
  65. #elif defined(CONFIG_LCD_RPORTRAIT)
  66. # if defined(CONFIG_LCD_LANDSCAPE) || defined(CONFIG_LCD_RLANDSCAPE)
  67. # error "Cannot define both rportrait and any other orientations"
  68. # endif
  69. #elif defined(CONFIG_LCD_LANDSCAPE)
  70. # ifdef CONFIG_LCD_RLANDSCAPE
  71. # error "Cannot define both landscape and any other orientations"
  72. # endif
  73. #elif !defined(CONFIG_LCD_RLANDSCAPE)
  74. # define CONFIG_LCD_LANDSCAPE 1
  75. #endif
  76. /* Display Resolution */
  77. #if !defined(CONFIG_LCD_ST7789_XRES)
  78. # define CONFIG_LCD_ST7789_XRES 240
  79. #endif
  80. #if !defined(CONFIG_LCD_ST7789_YRES)
  81. # define CONFIG_LCD_ST7789_YRES 320
  82. #endif
  83. #define ST7789_LUT_SIZE CONFIG_LCD_ST7789_YRES
  84. #if defined(CONFIG_LCD_LANDSCAPE) || defined(CONFIG_LCD_RLANDSCAPE)
  85. # define ST7789_XRES CONFIG_LCD_ST7789_YRES
  86. # define ST7789_YRES CONFIG_LCD_ST7789_XRES
  87. # define ST7789_XOFFSET CONFIG_LCD_ST7789_YOFFSET
  88. # define ST7789_YOFFSET CONFIG_LCD_ST7789_XOFFSET
  89. #else
  90. # define ST7789_XRES CONFIG_LCD_ST7789_XRES
  91. # define ST7789_YRES CONFIG_LCD_ST7789_YRES
  92. # define ST7789_XOFFSET CONFIG_LCD_ST7789_XOFFSET
  93. # define ST7789_YOFFSET CONFIG_LCD_ST7789_YOFFSET
  94. #endif
  95. /* Color depth and format */
  96. #ifdef CONFIG_LCD_ST7789_BPP
  97. # if (CONFIG_LCD_ST7789_BPP == 12)
  98. # define ST7789_BPP 12
  99. # define ST7789_COLORFMT FB_FMT_RGB12_444
  100. # define ST7789_BYTESPP 2
  101. # elif (CONFIG_LCD_ST7789_BPP == 16)
  102. # define ST7789_BPP 16
  103. # define ST7789_COLORFMT FB_FMT_RGB16_565
  104. # define ST7789_BYTESPP 2
  105. # else
  106. # define ST7789_BPP 16
  107. # define ST7789_COLORFMT FB_FMT_RGB16_565
  108. # define ST7789_BYTESPP 2
  109. # warning "Invalid color depth. Falling back to 16bpp"
  110. # endif
  111. #endif
  112. /****************************************************************************
  113. * Private Types
  114. ****************************************************************************/
  115. /* This structure describes the state of this driver */
  116. struct st7789_dev_s
  117. {
  118. /* Publicly visible device structure */
  119. struct lcd_dev_s dev;
  120. /* Private LCD-specific information follows */
  121. FAR struct spi_dev_s *spi; /* SPI device */
  122. uint8_t bpp; /* Selected color depth */
  123. uint8_t power; /* Current power setting */
  124. /* This is working memory allocated by the LCD driver for each LCD device
  125. * and for each color plane. This memory will hold one raster line of data.
  126. * The size of the allocated run buffer must therefore be at least
  127. * (bpp * xres / 8). Actual alignment of the buffer must conform to the
  128. * bitwidth of the underlying pixel type.
  129. *
  130. * If there are multiple planes, they may share the same working buffer
  131. * because different planes will not be operate on concurrently. However,
  132. * if there are multiple LCD devices, they must each have unique run
  133. * buffers.
  134. */
  135. uint16_t runbuffer[ST7789_LUT_SIZE];
  136. };
  137. /****************************************************************************
  138. * Private Function Protototypes
  139. ****************************************************************************/
  140. /* Misc. Helpers */
  141. static void st7789_select(FAR struct spi_dev_s *spi, int bits);
  142. static void st7789_deselect(FAR struct spi_dev_s *spi);
  143. static inline void st7789_sendcmd(FAR struct st7789_dev_s *dev, uint8_t cmd);
  144. static void st7789_sleep(FAR struct st7789_dev_s *dev, bool sleep);
  145. static void st7789_setorientation(FAR struct st7789_dev_s *dev);
  146. static void st7789_display(FAR struct st7789_dev_s *dev, bool on);
  147. static void st7789_setarea(FAR struct st7789_dev_s *dev,
  148. uint16_t x0, uint16_t y0,
  149. uint16_t x1, uint16_t y1);
  150. static void st7789_bpp(FAR struct st7789_dev_s *dev, int bpp);
  151. static void st7789_wrram(FAR struct st7789_dev_s *dev,
  152. FAR const uint16_t *buff, size_t size);
  153. #ifndef CONFIG_LCD_NOGETRUN
  154. static void st7789_rdram(FAR struct st7789_dev_s *dev,
  155. FAR uint16_t *buff, size_t size);
  156. #endif
  157. static void st7789_fill(FAR struct st7789_dev_s *dev, uint16_t color);
  158. /* LCD Data Transfer Methods */
  159. static int st7789_putrun(fb_coord_t row, fb_coord_t col,
  160. FAR const uint8_t *buffer, size_t npixels);
  161. static int st7789_putarea(fb_coord_t row_start, fb_coord_t row_end,
  162. fb_coord_t col_start, fb_coord_t col_end,
  163. FAR const uint8_t *buffer);
  164. #ifndef CONFIG_LCD_NOGETRUN
  165. static int st7789_getrun(fb_coord_t row, fb_coord_t col,
  166. FAR uint8_t *buffer, size_t npixels);
  167. #endif
  168. /* LCD Configuration */
  169. static int st7789_getvideoinfo(FAR struct lcd_dev_s *dev,
  170. FAR struct fb_videoinfo_s *vinfo);
  171. static int st7789_getplaneinfo(FAR struct lcd_dev_s *dev,
  172. unsigned int planeno,
  173. FAR struct lcd_planeinfo_s *pinfo);
  174. /* LCD Specific Controls */
  175. static int st7789_getpower(FAR struct lcd_dev_s *dev);
  176. static int st7789_setpower(FAR struct lcd_dev_s *dev, int power);
  177. static int st7789_getcontrast(FAR struct lcd_dev_s *dev);
  178. static int st7789_setcontrast(FAR struct lcd_dev_s *dev,
  179. unsigned int contrast);
  180. /****************************************************************************
  181. * Private Data
  182. ****************************************************************************/
  183. static struct st7789_dev_s g_lcddev;
  184. /****************************************************************************
  185. * Private Functions
  186. ****************************************************************************/
  187. /****************************************************************************
  188. * Name: st7789_select
  189. *
  190. * Description:
  191. * Select the SPI, locking and re-configuring if necessary
  192. *
  193. * Input Parameters:
  194. * spi - Reference to the SPI driver structure
  195. * bits - Number of SPI bits
  196. *
  197. * Returned Value:
  198. * None
  199. *
  200. * Assumptions:
  201. *
  202. ****************************************************************************/
  203. static void st7789_select(FAR struct spi_dev_s *spi, int bits)
  204. {
  205. /* Select ST7789 chip (locking the SPI bus in case there are multiple
  206. * devices competing for the SPI bus
  207. */
  208. SPI_LOCK(spi, true);
  209. SPI_SELECT(spi, SPIDEV_DISPLAY(0), true);
  210. /* Now make sure that the SPI bus is configured for the ST7789 (it
  211. * might have gotten configured for a different device while unlocked)
  212. */
  213. SPI_SETMODE(spi, CONFIG_LCD_ST7789_SPIMODE);
  214. SPI_SETBITS(spi, bits);
  215. SPI_SETFREQUENCY(spi, CONFIG_LCD_ST7789_FREQUENCY);
  216. }
  217. /****************************************************************************
  218. * Name: st7789_deselect
  219. *
  220. * Description:
  221. * De-select the SPI
  222. *
  223. * Input Parameters:
  224. * spi - Reference to the SPI driver structure
  225. *
  226. * Returned Value:
  227. * None
  228. *
  229. * Assumptions:
  230. *
  231. ****************************************************************************/
  232. static void st7789_deselect(FAR struct spi_dev_s *spi)
  233. {
  234. /* De-select ST7789 chip and relinquish the SPI bus. */
  235. SPI_SELECT(spi, SPIDEV_DISPLAY(0), false);
  236. SPI_LOCK(spi, false);
  237. }
  238. /****************************************************************************
  239. * Name: st7789_sendcmd
  240. *
  241. * Description:
  242. * Send a command to the driver.
  243. *
  244. ****************************************************************************/
  245. static inline void st7789_sendcmd(FAR struct st7789_dev_s *dev, uint8_t cmd)
  246. {
  247. st7789_select(dev->spi, ST7789_BYTESPP * 8);
  248. SPI_CMDDATA(dev->spi, SPIDEV_DISPLAY(0), true);
  249. SPI_SEND(dev->spi, cmd);
  250. SPI_CMDDATA(dev->spi, SPIDEV_DISPLAY(0), false);
  251. st7789_deselect(dev->spi);
  252. }
  253. /****************************************************************************
  254. * Name: st7789_sleep
  255. *
  256. * Description:
  257. * Sleep or wake up the driver.
  258. *
  259. ****************************************************************************/
  260. static void st7789_sleep(FAR struct st7789_dev_s *dev, bool sleep)
  261. {
  262. st7789_sendcmd(dev, sleep ? ST7789_SLPIN : ST7789_SLPOUT);
  263. up_mdelay(120);
  264. }
  265. /****************************************************************************
  266. * Name: st7789_display
  267. *
  268. * Description:
  269. * Turn on or off the display.
  270. *
  271. ****************************************************************************/
  272. static void st7789_display(FAR struct st7789_dev_s *dev, bool on)
  273. {
  274. st7789_sendcmd(dev, on ? ST7789_DISPON : ST7789_DISPOFF);
  275. st7789_sendcmd(dev, ST7789_INVON);
  276. }
  277. /****************************************************************************
  278. * Name: st7789_setorientation
  279. *
  280. * Description:
  281. * Set screen orientation.
  282. *
  283. ****************************************************************************/
  284. static void st7789_setorientation(FAR struct st7789_dev_s *dev)
  285. {
  286. /* No need to change the orientation in PORTRAIT mode */
  287. #if !defined(CONFIG_LCD_PORTRAIT)
  288. st7789_sendcmd(dev, ST7789_MADCTL);
  289. st7789_select(dev->spi, 8);
  290. # if defined(CONFIG_LCD_RLANDSCAPE)
  291. /* RLANDSCAPE : MY=1 MV=1 */
  292. SPI_SEND(dev->spi, 0xa0);
  293. # elif defined(CONFIG_LCD_LANDSCAPE)
  294. /* LANDSCAPE : MX=1 MV=1 */
  295. SPI_SEND(dev->spi, 0x70);
  296. # elif defined(CONFIG_LCD_RPORTRAIT)
  297. /* RPORTRAIT : MX=1 MY=1 */
  298. SPI_SEND(dev->spi, 0xc0);
  299. # endif
  300. st7789_deselect(dev->spi);
  301. #endif
  302. }
  303. /****************************************************************************
  304. * Name: st7789_setarea
  305. *
  306. * Description:
  307. * Set the rectangular area for an upcoming read or write from RAM.
  308. *
  309. ****************************************************************************/
  310. static void st7789_setarea(FAR struct st7789_dev_s *dev,
  311. uint16_t x0, uint16_t y0,
  312. uint16_t x1, uint16_t y1)
  313. {
  314. /* Set row address */
  315. st7789_sendcmd(dev, ST7789_RASET);
  316. st7789_select(dev->spi, 16);
  317. SPI_SEND(dev->spi, y0 + ST7789_YOFFSET);
  318. SPI_SEND(dev->spi, y1 + ST7789_YOFFSET);
  319. st7789_deselect(dev->spi);
  320. /* Set column address */
  321. st7789_sendcmd(dev, ST7789_CASET);
  322. st7789_select(dev->spi, 16);
  323. SPI_SEND(dev->spi, x0 + ST7789_XOFFSET);
  324. SPI_SEND(dev->spi, x1 + ST7789_XOFFSET);
  325. st7789_deselect(dev->spi);
  326. }
  327. /****************************************************************************
  328. * Name: st7789_bpp
  329. *
  330. * Description:
  331. * Set the color depth of the device.
  332. *
  333. ****************************************************************************/
  334. static void st7789_bpp(FAR struct st7789_dev_s *dev, int bpp)
  335. {
  336. uint8_t depth;
  337. /* Don't send any command if the depth hasn't changed. */
  338. if (dev->bpp != bpp)
  339. {
  340. /* REVISIT: Works only for 12 and 16 bpp! */
  341. depth = bpp >> 2 | 1;
  342. st7789_sendcmd(dev, ST7789_COLMOD);
  343. st7789_select(dev->spi, 8);
  344. SPI_SEND(dev->spi, depth);
  345. st7789_deselect(dev->spi);
  346. /* Cache the new BPP */
  347. dev->bpp = bpp;
  348. }
  349. }
  350. /****************************************************************************
  351. * Name: st7789_wrram
  352. *
  353. * Description:
  354. * Write to the driver's RAM.
  355. *
  356. ****************************************************************************/
  357. static void st7789_wrram(FAR struct st7789_dev_s *dev,
  358. FAR const uint16_t *buff, size_t size)
  359. {
  360. st7789_sendcmd(dev, ST7789_RAMWR);
  361. st7789_select(dev->spi, ST7789_BYTESPP * 8);
  362. SPI_SNDBLOCK(dev->spi, buff, size);
  363. st7789_deselect(dev->spi);
  364. }
  365. /****************************************************************************
  366. * Name: st7789_rdram
  367. *
  368. * Description:
  369. * Read from the driver's RAM.
  370. *
  371. ****************************************************************************/
  372. #ifndef CONFIG_LCD_NOGETRUN
  373. static void st7789_rdram(FAR struct st7789_dev_s *dev,
  374. FAR uint16_t *buff, size_t size)
  375. {
  376. st7789_sendcmd(dev, ST7789_RAMRD);
  377. st7789_select(dev->spi, ST7789_BYTESPP * 8);
  378. SPI_RECVBLOCK(dev->spi, buff, size);
  379. st7789_deselect(dev->spi);
  380. }
  381. #endif
  382. /****************************************************************************
  383. * Name: st7789_fill
  384. *
  385. * Description:
  386. * Fill the display with the specified color.
  387. *
  388. ****************************************************************************/
  389. static void st7789_fill(FAR struct st7789_dev_s *dev, uint16_t color)
  390. {
  391. int i;
  392. st7789_setarea(dev, 0, 0, ST7789_XRES - 1, ST7789_YRES - 1);
  393. st7789_sendcmd(dev, ST7789_RAMWR);
  394. st7789_select(dev->spi, ST7789_BYTESPP *8);
  395. for (i = 0; i < ST7789_XRES * ST7789_YRES; i++)
  396. {
  397. SPI_SEND(dev->spi, color);
  398. }
  399. st7789_deselect(dev->spi);
  400. }
  401. /****************************************************************************
  402. * Name: st7789_putrun
  403. *
  404. * Description:
  405. * This method can be used to write a partial raster line to the LCD:
  406. *
  407. * row - Starting row to write to (range: 0 <= row < yres)
  408. * col - Starting column to write to (range: 0 <= col <= xres-npixels)
  409. * buffer - The buffer containing the run to be written to the LCD
  410. * npixels - The number of pixels to write to the LCD
  411. * (range: 0 < npixels <= xres-col)
  412. *
  413. ****************************************************************************/
  414. static int st7789_putrun(fb_coord_t row, fb_coord_t col,
  415. FAR const uint8_t *buffer, size_t npixels)
  416. {
  417. FAR struct st7789_dev_s *priv = &g_lcddev;
  418. FAR const uint16_t *src = (FAR const uint16_t *)buffer;
  419. ginfo("row: %d col: %d npixels: %d\n", row, col, npixels);
  420. DEBUGASSERT(buffer && ((uintptr_t)buffer & 1) == 0);
  421. st7789_setarea(priv, col, row, col + npixels - 1, row);
  422. st7789_wrram(priv, src, npixels);
  423. return OK;
  424. }
  425. /****************************************************************************
  426. * Name: st7789_putarea
  427. *
  428. * Description:
  429. * This method can be used to write a partial area to the LCD:
  430. *
  431. * row_start - Starting row to write to (range: 0 <= row < yres)
  432. * row_end - Ending row to write to (range: row_start <= row < yres)
  433. * col_start - Starting column to write to (range: 0 <= col <= xres)
  434. * col_end - Ending column to write to
  435. * (range: col_start <= col_end < xres)
  436. * buffer - The buffer containing the area to be written to the LCD
  437. *
  438. ****************************************************************************/
  439. static int st7789_putarea(fb_coord_t row_start, fb_coord_t row_end,
  440. fb_coord_t col_start, fb_coord_t col_end,
  441. FAR const uint8_t *buffer)
  442. {
  443. FAR struct st7789_dev_s *priv = &g_lcddev;
  444. FAR const uint16_t *src = (FAR const uint16_t *)buffer;
  445. ginfo("row_start: %d row_end: %d col_start: %d col_end: %d\n",
  446. row_start, row_end, col_start, col_end);
  447. DEBUGASSERT(buffer && ((uintptr_t)buffer & 1) == 0);
  448. st7789_setarea(priv, col_start, row_start, col_end, row_end);
  449. st7789_wrram(priv, src,
  450. (row_end - row_start + 1) * (col_end - col_start + 1));
  451. return OK;
  452. }
  453. /****************************************************************************
  454. * Name: st7789_getrun
  455. *
  456. * Description:
  457. * This method can be used to read a partial raster line from the LCD:
  458. *
  459. * row - Starting row to read from (range: 0 <= row < yres)
  460. * col - Starting column to read read (range: 0 <= col <= xres-npixels)
  461. * buffer - The buffer in which to return the run read from the LCD
  462. * npixels - The number of pixels to read from the LCD
  463. * (range: 0 < npixels <= xres-col)
  464. *
  465. ****************************************************************************/
  466. #ifndef CONFIG_LCD_NOGETRUN
  467. static int st7789_getrun(fb_coord_t row, fb_coord_t col, FAR uint8_t *buffer,
  468. size_t npixels)
  469. {
  470. FAR struct st7789_dev_s *priv = &g_lcddev;
  471. FAR uint16_t *dest = (FAR uint16_t *)buffer;
  472. ginfo("row: %d col: %d npixels: %d\n", row, col, npixels);
  473. DEBUGASSERT(buffer && ((uintptr_t)buffer & 1) == 0);
  474. st7789_setarea(priv, col, row, col + npixels - 1, row);
  475. st7789_rdram(priv, dest, npixels);
  476. return OK;
  477. }
  478. #endif
  479. /****************************************************************************
  480. * Name: st7789_getvideoinfo
  481. *
  482. * Description:
  483. * Get information about the LCD video controller configuration.
  484. *
  485. ****************************************************************************/
  486. static int st7789_getvideoinfo(FAR struct lcd_dev_s *dev,
  487. FAR struct fb_videoinfo_s *vinfo)
  488. {
  489. DEBUGASSERT(dev && vinfo);
  490. lcdinfo("fmt: %d xres: %d yres: %d nplanes: 1\n",
  491. ST7789_COLORFMT, ST7789_XRES, ST7789_YRES);
  492. vinfo->fmt = ST7789_COLORFMT; /* Color format: RGB16-565: RRRR RGGG GGGB BBBB */
  493. vinfo->xres = ST7789_XRES; /* Horizontal resolution in pixel columns */
  494. vinfo->yres = ST7789_YRES; /* Vertical resolution in pixel rows */
  495. vinfo->nplanes = 1; /* Number of color planes supported */
  496. return OK;
  497. }
  498. /****************************************************************************
  499. * Name: st7789_getplaneinfo
  500. *
  501. * Description:
  502. * Get information about the configuration of each LCD color plane.
  503. *
  504. ****************************************************************************/
  505. static int st7789_getplaneinfo(FAR struct lcd_dev_s *dev,
  506. unsigned int planeno,
  507. FAR struct lcd_planeinfo_s *pinfo)
  508. {
  509. FAR struct st7789_dev_s *priv = (FAR struct st7789_dev_s *)dev;
  510. DEBUGASSERT(dev && pinfo && planeno == 0);
  511. lcdinfo("planeno: %d bpp: %d\n", planeno, ST7789_BPP);
  512. pinfo->putrun = st7789_putrun; /* Put a run into LCD memory */
  513. pinfo->putarea = st7789_putarea; /* Put an area into LCD */
  514. #ifndef CONFIG_LCD_NOGETRUN
  515. pinfo->getrun = st7789_getrun; /* Get a run from LCD memory */
  516. #endif
  517. pinfo->buffer = (FAR uint8_t *)priv->runbuffer; /* Run scratch buffer */
  518. pinfo->bpp = priv->bpp; /* Bits-per-pixel */
  519. return OK;
  520. }
  521. /****************************************************************************
  522. * Name: st7789_getpower
  523. ****************************************************************************/
  524. static int st7789_getpower(FAR struct lcd_dev_s *dev)
  525. {
  526. FAR struct st7789_dev_s *priv = (FAR struct st7789_dev_s *)dev;
  527. lcdinfo("power: %d\n", priv->power);
  528. return priv->power;
  529. }
  530. /****************************************************************************
  531. * Name: st7789_setpower
  532. ****************************************************************************/
  533. static int st7789_setpower(FAR struct lcd_dev_s *dev, int power)
  534. {
  535. FAR struct st7789_dev_s *priv = (FAR struct st7789_dev_s *)dev;
  536. lcdinfo("power: %d\n", power);
  537. DEBUGASSERT((unsigned)power <= CONFIG_LCD_MAXPOWER);
  538. /* Set new power level */
  539. if (power > 0)
  540. {
  541. /* Turn on the display */
  542. st7789_display(priv, true);
  543. /* Save the power */
  544. priv->power = power;
  545. }
  546. else
  547. {
  548. /* Turn off the display */
  549. st7789_display(priv, false);
  550. /* Save the power */
  551. priv->power = 0;
  552. }
  553. return OK;
  554. }
  555. /****************************************************************************
  556. * Name: st7789_getcontrast
  557. *
  558. * Description:
  559. * Get the current contrast setting (0-CONFIG_LCD_MAXCONTRAST).
  560. *
  561. ****************************************************************************/
  562. static int st7789_getcontrast(FAR struct lcd_dev_s *dev)
  563. {
  564. lcdinfo("Not implemented\n");
  565. return -ENOSYS;
  566. }
  567. /****************************************************************************
  568. * Name: st7789_setcontrast
  569. *
  570. * Description:
  571. * Set LCD panel contrast (0-CONFIG_LCD_MAXCONTRAST).
  572. *
  573. ****************************************************************************/
  574. static int st7789_setcontrast(FAR struct lcd_dev_s *dev,
  575. unsigned int contrast)
  576. {
  577. lcdinfo("contrast: %d\n", contrast);
  578. return -ENOSYS;
  579. }
  580. /****************************************************************************
  581. * Public Functions
  582. ****************************************************************************/
  583. /****************************************************************************
  584. * Name: st7789_initialize
  585. *
  586. * Description:
  587. * Initialize the ST7789 video hardware. The initial state of the
  588. * LCD is fully initialized, display memory cleared, and the LCD ready
  589. * to use, but with the power setting at 0 (full off == sleep mode).
  590. *
  591. * Returned Value:
  592. *
  593. * On success, this function returns a reference to the LCD object for
  594. * the specified LCD. NULL is returned on any failure.
  595. *
  596. ****************************************************************************/
  597. FAR struct lcd_dev_s *st7789_lcdinitialize(FAR struct spi_dev_s *spi)
  598. {
  599. FAR struct st7789_dev_s *priv = &g_lcddev;
  600. /* Initialize the driver data structure */
  601. priv->dev.getvideoinfo = st7789_getvideoinfo;
  602. priv->dev.getplaneinfo = st7789_getplaneinfo;
  603. priv->dev.getpower = st7789_getpower;
  604. priv->dev.setpower = st7789_setpower;
  605. priv->dev.getcontrast = st7789_getcontrast;
  606. priv->dev.setcontrast = st7789_setcontrast;
  607. priv->spi = spi;
  608. /* Init the hardware and clear the display */
  609. st7789_sleep(priv, false);
  610. st7789_bpp(priv, ST7789_BPP);
  611. st7789_setorientation(priv);
  612. st7789_display(priv, true);
  613. st7789_fill(priv, 0xffff);
  614. return &priv->dev;
  615. }
  616. #endif /* CONFIG_LCD_ST7789 */