跳到主要内容

sAkura-IO

  • 今日
  • 文章
  • 笔记
  • 游记
  • 友链
  • 作品
  • 我的
今日 文章 笔记 友链

sAkura-IO

  • 今日
  • 文章
  • 笔记
  • 游记
  • 友链
  • 作品
  • 我的

sAkura-IO

  • 今日
  • 文章
  • 笔记
  • 游记
  • 友链
  • 作品
  • 我的

sAkura-IO

  • 今日
  • 文章
  • 笔记
  • 游记
  • 友链
  • 作品
  • 我的

树莓派5屏幕驱动(drm、fbtft)

2026年9月23日 · 2076 字 · 约 6 分钟

DRM显示框架适配(推荐)

创建command.txt

# MIPI_DCS_SOFT_RESET
command 0x01
delay 150
 
command 0xF0 0xC3
 
command 0xF0 0x96
 
command 0x36 0x68	
 
# MIPI_DCS_SET_PIXEL_FORMAT
# 16-bit data bus for 16-bit/pixel (RGB 5-6-5-bit input) 65K-Color
command 0x3A 0x05	
 
command 0xB0 0x80	
 
command 0xB6 0x00 0x02	
 
command 0xB5 0x02 0x03 0x00 0x04
 
command 0xB1 0x80 0x10	
 
command 0xB4 0x00
 
command 0xB7 0xC6
 
command 0xC5 0x24
 
command 0xE4 0x31
 
command 0xE8 0x40 0x8A 0x00 0x00 0x29 0x19 0xA5 0x33 
 
command 0xC2
 
command 0xA7
 
command 0xE0 0xF0 0x09 0x13 0x12 0x12 0x2B 0x3C 0x44 0x4B 0x1B 0x18 0x17 0x1D 0x21 
 
command 0xE1 0xF0 0x09 0x13 0x0C 0x0D 0x27 0x3B 0x44 0x4D 0x0B 0x17 0x17 0x1D 0x21
 
command 0x36 0xEC
 
command 0xF0 0xC3
 
command 0xF0 0x69
 
# MIPI_DCS_ENTER_NORMAL_MODE
command 0x13
delay 20
 
# MIPI_DCS_EXIT_SLEEP_MODE
# Sleep out
command 0x11
 
# MIPI_DCS_ENTER_NORMAL_MODE
# Display on
command 0x29
delay 100

创建python脚本,命名为mipi-dbi-cmd

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
# SPDX-License-Identifier: CC0-1.0
#
# Written in 2022 by Noralf Trønnes <noralf@tronnes.org>
#
# To the extent possible under law, the author(s) have dedicated all copyright and related and
# neighboring rights to this software to the public domain worldwide. This software is
# distributed without any warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication along with this software.
# If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
 
from __future__ import print_function
import argparse
import sys
 
def hexstr(buf):
    return ' '.join('{:02x}'.format(x) for x in buf)
 
def parse_binary(buf):
    if len(buf) < 18:
        raise ValueError('file too short, len=%d' % len(buf))
    if buf[:15] != b'MIPI DBI\x00\x00\x00\x00\x00\x00\x00':
        raise ValueError('wrong magic: %s' % hexstr(buf[:15]))
    if buf[15] != 1:
        raise ValueError('wrong version: %d' % (buf[15]))
 
    result = ''
    cmds = buf[16:]
    i = 0
    while i < len(cmds):
        try:
            pos = i
            cmd = cmds[i]
            i += 1
            num_params = cmds[i]
            i += 1
            params = cmds[i:i+num_params]
            if len(params) != num_params:
                raise IndexError()
 
            if cmd == 0x00 and num_params == 1:
                s = 'delay %d\n' % params[0]
            else:
                s = 'command 0x%02x' % cmd
                if params:
                    s += ' '
                    s += ' '.join('0x{:02x}'.format(x) for x in params)
                s += '\n'
        except IndexError:
            raise ValueError('malformed file at offset %d: %s' % (pos + 16, hexstr(cmds[pos:])))
        i += num_params
        result += s
 
    return result
 
def print_file(fn):
    with open(args.fw_file, mode='rb') as f:
        fw = f.read()
    s = parse_binary(bytearray(fw))
    print(s)
 
def parse_values(parts):
    vals = []
    for x in parts:
        try:
            val = int(x, 0)
        except ValueError:
            raise ValueError('not a number: %s' % x)
        if val < 0 or val > 255:
            raise ValueError('value out of bounds: %s (%d)' % (hex(val), val))
        vals.append(val)
    return vals
 
def make_file(fw_file, input_file):
    with open(input_file, mode='r') as f:
        lines = f.readlines()
 
    buf = bytearray()
    buf.extend(b'MIPI DBI\x00\x00\x00\x00\x00\x00\x00') # magic
    buf.append(1) # version
 
    for idx, line in enumerate(lines):
        # strip off comments and skip empty lines
        comment_idx = line.find('#')
        if comment_idx >= 0:
            line = line[:comment_idx]
        line = line.strip()
        if not line:
            continue
 
        try:
            parts = line.split()
            if parts[0] == 'command':
                vals = parse_values(parts[1:])
                buf.append(vals[0])
                num_params = len(vals) - 1
                buf.append(num_params)
                if num_params:
                    buf.extend(vals[1:])
            elif parts[0] == 'delay':
                vals = parse_values(parts[1:])
                if len(vals) != 1:
                    raise ValueError('delay takes exactly one argument')
                buf.append(0x00)
                buf.append(1)
                buf.append(vals[0])
            else:
                raise ValueError('unknown keyword: %s' % parts[0])
        except ValueError as e:
            raise ValueError('error: %s\nline %d: %s' % (e, idx + 1, line))
 
    with open(fw_file, mode='wb') as f:
        f.write(buf)
 
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='MIPI DBI Linux driver firmware tool')
    parser.add_argument('fw_file', help='firmware binary file')
    parser.add_argument('input', nargs='?', help='Input commands file')
    parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output')
    parser.add_argument('-d', '--debug', action='store_true', help='Print exception callstack')
    args = parser.parse_args()
 
    try:
        if args.input:
                make_file(args.fw_file, args.input)
                if args.verbose:
                    print_file(args.fw_file)
        else:
            print_file(args.fw_file)
    except Exception as e:
        if args.debug:
            raise
        print(e, file=sys.stderr)
        sys.exit(1)

执行以下命令

(踩坑!不知道为何,使用别的自定义bin文件名是不行的,必须使用默认文件名panel.bin)

创建bin文件:
./mipi-dbi-cmd panel.bin command.txt
复制到固件文件夹
sudo cp ./panel.bin /lib/firmware/
vim /boot/firmware/config.txt
 
在文件末尾添加:
 
dtoverlay=mipi-dbi-spi,spi0-0,speed=48000000
dtparam=width=480,height=320
dtparam=reset-gpio=6,dc-gpio=5
dtparam=write-only



----------------华丽的分割线------------------


fbtft显示框架适配(老旧技术,不推荐)

(该技术用不了桌面)

首先请克隆我创建的驱动仓库:https://github.com/sAkuraOfficial/st7796\_linux\_driver

dts文件编译

 dtc -I dts -O dtb -o rpi5-st7796s.dtbo rpi5-st7796s.dts
 sudo cp ./rpi5-st7796s.dtbo /boot/firmware/overlays       

内核模块文件编译

make clean && make 
 
sudo make rpi-install   

添加overlay

dtoverlay=rpi5-st7796s

内核版本太新的情况下,会导致在镜像源中无法下载到相应的内核头文件,需要降级内核

sakura-io@sakura-io ~ ❯ sudo apt install linux-image-6.12.34+rpt-rpi-v8
 
sakura-io@sakura-io ~ ❯ sudo update-initramfs -u
 
sakura-io@sakura-io ~ ❯ sudo cp /boot/vmlinuz-6.12.34+rpt-rpi-v8 /boot/firmware/
 
sudo cp /boot/initrd.img-6.12.34+rpt-rpi-v8 /boot/firmware/
 
sudo chmod 755 /boot/firmware/vmlinuz-6.12.34+rpt-rpi-v8
 
sudo chmod 755 /boot/firmware/initrd.img-6.12.34+rpt-rpi-v8
编辑/boot/firmware/config.txt
 
末尾加入
 
kernel=vmlinuz-6.12.34+rpt-rpi-v8
 
initramfs initrd.img-6.12.34+rpt-rpi-v8 followkernel
 
重启树莓派
 
sudo reboot

触摸屏驱动适配(Linux官方优化较差、不推荐使用)

dtoverlay=ads7846,cs=1,penirq=16,penirq_pull=2,speed=1000000,swapxy=1,xmin=260,xmax=3920,ymin=228,ymax=3944

树莓派屏幕线缆连接顺序:

f2cd6ad7-61c1-4ab3-a3e0-afcc9a513287.webp

附件:4.0寸显示屏资料

34180322-004b-4872-abc6-6abd53210192.pdf

c33f09e1-b49d-4ea8-a6b7-c565546fa4a9.pdf

ddc3b0b6-4a9d-4d26-a27a-c6b8d15037af.pdf

评论

评论

1 条
?
  1. 树
    树莓派仙人2026/10/2 21:00

    感谢,drm驱动好用。