initial commit

This commit is contained in:
Ken Van Hoeylandt
2023-12-25 12:42:06 +01:00
commit 6fd12b2160
24 changed files with 1166 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
idf_component_register(
SRC_DIRS "."
INCLUDE_DIRS "."
REQUIRES nanobake esp_lcd_touch_cst816s esp_lcd_ili9341
)
@@ -0,0 +1,7 @@
#ifndef NANOBAKE_BOARD_2432S024_H
#define NANOBAKE_BOARD_2432S024_H
#include "board_2432s024_display.h"
#include "board_2432s024_touch.h"
#endif // NANOBAKE_BOARD_2432S024_H
@@ -0,0 +1,123 @@
#include "board_2432s024_display.h"
#include <esp_lcd_ili9341.h>
#include <esp_log.h>
#include <esp_err.h>
#include <esp_check.h>
#include <esp_lcd_panel_ops.h>
#include <driver/gpio.h>
#include <driver/spi_master.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
static const char* TAG = "ili9341";
static SemaphoreHandle_t refresh_finish = NULL;
#define SELECTED_SPI_HOST SPI2_HOST
#define PIN_SCLK GPIO_NUM_14
#define PIN_MOSI GPIO_NUM_13
#define PIN_CS GPIO_NUM_15
#define PIN_DC GPIO_NUM_2
#define PIN_BACKLIGHT GPIO_NUM_27
#define HORIZONTAL_RESOLUTION 240
#define VERTICAL_RESOLUTION 320
#define BITS_PER_PIXEL 16
#define DRAW_BUFFER_HEIGHT 80
IRAM_ATTR static bool test_notify_refresh_ready(esp_lcd_panel_io_handle_t io_handle, esp_lcd_panel_io_event_data_t* edata, void* user_ctx) {
BaseType_t need_yield = pdFALSE;
xSemaphoreGiveFromISR(refresh_finish, &need_yield);
return (need_yield == pdTRUE);
}
static esp_err_t ili9341_create_display(nb_display_t* display) {
ESP_LOGI(TAG, "init started");
gpio_config_t io_conf = {
.pin_bit_mask = BIT64(PIN_BACKLIGHT),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&io_conf);
const spi_bus_config_t bus_config = ILI9341_PANEL_BUS_SPI_CONFIG(
PIN_SCLK,
PIN_MOSI,
HORIZONTAL_RESOLUTION * DRAW_BUFFER_HEIGHT * (BITS_PER_PIXEL / 8)
);
ESP_RETURN_ON_ERROR(
spi_bus_initialize(SELECTED_SPI_HOST, &bus_config, SPI_DMA_CH_AUTO),
TAG,
"spi bus init failed"
);
const esp_lcd_panel_io_spi_config_t panel_io_config = ILI9341_PANEL_IO_SPI_CONFIG(
PIN_CS,
PIN_DC,
test_notify_refresh_ready,
NULL
);
ESP_RETURN_ON_ERROR(
esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SELECTED_SPI_HOST, &panel_io_config, &display->io_handle),
TAG,
"failed to create panel"
);
ESP_LOGI(TAG, "install driver");
const esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = GPIO_NUM_NC,
.rgb_endian = LCD_RGB_ENDIAN_RGB,
.bits_per_pixel = BITS_PER_PIXEL,
};
ESP_RETURN_ON_ERROR(
esp_lcd_new_panel_ili9341(display->io_handle, &panel_config, &display->display_handle),
TAG,
"failed to create ili9341"
);
ESP_RETURN_ON_ERROR(
esp_lcd_panel_reset(display->display_handle),
TAG,
"failed to reset panel"
);
ESP_RETURN_ON_ERROR(
esp_lcd_panel_init(display->display_handle),
TAG,
"failed to init panel"
);
ESP_RETURN_ON_ERROR(
esp_lcd_panel_mirror(display->display_handle, true, false),
TAG,
"failed to set panel to mirror"
);
ESP_RETURN_ON_ERROR(
esp_lcd_panel_disp_on_off(display->display_handle, true),
TAG,
"failed to turn display on"
);
ESP_RETURN_ON_ERROR(
gpio_set_level(PIN_BACKLIGHT, 1),
TAG,
"failed to turn backlight on"
);
display->horizontal_resolution = HORIZONTAL_RESOLUTION;
display->vertical_resolution = VERTICAL_RESOLUTION;
display->draw_buffer_height = DRAW_BUFFER_HEIGHT;
display->bits_per_pixel = BITS_PER_PIXEL;
return ESP_OK;
}
nb_display_driver_t board_2432s024_create_display_driver() {
nb_display_driver_t driver = {
.name = "ili9341_2432s024",
.create_display = &ili9341_create_display
};
return driver;
}
@@ -0,0 +1,8 @@
#ifndef NANOBAKE_BOARD_2432S024_DISPLAY_H
#define NANOBAKE_BOARD_2432S024_DISPLAY_H
#include <nb_display.h>
extern nb_display_driver_t board_2432s024_create_display_driver();
#endif //NANOBAKE_BOARD_2432S024_DISPLAY_H
@@ -0,0 +1,62 @@
#include "board_2432s024_touch.h"
#include <esp_lcd_touch_cst816s.h>
#include <esp_err.h>
#include <driver/i2c.h>
#include <esp_check.h>
#define CST816_I2C_PORT (0)
const char* TAG = "cst816";
static esp_err_t cst816_init_io(esp_lcd_panel_io_handle_t* io_handle) {
// Init I2C
const i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_33,
.sda_pullup_en = GPIO_PULLUP_DISABLE,
.scl_io_num = GPIO_NUM_32,
.scl_pullup_en = GPIO_PULLUP_DISABLE,
.master.clk_speed = 400000
};
ESP_RETURN_ON_ERROR(i2c_param_config(CST816_I2C_PORT, &i2c_conf), TAG, "i2c config failed");
ESP_RETURN_ON_ERROR(i2c_driver_install(CST816_I2C_PORT, i2c_conf.mode, 0, 0, 0), TAG, "i2c driver install failed");
// Configure I2C
const esp_lcd_panel_io_i2c_config_t touch_io_config = ESP_LCD_TOUCH_IO_I2C_CST816S_CONFIG();
ESP_RETURN_ON_ERROR(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)CST816_I2C_PORT, &touch_io_config, io_handle), TAG, "esp_lcd_panel creation failed");
return ESP_OK;
}
static esp_err_t cst816_create_touch(esp_lcd_panel_io_handle_t io_handle, esp_lcd_touch_handle_t* touch_handle) {
// Configure touch
esp_lcd_touch_config_t config = {
.x_max = 240,
.y_max = 320,
.rst_gpio_num = GPIO_NUM_25,
.int_gpio_num = GPIO_NUM_21,
.levels = {
.reset = 0,
.interrupt = 0,
},
.flags = {
.swap_xy = 0,
.mirror_x = 0,
.mirror_y = 0,
},
.interrupt_callback = NULL,
};
// Init touch
return esp_lcd_touch_new_i2c_cst816s(io_handle, &config, touch_handle);
}
nb_touch_driver_t board_2432s024_create_touch_driver() {
nb_touch_driver_t driver = {
.name = "cst816s_2432s024",
.init_io = cst816_init_io,
.create_touch = &cst816_create_touch
};
return driver;
}
@@ -0,0 +1,8 @@
#ifndef NANOBAKE_BOARD_2432S024_TOUCH_H
#define NANOBAKE_BOARD_2432S024_TOUCH_H
#include <nb_touch.h>
nb_touch_driver_t board_2432s024_create_touch_driver();
#endif // NANOBAKE_BOARD_2432S024_TOUCH_H
+6
View File
@@ -0,0 +1,6 @@
idf_component_register(
SRC_DIRS "src"
INCLUDE_DIRS "inc"
PRIV_INCLUDE_DIRS "src"
REQUIRES esp_lcd esp_lcd_touch driver
)
+6
View File
@@ -0,0 +1,6 @@
#ifndef NANOBAKE_H
#define NANOBAKE_H
#include "nb_platform.h"
#endif //NANOBAKE_H
+27
View File
@@ -0,0 +1,27 @@
#ifndef NANOBAKE_NB_DISPLAY_H
#define NANOBAKE_NB_DISPLAY_H
#include <esp_lcd_panel_io.h>
typedef struct nb_display nb_display_t;
struct nb_display {
bool io_initialized;
uint16_t horizontal_resolution;
uint16_t vertical_resolution;
uint16_t draw_buffer_height;
uint16_t bits_per_pixel;
esp_lcd_panel_io_handle_t io_handle;
esp_lcd_panel_handle_t display_handle;
};
typedef struct nb_display_driver nb_display_driver_t;
struct nb_display_driver {
const char name[32];
esp_err_t (*create_display)(nb_display_t* display);
};
extern esp_err_t nb_display_create(nb_display_driver_t driver, nb_display_t* display);
#endif // NANOBAKE_NB_DISPLAY_H
+22
View File
@@ -0,0 +1,22 @@
#ifndef NANOBAKE_NB_PLATFORM_H
#define NANOBAKE_NB_PLATFORM_H
#include "nb_display.h"
#include "nb_touch.h"
#include <esp_err.h>
typedef struct nb_platform_config nb_platform_config_t;
struct nb_platform_config {
nb_display_driver_t display_driver;
nb_touch_driver_t touch_driver;
};
typedef struct nb_platform nb_platform_t;
struct nb_platform {
nb_display_t display;
nb_touch_t touch;
};
esp_err_t nb_platform_create(nb_platform_config_t config, nb_platform_t* platform);
#endif // NANOBAKE_NB_PLATFORM_H
+24
View File
@@ -0,0 +1,24 @@
#ifndef NANOBAKE_NB_TOUCH_H
#define NANOBAKE_NB_TOUCH_H
#include "esp_lcd_touch.h"
#include <esp_lcd_panel_io.h>
typedef struct nb_touch_driver nb_touch_driver_t;
struct nb_touch_driver {
const char name[32];
esp_err_t (*init_io)(esp_lcd_panel_io_handle_t* io_handle);
esp_err_t (*create_touch)(esp_lcd_panel_io_handle_t io_handle, esp_lcd_touch_handle_t* touch_handle);
};
typedef struct nb_touch nb_touch_t;
struct nb_touch {
esp_lcd_panel_io_handle_t io_handle;
esp_lcd_touch_handle_t touch_handle;
};
esp_err_t nb_touch_create(nb_touch_driver_t driver, nb_touch_t* touch);
#endif // NANOBAKE_NB_TOUCH_H
+9
View File
@@ -0,0 +1,9 @@
#include "nb_display.h"
#include "nb_internal.h"
#include <esp_check.h>
#include <esp_log.h>
esp_err_t nb_display_create(nb_display_driver_t driver, nb_display_t* display) {
ESP_RETURN_ON_ERROR(driver.create_display(display), nbi_tag, "failed to create driver");
return ESP_OK;
}
+3
View File
@@ -0,0 +1,3 @@
#include "nb_internal.h"
const char* nbi_tag = "nanobake";
+6
View File
@@ -0,0 +1,6 @@
#ifndef NANOBAKE_NB_INTERNAL_H
#define NANOBAKE_NB_INTERNAL_H
extern const char* nbi_tag;
#endif // NANOBAKE_NB_INTERNAL_H
+25
View File
@@ -0,0 +1,25 @@
#include "nb_platform.h"
#include <esp_check.h>
#include "nb_display.h"
#include "nb_touch.h"
#include "nb_internal.h"
static const char* TAG = "nb_platform";
esp_err_t nb_platform_create(nb_platform_config_t config, nb_platform_t* platform) {
ESP_LOGI(TAG, "display with driver %s", config.display_driver.name);
ESP_RETURN_ON_ERROR(
nb_display_create(config.display_driver, &(platform->display)),
nbi_tag,
"display driver init failed"
);
ESP_LOGI(TAG, "touch with driver %s", config.touch_driver.name);
ESP_RETURN_ON_ERROR(
nb_touch_create(config.touch_driver, &(platform->touch)),
nbi_tag,
"touch driver init failed"
);
return ESP_OK;
}
+9
View File
@@ -0,0 +1,9 @@
#include "nb_touch.h"
#include <esp_check.h>
#include "nb_internal.h"
esp_err_t nb_touch_create(nb_touch_driver_t driver, nb_touch_t* touch) {
ESP_RETURN_ON_ERROR(driver.init_io(&(touch->io_handle)), nbi_tag, "failed to init io");
ESP_RETURN_ON_ERROR(driver.create_touch(touch->io_handle, &(touch->touch_handle)), nbi_tag, "failed to create driver");
return ESP_OK;
}