HOME
Shop
  • English
  • 简体中文
HOME
Shop
  • English
  • 简体中文
  • Product Series

    • FPGA+ARM

      • GM-3568JHF

        • Introduction

          • GM-3568JHF Introduction
        • Quick Start

          • Preface
          • Environment Setup
          • Compilation Notes
          • Flashing Guide
          • Debugging Tools
          • Software Update
          • Viewing System Information
          • Test Commands
          • Application Compilation
          • Source Code Access
        • Peripherals & Interfaces

          • USB
          • Display and Touch
          • Ethernet
          • WIFI
          • Bluetooth
          • TF-Card
          • Audio
          • Serial Port
          • CAN
          • RTC
        • Application Development

          • UART Read/Write Demo
          • Key Detection Demo
          • LED Blink Demo
          • MIPI Screen Detection Demo
          • Read USB Device Information Demo
          • FAN Detection Demo
          • FPGA FSPI Communication Demo
          • FPGA DMA Read/Write Demo
          • GPS Debugging Demo
          • Ethernet Test Demo
          • RS485 Read/Write Demo
          • FPGA I2C Read/Write Demo
          • PN532 NFC Card-Reading Demo
          • TF Card Read/Write Demo
        • QT Development

          • ARM64 Cross-Compiler Environment Setup
          • Adding a QT Program to Boot Auto-Start
        • RKNN_NPU Development

          • RK3568 NPU Overview
          • Development Environment Setup
          • Run the Official YOLOv5 Example
        • FPGA Development

          • ARM and FPGA Communication
          • FPGA Development Manual
        • Others

          • Modifying the Root Filesystem
          • System Auto-Start Services
        • Downloads

          • Downloads
      • MB-E30P

        • Introduction

          • MB-E30P Introduction
        • Quick Start

          • Preface
          • Environment Setup
          • Compilation Instructions
          • Flashing Guide
          • Debugging Tools
          • Software Update
          • Viewing Information
          • Test Commands
          • Application Compilation
          • Source Code Acquisition
        • Peripherals & Interfaces

          • USB
          • Display and Touch
          • Ethernet
          • WIFI
          • Bluetooth
          • TF-Card
          • Audio
          • RTC
        • Application Development

          • Key Detection Demo
          • LED Blink Demo
          • MIPI Screen Detection Demo
          • Read USB Device Information Demo
          • FAN Detection Demo
          • FPGA FSPI Communication Demo
          • FPGA DMA Read/Write Demo
          • Ethernet Test Demo
          • FPGA IIC Read/Write Demo
          • PN532 NFC Card Reading Demo
          • TF Card Read/Write Demo
        • QT Development

          • ARM64 Cross-Compiler Environment Setup
          • Adding a QT Program to the Boot Auto-Start Service
        • RKNN_NPU Development

          • RK3568 NPU Overview
          • Development Environment Setup
          • Run the Official YOLOv5 Example
          • Model Conversion In Detail
          • Run Custom Models on the Board
        • FPGA Development

          • ARM and FPGA Communication
          • FPGA Development Manual
        • Others

          • Modifying the Root Filesystem
          • System Auto-Start Service
        • Downloads

          • Downloads
    • ShimetaPi

      • M4-R1

        • Introduction

          • M4-R1 Introduction
        • Quick Start

          • OpenHarmony Overview
          • Image Burning
          • Application Development Quick Start
          • Device Development Quick Start
        • Application Development

          • ArkUI

            • ArkTS Language Overview
            • UI Components - Row Container Introduction
            • UI Components - Column Container Introduction
            • UI Components - Text Component
            • UI Components - Toggle Component
            • UI Components - Slider Component
            • UI Components - Animation Component & Transition Component
          • Documentation

            • OpenHarmony Official Materials
          • Development Notes

            • Full-SDK Replacement Tutorial
            • Introducing and Using Third-Party Libraries
            • HDC Debugging
            • Restore Factory Mode via Command Line
            • Upgrade App to System Permission
          • First App

            • Build Your First ArkTS Application - HelloWorld
          • Demos

            • Serial-Debug-Assistant Application Demo
            • Writing-Board Application Demo
            • Digital Clock Application Demo
            • Wi-Fi Information Acquisition Application Demo
        • Device Development

          • Ubuntu Development

            • Environment Setup
            • Download Source Code
            • Compile Source Code
          • DevEco Device Tool

            • Tool Introduction
            • Development Environment Construction
            • Import the SDK
            • HUAWEI DevEco Tool Function Introduction
        • Kernel Peripherals & Interfaces

          • Guide
          • Device Tree Introduction
          • NAPI Introduction
          • ArkTS Introduction
          • NAPI Development Hands-on Demo
          • GPIO Introduction
          • I2C Communication
          • SPI Communication
          • PWM Control
          • UART Communication
          • TF Card (MicroSD)
          • Screen (Display)
          • Touch
          • Ethernet
          • M.2 SSD
          • Audio
          • WIFI & BT
          • Camera
        • Downloads

          • Downloads
      • M5-R1

        • Introduction

          • M5-R1 Development Docs
        • Quick Start

          • Image Burning
          • Environment Setup
          • Download Source Code
        • Peripherals & Interfaces

          • Raspberry Pi Interfaces
          • GPIO Interface
          • I2C Interface
          • SPI Communication
          • PWM Control
          • Serial Port Communication
          • TF Card
          • Display
          • Touch
          • Audio
          • RTC
          • Ethernet
          • M.2
          • MINI-PCIE
          • Camera
          • WIFI & BT
        • Downloads

          • Downloads
      • Pico-G1

        • Product Overview

          • Product Introduction
          • SDK Version Information
        • Quick Start

          • Development Environment Setup
          • Image Build
          • Image Flashing
          • System Login
          • Network Configuration
          • File Transfer
          • SDK Directory Structure
          • Deploying Your First Application
          • Deploying Your First Driver
          • Mounting an SD Card
        • Peripherals & Interfaces

          • GPIO Control
          • UART Serial Communication
          • I2C Communication
          • SPI Communication
        • MPP Media Development

          • MPP Media Processing Software
          • Image Processing Chain
          • Video Input
          • Image Encoding
        • NPU & AI

          • NPU Driver and Runtime Library Architecture
          • .xmm Model Loading
          • SVP Video Processing
          • AI Noise Reduction (AI_NR)
        • Application Samples

          • Encryption/Decryption Application
          • ADC Acquisition Application
          • Low-Power Application
          • Audio Processing Application
          • Video Encoding Application
          • Video Input Application
          • Video Graphics Subsystem (VGS) Application
          • 08 Region Overlay Application
          • 09 Intelligent Video Engine Application
          • 10 UVC Webcam Application
          • 11 All-in-One Quickstart Application
          • 12 FPN Correction Application
          • 13 Regional Motion Detection Application
          • 14 MTCNN Face Detection Application
        • Expansion Board Peripheral Examples

          • 00 - Pico Expansion Board Peripheral Examples Overview
          • 01 - OLED Display Application
          • 02 - TFT Display Application
          • 03 - MPU6050 Gyroscope Application
          • 04 - ADC Acquisition Application
          • 05 - Passive Buzzer Application
          • 06 - MQ Gas Sensor Application
          • 07 - GPS Positioning Application
          • 08 - SHT20 Temperature & Humidity Application
          • 09 - Ultrasonic Ranging Application
          • 10 - SpO2 Sensor Application
          • 11 - DC Motor Control Application
          • 12 - Servo Control Application
    • OpenHarmony

      • SC-3568HA

        • Introduction

          • SC-3568HA Overview
        • Quick Start Guide

          • OpenHarmony Overview
          • Image Flashing
          • Setting Up the Development Environment
          • Hello World Application and Deployment
        • Application Development

          • ArkUI

            • Introduction to ArkTS Language
            • Introduction to UI Components and Practical Applications (Part 1)
            • Introduction to UI Components and Practical Applications (Part 2)
            • Introduction to UI Components and Practical Applications (Part 3)
          • Expand

            • Getting Started Guide
            • Referencing and Using Third-Party Libraries
            • Application Compilation and Deployment
            • Command-Line Factory Reset
            • System Debugging -- HDC Debugging
            • APP Stability Testing
            • Chapter 7 Application Testing
        • Device Development

          • Environment Setup
          • Download Source Code
          • Compiling Source Code
        • Peripheral And Interface

          • Raspberry Pi interface
          • GPIO Interface
          • I2C Interface
          • SPI communication
          • PWM (Pulse Width Modulation) control
          • Serial port communication
          • TF Card
          • Display Screen
          • Touch
          • Audio
          • RTC
          • Ethernet
          • M.2
          • MINI-PCIE
          • Camera
          • WIFI&BT
          • Raspberry Pi expansion board
        • Downloads

          • Downloads
      • M-K1HSE

        • Introduction

          • M-K1HSE Introduction
        • Quick Start

          • Development environment construction
          • Source code acquisition
          • Compilation Notes
          • Burning Guide
        • Application Development

          • Application Development Environment Setup
          • First Application - Hello World
        • Peripherals and interfaces

          • 01 Audio
          • 02 RS485
          • 03 Display
        • System customization development

          • System transplant
          • System customization
          • Driver Development
          • System Debugging
          • OTA Update
        • Downloads

          • Downloads
    • HVS Camera

      • Quick Start

        • SDK Overview
        • Downloads
        • Your First C++ Program
        • Python Data Analysis
        • MultiVision Studio
      • Development

        • Programming Guides

          • Open Camera
          • Read Events
          • Recording & Replay
          • Event Processing (Denoising)
          • Display & Visualization
          • Tuning
          • Capture APS Image
        • Toolkit SDK

          • Hybrid Vision Toolkit
          • Quick Start
          • C++ API
          • Python API
        • Algorithm

          • Hybrid Vision Algo
          • Hybrid Vision Algo API
          • Windows Algo SDK
        • Samples Overview
        • Applications
      • Fundamentals

        • Event Camera Fundamentals
        • HVS Hybrid Vision
        • Event Visualization
        • Data Formats Reference
        • Glossary
        • Bias & Tuning
        • Video Tutorials
      • USB Cameras

        • HVS Camera Quick Start
        • Networking Capabilities

          • HVS Camera System Architecture
          • EVS Network Server
          • EVS Time Sync
          • Web Window
        • HVS Camera Compatibility Matrix
        • FAQ & Troubleshooting Guide
        • Products

          • CF-NRS1 (Lingguang No.1 Hybrid Vision Camera)
      • MIPI Modules

        • MIPI Module Quick Start
        • Carrier Boards

          • RDK X5 Carrier Board Adaptation
          • Raspberry Pi Carrier Board Adaptation
          • Digua Pi Carrier Board Adaptation
          • ShimeTai Board Carrier Board Adaptation
        • MIPI Module Compatibility Matrix
        • Products

          • EVS_003 Sensor Module
    • AI-model

      • 1684XB-32T

        • Introduction

          • AIBOX-1684XB-32 Introduction
        • Quick Start

          • First Use
          • Network Configuration
          • Disk Usage
          • Memory Allocation
          • Fan Control Strategy
          • Firmware Upgrade
          • Cross Compilation
          • Model Quantization
        • Application Development

          • Development Overview

            • Sophgo SDK Development
            • Sophgo Demo Introduction
          • Large Language Models

            • Deploying Llama3 Example
            • Sophon LLM_api_server Development
            • Deploying MiniCPM-V-2_6
            • Qwen-2-5-VL Image and Video Recognition Demo
            • Qwen3-chat Demo
            • Qwen3-Qwen Agent-MCP Development
            • Qwen3-langchain-AI Agent
          • Deep Learning

            • ResNet (Image Classification)
            • LPRNet (License Plate Recognition)
            • SAM (General Image Segmentation Foundation Model)
            • YOLOv5 (Object Detection)
            • OpenPose (Human Keypoint Detection)
            • PP-OCR (Optical Character Recognition)
        • Downloads

          • Downloads
      • 1684X-416T

        • Introduction

          • AIBOX-1684X-416 Introduction
        • Demo Quick Guide

          • ShimeTai Intelligent Monitoring Demo Quick Usage Guide
      • RDK-X5

        • Introduction

          • RDK-X5 Hardware Introduction
        • Quick Start

          • RDK-X5 Quick Start
        • Application Development

          • AI Online Model Development

            • Experiment 01 - Access Volcengine Doubao AI
            • Experiment 02 - Image Analysis
            • Experiment 03 - Multimodal Visual Analysis & Localization
            • Experiment 04 - Multimodal Image-Text Comparison
            • Experiment 05 - Multimodal Document/Table Analysis
            • Experiment 06 - Camera-based AI Visual Analysis
          • Large Language Models

            • Experiment 01 - Speech Recognition
            • Experiment 02 - Voice Conversation
            • Experiment 03 - Multimodal Image Analysis - Voice
            • Experiment 04 - Multimodal Image Comparison - Voice
            • Experiment 05 - Multimodal Document Analysis - Voice
            • Experiment 06 - Multimodal Vision Application - Voice
          • ROS2 Basics

            • Experiment 01 - Environment Setup
            • Experiment 02 - Create & Build a Workspace Package
            • Experiment 03 - Run ROS2 Topic Communication Node
            • Experiment 04 - ROS2 Camera Application
          • 40-pin IO Development

            • Experiment 01 - GPIO Output (LED Blink)
            • Experiment 02 - GPIO Input
            • Experiment 03 - Button-controlled LED
            • Experiment 04 - PWM Output
            • Experiment 05 - Serial Output
            • Experiment 06 - I2C Experiment
            • Experiment 07 - SPI Experiment
          • USB Module Usage

            • Experiment 01 - USB Voice Module Usage
            • Experiment 02 - Sound Source Localization Module
          • Machine Vision Practice

            • Experiment 01 - Open USB Camera
            • Experiment 02 - Color Recognition
            • Experiment 03 - Gesture Recognition
            • Experiment 04 - YOLOv5 Object Detection
      • RDK-S100

        • Introduction

          • RDK-S100 Hardware Introduction
        • Quick Start

          • RDK-S100 Quick Start
        • Application Development

          • AI Online Model Development

            • Experiment 01 - Access Volcengine Doubao AI
            • Experiment 02 - Image Analysis
            • Experiment 03 - Multimodal Visual Analysis & Localization
            • Experiment 04 - Multimodal Image-Text Comparison
            • Experiment 05 - Multimodal Document/Table Analysis
            • Experiment 06 - Camera-based AI Visual Analysis
          • Large Language Models

            • Experiment 01 - Speech Recognition
            • Experiment 02 - Voice Conversation
            • Experiment 03 - Multimodal Image Analysis - Voice
            • Experiment 04 - Multimodal Image Comparison - Voice
            • Experiment 05 - Multimodal Document Analysis - Voice
            • Experiment 06 - Multimodal Vision Application - Voice
          • ROS2 Basics

            • Experiment 01 - Environment Setup
            • Experiment 02 - Create & Build a Workspace Package
            • Experiment 03 - Run ROS2 Topic Communication Node
            • Experiment 04 - ROS2 Camera Application
          • 40-pin IO Development

            • Experiment 01 - GPIO Output (LED Blink)
            • Experiment 02 - GPIO Input
            • Experiment 03 - Button-controlled LED
            • Experiment 04 - PWM Output
            • Experiment 05 - Serial Output
            • Experiment 06 - I2C Experiment
            • Experiment 07 - SPI Experiment
          • USB Module Usage

            • Experiment 01 - USB Voice Module Usage
            • Experiment 02 - Sound Source Localization Module
          • Machine Vision Practice

            • Experiment 01 - Open USB Camera
            • Experiment 02 - Image Processing Basics
            • Experiment 03 - Object Detection
            • Experiment 04 - Image Segmentation
      • RK1828

        • Introduction

          • M5-182X-A1 AI Edge Box - Product Introduction
          • M5-182X-A1 Hardware Specifications
          • M5-182X-A1 Usage & Safety
        • Quick Start

          • M5-182X-A1 Image Flashing
          • RK182X Hardware Installation & Verification
          • RK182X Development Environment Quick Setup
          • RK182X SDK Overview
          • RK182X Environment Setup in Detail
          • RK182X Quick Start
          • Vendor SDK Data Extraction Record
        • Development Guide

          • ClawChips Architecture and Principles
          • SKILL User Manual
          • RK182X Series LLM Inference (RK1828 Model)
          • RK182X Series CNN Inference (RK1828 Model)
          • Model Conversion
          • RK182X AI Agent Application Development Guide
          • RK182X Industrial Anomaly Detection Application
        • SDK Reference

          • RKNN3-SDK Overview

            • RKNN3 SDK Overview
          • RKNN3-Toolkit

            • RKNN3 Toolkit Installation and Usage
          • RKLLM

            • RKLLM On-Device LLM Inference
          • RK182X Series NPU Overview and Architecture (RK1828 Model)
          • RK182X INT8 Quantized Inference Deployment
          • RK182X MPP Multimedia Framework
          • MPP Details

            • RK182X Video Decoding
            • RK182X Video Encoding
          • NPU Details

            • RKNN Model Conversion
            • RK182X NPU INT8 Quantized Inference
            • RK182X Multi-Model Parallel Inference
          • RGA Details

            • RK182X RGA 2D Graphics Acceleration
          • VPU Details

            • RK182X VPU Codec
        • Hardware Reference

          • RK182X Series Hardware Architecture Overview (RK1828 Model)
          • RK182X Pin Definitions and Multiplexing Configuration
          • RK182X Pin Definitions
          • RK182X Power Management
          • RK182X Clock and PLL Configuration
          • RK182X Clock and Frequency Configuration
        • Tutorials

          • Hello World
          • Hello RK1828 - The First Program
          • RTSP Streaming
          • RTSP Streaming + AI Analysis
          • ShiMetaPi AI Lobster One-Click Deployment
          • PaddleOCR-VL Text Recognition
          • Qwen3-1.7B LLM Text Chat
          • AI Multi-View Inspection (Qwen3-VL Wrapper)
          • YOLOv5 Object Detection
        • Downloads

          • Downloads
        • FAQ

          • FAQ
    • Core-Board

      • C-3568BQ

        • Introduction

          • C-3568BQ Overview
      • C-3588LQ

        • Introduction

          • C-3588LQ Overview
      • GC-3568JBAF

        • Introduction

          • GC-3568JBAF Overview
      • C-K1BA

        • Introduction

          • C-K1BA Overview
    • Software Platform

      • ShiMetaPi Workbench

        • Introduction

          • Product Overview
          • Core Architecture
          • Feature Entries
          • Supported Hardware
          • Release Notes
        • Quick Start

          • Install & Login
          • Connect the Device
          • Set Up the Environment
          • Connect to AIHub
          • First Inference
        • User Guide

          • Workspace Overview
          • Device Manager
          • Model Market
          • One-Click Deploy
          • Vision — SVP
          • Vision - Custom Models
          • shimeta-py IDE
          • Terminal
          • Agent Debug Assistant
          • Settings and Resources
        • FAQ

          • Installation & Login
          • Device Connection
          • Models & Deployment
          • Vision & Runtime
          • Settings & Other
      • ShimetaPi Repository

        • Introduction

          • ShimetaPi Software Repository
        • Pico G1 (GK7206)

          • Quick Start

            • Installation & First Inference
            • shimeta_infer — Image Inference
            • shimeta_camera — Real-time Camera Inference
            • SVP Scene Detection
            • File Transfer & Built-in Model Reference
            • FAQ
          • HTTP API & Python SDK

            • HTTP API Reference
      • Model Fine-tuning Platform

        • Introduction

          • Model Training Platform
        • Quick Start

          • Register & Login
          • Create Your First Model (30-Minute Quick Experience)
        • Training Guide

          • Data Preparation & Annotation
          • Training Parameter Configuration
          • Start & Monitor Training
          • Model Evaluation & Testing
        • Model Deployment

          • Export Model
          • Deploy to Edge Device

02 - TFT Display Application

This chapter describes the TFT display application example on the Pico-G1 expansion board — tft_display. It demonstrates how to drive an ST7789 TFT color screen over the SPI interface for high-resolution color graphics. It is an advanced example for learning high-speed SPI communication and graphics programming, covering the full technology stack from low-level SPI operations to graphics rendering.

The application source code lives in the SDK directory source/app/02_TFT_display/, providing a complete SPI character-device implementation and ST7789 driver — an important reference for learning embedded graphics programming.

1 Application Overview

1.1 Features

  • High-speed SPI communication: demonstrates high-speed data transfer through the Linux SPI character-device interface
  • TFT driver: complete ST7789 driver implementation (initialization, drawing, refresh)
  • Color graphics display: high-resolution display in RGB565 format (65536 colors)
  • Graphics rendering: graphics functions such as pixel drawing, rectangle filling, and text rendering
  • Real-time info: displays uptime, CPU status, and other live system information
  • Font support: built-in 8×16 ASCII bitmap font (0x20~0x7E)

1.2 Technical Specifications

ParameterValue
TFT modelST7789, 240×240 pixels
Communication interfaceSPI (SPI2, /dev/spidev2.0)
SPI modeMODE3 (CPOL=1, CPHA=1)
SPI speed24MHz (maximum)
Color formatRGB565 (16-bit, 65536 colors)
Framebuffer size240×240×2 = 115200 bytes (113KB)
Font size8×16 pixel ASCII font
Refresh interval1 second (configurable)

1.3 Test Case List

indexNameTest commandExpected result (success)Possible causes of failure
1Welcome screen./tft_displayColor blocks and welcome message are shownSPI connection failure, wrong TFT address
2Continuous refresh./tft_display (no arguments)After the welcome screen, system info refreshes every secondSame as above
3Graphics testColor rectangles and text displayedAll colors render correctlyWrong color format, framebuffer error

1.4 Directory Structure

source/app/02_TFT_display/
├── Makefile              # Build script
├── main.c                # Main program
├── spi_hal.c             # SPI HAL layer implementation
├── spi_hal.h             # SPI HAL layer header
├── gpio_hal.c            # GPIO HAL layer implementation
├── gpio_hal.h            # GPIO HAL layer header
├── st7789.c              # ST7789 driver implementation
├── st7789.h              # ST7789 driver header
├── font8x16.h            # 8×16 ASCII bitmap font
└── README.md             # Documentation

2 Hardware Connection

2.1 Pin Definition

SignalOn-board GPIOController / nodeDescription
SCKGPIO4_7SPI2 → /dev/spidev2.0SPI clock
MOSIGPIO5_0SPI2 → /dev/spidev2.0SPI master output
CSGPIO5_1/dev/gpiochip5 line1Chip select (manual framing)
DCGPIO4_5/dev/gpiochip4 line5Data/command select
RESGPIO4_4/dev/gpiochip4 line4Reset
VCC3.3V—Power supply
GNDGND—Ground

2.2 Hardware Circuit

Standard SPI TFT wiring:

     Pico-G1                   ST7789 TFT Module
  ┌───────────┐              ┌──────────────┐
  │           │              │              │
  │ GPIO4_7 ──┼────── SCK ──┤ SCK          │
  │           │              │              │
  │ GPIO5_0 ──┼────── MOSI ──┤ SDA/MOSI     │
  │           │              │              │
  │ GPIO5_1 ──┼────── CS ───┤ CS           │
  │           │              │              │
  │ GPIO4_5 ──┼────── DC ───┤ DC/RS        │
  │           │              │              │
  │ GPIO4_4 ──┼────── RES ──┤ RES/RESET    │
  │           │              │              │
  │    3.3V ──┼─────────────┤ VCC          │
  │           │              │              │
  │     GND ──┼─────────────┤ GND          │
  │           │              │              │
  └───────────┘              └──────────────┘

Pin functions

  • SCK/MOSI: SPI data lines, driven by the SPI2 controller
  • CS: chip select, active low, controlled manually via GPIO
  • DC: data/command select, high = data, low = command
  • RES: reset signal, active low
  • VCC: supply voltage, 3.3V (some modules support 5V)

2.3 Pin Multiplexing

Pin multiplexing that needs to be configured:

padPhysical addressValueDescription
SCK (GPIO4_7)0x100C00280x1004func4 = SPI2_SCLK
MOSI (GPIO5_0)0x100C002C0x1004func4 = SPI2_SDO
CS (GPIO5_1)0x100C00300x1000func0 = GPIO (manual framing)
DC (GPIO4_5)0x100C00200x1005func5 = GPIO (default JTAG_TDO)
RES (GPIO4_4)0x100C001C0x1005func5 = GPIO (default JTAG_TDI)

JTAG pin conflict

GPIO4_4 and GPIO4_5 default to their JTAG function and must be manually configured as GPIO. Without this configuration, the SPI display will not work.

3 Build and Deployment

3.1 Prerequisites

Before building this application, make sure the following preparations are complete:

  1. SDK environment ready: set up the cross-compilation toolchain and SDK by following Development Environment Setup
  2. Kernel configuration confirmed: make sure the kernel has SPI driver support enabled
  3. Hardware connected: the TFT module is correctly wired to the corresponding pins

3.2 Building the Application

# Set the toolchain path
export PATH=$PATH:<SDK>/tools/linux/toolchains/arm-gcc12.2.0-linux-uclibceabi/bin

# Enter the example directory
cd <SDK>/source/app/02_TFT_display

# Build
make

# Clean
make clean

After a successful build, the executable tft_display is generated in the current directory.

3.3 Deploying to the Board

# Transfer to the development board with SCP
scp tft_display root@<board-IP>:/usr/bin/

# Or download via TFTP
tftp -g -r tft_display <board-IP>

3.4 Running the Application

# Add execute permission
chmod +x /usr/bin/tft_display

# Run the TFT display example
/usr/bin/tft_display

Once started, the TFT screen shows a color welcome screen; after 2 seconds it starts displaying real-time system information. Press Ctrl+C to exit.

3.5 Expected Output

Console output

/mnt # ./spi_display
[spi] init ST7789 @ /dev/spidev2.0 ...
[spi] pad 0x100C0028 -> 0x00001004
[spi] pad 0x100C002C -> 0x00001004
[spi] pad 0x100C0030 -> 0x00001000
[spi] pad 0x100C0020 -> 0x00001005
[spi] pad 0x100C001C -> 0x00001005
[spi] opening /dev/spidev2.0 ...
[spi] spidev opened, fd=3
[spi] spidev mode/bits/speed set (MODE3/8b/24MHz)
[spi] chardev request DC  @ /dev/gpiochip4 line 5 ...
[spi] chardev request RES @ /dev/gpiochip4 line 4 ...
[spi] chardev request CS  @ /dev/gpiochip5 line 1 ...
[spi] chardev-verify: DC=0(expect0) RES=1(expect1) CS=1(expect1)  ==> OK(chardev 真驱动了引脚)
[spi] spi_hal_init done
[tft] >>> st7789_init()
[tft] init: SLPOUT
[tft] init: SLPOUT +120ms ok
[tft] init: config cmds ok
[tft] init: DISPON
[tft] init: DISPON ok
[tft] init: clear-flush start
[tft] flush #1 start
[tft] flush #1 done
[tft] init: clear-flush done
[tft] <<< st7789_init() returned
[spi] 初始化成功,开始显示。
[tft] flush #2 start
[tft] flush #2 done
[tft] flush #3 start
[tft] flush #3 done

TFT screen display

TFT display effect

Fixed and variable parts

  • Fixed part: the display format and layout (matches the fixed code)
  • Variable part: the system uptime values (updated on every refresh)

4 Internal Execution Logic

4.1 Application Architecture

This application uses a layered design consisting of a hardware abstraction layer, a driver layer, and an application layer:

// Application layer (main.c)
int main(int argc, char *argv[])
{
    // 1. Initialize pin multiplexing
    padmux_init();

    // 2. Initialize the SPI HAL layer
    spi_init();

    // 3. Initialize GPIO (DC/RES/CS)
    gpio_init();

    // 4. Initialize the TFT driver
    st7789_init();

    // 5. Show the welcome screen
    display_welcome();
    sleep(2);

    // 6. Main loop refreshing system information
    while (1) {
        st7789_clear();
        display_system_info();
        st7789_refresh();
        sleep(1);
    }

    return 0;
}

4.2 SPI HAL Layer Implementation

The SPI HAL layer wraps the Linux SPI character-device operations:

// SPI initialization
int spi_init(void)
{
    int fd = open("/dev/spidev2.0", O_RDWR);
    if (fd < 0) {
        perror("打开 SPI 设备失败");
        return -1;
    }

    // Set the SPI mode to MODE3
    uint8_t mode = SPI_MODE_3;
    ioctl(fd, SPI_IOC_WR_MODE, &mode);

    // Set the number of bits per transfer to 8
    uint8_t bits = 8;
    ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);

    // Set the maximum SPI speed
    uint32_t speed = 24000000;  // 24MHz
    ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);

    return fd;
}

// SPI data transfer
int spi_transfer(uint8_t *tx_data, uint8_t *rx_data, uint32_t length)
{
    struct spi_ioc_transfer tr = {
        .tx_buf = (unsigned long)tx_data,
        .rx_buf = (unsigned long)rx_data,
        .len = length,
        .speed_hz = 24000000,
        .bits_per_word = 8,
        .delay_usecs = 0,
    };

    return ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);
}

4.3 ST7789 Driver Implementation

The ST7789 driver implements full TFT screen control:

// TFT initialization sequence
void st7789_init(void)
{
    // Hardware reset
    gpio_set_value(RES_GPIO, 0);
    usleep(10000);
    gpio_set_value(RES_GPIO, 1);
    usleep(10000);

    // Send initialization commands
    st7789_write_command(0x11);  // SLPOUT (exit sleep mode)
    usleep(120000);                // wait 120ms

    // MADCTL (page address order)
    st7789_write_command(0x36);
    st7789_write_data(0x00);

    // COLMOD (pixel format set)
    st7789_write_command(0x3A);
    st7789_write_data(0x05);     // 16-bit/pixel

    // ... other initialization commands

    st7789_write_command(0x21);  // INVON (display inversion on)
    st7789_write_command(0x29);  // DISPON (display on)

    usleep(20000);                // wait for the display to turn on

    st7789_clear();
    st7789_refresh();
}

// Send a command
void st7789_write_command(uint8_t cmd)
{
    gpio_set_value(DC_GPIO, 0);   // DC = 0, command mode
    gpio_set_value(CS_GPIO, 0);   // CS = 0, chip select active

    spi_transfer(&cmd, NULL, 1);

    gpio_set_value(CS_GPIO, 1);   // CS = 1, chip select inactive
}

// Send data
void st7789_write_data(uint8_t *data, uint32_t length)
{
    gpio_set_value(DC_GPIO, 1);   // DC = 1, data mode
    gpio_set_value(CS_GPIO, 0);   // CS = 0, chip select active

    spi_transfer(data, NULL, length);

    gpio_set_value(CS_GPIO, 1);   // CS = 1, chip select inactive
}

4.4 Graphics Rendering

The driver layer provides basic graphics rendering:

// Set a pixel
void st7789_set_pixel(int x, int y, uint16_t color)
{
    if (x < 0 || x >= TFT_WIDTH || y < 0 || y >= TFT_HEIGHT)
        return;

    int index = (y * TFT_WIDTH + x) * 2;
    framebuffer[index] = color >> 8;      // high byte
    framebuffer[index + 1] = color & 0xFF; // low byte
}

// Clear the screen
void st7789_clear(void)
{
    memset(framebuffer, 0, sizeof(framebuffer));
}

// Refresh the display
void st7789_refresh(void)
{
    // Set the column address range
    st7789_write_command(0x2A);  // CASET
    uint8_t col_data[] = {0, 0, 0, (TFT_WIDTH - 1) & 0xFF};
    st7789_write_data(col_data, 4);

    // Set the row address range
    st7789_write_command(0x2B);  // RASET
    uint8_t row_data[] = {0, 0, 0, (TFT_HEIGHT - 1) & 0xFF};
    st7789_write_data(row_data, 4);

    // Write the framebuffer data
    st7789_write_command(0x2C);  // RAMWR
    st7789_write_data(framebuffer, sizeof(framebuffer));
}

5 Key Programming Points

5.1 SPI Timing Control

Characteristics of SPI MODE3:

  • CPOL = 1: the clock idles high
  • CPHA = 1: data is sampled on the falling clock edge

5.2 RGB565 Color Format

RGB565 is a 16-bit color format:

Bit layout: RRRRRGGG GGGBBBBB
          ↑5-bit red↑ ↑6-bit green↑↑5-bit blue↑

Examples:
- Red   (0xF800): 11111 000000 00000
- Green (0x07E0): 00000 111111 00000
- Blue  (0x001F): 00000 000000 11111
- White (0xFFFF): 11111 111111 11111

5.3 Framebuffer Management

Framebuffer layout:

  • 240×240 pixels, 2 bytes per pixel
  • Total size: 240 × 240 × 2 = 115,200 bytes
  • Byte order: big-endian (MSB first)

Access method:

// Compute the framebuffer offset of pixel (x,y)
int index = (y * TFT_WIDTH + x) * 2;

// Write pixel data (big-endian)
framebuffer[index] = color >> 8;      // high byte
framebuffer[index + 1] = color & 0xFF; // low byte

5.4 High-Speed Refresh Optimization

Chunked transfer:

// The TX FIFO is < 128 bytes, so data must be transferred in chunks
#define CHUNK_SIZE 16

void st7789_refresh(void)
{
    // ... set the address range ...

    gpio_set_value(DC_GPIO, 1);   // data mode
    gpio_set_value(CS_GPIO, 0);   // chip select active

    // Transfer the framebuffer data in chunks
    for (int i = 0; i < sizeof(framebuffer); i += CHUNK_SIZE) {
        int chunk_len = min(CHUNK_SIZE, sizeof(framebuffer) - i);
        spi_transfer(&framebuffer[i], NULL, chunk_len);
        usleep(100);  // breather between chunks to avoid FIFO overflow
    }

    gpio_set_value(CS_GPIO, 1);   // chip select inactive
}

6 Code Customization

6.1 Changing the SPI Speed

Edit the SPI speed parameter in spi_hal.c:

uint32_t speed = 24000000;  // 24MHz (maximum)
// uint32_t speed = 12000000;  // 12MHz (stable)
// uint32_t speed = 8000000;   // 8MHz (compatible)

Choosing the SPI speed

  • 24MHz: highest speed, may be unstable
  • 12MHz: recommended, stable and reliable
  • 8MHz: best compatibility, suitable for long wires

6.2 Changing the Resolution

Other resolutions (e.g. 135×240) are supported:

// Modify st7789.h
#define TFT_WIDTH   135
#define TFT_HEIGHT  240

// Adjust the corresponding parameters in the initialization sequence
st7789_write_command(0x2A);  // CASET
uint8_t col_data[] = {0, 0, 0, (TFT_WIDTH - 1) & 0xFF};
st7789_write_data(col_data, 4);

6.3 Adding Graphics Functions

Add more graphics functions:

// Draw a horizontal line
void st7789_draw_hline(int x1, int x2, int y, uint16_t color)
{
    for (int x = x1; x <= x2; x++)
        st7789_set_pixel(x, y, color);
}

// Draw a vertical line
void st7789_draw_vline(int x, int y1, int y2, uint16_t color)
{
    for (int y = y1; y <= y2; y++)
        st7789_set_pixel(x, y, color);
}

// Draw a rectangle
void st7789_draw_rect(int x1, int y1, int x2, int y2, uint16_t color)
{
    st7789_draw_hline(x1, x2, y1, color);
    st7789_draw_hline(x1, x2, y2, color);
    st7789_draw_vline(x1, y1, y2, color);
    st7789_draw_vline(x2, y1, y2, color);
}

7 Troubleshooting

ProblemPossible causeSolution
Screen stays darkSPI connection failure, insufficient powerCheck wiring, confirm the supply voltage
Garbled or corrupted screenWrong initialization sequence, SPI speed too highCheck init parameters, lower the SPI speed
Wrong colorsWrong RGB565 format, wrong byte orderCheck the color format conversion, confirm byte order
Partial display anomaliesWrong address range settingsCheck the CASET/RASET parameters
Slow refreshUnsuitable chunk size, speed too lowAdjust the chunk size, raise the SPI speed
White screen, no displayContrast setting, display not turned onAdjust contrast, confirm the DISPON command

8 Advanced Extensions

8.1 Image Display

Support bitmap-format images:

// Display an RGB565 image
void st7789_draw_image(int x, int y, int width, int height, const uint16_t *image)
{
    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            st7789_set_pixel(x + col, y + row, image[row * width + col]);
        }
    }
}

8.2 Partial Refresh

Refresh only the changed region to improve efficiency:

// Partial refresh
void st7789_refresh_partial(int x1, int y1, int x2, int y2)
{
    // Set the partial refresh range
    st7789_write_command(0x2A);  // CASET
    uint8_t col_data[] = {x1 >> 8, x1 & 0xFF, x2 >> 8, x2 & 0xFF};
    st7789_write_data(col_data, 4);

    st7789_write_command(0x2B);  // RASET
    uint8_t row_data[] = {y1 >> 8, y1 & 0xFF, y2 >> 8, y2 & 0xFF};
    st7789_write_data(row_data, 4);

    // Write the partial framebuffer data
    st7789_write_command(0x2C);  // RAMWR
    // ... write the data of the partial region ...
}

8.3 Double Buffering

Use double buffering to avoid flicker:

uint16_t framebuffer_front[TFT_WIDTH * TFT_HEIGHT];
uint16_t framebuffer_back[TFT_WIDTH * TFT_HEIGHT];

void st7789_swap_buffers(void)
{
    // Swap the front and back buffer pointers
    uint16_t *temp = framebuffer_front;
    framebuffer_front = framebuffer_back;
    framebuffer_back = temp;

    // Refresh the front buffer to the display
    st7789_refresh();
}

9 References

  • SPI Interface in Detail
  • GPIO Interface in Detail
  • OLED Display Application
  • MPU6050 Gyroscope Application
  • Development Environment Setup
  • ST7789 datasheet
Edit this page on GitHub
Prev
01 - OLED Display Application
Next
03 - MPU6050 Gyroscope Application