st7565.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /**************************************************************************************
  2. * drivers/lcd/st7565.c
  3. *
  4. * Definitions for the ST7565 128x64 Dot Matrix LCD Driver with C
  5. *
  6. * Copyright (C) 2014 Pierre-noel Bouteville. All rights reserved.
  7. * Author: Pierre-noel Boutevlle <pnb990@gmail.com>
  8. *
  9. * Based on drivers/lcd/st7567.c
  10. * Copyright (C) 2013 Zilogic Systems. All rights reserved.
  11. * Author: Manikandan <code@zilogic.com>
  12. *
  13. * Based on drivers/lcd/ug-9664hswag01.c
  14. *
  15. * Copyright (C) 2011 Gregory Nutt. All rights reserved.
  16. * Author: Gregory Nutt <gnutt@nuttx.org>
  17. *
  18. * Reference: "Product Specification, OEL Display Module, ST7567", Univision
  19. * Technology Inc., SAS1-6020-B, January 3, 2008.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. *
  25. * 1. Redistributions of source code must retain the above copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in
  29. * the documentation and/or other materials provided with the
  30. * distribution.
  31. * 3. Neither the name NuttX nor the names of its contributors may be
  32. * used to endorse or promote products derived from this software
  33. * without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  38. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  39. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  41. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  42. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  43. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  45. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  46. * POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. **************************************************************************************/
  49. /**************************************************************************************
  50. * Included Files
  51. **************************************************************************************/
  52. #include <nuttx/config.h>
  53. #include <sys/types.h>
  54. #include <stdint.h>
  55. #include <stdbool.h>
  56. #include <string.h>
  57. #include <errno.h>
  58. #include <debug.h>
  59. #include <nuttx/arch.h>
  60. #include <nuttx/spi/spi.h>
  61. #include <nuttx/lcd/lcd.h>
  62. #include <nuttx/lcd/st7565.h>
  63. #include "st7565.h"
  64. /**************************************************************************************
  65. * Pre-processor Definitions
  66. **************************************************************************************/
  67. /* Configuration **********************************************************************/
  68. /* ST7565 Configuration Settings:
  69. *
  70. * CONFIG_ST7565_NINTERFACES - Specifies the number of physical
  71. * ST7565 devices that will be supported. NOTE: At present, this
  72. * must be undefined or defined to be 1.
  73. * CONFIG_LCD_ST7565DEBUG - Enable detailed ST7565 debst7565 output
  74. * (CONFIG_DEBUG_FEATURES and CONFIG_VERBOSE must also be enabled).
  75. *
  76. * Required LCD driver settings:
  77. * CONFIG_LCD_ST7565 - Enable ST7565 support
  78. * CONFIG_LCD_MAXCONTRAST should be 255, but any value >0 and <=255 will be accepted.
  79. *
  80. */
  81. /* Verify that all configuration requirements have been met */
  82. /* CONFIG_ST7565_NINTERFACES determines the number of physical interfaces
  83. * that will be supported.
  84. */
  85. #ifndef CONFIG_ST7565_NINTERFACES
  86. # define CONFIG_ST7565_NINTERFACES 1
  87. #endif
  88. #if CONFIG_ST7565_NINTERFACES != 1
  89. # warning "Only a single ST7565 interface is supported"
  90. # undef CONFIG_ST7565_NINTERFACES
  91. # define CONFIG_ST7565_NINTERFACES 1
  92. #endif
  93. /* Verbose debst7565 must also be enabled to use the extra OLED debst7565 */
  94. #ifndef CONFIG_DEBUG_FEATURES
  95. # undef CONFIG_DEBUG_INFO
  96. # undef CONFIG_DEBUG_GRAPHICS
  97. #endif
  98. #ifndef CONFIG_DEBUG_INFO
  99. # undef CONFIG_LCD_ST7565DEBUG
  100. #endif
  101. /* Check contrast selection */
  102. #ifndef CONFIG_LCD_MAXCONTRAST
  103. # define CONFIG_LCD_MAXCONTRAST 255
  104. #endif
  105. #if CONFIG_LCD_MAXCONTRAST <= 0 || CONFIG_LCD_MAXCONTRAST > 255
  106. # error "CONFIG_LCD_MAXCONTRAST exceeds supported maximum"
  107. #endif
  108. #if CONFIG_LCD_MAXCONTRAST < 255
  109. # warning "Optimal setting of CONFIG_LCD_MAXCONTRAST is 255"
  110. #endif
  111. /* Color Properties *******************************************************************/
  112. /* The ST7565 display controller can handle a resolution of 128x64.
  113. */
  114. /* Display Resolution */
  115. #ifdef CONFIG_ST7565_XRES
  116. # define ST7565_XRES CONFIG_ST7565_XRES
  117. #else
  118. # define ST7565_XRES 128
  119. #endif
  120. #ifdef CONFIG_ST7565_YRES
  121. # define ST7565_YRES CONFIG_ST7565_YRES
  122. #else
  123. # define ST7565_YRES 64
  124. #endif
  125. /* Color depth and format */
  126. #define ST7565_BPP 1
  127. #define ST7565_COLORFMT FB_FMT_Y1
  128. /* Bytes per logical row andactual device row */
  129. #define ST7565_XSTRIDE (ST7565_XRES >> 3) /* Pixels arrange "horizontally
  130. * for user" */
  131. #define ST7565_YSTRIDE (ST7565_YRES >> 3) /* But actual device
  132. * arrangement is "vertical" */
  133. /* The size of the shadow frame buffer */
  134. #define ST7565_FBSIZE (ST7565_XRES * ST7565_YSTRIDE)
  135. /* Bit helpers */
  136. #define LS_BIT (1 << 0)
  137. #define MS_BIT (1 << 7)
  138. /**************************************************************************************
  139. * Private Type Definition
  140. **************************************************************************************/
  141. /* This structure describes the state of this driver */
  142. struct st7565_dev_s
  143. {
  144. /* Publicly visible device structure */
  145. struct lcd_dev_s dev;
  146. /* Private LCD-specific information follows */
  147. FAR struct st7565_lcd_s *lcd;
  148. uint8_t contrast;
  149. uint8_t power_level;
  150. /* TODO implement READ mode, possible in 8080bus interface */
  151. /* The ST7565 does not support reading from the display memory in SPI mode.
  152. * Since there is 1 BPP and access is byte-by-byte, it is necessary to keep
  153. * a shadow copy of the framebuffer memory. */
  154. uint8_t fb[ST7565_FBSIZE];
  155. };
  156. /**************************************************************************************
  157. * Private Function Prototypes
  158. **************************************************************************************/
  159. /* Drivers helpers */
  160. static inline void st7565_reset(FAR struct st7565_dev_s *priv, bool on);
  161. static inline void st7565_select(FAR struct st7565_dev_s *priv);
  162. static inline void st7565_deselect(FAR struct st7565_dev_s *priv);
  163. static inline void st7565_cmddata(FAR struct st7565_dev_s *priv, bool cmd);
  164. static inline int st7565_send_one_data(FAR struct st7565_dev_s *priv,
  165. uint8_t data);
  166. static inline int st7565_send_data_buf(FAR struct st7565_dev_s *priv,
  167. FAR const uint8_t * buf, int size);
  168. static inline int st7565_backlight(FAR struct st7565_dev_s *priv, int level);
  169. /* LCD Data Transfer Methods */
  170. static int st7565_putrun(fb_coord_t row, fb_coord_t col,
  171. FAR const uint8_t * buffer, size_t npixels);
  172. static int st7565_getrun(fb_coord_t row, fb_coord_t col, FAR uint8_t * buffer,
  173. size_t npixels);
  174. /* LCD Configuration */
  175. static int st7565_getvideoinfo(FAR struct lcd_dev_s *dev,
  176. FAR struct fb_videoinfo_s *vinfo);
  177. static int st7565_getplaneinfo(FAR struct lcd_dev_s *dev, unsigned int planeno,
  178. FAR struct lcd_planeinfo_s *pinfo);
  179. /* LCD RGB Mapping */
  180. #ifdef CONFIG_FB_CMAP
  181. # error "RGB color mapping not supported by this driver"
  182. #endif
  183. /* Cursor Controls */
  184. #ifdef CONFIG_FB_HWCURSOR
  185. # error "Cursor control not supported by this driver"
  186. #endif
  187. /* LCD Specific Controls */
  188. static int st7565_getpower(struct lcd_dev_s *dev);
  189. static int st7565_setpower(struct lcd_dev_s *dev, int power);
  190. static int st7565_getcontrast(struct lcd_dev_s *dev);
  191. static int st7565_setcontrast(struct lcd_dev_s *dev, unsigned int contrast);
  192. /* Initialization */
  193. static inline void up_clear(FAR struct st7565_dev_s *priv);
  194. /**************************************************************************************
  195. * Private Data
  196. **************************************************************************************/
  197. /* This is working memory allocated by the LCD driver for each LCD device
  198. * and for each color plane. This memory will hold one raster line of data.
  199. * The size of the allocated run buffer must therefore be at least
  200. * (bpp * xres / 8). Actual alignment of the buffer must conform to the
  201. * bitwidth of the underlying pixel type.
  202. *
  203. * If there are multiple planes, they may share the same working buffer
  204. * because different planes will not be operate on concurrently. However,
  205. * if there are multiple LCD devices, they must each have unique run buffers.
  206. */
  207. static uint8_t g_runbuffer[ST7565_XSTRIDE + 1];
  208. /* This structure describes the overall LCD video controller */
  209. static const struct fb_videoinfo_s g_videoinfo =
  210. {
  211. .fmt = ST7565_COLORFMT, /* Color format: RGB16-565: RRRR RGGG GGGB BBBB */
  212. .xres = ST7565_XRES, /* Horizontal resolution in pixel columns */
  213. .yres = ST7565_YRES, /* Vertical resolution in pixel rows */
  214. .nplanes = 1, /* Number of color planes supported */
  215. };
  216. /* This is the standard, NuttX Plane information object */
  217. static const struct lcd_planeinfo_s g_planeinfo =
  218. {
  219. .putrun = st7565_putrun, /* Put a run into LCD memory */
  220. .getrun = st7565_getrun, /* Get a run from LCD memory */
  221. .buffer = (uint8_t *) g_runbuffer, /* Run scratch buffer */
  222. .bpp = ST7565_BPP, /* Bits-per-pixel */
  223. };
  224. /* This is the standard, NuttX LCD driver object */
  225. static struct st7565_dev_s g_st7565dev =
  226. {
  227. .dev =
  228. {
  229. /* LCD Configuration */
  230. .getvideoinfo = st7565_getvideoinfo,
  231. .getplaneinfo = st7565_getplaneinfo,
  232. /* LCD RGB Mapping -- Not supported */
  233. /* Cursor Controls -- Not supported */
  234. /* LCD Specific Controls */
  235. .getpower = st7565_getpower,
  236. .setpower = st7565_setpower,
  237. .getcontrast = st7565_getcontrast,
  238. .setcontrast = st7565_setcontrast,
  239. },
  240. };
  241. /**************************************************************************************
  242. * Private Functions
  243. **************************************************************************************/
  244. /**************************************************************************************
  245. * Name: st7565_reset
  246. *
  247. * Description:
  248. * Enable/Disable reset pin of LCD the device.
  249. *
  250. * priv - A reference to the driver specific structure
  251. *
  252. **************************************************************************************/
  253. static inline void st7565_reset(FAR struct st7565_dev_s *priv, bool on)
  254. {
  255. if (priv->lcd->reset != NULL)
  256. {
  257. priv->lcd->reset(priv->lcd, on);
  258. }
  259. }
  260. /**************************************************************************************
  261. * Name: st7565_select
  262. *
  263. * Description:
  264. * Select the device (as necessary) before performing any operations.
  265. *
  266. * priv - A reference to the driver specific structure
  267. *
  268. **************************************************************************************/
  269. static inline void st7565_select(FAR struct st7565_dev_s *priv)
  270. {
  271. priv->lcd->select(priv->lcd);
  272. }
  273. /**************************************************************************************
  274. * Name: st7565_deselect
  275. *
  276. * Description:
  277. * Deselect the device (as necessary).
  278. *
  279. * priv - A reference to the driver specific structure
  280. *
  281. **************************************************************************************/
  282. static inline void st7565_deselect(FAR struct st7565_dev_s *priv)
  283. {
  284. priv->lcd->deselect(priv->lcd);
  285. }
  286. /**************************************************************************************
  287. * Name: st7565_cmddata
  288. *
  289. * Description:
  290. * Select command (A0 = 0) or data (A0 = 1) mode.
  291. *
  292. * priv - A reference to the driver specific structure.
  293. * cmd - If true command mode will be selected.
  294. *
  295. **************************************************************************************/
  296. static inline void st7565_cmddata(FAR struct st7565_dev_s *priv, bool cmd)
  297. {
  298. priv->lcd->cmddata(priv->lcd, cmd);
  299. }
  300. /**************************************************************************************
  301. * Name: st7565_send_one_data
  302. *
  303. * Description:
  304. * Send one data to the LCD driver (A0 = 1).
  305. *
  306. * priv - A reference to the driver specific structure.
  307. * data - Byte to send as data to LCD driver.
  308. *
  309. **************************************************************************************/
  310. static inline int st7565_send_one_data(FAR struct st7565_dev_s *priv,
  311. uint8_t data)
  312. {
  313. return priv->lcd->senddata(priv->lcd, &data, 1);
  314. }
  315. /**************************************************************************************
  316. * Name: st7565_send_data_buf
  317. *
  318. * Description:
  319. * Send a data buffer to the LCD driver (A0 = 1).
  320. *
  321. * priv - A reference to the driver specific structure.
  322. * buf - Buffer sent as data to LCD driver.
  323. * size - Size of buffer in bytes.
  324. *
  325. **************************************************************************************/
  326. static inline int st7565_send_data_buf(FAR struct st7565_dev_s *priv,
  327. FAR const uint8_t * buf, int size)
  328. {
  329. return priv->lcd->senddata(priv->lcd, buf, size);
  330. }
  331. /**************************************************************************************
  332. * Name: st7565_backlight
  333. *
  334. * Description:
  335. * Change backlight level of display.
  336. *
  337. * priv - A reference to the driver specific structure.
  338. * level - Set backlight pwm from 0 CONFIG_LCD_MAXPOWER-1.
  339. *
  340. **************************************************************************************/
  341. static inline int st7565_backlight(FAR struct st7565_dev_s *priv, int level)
  342. {
  343. return priv->lcd->backlight(priv->lcd, level);
  344. }
  345. /**************************************************************************************
  346. * Name: st7565_putrun
  347. *
  348. * Description:
  349. * This method can be used to write a partial raster line to the LCD:
  350. *
  351. * row - Starting row to write to (range: 0 <= row < yres)
  352. * col - Starting column to write to (range: 0 <= col <= xres-npixels)
  353. * buffer - The buffer containing the run to be written to the LCD
  354. * npixels - The number of pixels to write to the LCD
  355. * (range: 0 < npixels <= xres-col)
  356. *
  357. **************************************************************************************/
  358. static int st7565_putrun(fb_coord_t row, fb_coord_t col,
  359. FAR const uint8_t * buffer, size_t npixels)
  360. {
  361. /* Because of this line of code, we will only be able to support a single
  362. * ST7565 device.
  363. */
  364. FAR struct st7565_dev_s *priv = &g_st7565dev;
  365. FAR uint8_t *fbptr;
  366. FAR uint8_t *ptr;
  367. uint8_t fbmask;
  368. uint8_t page;
  369. uint8_t usrmask;
  370. uint8_t i;
  371. int pixlen;
  372. ginfo("row: %d col: %d npixels: %d\n", row, col, npixels);
  373. DEBUGASSERT(buffer);
  374. /* Clip the run to the display */
  375. pixlen = npixels;
  376. if ((unsigned int)col + (unsigned int)pixlen > (unsigned int)ST7565_XRES)
  377. {
  378. pixlen = (int)ST7565_XRES - (int)col;
  379. }
  380. /* Verify that some portion of the run remains on the display */
  381. if (pixlen <= 0 || row > ST7565_YRES)
  382. {
  383. return OK;
  384. }
  385. /* Get the page number. The range of 64 lines is divided up into eight pages
  386. * of 8 lines each.
  387. */
  388. page = row >> 3;
  389. /* Update the shadow frame buffer memory. First determine the pixel position
  390. * in the frame buffer memory. Pixels are organized like this:
  391. *
  392. * --------+---+---+---+---+-...-+-----+
  393. * Segment | 0 | 1 | 2 | 3 | ... | 131 |
  394. * --------+---+---+---+---+-...-+-----+
  395. * Bit 0 | | X | | | | |
  396. * Bit 1 | | X | | | | |
  397. * Bit 2 | | X | | | | |
  398. * Bit 3 | | X | | | | |
  399. * Bit 4 | | X | | | | |
  400. * Bit 5 | | X | | | | |
  401. * Bit 6 | | X | | | | |
  402. * Bit 7 | | X | | | | |
  403. * --------+---+---+---+---+-...-+-----+
  404. *
  405. * So, in order to draw a white, horizontal line, at row 45. we would have
  406. * to modify all of the bytes in* page 45/8 = 5. We would have to set bit
  407. * 45%8 = 5 in every byte in the page.
  408. */
  409. fbmask = 1 << (row & 7);
  410. fbptr = &priv->fb[page * ST7565_XRES + col];
  411. ptr = fbptr;
  412. #ifdef CONFIG_LCD_PACKEDMSFIRST
  413. usrmask = MS_BIT;
  414. #else
  415. usrmask = LS_BIT;
  416. #endif
  417. for (i = 0; i < pixlen; i++)
  418. {
  419. /* Set or clear the corresponding bit */
  420. if ((*buffer & usrmask) != 0)
  421. {
  422. *ptr++ |= fbmask;
  423. }
  424. else
  425. {
  426. *ptr++ &= ~fbmask;
  427. }
  428. /* Inc/Decrement to the next source pixel */
  429. #ifdef CONFIG_LCD_PACKEDMSFIRST
  430. if (usrmask == LS_BIT)
  431. {
  432. buffer++;
  433. usrmask = MS_BIT;
  434. }
  435. else
  436. {
  437. usrmask >>= 1;
  438. }
  439. #else
  440. if (usrmask == MS_BIT)
  441. {
  442. buffer++;
  443. usrmask = LS_BIT;
  444. }
  445. else
  446. {
  447. usrmask <<= 1;
  448. }
  449. #endif
  450. }
  451. /* Select and lock the device */
  452. st7565_select(priv);
  453. /* Select command transfer */
  454. st7565_cmddata(priv, true);
  455. /* Set the starting position for the run */
  456. (void)st7565_send_one_data(priv, ST7565_SETPAGESTART + page);
  457. (void)st7565_send_one_data(priv, ST7565_SETCOLL + (col & 0x0f));
  458. (void)st7565_send_one_data(priv, ST7565_SETCOLH + (col >> 4));
  459. /* Select data transfer */
  460. st7565_cmddata(priv, false);
  461. /* Then transfer all of the data */
  462. (void)st7565_send_data_buf(priv, fbptr, pixlen);
  463. /* Unlock and de-select the device */
  464. st7565_deselect(priv);
  465. return OK;
  466. }
  467. /**************************************************************************************
  468. * Name: st7565_getrun
  469. *
  470. * TODO implement read function that possible in 8080bus
  471. *
  472. * Description:
  473. * This method can be used to read a partial raster line from the LCD:
  474. *
  475. * row - Starting row to read from (range: 0 <= row < yres)
  476. * col - Starting column to read read (range: 0 <= col <= xres-npixels)
  477. * buffer - The buffer in which to return the run read from the LCD
  478. * npixels - The number of pixels to read from the LCD
  479. * (range: 0 < npixels <= xres-col)
  480. *
  481. **************************************************************************************/
  482. static int st7565_getrun(fb_coord_t row, fb_coord_t col, FAR uint8_t * buffer,
  483. size_t npixels)
  484. {
  485. /* Because of this line of code, we will only be able to support a single
  486. * ST7565 device.
  487. */
  488. FAR struct st7565_dev_s *priv = &g_st7565dev;
  489. FAR uint8_t *fbptr;
  490. uint8_t page;
  491. uint8_t fbmask;
  492. uint8_t usrmask;
  493. uint8_t i;
  494. int pixlen;
  495. ginfo("row: %d col: %d npixels: %d\n", row, col, npixels);
  496. DEBUGASSERT(buffer);
  497. /* Clip the run to the display */
  498. pixlen = npixels;
  499. if ((unsigned int)col + (unsigned int)pixlen > (unsigned int)ST7565_XRES)
  500. {
  501. pixlen = (int)ST7565_XRES - (int)col;
  502. }
  503. /* Verify that some portion of the run is actually the display */
  504. if (pixlen <= 0 || row > ST7565_YRES)
  505. {
  506. return -EINVAL;
  507. }
  508. /* Then transfer the display data from the shadow frame buffer memory */
  509. /* Get the page number. The range of 64 lines is divided up into eight pages
  510. * of 8 lines each.
  511. */
  512. page = row >> 3;
  513. /* Update the shadow frame buffer memory. First determine the pixel position
  514. * in the frame buffer memory. Pixels are organized like this:
  515. *
  516. * --------+---+---+---+---+-...-+-----+
  517. * Segment | 0 | 1 | 2 | 3 | ... | 131 |
  518. * --------+---+---+---+---+-...-+-----+
  519. * Bit 0 | | X | | | | |
  520. * Bit 1 | | X | | | | |
  521. * Bit 2 | | X | | | | |
  522. * Bit 3 | | X | | | | |
  523. * Bit 4 | | X | | | | |
  524. * Bit 5 | | X | | | | |
  525. * Bit 6 | | X | | | | |
  526. * Bit 7 | | X | | | | |
  527. * --------+---+---+---+---+-...-+-----+
  528. *
  529. * So, in order to draw a white, horizontal line, at row 45. we would have
  530. * to modify all of the bytes in page 45/8 = 5. We would have to set bit
  531. * 45%8 = 5 in every byte in the page.
  532. */
  533. fbmask = 1 << (row & 7);
  534. fbptr = &priv->fb[page * ST7565_XRES + col];
  535. #ifdef CONFIG_LCD_PACKEDMSFIRST
  536. usrmask = MS_BIT;
  537. #else
  538. usrmask = LS_BIT;
  539. #endif
  540. *buffer = 0;
  541. for (i = 0; i < pixlen; i++)
  542. {
  543. /* Set or clear the corresponding bit */
  544. uint8_t byte = *fbptr++;
  545. if ((byte & fbmask) != 0)
  546. {
  547. *buffer |= usrmask;
  548. }
  549. /* Inc/Decrement to the next destination pixel. Hmmmm. It looks like this
  550. * logic could write past the end of the user buffer. Revisit this!
  551. */
  552. #ifdef CONFIG_LCD_PACKEDMSFIRST
  553. if (usrmask == LS_BIT)
  554. {
  555. buffer++;
  556. *buffer = 0;
  557. usrmask = MS_BIT;
  558. }
  559. else
  560. {
  561. usrmask >>= 1;
  562. }
  563. #else
  564. if (usrmask == MS_BIT)
  565. {
  566. buffer++;
  567. *buffer = 0;
  568. usrmask = LS_BIT;
  569. }
  570. else
  571. {
  572. usrmask <<= 1;
  573. }
  574. #endif
  575. }
  576. return OK;
  577. }
  578. /**************************************************************************************
  579. * Name: st7565_getvideoinfo
  580. *
  581. * Description:
  582. * Get information about the LCD video controller configuration.
  583. *
  584. **************************************************************************************/
  585. static int st7565_getvideoinfo(FAR struct lcd_dev_s *dev,
  586. FAR struct fb_videoinfo_s *vinfo)
  587. {
  588. DEBUGASSERT(dev && vinfo);
  589. ginfo("fmt: %d xres: %d yres: %d nplanes: %d\n",
  590. g_videoinfo.fmt, g_videoinfo.xres, g_videoinfo.yres,
  591. g_videoinfo.nplanes);
  592. memcpy(vinfo, &g_videoinfo, sizeof(struct fb_videoinfo_s));
  593. return OK;
  594. }
  595. /**************************************************************************************
  596. * Name: st7565_getplaneinfo
  597. *
  598. * Description:
  599. * Get information about the configuration of each LCD color plane.
  600. *
  601. **************************************************************************************/
  602. static int st7565_getplaneinfo(FAR struct lcd_dev_s *dev, unsigned int planeno,
  603. FAR struct lcd_planeinfo_s *pinfo)
  604. {
  605. DEBUGASSERT(dev && pinfo && planeno == 0);
  606. ginfo("planeno: %d bpp: %d\n", planeno, g_planeinfo.bpp);
  607. memcpy(pinfo, &g_planeinfo, sizeof(struct lcd_planeinfo_s));
  608. return OK;
  609. }
  610. /**************************************************************************************
  611. * Name: st7565_getpower
  612. *
  613. * Description:
  614. * Get the LCD panel power status (0: full off - CONFIG_LCD_MAXPOWER: full on). On
  615. * backlit LCDs, this setting may correspond to the backlight setting.
  616. *
  617. **************************************************************************************/
  618. static int st7565_getpower(struct lcd_dev_s *dev)
  619. {
  620. struct st7565_dev_s *priv = (struct st7565_dev_s *)dev;
  621. DEBUGASSERT(priv);
  622. ginfo("powered: %s\n", priv->power_level);
  623. return priv->power_level;
  624. }
  625. /**************************************************************************************
  626. * Name: st7565_setpower
  627. *
  628. * Description:
  629. * Enable/disable LCD panel power (0: full off - CONFIG_LCD_MAXPOWER: full on). On
  630. * backlight LCDs, this setting may correspond to the backlight setting.
  631. *
  632. **************************************************************************************/
  633. static int st7565_setpower(struct lcd_dev_s *dev, int power)
  634. {
  635. struct st7565_dev_s *priv = (struct st7565_dev_s *)dev;
  636. DEBUGASSERT(priv && (unsigned)power <= CONFIG_LCD_MAXPOWER);
  637. ginfo("power: %s powered: %s\n", power, priv->power_level);
  638. /* Select and lock the device */
  639. st7565_select(priv);
  640. st7565_cmddata(priv, true);
  641. if (power <= 0)
  642. {
  643. /* Turn the display off */
  644. (void)st7565_send_one_data(priv, ST7565_DISPOFF);
  645. (void)st7565_backlight(priv, 0);
  646. priv->power_level = 0;
  647. }
  648. else
  649. {
  650. (void)st7565_send_one_data(priv, ST7565_DISPON);
  651. (void)st7565_send_one_data(priv, ST7565_DISPRAM);
  652. /* Don't use value 1 of backlight to allow low power mode */
  653. if (power == 1)
  654. {
  655. (void)st7565_backlight(priv, 0);
  656. }
  657. else
  658. {
  659. (void)st7565_backlight(priv, power);
  660. }
  661. priv->power_level = 1;
  662. }
  663. st7565_cmddata(priv, false);
  664. st7565_deselect(priv);
  665. return OK;
  666. }
  667. /**************************************************************************************
  668. * Name: st7565_getcontrast
  669. *
  670. * Description:
  671. * Get the current contrast setting (0-CONFIG_LCD_MAXCONTRAST).
  672. *
  673. **************************************************************************************/
  674. static int st7565_getcontrast(struct lcd_dev_s *dev)
  675. {
  676. struct st7565_dev_s *priv = (struct st7565_dev_s *)dev;
  677. DEBUGASSERT(priv);
  678. return (int)priv->contrast;
  679. }
  680. /**************************************************************************************
  681. * Name: st7565_setcontrast
  682. *
  683. * Description:
  684. * Set LCD panel contrast (0-CONFIG_LCD_MAXCONTRAST).
  685. *
  686. **************************************************************************************/
  687. static int st7565_setcontrast(struct lcd_dev_s *dev, unsigned int contrast)
  688. {
  689. struct st7565_dev_s *priv = (struct st7565_dev_s *)dev;
  690. ginfo("contrast: %d\n", contrast);
  691. DEBUGASSERT(priv);
  692. if (contrast > 255)
  693. {
  694. return -EINVAL;
  695. }
  696. /* Select and lock the device */
  697. st7565_select(priv);
  698. /* Select command transfer */
  699. st7565_cmddata(priv, true);
  700. /* Set the contrast */
  701. (void)st7565_send_one_data(priv, ST7565_SETEVMODE);
  702. (void)st7565_send_one_data(priv, ST7565_SETEVREG(contrast));
  703. priv->contrast = contrast;
  704. /* Deselect command transfer */
  705. st7565_cmddata(priv, false);
  706. /* Unlock and de-select the device */
  707. st7565_deselect(priv);
  708. return OK;
  709. }
  710. /**************************************************************************************
  711. * Name: up_clear
  712. *
  713. * Description:
  714. * Clear the display.
  715. *
  716. **************************************************************************************/
  717. static inline void up_clear(FAR struct st7565_dev_s *priv)
  718. {
  719. int page;
  720. int i;
  721. /* Clear the framebuffer */
  722. memset(priv->fb, 0x00, ST7565_FBSIZE);
  723. /* Select and lock the device */
  724. st7565_select(priv);
  725. /* Go throw all 8 pages */
  726. for (page = 0, i = 0; i < 8; i++)
  727. {
  728. /* Select command transfer */
  729. st7565_cmddata(priv, true);
  730. /* Set the starting position for the run */
  731. (void)st7565_send_one_data(priv, ST7565_SETPAGESTART + i);
  732. (void)st7565_send_one_data(priv, ST7565_SETCOLL);
  733. (void)st7565_send_one_data(priv, ST7565_SETCOLH);
  734. /* Select data transfer */
  735. st7565_cmddata(priv, false);
  736. /* Then transfer all 96 columns of data */
  737. (void)st7565_send_data_buf(priv, &priv->fb[page * ST7565_XRES],
  738. ST7565_XRES);
  739. }
  740. /* Unlock and de-select the device */
  741. st7565_deselect(priv);
  742. }
  743. /**************************************************************************************
  744. * Public Functions
  745. **************************************************************************************/
  746. /**************************************************************************************
  747. * Name: st7565_initialize
  748. *
  749. * Description:
  750. * Initialize the ST7565 video hardware. The initial state of the
  751. * OLED is fully initialized, display memory cleared, and the OLED ready to
  752. * use, but with the power setting at 0 (full off == sleep mode).
  753. *
  754. * Input Parameters:
  755. *
  756. * lcd - A reference to the lcd low level driver instance.
  757. * devno - A value in the range of 0 throw CONFIG_ST7565_NINTERFACES-1.
  758. * This allows support for multiple OLED devices.
  759. *
  760. * Returned Value:
  761. *
  762. * On success, this function returns a reference to the LCD object
  763. * for the specified
  764. * OLED. NULL is returned on any failure.
  765. *
  766. **************************************************************************************/
  767. FAR struct lcd_dev_s *st7565_initialize(FAR struct st7565_lcd_s *lcd,
  768. unsigned int devno)
  769. {
  770. /* Configure and enable LCD */
  771. FAR struct st7565_dev_s *priv = &g_st7565dev;
  772. ginfo("Initializing\n");
  773. DEBUGASSERT(lcd && devno == 0);
  774. /* Save the reference to the SPI device */
  775. priv->lcd = lcd;
  776. /* Select and lock the device */
  777. st7565_select(priv);
  778. /* Reset device */
  779. st7565_reset(priv, true);
  780. /* it seems too long but written in NHD‐C12864KGZ DISPLAY
  781. * INITIALIZATION...
  782. */
  783. up_mdelay(150);
  784. st7565_reset(priv, false);
  785. /* it seems too long but written in NHD‐C12864KGZ DISPLAY
  786. * INITIALIZATION...
  787. */
  788. up_mdelay(150);
  789. /* Make sure that LCD backlight is off */
  790. st7565_backlight(priv, 0);
  791. /* Select command transfer */
  792. st7565_cmddata(priv, true);
  793. /* Reset by command in case of st7565_reset not implemeted */
  794. (void)st7565_send_one_data(priv, ST7565_EXIT_SOFTRST);
  795. /* Follow NHD-C12864KGZ DISPLAY INITIALIZATION... */
  796. #if defined(CONFIG_NHD_C12864KGZ)
  797. (void)st7565_send_one_data(priv, ST7565_BIAS_1_9);
  798. (void)st7565_send_one_data(priv, ST7565_REG_RES_5_5);
  799. (void)st7565_send_one_data(priv, ST7565_SETEVMODE);
  800. (void)st7565_send_one_data(priv, ST7565_SETEVREG(15));
  801. (void)st7565_send_one_data(priv, ST7565_POWERCTRL_INT);
  802. (void)st7565_send_one_data(priv, ST7565_SETSTARTLINE);
  803. #elif defined(CONFIG_ERC_12864_3)
  804. (void)st7565_send_one_data(priv, ST7565_ADCNORMAL);
  805. (void)st7565_send_one_data(priv, ST7565_SETCOMREVERSE);
  806. (void)st7565_send_one_data(priv, ST7565_BIAS_1_9);
  807. (void)st7565_send_one_data(priv, ST7565_POWERCTRL_INT);
  808. (void)st7565_send_one_data(priv, ST7565_REG_RES_5_5);
  809. (void)st7565_send_one_data(priv, ST7565_SETEVMODE);
  810. (void)st7565_send_one_data(priv, ST7565_SETEVREG(0x24));
  811. (void)st7565_send_one_data(priv, ST7565_SETSTARTLINE);
  812. #elif defined(CONFIG_AQM_1248A)
  813. (void)st7565_send_one_data(priv, ST7565_DISPOFF);
  814. (void)st7565_send_one_data(priv, ST7565_ADCNORMAL);
  815. (void)st7565_send_one_data(priv, ST7565_SETCOMREVERSE);
  816. (void)st7565_send_one_data(priv, ST7565_BIAS_1_7);
  817. (void)st7565_send_one_data(priv, ST7565_POWERCTRL_B);
  818. up_mdelay(2);
  819. (void)st7565_send_one_data(priv, ST7565_POWERCTRL_BR);
  820. up_mdelay(2);
  821. (void)st7565_send_one_data(priv, ST7565_POWERCTRL_INT);
  822. (void)st7565_send_one_data(priv, ST7565_REG_RES_4_5);
  823. (void)st7565_send_one_data(priv, ST7565_SETEVMODE);
  824. (void)st7565_send_one_data(priv, ST7565_SETEVREG(0x1c));
  825. (void)st7565_send_one_data(priv, ST7565_DISPRAM);
  826. (void)st7565_send_one_data(priv, ST7565_SETSTARTLINE);
  827. (void)st7565_send_one_data(priv, ST7565_DISPNORMAL);
  828. (void)st7565_send_one_data(priv, ST7565_DISPON);
  829. #else
  830. # error "No initialization sequence selected"
  831. #endif
  832. #if 0
  833. (void)st7565_send_one_data(priv, ST7565_DISPON);
  834. (void)st7565_send_one_data(priv, ST7565_SETCOMREVERSE);
  835. (void)st7565_send_one_data(priv, ST7565_REG_RES_RR1);
  836. (void)st7565_send_one_data(priv, ST7565_SETEV);
  837. (void)st7565_send_one_data(priv, 0x32);
  838. (void)st7565_send_one_data(priv, ST7565_POWERCTRL);
  839. (void)st7565_send_one_data(priv, ST7565_SETSTARTLINE);
  840. (void)st7565_send_one_data(priv, ST7565_SETPAGESTART);
  841. (void)st7565_send_one_data(priv, ST7565_SETCOLH);
  842. (void)st7565_send_one_data(priv, ST7565_SETCOLL);
  843. (void)st7565_send_one_data(priv, ST7565_DISPON);
  844. (void)st7565_send_one_data(priv, ST7565_DISPRAM);
  845. #endif
  846. #ifdef CONFIG_ST7565_MIRROR_X
  847. (void)st7565_send_one_data(priv, ST7565_ADCINVERSE);
  848. #else
  849. (void)st7565_send_one_data(priv, ST7565_ADCNORMAL);
  850. #endif
  851. #ifdef CONFIG_ST7565_MIRROR_Y
  852. (void)st7565_send_one_data(priv, ST7565_SETCOMREVERSE);
  853. #else
  854. (void)st7565_send_one_data(priv, ST7565_SETCOMNORMAL);
  855. #endif
  856. #ifdef CONFIG_ST7565_INVERSE_VIDEO
  857. (void)st7565_send_one_data(priv, ST7565_DISPINVERSE);
  858. #else
  859. (void)st7565_send_one_data(priv, ST7565_DISPNORMAL);
  860. #endif
  861. /* Let go of the SPI lock and de-select the device */
  862. st7565_deselect(priv);
  863. up_mdelay(10);
  864. /* Clear the framebuffer */
  865. up_clear(priv);
  866. return &priv->dev;
  867. }