From 5a5a3d67ca8ce01950540e4e1ea4b4f38c5cdb6b Mon Sep 17 00:00:00 2001 From: Yilin Sun Date: Sat, 16 Mar 2024 01:04:42 +0800 Subject: [PATCH] Initial LCD driver support. Signed-off-by: Yilin Sun --- .drone.yml | 2 +- .gitmodules | 3 ++ CMakeLists.txt | 4 ++ MIMXRT1021xxxxx.mex | 1 + board/pin_mux.c | 5 ++- include/FreeRTOSConfig.h | 2 +- include/app_lcd_impl.h | 11 ++++++ lib/lcd | 1 + src/app_lcd_impl.c | 79 ++++++++++++++++++++++++++++++++++++++++ src/main.c | 35 ++++++++++++++++++ 10 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 include/app_lcd_impl.h create mode 160000 lib/lcd create mode 100644 src/app_lcd_impl.c diff --git a/.drone.yml b/.drone.yml index 6afe870..128f418 100644 --- a/.drone.yml +++ b/.drone.yml @@ -14,4 +14,4 @@ steps: commands: - mkdir build && cd build - cmake -DCMAKE_TOOLCHAIN_FILE=arm-none-eabi.cmake .. - - make fire_rt1021_evk_template_FLASH.elf + - make fire_rt1021_watch_FLASH.elf diff --git a/.gitmodules b/.gitmodules index ce86312..356e576 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "lib/freertos"] path = lib/freertos url = https://github.com/FreeRTOS/FreeRTOS-Kernel.git +[submodule "lib/lcd"] + path = lib/lcd + url = https://git.minori.work/Embedded_Drivers/epd-spi.git diff --git a/CMakeLists.txt b/CMakeLists.txt index bbac910..7a492cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,6 +90,7 @@ set(TARGET_SOURCES "board/dcd.c" "board/peripherals.c" "board/pin_mux.c" + "src/app_lcd_impl.c" "src/main.c" "xip/fire_rt1021_evk_flexspi_nor_config.c" ) @@ -125,6 +126,7 @@ set(TARGET_C_INCLUDES # Shared libraries linked with application set(TARGET_LIBS + "epd-spi" "freertos_kernel" "c" "m" @@ -160,6 +162,8 @@ set(FREERTOS_PORT GCC_ARM_CM4F CACHE STRING "") set(FREERTOS_HEAP "4" CACHE STRING "") add_subdirectory(lib/freertos) +add_subdirectory(lib/lcd) + # Shared sources, includes and definitions add_compile_definitions(${TARGET_C_DEFINES}) include_directories(${TARGET_C_INCLUDES}) diff --git a/MIMXRT1021xxxxx.mex b/MIMXRT1021xxxxx.mex index 83203d1..a058b3d 100644 --- a/MIMXRT1021xxxxx.mex +++ b/MIMXRT1021xxxxx.mex @@ -163,6 +163,7 @@ + diff --git a/board/pin_mux.c b/board/pin_mux.c index 8b87e8b..5a4937a 100644 --- a/board/pin_mux.c +++ b/board/pin_mux.c @@ -123,7 +123,8 @@ void BOARD_InitLCDTPPins(void) { BOARD_InitLEDPins: - options: {callFromInitBoot: 'true', coreID: core0, enableClock: 'true'} - pin_list: - - {pin_num: '76', peripheral: GPIO1, signal: 'gpio_io, 29', pin_signal: GPIO_AD_B1_13, direction: OUTPUT, gpio_init_state: 'true', open_drain: Enable, speed: MHZ_50} + - {pin_num: '76', peripheral: GPIO1, signal: 'gpio_io, 29', pin_signal: GPIO_AD_B1_13, direction: OUTPUT, gpio_init_state: 'true', open_drain: Enable, speed: MHZ_50, + pull_keeper_enable: Disable} * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS *********** */ @@ -146,7 +147,7 @@ void BOARD_InitLEDPins(void) { GPIO_PinInit(GPIO1, 29U, &LEDA_config); IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B1_13_GPIO1_IO29, 0U); - IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_13_GPIO1_IO29, 0x1830U); + IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_13_GPIO1_IO29, 0x0830U); } /*********************************************************************************************************************** * EOF diff --git a/include/FreeRTOSConfig.h b/include/FreeRTOSConfig.h index 2bf04e0..e779be0 100644 --- a/include/FreeRTOSConfig.h +++ b/include/FreeRTOSConfig.h @@ -65,7 +65,7 @@ /* Memory allocation related definitions. */ #define configSUPPORT_STATIC_ALLOCATION 0 #define configSUPPORT_DYNAMIC_ALLOCATION 1 -#define configTOTAL_HEAP_SIZE ((size_t)(48 * 1024)) +#define configTOTAL_HEAP_SIZE ((size_t)(32 * 1024)) #define configAPPLICATION_ALLOCATED_HEAP 0 /* Hook function related definitions. */ diff --git a/include/app_lcd_impl.h b/include/app_lcd_impl.h new file mode 100644 index 0000000..5bb4db4 --- /dev/null +++ b/include/app_lcd_impl.h @@ -0,0 +1,11 @@ +#ifndef APP_LCD_IMPL_H +#define APP_LCD_IMPL_H + +#include "epd-spi/epd_common.h" + +int app_lcd_impl_init(void *handle); +epd_ret_t app_lcd_impl_write_command(void *handle, uint8_t *command, uint32_t len); +epd_ret_t app_lcd_impl_write_data(void *handle, const uint8_t *data, uint32_t len); +epd_ret_t app_lcd_impl_delay(void *handle, uint32_t msec); + +#endif // APP_LCD_IMPL_H diff --git a/lib/lcd b/lib/lcd new file mode 160000 index 0000000..9fe29d3 --- /dev/null +++ b/lib/lcd @@ -0,0 +1 @@ +Subproject commit 9fe29d33414911a8ae6ca0a347d1bd9af05be3b4 diff --git a/src/app_lcd_impl.c b/src/app_lcd_impl.c new file mode 100644 index 0000000..9e91d40 --- /dev/null +++ b/src/app_lcd_impl.c @@ -0,0 +1,79 @@ +/* FreeRTOS */ +#include "FreeRTOS.h" +#include "task.h" + +/* SDK drivers */ +#include "fsl_lpspi.h" + +/* Board */ +#include "board.h" +#include "clock_config.h" +#include "peripherals.h" +#include "pin_mux.h" + +/* LCD */ +#include "app_lcd_impl.h" + +int app_lcd_impl_init(void *handle) { + CLOCK_EnableClock(kCLOCK_Lpspi4); + + lpspi_master_config_t spi_cfg; + LPSPI_MasterGetDefaultConfig(&spi_cfg); + + spi_cfg.baudRate = 24000000UL; + + LPSPI_MasterInit(LPSPI4, &spi_cfg, CLOCK_GetClockRootFreq(kCLOCK_LpspiClkRoot)); + + return 0; +} + +epd_ret_t app_lcd_impl_write_command(void *handle, uint8_t *command, uint32_t len) { + lpspi_transfer_t xfer = { + .txData = command, + .dataSize = 1U, + .configFlags = kLPSPI_MasterPcsContinuous, + }; + + GPIO_WritePinOutput(BOARD_LCD_DC_GPIO, BOARD_INITLCDSPIPINS_DC_PIN, 0U); + + status_t ret = LPSPI_MasterTransferBlocking(LPSPI4, &xfer); + if (ret != kStatus_Success) { + return EPD_FAIL; + } + + if (len > 1) { + GPIO_WritePinOutput(BOARD_LCD_DC_GPIO, BOARD_INITLCDSPIPINS_DC_PIN, 1U); + xfer.txData = &command[1]; + xfer.dataSize = len - 1; + + ret = LPSPI_MasterTransferBlocking(LPSPI4, &xfer); + if (ret != kStatus_Success) { + return EPD_FAIL; + } + } + + return EPD_OK; +} + +epd_ret_t app_lcd_impl_write_data(void *handle, const uint8_t *data, uint32_t len) { + lpspi_transfer_t xfer = { + .txData = (uint8_t *)data, + .dataSize = len, + .configFlags = kLPSPI_MasterPcsContinuous, + }; + + GPIO_WritePinOutput(BOARD_LCD_DC_GPIO, BOARD_INITLCDSPIPINS_DC_PIN, 1U); + + status_t ret = LPSPI_MasterTransferBlocking(LPSPI4, &xfer); + if (ret != kStatus_Success) { + return EPD_FAIL; + } + + return EPD_OK; +} + +epd_ret_t app_lcd_impl_delay(void *handle, uint32_t msec) { + vTaskDelay(pdMS_TO_TICKS(msec)); + + return EPD_OK; +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index e47ef7a..d662c98 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,14 @@ #include "FreeRTOS.h" #include "task.h" +/* LCD */ +#include "epd-spi/panel/lcd_tk0096f611.h" + +/* App */ +#include "app_lcd_impl.h" + +static uint16_t s_display_buf[160 * 80]; + static void app_task_initialization(void *arguments); int main(void) { @@ -40,6 +48,33 @@ dead_loop: } static void app_task_initialization(void *arguments) { + lcd_st7789_t lcd = { + .cb = + { + .write_command_cb = app_lcd_impl_write_command, + .write_data_cb = app_lcd_impl_write_data, + .delay_cb = app_lcd_impl_delay, + }, + }; + + app_lcd_impl_init(NULL); + + lcd_st7789_init(&lcd, &lcd_tk0096_panel_config); + lcd_st7789_set_direction(&lcd, LCD_ST7789_DIR_90); + lcd_st7789_set_pixel_format(&lcd, LCD_ST7789_RGB565); + lcd_st7789_enable_display(&lcd, true); + + memset(s_display_buf, 0xDD, sizeof(s_display_buf)); + + epd_coord_t coord = { + .x_start = 0, + .x_end = 159, + .y_start = 0, + .y_end = 79, + }; + + lcd_st7789_load(&lcd, &coord, (const uint8_t *)s_display_buf); + for (;;) { GPIO_PortToggle(BOARD_INITLEDPINS_LEDA_GPIO, BOARD_INITLEDPINS_LEDA_PIN_MASK); vTaskDelay(pdMS_TO_TICKS(250));