MwahMallah
MwahMallah

Reputation: 41

Driver for ILI9163C Display in esp-idf

Driver for ILI9163C Display

Hello, I am working on a very simple driver for the ILI9163C display, and I am new to this kind of development. I am using the ESP32 for communication with the display over SPI (i am using esp-idf). However, I am encountering some issues. The screen shows a mix of colors and appears to flicker every time data is transmitted to it. I need help identifying the cause of this behavior.

SPI Initialization Code

esp_err_t spi_init() {
    esp_err_t ret;

    ret = spi_bus_free(SPI_HOST);

    spi_bus_config_t buscfg = {
        .miso_io_num = -1,   // MISO is not used
        .mosi_io_num = 23,   // MOSI pin
        .sclk_io_num = 18,   // SCK pin 
        .quadwp_io_num = -1,
        .quadhd_io_num = -1
    };
    
    spi_device_interface_config_t devcfg = {
        .clock_speed_hz = SPI_CLK_SPEED,
        .mode = 0,  // SPI mode 0 (CPOL = 0, CPHA = 0)
        .spics_io_num = 5,   // CS pin
        .queue_size = 7,
        .pre_cb = 0    
    };

    ret = spi_bus_initialize(SPI_HOST, &buscfg, SPI_DMA_CH_AUTO);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to initialize SPI bus");
        return ret;
    }

    ret = spi_bus_add_device(SPI_HOST, &devcfg, &spi);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to add SPI device");
        return ret;
    }

    return ESP_OK;
}

SPI Data Sending Code

void spi_send_data(const uint8_t *data, size_t len) {
    spi_transaction_t t = {
        .length = 8 * len,                
        .tx_buffer = data,          
        .rx_buffer = NULL,          // No data to be received
        .flags = 0          
    };
    
    esp_err_t ret = spi_device_transmit(spi, &t);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "SPI transmission failed");
    }
}

Display Initialization Code

void ili9163c_init() {
    // Hardware reset of the display
    gpio_set_level(4, 0);  // Set RESET to low
    vTaskDelay(pdMS_TO_TICKS(10));
    gpio_set_level(4, 1);  // Set RESET to high
    vTaskDelay(pdMS_TO_TICKS(150)); // Wait for the display to recover

    // Exit sleep mode
    ili9163c_send_cmd(ILI9163C_SLPOUT);
    vTaskDelay(pdMS_TO_TICKS(1500)); // Increase delay

    // Set column address (CAS) (set the display area)
    ili9163c_send_cmd(ILI9163C_CASET);
    ili9163c_send_data16(0);
    ili9163c_send_data16(0x7F);

    // Set row address (RAS)
    ili9163c_send_cmd(ILI9163C_RASET);
    ili9163c_send_data16(0);
    ili9163c_send_data16(160);

    // Enable the display
    ili9163c_send_cmd(ILI9163C_DISPON);
    vTaskDelay(pdMS_TO_TICKS(1500)); // Add delay after turning on

    // Activate memory
    ili9163c_send_cmd(ILI9163C_RAMWR);
    vTaskDelay(pdMS_TO_TICKS(1000)); // Add delay after enabling
}

Sending Commands and Data to the Display

void ili9163c_send_cmd(uint8_t cmd) {
    gpio_set_level(5, 0);  // Activate CS before sending command
    spi_send_data(&cmd, 1);    // Send the command
    gpio_set_level(5, 1);  // Deactivate CS after sending
}

void ili9163c_send_data16(uint16_t data) {
    gpio_set_level(5, 0);  // Activate CS before sending data
    uint8_t high = data >> 8;
    uint8_t low = data & 0xFF;
    spi_send_data(&high, 1);
    spi_send_data(&low, 1);
    gpio_set_level(5, 1);  // Deactivate CS after sending
}

Main code

#include "ili9163c.h"
#include "spi_driver.h"
#include "esp_log.h"
#include "driver/gpio.h"

void app_main() {

    esp_err_t ret = spi_init();
    if (ret != ESP_OK) {
        // ESP_LOGE("MAIN", "SPI initialization failed");
        return;
    }

    ili9163c_init();

    for (int x = 5; x < 65; x++) {
        for (int y = 5; y < 65; y++) {
            ili9163c_draw_pixel(x, y, 0x0000);  // Draw pixel with black color
        }
    } 
}

Problem Description

Upvotes: 0

Views: 30

Answers (0)

Related Questions