2015-07-15 19:21:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2016-08-14 00:20:20 +02:00
|
|
|
#include <string.h>
|
2015-07-15 19:21:54 +02:00
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2016-01-30 15:00:16 +01:00
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2015-07-15 19:21:54 +02:00
|
|
|
#include <nuttx/lcd/ssd1306.h>
|
|
|
|
|
2015-07-15 19:46:28 +02:00
|
|
|
#include "ssd1306.h"
|
2015-07-15 19:21:54 +02:00
|
|
|
|
2015-07-15 21:28:31 +02:00
|
|
|
#if defined(CONFIG_LCD_SSD1306) && defined(CONFIG_LCD_SSD1306_I2C)
|
2015-07-15 19:21:54 +02:00
|
|
|
|
2016-08-14 00:53:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-07-15 19:21:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ssd1306_sendbyte
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write an 8-bit value into SSD1306
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-02-19 14:32:44 +01:00
|
|
|
int ssd1306_sendbyte(FAR struct ssd1306_dev_s *priv, uint8_t regval)
|
2015-07-15 19:21:54 +02:00
|
|
|
{
|
|
|
|
/* 8-bit data read sequence:
|
|
|
|
*
|
2020-11-02 07:56:04 +01:00
|
|
|
* Start - I2C_Write_Address - SSD1306_Reg_Address - SSD1306_Write_Data
|
|
|
|
* - STOP
|
2015-07-15 19:21:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct i2c_msg_s msg;
|
2016-08-14 00:20:20 +02:00
|
|
|
uint8_t txbuffer[2];
|
2015-07-15 19:21:54 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef CONFIG_LCD_SSD1306_REGDEBUG
|
2016-06-20 20:44:38 +02:00
|
|
|
_err("-> 0x%02x\n", regval);
|
2015-07-15 19:21:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Setup to the data to be transferred. Two bytes: The SSD1306 register
|
|
|
|
* address followed by one byte of data.
|
|
|
|
*/
|
|
|
|
|
2016-08-14 00:53:23 +02:00
|
|
|
txbuffer[0] = 0x00;
|
|
|
|
txbuffer[1] = regval;
|
2015-07-15 19:21:54 +02:00
|
|
|
|
|
|
|
/* Setup 8-bit SSD1306 address write message */
|
|
|
|
|
2016-02-01 21:17:20 +01:00
|
|
|
msg.frequency = CONFIG_SSD1306_I2CFREQ; /* I2C frequency */
|
|
|
|
msg.addr = priv->addr; /* 7-bit address */
|
|
|
|
msg.flags = 0; /* Write transaction, beginning with START */
|
|
|
|
msg.buffer = txbuffer; /* Transfer from this address */
|
2016-08-14 15:16:18 +02:00
|
|
|
msg.length = 2; /* Send two bytes following the address
|
|
|
|
* then STOP */
|
2015-07-15 19:21:54 +02:00
|
|
|
|
|
|
|
/* Perform the transfer */
|
|
|
|
|
|
|
|
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-03-07 12:49:18 +01:00
|
|
|
lcderr("ERROR: I2C_TRANSFER failed: %d\n", ret);
|
2015-07-15 19:21:54 +02:00
|
|
|
}
|
2018-02-19 14:32:44 +01:00
|
|
|
|
|
|
|
return ret;
|
2015-07-15 19:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ssd1306_sendblk
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write an array of bytes to SSD1306
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-02 07:56:04 +01:00
|
|
|
int ssd1306_sendblk(FAR struct ssd1306_dev_s *priv, uint8_t *data,
|
|
|
|
uint8_t len)
|
2015-07-15 19:21:54 +02:00
|
|
|
{
|
2016-08-14 00:53:23 +02:00
|
|
|
struct i2c_msg_s msg[2];
|
2016-08-14 21:22:32 +02:00
|
|
|
uint8_t transfer_mode;
|
2016-08-14 00:53:23 +02:00
|
|
|
int ret;
|
|
|
|
|
2015-07-15 19:21:54 +02:00
|
|
|
/* 8-bit data read sequence:
|
|
|
|
*
|
2020-11-02 07:56:04 +01:00
|
|
|
* Start - I2C_Write_Address - Data transfer select - SSD1306_Write_Data
|
|
|
|
* - STOP
|
2015-07-15 19:21:54 +02:00
|
|
|
*/
|
|
|
|
|
2016-08-14 15:16:18 +02:00
|
|
|
/* Send the SSD1306 register address (with no STOP) */
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-08-14 21:42:24 +02:00
|
|
|
transfer_mode = 0x40; /* Select data transfer */
|
2015-07-15 19:21:54 +02:00
|
|
|
|
2016-08-14 00:53:23 +02:00
|
|
|
msg[0].frequency = CONFIG_SSD1306_I2CFREQ; /* I2C frequency */
|
|
|
|
msg[0].addr = priv->addr; /* 7-bit address */
|
2020-11-02 07:46:04 +01:00
|
|
|
msg[0].flags = I2C_M_NOSTOP; /* Write transaction, beginning with START */
|
2016-08-14 21:22:32 +02:00
|
|
|
msg[0].buffer = &transfer_mode; /* Transfer mode send */
|
2016-08-14 00:53:23 +02:00
|
|
|
msg[0].length = 1; /* Send the one byte register address */
|
2015-07-15 19:21:54 +02:00
|
|
|
|
2016-08-14 15:16:18 +02:00
|
|
|
/* Followed by the SSD1306 write data (with no RESTART, then STOP) */
|
2016-08-14 00:53:23 +02:00
|
|
|
|
|
|
|
msg[1].frequency = CONFIG_SSD1306_I2CFREQ; /* I2C frequency */
|
|
|
|
msg[1].addr = priv->addr; /* 7-bit address */
|
2018-08-03 16:51:55 +02:00
|
|
|
msg[1].flags = I2C_M_NOSTART; /* Write transaction with no RESTART */
|
2016-08-14 00:53:23 +02:00
|
|
|
msg[1].buffer = data; /* Transfer from this address */
|
|
|
|
msg[1].length = len; /* Send the data, then STOP */
|
|
|
|
|
|
|
|
ret = I2C_TRANSFER(priv->i2c, msg, 2);
|
2015-07-15 19:21:54 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-03-07 12:49:18 +01:00
|
|
|
lcderr("ERROR: I2C_TRANSFER failed: %d\n", ret);
|
2015-07-15 19:21:54 +02:00
|
|
|
}
|
2018-02-19 14:32:44 +01:00
|
|
|
|
|
|
|
return ret;
|
2015-07-15 19:21:54 +02:00
|
|
|
}
|
|
|
|
|
2015-07-15 21:28:31 +02:00
|
|
|
#endif /* CONFIG_LCD_SSD1306 &7 CONFIG_LCD_SSD1306_I2C */
|