From ef6c473b93755ce5e17d0f5966d31bf0a05558e5 Mon Sep 17 00:00:00 2001 From: rkcf Date: Wed, 1 Feb 2023 04:07:42 -0600 Subject: [PATCH] add ili9341 lcd panel init function --- README.org | 15 +++++++++ main/esp32-sequencer.c | 70 +++++++++++++++++++++++++++++++++++++++++- main/idf_component.yml | 17 ++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 main/idf_component.yml diff --git a/README.org b/README.org index a786915..cfca22e 100644 --- a/README.org +++ b/README.org @@ -1,2 +1,17 @@ * ESP32 Sequencer A basic step sequencer based on the esp32. + +** TFT LCD Screen +Pinout +| LCD Pin | ESP32 Pin | Function | +|---------+-----------+-----------| +| 1 | | VCC (3V3) | +| 2 | | GND | +| 3 | 36 | CS | +| 4 | 37 | RESET | +| 5 | 38 | DC/RS | +| 6 | 39 | SDI/MOSI | +| 7 | 40 | SCK | +| 8 | 41 | LED | +| 9 | 42 | SDO/MISO | + diff --git a/main/esp32-sequencer.c b/main/esp32-sequencer.c index 8f75c7c..601568d 100644 --- a/main/esp32-sequencer.c +++ b/main/esp32-sequencer.c @@ -4,8 +4,10 @@ #include "driver/gpio.h" #include "driver/gptimer.h" #include "driver/ledc.h" +#include "driver/spi_common.h" #include "esp_attr.h" #include "esp_err.h" +#include "esp_lcd_types.h" #include "esp_log.h" #include "esp_random.h" #include "freertos/FreeRTOS.h" @@ -13,10 +15,16 @@ #include "freertos/queue.h" #include "freertos/task.h" #include "hal/gpio_types.h" +#include "hal/lcd_types.h" #include "hal/ledc_types.h" +#include "hal/spi_types.h" #include "hal/timer_types.h" #include "soc/clk_tree_defs.h" - +#include "driver/spi_master.h" +#include "esp_lcd_panel_io.h" +#include "esp_lcd_panel_vendor.h" +#include "esp_lcd_panel_ops.h" +#include "esp_lcd_ili9341.h" /* Inputs */ // Clock In @@ -34,6 +42,18 @@ static QueueHandle_t control_event_queue = NULL; #define LEDC_CLK_OUT (5) #define LEDC_GATE_OUT (6) +/* SPI LCD Config and Pins */ +#define LCD_V_RES 320 +#define LCD_H_RES 240 + +#define LCD_SCLK_PIN 40 +#define LCD_MOSI_PIN 39 +#define LCD_MISO_PIN 42 +#define LCD_DC_PIN 38 +#define LCD_RST_PIN 37 +#define LCD_CS_PIN 36 +#define LCD_BK_LIGHT_PIN 41 + /* Timers */ static gptimer_handle_t tap_tempo_timer = NULL; static gptimer_handle_t gate_timer = NULL; @@ -238,6 +258,52 @@ static void ledc_init(void) { ESP_ERROR_CHECK(ledc_channel_config(&ledc_gate_channel)); } +static void lcd_init(void) { + ESP_LOGI(TAG, "Initialing LCD"); + // Configure backlight + gpio_reset_pin(LCD_BK_LIGHT_PIN); + gpio_set_direction(LCD_BK_LIGHT_PIN, GPIO_MODE_OUTPUT); + gpio_set_level(LCD_BK_LIGHT_PIN, 1); + + // Setup SPI Bus + spi_bus_config_t spi_cfg = { + .sclk_io_num = LCD_SCLK_PIN, + .mosi_io_num = LCD_MOSI_PIN, + .miso_io_num = LCD_MISO_PIN, + .quadhd_io_num = -1, + .quadwp_io_num = -1, + .max_transfer_sz = LCD_H_RES * 80 * sizeof(uint16_t) + }; + ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &spi_cfg, SPI_DMA_CH_AUTO)); + + // Setup Panel IO + esp_lcd_panel_io_handle_t lcd_io_handle = NULL; + esp_lcd_panel_io_spi_config_t lcd_spi_config = { + .dc_gpio_num = LCD_DC_PIN, + .cs_gpio_num = LCD_CS_PIN, + .pclk_hz = 20 * 1000 * 1000, + .lcd_cmd_bits = 8, + .lcd_param_bits = 8, + .spi_mode = 0, + .trans_queue_depth = 10 + }; + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SPI2_HOST, &lcd_spi_config, &lcd_io_handle)); + + // Setup ili9341 panel driver + esp_lcd_panel_handle_t lcd_handle = NULL; + esp_lcd_panel_dev_config_t lcd_config = { + .reset_gpio_num = LCD_RST_PIN, + .rgb_endian = LCD_RGB_ENDIAN_BGR, + .bits_per_pixel = 16 + }; + ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(lcd_io_handle, &lcd_config, &lcd_handle)); + ESP_ERROR_CHECK(esp_lcd_panel_reset(lcd_handle)); + ESP_ERROR_CHECK(esp_lcd_panel_init(lcd_handle)); + + // Turn on the panel + ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(lcd_handle, true)); +} + static void pulse_cv(bool triggered) { ESP_ERROR_CHECK(gptimer_start(gate_timer)); ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 4095)); @@ -255,6 +321,8 @@ void app_main(void) { /* Initialize GPIO */ gpio_init(); controls_init(); + /* Initialize LCD */ + lcd_init(); // Start task to handle control events xTaskCreate(control_event_handler, "control event handler", 2048, NULL, 10, NULL); diff --git a/main/idf_component.yml b/main/idf_component.yml new file mode 100644 index 0000000..3619054 --- /dev/null +++ b/main/idf_component.yml @@ -0,0 +1,17 @@ +## IDF Component Manager Manifest File +dependencies: + espressif/esp_lcd_ili9341: "*" + ## Required IDF version + idf: + version: ">=4.1.0" + # # Put list of dependencies here + # # For components maintained by Espressif: + # component: "~1.0.0" + # # For 3rd party components: + # username/component: ">=1.0.0,<2.0.0" + # username2/component2: + # version: "~1.0.0" + # # For transient dependencies `public` flag can be set. + # # `public` flag doesn't have an effect dependencies of the `main` component. + # # All dependencies of `main` are public by default. + # public: true