pcf8574_lcd_backpack.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /****************************************************************************
  2. * drivers/lcd/pcf8574_lcd_backpack.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 <poll.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <debug.h>
  28. #include <nuttx/kmalloc.h>
  29. #include <nuttx/signal.h>
  30. #include <nuttx/ascii.h>
  31. #include <nuttx/fs/fs.h>
  32. #include <nuttx/lcd/slcd_codec.h>
  33. #include <nuttx/lcd/pcf8574_lcd_backpack.h>
  34. #ifndef CONFIG_LIB_SLCDCODEC
  35. # error please also select Library Routines, Segment LCD CODEC
  36. #endif
  37. /****************************************************************************
  38. * Pre-processor Definitions
  39. ****************************************************************************/
  40. /* The PCF8574 is a 100 KHz device */
  41. #define I2C_FREQ 100000
  42. /* timing characteristics of the LCD interface */
  43. #define DELAY_US_NYBBLE0 20
  44. #define DELAY_US_NYBBLE1 10
  45. #define DELAY_US_WRITE 40
  46. #define DELAY_US_HOMECLEAR 2000
  47. /* HD44780 commands */
  48. #define CMD_CLEAR 0x01
  49. #define CMD_HOME 0x02
  50. #define CMD_CURSOR_ON_SOLID 0x0e
  51. #define CMD_CURSOR_OFF 0x0c
  52. #define CMD_CURSOR_ON_BLINK 0x0f
  53. #define CMD_SET_CGADDR 0x40
  54. #define CMD_SET_DDADDR 0x80
  55. #define MAX_OPENCNT (255) /* Limit of uint8_t */
  56. /****************************************************************************
  57. * Private Types
  58. ****************************************************************************/
  59. struct pcf8574_lcd_dev_s
  60. {
  61. FAR struct i2c_master_s *i2c; /* I2C interface */
  62. struct pcf8574_lcd_backpack_config_s cfg; /* gpio configuration */
  63. uint8_t bl_bit; /* current backlight bit */
  64. uint8_t refs; /* Number of references */
  65. uint8_t unlinked; /* We are unlinked, so teardown
  66. * on last close */
  67. sem_t sem_excl; /* mutex */
  68. };
  69. struct lcd_instream_s
  70. {
  71. struct lib_instream_s stream;
  72. FAR const char *buffer;
  73. ssize_t nbytes;
  74. };
  75. /****************************************************************************
  76. * Private Function Prototypes
  77. ****************************************************************************/
  78. /* Character driver methods */
  79. static int pcf8574_lcd_open(FAR struct file *filep);
  80. static int pcf8574_lcd_close(FAR struct file *filep);
  81. static ssize_t pcf8574_lcd_read(FAR struct file *filep, FAR char *buffer,
  82. size_t buflen);
  83. static ssize_t pcf8574_lcd_write(FAR struct file *filep,
  84. FAR const char *buffer, size_t buflen);
  85. static off_t pcf8574_lcd_seek(FAR struct file *filep, off_t offset,
  86. int whence);
  87. static int pcf8574_lcd_ioctl(FAR struct file *filep, int cmd,
  88. unsigned long arg);
  89. static int pcf8574lcd_poll(FAR struct file *filep, FAR struct pollfd *fds,
  90. bool setup);
  91. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  92. static int pcf8574_lcd_unlink(FAR struct inode *inode);
  93. #endif
  94. /****************************************************************************
  95. * Private Data
  96. ****************************************************************************/
  97. static const struct file_operations g_pcf8574_lcd_fops =
  98. {
  99. pcf8574_lcd_open, /* open */
  100. pcf8574_lcd_close, /* close */
  101. pcf8574_lcd_read, /* read */
  102. pcf8574_lcd_write, /* write */
  103. pcf8574_lcd_seek, /* seek */
  104. pcf8574_lcd_ioctl, /* ioctl */
  105. pcf8574lcd_poll, /* poll */
  106. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  107. pcf8574_lcd_unlink /* unlink */
  108. #endif
  109. };
  110. /****************************************************************************
  111. * Private Functions
  112. ****************************************************************************/
  113. /****************************************************************************
  114. * Name: pca8574_write
  115. *
  116. * Description:
  117. * primitive I2C write operation for the PCA8574, which is the IO expander
  118. * device used on the board. The board essentially byte-bangs the
  119. * parallel interface in nybble mode much as one might with a conventional
  120. * GPIO based interface. The I2C interface simply sets the state of the
  121. * 8 IO lines to control the 4 data, 3 control, and one for backlight,
  122. * signals.
  123. *
  124. ****************************************************************************/
  125. static void pca8574_write(FAR struct pcf8574_lcd_dev_s *priv, uint8_t data)
  126. {
  127. struct i2c_config_s config;
  128. int ret;
  129. /* Set up the I2C configuration */
  130. config.frequency = I2C_FREQ;
  131. config.address = priv->cfg.addr;
  132. config.addrlen = 7;
  133. /* Write the value */
  134. ret = i2c_write(priv->i2c, &config, &data, 1);
  135. if (ret < 0)
  136. {
  137. lcdinfo("pca8574_write() failed: %d\n", ret);
  138. return;
  139. }
  140. return;
  141. }
  142. /****************************************************************************
  143. * Name: pca8574_read
  144. *
  145. * Description:
  146. * primitive I2C read operation for the PCA8574, which is the IO expander
  147. * device used on the board. The PCF8574 is 'interesting' in that it
  148. * doesn't really have a data direction register, but instead the outputs
  149. * are current-limited when high, so by setting an IO line high, you are
  150. * also making it an input. Consequently, before using this method, you'll
  151. * need to perform a pca8574_write() setting the bits you are interested in
  152. * reading to 1's, then call this method.
  153. *
  154. ****************************************************************************/
  155. static int pca8574_read(FAR struct pcf8574_lcd_dev_s *priv, uint8_t *data)
  156. {
  157. struct i2c_config_s config;
  158. int ret;
  159. /* Set up the I2C configuration */
  160. config.frequency = I2C_FREQ;
  161. config.address = priv->cfg.addr;
  162. config.addrlen = 7;
  163. /* Read the value */
  164. ret = i2c_read(priv->i2c, &config, data, 1);
  165. if (ret < 0)
  166. {
  167. lcdinfo("pca8574_read() failed: %d\n", ret);
  168. }
  169. return ret;
  170. }
  171. /****************************************************************************
  172. * Name: lcd_backlight
  173. *
  174. * Description:
  175. * turn on, or off, the LCD backlight
  176. *
  177. ****************************************************************************/
  178. static void lcd_backlight(FAR struct pcf8574_lcd_dev_s *priv, bool blon)
  179. {
  180. uint8_t data;
  181. data = ((blon && priv->cfg.bl_active_high) ||
  182. (!blon && !priv->cfg.bl_active_high)) ? (1 << priv->cfg.bl) : 0;
  183. pca8574_write(priv, data);
  184. priv->bl_bit = data;
  185. }
  186. /****************************************************************************
  187. * Name: rc2addr
  188. *
  189. * Description:
  190. * This converts a row/column pair to a screen memory address.
  191. *
  192. ****************************************************************************/
  193. static inline uint8_t rc2addr(FAR struct pcf8574_lcd_dev_s *priv,
  194. uint8_t row, uint8_t col)
  195. {
  196. if (row < 2)
  197. {
  198. /* 1 and 2 line displays are simple; line0 @ 0x00, line1 @ 0x40 */
  199. return row * 0x40 + col;
  200. }
  201. else
  202. {
  203. /* 4 line displays are interesting; third line really is a continuation
  204. * of first line, and fourth line is a continuation of second.
  205. */
  206. return (row - 2) * 0x40 + (col + priv->cfg.cols);
  207. }
  208. }
  209. /****************************************************************************
  210. * Name: addr2rc
  211. *
  212. * Description:
  213. * This converts a screen memory address to a row/column pair.
  214. *
  215. ****************************************************************************/
  216. static inline void addr2rc(FAR struct pcf8574_lcd_dev_s *priv, uint8_t addr,
  217. FAR uint8_t *row, FAR uint8_t *col)
  218. {
  219. *row = addr / 0x40;
  220. *col = addr % 0x40;
  221. if (*col >= priv->cfg.cols)
  222. {
  223. /* 4 line displays have third and fourth lines really as continuation
  224. * of the first and second.
  225. */
  226. *row += 2;
  227. *col -= priv->cfg.cols;
  228. }
  229. }
  230. /****************************************************************************
  231. * Name: prepare_nybble
  232. *
  233. * Description:
  234. * This is a bit tedious, but scramble the bits of the nybble into position
  235. * as per this board's particular wiring. Most boards are either on the
  236. * top four bits, or bottom four, so a shift would do typically in those
  237. * cases, but this gives us ultimate flexibility.
  238. *
  239. ****************************************************************************/
  240. uint8_t prepare_nybble(FAR struct pcf8574_lcd_dev_s *priv, uint8_t nybble)
  241. {
  242. uint8_t lcddata = 0;
  243. if (nybble & 0x08)
  244. {
  245. lcddata |= (1 << priv->cfg.d7);
  246. }
  247. if (nybble & 0x04)
  248. {
  249. lcddata |= (1 << priv->cfg.d6);
  250. }
  251. if (nybble & 0x02)
  252. {
  253. lcddata |= (1 << priv->cfg.d5);
  254. }
  255. if (nybble & 0x01)
  256. {
  257. lcddata |= (1 << priv->cfg.d4);
  258. }
  259. return lcddata;
  260. }
  261. /****************************************************************************
  262. * Name: unprepare_nybble
  263. *
  264. * Description:
  265. * This is the opposite of prepare_nybble(), and is used to unscramble bits
  266. * when reading data from the display, as per board wiring.
  267. *
  268. ****************************************************************************/
  269. uint8_t unprepare_nybble(FAR struct pcf8574_lcd_dev_s *priv, uint8_t lcddata)
  270. {
  271. uint8_t data = 0;
  272. if (lcddata & (1 << priv->cfg.d7))
  273. {
  274. data |= 0x08;
  275. }
  276. if (lcddata & (1 << priv->cfg.d6))
  277. {
  278. data |= 0x04;
  279. }
  280. if (lcddata & (1 << priv->cfg.d5))
  281. {
  282. data |= 0x02;
  283. }
  284. if (lcddata & (1 << priv->cfg.d4))
  285. {
  286. data |= 0x01;
  287. }
  288. return data;
  289. }
  290. /****************************************************************************
  291. * Name: latch_nybble
  292. *
  293. * Description:
  294. * Latch a nybble on the LCD bus. This is done for each of two halves of a
  295. * write operation in 4-bit mode. The 'rs' param is false for command
  296. * transfers, and true for data transfers.
  297. *
  298. ****************************************************************************/
  299. static void latch_nybble(FAR struct pcf8574_lcd_dev_s *priv, uint8_t nybble,
  300. bool rs)
  301. {
  302. uint8_t lcddata;
  303. uint8_t en_bit;
  304. uint8_t rs_bit;
  305. en_bit = 1 << priv->cfg.en;
  306. rs_bit = rs ? (1 << priv->cfg.rs) : 0;
  307. /* Put the nybble, preserving backlight, reset R/~W and maybe RS */
  308. lcddata = prepare_nybble(priv, nybble) | priv->bl_bit | rs_bit;
  309. pca8574_write(priv, lcddata);
  310. /* Now set EN */
  311. lcddata |= en_bit;
  312. pca8574_write(priv, lcddata);
  313. up_udelay(DELAY_US_NYBBLE0); /* setup */
  314. /* Latch on EN falling edge */
  315. lcddata &= ~en_bit;
  316. pca8574_write(priv, lcddata);
  317. up_udelay(DELAY_US_NYBBLE1); /* hold */
  318. }
  319. /****************************************************************************
  320. * Name: load_nybble
  321. *
  322. * Description:
  323. * Load a nybble from the LCD bus. This is done for each of two halves of a
  324. * read operation in 4-bit mode. The 'rs' param is false for command
  325. * transfers (the only one is to read status and the address register), and
  326. * true for data transfers.
  327. *
  328. ****************************************************************************/
  329. static uint8_t load_nybble(FAR struct pcf8574_lcd_dev_s *priv, bool rs)
  330. {
  331. uint8_t lcddata;
  332. uint8_t en_bit;
  333. uint8_t rs_bit;
  334. uint8_t rw_bit;
  335. uint8_t data;
  336. en_bit = 1 << priv->cfg.en;
  337. rs_bit = rs ? (1 << priv->cfg.rs) : 0;
  338. rw_bit = 1 << priv->cfg.rw;
  339. /* Put highs on the data lines, preserve, set R/~W and maybe RS */
  340. lcddata = prepare_nybble(priv, 0x0f) | priv->bl_bit | rw_bit | rs_bit;
  341. pca8574_write(priv, lcddata);
  342. /* Now set EN */
  343. lcddata |= en_bit;
  344. pca8574_write(priv, lcddata);
  345. up_udelay(DELAY_US_NYBBLE0); /* setup */
  346. /* Now read the data */
  347. pca8574_read(priv, &data);
  348. data = unprepare_nybble(priv, data);
  349. /* Transaction completed on EN falling edge */
  350. lcddata &= ~en_bit;
  351. pca8574_write(priv, lcddata);
  352. up_udelay(DELAY_US_NYBBLE1); /* hold */
  353. return data;
  354. }
  355. /****************************************************************************
  356. * Name: lcd_putcmd
  357. *
  358. * Description:
  359. * Write a command to the LCD. Most of the time this is done in nybble
  360. * mode in two phases, but in special cases (like initialization) we do not
  361. * do two phases.
  362. *
  363. ****************************************************************************/
  364. static void lcd_putcmd(FAR struct pcf8574_lcd_dev_s *priv, uint8_t data)
  365. {
  366. latch_nybble(priv, data >> 4, false);
  367. latch_nybble(priv, data, false);
  368. up_udelay(DELAY_US_WRITE);
  369. }
  370. /****************************************************************************
  371. * Name: lcd_putdata
  372. *
  373. * Description:
  374. * Write a byte to the LCD. This is used both for screen data and for
  375. * character generator data, depending on a previous command that selected
  376. * which ever is the destination.
  377. *
  378. ****************************************************************************/
  379. static inline void lcd_putdata(FAR struct pcf8574_lcd_dev_s *priv,
  380. uint8_t data)
  381. {
  382. latch_nybble(priv, data >> 4, true);
  383. latch_nybble(priv, data, true);
  384. up_udelay(DELAY_US_WRITE);
  385. }
  386. /****************************************************************************
  387. * Name: lcd_getdata
  388. *
  389. * Description:
  390. * Read a data byte from the LCD.
  391. *
  392. ****************************************************************************/
  393. static inline uint8_t lcd_getdata(FAR struct pcf8574_lcd_dev_s *priv)
  394. {
  395. uint8_t data;
  396. data = (load_nybble(priv, true) << 4) | load_nybble(priv, true);
  397. return data;
  398. }
  399. /****************************************************************************
  400. * Name: lcd_getcmd
  401. *
  402. * Description:
  403. * Read a command byte from the LCD. There really is only one such read:
  404. * get 'busy' status, and current address value.
  405. *
  406. ****************************************************************************/
  407. static inline uint8_t lcd_getcmd(FAR struct pcf8574_lcd_dev_s *priv)
  408. {
  409. uint8_t data;
  410. data = (load_nybble(priv, false) << 4) | load_nybble(priv, false);
  411. return data;
  412. }
  413. /****************************************************************************
  414. * Name: lcd_read_busy_addr
  415. *
  416. * Description:
  417. * Read the busy flag, and, optionally, the current value of the address
  418. * register (data or character generator dependent on a previous command).
  419. *
  420. ****************************************************************************/
  421. static bool lcd_read_busy_addr(FAR struct pcf8574_lcd_dev_s *priv,
  422. FAR uint8_t *addr)
  423. {
  424. uint8_t data = lcd_getcmd(priv);
  425. if (NULL != addr)
  426. {
  427. *addr = data & 0x7f;
  428. }
  429. return (data & 0x80) ? true : false;
  430. }
  431. /****************************************************************************
  432. * Name: lcd_init
  433. *
  434. * Description:
  435. * perform the initialization sequence to get the LCD into a known state.
  436. *
  437. ****************************************************************************/
  438. static void lcd_init(FAR struct pcf8574_lcd_dev_s *priv)
  439. {
  440. /* Wait for more than 15 ms after Vcc for the LCD to stabilize */
  441. nxsig_usleep(50000);
  442. /* Perform the init sequence. This sequence of commands is constructed so
  443. * that it will get the device into nybble mode irrespective of what state
  444. * the device is currently in (could be 8 bit, 4 bit nyb 0, 4 bit nyb 1).
  445. * By sending the 'set 8-bit mode' three times, we will definitely end up
  446. * in 8 bit mode, and then we can reliably transition to 4 bit mode for
  447. * the remainder of operations.
  448. */
  449. /* Send Command 0x30, set 8-bit mode, and wait > 4.1 ms */
  450. latch_nybble(priv, 0x30 >> 4, false);
  451. nxsig_usleep(5000);
  452. /* Send Command 0x30, set 8-bit mode, and wait > 100 us */
  453. latch_nybble(priv, 0x30 >> 4, false);
  454. nxsig_usleep(5000);
  455. /* Send Command 0x30, set 8-bit mode */
  456. latch_nybble(priv, 0x30 >> 4, false);
  457. nxsig_usleep(200);
  458. /* now Function set: Set interface to be 4 bits long (only 1 cycle write
  459. * for the first time).
  460. */
  461. latch_nybble(priv, 0x20 >> 4, false);
  462. nxsig_usleep(5000);
  463. /* Function set: DL=0;Interface is 4 bits, N=1 (2 Lines), F=0 (5x8 dots
  464. * font)
  465. */
  466. lcd_putcmd(priv, 0x28);
  467. /* Display Off: D=0 (Display off), C=0 (Cursor Off), B=0 (Blinking Off) */
  468. lcd_putcmd(priv, 0x08);
  469. /* Display Clear */
  470. lcd_putcmd(priv, CMD_CLEAR);
  471. up_udelay(DELAY_US_HOMECLEAR); /* clear needs extra time */
  472. /* Entry Mode Set: I/D=1 (Increment), S=0 (No shift) */
  473. lcd_putcmd(priv, 0x06);
  474. /* Display On, Cursor Off */
  475. lcd_putcmd(priv, 0x0c);
  476. }
  477. /****************************************************************************
  478. * Name: lcd_create_char
  479. *
  480. * Description:
  481. * This creates a custom character pattern. There can be 8 5x8 patterns.
  482. * The bitmap proceeds top to bottom, msb-lsb, and is right justified (i.e.
  483. * only bits 4-0 are used). By convention, you are meant to always leave
  484. * the last line (byte) zero so that the cursor can use this line, but this
  485. * is not strictly required.
  486. *
  487. * Input Parameters:
  488. * priv - device instance
  489. * idxchar - which character is being imaged; 0 - 7
  490. * chardata - the character image bitmap; must be 8 bytes always
  491. *
  492. ****************************************************************************/
  493. static void lcd_create_char(FAR struct pcf8574_lcd_dev_s *priv,
  494. uint8_t idxchar, FAR const uint8_t *chardata)
  495. {
  496. int nidx;
  497. uint8_t addr;
  498. lcd_read_busy_addr(priv, &addr);
  499. lcd_putcmd(priv, CMD_SET_CGADDR | (idxchar << 3)); /* set CGRAM address */
  500. for (nidx = 0; nidx < 8; ++nidx)
  501. {
  502. lcd_putdata(priv, chardata[nidx]);
  503. }
  504. lcd_putcmd(priv, CMD_SET_DDADDR | addr); /* restore DDRAM address */
  505. }
  506. /****************************************************************************
  507. * Name: lcd_set_curpos
  508. *
  509. * Description:
  510. * This sets the cursor position based on row, column addressing.
  511. *
  512. * Input Parameters:
  513. * priv - device instance
  514. * row - row position
  515. * col - column position
  516. *
  517. ****************************************************************************/
  518. static void lcd_set_curpos(FAR struct pcf8574_lcd_dev_s *priv,
  519. uint8_t row, uint8_t col)
  520. {
  521. uint8_t addr;
  522. addr = rc2addr(priv, row, col);
  523. lcd_putcmd(priv, CMD_SET_DDADDR | addr); /* set DDRAM address */
  524. }
  525. /****************************************************************************
  526. * Name: lcd_get_curpos
  527. *
  528. * Description:
  529. * This gets the cursor position based on row, column addressing.
  530. *
  531. * Input Parameters:
  532. * priv - device instance
  533. * row - row position
  534. * col - column position
  535. *
  536. ****************************************************************************/
  537. static void lcd_get_curpos(FAR struct pcf8574_lcd_dev_s *priv,
  538. FAR uint8_t *row, FAR uint8_t *col)
  539. {
  540. uint8_t addr;
  541. lcd_read_busy_addr(priv, &addr);
  542. addr2rc(priv, addr, row, col);
  543. }
  544. /****************************************************************************
  545. * Name: lcd_scroll_up
  546. *
  547. * Description:
  548. * Scroll the display up, and clear the new (last) line.
  549. *
  550. ****************************************************************************/
  551. static void lcd_scroll_up(FAR struct pcf8574_lcd_dev_s *priv)
  552. {
  553. FAR uint8_t *data;
  554. int nrow;
  555. int ncol;
  556. data = (FAR uint8_t *)kmm_malloc(priv->cfg.cols);
  557. if (NULL == data)
  558. {
  559. lcdinfo("Failed to allocate buffer in lcd_scroll_up()\n");
  560. return;
  561. }
  562. for (nrow = 1; nrow < priv->cfg.rows; ++nrow)
  563. {
  564. lcd_set_curpos(priv, nrow, 0);
  565. for (ncol = 0; ncol < priv->cfg.cols; ++ncol)
  566. {
  567. data[ncol] = lcd_getdata(priv);
  568. }
  569. lcd_set_curpos(priv, nrow - 1, 0);
  570. for (ncol = 0; ncol < priv->cfg.cols; ++ncol)
  571. {
  572. lcd_putdata(priv, data[ncol]);
  573. }
  574. }
  575. lcd_set_curpos(priv, priv->cfg.rows - 1, 0);
  576. for (ncol = 0; ncol < priv->cfg.cols; ++ncol)
  577. {
  578. lcd_putdata(priv, ' ');
  579. }
  580. lcd_set_curpos(priv, priv->cfg.rows - 1, 0);
  581. kmm_free(data);
  582. return;
  583. }
  584. /****************************************************************************
  585. * Name: lcd_codec_action
  586. *
  587. * Description:
  588. * Perform an 'action' as per the Segment LCD codec.
  589. *
  590. * Input Parameters:
  591. * priv - device instance
  592. * code - SLCD code action code
  593. * count - count param for those actions that take it
  594. *
  595. ****************************************************************************/
  596. static void lcd_codec_action(FAR struct pcf8574_lcd_dev_s *priv,
  597. enum slcdcode_e code, uint8_t count)
  598. {
  599. switch (code)
  600. {
  601. /* Erasure */
  602. case SLCDCODE_BACKDEL: /* Backspace (backward delete) N characters */
  603. {
  604. if (count <= 0) /* silly case */
  605. {
  606. break;
  607. }
  608. else
  609. {
  610. uint8_t row;
  611. uint8_t col;
  612. lcd_get_curpos(priv, &row, &col);
  613. if (count > col) /* saturate to preceding columns available */
  614. {
  615. count = col;
  616. }
  617. lcd_set_curpos(priv, row, col - count);
  618. }
  619. /* ... and conscientiously fall through to next case ... */
  620. }
  621. case SLCDCODE_FWDDEL: /* Delete (forward delete) N characters, moving text */
  622. {
  623. if (count <= 0) /* silly case */
  624. {
  625. break;
  626. }
  627. else
  628. {
  629. uint8_t row;
  630. uint8_t col;
  631. uint8_t start;
  632. uint8_t end;
  633. uint8_t nidx;
  634. uint8_t data;
  635. lcd_get_curpos(priv, &row, &col);
  636. start = col + count;
  637. if (start >= priv->cfg.cols) /* silly case of nothing left */
  638. {
  639. break;
  640. }
  641. end = start + count;
  642. if (end > priv->cfg.cols) /* saturate */
  643. {
  644. end = priv->cfg.cols;
  645. }
  646. for (nidx = col; nidx < end; ++start, ++nidx) /* much like memmove */
  647. {
  648. lcd_set_curpos(priv, row, start);
  649. data = lcd_getdata(priv);
  650. lcd_set_curpos(priv, row, nidx);
  651. lcd_putdata(priv, data);
  652. }
  653. for (; nidx < priv->cfg.cols; ++nidx) /* much like memset */
  654. {
  655. lcd_putdata(priv, ' ');
  656. }
  657. lcd_set_curpos(priv, row, col);
  658. }
  659. }
  660. break;
  661. case SLCDCODE_ERASE: /* Erase N characters from the cursor position */
  662. if (count > 0)
  663. {
  664. uint8_t row;
  665. uint8_t col;
  666. uint8_t end;
  667. uint8_t nidx;
  668. lcd_get_curpos(priv, &row, &col);
  669. end = col + count;
  670. if (end > priv->cfg.cols)
  671. {
  672. end = priv->cfg.cols;
  673. }
  674. for (nidx = col; nidx < end; ++nidx)
  675. {
  676. lcd_putdata(priv, ' ');
  677. }
  678. lcd_set_curpos(priv, row, col);
  679. }
  680. break;
  681. case SLCDCODE_CLEAR: /* Home the cursor and erase the entire display */
  682. {
  683. lcd_putcmd(priv, CMD_CLEAR);
  684. up_udelay(DELAY_US_HOMECLEAR); /* clear needs extra time */
  685. }
  686. break;
  687. case SLCDCODE_ERASEEOL: /* Erase from the cursor position to the end of
  688. * line */
  689. {
  690. uint8_t row;
  691. uint8_t col;
  692. uint8_t nidx;
  693. lcd_get_curpos(priv, &row, &col);
  694. for (nidx = col; nidx < priv->cfg.cols; ++nidx)
  695. {
  696. lcd_putdata(priv, ' ');
  697. }
  698. lcd_set_curpos(priv, row, col);
  699. }
  700. break;
  701. /* Cursor movement */
  702. case SLCDCODE_LEFT: /* Cursor left by N characters */
  703. {
  704. uint8_t row;
  705. uint8_t col;
  706. lcd_get_curpos(priv, &row, &col);
  707. if (count > col)
  708. {
  709. col = 0;
  710. }
  711. else
  712. {
  713. col -= count;
  714. }
  715. lcd_set_curpos(priv, row, col);
  716. }
  717. break;
  718. case SLCDCODE_RIGHT: /* Cursor right by N characters */
  719. {
  720. uint8_t row;
  721. uint8_t col;
  722. lcd_get_curpos(priv, &row, &col);
  723. col += count;
  724. if (col >= priv->cfg.cols)
  725. {
  726. col = priv->cfg.cols - 1;
  727. }
  728. lcd_set_curpos(priv, row, col);
  729. }
  730. break;
  731. case SLCDCODE_UP: /* Cursor up by N lines */
  732. {
  733. uint8_t row;
  734. uint8_t col;
  735. lcd_get_curpos(priv, &row, &col);
  736. if (count > row)
  737. {
  738. row = 0;
  739. }
  740. else
  741. {
  742. row -= count;
  743. }
  744. lcd_set_curpos(priv, row, col);
  745. }
  746. break;
  747. case SLCDCODE_DOWN: /* Cursor down by N lines */
  748. {
  749. uint8_t row;
  750. uint8_t col;
  751. lcd_get_curpos(priv, &row, &col);
  752. row += count;
  753. if (row >= priv->cfg.rows)
  754. {
  755. row = priv->cfg.rows - 1;
  756. }
  757. lcd_set_curpos(priv, row, col);
  758. }
  759. break;
  760. case SLCDCODE_HOME: /* Cursor home */
  761. {
  762. uint8_t row;
  763. uint8_t col;
  764. lcd_get_curpos(priv, &row, &col);
  765. lcd_set_curpos(priv, row, 0);
  766. }
  767. break;
  768. case SLCDCODE_END: /* Cursor end */
  769. {
  770. uint8_t row;
  771. uint8_t col;
  772. lcd_get_curpos(priv, &row, &col);
  773. lcd_set_curpos(priv, row, priv->cfg.cols - 1);
  774. }
  775. break;
  776. case SLCDCODE_PAGEUP: /* Cursor up by N pages */
  777. case SLCDCODE_PAGEDOWN: /* Cursor down by N pages */
  778. break; /* Not supportable on this SLCD */
  779. /* Blinking */
  780. case SLCDCODE_BLINKSTART: /* Start blinking with current cursor position */
  781. lcd_putcmd(priv, CMD_CURSOR_ON_BLINK);
  782. break;
  783. case SLCDCODE_BLINKEND: /* End blinking after the current cursor
  784. * position */
  785. case SLCDCODE_BLINKOFF: /* Turn blinking off */
  786. lcd_putcmd(priv, CMD_CURSOR_OFF);
  787. break; /* Not implemented */
  788. /* These are actually unreportable errors */
  789. default:
  790. case SLCDCODE_NORMAL: /* Not a special keycode */
  791. break;
  792. }
  793. }
  794. /****************************************************************************
  795. * Name: lcd_getstream
  796. *
  797. * Description:
  798. * Get one character from the LCD codec stream.
  799. *
  800. ****************************************************************************/
  801. static int lcd_getstream(FAR struct lib_instream_s *instream)
  802. {
  803. FAR struct lcd_instream_s *lcdstream =
  804. (FAR struct lcd_instream_s *)instream;
  805. if (lcdstream->nbytes > 0)
  806. {
  807. lcdstream->nbytes--;
  808. lcdstream->stream.nget++;
  809. return (int)*lcdstream->buffer++;
  810. }
  811. return EOF;
  812. }
  813. /****************************************************************************
  814. * Name: lcd_fpos_to_curpos
  815. *
  816. * Description:
  817. * Convert a file logical offset to a screen cursor pos (row,col). This
  818. * discounts 'synthesized' line feeds at the end of screen lines.
  819. *
  820. ****************************************************************************/
  821. static void lcd_fpos_to_curpos(FAR struct pcf8574_lcd_dev_s *priv,
  822. off_t fpos, FAR uint8_t *row,
  823. FAR uint8_t *col, FAR bool *onlf)
  824. {
  825. int virtcols;
  826. virtcols = (priv->cfg.cols + 1);
  827. /* Determine if this is a 'virtual' position (on the synthetic LF) */
  828. *onlf = (priv->cfg.cols == fpos % virtcols);
  829. /* Adjust off any preceding synthetic LF's to get linear position */
  830. fpos -= fpos / virtcols;
  831. /* Compute row/col from linear position */
  832. *row = fpos / priv->cfg.cols;
  833. *col = fpos % priv->cfg.cols;
  834. }
  835. /****************************************************************************
  836. * Name: lcd_curpos_to_fpos
  837. *
  838. * Description:
  839. * Convert a screen cursor pos (row,col) to a file logical offset. This
  840. * includes 'synthesized' line feeds at the end of screen lines.
  841. *
  842. ****************************************************************************/
  843. static void lcd_curpos_to_fpos(FAR struct pcf8574_lcd_dev_s *priv,
  844. uint8_t row, uint8_t col, FAR off_t *fpos)
  845. {
  846. /* the logical file position is the linear position plus any synthetic LF */
  847. *fpos = (row * priv->cfg.cols) + col + row;
  848. }
  849. /****************************************************************************
  850. * Name: pcf8574_lcd_open
  851. *
  852. * Description:
  853. * requisite device 'open' method; we don't do anything special
  854. *
  855. ****************************************************************************/
  856. static int pcf8574_lcd_open(FAR struct file *filep)
  857. {
  858. FAR struct inode *inode = filep->f_inode;
  859. FAR struct pcf8574_lcd_dev_s *priv =
  860. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  861. /* Increment the reference count */
  862. nxsem_wait(&priv->sem_excl);
  863. if (priv->refs == MAX_OPENCNT)
  864. {
  865. return -EMFILE;
  866. }
  867. else
  868. {
  869. priv->refs++;
  870. }
  871. nxsem_post(&priv->sem_excl);
  872. return OK;
  873. }
  874. /****************************************************************************
  875. * Name: pcf8574_lcd_close
  876. *
  877. * Description:
  878. * requisite device 'close' method; we don't do anything special
  879. *
  880. ****************************************************************************/
  881. static int pcf8574_lcd_close(FAR struct file *filep)
  882. {
  883. FAR struct inode *inode = filep->f_inode;
  884. FAR struct pcf8574_lcd_dev_s *priv =
  885. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  886. int ret;
  887. /* Decrement the reference count */
  888. nxsem_wait(&priv->sem_excl);
  889. if (priv->refs == 0)
  890. {
  891. ret = -EIO;
  892. }
  893. else
  894. {
  895. priv->refs--;
  896. /* If we had previously unlinked, but there were open references at
  897. * the time, we need to do the final teardown now.
  898. */
  899. if (priv->refs == 0 && priv->unlinked)
  900. {
  901. /* We have no real teardown at present */
  902. }
  903. ret = OK;
  904. }
  905. nxsem_post(&priv->sem_excl);
  906. return ret;
  907. }
  908. /****************************************************************************
  909. * Name: pcf8574_lcd_read
  910. *
  911. * Description:
  912. * This simply reads as much of the display memory as possible. This is
  913. * generally not very interesting, but we do it in a way that allows us to
  914. * 'cat' the LCD contents via the shell.
  915. *
  916. ****************************************************************************/
  917. static ssize_t pcf8574_lcd_read(FAR struct file *filep, FAR char *buffer,
  918. size_t buflen)
  919. {
  920. FAR struct inode *inode = filep->f_inode;
  921. FAR struct pcf8574_lcd_dev_s *priv =
  922. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  923. int nidx;
  924. uint8_t addr;
  925. uint8_t row;
  926. uint8_t col;
  927. bool onlf;
  928. nxsem_wait(&priv->sem_excl);
  929. /* Get current cursor position so we can restore it */
  930. lcd_read_busy_addr(priv, &addr);
  931. /* Convert file position to row/col address and position DDADDR there */
  932. lcd_fpos_to_curpos(priv, filep->f_pos, &row, &col, &onlf);
  933. lcd_set_curpos(priv, row, col);
  934. /* Read as much of the display as possible */
  935. nidx = 0;
  936. while (nidx < buflen && row < priv->cfg.rows)
  937. {
  938. /* Synthesize end-of-line LF and advance to start of next row */
  939. if (onlf)
  940. {
  941. /* Synthesize LF for all but last row */
  942. if (row < priv->cfg.rows - 1)
  943. {
  944. buffer[nidx] = '\x0a';
  945. onlf = false;
  946. ++filep->f_pos;
  947. ++nidx;
  948. }
  949. ++row;
  950. col = 0;
  951. continue;
  952. }
  953. /* If we are at start of line we will need to update DDRAM address */
  954. if (0 == col)
  955. {
  956. lcd_set_curpos(priv, row, 0);
  957. }
  958. buffer[nidx] = lcd_getdata(priv);
  959. ++filep->f_pos;
  960. ++nidx;
  961. ++col;
  962. /* If we are now at the end of a line, we setup for the synthetic LF */
  963. if (priv->cfg.cols == col)
  964. {
  965. onlf = true;
  966. }
  967. }
  968. lcd_putcmd(priv, CMD_SET_DDADDR | addr); /* Restore DDRAM address */
  969. nxsem_post(&priv->sem_excl);
  970. return nidx;
  971. }
  972. /****************************************************************************
  973. * Name: pcf8574_lcd_write
  974. *
  975. * Description:
  976. * Output a sequence of characters to the device.
  977. *
  978. ****************************************************************************/
  979. static ssize_t pcf8574_lcd_write(FAR struct file *filep,
  980. FAR const char *buffer, size_t buflen)
  981. {
  982. FAR struct inode *inode = filep->f_inode;
  983. FAR struct pcf8574_lcd_dev_s *priv =
  984. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  985. struct lcd_instream_s instream;
  986. uint8_t row;
  987. uint8_t col;
  988. struct slcdstate_s state;
  989. enum slcdret_e result;
  990. uint8_t ch;
  991. uint8_t count;
  992. nxsem_wait(&priv->sem_excl);
  993. /* Initialize the stream for use with the SLCD CODEC */
  994. instream.stream.get = lcd_getstream;
  995. instream.stream.nget = 0;
  996. instream.buffer = buffer;
  997. instream.nbytes = buflen;
  998. /* Get the current cursor position now; we'll keep track of it as we go */
  999. lcd_get_curpos(priv, &row, &col);
  1000. /* Now decode and process every byte in the input buffer */
  1001. memset(&state, 0, sizeof(struct slcdstate_s));
  1002. while ((result =
  1003. slcd_decode(&instream.stream, &state, &ch, &count)) != SLCDRET_EOF)
  1004. {
  1005. if (result == SLCDRET_CHAR) /* A normal character was returned */
  1006. {
  1007. /* Check for ASCII control characters */
  1008. if (ch == ASCII_TAB)
  1009. {
  1010. lcd_putcmd(priv, CMD_CURSOR_ON_BLINK);
  1011. }
  1012. else if (ch == ASCII_VT)
  1013. {
  1014. /* Turn the backlight on */
  1015. lcd_backlight(priv, true);
  1016. }
  1017. else if (ch == ASCII_FF)
  1018. {
  1019. /* Turn the backlight off */
  1020. lcd_backlight(priv, false);
  1021. }
  1022. else if (ch == ASCII_CR)
  1023. {
  1024. /* Perform a Home */
  1025. lcd_putcmd(priv, CMD_HOME);
  1026. up_udelay(DELAY_US_HOMECLEAR); /* home needs extra time */
  1027. row = 0;
  1028. col = 0;
  1029. }
  1030. else if (ch == ASCII_SO)
  1031. {
  1032. lcd_putcmd(priv, CMD_CURSOR_OFF);
  1033. }
  1034. else if (ch == ASCII_SI)
  1035. {
  1036. /* Perform the re-initialize */
  1037. lcd_init(priv);
  1038. row = 0;
  1039. col = 0;
  1040. }
  1041. else if (ch == ASCII_LF)
  1042. {
  1043. /* unixian line term; go to start of next line */
  1044. row += 1;
  1045. if (row >= priv->cfg.rows)
  1046. {
  1047. lcd_scroll_up(priv);
  1048. row = priv->cfg.rows - 1;
  1049. }
  1050. col = 0;
  1051. lcd_set_curpos(priv, row, col);
  1052. }
  1053. else if (ch == ASCII_BS)
  1054. {
  1055. /* Perform the backward deletion */
  1056. lcd_codec_action(priv, SLCDCODE_BACKDEL, 1);
  1057. lcd_get_curpos(priv, &row, &col);
  1058. }
  1059. else if (ch == ASCII_DEL)
  1060. {
  1061. /* Perform the forward deletion */
  1062. lcd_codec_action(priv, SLCDCODE_FWDDEL, 1);
  1063. lcd_get_curpos(priv, &row, &col);
  1064. }
  1065. else
  1066. {
  1067. /* All others are fair game. See if we need to wrap line. */
  1068. if (col >= priv->cfg.cols)
  1069. {
  1070. row += 1;
  1071. if (row >= priv->cfg.rows)
  1072. {
  1073. lcd_scroll_up(priv);
  1074. row = priv->cfg.rows - 1;
  1075. }
  1076. col = 0;
  1077. lcd_set_curpos(priv, row, col);
  1078. }
  1079. lcd_putdata(priv, ch);
  1080. ++col;
  1081. }
  1082. }
  1083. /* A special SLCD action was returned */
  1084. else /* (result == SLCDRET_SPEC) */
  1085. {
  1086. lcd_codec_action(priv, (enum slcdcode_e)ch, count);
  1087. /* we can't know what happened, so it's easier just to re-inquire
  1088. * as to where we are.
  1089. */
  1090. lcd_get_curpos(priv, &row, &col);
  1091. }
  1092. }
  1093. /* Wherever we wound up, update our logical file pos to reflect it */
  1094. lcd_curpos_to_fpos(priv, row, col, &filep->f_pos);
  1095. nxsem_post(&priv->sem_excl);
  1096. return buflen;
  1097. }
  1098. /****************************************************************************
  1099. * Name: pcf8574_lcd_seek
  1100. *
  1101. * Description:
  1102. * Seek the logical file pointer to the specified position. This is
  1103. * probably not very interesting except possibly for (SEEK_SET, 0) to
  1104. * rewind the pointer for a subsequent read().
  1105. * The file pointer is logical, and includes synthesized LF chars at the
  1106. * end of the display lines.
  1107. *
  1108. ****************************************************************************/
  1109. static off_t pcf8574_lcd_seek(FAR struct file *filep, off_t offset,
  1110. int whence)
  1111. {
  1112. FAR struct inode *inode = filep->f_inode;
  1113. FAR struct pcf8574_lcd_dev_s *priv =
  1114. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  1115. off_t pos;
  1116. int maxpos;
  1117. nxsem_wait(&priv->sem_excl);
  1118. maxpos = priv->cfg.rows * priv->cfg.cols + (priv->cfg.rows - 1);
  1119. pos = filep->f_pos;
  1120. switch (whence)
  1121. {
  1122. case SEEK_CUR:
  1123. pos += offset;
  1124. if (pos > maxpos)
  1125. {
  1126. pos = maxpos;
  1127. }
  1128. else if (pos < 0)
  1129. {
  1130. pos = 0;
  1131. }
  1132. filep->f_pos = pos;
  1133. break;
  1134. case SEEK_SET:
  1135. pos = offset;
  1136. if (pos > maxpos)
  1137. {
  1138. pos = maxpos;
  1139. }
  1140. else if (pos < 0)
  1141. {
  1142. pos = 0;
  1143. }
  1144. filep->f_pos = pos;
  1145. break;
  1146. case SEEK_END:
  1147. pos = maxpos + offset;
  1148. if (pos > maxpos)
  1149. {
  1150. pos = maxpos;
  1151. }
  1152. else if (pos < 0)
  1153. {
  1154. pos = 0;
  1155. }
  1156. filep->f_pos = pos;
  1157. break;
  1158. default:
  1159. /* Return EINVAL if the whence argument is invalid */
  1160. pos = (off_t) - EINVAL;
  1161. break;
  1162. }
  1163. nxsem_post(&priv->sem_excl);
  1164. return pos;
  1165. }
  1166. /****************************************************************************
  1167. * Name: pcf8574_lcd_ioctl
  1168. *
  1169. * Description:
  1170. * Perform device operations that are outside the standard I/O model.
  1171. *
  1172. ****************************************************************************/
  1173. static int pcf8574_lcd_ioctl(FAR struct file *filep, int cmd,
  1174. unsigned long arg)
  1175. {
  1176. switch (cmd)
  1177. {
  1178. case SLCDIOC_GETATTRIBUTES: /* SLCDIOC_GETATTRIBUTES: Get the
  1179. * attributes of the SLCD */
  1180. {
  1181. FAR struct inode *inode = filep->f_inode;
  1182. FAR struct pcf8574_lcd_dev_s *priv =
  1183. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  1184. FAR struct slcd_attributes_s *attr =
  1185. (FAR struct slcd_attributes_s *)((uintptr_t) arg);
  1186. lcdinfo("SLCDIOC_GETATTRIBUTES:\n");
  1187. if (!attr)
  1188. {
  1189. return -EINVAL;
  1190. }
  1191. attr->nrows = priv->cfg.rows;
  1192. attr->ncolumns = priv->cfg.cols;
  1193. attr->nbars = 0;
  1194. attr->maxcontrast = 0;
  1195. attr->maxbrightness = 1; /* 'brightness' for us is the backlight */
  1196. }
  1197. break;
  1198. case SLCDIOC_CURPOS: /* SLCDIOC_CURPOS: Get the SLCD cursor position */
  1199. {
  1200. FAR struct inode *inode = filep->f_inode;
  1201. FAR struct pcf8574_lcd_dev_s *priv =
  1202. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  1203. FAR struct slcd_curpos_s *attr =
  1204. (FAR struct slcd_curpos_s *)((uintptr_t) arg);
  1205. uint8_t row;
  1206. uint8_t col;
  1207. nxsem_wait(&priv->sem_excl);
  1208. lcd_get_curpos(priv, &row, &col);
  1209. attr->row = row;
  1210. attr->column = col;
  1211. nxsem_post(&priv->sem_excl);
  1212. }
  1213. break;
  1214. case SLCDIOC_GETBRIGHTNESS: /* Get the current brightness setting */
  1215. {
  1216. FAR struct inode *inode = filep->f_inode;
  1217. FAR struct pcf8574_lcd_dev_s *priv =
  1218. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  1219. bool bon;
  1220. bon = (priv->bl_bit && priv->cfg.bl_active_high) ||
  1221. (!priv->bl_bit && !priv->cfg.bl_active_high);
  1222. *(FAR int *)((uintptr_t) arg) = bon ? 1 : 0;
  1223. }
  1224. break;
  1225. case SLCDIOC_SETBRIGHTNESS: /* Set the brightness to a new value */
  1226. {
  1227. FAR struct inode *inode = filep->f_inode;
  1228. FAR struct pcf8574_lcd_dev_s *priv =
  1229. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  1230. nxsem_wait(&priv->sem_excl);
  1231. lcd_backlight(priv, arg ? true : false);
  1232. nxsem_post(&priv->sem_excl);
  1233. }
  1234. break;
  1235. case SLCDIOC_CREATECHAR: /* Create a custom character pattern */
  1236. {
  1237. FAR struct inode *inode = filep->f_inode;
  1238. FAR struct pcf8574_lcd_dev_s *priv =
  1239. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  1240. FAR struct slcd_createchar_s *attr =
  1241. (FAR struct slcd_createchar_s *)((uintptr_t) arg);
  1242. nxsem_wait(&priv->sem_excl);
  1243. lcd_create_char(priv, attr->idx, attr->bmp);
  1244. nxsem_post(&priv->sem_excl);
  1245. }
  1246. break;
  1247. case SLCDIOC_SETBAR: /* SLCDIOC_SETBAR: Set bars on a bar display */
  1248. case SLCDIOC_GETCONTRAST: /* SLCDIOC_GETCONTRAST: Get the current
  1249. * contrast setting */
  1250. case SLCDIOC_SETCONTRAST: /* SLCDIOC_SETCONTRAST: Set the contrast to a
  1251. * new value */
  1252. default:
  1253. return -ENOTTY;
  1254. }
  1255. return OK;
  1256. }
  1257. /****************************************************************************
  1258. * Name: pcf8574lcd_poll
  1259. ****************************************************************************/
  1260. static int pcf8574lcd_poll(FAR struct file *filep, FAR struct pollfd *fds,
  1261. bool setup)
  1262. {
  1263. if (setup)
  1264. {
  1265. /* Data is always available to be read */
  1266. fds->revents |= (fds->events & (POLLIN | POLLOUT));
  1267. if (fds->revents != 0)
  1268. {
  1269. nxsem_post(fds->sem);
  1270. }
  1271. }
  1272. return OK;
  1273. }
  1274. /****************************************************************************
  1275. * Name: pcf8574_lcd_unlink
  1276. ****************************************************************************/
  1277. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  1278. static int pcf8574_lcd_unlink(FAR struct inode *inode)
  1279. {
  1280. FAR struct pcf8574_lcd_dev_s *priv =
  1281. (FAR struct pcf8574_lcd_dev_s *)inode->i_private;
  1282. int ret = OK;
  1283. nxsem_wait(&priv->sem_excl);
  1284. priv->unlinked = true;
  1285. /* If there are no open references to the driver then tear it down now */
  1286. if (priv->refs == 0)
  1287. {
  1288. /* We have no real teardown at present */
  1289. ret = OK;
  1290. }
  1291. nxsem_post(&priv->sem_excl);
  1292. return ret;
  1293. }
  1294. #endif
  1295. /****************************************************************************
  1296. * Public Functions
  1297. ****************************************************************************/
  1298. /****************************************************************************
  1299. * Name: pcf8574_lcd_backpack_register
  1300. *
  1301. * Description:
  1302. * Register a character driver that is an I2C LCD 'backpack' for the
  1303. * ever-popular HD44780 based 16x2 LCD via pcf8574 I2C IO expander.
  1304. *
  1305. ****************************************************************************/
  1306. int pcf8574_lcd_backpack_register(FAR const char *devpath,
  1307. FAR struct i2c_master_s *i2c,
  1308. FAR struct pcf8574_lcd_backpack_config_s
  1309. *cfg)
  1310. {
  1311. FAR struct pcf8574_lcd_dev_s *priv;
  1312. int ret;
  1313. /* Sanity check on geometry */
  1314. if (cfg->rows < 1 || cfg->rows > 4)
  1315. {
  1316. lcdinfo("Display rows must be 1-4\n");
  1317. return -EINVAL;
  1318. }
  1319. if ((cfg->cols < 1 || cfg->cols > 64) ||
  1320. (cfg->rows == 4 && cfg->cols > 32))
  1321. {
  1322. lcdinfo("Display cols must be 1-64, and may not be part of a 4x40 "
  1323. "configuration\n");
  1324. return -EINVAL;
  1325. }
  1326. /* Initialize the device structure */
  1327. priv = (FAR struct pcf8574_lcd_dev_s *)
  1328. kmm_malloc(sizeof(struct pcf8574_lcd_dev_s));
  1329. if (!priv)
  1330. {
  1331. lcdinfo("Failed to allocate instance\n");
  1332. return -ENOMEM;
  1333. }
  1334. priv->i2c = i2c;
  1335. priv->cfg = *cfg;
  1336. priv->bl_bit = priv->cfg.bl_active_high ? 0 : (1 << priv->cfg.bl);
  1337. priv->refs = 0;
  1338. priv->unlinked = false;
  1339. nxsem_init(&priv->sem_excl, 0, 1);
  1340. /* Initialize */
  1341. lcd_init(priv);
  1342. /* If SLCD is console enable the backlight from start */
  1343. #ifdef CONFIG_SLCD_CONSOLE
  1344. lcd_backlight(priv, true);
  1345. #endif
  1346. /* Register the character driver */
  1347. ret = register_driver(devpath, &g_pcf8574_lcd_fops, 0666, priv);
  1348. if (ret < 0)
  1349. {
  1350. lcdinfo("Failed to register driver: %d\n", ret);
  1351. kmm_free(priv);
  1352. }
  1353. lcdinfo("pcf8574_lcd_backpack driver loaded successfully!\n");
  1354. return ret;
  1355. }