nuttx/drivers/lcd/ssd1306_i2c.c

144 lines
4.6 KiB
C
Raw Permalink Normal View History

/****************************************************************************
* drivers/lcd/ssd1306_i2c.c
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <errno.h>
#include <debug.h>
#include <string.h>
#include <nuttx/kmalloc.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/lcd/ssd1306.h>
#include "ssd1306.h"
2015-07-15 21:28:31 +02:00
#if defined(CONFIG_LCD_SSD1306) && defined(CONFIG_LCD_SSD1306_I2C)
2016-08-14 00:53:23 +02:00
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ssd1306_sendbyte
*
* Description:
* Write an 8-bit value into SSD1306
*
****************************************************************************/
int 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[2];
int ret;
#ifdef CONFIG_LCD_SSD1306_REGDEBUG
_err("-> 0x%02x\n", regval);
#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;
/* Setup 8-bit SSD1306 address write message */
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 */
/* Perform the transfer */
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
if (ret < 0)
{
lcderr("ERROR: I2C_TRANSFER failed: %d\n", ret);
}
return ret;
}
/****************************************************************************
* Name: ssd1306_sendblk
*
* Description:
* Write an array of bytes to SSD1306
*
****************************************************************************/
int ssd1306_sendblk(FAR struct ssd1306_dev_s *priv, uint8_t *data,
uint8_t len)
{
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;
/* 8-bit data read sequence:
*
* Start - I2C_Write_Address - Data transfer select - SSD1306_Write_Data
* - STOP
*/
2016-08-14 15:16:18 +02:00
/* Send the SSD1306 register address (with no STOP) */
transfer_mode = 0x40; /* Select data transfer */
2016-08-14 00:53:23 +02:00
msg[0].frequency = CONFIG_SSD1306_I2CFREQ; /* I2C frequency */
msg[0].addr = priv->addr; /* 7-bit address */
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 */
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 */
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);
if (ret < 0)
{
lcderr("ERROR: I2C_TRANSFER failed: %d\n", ret);
}
return ret;
}
2015-07-15 21:28:31 +02:00
#endif /* CONFIG_LCD_SSD1306 &7 CONFIG_LCD_SSD1306_I2C */