Modify the SSD1306 LCD driver to support either the SPI or I2C interface. From Alan Carvalho de Assis,

This commit is contained in:
Alan Carvalho de Assis 2015-07-15 11:21:54 -06:00 committed by Gregory Nutt
parent cebbe27cfe
commit a3e24a0b6c
7 changed files with 529 additions and 185 deletions

View File

@ -272,7 +272,6 @@ endif
config LCD_UG2864HSWEG01
bool "UG-2864HSWEG01 OLED Display Module"
default n
depends on SPI
select LCD_SSD1306
---help---
OLED Display Module, UG-2864HSWEG01, Univision Technology Inc based
@ -288,7 +287,7 @@ config LCD_UG2864HSWEG01
config LCD_UG2832HSWEG04
bool "UG-2832HSWEG04 OLED Display Module"
default n
depends on SPI && !LCD_UG2864HSWEG01
depends on !LCD_UG2864HSWEG01
select LCD_SSD1306
---help---
OLED Display Module, UG-UG2832HSWEG04, Univision Technology Inc
@ -307,6 +306,27 @@ config LCD_SSD1306
if LCD_SSD1306
choice
prompt "UG-2832HSWEG04 Interface"
default LCD_SSD1306_SPI
config LCD_SSD1306_SPI
bool "UG-2832HSWEG04 on SPI Interface"
select SPI
---help---
Enables support for the SPI interface.
config LCD_SSD1306_I2C
bool "UG-2832HSWEG04 on I2C Interface"
select I2C
---help---
Enables support for the I2C interface
endchoice # UG-2832HSWEG04 Interface
endif # LCD_SSD1306
if LCD_SSD1306_SPI
config SSD1306_SPIMODE
int "SSD1306 SPI Mode"
default 0
@ -328,7 +348,23 @@ config SSD1306_FREQUENCY
# supported. NOTE: At present, this must be undefined or defined to
# be 1.
endif
endif # LCD_SSD1306_SPI
if LCD_SSD1306_I2C
config SSD1306_I2CADDR
int "UG-2832HSWEG04 I2C Address"
default 120
---help---
I2C Address of SSD1306
config SSD1306_I2CFREQ
int "UG-2832HSWEG04 I2C Frequency"
default 400000
---help---
I2C Frequency to communicate with SSD1306
endif # LCD_SSD1306_I2C
config LCD_ST7565
bool "ST7565 LCD Display Module"

View File

@ -56,7 +56,15 @@ ifeq ($(CONFIG_LCD_UG9664HSWAG01),y)
endif
ifeq ($(CONFIG_LCD_SSD1306),y)
CSRCS += ssd1306.c
CSRCS += ssd1306_base.c
endif
ifeq ($(CONFIG_LCD_SSD1306_SPI),y)
CSRCS += ssd1306_spi.c
endif
ifeq ($(CONFIG_LCD_SSD1306_I2C),y)
CSRCS += ssd1306_i2c.c
endif
ifeq ($(CONFIG_LCD_SSD1289),y)

View File

@ -3,7 +3,7 @@
* Driver for Univision UG-2864HSWEG01 OLED display or UG-2832HSWEG04 both with the
* Univision SSD1306 controller in SPI mode
*
* Copyright (C) 2012-2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2012-2013, 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* References:
@ -129,12 +129,15 @@
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/i2c.h>
#include <nuttx/spi/spi.h>
#include <nuttx/lcd/lcd.h>
#include <nuttx/lcd/ssd1306.h>
#include <arch/irq.h>
#include "ssd1306_helpers.h"
#ifdef CONFIG_LCD_SSD1306
/**************************************************************************************
@ -316,15 +319,18 @@
struct ssd1306_dev_s
{
struct lcd_dev_s dev; /* Publically visible device structure */
struct lcd_dev_s dev; /* Publicly visible device structure */
/* Private LCD-specific information follows */
#ifdef CONFIG_LCD_SSD1306_SPI
FAR struct spi_dev_s *spi; /* Cached SPI device reference */
#else
FAR struct i2c_dev_s *i2c; /* Cached SPI device reference */
#endif
uint8_t contrast; /* Current contrast setting */
bool on; /* true: display is on */
/* The SSD1306 does not support reading from the display memory in SPI mode.
* Since there is 1 BPP and access is byte-by-byte, it is necessary to keep
* a shadow copy of the framebuffer memory. At 128x64, this amounts to 1KB.
@ -334,21 +340,9 @@ struct ssd1306_dev_s
};
/**************************************************************************************
* Private Function Protototypes
* Private Function Prototypes
**************************************************************************************/
/* Low-level SPI helpers */
#ifdef CONFIG_SPI_OWNBUS
static inline void ssd1306_configspi(FAR struct spi_dev_s *spi);
# define ssd1306_lock(spi)
# define ssd1306_unlock(spi)
#else
# define ssd1306_configspi(spi)
static void ssd1306_lock(FAR struct spi_dev_s *spi);
static void ssd1306_unlock(FAR struct spi_dev_s *spi);
#endif
/* LCD Data Transfer Methods */
static int ssd1306_putrun(fb_coord_t row, fb_coord_t col,
@ -446,96 +440,6 @@ static struct ssd1306_dev_s g_oleddev =
* Private Functions
**************************************************************************************/
/**************************************************************************************
* Name: ssd1306_configspi
*
* Description:
* Configure the SPI for use with the UG-2864HSWEG01
*
* Input Parameters:
* spi - Reference to the SPI driver structure
*
* Returned Value:
* None
*
* Assumptions:
*
**************************************************************************************/
#ifdef CONFIG_SPI_OWNBUS
static inline void ssd1306_configspi(FAR struct spi_dev_s *spi)
{
lcdvdbg("Mode: %d Bits: 8 Frequency: %d\n",
CONFIG_SSD1306_SPIMODE, CONFIG_SSD1306_FREQUENCY);
/* Configure SPI for the UG-2864HSWEG01. But only if we own the SPI bus. Otherwise,
* don't bother because it might change.
*/
SPI_SETMODE(spi, CONFIG_SSD1306_SPIMODE);
SPI_SETBITS(spi, 8);
SPI_SETFREQUENCY(spi, CONFIG_SSD1306_FREQUENCY);
}
#endif
/**************************************************************************************
* Name: ssd1306_lock
*
* Description:
* Select the SPI, locking and re-configuring if necessary
*
* Input Parameters:
* spi - Reference to the SPI driver structure
*
* Returned Value:
* None
*
* Assumptions:
*
**************************************************************************************/
#ifndef CONFIG_SPI_OWNBUS
static inline void ssd1306_lock(FAR struct spi_dev_s *spi)
{
/* Lock the SPI bus if there are multiple devices competing for the SPI bus. */
SPI_LOCK(spi, true);
/* Now make sure that the SPI bus is configured for the UG-2864HSWEG01 (it
* might have gotten configured for a different device while unlocked)
*/
SPI_SETMODE(spi, CONFIG_SSD1306_SPIMODE);
SPI_SETBITS(spi, 8);
SPI_SETFREQUENCY(spi, CONFIG_SSD1306_FREQUENCY);
}
#endif
/**************************************************************************************
* Name: ssd1306_unlock
*
* Description:
* De-select the SPI
*
* Input Parameters:
* spi - Reference to the SPI driver structure
*
* Returned Value:
* None
*
* Assumptions:
*
**************************************************************************************/
#ifndef CONFIG_SPI_OWNBUS
static inline void ssd1306_unlock(FAR struct spi_dev_s *spi)
{
/* De-select UG-2864HSWEG01 chip and relinquish the SPI bus. */
SPI_LOCK(spi, false);
}
#endif
/**************************************************************************************
* Name: ssd1306_putrun
*
@ -711,35 +615,33 @@ static int ssd1306_putrun(fb_coord_t row, fb_coord_t col, FAR const uint8_t *buf
/* Lock and select device */
ssd1306_lock(priv->spi);
SPI_SELECT(priv->spi, SPIDEV_DISPLAY, true);
ssd1306_select(priv, true);
/* Select command transfer */
SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY, true);
ssd1306_cmddata(priv, true);
/* Set the starting position for the run */
/* Set the column address to the XOFFSET value */
SPI_SEND(priv->spi, SSD1306_SETCOLL(devcol & 0x0f));
SPI_SEND(priv->spi, SSD1306_SETCOLH(devcol >> 4));
ssd1306_sendbyte(priv, SSD1306_SETCOLL(devcol & 0x0f));
ssd1306_sendbyte(priv, SSD1306_SETCOLH(devcol >> 4));
/* Set the page address */
SPI_SEND(priv->spi, SSD1306_PAGEADDR(page));
ssd1306_sendbyte(priv, SSD1306_PAGEADDR(page));
/* Select data transfer */
SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY, false);
ssd1306_cmddata(priv, false);
/* Then transfer all of the data */
(void)SPI_SNDBLOCK(priv->spi, fbptr, pixlen);
ssd1306_sendblk(priv, fbptr, pixlen);
/* De-select and unlock the device */
SPI_SELECT(priv->spi, SPIDEV_DISPLAY, false);
ssd1306_unlock(priv->spi);
ssd1306_select(priv, false);
return OK;
}
#else
@ -975,34 +877,32 @@ static int ssd1306_getpower(FAR struct lcd_dev_s *dev)
static int ssd1306_setpower(FAR struct lcd_dev_s *dev, int power)
{
struct ssd1306_dev_s *priv = (struct ssd1306_dev_s *)dev;
DEBUGASSERT(priv && (unsigned)power <= CONFIG_LCD_MAXPOWER && priv->spi);
DEBUGASSERT(priv && (unsigned)power <= CONFIG_LCD_MAXPOWER);
lcdvdbg("power: %d [%d]\n", power, priv->on ? CONFIG_LCD_MAXPOWER : 0);
/* Lock and select device */
ssd1306_lock(priv->spi);
SPI_SELECT(priv->spi, SPIDEV_DISPLAY, true);
ssd1306_select(priv, true);
if (power <= 0)
{
/* Turn the display off */
(void)SPI_SEND(priv->spi, SSD1306_DISPOFF);
(void)ssd1306_sendbyte(priv, SSD1306_DISPOFF);
priv->on = false;
}
else
{
/* Turn the display on */
(void)SPI_SEND(priv->spi, SSD1306_DISPON);
(void)ssd1306_sendbyte(priv, SSD1306_DISPON);
priv->on = true;
}
/* De-select and unlock the device */
SPI_SELECT(priv->spi, SPIDEV_DISPLAY, false);
ssd1306_unlock(priv->spi);
ssd1306_select(priv, false);
return OK;
}
@ -1060,23 +960,21 @@ static int ssd1306_setcontrast(struct lcd_dev_s *dev, unsigned int contrast)
/* Lock and select device */
ssd1306_lock(priv->spi);
SPI_SELECT(priv->spi, SPIDEV_DISPLAY, true);
ssd1306_select(priv, true);
/* Select command transfer */
SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY, true);
ssd1306_cmddata(priv, true);
/* Set the contrast */
(void)SPI_SEND(priv->spi, SSD1306_CONTRAST_MODE); /* Set contrast control register */
(void)SPI_SEND(priv->spi, SSD1306_CONTRAST(scaled)); /* Data 1: Set 1 of 256 contrast steps */
(void)ssd1306_sendbyte(priv, SSD1306_CONTRAST_MODE); /* Set contrast control register */
(void)ssd1306_sendbyte(priv, SSD1306_CONTRAST(scaled)); /* Data 1: Set 1 of 256 contrast steps */
priv->contrast = contrast;
/* De-select and unlock the device */
SPI_SELECT(priv->spi, SPIDEV_DISPLAY, false);
ssd1306_unlock(priv->spi);
ssd1306_select(priv, false);
return OK;
}
@ -1105,29 +1003,56 @@ static int ssd1306_setcontrast(struct lcd_dev_s *dev, unsigned int contrast)
*
**************************************************************************************/
FAR struct lcd_dev_s *ssd1306_initialize(FAR struct spi_dev_s *spi, unsigned int devno)
#ifdef CONFIG_LCD_SSD1306_SPI
FAR struct lcd_dev_s *ssd1306_initialize(FAR struct spi_dev_s *dev, unsigned int devno)
#else
FAR struct lcd_dev_s *ssd1306_initialize(FAR struct i2c_dev_s *dev, unsigned int devno)
#endif
{
FAR struct ssd1306_dev_s *priv = &g_oleddev;
lcdvdbg("Initializing\n");
DEBUGASSERT(spi && devno == 0);
/* Save the reference to the SPI device */
#ifdef CONFIG_LCD_SSD1306_SPI
priv->spi = dev;
priv->spi = spi;
/* If this SPI bus is not shared, then we can config it now.
* If it is shared, then other device could change our config,
* then just configure before sending data.
*/
/* Configure the SPI */
# ifdef CONFIG_SPI_OWNBUS
/* Configure SPI */
ssd1306_configspi(spi);
SPI_SETMODE(priv->spi, CONFIG_SSD1306_SPIMODE);
SPI_SETBITS(priv->spi, 8);
SPI_SETFREQUENCY(priv->spi, CONFIG_SSD1306_FREQUENCY);
# else
/* Configure the SPI */
ssd1306_configspi(priv->spi);
# endif
#else
priv->i2c = dev;
/* Set the I2C address and frequency. REVISIT: This logic would be
* insufficient if we share the I2C bus with any other devices that also
* modify the address and frequency.
*/
I2C_SETADDRESS(priv->i2c, CONFIG_SSD1306_I2CADDR, 7);
I2C_SETFREQUENCY(priv->i2c, CONFIG_SSD1306_I2CFREQ);
#endif
/* Lock and select device */
ssd1306_lock(priv->spi);
SPI_SELECT(spi, SPIDEV_DISPLAY, true);
ssd1306_select(priv, true);
/* Select command transfer */
SPI_CMDDATA(spi, SPIDEV_DISPLAY, true);
ssd1306_cmddata(priv, true);
/* Configure OLED SPI or I/O, must be delayed 1-10ms */
@ -1135,45 +1060,44 @@ FAR struct lcd_dev_s *ssd1306_initialize(FAR struct spi_dev_s *spi, unsigned int
/* Configure the device */
SPI_SEND(spi, SSD1306_DISPOFF); /* Display off 0xae */
SPI_SEND(spi, SSD1306_SETCOLL(0)); /* Set lower column address 0x00 */
SPI_SEND(spi, SSD1306_SETCOLH(0)); /* Set higher column address 0x10 */
SPI_SEND(spi, SSD1306_STARTLINE(0)); /* Set display start line 0x40 */
/* SPI_SEND(spi, SSD1306_PAGEADDR(0));*//* Set page address (Can ignore)*/
SPI_SEND(spi, SSD1306_CONTRAST_MODE); /* Contrast control 0x81 */
SPI_SEND(spi ,SSD1306_CONTRAST(SSD1306_DEV_CONTRAST)); /* Default contrast 0xCF */
SPI_SEND(spi, SSD1306_REMAPPLEFT); /* Set segment remap left 95 to 0 | 0xa1 */
/* SPI_SEND(spi, SSD1306_EDISPOFF); */ /* Normal display off 0xa4 (Can ignore)*/
SPI_SEND(spi, SSD1306_NORMAL); /* Normal (un-reversed) display mode 0xa6 */
SPI_SEND(spi, SSD1306_MRATIO_MODE); /* Multiplex ratio 0xa8 */
SPI_SEND(spi, SSD1306_MRATIO(SSD1306_DEV_DUTY)); /* Duty = 1/64 or 1/32 */
/* SPI_SEND(spi, SSD1306_SCANTOCOM0);*/ /* Com scan direction: Scan from COM[n-1] to COM[0] (Can ignore)*/
SPI_SEND(spi, SSD1306_DISPOFFS_MODE); /* Set display offset 0xd3 */
SPI_SEND(spi, SSD1306_DISPOFFS(0));
SPI_SEND(spi, SSD1306_CLKDIV_SET); /* Set clock divider 0xd5*/
SPI_SEND(spi, SSD1306_CLKDIV(8,0)); /* 0x80*/
ssd1306_sendbyte(priv, SSD1306_DISPOFF); /* Display off 0xae */
ssd1306_sendbyte(priv, SSD1306_SETCOLL(0)); /* Set lower column address 0x00 */
ssd1306_sendbyte(priv, SSD1306_SETCOLH(0)); /* Set higher column address 0x10 */
ssd1306_sendbyte(priv, SSD1306_STARTLINE(0)); /* Set display start line 0x40 */
/* ssd1306_sendbyte(priv, SSD1306_PAGEADDR(0));*//* Set page address (Can ignore)*/
ssd1306_sendbyte(priv, SSD1306_CONTRAST_MODE); /* Contrast control 0x81 */
ssd1306_sendbyte(priv,SSD1306_CONTRAST(SSD1306_DEV_CONTRAST)); /* Default contrast 0xCF */
ssd1306_sendbyte(priv, SSD1306_REMAPPLEFT); /* Set segment remap left 95 to 0 | 0xa1 */
/* ssd1306_sendbyte(priv, SSD1306_EDISPOFF); */ /* Normal display off 0xa4 (Can ignore)*/
ssd1306_sendbyte(priv, SSD1306_NORMAL); /* Normal (un-reversed) display mode 0xa6 */
ssd1306_sendbyte(priv, SSD1306_MRATIO_MODE); /* Multiplex ratio 0xa8 */
ssd1306_sendbyte(priv, SSD1306_MRATIO(SSD1306_DEV_DUTY)); /* Duty = 1/64 or 1/32 */
/* ssd1306_sendbyte(priv, SSD1306_SCANTOCOM0);*/ /* Com scan direction: Scan from COM[n-1] to COM[0] (Can ignore)*/
ssd1306_sendbyte(priv, SSD1306_DISPOFFS_MODE); /* Set display offset 0xd3 */
ssd1306_sendbyte(priv, SSD1306_DISPOFFS(0));
ssd1306_sendbyte(priv, SSD1306_CLKDIV_SET); /* Set clock divider 0xd5*/
ssd1306_sendbyte(priv, SSD1306_CLKDIV(8,0)); /* 0x80*/
SPI_SEND(spi, SSD1306_CHRGPER_SET); /* Set pre-charge period 0xd9 */
SPI_SEND(spi, SSD1306_CHRGPER(0x0f,1)); /* 0xf1 or 0x22 Enhanced mode */
ssd1306_sendbyte(priv, SSD1306_CHRGPER_SET); /* Set pre-charge period 0xd9 */
ssd1306_sendbyte(priv, SSD1306_CHRGPER(0x0f,1)); /* 0xf1 or 0x22 Enhanced mode */
SPI_SEND(spi, SSD1306_CMNPAD_CONFIG); /* Set common pads / set com pins hardware configuration 0xda */
SPI_SEND(spi, SSD1306_CMNPAD(SSD1306_DEV_CMNPAD)); /* 0x12 or 0x02 */
ssd1306_sendbyte(priv, SSD1306_CMNPAD_CONFIG); /* Set common pads / set com pins hardware configuration 0xda */
ssd1306_sendbyte(priv, SSD1306_CMNPAD(SSD1306_DEV_CMNPAD)); /* 0x12 or 0x02 */
SPI_SEND(spi, SSD1306_VCOM_SET); /* set vcomh 0xdb*/
SPI_SEND(spi, SSD1306_VCOM(0x40));
ssd1306_sendbyte(priv, SSD1306_VCOM_SET); /* set vcomh 0xdb*/
ssd1306_sendbyte(priv, SSD1306_VCOM(0x40));
SPI_SEND(spi, SSD1306_CHRPUMP_SET); /* Set Charge Pump enable/disable 0x8d ssd1306 */
SPI_SEND(spi, SSD1306_CHRPUMP_ON); /* 0x14 close 0x10 */
ssd1306_sendbyte(priv, SSD1306_CHRPUMP_SET); /* Set Charge Pump enable/disable 0x8d ssd1306 */
ssd1306_sendbyte(priv, SSD1306_CHRPUMP_ON); /* 0x14 close 0x10 */
/* SPI_SEND(spi, SSD1306_DCDC_MODE); */ /* DC/DC control mode: on (SSD1306 Not supported) */
/* SPI_SEND(spi, SSD1306_DCDC_ON); */
/* ssd1306_sendbyte(priv, SSD1306_DCDC_MODE); */ /* DC/DC control mode: on (SSD1306 Not supported) */
/* ssd1306_sendbyte(priv, SSD1306_DCDC_ON); */
SPI_SEND(spi, SSD1306_DISPON); /* Display ON 0xaf */
ssd1306_sendbyte(priv, SSD1306_DISPON); /* Display ON 0xaf */
/* De-select and unlock the device */
SPI_SELECT(spi, SPIDEV_DISPLAY, false);
ssd1306_unlock(priv->spi);
ssd1306_select(priv, false);
/* Clear the display */
@ -1219,8 +1143,7 @@ void ssd1306_fill(FAR struct lcd_dev_s *dev, uint8_t color)
/* Lock and select device */
ssd1306_lock(priv->spi);
SPI_SELECT(priv->spi, SPIDEV_DISPLAY, true);
ssd1306_select(priv, true);
/* Visit each page */
@ -1228,31 +1151,30 @@ void ssd1306_fill(FAR struct lcd_dev_s *dev, uint8_t color)
{
/* Select command transfer */
SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY, true);
ssd1306_cmddata(priv, true);
/* Set the column address to the XOFFSET value */
SPI_SEND(priv->spi, SSD1306_SETCOLL(SSD1306_DEV_XOFFSET));
SPI_SEND(priv->spi, SSD1306_SETCOLH(0));
ssd1306_sendbyte(priv, SSD1306_SETCOLL(SSD1306_DEV_XOFFSET));
ssd1306_sendbyte(priv, SSD1306_SETCOLH(0));
/* Set the page address */
SPI_SEND(priv->spi, SSD1306_PAGEADDR(page));
ssd1306_sendbyte(priv, SSD1306_PAGEADDR(page));
/* Select data transfer */
SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY, false);
ssd1306_cmddata(priv, false);
/* Transfer one page of the selected color */
(void)SPI_SNDBLOCK(priv->spi, &priv->fb[page * SSD1306_DEV_XRES],
ssd1306_sendblk(priv, &priv->fb[page * SSD1306_DEV_XRES],
SSD1306_DEV_XRES);
}
/* De-select and unlock the device */
SPI_SELECT(priv->spi, SPIDEV_DISPLAY, false);
ssd1306_unlock(priv->spi);
ssd1306_select(priv, false);
}
#endif /* CONFIG_LCD_SSD1306 */

View File

@ -0,0 +1,52 @@
/********************************************************************************************
* drivers/lcd/ssd1306_helpers.h
*
* Copyright (C) 2015 Alan Carvalho de Assis
* Author: Alan Carvalho de Assis <acassis@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
********************************************************************************************/
#include <nuttx/lcd/ssd1306.h>
//void ssd1306_sendbyte(FAR struct ssd1306_dev_s *priv, uint8_t regval);
//void ssd1306_sendblk(FAR struct ssd1306_dev_s *priv, uint8_t *data, uint8_t len);
#ifdef CONFIG_LCD_SSD1306_SPI
// void ssd1306_select(FAR struct ssd1306_dev_s *priv, bool cs);
// void ssd1306_cmddata(FAR struct ssd1306_dev_s *priv, bool cmd);
# ifndef CONFIG_SPI_OWNBUS
// static inline void ssd1306_configspi(FAR struct spi_dev_s *spi)
# endif
#else
# define ssd1306_select(priv, cs)
# define ssd1306_cmddata(priv, cmd)
#endif

134
drivers/lcd/ssd1306_i2c.c Normal file
View File

@ -0,0 +1,134 @@
/****************************************************************************
* drivers/lcd/ssd1306_i2c.c
*
* Copyright (C) 2015 Alan Carvalho de Assis. All rights reserved.
* Author: Alan Carvalho de Assis <acassis@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/lcd/ssd1306.h>
#include "ssd1306_helpers.h"
#if defined(CONFIG_LCD_SSD1306) && defined(CONFIG_LCD_UG2864HSWEG01_I2C)
/****************************************************************************
* Name: ssd1306_sendbyte
*
* Description:
* Write an 8-bit value into SSD1306
*
****************************************************************************/
void ssd1306_sendbyte(FAR struct ssd1306_dev_s *priv, uint8_t regval)
{
/* 8-bit data read sequence:
*
* Start - I2C_Write_Address - SSD1306_Reg_Address - SSD1306_Write_Data - STOP
*/
struct i2c_msg_s msg;
uint8_t txbuffer[1];
int ret;
#ifdef CONFIG_LCD_SSD1306_REGDEBUG
lldbg("-> 0x%02x\n", regval);
#endif
/* Setup to the data to be transferred. Two bytes: The SSD1306 register
* address followed by one byte of data.
*/
txbuffer[0] = regval;
/* Setup 8-bit SSD1306 address write message */
msg.addr = priv->config->address; /* 7-bit address */
msg.flags = 0; /* Write transaction, beginning with START */
msg.buffer = txbuffer; /* Transfer from this address */
msg.length = 1; /* Send one byte following the address
* (then STOP) */
/* Perform the transfer */
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
if (ret < 0)
{
sndbg("I2C_TRANSFER failed: %d\n", ret);
}
}
/****************************************************************************
* Name: ssd1306_sendblk
*
* Description:
* Write an array of bytes to SSD1306
*
****************************************************************************/
void ssd1306_sendblk(FAR struct ssd1306_dev_s *priv, uint8_t *data, uint8_t len)
{
/* 8-bit data read sequence:
*
* Start - I2C_Write_Address - SSD1306_Reg_Address - SSD1306_Write_Data - STOP
*/
struct i2c_msg_s msg;
int ret;
/* Setup 8-bit SSD1306 address write message */
msg.addr = priv->config->address; /* 7-bit address */
msg.flags = 0; /* Write transaction, beginning with START */
msg.buffer = data; /* Transfer from this address */
msg.length = len; /* Send one byte following the address
* (then STOP) */
/* Perform the transfer */
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
if (ret < 0)
{
sndbg("I2C_TRANSFER failed: %d\n", ret);
}
}
#endif /* CONFIG_LCD_SSD1306 &7 CONFIG_LCD_UG2864HSWEG01_I2C */

168
drivers/lcd/ssd1306_spi.c Normal file
View File

@ -0,0 +1,168 @@
/****************************************************************************
* drivers/lcd/ssd1306_spi.c
*
* Copyright (C) 2015 Alan Carvalho de Assis. All rights reserved.
* Author: Alan Carvalho de Assis <acassis@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/lcd/ssd1306.h>
#include "ssd1306_helpers.h"
#if defined(CONFIG_LCD_UG2864HSWEG01) && defined(CONFIG_LCD_UG2864HSWEG01_SPI)
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: ssd1306_configspi
*
* Description:
*
****************************************************************************/
#ifndef CONFIG_SPI_OWNBUS
static inline void ssd1306_configspi(FAR struct spi_dev_s *spi)
{
lcdvdbg("Mode: %d Bits: 8 Frequency: %d\n",
CONFIG_SSD1306_SPIMODE, CONFIG_SSD1306_FREQUENCY);
/* Configure SPI for the UG2864AMBAG01 */
SPI_SETMODE(spi, CONFIG_SSD1306_SPIMODE);
SPI_SETBITS(spi, 8);
SPI_SETFREQUENCY(spi, CONFIG_SSD1306_FREQUENCY);
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ssd1306_sendbyte
*
* Description:
* Write a value to an 8-bit value to UG2864AMBAG01
*
****************************************************************************/
void ssd1306_sendbyte(FAR struct ssd1306_dev_s *priv, uint8_t regval)
{
#ifdef CONFIG_LCD_UG2864AMBAG01_REGDEBUG
lldbg("->0x%02x\n", regval);
#endif
/* Send byte value to display */
(void)SPI_SEND(priv->spi, regval);
}
/****************************************************************************
* Name: ssd1306_sendblk
*
* Description:
* Write an array of bytes to UG2864AMBAG01
*
****************************************************************************/
void ssd1306_sendblk(FAR struct ssd1306_dev_s *priv, uint8_t *data, uint8_t len)
{
/* Send byte value to display */
(void)SPI_SNDBLOCK(priv, data, len);
}
/****************************************************************************
* Name: ssd1306_select
*
* Description:
* Enable/Disable UG2864AMBAG01 SPI CS
*
****************************************************************************/
void ssd1306_select(FAR struct ssd1306_dev_s *priv, bool cs)
{
#ifndef CONFIG_SPI_OWNBUS
/* If we are selecting the device */
if (cs == true)
{
/* If SPI bus is shared then lock and configure it */
(void)SPI_LOCK(priv->spi, true);
ssd1306_configspi(priv->spi);
}
#endif
/* Select/deselect SPI device */
SPI_SELECT(priv->spi, SPIDEV_DISPLAY, cs);
#ifndef CONFIG_SPI_OWNBUS
/* If we are deselecting the device */
if (cs == false)
{
/* Unlock bus */
(void)SPI_LOCK(priv->spi, false);
}
#endif
}
/****************************************************************************
* Name: ssd1306_cmddata
*
* Description:
* Select Command/Data mode for UG2864AMBAG01
*
****************************************************************************/
void ssd1306_cmddata(FAR struct ssd1306_dev_s *priv, bool cmd)
{
/* Select command transfer */
SPI_CMDDATA(priv->spi, SPIDEV_DISPLAY, cmd);
}
#endif /* CONFIG_LCD_UG2864AMBAG01 && CONFIG_LCD_UG2864AMBAG01_SPI */

View File

@ -4,7 +4,7 @@
* Driver for Univision UG-2864HSWEG01 OLED display or UG-2832HSWEG04 both with the
* Univision SSD1306 controller in SPI mode
*
* Copyright (C) 2012-2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2012-2013, 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* References:
@ -97,6 +97,8 @@
* Clock phase: Sample on trailing (rising edge) (CPHA 1)
*/
#ifdef LCD_SSD1306_SPI
#ifndef CONFIG_UG2864HSWEG01_SPIMODE
# define CONFIG_UG2864HSWEG01_SPIMODE SPIDEV_MODE3
#endif
@ -112,6 +114,24 @@
# error "CONFIG_SPI_CMDDATA must be defined in your NuttX configuration"
#endif
#endif /* CONFIG_LCD_SSD1306_SPI */
#ifdef CONFIG_LCD_SSD1306_I2C
#ifndef CONFIG_I2C_TRANSFER
# error "CONFIG_I2C_TRANSFER must be defined in your NuttX configuration"
#endif
#ifndef CONFIG_SSD1306_I2CADDR
# define CONFIG_SSD1306_I2CADDR 0x78 /* 120 in decimal */
#endif
#ifndef CONFIG_SSD1306_I2CFREQ
# define CONFIG_SSD1306_I2CADDR 400000
#endif
#endif /* CONFIG_LCD_SSD1306_I2C */
/* CONFIG_UG2864HSWEG01_NINTERFACES determines the number of physical interfaces
* that will be supported.
*/
@ -207,7 +227,7 @@ extern "C"
*
* Input Parameters:
*
* spi - A reference to the SPI driver instance.
* dev - A reference to the SPI/I2C driver instance.
* devno - A value in the range of 0 through CONFIG_UG2864HSWEG01_NINTERFACES-1.
* This allows support for multiple OLED devices.
*
@ -220,7 +240,11 @@ extern "C"
struct lcd_dev_s; /* See include/nuttx/lcd/lcd.h */
struct spi_dev_s; /* See include/nuttx/spi/spi.h */
FAR struct lcd_dev_s *ssd1306_initialize(FAR struct spi_dev_s *spi, unsigned int devno);
#ifdef CONFIG_LCD_SSD1306_SPI
FAR struct lcd_dev_s *ssd1306_initialize(FAR struct spi_dev_s *dev, unsigned int devno);
#else
FAR struct lcd_dev_s *ssd1306_initialize(FAR struct i2c_dev_s *dev, unsigned int devno);
#endif
/************************************************************************************************
* Name: ssd1306_fill