UsefulElectronics / esp32-ssd1306-lvgl-monochrome

ESP32 Driving SSD1306 OLED LCD and Running LVGL Library
5 stars 1 forks source link

SSD1306 Display with SPI Communication Problem. #1

Open VigneshVicky97 opened 1 week ago

VigneshVicky97 commented 1 week ago

Hi @UsefulElectronics , I want to connect SSD1306 (128X64) display with ESP32 using SPI communication. The LVGL already has I2C communication for this particular Display. I have modified that code to communicate using SPI. But, I am not able to get the output. I don't know what I am wrong. I will attach my code . Can someone please help me to get correct output?

/*
 * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

/* INCLUDES ------------------------------------------------------------------*/

#include "driver/gpio.h"
#include "driver/spi_master.h"

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_timer.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_log.h"
#include "lvgl.h"
#include "esp_lvgl_port.h"
//#include "spi_oled_example_main.h"

#include "esp_lcd_panel_vendor.h"

static const char *TAG = "SSD1306_SPI";

/* MACROS --------------------------------------------------------------------*/
// Using SPI2 in the example
#define LCD_HOST  SPI2_HOST

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define EXAMPLE_LCD_PIXEL_CLOCK_HZ     (2 * 1000 * 1000)

#define GPIO_SCLK                      18
#define GPIO_MOSI                      23
#define GPIO_DC                        27
#define GPIO_RESET                     32
#define GPIO_CS                        5

#define EXAMPLE_LCD_H_RES              128
#define EXAMPLE_LCD_V_RES              64

#define EXAMPLE_LCD_CMD_BITS           8
#define EXAMPLE_LCD_PARAM_BITS         8

extern void example_lvgl_demo_ui(lv_disp_t *disp);

/* The LVGL port component calls esp_lcd_panel_draw_bitmap API for send data to the screen. There must be called
lvgl_port_flush_ready(disp) after each transaction to display. The best way is to use on_color_trans_done
callback from esp_lcd IO config structure. In IDF 5.1 and higher, it is solved inside LVGL port component. */
static bool notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
{
    lv_disp_t * disp = (lv_disp_t *)user_ctx;
    lvgl_port_flush_ready(disp);
    return false;
}

void app_main(void)
{
    /* SPI Bus Initialisation --------------------------------------------------------------------*/
    esp_err_t ret;

//  gpio_reset_pin( GPIO_CS );
//  gpio_set_direction( GPIO_CS, GPIO_MODE_OUTPUT );
//  gpio_set_level( GPIO_CS, 0 );
//
//  gpio_reset_pin( GPIO_DC );
//  gpio_set_direction( GPIO_DC, GPIO_MODE_OUTPUT );
//  gpio_set_level( GPIO_DC, 0 );
//
//  if ( GPIO_RESET >= 0 ) {
//      gpio_reset_pin( GPIO_RESET );
//      gpio_set_direction( GPIO_RESET, GPIO_MODE_OUTPUT );
//      gpio_set_level( GPIO_RESET, 0 );
//      vTaskDelay( pdMS_TO_TICKS( 100 ) );
//      gpio_set_level( GPIO_RESET, 1 );
//  }

    spi_bus_config_t spi_bus_config = {
        .mosi_io_num = GPIO_MOSI,
        .miso_io_num = -1,
        .sclk_io_num = GPIO_SCLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
//      .max_transfer_sz = 0,
        .max_transfer_sz = EXAMPLE_LCD_H_RES * 80 * sizeof(uint16_t),
        .flags = 0
    };

    ESP_LOGI(TAG, "SPI HOST_ID=%d", LCD_HOST);
    ret = spi_bus_initialize( LCD_HOST, &spi_bus_config, SPI_DMA_CH_AUTO );
    ESP_LOGI(TAG, "spi_bus_initialize=%d",ret);
    assert(ret==ESP_OK);
    /*  --------------------------------------------------------------------*/

    /* Panel IO Initialisation --------------------------------------------------------------------*/
    ESP_LOGI(TAG, "Install panel IO");
    esp_lcd_panel_io_handle_t io_handle = NULL;
    esp_lcd_panel_io_spi_config_t io_config = {
        .dc_gpio_num = GPIO_DC,
        .cs_gpio_num = GPIO_CS,
        .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
        .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
        .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
        .spi_mode = 0,
        .trans_queue_depth = 10,
    };
    // Attach the LCD to the SPI bus
    ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle));
    /* --------------------------------------------------------------------*/

    /* SSD1306 Panel Driver Initialisation --------------------------------------------------------------------*/
    ESP_LOGI(TAG, "Install SSD1306 panel driver");
    esp_lcd_panel_handle_t panel_handle = NULL;
    esp_lcd_panel_dev_config_t panel_config = {
        .bits_per_pixel = 1,
        .reset_gpio_num = GPIO_RESET,
    };

    ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io_handle, &panel_config, &panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
    /* --------------------------------------------------------------------*/

    /* LVGL Library Initialisation --------------------------------------------------------------------*/
    ESP_LOGI(TAG, "Initialize LVGL");
    const lvgl_port_cfg_t lvgl_cfg = ESP_LVGL_PORT_INIT_CONFIG();
    lvgl_port_init(&lvgl_cfg);

    const lvgl_port_display_cfg_t disp_cfg = {
        .io_handle = io_handle,
        .panel_handle = panel_handle,
        .buffer_size = EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES,
        .double_buffer = true,
        .hres = EXAMPLE_LCD_H_RES,
        .vres = EXAMPLE_LCD_V_RES,
        .monochrome = true,
        .rotation = {
            .swap_xy = false,
            .mirror_x = false,
            .mirror_y = false,
        }
    };
    lv_disp_t * disp = lvgl_port_add_disp(&disp_cfg);
    /* Register done callback for IO */
    const esp_lcd_panel_io_callbacks_t cbs = {
        .on_color_trans_done = notify_lvgl_flush_ready,
    };
    esp_lcd_panel_io_register_event_callbacks(io_handle, &cbs, disp);

    /* Rotation of the screen */
    lv_disp_set_rotation(disp, LV_DISP_ROT_NONE);

    ESP_LOGI(TAG, "Display LVGL Scroll Text");
    example_lvgl_demo_ui(disp);
}

I used this library to get output in my SSD1306 Display using SPI communication. Please help me to display using LVGL library in my display.

ucflumm commented 5 days ago

is that a main file? its not even the same code its not relevant to here. if that is a main file there is no tasks?