2. Code Structure Overview

This chapter explains the current project organization so you can find the main application code, hardware drivers, LVGL porting layer, and build configuration more easily.

Note

This project uses CMake, FreeRTOS, and LVGL. It provides build presets for Pico W / Pico WH and Pico 2 W.

Project Structure

The project is organized by responsibility:

LAFVIN-PICO-Development-Kit/
|-- app/
|   |-- main.c                         # Main program entry and demo logic
|   `-- assets/
|       `-- sea.c                      # LVGL image resource
|-- drivers/
|   |-- display/
|   |   |-- st7796.c
|   |   `-- st7796.h                   # ST7796 display driver
|   |-- touch/
|   |   |-- gt911.c
|   |   `-- gt911.h                    # GT911 touch driver
|   `-- rgb/
|       `-- ws2812.pio                 # WS2812 PIO program
|-- ports/
|   `-- lvgl/
|       |-- lv_port_disp.c/.h          # LVGL display port
|       `-- lv_port_indev.c/.h         # LVGL input device port
|-- config/
|   `-- freertos/
|       |-- FreeRTOSConfig.h           # FreeRTOS project configuration
|       `-- freertos_rp2350_irq_compat.h
|-- cmake/
|   |-- FreeRTOS_PicoW.cmake           # FreeRTOS import for Pico W / Pico WH
|   `-- FreeRTOS_Pico2W.cmake          # FreeRTOS import for Pico 2 W
|-- components/
|   |-- lvgl/                          # LVGL submodule
|   |-- FreeRTOS/                      # Legacy FreeRTOS submodule for Pico W
|   `-- FreeRTOS-RaspberryPi/          # Raspberry Pi FreeRTOS submodule for Pico 2 W
|-- CMakeLists.txt                     # Main build configuration
|-- CMakePresets.json                  # Build presets
|-- lv_conf.h                          # LVGL configuration
`-- pico_sdk_import.cmake              # Pico SDK import script

Core Files

  • app/main.c - Program entry point, system initialization, FreeRTOS task creation, UI demo logic, hardware demo logic, and calculator logic.

  • CMakeLists.txt - Main build file. It selects the board, source files, include paths, libraries, PIO header generation, and compile definitions.

  • CMakePresets.json - Defines the supported build presets: pico-w and pico2-w.

  • config/freertos/FreeRTOSConfig.h - FreeRTOS configuration shared by the supported boards.

  • config/freertos/freertos_rp2350_irq_compat.h - Compatibility header used only when building Pico 2 W.

Main Code Modules

Application Layer

  • app/main.c - Creates the FreeRTOS tasks, initializes LVGL, builds the startup screen, and handles the hardware demo and calculator demo.

  • app/assets/sea.c - Image data used by the startup screen.

Hardware Driver Layer

  • drivers/display/st7796.c/.h - 3.5-inch TFT display driver using SPI.

  • drivers/touch/gt911.c/.h - Capacitive touch screen driver using I2C.

  • drivers/rgb/ws2812.pio - PIO program for the WS2812 RGB LED.

LVGL Porting Layer

  • lv_conf.h - LVGL configuration file, including memory, color depth, fonts, and feature switches.

  • ports/lvgl/lv_port_disp.c/.h - Connects LVGL drawing to the ST7796 display driver.

  • ports/lvgl/lv_port_indev.c/.h - Connects LVGL input handling to the GT911 touch driver.

Build and System Configuration

  • cmake/FreeRTOS_PicoW.cmake - Uses components/FreeRTOS for Pico W / Pico WH builds.

  • cmake/FreeRTOS_Pico2W.cmake - Uses components/FreeRTOS-RaspberryPi for Pico 2 W builds.

  • CMakePresets.json - Keeps the Pico W and Pico 2 W build commands short and repeatable.

Generated Files

PIO headers are generated during the CMake build under the selected build directory, for example:

  • build/pico_w/generated/ws2812.pio.h

  • build/pico2_w/generated/ws2812.pio.h

These generated files are not stored in the source tree.

Hardware Resources Overview

Pin Assignment

  • GP2-GP7, GP10-GP11: SPI0 connected to TFT screen

  • GP8-GP9: I2C0 connected to touch screen

  • GP12: RGB LED (WS2812)

  • GP13: Buzzer

  • GP14-GP15: Buttons (BTN2, BTN1)

  • GP16-GP17: LED indicators (D1, D2)

  • GP26-GP27: Joystick (X-axis, Y-axis)

For more detailed hardware resource description, please refer to Component List.

Code Execution Flow

Startup Process

  1. Initialize the standard Pico SDK runtime.

  2. Initialize LVGL.

  3. Initialize the display and touch porting layers.

  4. Create the LVGL mutex.

  5. Create FreeRTOS tasks.

  6. Start the FreeRTOS scheduler.

Runtime Architecture

  • task0 creates the startup UI and handles slower hardware monitoring, such as joystick updates.

  • task1 runs lv_task_handler() and processes LVGL-safe UI updates.

  • GPIO interrupts handle button press events and update physical LED state.

Dependencies

The project depends on these main libraries:

  • Pico SDK - Raspberry Pi Pico development SDK

  • FreeRTOS - Real-time operating system

  • LVGL - Graphics library

Next Steps