# harmony-raspberry **Repository Path**: liangzili/harmony-raspberry ## Basic Information - **Project Name**: harmony-raspberry - **Description**: 移植鸿蒙Harmony到树莓派 - **Primary Language**: Unknown - **License**: BSD-3-Clause - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 217 - **Forks**: 55 - **Created**: 2020-12-11 - **Last Updated**: 2025-06-24 ## Categories & Tags **Categories**: harmonyos-devtools **Tags**: None ## README # harmony-raspberry #### 介绍 努力将 OpenHarmony 移植到树莓派 raspberryPi # OHOS3.0 - 树莓派4B ## 一、添加编译配置文件和gn ### 1.添加RPI4B.json 主要参考 [hisilicon build组件仓](https://gitee.com/openharmony/device_hisilicon_build),添加一个products编译组件,这个组件是在产品配置文件中指定的。比如 productdefine\common\products\RPI4B.json 其他部分参考Hi3516,但是其中2条,指定单板组件路径,并添加组件。如果删除这两条,将不能编译内核,只生成OHOS的文件系统。 ```json ... .. "product_build_path": "device/raspberrypi/build", "parts":{ ... ... "raspberrypi_products:raspberrypi_products":{}, ... ... } ``` ### 2.添加ohos.build 接下来在device目录下,新建一个raspberrypi编译组件文件夹,并添加 ohos.build 文件。和前面产品配置文件中的设置对应起来了。 device\raspberrypi\build\ohos.build ``` { "subsystem": "raspberrypi_products", "parts": { "raspberrypi_products": { "module_list": [ "//device/raspberrypi/build:products_group" ] } } } ``` 新建 device\raspberrypi\build\BUILD.gn 当然每个厂家不可能只有1个板子,如果有其他单板就在这里指定,比如树莓派2B、3B等 ``` import("//build/ohos.gni") device_type = "rpi4b" group("products_group") { deps += [ "//device/raspberrypi/rpi4b:rpi4b_group" ] } ``` 既然前面指定了rpi4b的编译配置组件,那么就在 device\raspberrypi 新建一个 rpi4b 的目录,可以参考 [hi3516dv300 build组件](https://gitee.com/openharmony/device_hisilicon_hi3516dv300) device\raspberrypi\rpi4b\BUILD.gn ``` import("//build/ohos.gni") print("rpi4b_group in") group("rpi4b_group") { deps = [ "build/rootfs:init_configs", "//kernel/linux/build:linux_kernel" ] } ``` 至此一个rpi4b build组件就添加到OHOS3.0的编译框架了,之后相关内容添加到这个文件夹下就可以了。 ## 二、树莓派内核相关 接下来分析下目前移植了树莓派4B的哪些内容,如何将这些内容编译进OHOS3.0。 ### 1.raspberrypi内核补丁文件 关于补丁可以参考 [Patch组件](https://gitee.com/openharmony/kernel_linux_patches),可以得知内核编译由kernel.mk来执行 kernel\linux\build\kernel.mk ```makefile DEVICE_PATCH_DIR := $(OHOS_BUILD_HOME)/kernel/linux/patches/${KERNEL_VERSION}/$(DEVICE_NAME)_patch DEVICE_PATCH_FILE := $(DEVICE_PATCH_DIR)/$(DEVICE_NAME).patch ... ... $(KERNEL_IMAGE_FILE): $(hide) echo "build kernel..." $(hide) rm -rf $(KERNEL_SRC_TMP_PATH);mkdir -p $(KERNEL_SRC_TMP_PATH);cp -arfL $(KERNEL_SRC_PATH)/* $(KERNEL_SRC_TMP_PATH)/ $(hide) cd $(KERNEL_SRC_TMP_PATH) && patch -p1 < $(HDF_PATCH_FILE) && patch -p1 < $(DEVICE_PATCH_FILE) ifneq ($(findstring $(BUILD_TYPE), small),) $(hide) cd $(KERNEL_SRC_TMP_PATH) && patch -p1 < $(SMALL_PATCH_FILE) endif ``` 所以补丁文件需要放到正确的路径下,以正确的名字命名就可以patch到内核。 hdf.patch补丁文件,现在还没有移植HDF相关内容,所以可以先使用Hi3516的 rpi4b.patch补丁文件,使用树莓派的官方镜像,[https://github.com/raspberrypi/linux](https://github.com/raspberrypi/linux) ``` kernel\linux\patches\linux-5.10\rpi4b_patch\hdf.patch kernel\linux\patches\linux-5.10\rpi4b_patch\rpi4b.patch ``` ### 2.内核编译配置文件 kernel\linux\config\linux-5.10\arch\arm\configs\rpi4b_standard_defconfig 内核配置文件目前已知的需要开启下面内容,但是肯定不止这些,以后会继续更新 ```python ~/ohos/kernel/linux/config/linux-5.10/arch/arm/configs/rpi4b_standard_defconfig ##################################################################################### > Security options > (32768) Low address space for LSM to protect from user allocation [*] NSA SELinux Support #(选中) [*] NSA SELinux boot parameter #(选中) [ ] NSA SELinux runtime disable [*] NSA SELinux Development Support [*] NSA SELinux AVC Statistics (1) NSA SELinux checkreqprot default value #(设置为1) (9) NSA SELinux sidtab hashtable size (256) NSA SELinux SID to context string translation cache size First legacy 'major LSM' to be initialized (SELinux) ---> #(选中) SELinux Ordered list of enabled LSMs #(填入:"lockdown,yama,loadpin,safesetid,integrity,selinux,smack,tomoyo,apparmor,bpf") ##################################################################################### > Device Drivers > Android [*] Android Drivers #(开启) [*] Android Binder IPC Driver #(开启) ##################################################################################### > Device Drivers > Sound card support <*> Advanced Linux Sound Architecture #(选中,直接编进内核) <*> ALSA for SoC audio support #(选中,直接编进内核) > Device Drivers > Graphics support <*> Direct Rendering Manager (XFree86 4.1.0 and higher DRI support) #(选中,直接编进内核) <*> Broadcom V3D 3.x and newer #(选中,直接编进内核) <*> Broadcom VC4 Graphics #(选中,这个依赖前面的声卡设置,不然是无法编入内核的) ##################################################################################### > Device Drivers > Input device support > Touchscreens <*> Raspberry Pi's firmware base touch screen support #(选中,直接编进内核) ``` ## 三、驱动适配 ### 1.显示配置 Pi4的GPU是VideoCore VI支持OpenGL ES 3.2,而Pi3的GPU是VideoCore IV支持OpenGL ES 2.0。VideoCore IV 驱动程序是 VC4,VideoCore VI 驱动程序的 V3D。内核已经提供驱动,参考rpi4b_standard_defconfig将驱动直接编入到内核。 同时需要在config.txt中开启设置 ``` dtoverlay=vc4-fkms-v3d ``` OHOS中修改weston的配置文件,指定显示驱动 system\etc\weston.ini ```c [output] name=card0 ``` ### 2.触摸配置 具体思路就是先查找设备号,根据设备号找到驱动程序。 ``` ls -l /sys/dev/char/|grep input # 查看input下的触摸设备的主次设备号 cat /sys/dev/char/13\:64/device/uevent # 然后输入主次设备号,查看设备的驱动程序 PRODUCT=19/0/0/0 NAME="raspberrypi-ts" PROP=2 EV=b KEY=400 0 0 0 0 0 0 0 0 0 0 ABS=2608000 3 MODALIAS=input:b0019v0000p0000e0000-e0,1,3,k14A,ra0,1,2F,35,36,39,mlsfw ``` 前面内核配置的时候rpi4b_standard_defconfig中已经将触摸驱动编入内核,所以后面不需要在init加载模块了,修改下eudev的配置文件即可。 third_party\eudev\rules.d\touchscreen.rules ``` ATTRS{name}=="raspberrypi-ts", ENV{ID_INPUT}="1", ENV{ID_INPUT_TOUCHSCREEN}="1" ATTRS{name}=="VSoC keyboard", ENV{ID_INPUT}="1", ENV{ID_INPUT_KEYBOARD}="1" DRIVERS=="hid-multitouch", ENV{ID_INPUT}="1", ENV{ID_INPUT_TOUCHSCREEN}="1" ``` ## 四、制作镜像文件 ### 1.修改内核编译脚本 正常情况下内核是由uboot进行引导的,而且OHOS默认生成uImage。但是树莓派自带BootLoader,虽然可以先用树莓派自带的BootLoader启动uboot,再用uboot加载uImage,但是这样会比较麻烦,而且会增加启动时间。不过目前 zImage是写死在kernel.mk中的,没办法改下编译脚本把。 kernel\linux\build\kernel.mk 将 uImage 改为 zImage modules dtbs ``` $(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) -j64 zImage ``` kernel\linux\build\build_kernel.sh ``` - cp ${2}/kernel/src_tmp/${8}/arch/arm/boot/uImage ${3}/uImage + cp ${2}/kernel/src_tmp/${8}/arch/arm/boot/zImage ${3}/zImage ``` kernel\linux\build\BUILD.gn ``` - outputs = [ "$root_build_dir/packages/phone/images/uImage" ] + outputs = [ "$root_build_dir/packages/phone/images/zImage" ] ``` kernel\linux\build\kernel_module_build.sh ``` - LINUX_KERNEL_UIMAGE_FILE=${LINUX_KERNEL_OUT}/arch/arm/boot/uImage + LINUX_KERNEL_UIMAGE_FILE=${LINUX_KERNEL_OUT}/arch/arm/boot/zImage ``` 这里内核编译会依赖product_path="vendor/$product_company/$product_name"下的hdf.hcs文件,得先新建一个应付下,不然会报下面这个错误。 ninja: error: '../../vendor/raspberrypi/RPI4B/hdf_config/uhdf/hdf.hcs', needed by 'gen/drivers/adapter/uhdf2/hcs/hdf_default.hcb', missing and no known rule to make it 新建:vendor/raspberrypi/RPI4B/hdf_config/uhdf/hdf.hcs ``` root { module = "default"; } ``` ### 2.制作树莓派boot目录 对于镜像烧录,Hi3516会将uImage、system.img、vendor.img等镜像烧写到emmc,但是树莓派使用TF卡启动,所以需要对TF卡进行分区,然后复制对应的内容到各个分区。首先制作树莓派boot目录,这个用来目录存放树莓派设备树、config.txt、cmdline.txt、内核镜像等信息。写一个简单的mkboot.py脚本来实现这个功能,位置在码仓rpi4b\device\raspberrypi\images\mkboot.py将会生成boot.img。 为了方便烧录,需要将boot.img、system.img、updater.img、vendor.img、userdata.img合并成一个rpi4b.img。还是写一个简单的脚本来处理这个步骤rpi4b\device\raspberrypi\images\mkboot.py。 不过有个问题,主分区只支持4个,所以updater.img暂时先不合并了,这个问题等以后再来处理。 最后将会得到一个rpi4b.img的镜像文件,将这个文件烧录到SD卡就可以了。 Linux:可以使用dd命令 windows:使用[Win32 Disk Imager](https://sourceforge.net/projects/win32diskimager/)工具烧录即可。 到这里总算是跑通了一个完整的添加新单板的流程,只不过目前只适配了显示和触摸。接下来打算尝试HDF或者distributed部分。 # OHOS1.0 - 树莓派2B ### 1、前期准备 1.1、[环境搭建](https://gitee.com/liangzili/harmony-raspberry/blob/master/doc/1.1%E3%80%81%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA.md) 1.2、[源码下载](https://gitee.com/liangzili/harmony-raspberry/blob/master/doc/1.2%E3%80%81%E6%BA%90%E7%A0%81%E4%B8%8B%E8%BD%BD.md) 1.3、[树莓派启动流程](https://gitee.com/liangzili/harmony-raspberry/blob/master/doc/1.3%E3%80%81%E6%A0%91%E8%8E%93%E6%B4%BE%E5%90%AF%E5%8A%A8%E6%B5%81%E7%A8%8B.md) 1.4、[树莓派U-Boot编译.md](https://gitee.com/liangzili/harmony-raspberry/blob/master/doc/1.4%E3%80%81%E6%A0%91%E8%8E%93%E6%B4%BEU-Boot%E7%BC%96%E8%AF%91.md) ### 2、代码移植 2.1、[增加新单板](https://gitee.com/liangzili/harmony-raspberry/blob/master/doc/2.1%E3%80%81%E5%A2%9E%E5%8A%A0%E6%96%B0%E5%8D%95%E6%9D%BF.md) 2.2、[启动设置](https://gitee.com/liangzili/harmony-raspberry/blob/master/doc/2.2%E3%80%81%E5%90%AF%E5%8A%A8%E8%AE%BE%E7%BD%AE.md) 2.3、[串口分析移植](https://gitee.com/liangzili/harmony-raspberry/blob/master/doc/2.3%E3%80%81%E4%B8%B2%E5%8F%A3%E5%88%86%E6%9E%90%E7%A7%BB%E6%A4%8D.md) 2.4、[系统时钟](https://gitee.com/liangzili/harmony-raspberry/blob/master/doc/2.4%E3%80%81%E7%B3%BB%E7%BB%9F%E6%97%B6%E9%92%9F.md) 2.5、[块设备驱动程序](https://gitee.com/liangzili/harmony-raspberry/blob/master/doc/2.5%E3%80%81%E5%9D%97%E8%AE%BE%E5%A4%87%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BA%8F.md)