Skip to content

Commit

Permalink
init lvgl core
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanperdung committed May 4, 2024
1 parent 21f65c2 commit a5a03b6
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ include_directories("lvgl")
include_directories("spi")
include_directories("timer")
include_directories("uart")
include_directories("hal")
project(swatch C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
Expand All @@ -19,6 +20,7 @@ add_subdirectory(lvgl)
add_subdirectory(spi)
add_subdirectory(timer)
add_subdirectory(uart)
add_subdirectory(hal)
pico_add_extra_outputs(${PROJECT_NAME})
pico_enable_stdio_uart(${PROJECT_NAME} 1)
target_link_libraries(${PROJECT_NAME} pico_stdlib sw_clock sw_gpio sw_lcd lvgl sw_spi sw_timer sw_uart)
target_link_libraries(${PROJECT_NAME} pico_stdlib sw_clock sw_gpio sw_lcd lvgl hal sw_spi sw_timer sw_uart)
2 changes: 2 additions & 0 deletions hal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_library(hal hal_lvgl.c)
target_link_libraries(hal sw_spi lvgl)
7 changes: 7 additions & 0 deletions hal/hal_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _HAL_COMMON_H_
#define _HAL_COMMON_H_

#define DRAW_BUFFER_RATIO (1.0 / 10)
#define USE_SECOND_DRAW_BUFFER

#endif
64 changes: 64 additions & 0 deletions hal/hal_lvgl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/dma.h"
#include "../timer/sw_timer.h"
#include "../lcd/sw_lcd.h"
#include "../lvgl/lvgl.h"
#include "../spi/sw_spi.h"
#include "hal_common.h"

static struct repeating_timer lvgl_timer;
extern uint dma_tx;
extern dma_channel_config dma_cfg;
uint16_t *draw_buf1 = NULL;
uint16_t *draw_buf2 = NULL;
static lv_display_t *disp = NULL;

/*
Refresh image by transferring the color data to the SPI bus by DMA
*/
void hal_display_flush_cb(lv_display_t *disp, const lv_area_t *area, uint8_t *px_map)
{
lcd_frame frame = {{area->x1, area->y1}, {area->x2, area->y2}};

sw_lcd_set_screen(frame);
sw_spi_dma_write_byte(px_map, ((area->x2 + 1 - area->x1) * (area->y2 + 1 - area->y1)) * 2, true);
}

void sw_dma_irq_handler(void)
{
if (dma_channel_get_irq0_status(dma_tx))
{
dma_channel_acknowledge_irq0(dma_tx);
lv_display_flush_ready(disp);
}
}

static bool hal_repeating_lvgl_timer_callback(struct repeating_timer *timer)
{
lv_tick_inc(5);
return true;
}

void hal_lvgl_init(void)
{
draw_buf1 = (uint16_t *)malloc(LCD_HEIGHT * LCD_WIDTH * DRAW_BUFFER_RATIO);
#if defined(USE_SECOND_DRAW_BUFFER)
draw_buf2 = (uint16_t *)malloc(LCD_HEIGHT * LCD_WIDTH * DRAW_BUFFER_RATIO);
#endif
// Init timer for LVGL
sw_repeating_timer_init(-5, hal_repeating_lvgl_timer_callback, NULL, &lvgl_timer);

// Init LVGL core
lv_init();

// Init LVGL display
disp = lv_display_create(LCD_WIDTH, LCD_HEIGHT);
lv_display_set_flush_cb(disp, hal_display_flush_cb);
lv_display_set_buffers(disp, draw_buf1, draw_buf2, sizeof(draw_buf1), LV_DISPLAY_RENDER_MODE_PARTIAL);
}

void hal_widget_init(void)
{

}
6 changes: 6 additions & 0 deletions hal/hal_lvgl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef _HAL_LVGL_H_
#define _HAL_LVGL_H_

void hal_lvgl_init(void);

#endif
5 changes: 5 additions & 0 deletions lcd/sw_lcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ void sw_lcd_display_partial(lcd_frame frame, uint16_t *image)
void sw_lcd_display_fullscreen(lcd_frame frame, uint16_t *image)
{
lcd_display_fullscreen(frame, image);
}

void sw_lcd_set_screen(lcd_frame frame)
{
lcd_set_frame(frame);
}
1 change: 1 addition & 0 deletions lcd/sw_lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ void sw_lcd_clear_screen(lcd_frame frame, uint16_t color);
void sw_lcd_display_point(uint16_t X, uint16_t Y, uint16_t color);
void sw_lcd_display_partial(lcd_frame frame, uint16_t *image);
void sw_lcd_display_fullscreen(lcd_frame frame, uint16_t *image);
void sw_lcd_set_screen(lcd_frame frame);
#endif
15 changes: 4 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@
#include "gpio/sw_gpio.h"
#include "spi/sw_spi.h"
#include "clock/sw_clock.h"
#include "hal/hal_lvgl.h"
#include "devicetree.h"
static struct repeating_timer lvgl_timer;

static bool repeating_lvgl_timer_callback(struct repeating_timer *timer)
{
lv_tick_inc(5);
return true;
}

int main(void)
{
Expand All @@ -34,12 +28,11 @@ int main(void)
sw_spi_dma_init(270000 * 1000);
sw_lcd_gpio_init(&lcd_io);
sw_lcd_init(&lcd_func_cfg, HORIZONTAL);
sw_lcd_clear_screen(frame, WHITE);

// Software configurations;
sw_repeating_timer_init(-5, repeating_lvgl_timer_callback, NULL, &lvgl_timer);
lv_init();
// Software configurations
hal_lvgl_init();

sw_lcd_clear_screen(frame, RED);
while (1)
{
lv_timer_handler();
Expand Down
14 changes: 14 additions & 0 deletions spi/sw_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
uint dma_tx;
dma_channel_config dma_cfg;

__attribute__((weak)) void sw_dma_irq_handler(void)
{

}

uint sw_spi_dma_init(uint baudrate)
{
uint ret;
Expand All @@ -29,6 +34,10 @@ uint sw_spi_dma_init(uint baudrate)
channel_config_set_transfer_data_size(&dma_cfg, DMA_SIZE_8);
channel_config_set_dreq(&dma_cfg, spi_get_dreq(SPI_ID, true));

// Init DMA for transmit data from memory to SPI
dma_channel_set_irq0_enabled(dma_tx, true);
irq_set_exclusive_handler(DMA_IRQ_0, sw_dma_irq_handler);
irq_set_enabled(DMA_IRQ_0, true);
return ret;
}

Expand All @@ -46,4 +55,9 @@ uint sw_spi_init(uint baudrate)
void sw_spi_write_bytes(uint8_t *data, uint len)
{
spi_write_blocking(SPI_ID, data, len);
}

void sw_spi_dma_write_byte(const volatile void *read_addr, uint transfer_count, bool trigger)
{
dma_channel_configure(dma_tx, &dma_cfg, &spi_get_hw(SPI_ID)->dr, read_addr, transfer_count, trigger);
}
3 changes: 2 additions & 1 deletion spi/sw_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

typedef unsigned int uint;

void sw_dma_irq_handler(void);
uint sw_spi_dma_init(uint baudrate);
uint sw_spi_init(uint baudrate);
void sw_spi_write_bytes(uint8_t *data, uint len);

void sw_spi_dma_write_byte(const volatile void *read_addr, uint transfer_count, bool trigger);
#endif

0 comments on commit a5a03b6

Please sign in to comment.