pcd8544.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /**************************************************************************************
  2. * drivers/lcd/pcd8544.c
  3. *
  4. * Driver for the Philips PCD8544 Display controller
  5. *
  6. * Copyright (C) 2017 Alan Carvalho de Assis. All rights reserved.
  7. * Author: Alan Carvalho de Assis <acassis@gmail.com>
  8. *
  9. * Based on drivers/lcd/pcd8544.c.
  10. *
  11. * Copyright (C) 2013 Zilogic Systems. All rights reserved.
  12. * Author: Manikandan <code@zilogic.com>
  13. *
  14. * Copyright (C) 2011 Gregory Nutt. All rights reserved.
  15. * Author: Gregory Nutt <gnutt@nuttx.org>
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in
  25. * the documentation and/or other materials provided with the
  26. * distribution.
  27. * 3. Neither the name NuttX nor the names of its contributors may be
  28. * used to endorse or promote products derived from this software
  29. * without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  35. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  36. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  37. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  38. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  39. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  41. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. * POSSIBILITY OF SUCH DAMAGE.
  43. *
  44. **************************************************************************************/
  45. /**************************************************************************************
  46. * Included Files
  47. **************************************************************************************/
  48. #include <nuttx/config.h>
  49. #include <sys/types.h>
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include <string.h>
  53. #include <errno.h>
  54. #include <debug.h>
  55. #include <nuttx/arch.h>
  56. #include <nuttx/spi/spi.h>
  57. #include <nuttx/lcd/lcd.h>
  58. #include <nuttx/lcd/pcd8544.h>
  59. #include "pcd8544.h"
  60. /**************************************************************************************
  61. * Pre-processor Definitions
  62. **************************************************************************************/
  63. /* Configuration **********************************************************************/
  64. /* PCD8544 Configuration Settings:
  65. *
  66. * CONFIG_PCD8544_SPIMODE - Controls the SPI mode
  67. * CONFIG_PCD8544_FREQUENCY - Define to use a different bus frequency
  68. * CONFIG_PCD8544_NINTERFACES - Specifies the number of physical
  69. * PCD8544 devices that will be supported. NOTE: At present, this
  70. * must be undefined or defined to be 1.
  71. * CONFIG_LCD_PCD8544DEBUG - Enable detailed PCD8544 debug pcd8544 output
  72. * (CONFIG_DEBUG_FEATURES and CONFIG_VERBOSE must also be enabled).
  73. *
  74. * Required LCD driver settings:
  75. * CONFIG_LCD_PCD8544 - Enable PCD8544 support
  76. * CONFIG_LCD_MAXCONTRAST should be 255, but any value >0 and <=255 will be accepted.
  77. * CONFIG_LCD_MAXPOWER should be 1: 0=off, 1=normal
  78. *
  79. * Required SPI driver settings:
  80. * CONFIG_SPI_CMDDATA - Include support for cmd/data selection.
  81. */
  82. /* Verify that all configuration requirements have been met */
  83. #ifndef CONFIG_PCD8544_SPIMODE
  84. # define CONFIG_PCD8544_SPIMODE SPIDEV_MODE0
  85. #endif
  86. /* SPI frequency */
  87. #ifndef CONFIG_PCD8544_FREQUENCY
  88. # define CONFIG_PCD8544_FREQUENCY 3000000
  89. #endif
  90. /* CONFIG_PCD8544_NINTERFACES determines the number of physical interfaces
  91. * that will be supported.
  92. */
  93. #ifndef CONFIG_PCD8544_NINTERFACES
  94. # define CONFIG_PCD8544_NINTERFACES 1
  95. #endif
  96. #if CONFIG_PCD8544_NINTERFACES != 1
  97. # warning "Only a single PCD8544 interface is supported"
  98. # undef CONFIG_PCD8544_NINTERFACES
  99. # define CONFIG_PCD8544_NINTERFACES 1
  100. #endif
  101. /* Verbose debug pcd8544 must also be enabled to use the extra OLED debug pcd8544 */
  102. #ifndef CONFIG_DEBUG_FEATURES
  103. # undef CONFIG_DEBUG_INFO
  104. # undef CONFIG_DEBUG_GRAPHICS
  105. #endif
  106. #ifndef CONFIG_DEBUG_INFO
  107. # undef CONFIG_LCD_PCD8544DEBUG
  108. #endif
  109. /* Check contrast selection */
  110. #ifndef CONFIG_LCD_MAXCONTRAST
  111. # define CONFIG_LCD_MAXCONTRAST 127
  112. #endif
  113. #if CONFIG_LCD_MAXCONTRAST <= 0 || CONFIG_LCD_MAXCONTRAST > 127
  114. # error "CONFIG_LCD_MAXCONTRAST exceeds supported maximum"
  115. #endif
  116. #if CONFIG_LCD_MAXCONTRAST < 60
  117. # warning "Optimal setting of CONFIG_LCD_MAXCONTRAST is 60"
  118. #endif
  119. /* Check power setting */
  120. #if !defined(CONFIG_LCD_MAXPOWER)
  121. # define CONFIG_LCD_MAXPOWER 1
  122. #endif
  123. #if CONFIG_LCD_MAXPOWER != 1
  124. # warning "CONFIG_LCD_MAXPOWER should be 1"
  125. # undef CONFIG_LCD_MAXPOWER
  126. # define CONFIG_LCD_MAXPOWER 1
  127. #endif
  128. /* The Display requires CMD/DATA SPI support */
  129. #ifndef CONFIG_SPI_CMDDATA
  130. # error "CONFIG_SPI_CMDDATA must be defined in your NuttX configuration"
  131. #endif
  132. /* Color Properties *******************************************************************/
  133. /* The PCD8544 display controller can handle a resolution of 84x48.
  134. */
  135. /* Display Resolution */
  136. #ifdef CONFIG_PCD8544_XRES
  137. #define PCD8544_XRES CONFIG_PCD8544_XRES
  138. #else
  139. #define PCD8544_XRES 84
  140. #endif
  141. #ifdef CONFIG_PCD8544_YRES
  142. #define PCD8544_YRES CONFIG_PCD8544_YRES
  143. #else
  144. #define PCD8544_YRES 48
  145. #endif
  146. /* Color depth and format */
  147. #define PCD8544_BPP 1
  148. #define PCD8544_COLORFMT FB_FMT_Y1
  149. /* Bytes per logical row and actual device row */
  150. #define PCD8544_XSTRIDE (PCD8544_XRES >> 3) /* Pixels arrange "horizontally for user" */
  151. #define PCD8544_YSTRIDE (PCD8544_YRES >> 3) /* But actual device arrangement is "vertical" */
  152. /* The size of the shadow frame buffer */
  153. #define PCD8544_FBSIZE (PCD8544_XRES * PCD8544_YSTRIDE)
  154. /* Bit helpers */
  155. #define LS_BIT (1 << 0)
  156. #define MS_BIT (1 << 7)
  157. /**************************************************************************************
  158. * Private Type Definition
  159. **************************************************************************************/
  160. /* This structure describes the state of this driver */
  161. struct pcd8544_dev_s
  162. {
  163. /* Publically visible device structure */
  164. struct lcd_dev_s dev;
  165. /* Private LCD-specific information follows */
  166. FAR struct spi_dev_s *spi;
  167. uint8_t contrast;
  168. uint8_t powered;
  169. /* The PCD8544 does not support reading from the display memory in SPI mode.
  170. * Since there is 1 BPP and access is byte-by-byte, it is necessary to keep
  171. * a shadow copy of the framebuffer memory.
  172. */
  173. uint8_t fb[PCD8544_FBSIZE];
  174. };
  175. /**************************************************************************************
  176. * Private Function Protototypes
  177. **************************************************************************************/
  178. /* SPI helpers */
  179. static void pcd8544_select(FAR struct spi_dev_s *spi);
  180. static void pcd8544_deselect(FAR struct spi_dev_s *spi);
  181. /* LCD Data Transfer Methods */
  182. static int pcd8544_putrun(fb_coord_t row, fb_coord_t col, FAR const uint8_t *buffer,
  183. size_t npixels);
  184. static int pcd8544_getrun(fb_coord_t row, fb_coord_t col, FAR uint8_t *buffer,
  185. size_t npixels);
  186. /* LCD Configuration */
  187. static int pcd8544_getvideoinfo(FAR struct lcd_dev_s *dev,
  188. FAR struct fb_videoinfo_s *vinfo);
  189. static int pcd8544_getplaneinfo(FAR struct lcd_dev_s *dev, unsigned int planeno,
  190. FAR struct lcd_planeinfo_s *pinfo);
  191. /* LCD RGB Mapping */
  192. #ifdef CONFIG_FB_CMAP
  193. # error "RGB color mapping not supported by this driver"
  194. #endif
  195. /* Cursor Controls */
  196. #ifdef CONFIG_FB_HWCURSOR
  197. # error "Cursor control not supported by this driver"
  198. #endif
  199. /* LCD Specific Controls */
  200. static int pcd8544_getpower(struct lcd_dev_s *dev);
  201. static int pcd8544_setpower(struct lcd_dev_s *dev, int power);
  202. static int pcd8544_getcontrast(struct lcd_dev_s *dev);
  203. static int pcd8544_setcontrast(struct lcd_dev_s *dev, unsigned int contrast);
  204. /* Initialization */
  205. static inline void up_clear(FAR struct pcd8544_dev_s *priv);
  206. /**************************************************************************************
  207. * Private Data
  208. **************************************************************************************/
  209. /* This is working memory allocated by the LCD driver for each LCD device
  210. * and for each color plane. This memory will hold one raster line of data.
  211. * The size of the allocated run buffer must therefore be at least
  212. * (bpp * xres / 8). Actual alignment of the buffer must conform to the
  213. * bitwidth of the underlying pixel type.
  214. *
  215. * If there are multiple planes, they may share the same working buffer
  216. * because different planes will not be operate on concurrently. However,
  217. * if there are multiple LCD devices, they must each have unique run buffers.
  218. */
  219. static uint8_t g_runbuffer[PCD8544_XSTRIDE+1];
  220. /* This structure describes the overall LCD video controller */
  221. static const struct fb_videoinfo_s g_videoinfo =
  222. {
  223. PCD8544_COLORFMT, /* Color format: RGB16-565: RRRR RGGG GGGB BBBB */
  224. PCD8544_XRES, /* Horizontal resolution in pixel columns */
  225. PCD8544_YRES, /* Vertical resolution in pixel rows */
  226. 1, /* Number of color planes supported */
  227. };
  228. /* This is the standard, NuttX Plane information object */
  229. static const struct lcd_planeinfo_s g_planeinfo =
  230. {
  231. pcd8544_putrun, /* Put a run into LCD memory */
  232. pcd8544_getrun, /* Get a run from LCD memory */
  233. (FAR uint8_t *)g_runbuffer, /* Run scratch buffer */
  234. PCD8544_BPP, /* Bits-per-pixel */
  235. };
  236. /* This is the standard, NuttX LCD driver object */
  237. static struct pcd8544_dev_s g_pcd8544dev =
  238. {
  239. /* struct lcd_dev_s */
  240. {
  241. /* LCD Configuration */
  242. pcd8544_getvideoinfo,
  243. pcd8544_getplaneinfo,
  244. /* LCD RGB Mapping -- Not supported */
  245. #ifdef CONFIG_FB_CMAP
  246. NULL,
  247. NULL,
  248. #endif
  249. /* Cursor Controls -- Not supported */
  250. #ifdef CONFIG_FB_HWCURSOR
  251. NULL,
  252. NULL,
  253. #endif
  254. /* LCD Specific Controls */
  255. pcd8544_getpower,
  256. pcd8544_setpower,
  257. pcd8544_getcontrast,
  258. pcd8544_setcontrast,
  259. },
  260. };
  261. /**************************************************************************************
  262. * Private Functions
  263. **************************************************************************************/
  264. /**************************************************************************************
  265. * Name: pcd8544_powerstring
  266. *
  267. * Description:
  268. * Convert the power setting to a string.
  269. *
  270. **************************************************************************************/
  271. static inline FAR const char *pcd8544_powerstring(uint8_t power)
  272. {
  273. if (power == PCD8544_POWER_OFF)
  274. {
  275. return "OFF";
  276. }
  277. else if (power == PCD8544_POWER_ON)
  278. {
  279. return "ON";
  280. }
  281. else
  282. {
  283. return "ERROR";
  284. }
  285. }
  286. /**************************************************************************************
  287. * Name: pcd8544_select
  288. *
  289. * Description:
  290. * Select the SPI, locking and re-configuring if necessary
  291. *
  292. * Input Parameters:
  293. * spi - Reference to the SPI driver structure
  294. *
  295. * Returned Value:
  296. * None
  297. *
  298. * Assumptions:
  299. *
  300. **************************************************************************************/
  301. static void pcd8544_select(FAR struct spi_dev_s *spi)
  302. {
  303. /* Select PCD8544 chip (locking the SPI bus in case there are multiple
  304. * devices competing for the SPI bus
  305. */
  306. SPI_LOCK(spi, true);
  307. SPI_SELECT(spi, SPIDEV_DISPLAY(0), true);
  308. /* Now make sure that the SPI bus is configured for the PCD8544 (it
  309. * might have gotten configured for a different device while unlocked)
  310. */
  311. SPI_SETMODE(spi, CONFIG_PCD8544_SPIMODE);
  312. SPI_SETBITS(spi, 8);
  313. (void)SPI_HWFEATURES(spi, 0);
  314. (void)SPI_SETFREQUENCY(spi, CONFIG_PCD8544_FREQUENCY);
  315. }
  316. /**************************************************************************************
  317. * Name: pcd8544_deselect
  318. *
  319. * Description:
  320. * De-select the SPI
  321. *
  322. * Input Parameters:
  323. * spi - Reference to the SPI driver structure
  324. *
  325. * Returned Value:
  326. * None
  327. *
  328. * Assumptions:
  329. *
  330. **************************************************************************************/
  331. static void pcd8544_deselect(FAR struct spi_dev_s *spi)
  332. {
  333. /* De-select PCD8544 chip and relinquish the SPI bus. */
  334. SPI_SELECT(spi, SPIDEV_DISPLAY(0), false);
  335. SPI_LOCK(spi, false);
  336. }
  337. /**************************************************************************************
  338. * Name: pcd8544_putrun
  339. *
  340. * Description:
  341. * This method can be used to write a partial raster line to the LCD:
  342. *
  343. * row - Starting row to write to (range: 0 <= row < yres)
  344. * col - Starting column to write to (range: 0 <= col <= xres-npixels)
  345. * buffer - The buffer containing the run to be written to the LCD
  346. * npixels - The number of pixels to write to the LCD
  347. * (range: 0 < npixels <= xres-col)
  348. *
  349. **************************************************************************************/
  350. static int pcd8544_putrun(fb_coord_t row, fb_coord_t col, FAR const uint8_t *buffer,
  351. size_t npixels)
  352. {
  353. /* Because of this line of code, we will only be able to support a single PCD8544 device */
  354. FAR struct pcd8544_dev_s *priv = &g_pcd8544dev;
  355. FAR uint8_t *fbptr;
  356. FAR uint8_t *ptr;
  357. uint8_t fbmask;
  358. uint8_t page;
  359. uint8_t usrmask;
  360. uint8_t i;
  361. int pixlen;
  362. ginfo("row: %d col: %d npixels: %d\n", row, col, npixels);
  363. DEBUGASSERT(buffer);
  364. /* Clip the run to the display */
  365. pixlen = npixels;
  366. if ((unsigned int)col + (unsigned int)pixlen > (unsigned int)PCD8544_XRES)
  367. {
  368. pixlen = (int)PCD8544_XRES - (int)col;
  369. }
  370. /* Verify that some portion of the run remains on the display */
  371. if (pixlen <= 0 || row > PCD8544_YRES)
  372. {
  373. return OK;
  374. }
  375. /* Get the page number. The range of 48 lines is divided up into six
  376. * pages of 8 lines each.
  377. */
  378. page = row >> 3;
  379. /* Update the shadow frame buffer memory. First determine the pixel
  380. * position in the frame buffer memory. Pixels are organized like
  381. * this:
  382. *
  383. * --------+---+---+---+---+-...-+----+
  384. * Segment | 0 | 1 | 2 | 3 | ... | 83 |
  385. * --------+---+---+---+---+-...-+----+
  386. * Bit 0 | | X | | | | |
  387. * Bit 1 | | X | | | | |
  388. * Bit 2 | | X | | | | |
  389. * Bit 3 | | X | | | | |
  390. * Bit 4 | | X | | | | |
  391. * Bit 5 | | X | | | | |
  392. * Bit 6 | | X | | | | |
  393. * Bit 7 | | X | | | | |
  394. * --------+---+---+---+---+-...-+----+
  395. *
  396. * So, in order to draw a white, horizontal line, at row 45. we
  397. * would have to modify all of the bytes in page 45/8 = 5. We
  398. * would have to set bit 45%8 = 5 in every byte in the page.
  399. */
  400. fbmask = 1 << (row & 7);
  401. fbptr = &priv->fb[page * PCD8544_XRES + col];
  402. ptr = fbptr;
  403. #ifdef CONFIG_LCD_PACKEDMSFIRST
  404. usrmask = MS_BIT;
  405. #else
  406. usrmask = LS_BIT;
  407. #endif
  408. for (i = 0; i < pixlen; i++)
  409. {
  410. /* Set or clear the corresponding bit */
  411. if ((*buffer & usrmask) != 0)
  412. {
  413. *ptr++ |= fbmask;
  414. }
  415. else
  416. {
  417. *ptr++ &= ~fbmask;
  418. }
  419. /* Inc/Decrement to the next source pixel */
  420. #ifdef CONFIG_LCD_PACKEDMSFIRST
  421. if (usrmask == LS_BIT)
  422. {
  423. buffer++;
  424. usrmask = MS_BIT;
  425. }
  426. else
  427. {
  428. usrmask >>= 1;
  429. }
  430. #else
  431. if (usrmask == MS_BIT)
  432. {
  433. buffer++;
  434. usrmask = LS_BIT;
  435. }
  436. else
  437. {
  438. usrmask <<= 1;
  439. }
  440. #endif
  441. }
  442. /* Select and lock the device */
  443. pcd8544_select(priv->spi);
  444. /* Select command transfer */
  445. SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY(0), true);
  446. /* Set the starting position for the run */
  447. (void)SPI_SEND(priv->spi, PCD8544_SET_Y_ADDR+page); /* Set the page start */
  448. (void)SPI_SEND(priv->spi, PCD8544_SET_X_ADDR + (col & 0x7f)); /* Set the low column */
  449. /* Select data transfer */
  450. SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY(0), false);
  451. /* Then transfer all of the data */
  452. (void)SPI_SNDBLOCK(priv->spi, fbptr, pixlen);
  453. /* Unlock and de-select the device */
  454. pcd8544_deselect(priv->spi);
  455. return OK;
  456. }
  457. /**************************************************************************************
  458. * Name: pcd8544_getrun
  459. *
  460. * Description:
  461. * This method can be used to read a partial raster line from the LCD:
  462. *
  463. * row - Starting row to read from (range: 0 <= row < yres)
  464. * col - Starting column to read read (range: 0 <= col <= xres-npixels)
  465. * buffer - The buffer in which to return the run read from the LCD
  466. * npixels - The number of pixels to read from the LCD
  467. * (range: 0 < npixels <= xres-col)
  468. *
  469. **************************************************************************************/
  470. static int pcd8544_getrun(fb_coord_t row, fb_coord_t col, FAR uint8_t *buffer,
  471. size_t npixels)
  472. {
  473. /* Because of this line of code, we will only be able to support a single PCD8544 device */
  474. FAR struct pcd8544_dev_s *priv = &g_pcd8544dev;
  475. FAR uint8_t *fbptr;
  476. uint8_t page;
  477. uint8_t fbmask;
  478. uint8_t usrmask;
  479. uint8_t i;
  480. int pixlen;
  481. ginfo("row: %d col: %d npixels: %d\n", row, col, npixels);
  482. DEBUGASSERT(buffer);
  483. /* Clip the run to the display */
  484. pixlen = npixels;
  485. if ((unsigned int)col + (unsigned int)pixlen > (unsigned int)PCD8544_XRES)
  486. {
  487. pixlen = (int)PCD8544_XRES - (int)col;
  488. }
  489. /* Verify that some portion of the run is actually the display */
  490. if (pixlen <= 0 || row > PCD8544_YRES)
  491. {
  492. return -EINVAL;
  493. }
  494. /* Then transfer the display data from the shadow frame buffer memory */
  495. /* Get the page number. The range of 48 lines is divided up into six
  496. * pages of 8 lines each.
  497. */
  498. page = row >> 3;
  499. /* Update the shadow frame buffer memory. First determine the pixel
  500. * position in the frame buffer memory. Pixels are organized like
  501. * this:
  502. *
  503. * --------+---+---+---+---+-...-+----+
  504. * Segment | 0 | 1 | 2 | 3 | ... | 83 |
  505. * --------+---+---+---+---+-...-+----+
  506. * Bit 0 | | X | | | | |
  507. * Bit 1 | | X | | | | |
  508. * Bit 2 | | X | | | | |
  509. * Bit 3 | | X | | | | |
  510. * Bit 4 | | X | | | | |
  511. * Bit 5 | | X | | | | |
  512. * Bit 6 | | X | | | | |
  513. * Bit 7 | | X | | | | |
  514. * --------+---+---+---+---+-...-+----+
  515. *
  516. * So, in order to draw a white, horizontal line, at row 45. we
  517. * would have to modify all of the bytes in page 45/8 = 5. We
  518. * would have to set bit 45%8 = 5 in every byte in the page.
  519. */
  520. fbmask = 1 << (row & 7);
  521. fbptr = &priv->fb[page * PCD8544_XRES + col];
  522. #ifdef CONFIG_LCD_PACKEDMSFIRST
  523. usrmask = MS_BIT;
  524. #else
  525. usrmask = LS_BIT;
  526. #endif
  527. *buffer = 0;
  528. for (i = 0; i < pixlen; i++)
  529. {
  530. /* Set or clear the corresponding bit */
  531. uint8_t byte = *fbptr++;
  532. if ((byte & fbmask) != 0)
  533. {
  534. *buffer |= usrmask;
  535. }
  536. /* Inc/Decrement to the next destination pixel. Hmmmm. It looks like
  537. * this logic could write past the end of the user buffer. Revisit
  538. * this!
  539. */
  540. #ifdef CONFIG_LCD_PACKEDMSFIRST
  541. if (usrmask == LS_BIT)
  542. {
  543. buffer++;
  544. *buffer = 0;
  545. usrmask = MS_BIT;
  546. }
  547. else
  548. {
  549. usrmask >>= 1;
  550. }
  551. #else
  552. if (usrmask == MS_BIT)
  553. {
  554. buffer++;
  555. *buffer = 0;
  556. usrmask = LS_BIT;
  557. }
  558. else
  559. {
  560. usrmask <<= 1;
  561. }
  562. #endif
  563. }
  564. return OK;
  565. }
  566. /**************************************************************************************
  567. * Name: pcd8544_getvideoinfo
  568. *
  569. * Description:
  570. * Get information about the LCD video controller configuration.
  571. *
  572. **************************************************************************************/
  573. static int pcd8544_getvideoinfo(FAR struct lcd_dev_s *dev,
  574. FAR struct fb_videoinfo_s *vinfo)
  575. {
  576. DEBUGASSERT(dev && vinfo);
  577. ginfo("fmt: %d xres: %d yres: %d nplanes: %d\n",
  578. g_videoinfo.fmt, g_videoinfo.xres, g_videoinfo.yres, g_videoinfo.nplanes);
  579. memcpy(vinfo, &g_videoinfo, sizeof(struct fb_videoinfo_s));
  580. return OK;
  581. }
  582. /**************************************************************************************
  583. * Name: pcd8544_getplaneinfo
  584. *
  585. * Description:
  586. * Get information about the configuration of each LCD color plane.
  587. *
  588. **************************************************************************************/
  589. static int pcd8544_getplaneinfo(FAR struct lcd_dev_s *dev, unsigned int planeno,
  590. FAR struct lcd_planeinfo_s *pinfo)
  591. {
  592. DEBUGASSERT(dev && pinfo && planeno == 0);
  593. ginfo("planeno: %d bpp: %d\n", planeno, g_planeinfo.bpp);
  594. memcpy(pinfo, &g_planeinfo, sizeof(struct lcd_planeinfo_s));
  595. return OK;
  596. }
  597. /**************************************************************************************
  598. * Name: pcd8544_getpower
  599. *
  600. * Description:
  601. * Get the LCD panel power status (0: full off - CONFIG_LCD_MAXPOWER: full on). On
  602. * backlit LCDs, this setting may correspond to the backlight setting.
  603. *
  604. **************************************************************************************/
  605. static int pcd8544_getpower(struct lcd_dev_s *dev)
  606. {
  607. struct pcd8544_dev_s *priv = (struct pcd8544_dev_s *)dev;
  608. DEBUGASSERT(priv);
  609. ginfo("powered: %s\n", pcd8544_powerstring(priv->powered));
  610. return priv->powered;
  611. }
  612. /**************************************************************************************
  613. * Name: pcd8544_setpower
  614. *
  615. * Description:
  616. * Enable/disable LCD panel power (0: full off - CONFIG_LCD_MAXPOWER: full on). On
  617. * backlit LCDs, this setting may correspond to the backlight setting.
  618. *
  619. **************************************************************************************/
  620. static int pcd8544_setpower(struct lcd_dev_s *dev, int power)
  621. {
  622. struct pcd8544_dev_s *priv = (struct pcd8544_dev_s *)dev;
  623. DEBUGASSERT(priv && (unsigned)power <= CONFIG_LCD_MAXPOWER);
  624. ginfo("power: %s powered: %s\n",
  625. pcd8544_powerstring(power), pcd8544_powerstring(priv->powered));
  626. /* Select and lock the device */
  627. pcd8544_select(priv->spi);
  628. /* Select command transfer */
  629. SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY(0), true);
  630. if (power <= PCD8544_POWER_OFF)
  631. {
  632. /* Turn the display off (power-down) */
  633. (void)SPI_SEND(priv->spi, (PCD8544_FUNC_SET | PCD8544_POWER_DOWN));
  634. priv->powered = PCD8544_POWER_OFF;
  635. }
  636. else
  637. {
  638. /* Leave the power-down */
  639. (void)SPI_SEND(priv->spi, PCD8544_FUNC_SET);
  640. priv->powered = PCD8544_POWER_ON;
  641. }
  642. /* Select data transfer */
  643. SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY(0), false);
  644. /* Let go of the SPI lock and de-select the device */
  645. pcd8544_deselect(priv->spi);
  646. return OK;
  647. }
  648. /**************************************************************************************
  649. * Name: pcd8544_getcontrast
  650. *
  651. * Description:
  652. * Get the current contrast setting (0-CONFIG_LCD_MAXCONTRAST).
  653. *
  654. **************************************************************************************/
  655. static int pcd8544_getcontrast(struct lcd_dev_s *dev)
  656. {
  657. struct pcd8544_dev_s *priv = (struct pcd8544_dev_s *)dev;
  658. DEBUGASSERT(priv);
  659. return (int)priv->contrast;
  660. }
  661. /**************************************************************************************
  662. * Name: pcd8544_setcontrast
  663. *
  664. * Description:
  665. * Set LCD panel contrast (0-CONFIG_LCD_MAXCONTRAST).
  666. *
  667. **************************************************************************************/
  668. static int pcd8544_setcontrast(struct lcd_dev_s *dev, unsigned int contrast)
  669. {
  670. struct pcd8544_dev_s *priv = (struct pcd8544_dev_s *)dev;
  671. ginfo("contrast: %d\n", contrast);
  672. DEBUGASSERT(priv);
  673. if (contrast > 127)
  674. {
  675. return -EINVAL;
  676. }
  677. /* Save the contrast */
  678. priv->contrast = contrast;
  679. /* Select and lock the device */
  680. pcd8544_select(priv->spi);
  681. /* Select command transfer */
  682. SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY(0), true);
  683. /* Select the extended instruction set ( H = 1 ) */
  684. (void)SPI_SEND(priv->spi, (PCD8544_FUNC_SET | PCD8544_MODEH));
  685. /* Set the contrast */
  686. (void)SPI_SEND(priv->spi, (PCD8544_WRITE_VOP | contrast) );
  687. /* Return to normal mode */
  688. (void)SPI_SEND(priv->spi, PCD8544_FUNC_SET);
  689. /* Select data transfer */
  690. SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY(0), false);
  691. /* Let go of the SPI lock and de-select the device */
  692. pcd8544_deselect(priv->spi);
  693. return OK;
  694. }
  695. /**************************************************************************************
  696. * Name: up_clear
  697. *
  698. * Description:
  699. * Clear the display.
  700. *
  701. **************************************************************************************/
  702. static inline void up_clear(FAR struct pcd8544_dev_s *priv)
  703. {
  704. FAR struct spi_dev_s *spi = priv->spi;
  705. int page;
  706. int i;
  707. /* Clear the framebuffer */
  708. memset(priv->fb, PCD8544_Y1_BLACK, PCD8544_FBSIZE);
  709. /* Select and lock the device */
  710. pcd8544_select(priv->spi);
  711. /* Go throw pcd8544 all 6 pages */
  712. for (page = 0, i = 0; i < 6; i++)
  713. {
  714. /* Select command transfer */
  715. SPI_CMDDATA(spi, SPIDEV_DISPLAY(0), true);
  716. /* Set the starting position for the run */
  717. (void)SPI_SEND(priv->spi, PCD8544_SET_Y_ADDR+i); /* Set the page start */
  718. (void)SPI_SEND(priv->spi, PCD8544_SET_X_ADDR + page); /* Set the column */
  719. /* Select data transfer */
  720. SPI_CMDDATA(spi, SPIDEV_DISPLAY(0), false);
  721. /* Then transfer all 84 columns of data */
  722. (void)SPI_SNDBLOCK(priv->spi, &priv->fb[page * PCD8544_XRES], PCD8544_XRES);
  723. }
  724. /* Unlock and de-select the device */
  725. pcd8544_deselect(spi);
  726. }
  727. /**************************************************************************************
  728. * Public Functions
  729. **************************************************************************************/
  730. /**************************************************************************************
  731. * Name: pcd8544_initialize
  732. *
  733. * Description:
  734. * Initialize the PCD8544 video hardware. The initial state of the
  735. * OLED is fully initialized, display memory cleared, and the OLED ready to
  736. * use, but with the power setting at 0 (full off == sleep mode).
  737. *
  738. * Input Parameters:
  739. *
  740. * spi - A reference to the SPI driver instance.
  741. * devno - A value in the range of 0 thropcd8544h CONFIG_PCD8544_NINTERFACES-1.
  742. * This allows support for multiple OLED devices.
  743. *
  744. * Returned Value:
  745. *
  746. * On success, this function returns a reference to the LCD object for the specified
  747. * OLED. NULL is returned on any failure.
  748. *
  749. **************************************************************************************/
  750. FAR struct lcd_dev_s *pcd8544_initialize(FAR struct spi_dev_s *spi, unsigned int devno)
  751. {
  752. /* Configure and enable LCD */
  753. FAR struct pcd8544_dev_s *priv = &g_pcd8544dev;
  754. ginfo("Initializing\n");
  755. DEBUGASSERT(spi && devno == 0);
  756. /* Save the reference to the SPI device */
  757. priv->spi = spi;
  758. /* Select and lock the device */
  759. pcd8544_select(spi);
  760. /* Select command transfer */
  761. SPI_CMDDATA(spi, SPIDEV_DISPLAY(0), true);
  762. /* Leave the power-down and select extended instruction set mode H = 1 */
  763. (void)SPI_SEND(spi, (PCD8544_FUNC_SET | PCD8544_MODEH));
  764. /* Set LCD Bias to n = 3 */
  765. (void)SPI_SEND(spi, (PCD8544_BIAS_SYSTEM | PCD8544_BIAS_BS2));
  766. /* Select the normal instruction set mode H = 0 */
  767. (void)SPI_SEND(spi, PCD8544_FUNC_SET);
  768. /* Clear the screen */
  769. (void)SPI_SEND(spi, (PCD8544_DISP_CTRL | PCD8544_DISP_BLANK));
  770. /* Set the Display Control to Normal Mode D = 1 and E = 0 */
  771. (void)SPI_SEND(spi, (PCD8544_DISP_CTRL | PCD8544_DISP_NORMAL));
  772. /* Select data transfer */
  773. SPI_CMDDATA(spi, SPIDEV_DISPLAY(0), false);
  774. /* Let go of the SPI lock and de-select the device */
  775. pcd8544_deselect(spi);
  776. /* Clear the framebuffer */
  777. up_mdelay(100);
  778. up_clear(priv);
  779. return &priv->dev;
  780. }