drivers: display: ili9340: code cleanup
Multiple enhancements towards better code readability and consistency: - sorted headers - define and reference magic constants - adjust some names - add U suffix to unsigned constants - move hw reset to a function - remove non-needed initialization code from seeed tft Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
This commit is contained in:
parent
2d4ec8fca9
commit
b406527a66
3 changed files with 85 additions and 75 deletions
|
@ -9,17 +9,17 @@
|
||||||
#define DT_DRV_COMPAT ilitek_ili9340
|
#define DT_DRV_COMPAT ilitek_ili9340
|
||||||
|
|
||||||
#include "display_ili9340.h"
|
#include "display_ili9340.h"
|
||||||
#include <drivers/display.h>
|
|
||||||
|
|
||||||
#define LOG_LEVEL CONFIG_DISPLAY_LOG_LEVEL
|
|
||||||
#include <logging/log.h>
|
|
||||||
LOG_MODULE_REGISTER(display_ili9340);
|
|
||||||
|
|
||||||
#include <drivers/gpio.h>
|
|
||||||
#include <sys/byteorder.h>
|
|
||||||
#include <drivers/spi.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <drivers/display.h>
|
||||||
|
#include <drivers/gpio.h>
|
||||||
|
#include <drivers/spi.h>
|
||||||
|
#include <sys/byteorder.h>
|
||||||
|
|
||||||
|
#include <logging/log.h>
|
||||||
|
LOG_MODULE_REGISTER(display_ili9340, CONFIG_DISPLAY_LOG_LEVEL);
|
||||||
|
|
||||||
struct ili9340_config {
|
struct ili9340_config {
|
||||||
const char *spi_name;
|
const char *spi_name;
|
||||||
uint16_t spi_addr;
|
uint16_t spi_addr;
|
||||||
|
@ -43,9 +43,6 @@ struct ili9340_data {
|
||||||
struct spi_cs_control cs_ctrl;
|
struct spi_cs_control cs_ctrl;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define ILI9340_CMD_DATA_PIN_COMMAND 1
|
|
||||||
#define ILI9340_CMD_DATA_PIN_DATA 0
|
|
||||||
|
|
||||||
/* The number of bytes taken by a RGB pixel */
|
/* The number of bytes taken by a RGB pixel */
|
||||||
#ifdef CONFIG_ILI9340_RGB565
|
#ifdef CONFIG_ILI9340_RGB565
|
||||||
#define ILI9340_RGB_SIZE 2U
|
#define ILI9340_RGB_SIZE 2U
|
||||||
|
@ -62,11 +59,27 @@ static int ili9340_exit_sleep(const struct device *dev)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
k_sleep(K_MSEC(120));
|
k_sleep(K_MSEC(ILI9340_SLEEP_OUT_TIME));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void ili9340_hw_reset(const struct device *dev)
|
||||||
|
{
|
||||||
|
const struct ili9340_config *config = (struct ili9340_config *)dev->config;
|
||||||
|
struct ili9340_data *data = (struct ili9340_data *)dev->data;
|
||||||
|
|
||||||
|
if (data->reset_gpio == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
gpio_pin_set(data->reset_gpio, config->reset_pin, 1);
|
||||||
|
k_sleep(K_MSEC(ILI9340_RESET_PULSE_TIME));
|
||||||
|
gpio_pin_set(data->reset_gpio, config->reset_pin, 0);
|
||||||
|
|
||||||
|
k_sleep(K_MSEC(ILI9340_RESET_WAIT_TIME));
|
||||||
|
}
|
||||||
|
|
||||||
static int ili9340_init(const struct device *dev)
|
static int ili9340_init(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct ili9340_config *config = (struct ili9340_config *)dev->config;
|
const struct ili9340_config *config = (struct ili9340_config *)dev->config;
|
||||||
|
@ -74,8 +87,6 @@ static int ili9340_init(const struct device *dev)
|
||||||
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
LOG_DBG("Initializing display driver");
|
|
||||||
|
|
||||||
data->spi_dev = device_get_binding(config->spi_name);
|
data->spi_dev = device_get_binding(config->spi_name);
|
||||||
if (data->spi_dev == NULL) {
|
if (data->spi_dev == NULL) {
|
||||||
LOG_ERR("Could not get SPI device %s", config->spi_name);
|
LOG_ERR("Could not get SPI device %s", config->spi_name);
|
||||||
|
@ -83,25 +94,15 @@ static int ili9340_init(const struct device *dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
data->spi_config.frequency = config->spi_max_freq;
|
data->spi_config.frequency = config->spi_max_freq;
|
||||||
data->spi_config.operation = SPI_OP_MODE_MASTER | SPI_WORD_SET(8);
|
data->spi_config.operation = SPI_OP_MODE_MASTER | SPI_WORD_SET(8U);
|
||||||
data->spi_config.slave = config->spi_addr;
|
data->spi_config.slave = config->spi_addr;
|
||||||
|
|
||||||
data->cs_ctrl.gpio_dev = device_get_binding(config->spi_cs_label);
|
data->cs_ctrl.gpio_dev = device_get_binding(config->spi_cs_label);
|
||||||
if (data->cs_ctrl.gpio_dev) {
|
if (data->cs_ctrl.gpio_dev != NULL) {
|
||||||
data->cs_ctrl.gpio_pin = config->spi_cs_pin;
|
data->cs_ctrl.gpio_pin = config->spi_cs_pin;
|
||||||
data->cs_ctrl.gpio_dt_flags = config->spi_cs_flags;
|
data->cs_ctrl.gpio_dt_flags = config->spi_cs_flags;
|
||||||
data->cs_ctrl.delay = 0U;
|
data->cs_ctrl.delay = 0U;
|
||||||
data->spi_config.cs = &(data->cs_ctrl);
|
data->spi_config.cs = &data->cs_ctrl;
|
||||||
}
|
|
||||||
|
|
||||||
data->reset_gpio = device_get_binding(config->reset_label);
|
|
||||||
if (data->reset_gpio) {
|
|
||||||
r = gpio_pin_configure(data->reset_gpio, config->reset_pin,
|
|
||||||
GPIO_OUTPUT_INACTIVE | config->reset_flags);
|
|
||||||
if (r < 0) {
|
|
||||||
LOG_ERR("Could not configure reset GPIO (%d)", r);
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data->command_data_gpio = device_get_binding(config->cmd_data_label);
|
data->command_data_gpio = device_get_binding(config->cmd_data_label);
|
||||||
|
@ -117,23 +118,24 @@ static int ili9340_init(const struct device *dev)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data->reset_gpio) {
|
data->reset_gpio = device_get_binding(config->reset_label);
|
||||||
LOG_DBG("Resetting display driver");
|
if (data->reset_gpio != NULL) {
|
||||||
k_sleep(K_MSEC(1));
|
r = gpio_pin_configure(data->reset_gpio, config->reset_pin,
|
||||||
gpio_pin_set(data->reset_gpio, config->reset_pin, 1);
|
GPIO_OUTPUT_INACTIVE | config->reset_flags);
|
||||||
k_sleep(K_MSEC(1));
|
if (r < 0) {
|
||||||
gpio_pin_set(data->reset_gpio, config->reset_pin, 0);
|
LOG_ERR("Could not configure reset GPIO (%d)", r);
|
||||||
k_sleep(K_MSEC(5));
|
return r;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_DBG("Initializing LCD");
|
ili9340_hw_reset(dev);
|
||||||
|
|
||||||
r = ili9340_lcd_init(dev);
|
r = ili9340_lcd_init(dev);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
LOG_ERR("Could not initialize LCD (%d)", r);
|
LOG_ERR("Could not initialize LCD (%d)", r);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_DBG("Exiting sleep mode");
|
|
||||||
r = ili9340_exit_sleep(dev);
|
r = ili9340_exit_sleep(dev);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
LOG_ERR("Could not exit sleep mode (%d)", r);
|
LOG_ERR("Could not exit sleep mode (%d)", r);
|
||||||
|
@ -144,21 +146,21 @@ static int ili9340_init(const struct device *dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ili9340_set_mem_area(const struct device *dev, const uint16_t x,
|
static int ili9340_set_mem_area(const struct device *dev, const uint16_t x,
|
||||||
const uint16_t y, const uint16_t w, const uint16_t h)
|
const uint16_t y, const uint16_t w, const uint16_t h)
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
uint16_t spi_data[2];
|
uint16_t spi_data[2];
|
||||||
|
|
||||||
spi_data[0] = sys_cpu_to_be16(x);
|
spi_data[0] = sys_cpu_to_be16(x);
|
||||||
spi_data[1] = sys_cpu_to_be16(x + w - 1);
|
spi_data[1] = sys_cpu_to_be16(x + w - 1U);
|
||||||
r = ili9340_transmit(dev, ILI9340_CMD_COLUMN_ADDR, &spi_data[0], 4);
|
r = ili9340_transmit(dev, ILI9340_CMD_COLUMN_ADDR, &spi_data[0], 4U);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
spi_data[0] = sys_cpu_to_be16(y);
|
spi_data[0] = sys_cpu_to_be16(y);
|
||||||
spi_data[1] = sys_cpu_to_be16(y + h - 1);
|
spi_data[1] = sys_cpu_to_be16(y + h - 1U);
|
||||||
r = ili9340_transmit(dev, ILI9340_CMD_PAGE_ADDR, &spi_data[0], 4);
|
r = ili9340_transmit(dev, ILI9340_CMD_PAGE_ADDR, &spi_data[0], 4U);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
@ -174,16 +176,16 @@ static int ili9340_write(const struct device *dev, const uint16_t x,
|
||||||
struct ili9340_data *data = (struct ili9340_data *)dev->data;
|
struct ili9340_data *data = (struct ili9340_data *)dev->data;
|
||||||
|
|
||||||
int r;
|
int r;
|
||||||
const uint8_t *write_data_start = (uint8_t *) buf;
|
const uint8_t *write_data_start = (const uint8_t *)buf;
|
||||||
struct spi_buf tx_buf;
|
struct spi_buf tx_buf;
|
||||||
struct spi_buf_set tx_bufs;
|
struct spi_buf_set tx_bufs;
|
||||||
uint16_t write_cnt;
|
uint16_t write_cnt;
|
||||||
uint16_t nbr_of_writes;
|
uint16_t nbr_of_writes;
|
||||||
uint16_t write_h;
|
uint16_t write_h;
|
||||||
|
|
||||||
__ASSERT(desc->width <= desc->pitch, "Pitch is smaller then width");
|
__ASSERT(desc->width <= desc->pitch, "Pitch is smaller than width");
|
||||||
__ASSERT((desc->pitch * ILI9340_RGB_SIZE * desc->height)
|
__ASSERT((desc->pitch * ILI9340_RGB_SIZE * desc->height) <= desc->buf_size,
|
||||||
<= desc->buf_size, "Input buffer to small");
|
"Input buffer to small");
|
||||||
|
|
||||||
LOG_DBG("Writing %dx%d (w,h) @ %dx%d (x,y)", desc->width, desc->height,
|
LOG_DBG("Writing %dx%d (w,h) @ %dx%d (x,y)", desc->width, desc->height,
|
||||||
x, y);
|
x, y);
|
||||||
|
@ -201,7 +203,7 @@ static int ili9340_write(const struct device *dev, const uint16_t x,
|
||||||
}
|
}
|
||||||
|
|
||||||
r = ili9340_transmit(dev, ILI9340_CMD_MEM_WRITE,
|
r = ili9340_transmit(dev, ILI9340_CMD_MEM_WRITE,
|
||||||
(void *) write_data_start,
|
write_data_start,
|
||||||
desc->width * ILI9340_RGB_SIZE * write_h);
|
desc->width * ILI9340_RGB_SIZE * write_h);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
return r;
|
return r;
|
||||||
|
@ -220,7 +222,7 @@ static int ili9340_write(const struct device *dev, const uint16_t x,
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
write_data_start += (desc->pitch * ILI9340_RGB_SIZE);
|
write_data_start += desc->pitch * ILI9340_RGB_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -256,7 +258,7 @@ static int ili9340_display_blanking_on(const struct device *dev)
|
||||||
static int ili9340_set_brightness(const struct device *dev,
|
static int ili9340_set_brightness(const struct device *dev,
|
||||||
const uint8_t brightness)
|
const uint8_t brightness)
|
||||||
{
|
{
|
||||||
LOG_WRN("Set brightness not implemented");
|
LOG_ERR("Set brightness not implemented");
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,8 +297,8 @@ static void ili9340_get_capabilities(const struct device *dev,
|
||||||
struct display_capabilities *capabilities)
|
struct display_capabilities *capabilities)
|
||||||
{
|
{
|
||||||
memset(capabilities, 0, sizeof(struct display_capabilities));
|
memset(capabilities, 0, sizeof(struct display_capabilities));
|
||||||
capabilities->x_resolution = 320U;
|
capabilities->x_resolution = ILI9340_X_RES;
|
||||||
capabilities->y_resolution = 240U;
|
capabilities->y_resolution = ILI9340_Y_RES;
|
||||||
#ifdef CONFIG_ILI9340_RGB565
|
#ifdef CONFIG_ILI9340_RGB565
|
||||||
capabilities->supported_pixel_formats = PIXEL_FORMAT_RGB_565;
|
capabilities->supported_pixel_formats = PIXEL_FORMAT_RGB_565;
|
||||||
capabilities->current_pixel_format = PIXEL_FORMAT_RGB_565;
|
capabilities->current_pixel_format = PIXEL_FORMAT_RGB_565;
|
||||||
|
@ -307,28 +309,32 @@ static void ili9340_get_capabilities(const struct device *dev,
|
||||||
capabilities->current_orientation = DISPLAY_ORIENTATION_NORMAL;
|
capabilities->current_orientation = DISPLAY_ORIENTATION_NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ili9340_transmit(const struct device *dev, uint8_t cmd, void *tx_data,
|
int ili9340_transmit(const struct device *dev, uint8_t cmd, const void *tx_data,
|
||||||
size_t tx_len)
|
size_t tx_len)
|
||||||
{
|
{
|
||||||
const struct ili9340_config *config = (struct ili9340_config *)dev->config;
|
const struct ili9340_config *config = (struct ili9340_config *)dev->config;
|
||||||
struct ili9340_data *data = (struct ili9340_data *)dev->data;
|
struct ili9340_data *data = (struct ili9340_data *)dev->data;
|
||||||
|
|
||||||
int r;
|
int r;
|
||||||
struct spi_buf tx_buf = { .buf = &cmd, .len = 1 };
|
struct spi_buf tx_buf;
|
||||||
struct spi_buf_set tx_bufs = { .buffers = &tx_buf, .count = 1 };
|
struct spi_buf_set tx_bufs = { .buffers = &tx_buf, .count = 1U };
|
||||||
|
|
||||||
gpio_pin_set(data->command_data_gpio, config->cmd_data_pin,
|
/* send command */
|
||||||
ILI9340_CMD_DATA_PIN_COMMAND);
|
tx_buf.buf = &cmd;
|
||||||
|
tx_buf.len = 1U;
|
||||||
|
|
||||||
|
gpio_pin_set(data->command_data_gpio, config->cmd_data_pin, ILI9340_CMD);
|
||||||
r = spi_write(data->spi_dev, &data->spi_config, &tx_bufs);
|
r = spi_write(data->spi_dev, &data->spi_config, &tx_bufs);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* send data (if any) */
|
||||||
if (tx_data != NULL) {
|
if (tx_data != NULL) {
|
||||||
tx_buf.buf = tx_data;
|
tx_buf.buf = (void *)tx_data;
|
||||||
tx_buf.len = tx_len;
|
tx_buf.len = tx_len;
|
||||||
gpio_pin_set(data->command_data_gpio, config->cmd_data_pin,
|
|
||||||
ILI9340_CMD_DATA_PIN_DATA);
|
gpio_pin_set(data->command_data_gpio, config->cmd_data_pin, ILI9340_DATA);
|
||||||
r = spi_write(data->spi_dev, &data->spi_config, &tx_bufs);
|
r = spi_write(data->spi_dev, &data->spi_config, &tx_bufs);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
return r;
|
return r;
|
||||||
|
|
|
@ -51,6 +51,26 @@
|
||||||
#define ILI9340_DATA_PIXEL_FORMAT_MCU_18_BIT 0x06
|
#define ILI9340_DATA_PIXEL_FORMAT_MCU_18_BIT 0x06
|
||||||
#define ILI9340_DATA_PIXEL_FORMAT_MCU_16_BIT 0x05
|
#define ILI9340_DATA_PIXEL_FORMAT_MCU_16_BIT 0x05
|
||||||
|
|
||||||
|
/** Command/data GPIO level for commands. */
|
||||||
|
#define ILI9340_CMD 1U
|
||||||
|
/** Command/data GPIO level for data. */
|
||||||
|
#define ILI9340_DATA 0U
|
||||||
|
|
||||||
|
/** Sleep out time (ms), ref. 8.2.12 of ILI9340 manual. */
|
||||||
|
#define ILI9340_SLEEP_OUT_TIME 120
|
||||||
|
|
||||||
|
/** Reset pulse time (ms), ref 15.4 of ILI9340 manual. */
|
||||||
|
#define ILI9340_RESET_PULSE_TIME 1
|
||||||
|
|
||||||
|
/** Reset wait time (ms), ref 15.4 of ILI9340 manual. */
|
||||||
|
#define ILI9340_RESET_WAIT_TIME 5
|
||||||
|
|
||||||
|
/** X resolution (pixels). */
|
||||||
|
#define ILI9340_X_RES 320U
|
||||||
|
|
||||||
|
/** Y resolution (pixels). */
|
||||||
|
#define ILI9340_Y_RES 240U
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send data to ILI9340 display controller
|
* Send data to ILI9340 display controller
|
||||||
*
|
*
|
||||||
|
@ -62,7 +82,7 @@
|
||||||
*
|
*
|
||||||
* @return 0 on success, errno otherwise.
|
* @return 0 on success, errno otherwise.
|
||||||
*/
|
*/
|
||||||
int ili9340_transmit(const struct device *dev, uint8_t cmd, void *tx_data,
|
int ili9340_transmit(const struct device *dev, uint8_t cmd, const void *tx_data,
|
||||||
size_t tx_len);
|
size_t tx_len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -220,21 +220,5 @@ int ili9340_lcd_init(const struct device *dev)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sleep Out */
|
|
||||||
cmd = ILI9340_CMD_EXIT_SLEEP;
|
|
||||||
r = ili9340_transmit(dev, cmd, NULL, 0);
|
|
||||||
if (r < 0) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
k_sleep(K_MSEC(120));
|
|
||||||
|
|
||||||
/* Display Off */
|
|
||||||
cmd = ILI9340_CMD_DISPLAY_OFF;
|
|
||||||
r = ili9340_transmit(dev, cmd, NULL, 0);
|
|
||||||
if (r < 0) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue