2016-05-25 16:47:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/lcd/pcf8574_lcd_backpack.c
|
|
|
|
*
|
2021-05-27 11:12:43 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-05-25 16:47:21 +02:00
|
|
|
*
|
2021-05-27 11:12:43 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-05-25 16:47:21 +02:00
|
|
|
*
|
2021-05-27 11:12:43 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-05-25 16:47:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2017-03-27 20:28:34 +02:00
|
|
|
#include <poll.h>
|
2016-05-25 16:47:21 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-05-25 16:47:21 +02:00
|
|
|
#include <nuttx/ascii.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/lcd/slcd_codec.h>
|
|
|
|
#include <nuttx/lcd/pcf8574_lcd_backpack.h>
|
|
|
|
|
2021-08-01 09:27:08 +02:00
|
|
|
#ifndef CONFIG_LIBC_SLCDCODEC
|
2019-02-27 15:41:08 +01:00
|
|
|
# error please also select Library Routines, Segment LCD CODEC
|
2016-05-25 16:47:21 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The PCF8574 is a 100 KHz device */
|
|
|
|
|
|
|
|
#define I2C_FREQ 100000
|
|
|
|
|
|
|
|
/* timing characteristics of the LCD interface */
|
|
|
|
|
2016-05-29 18:09:00 +02:00
|
|
|
#define DELAY_US_NYBBLE0 20
|
|
|
|
#define DELAY_US_NYBBLE1 10
|
|
|
|
#define DELAY_US_WRITE 40
|
2020-03-18 15:39:04 +01:00
|
|
|
#define DELAY_US_HOMECLEAR 2000
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* HD44780 commands */
|
|
|
|
|
|
|
|
#define CMD_CLEAR 0x01
|
|
|
|
#define CMD_HOME 0x02
|
|
|
|
#define CMD_CURSOR_ON_SOLID 0x0e
|
|
|
|
#define CMD_CURSOR_OFF 0x0c
|
|
|
|
#define CMD_CURSOR_ON_BLINK 0x0f
|
|
|
|
#define CMD_SET_CGADDR 0x40
|
|
|
|
#define CMD_SET_DDADDR 0x80
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
#define MAX_OPENCNT (255) /* Limit of uint8_t */
|
2016-05-29 21:53:37 +02:00
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct pcf8574_lcd_dev_s
|
|
|
|
{
|
2020-03-18 18:24:10 +01:00
|
|
|
FAR struct i2c_master_s *i2c; /* I2C interface */
|
2019-02-27 15:41:08 +01:00
|
|
|
struct pcf8574_lcd_backpack_config_s cfg; /* gpio configuration */
|
2020-03-18 18:24:10 +01:00
|
|
|
uint8_t bl_bit; /* current backlight bit */
|
|
|
|
uint8_t refs; /* Number of references */
|
|
|
|
uint8_t unlinked; /* We are unlinked, so teardown
|
|
|
|
* on last close */
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lock; /* mutex */
|
2016-05-25 16:47:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static int pcf8574_lcd_open(FAR struct file *filep);
|
|
|
|
static int pcf8574_lcd_close(FAR struct file *filep);
|
|
|
|
static ssize_t pcf8574_lcd_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t pcf8574_lcd_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t buflen);
|
2019-02-27 15:41:08 +01:00
|
|
|
static off_t pcf8574_lcd_seek(FAR struct file *filep, off_t offset,
|
|
|
|
int whence);
|
2016-05-25 16:47:21 +02:00
|
|
|
static int pcf8574_lcd_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
2022-04-12 12:16:13 +02:00
|
|
|
static int pcf8574_lcd_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
2016-05-29 21:53:37 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int pcf8574_lcd_unlink(FAR struct inode *inode);
|
|
|
|
#endif
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_pcf8574_lcd_fops =
|
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
pcf8574_lcd_open, /* open */
|
|
|
|
pcf8574_lcd_close, /* close */
|
|
|
|
pcf8574_lcd_read, /* read */
|
|
|
|
pcf8574_lcd_write, /* write */
|
|
|
|
pcf8574_lcd_seek, /* seek */
|
|
|
|
pcf8574_lcd_ioctl, /* ioctl */
|
2022-12-22 11:51:59 +01:00
|
|
|
NULL, /* truncate */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2022-04-12 12:16:13 +02:00
|
|
|
pcf8574_lcd_poll /* poll */
|
2016-05-29 21:53:37 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2022-01-10 15:51:17 +01:00
|
|
|
, pcf8574_lcd_unlink /* unlink */
|
2016-05-29 21:53:37 +02:00
|
|
|
#endif
|
2016-05-25 16:47:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca8574_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* primitive I2C write operation for the PCA8574, which is the IO expander
|
|
|
|
* device used on the board. The board essentially byte-bangs the
|
|
|
|
* parallel interface in nybble mode much as one might with a conventional
|
|
|
|
* GPIO based interface. The I2C interface simply sets the state of the
|
|
|
|
* 8 IO lines to control the 4 data, 3 control, and one for backlight,
|
|
|
|
* signals.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void pca8574_write(FAR struct pcf8574_lcd_dev_s *priv, uint8_t data)
|
|
|
|
{
|
|
|
|
struct i2c_config_s config;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set up the I2C configuration */
|
|
|
|
|
|
|
|
config.frequency = I2C_FREQ;
|
2019-02-27 15:41:08 +01:00
|
|
|
config.address = priv->cfg.addr;
|
|
|
|
config.addrlen = 7;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Write the value */
|
|
|
|
|
|
|
|
ret = i2c_write(priv->i2c, &config, &data, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
lcdinfo("pca8574_write() failed: %d\n", ret);
|
2016-05-25 16:47:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pca8574_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* primitive I2C read operation for the PCA8574, which is the IO expander
|
2019-02-27 15:41:08 +01:00
|
|
|
* device used on the board. The PCF8574 is 'interesting' in that it
|
|
|
|
* doesn't really have a data direction register, but instead the outputs
|
|
|
|
* are current-limited when high, so by setting an IO line high, you are
|
|
|
|
* also making it an input. Consequently, before using this method, you'll
|
|
|
|
* need to perform a pca8574_write() setting the bits you are interested in
|
|
|
|
* reading to 1's, then call this method.
|
2016-05-25 16:47:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
static int pca8574_read(FAR struct pcf8574_lcd_dev_s *priv, uint8_t *data)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
struct i2c_config_s config;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set up the I2C configuration */
|
|
|
|
|
|
|
|
config.frequency = I2C_FREQ;
|
2019-02-27 15:41:08 +01:00
|
|
|
config.address = priv->cfg.addr;
|
|
|
|
config.addrlen = 7;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Read the value */
|
|
|
|
|
|
|
|
ret = i2c_read(priv->i2c, &config, data, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
lcdinfo("pca8574_read() failed: %d\n", ret);
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_backlight
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* turn on, or off, the LCD backlight
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
static void lcd_backlight(FAR struct pcf8574_lcd_dev_s *priv, bool blon)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
uint8_t data;
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
data = ((blon && priv->cfg.bl_active_high) ||
|
|
|
|
(!blon && !priv->cfg.bl_active_high)) ? (1 << priv->cfg.bl) : 0;
|
2016-05-25 16:47:21 +02:00
|
|
|
pca8574_write(priv, data);
|
|
|
|
priv->bl_bit = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: rc2addr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This converts a row/column pair to a screen memory address.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint8_t rc2addr(FAR struct pcf8574_lcd_dev_s *priv,
|
|
|
|
uint8_t row, uint8_t col)
|
|
|
|
{
|
|
|
|
if (row < 2)
|
|
|
|
{
|
|
|
|
/* 1 and 2 line displays are simple; line0 @ 0x00, line1 @ 0x40 */
|
|
|
|
|
|
|
|
return row * 0x40 + col;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
/* 4 line displays are interesting; third line really is a continuation
|
2016-05-25 16:47:21 +02:00
|
|
|
* of first line, and fourth line is a continuation of second.
|
|
|
|
*/
|
|
|
|
|
2016-05-29 18:09:00 +02:00
|
|
|
return (row - 2) * 0x40 + (col + priv->cfg.cols);
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: addr2rc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This converts a screen memory address to a row/column pair.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
static inline void addr2rc(FAR struct pcf8574_lcd_dev_s *priv, uint8_t addr,
|
|
|
|
FAR uint8_t *row, FAR uint8_t *col)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
*row = addr / 0x40;
|
|
|
|
*col = addr % 0x40;
|
|
|
|
|
|
|
|
if (*col >= priv->cfg.cols)
|
|
|
|
{
|
2020-03-18 18:24:10 +01:00
|
|
|
/* 4 line displays have third and fourth lines really as continuation
|
|
|
|
* of the first and second.
|
2016-05-25 16:47:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
*row += 2;
|
|
|
|
*col -= priv->cfg.cols;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: prepare_nybble
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is a bit tedious, but scramble the bits of the nybble into position
|
|
|
|
* as per this board's particular wiring. Most boards are either on the
|
|
|
|
* top four bits, or bottom four, so a shift would do typically in those
|
|
|
|
* cases, but this gives us ultimate flexibility.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
uint8_t prepare_nybble(FAR struct pcf8574_lcd_dev_s *priv, uint8_t nybble)
|
|
|
|
{
|
|
|
|
uint8_t lcddata = 0;
|
|
|
|
|
|
|
|
if (nybble & 0x08)
|
|
|
|
{
|
|
|
|
lcddata |= (1 << priv->cfg.d7);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nybble & 0x04)
|
|
|
|
{
|
|
|
|
lcddata |= (1 << priv->cfg.d6);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nybble & 0x02)
|
|
|
|
{
|
|
|
|
lcddata |= (1 << priv->cfg.d5);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nybble & 0x01)
|
|
|
|
{
|
|
|
|
lcddata |= (1 << priv->cfg.d4);
|
|
|
|
}
|
|
|
|
|
|
|
|
return lcddata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unprepare_nybble
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the opposite of prepare_nybble(), and is used to unscramble bits
|
|
|
|
* when reading data from the display, as per board wiring.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
uint8_t unprepare_nybble(FAR struct pcf8574_lcd_dev_s *priv, uint8_t lcddata)
|
|
|
|
{
|
|
|
|
uint8_t data = 0;
|
|
|
|
|
|
|
|
if (lcddata & (1 << priv->cfg.d7))
|
|
|
|
{
|
|
|
|
data |= 0x08;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lcddata & (1 << priv->cfg.d6))
|
|
|
|
{
|
|
|
|
data |= 0x04;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lcddata & (1 << priv->cfg.d5))
|
|
|
|
{
|
|
|
|
data |= 0x02;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lcddata & (1 << priv->cfg.d4))
|
|
|
|
{
|
|
|
|
data |= 0x01;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: latch_nybble
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Latch a nybble on the LCD bus. This is done for each of two halves of a
|
|
|
|
* write operation in 4-bit mode. The 'rs' param is false for command
|
|
|
|
* transfers, and true for data transfers.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void latch_nybble(FAR struct pcf8574_lcd_dev_s *priv, uint8_t nybble,
|
|
|
|
bool rs)
|
|
|
|
{
|
|
|
|
uint8_t lcddata;
|
|
|
|
uint8_t en_bit;
|
|
|
|
uint8_t rs_bit;
|
|
|
|
|
|
|
|
en_bit = 1 << priv->cfg.en;
|
|
|
|
rs_bit = rs ? (1 << priv->cfg.rs) : 0;
|
|
|
|
|
2016-05-29 18:09:00 +02:00
|
|
|
/* Put the nybble, preserving backlight, reset R/~W and maybe RS */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2016-05-29 18:09:00 +02:00
|
|
|
lcddata = prepare_nybble(priv, nybble) | priv->bl_bit | rs_bit;
|
|
|
|
pca8574_write(priv, lcddata);
|
|
|
|
|
|
|
|
/* Now set EN */
|
|
|
|
|
|
|
|
lcddata |= en_bit;
|
2016-05-25 16:47:21 +02:00
|
|
|
pca8574_write(priv, lcddata);
|
|
|
|
up_udelay(DELAY_US_NYBBLE0); /* setup */
|
|
|
|
|
|
|
|
/* Latch on EN falling edge */
|
|
|
|
|
|
|
|
lcddata &= ~en_bit;
|
|
|
|
pca8574_write(priv, lcddata);
|
|
|
|
up_udelay(DELAY_US_NYBBLE1); /* hold */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: load_nybble
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Load a nybble from the LCD bus. This is done for each of two halves of a
|
|
|
|
* read operation in 4-bit mode. The 'rs' param is false for command
|
|
|
|
* transfers (the only one is to read status and the address register), and
|
|
|
|
* true for data transfers.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint8_t load_nybble(FAR struct pcf8574_lcd_dev_s *priv, bool rs)
|
|
|
|
{
|
|
|
|
uint8_t lcddata;
|
|
|
|
uint8_t en_bit;
|
|
|
|
uint8_t rs_bit;
|
|
|
|
uint8_t rw_bit;
|
|
|
|
uint8_t data;
|
|
|
|
|
|
|
|
en_bit = 1 << priv->cfg.en;
|
|
|
|
rs_bit = rs ? (1 << priv->cfg.rs) : 0;
|
|
|
|
rw_bit = 1 << priv->cfg.rw;
|
|
|
|
|
2016-05-29 18:09:00 +02:00
|
|
|
/* Put highs on the data lines, preserve, set R/~W and maybe RS */
|
|
|
|
|
|
|
|
lcddata = prepare_nybble(priv, 0x0f) | priv->bl_bit | rw_bit | rs_bit;
|
|
|
|
pca8574_write(priv, lcddata);
|
|
|
|
|
|
|
|
/* Now set EN */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2016-05-29 18:09:00 +02:00
|
|
|
lcddata |= en_bit;
|
2016-05-25 16:47:21 +02:00
|
|
|
pca8574_write(priv, lcddata);
|
|
|
|
up_udelay(DELAY_US_NYBBLE0); /* setup */
|
|
|
|
|
|
|
|
/* Now read the data */
|
|
|
|
|
|
|
|
pca8574_read(priv, &data);
|
|
|
|
data = unprepare_nybble(priv, data);
|
|
|
|
|
|
|
|
/* Transaction completed on EN falling edge */
|
|
|
|
|
|
|
|
lcddata &= ~en_bit;
|
|
|
|
pca8574_write(priv, lcddata);
|
|
|
|
up_udelay(DELAY_US_NYBBLE1); /* hold */
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_putcmd
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write a command to the LCD. Most of the time this is done in nybble
|
|
|
|
* mode in two phases, but in special cases (like initialization) we do not
|
|
|
|
* do two phases.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lcd_putcmd(FAR struct pcf8574_lcd_dev_s *priv, uint8_t data)
|
|
|
|
{
|
|
|
|
latch_nybble(priv, data >> 4, false);
|
|
|
|
latch_nybble(priv, data, false);
|
|
|
|
up_udelay(DELAY_US_WRITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_putdata
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write a byte to the LCD. This is used both for screen data and for
|
|
|
|
* character generator data, depending on a previous command that selected
|
|
|
|
* which ever is the destination.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void lcd_putdata(FAR struct pcf8574_lcd_dev_s *priv,
|
|
|
|
uint8_t data)
|
|
|
|
{
|
|
|
|
latch_nybble(priv, data >> 4, true);
|
|
|
|
latch_nybble(priv, data, true);
|
|
|
|
up_udelay(DELAY_US_WRITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_getdata
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read a data byte from the LCD.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint8_t lcd_getdata(FAR struct pcf8574_lcd_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint8_t data;
|
2019-02-27 15:41:08 +01:00
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
data = (load_nybble(priv, true) << 4) | load_nybble(priv, true);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_getcmd
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read a command byte from the LCD. There really is only one such read:
|
|
|
|
* get 'busy' status, and current address value.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint8_t lcd_getcmd(FAR struct pcf8574_lcd_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint8_t data;
|
|
|
|
data = (load_nybble(priv, false) << 4) | load_nybble(priv, false);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_read_busy_addr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the busy flag, and, optionally, the current value of the address
|
|
|
|
* register (data or character generator dependent on a previous command).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
static bool lcd_read_busy_addr(FAR struct pcf8574_lcd_dev_s *priv,
|
|
|
|
FAR uint8_t *addr)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
uint8_t data = lcd_getcmd(priv);
|
|
|
|
|
|
|
|
if (NULL != addr)
|
|
|
|
{
|
|
|
|
*addr = data & 0x7f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (data & 0x80) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_init
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* perform the initialization sequence to get the LCD into a known state.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lcd_init(FAR struct pcf8574_lcd_dev_s *priv)
|
|
|
|
{
|
|
|
|
/* Wait for more than 15 ms after Vcc for the LCD to stabilize */
|
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(50000);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Perform the init sequence. This sequence of commands is constructed so
|
|
|
|
* that it will get the device into nybble mode irrespective of what state
|
2020-03-18 18:24:10 +01:00
|
|
|
* the device is currently in (could be 8 bit, 4 bit nyb 0, 4 bit nyb 1).
|
|
|
|
* By sending the 'set 8-bit mode' three times, we will definitely end up
|
|
|
|
* in 8 bit mode, and then we can reliably transition to 4 bit mode for
|
|
|
|
* the remainder of operations.
|
2016-05-25 16:47:21 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-29 18:09:00 +02:00
|
|
|
/* Send Command 0x30, set 8-bit mode, and wait > 4.1 ms */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
latch_nybble(priv, 0x30 >> 4, false);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(5000);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Send Command 0x30, set 8-bit mode, and wait > 100 us */
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
latch_nybble(priv, 0x30 >> 4, false);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(5000);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Send Command 0x30, set 8-bit mode */
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
latch_nybble(priv, 0x30 >> 4, false);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(200);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2020-03-18 18:24:10 +01:00
|
|
|
/* now Function set: Set interface to be 4 bits long (only 1 cycle write
|
|
|
|
* for the first time).
|
2019-02-27 15:41:08 +01:00
|
|
|
*/
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
latch_nybble(priv, 0x20 >> 4, false);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(5000);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
/* Function set: DL=0;Interface is 4 bits, N=1 (2 Lines), F=0 (5x8 dots
|
|
|
|
* font)
|
|
|
|
*/
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
lcd_putcmd(priv, 0x28);
|
|
|
|
|
|
|
|
/* Display Off: D=0 (Display off), C=0 (Cursor Off), B=0 (Blinking Off) */
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
lcd_putcmd(priv, 0x08);
|
|
|
|
|
|
|
|
/* Display Clear */
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
lcd_putcmd(priv, CMD_CLEAR);
|
2019-02-27 15:41:08 +01:00
|
|
|
up_udelay(DELAY_US_HOMECLEAR); /* clear needs extra time */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Entry Mode Set: I/D=1 (Increment), S=0 (No shift) */
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
lcd_putcmd(priv, 0x06);
|
|
|
|
|
|
|
|
/* Display On, Cursor Off */
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2020-03-18 18:24:10 +01:00
|
|
|
lcd_putcmd(priv, 0x0c);
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_create_char
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-18 18:24:10 +01:00
|
|
|
* This creates a custom character pattern. There can be 8 5x8 patterns.
|
|
|
|
* The bitmap proceeds top to bottom, msb-lsb, and is right justified (i.e.
|
|
|
|
* only bits 4-0 are used). By convention, you are meant to always leave
|
|
|
|
* the last line (byte) zero so that the cursor can use this line, but this
|
|
|
|
* is not strictly required.
|
2016-05-25 16:47:21 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2020-03-18 18:24:10 +01:00
|
|
|
* priv - device instance
|
|
|
|
* idxchar - which character is being imaged; 0 - 7
|
|
|
|
* chardata - the character image bitmap; must be 8 bytes always
|
2016-05-25 16:47:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lcd_create_char(FAR struct pcf8574_lcd_dev_s *priv,
|
2019-02-27 15:41:08 +01:00
|
|
|
uint8_t idxchar, FAR const uint8_t *chardata)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
int nidx;
|
2016-05-25 16:47:21 +02:00
|
|
|
uint8_t addr;
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
lcd_read_busy_addr(priv, &addr);
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_putcmd(priv, CMD_SET_CGADDR | (idxchar << 3)); /* set CGRAM address */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
for (nidx = 0; nidx < 8; ++nidx)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_putdata(priv, chardata[nidx]);
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_putcmd(priv, CMD_SET_DDADDR | addr); /* restore DDRAM address */
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_set_curpos
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This sets the cursor position based on row, column addressing.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2016-05-25 16:47:21 +02:00
|
|
|
* priv - device instance
|
|
|
|
* row - row position
|
|
|
|
* col - column position
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lcd_set_curpos(FAR struct pcf8574_lcd_dev_s *priv,
|
|
|
|
uint8_t row, uint8_t col)
|
|
|
|
{
|
|
|
|
uint8_t addr;
|
2019-02-27 15:41:08 +01:00
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
addr = rc2addr(priv, row, col);
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_putcmd(priv, CMD_SET_DDADDR | addr); /* set DDRAM address */
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_get_curpos
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This gets the cursor position based on row, column addressing.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2016-05-25 16:47:21 +02:00
|
|
|
* priv - device instance
|
|
|
|
* row - row position
|
|
|
|
* col - column position
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lcd_get_curpos(FAR struct pcf8574_lcd_dev_s *priv,
|
2019-02-27 15:41:08 +01:00
|
|
|
FAR uint8_t *row, FAR uint8_t *col)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
uint8_t addr;
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
lcd_read_busy_addr(priv, &addr);
|
2016-05-25 16:47:21 +02:00
|
|
|
addr2rc(priv, addr, row, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_scroll_up
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Scroll the display up, and clear the new (last) line.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lcd_scroll_up(FAR struct pcf8574_lcd_dev_s *priv)
|
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
FAR uint8_t *data;
|
|
|
|
int nrow;
|
|
|
|
int ncol;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2020-08-04 19:29:52 +02:00
|
|
|
data = (FAR uint8_t *)kmm_malloc(priv->cfg.cols);
|
2016-05-25 16:47:21 +02:00
|
|
|
if (NULL == data)
|
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
lcdinfo("Failed to allocate buffer in lcd_scroll_up()\n");
|
2016-05-25 16:47:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
for (nrow = 1; nrow < priv->cfg.rows; ++nrow)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_set_curpos(priv, nrow, 0);
|
|
|
|
for (ncol = 0; ncol < priv->cfg.cols; ++ncol)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
data[ncol] = lcd_getdata(priv);
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_set_curpos(priv, nrow - 1, 0);
|
|
|
|
for (ncol = 0; ncol < priv->cfg.cols; ++ncol)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_putdata(priv, data[ncol]);
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lcd_set_curpos(priv, priv->cfg.rows - 1, 0);
|
2019-02-27 15:41:08 +01:00
|
|
|
for (ncol = 0; ncol < priv->cfg.cols; ++ncol)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
lcd_putdata(priv, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
lcd_set_curpos(priv, priv->cfg.rows - 1, 0);
|
|
|
|
|
2020-08-04 19:29:52 +02:00
|
|
|
kmm_free(data);
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_codec_action
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform an 'action' as per the Segment LCD codec.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2016-05-25 16:47:21 +02:00
|
|
|
* priv - device instance
|
|
|
|
* code - SLCD code action code
|
|
|
|
* count - count param for those actions that take it
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lcd_codec_action(FAR struct pcf8574_lcd_dev_s *priv,
|
|
|
|
enum slcdcode_e code, uint8_t count)
|
|
|
|
{
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
/* Erasure */
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_BACKDEL: /* Backspace (backward delete) N characters */
|
|
|
|
{
|
|
|
|
if (count <= 0) /* silly case */
|
|
|
|
{
|
2016-05-25 16:47:21 +02:00
|
|
|
break;
|
2019-02-27 15:41:08 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
if (count > col) /* saturate to preceding columns available */
|
|
|
|
{
|
|
|
|
count = col;
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_set_curpos(priv, row, col - count);
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
/* ... and conscientiously fall through to next case ... */
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_FWDDEL: /* Delete (forward delete) N characters, moving text */
|
|
|
|
{
|
|
|
|
if (count <= 0) /* silly case */
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
|
|
|
uint8_t start;
|
|
|
|
uint8_t end;
|
|
|
|
uint8_t nidx;
|
|
|
|
uint8_t data;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
start = col + count;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
if (start >= priv->cfg.cols) /* silly case of nothing left */
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
end = start + count;
|
|
|
|
if (end > priv->cfg.cols) /* saturate */
|
|
|
|
{
|
|
|
|
end = priv->cfg.cols;
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
for (nidx = col; nidx < end; ++start, ++nidx) /* much like memmove */
|
|
|
|
{
|
|
|
|
lcd_set_curpos(priv, row, start);
|
|
|
|
data = lcd_getdata(priv);
|
|
|
|
lcd_set_curpos(priv, row, nidx);
|
|
|
|
lcd_putdata(priv, data);
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
for (; nidx < priv->cfg.cols; ++nidx) /* much like memset */
|
|
|
|
{
|
|
|
|
lcd_putdata(priv, ' ');
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_set_curpos(priv, row, col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_ERASE: /* Erase N characters from the cursor position */
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2016-05-25 16:47:21 +02:00
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
|
|
|
uint8_t end;
|
2019-02-27 15:41:08 +01:00
|
|
|
uint8_t nidx;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
end = col + count;
|
|
|
|
if (end > priv->cfg.cols)
|
|
|
|
{
|
|
|
|
end = priv->cfg.cols;
|
|
|
|
}
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
for (nidx = col; nidx < end; ++nidx)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
lcd_putdata(priv, ' ');
|
|
|
|
}
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
lcd_set_curpos(priv, row, col);
|
|
|
|
}
|
2019-02-27 15:41:08 +01:00
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_CLEAR: /* Home the cursor and erase the entire display */
|
|
|
|
{
|
|
|
|
lcd_putcmd(priv, CMD_CLEAR);
|
|
|
|
up_udelay(DELAY_US_HOMECLEAR); /* clear needs extra time */
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_ERASEEOL: /* Erase from the cursor position to the end of
|
|
|
|
* line */
|
|
|
|
{
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
|
|
|
uint8_t nidx;
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_get_curpos(priv, &row, &col);
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
for (nidx = col; nidx < priv->cfg.cols; ++nidx)
|
|
|
|
{
|
|
|
|
lcd_putdata(priv, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
lcd_set_curpos(priv, row, col);
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Cursor movement */
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_LEFT: /* Cursor left by N characters */
|
|
|
|
{
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
if (count > col)
|
|
|
|
{
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
col -= count;
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_set_curpos(priv, row, col);
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_RIGHT: /* Cursor right by N characters */
|
|
|
|
{
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
col += count;
|
|
|
|
if (col >= priv->cfg.cols)
|
|
|
|
{
|
|
|
|
col = priv->cfg.cols - 1;
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_set_curpos(priv, row, col);
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_UP: /* Cursor up by N lines */
|
|
|
|
{
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
if (count > row)
|
|
|
|
{
|
|
|
|
row = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
row -= count;
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_set_curpos(priv, row, col);
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_DOWN: /* Cursor down by N lines */
|
|
|
|
{
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
row += count;
|
|
|
|
if (row >= priv->cfg.rows)
|
|
|
|
{
|
|
|
|
row = priv->cfg.rows - 1;
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_set_curpos(priv, row, col);
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_HOME: /* Cursor home */
|
|
|
|
{
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
lcd_set_curpos(priv, row, 0);
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_END: /* Cursor end */
|
|
|
|
{
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
lcd_set_curpos(priv, row, priv->cfg.cols - 1);
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_PAGEUP: /* Cursor up by N pages */
|
|
|
|
case SLCDCODE_PAGEDOWN: /* Cursor down by N pages */
|
|
|
|
break; /* Not supportable on this SLCD */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Blinking */
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_BLINKSTART: /* Start blinking with current cursor position */
|
|
|
|
lcd_putcmd(priv, CMD_CURSOR_ON_BLINK);
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDCODE_BLINKEND: /* End blinking after the current cursor
|
|
|
|
* position */
|
|
|
|
case SLCDCODE_BLINKOFF: /* Turn blinking off */
|
|
|
|
lcd_putcmd(priv, CMD_CURSOR_OFF);
|
|
|
|
break; /* Not implemented */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* These are actually unreportable errors */
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
default:
|
|
|
|
case SLCDCODE_NORMAL: /* Not a special keycode */
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_fpos_to_curpos
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert a file logical offset to a screen cursor pos (row,col). This
|
|
|
|
* discounts 'synthesized' line feeds at the end of screen lines.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lcd_fpos_to_curpos(FAR struct pcf8574_lcd_dev_s *priv,
|
2020-03-18 18:24:10 +01:00
|
|
|
off_t fpos, FAR uint8_t *row,
|
|
|
|
FAR uint8_t *col, FAR bool *onlf)
|
2016-05-29 21:53:37 +02:00
|
|
|
{
|
|
|
|
int virtcols;
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
virtcols = (priv->cfg.cols + 1);
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
/* Determine if this is a 'virtual' position (on the synthetic LF) */
|
|
|
|
|
|
|
|
*onlf = (priv->cfg.cols == fpos % virtcols);
|
|
|
|
|
|
|
|
/* Adjust off any preceding synthetic LF's to get linear position */
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
fpos -= fpos / virtcols;
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
/* Compute row/col from linear position */
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
*row = fpos / priv->cfg.cols;
|
|
|
|
*col = fpos % priv->cfg.cols;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lcd_curpos_to_fpos
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert a screen cursor pos (row,col) to a file logical offset. This
|
|
|
|
* includes 'synthesized' line feeds at the end of screen lines.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void lcd_curpos_to_fpos(FAR struct pcf8574_lcd_dev_s *priv,
|
2019-02-27 15:41:08 +01:00
|
|
|
uint8_t row, uint8_t col, FAR off_t *fpos)
|
2016-05-29 21:53:37 +02:00
|
|
|
{
|
|
|
|
/* the logical file position is the linear position plus any synthetic LF */
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
*fpos = (row * priv->cfg.cols) + col + row;
|
|
|
|
}
|
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcf8574_lcd_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* requisite device 'open' method; we don't do anything special
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcf8574_lcd_open(FAR struct file *filep)
|
|
|
|
{
|
2016-05-29 21:53:37 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2019-02-27 15:41:08 +01:00
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
2016-05-29 21:53:37 +02:00
|
|
|
|
|
|
|
/* Increment the reference count */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2016-05-29 21:53:37 +02:00
|
|
|
if (priv->refs == MAX_OPENCNT)
|
|
|
|
{
|
2022-11-03 11:07:17 +01:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2016-05-29 21:53:37 +02:00
|
|
|
return -EMFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->refs++;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2016-05-25 16:47:21 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcf8574_lcd_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* requisite device 'close' method; we don't do anything special
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcf8574_lcd_close(FAR struct file *filep)
|
|
|
|
{
|
2016-05-29 21:53:37 +02:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2019-02-27 15:41:08 +01:00
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
2016-05-29 21:53:37 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Decrement the reference count */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2016-05-29 21:53:37 +02:00
|
|
|
|
|
|
|
if (priv->refs == 0)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->refs--;
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
/* If we had previously unlinked, but there were open references at
|
|
|
|
* the time, we need to do the final teardown now.
|
2016-05-29 21:53:37 +02:00
|
|
|
*/
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
if (priv->refs == 0 && priv->unlinked)
|
|
|
|
{
|
|
|
|
/* We have no real teardown at present */
|
|
|
|
}
|
2020-03-18 18:24:10 +01:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2016-05-29 21:53:37 +02:00
|
|
|
return ret;
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcf8574_lcd_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This simply reads as much of the display memory as possible. This is
|
2016-05-29 21:53:37 +02:00
|
|
|
* generally not very interesting, but we do it in a way that allows us to
|
|
|
|
* 'cat' the LCD contents via the shell.
|
2016-05-25 16:47:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t pcf8574_lcd_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2019-02-27 15:41:08 +01:00
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
|
|
|
int nidx;
|
2016-05-25 16:47:21 +02:00
|
|
|
uint8_t addr;
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
2016-05-29 21:53:37 +02:00
|
|
|
bool onlf;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Get current cursor position so we can restore it */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
lcd_read_busy_addr(priv, &addr);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
/* Convert file position to row/col address and position DDADDR there */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
lcd_fpos_to_curpos(priv, filep->f_pos, &row, &col, &onlf);
|
|
|
|
lcd_set_curpos(priv, row, col);
|
|
|
|
|
|
|
|
/* Read as much of the display as possible */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
nidx = 0;
|
|
|
|
while (nidx < buflen && row < priv->cfg.rows)
|
|
|
|
{
|
|
|
|
/* Synthesize end-of-line LF and advance to start of next row */
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
if (onlf)
|
|
|
|
{
|
|
|
|
/* Synthesize LF for all but last row */
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
if (row < priv->cfg.rows - 1)
|
|
|
|
{
|
|
|
|
buffer[nidx] = '\x0a';
|
|
|
|
onlf = false;
|
|
|
|
++filep->f_pos;
|
|
|
|
++nidx;
|
|
|
|
}
|
2016-05-29 21:53:37 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
++row;
|
|
|
|
col = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-29 21:53:37 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
/* If we are at start of line we will need to update DDRAM address */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
if (0 == col)
|
|
|
|
{
|
|
|
|
lcd_set_curpos(priv, row, 0);
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
buffer[nidx] = lcd_getdata(priv);
|
2016-05-29 21:53:37 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
++filep->f_pos;
|
|
|
|
++nidx;
|
|
|
|
++col;
|
2016-05-29 21:53:37 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
/* If we are now at the end of a line, we setup for the synthetic LF */
|
|
|
|
|
|
|
|
if (priv->cfg.cols == col)
|
|
|
|
{
|
|
|
|
onlf = true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_putcmd(priv, CMD_SET_DDADDR | addr); /* Restore DDRAM address */
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2019-02-27 15:41:08 +01:00
|
|
|
return nidx;
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcf8574_lcd_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Output a sequence of characters to the device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t pcf8574_lcd_write(FAR struct file *filep,
|
2019-02-27 15:41:08 +01:00
|
|
|
FAR const char *buffer, size_t buflen)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2019-02-27 15:41:08 +01:00
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
2022-12-04 12:50:09 +01:00
|
|
|
struct lib_meminstream_s instream;
|
2016-05-25 16:47:21 +02:00
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
|
|
|
struct slcdstate_s state;
|
|
|
|
enum slcdret_e result;
|
|
|
|
uint8_t ch;
|
|
|
|
uint8_t count;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Initialize the stream for use with the SLCD CODEC */
|
|
|
|
|
2022-12-04 12:50:09 +01:00
|
|
|
lib_meminstream(&instream, buffer, buflen);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Get the current cursor position now; we'll keep track of it as we go */
|
|
|
|
|
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
|
|
|
|
/* Now decode and process every byte in the input buffer */
|
|
|
|
|
|
|
|
memset(&state, 0, sizeof(struct slcdstate_s));
|
2022-12-04 12:50:09 +01:00
|
|
|
while ((result = slcd_decode(&instream.public,
|
|
|
|
&state, &ch, &count)) != SLCDRET_EOF)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
if (result == SLCDRET_CHAR) /* A normal character was returned */
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
/* Check for ASCII control characters */
|
|
|
|
|
|
|
|
if (ch == ASCII_TAB)
|
|
|
|
{
|
|
|
|
lcd_putcmd(priv, CMD_CURSOR_ON_BLINK);
|
|
|
|
}
|
|
|
|
else if (ch == ASCII_VT)
|
|
|
|
{
|
|
|
|
/* Turn the backlight on */
|
|
|
|
|
|
|
|
lcd_backlight(priv, true);
|
|
|
|
}
|
|
|
|
else if (ch == ASCII_FF)
|
|
|
|
{
|
|
|
|
/* Turn the backlight off */
|
|
|
|
|
|
|
|
lcd_backlight(priv, false);
|
|
|
|
}
|
|
|
|
else if (ch == ASCII_CR)
|
|
|
|
{
|
|
|
|
/* Perform a Home */
|
|
|
|
|
|
|
|
lcd_putcmd(priv, CMD_HOME);
|
2019-02-27 15:41:08 +01:00
|
|
|
up_udelay(DELAY_US_HOMECLEAR); /* home needs extra time */
|
2016-05-25 16:47:21 +02:00
|
|
|
row = 0;
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
else if (ch == ASCII_SO)
|
|
|
|
{
|
|
|
|
lcd_putcmd(priv, CMD_CURSOR_OFF);
|
|
|
|
}
|
|
|
|
else if (ch == ASCII_SI)
|
|
|
|
{
|
|
|
|
/* Perform the re-initialize */
|
|
|
|
|
|
|
|
lcd_init(priv);
|
|
|
|
row = 0;
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
else if (ch == ASCII_LF)
|
|
|
|
{
|
|
|
|
/* unixian line term; go to start of next line */
|
|
|
|
|
|
|
|
row += 1;
|
|
|
|
if (row >= priv->cfg.rows)
|
|
|
|
{
|
|
|
|
lcd_scroll_up(priv);
|
|
|
|
row = priv->cfg.rows - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
col = 0;
|
|
|
|
lcd_set_curpos(priv, row, col);
|
|
|
|
}
|
|
|
|
else if (ch == ASCII_BS)
|
|
|
|
{
|
|
|
|
/* Perform the backward deletion */
|
|
|
|
|
|
|
|
lcd_codec_action(priv, SLCDCODE_BACKDEL, 1);
|
|
|
|
|
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
}
|
|
|
|
else if (ch == ASCII_DEL)
|
|
|
|
{
|
|
|
|
/* Perform the forward deletion */
|
|
|
|
|
|
|
|
lcd_codec_action(priv, SLCDCODE_FWDDEL, 1);
|
|
|
|
|
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* All others are fair game. See if we need to wrap line. */
|
|
|
|
|
|
|
|
if (col >= priv->cfg.cols)
|
2019-02-27 15:41:08 +01:00
|
|
|
{
|
|
|
|
row += 1;
|
|
|
|
if (row >= priv->cfg.rows)
|
|
|
|
{
|
|
|
|
lcd_scroll_up(priv);
|
|
|
|
row = priv->cfg.rows - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
col = 0;
|
|
|
|
lcd_set_curpos(priv, row, col);
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
lcd_putdata(priv, ch);
|
|
|
|
++col;
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 15:41:08 +01:00
|
|
|
|
|
|
|
/* A special SLCD action was returned */
|
|
|
|
|
|
|
|
else /* (result == SLCDRET_SPEC) */
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
lcd_codec_action(priv, (enum slcdcode_e)ch, count);
|
|
|
|
|
2020-03-18 18:24:10 +01:00
|
|
|
/* we can't know what happened, so it's easier just to re-inquire
|
|
|
|
* as to where we are.
|
2016-05-25 16:47:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
/* Wherever we wound up, update our logical file pos to reflect it */
|
|
|
|
|
|
|
|
lcd_curpos_to_fpos(priv, row, col, &filep->f_pos);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2016-05-25 16:47:21 +02:00
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcf8574_lcd_seek
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Seek the logical file pointer to the specified position. This is
|
|
|
|
* probably not very interesting except possibly for (SEEK_SET, 0) to
|
|
|
|
* rewind the pointer for a subsequent read().
|
|
|
|
* The file pointer is logical, and includes synthesized LF chars at the
|
|
|
|
* end of the display lines.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
static off_t pcf8574_lcd_seek(FAR struct file *filep, off_t offset,
|
|
|
|
int whence)
|
2016-05-29 21:53:37 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2019-02-27 15:41:08 +01:00
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
2019-01-14 23:38:57 +01:00
|
|
|
off_t pos;
|
2016-05-29 21:53:37 +02:00
|
|
|
int maxpos;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2016-05-29 21:53:37 +02:00
|
|
|
|
|
|
|
maxpos = priv->cfg.rows * priv->cfg.cols + (priv->cfg.rows - 1);
|
2019-02-27 15:41:08 +01:00
|
|
|
pos = filep->f_pos;
|
2019-01-14 23:38:57 +01:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
switch (whence)
|
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
case SEEK_CUR:
|
|
|
|
pos += offset;
|
|
|
|
if (pos > maxpos)
|
|
|
|
{
|
|
|
|
pos = maxpos;
|
|
|
|
}
|
|
|
|
else if (pos < 0)
|
|
|
|
{
|
|
|
|
pos = 0;
|
|
|
|
}
|
2019-01-14 23:38:57 +01:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
filep->f_pos = pos;
|
|
|
|
break;
|
2019-01-14 23:38:57 +01:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SEEK_SET:
|
|
|
|
pos = offset;
|
|
|
|
if (pos > maxpos)
|
|
|
|
{
|
|
|
|
pos = maxpos;
|
|
|
|
}
|
|
|
|
else if (pos < 0)
|
|
|
|
{
|
|
|
|
pos = 0;
|
|
|
|
}
|
2019-01-14 23:38:57 +01:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
filep->f_pos = pos;
|
|
|
|
break;
|
2019-01-14 23:38:57 +01:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SEEK_END:
|
|
|
|
pos = maxpos + offset;
|
|
|
|
if (pos > maxpos)
|
|
|
|
{
|
|
|
|
pos = maxpos;
|
|
|
|
}
|
|
|
|
else if (pos < 0)
|
|
|
|
{
|
|
|
|
pos = 0;
|
|
|
|
}
|
2019-01-14 23:38:57 +01:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
filep->f_pos = pos;
|
|
|
|
break;
|
2019-01-14 23:38:57 +01:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
default:
|
2020-03-18 18:24:10 +01:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
/* Return EINVAL if the whence argument is invalid */
|
2019-01-14 23:38:57 +01:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
pos = (off_t) - EINVAL;
|
|
|
|
break;
|
2016-05-29 21:53:37 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2019-01-14 23:38:57 +01:00
|
|
|
return pos;
|
2016-05-29 21:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcf8574_lcd_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform device operations that are outside the standard I/O model.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcf8574_lcd_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDIOC_GETATTRIBUTES: /* SLCDIOC_GETATTRIBUTES: Get the
|
|
|
|
* attributes of the SLCD */
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
|
|
|
FAR struct slcd_attributes_s *attr =
|
|
|
|
(FAR struct slcd_attributes_s *)((uintptr_t) arg);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcdinfo("SLCDIOC_GETATTRIBUTES:\n");
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
if (!attr)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
attr->nrows = priv->cfg.rows;
|
|
|
|
attr->ncolumns = priv->cfg.cols;
|
|
|
|
attr->nbars = 0;
|
|
|
|
attr->maxcontrast = 0;
|
|
|
|
attr->maxbrightness = 1; /* 'brightness' for us is the backlight */
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDIOC_CURPOS: /* SLCDIOC_CURPOS: Get the SLCD cursor position */
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
|
|
|
FAR struct slcd_curpos_s *attr =
|
|
|
|
(FAR struct slcd_curpos_s *)((uintptr_t) arg);
|
|
|
|
uint8_t row;
|
|
|
|
uint8_t col;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_get_curpos(priv, &row, &col);
|
|
|
|
attr->row = row;
|
|
|
|
attr->column = col;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2019-02-27 15:41:08 +01:00
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDIOC_GETBRIGHTNESS: /* Get the current brightness setting */
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
|
|
|
bool bon;
|
|
|
|
|
|
|
|
bon = (priv->bl_bit && priv->cfg.bl_active_high) ||
|
|
|
|
(!priv->bl_bit && !priv->cfg.bl_active_high);
|
|
|
|
*(FAR int *)((uintptr_t) arg) = bon ? 1 : 0;
|
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDIOC_SETBRIGHTNESS: /* Set the brightness to a new value */
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_backlight(priv, arg ? true : false);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2019-02-27 15:41:08 +01:00
|
|
|
}
|
|
|
|
break;
|
2016-05-25 16:47:21 +02:00
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
case SLCDIOC_CREATECHAR: /* Create a custom character pattern */
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
|
|
|
FAR struct slcd_createchar_s *attr =
|
|
|
|
(FAR struct slcd_createchar_s *)((uintptr_t) arg);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2019-02-27 15:41:08 +01:00
|
|
|
lcd_create_char(priv, attr->idx, attr->bmp);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2019-02-27 15:41:08 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SLCDIOC_SETBAR: /* SLCDIOC_SETBAR: Set bars on a bar display */
|
|
|
|
case SLCDIOC_GETCONTRAST: /* SLCDIOC_GETCONTRAST: Get the current
|
|
|
|
* contrast setting */
|
|
|
|
case SLCDIOC_SETCONTRAST: /* SLCDIOC_SETCONTRAST: Set the contrast to a
|
|
|
|
* new value */
|
|
|
|
default:
|
|
|
|
return -ENOTTY;
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-04-12 12:16:13 +02:00
|
|
|
* Name: pcf8574_lcd_poll
|
2016-05-25 16:47:21 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-12 12:16:13 +02:00
|
|
|
static int pcf8574_lcd_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* Data is always available to be read */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&fds, 1, POLLIN | POLLOUT);
|
2016-05-25 16:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcf8574_lcd_unlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int pcf8574_lcd_unlink(FAR struct inode *inode)
|
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
FAR struct pcf8574_lcd_dev_s *priv =
|
|
|
|
(FAR struct pcf8574_lcd_dev_s *)inode->i_private;
|
2016-05-29 21:53:37 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2016-05-29 21:53:37 +02:00
|
|
|
|
|
|
|
priv->unlinked = true;
|
|
|
|
|
|
|
|
/* If there are no open references to the driver then tear it down now */
|
2019-02-27 15:41:08 +01:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
if (priv->refs == 0)
|
|
|
|
{
|
|
|
|
/* We have no real teardown at present */
|
2019-02-27 15:41:08 +01:00
|
|
|
|
2016-05-29 21:53:37 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2016-05-29 21:53:37 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcf8574_lcd_backpack_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register a character driver that is an I2C LCD 'backpack' for the
|
|
|
|
* ever-popular HD44780 based 16x2 LCD via pcf8574 I2C IO expander.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int pcf8574_lcd_backpack_register(FAR const char *devpath,
|
|
|
|
FAR struct i2c_master_s *i2c,
|
2020-03-18 18:24:10 +01:00
|
|
|
FAR struct pcf8574_lcd_backpack_config_s
|
|
|
|
*cfg)
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
|
|
|
FAR struct pcf8574_lcd_dev_s *priv;
|
|
|
|
int ret;
|
2016-05-25 16:48:32 +02:00
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
/* Sanity check on geometry */
|
|
|
|
|
|
|
|
if (cfg->rows < 1 || cfg->rows > 4)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
lcdinfo("Display rows must be 1-4\n");
|
2016-05-25 16:47:21 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:10 +01:00
|
|
|
if ((cfg->cols < 1 || cfg->cols > 64) ||
|
|
|
|
(cfg->rows == 4 && cfg->cols > 32))
|
2016-05-25 16:47:21 +02:00
|
|
|
{
|
2019-02-27 15:41:08 +01:00
|
|
|
lcdinfo("Display cols must be 1-64, and may not be part of a 4x40 "
|
|
|
|
"configuration\n");
|
2016-05-25 16:47:21 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the device structure */
|
|
|
|
|
2019-02-27 15:41:08 +01:00
|
|
|
priv = (FAR struct pcf8574_lcd_dev_s *)
|
|
|
|
kmm_malloc(sizeof(struct pcf8574_lcd_dev_s));
|
2016-05-25 16:47:21 +02:00
|
|
|
if (!priv)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
lcdinfo("Failed to allocate instance\n");
|
2016-05-25 16:47:21 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->cfg = *cfg;
|
|
|
|
priv->bl_bit = priv->cfg.bl_active_high ? 0 : (1 << priv->cfg.bl);
|
2016-05-29 21:53:37 +02:00
|
|
|
priv->refs = 0;
|
|
|
|
priv->unlinked = false;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->lock);
|
2016-05-25 16:47:21 +02:00
|
|
|
|
|
|
|
/* Initialize */
|
|
|
|
|
|
|
|
lcd_init(priv);
|
|
|
|
|
2021-03-14 23:59:30 +01:00
|
|
|
/* If SLCD is console enable the backlight from start */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SLCD_CONSOLE
|
|
|
|
lcd_backlight(priv, true);
|
|
|
|
#endif
|
|
|
|
|
2016-05-25 16:47:21 +02:00
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_pcf8574_lcd_fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
lcdinfo("Failed to register driver: %d\n", ret);
|
2022-10-16 18:50:59 +02:00
|
|
|
nxmutex_destroy(&priv->lock);
|
2016-05-25 16:47:21 +02:00
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
lcdinfo("pcf8574_lcd_backpack driver loaded successfully!\n");
|
2016-05-25 16:47:21 +02:00
|
|
|
return ret;
|
|
|
|
}
|