設置 | 登錄 | 註冊

目前共有10篇帖子。

Luckfox RV1103開發板修改設備樹的方法

1樓 巨大八爪鱼 2025-3-14 10:41
備兩份內核代碼。
一份專門用來用./build.sh生成鏡像並燒錄,設備樹也在這裏面改。
另一份專門用來編譯內核模塊,不用改裏面的設備樹。
cp -r /home/oct1158/Documents/Code/C/luckfox-pico/sysdrv/source/kernel /home/oct1158/Documents/Code/C/kernel_for_modules
cd /home/oct1158/Documents/Code/C/kernel_for_modules
make luckfox_rv1106_linux_defconfig ARCH=arm CROSS_COMPILE=/home/oct1158/Documents/Code/C/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin/arm-rockchip830-linux-uclibcgnueabihf-
make ARCH=arm CROSS_COMPILE=/home/oct1158/Documents/Code/C/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin/arm-rockchip830-linux-uclibcgnueabihf-
2樓 巨大八爪鱼 2025-3-14 10:42

修改設備樹後重新編譯內核:
cd /home/oct1158/Documents/Code/C/luckfox-pico
./build.sh
燒錄到板子上:
(板子拔掉USB線,按住BOOT鍵重新插上)
sudo ./rkflash.sh update

然後用另一份沒有改設備樹的內核代碼重新編譯驅動模塊:
cd /home/oct1158/Documents/Code/C/driver_test/first
(記得把Makefile裏面的內核目錄KDIR改成/home/oct1158/Documents/Code/C/kernel_for_modules)
make

上傳到板子上運行:
adb push test.ko /root(如果失敗,那就需要先sudo adb shell一下,再exit)
sudo adb shell
cd /root
insmod test.ko

查看新增的設備樹節點:
cat /sys/firmware/devicetree/base/mytest/compatible
cat /sys/firmware/devicetree/base/mytest/gpios
cat /sys/firmware/devicetree/base/mytest/name
cat /sys/firmware/devicetree/base/mytest/xxxyyy

3樓 巨大八爪鱼 2025-3-14 10:43

從打印輸出裏面可以看到,編譯好的設備樹文件最終是放到boot.img鏡像裏面的。

Image:  resource.img (with rv1103g-luckfox-pico-mini.dtb logo.bmp logo_kernel.bmp) is ready
Image:  boot.img (FIT image with Linux kernel, FDT blob and resource) is ready

4樓 巨大八爪鱼 2025-3-14 11:02

設備樹文件rv1103g-luckfox-pico-mini.dts的修改內容:

/ {}裏面添加

mytest {
  compatible = "mytest,myboard_20250314";
  gpios = <&gpio1 RK_PA2 GPIO_ACTIVE_HIGH>;
  xxxyyy = "https://zh.purasbar.com/post.php?t=31629";
};

5樓 巨大八爪鱼 2025-3-14 11:05

內核模塊test.c的代碼:

#include <linux/gpio/consumer.h> // gpiod_set_value
#include <linux/module.h> // MODULE_AUTHOR, MODULE_LICENSE
#include <linux/of.h> // of_match_ptr
#include <linux/platform_device.h> // module_platform_driver

static int mytestdriver_probe(struct platform_device *pdev)
{
    const char *str;
    int ret;
    struct gpio_desc *gpio;
   
    dev_err(&pdev->dev, "probe");
    gpio = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_HIGH); // led on
    if (IS_ERR(gpio))
    {
        dev_err(&pdev->dev, "gpio error\n");
        return -1;
    }
    platform_set_drvdata(pdev, gpio);
   
    ret = of_property_read_string(pdev->dev.of_node, "xxxyyy", &str);
    if (ret == 0)
        dev_err(&pdev->dev, "str=%s\n", str);
    return 0;
}

static int mytestdriver_remove(struct platform_device *pdev)
{
    struct gpio_desc *gpio;
   
    dev_err(&pdev->dev, "remove");
    gpio = platform_get_drvdata(pdev);
    if (gpio != NULL)
    {
        gpiod_set_value(gpio, 0); // led off
        platform_set_drvdata(pdev, NULL);
    }
    return 0;
}

static const struct of_device_id mytestdriver_match_ids[] = {
    {.compatible = "mytest,myboard_20250314"},
    {}
};

static struct platform_driver mytestdriver = {
    .driver = {
        .name = "mytestdriver",
        .owner = THIS_MODULE,
        .of_match_table = of_match_ptr(mytestdriver_match_ids)
    },
    .probe = mytestdriver_probe,
    .remove = mytestdriver_remove
};

module_platform_driver(mytestdriver);

MODULE_AUTHOR("Oct1158");
MODULE_LICENSE("GPL");

巨大八爪鱼用devm_函數獲取的資源,會在設備卸載時自動釋放,不需要在remove函數裏面手動釋放。
巨大八爪鱼of_property_read_string的第三個參數返回的字符串可以直接使用,用完不需要釋放。
文檔上寫的:Search for a property in a device tree node and retrieve a null terminated string value (pointer to data, not a copy).
6樓 巨大八爪鱼 2025-3-14 11:07

內核模塊Makefile文件:

KDIR := /home/oct1158/Documents/Code/C/kernel_for_modules
CROSS_COMPILE := /home/oct1158/Documents/Code/C/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin/arm-rockchip830-linux-uclibcgnueabihf-

obj-m := test.o

build:
    $(MAKE) -C $(KDIR) M=$(shell pwd) modules ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE)

clean:
    $(MAKE) -C $(KDIR) M=$(shell pwd) clean

 

7樓 巨大八爪鱼 2025-3-14 11:10

[root@luckfox root]# cat /sys/firmware/devicetree/base/mytest/compatible && echo
 ""
mytest,myboard_20250314
[root@luckfox root]# cat /sys/firmware/devicetree/base/mytest/gpios && echo ""
6
[root@luckfox root]# cat /sys/firmware/devicetree/base/mytest/name && echo ""
mytest
[root@luckfox root]# cat /sys/firmware/devicetree/base/mytest/xxxyyy && echo ""
https://zh.purasbar.com/post.php?t=31629
[root@luckfox root]#

[root@luckfox root]# insmod test.ko
[ 1290.848314] mytestdriver mytest: probe
[ 1290.848491] mytestdriver mytest: str=https://zh.purasbar.com/post.php?t=31629
命令執行後,板子上的紅燈常亮。

[root@luckfox root]# rmmod test
[ 1293.102352] mytestdriver mytest: remove
命令執行後,板子上的紅燈熄滅。

8樓 巨大八爪鱼 2025-3-14 11:17
【解決辦法】編譯linux內核或模塊時遇到錯誤不顯示報錯信息:https://zh.purasbar.com/post.php?t=31654

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。