创建工程文件夹
在sysdrv/drv_ko/创建led文件夹
修改Makefile
在led文件夹的同级Makefile添加上led文件夹
bash
ifeq ($(SYSDRV_PARAM), )
SYSDRV_PARAM:=../../Makefile.param
include $(SYSDRV_PARAM)
endif
CURRENT_DIR := $(shell pwd)
M_OUT_DIR ?= ../out
MODULE_NAME := led
build_target := modules
all: $(build_target)
@echo "build $(MODULE_NAME) done"
.PHONY: modules clean
$(MODULE_NAME)-objs := led.o
obj-m := $(MODULE_NAME).o
modules:
$(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERNEL_DIR) M=$(shell pwd) $@ -j12
cp $(shell pwd)/led.ko $(M_OUT_DIR)
@rm -rf *.o *~ .depend .*.cmd *.mod.c .tmp_versions *.symvers modules.order *.mod
clean:
@rm -rf *.o *~ .depend .*.cmd *.mod.c .tmp_versions *.ko *.symvers modules.order *.mod
创建源文件led.c
c
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/clk.h>
#include <linux/pwm.h>
#include <linux/file.h>
#include <linux/list.h>
#include <linux/gpio.h>
#include <linux/time.h>
#include <linux/hrtimer.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/kthread.h>
#include <linux/mempolicy.h>
#include <linux/miscdevice.h>
#include <linux/interrupt.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/module.h>
static int led_probe(struct platform_device *pdev)
{
printk("%s enter\n", __func__);
printk("%s leave\n", __func__);
return 0;
}
static int led_remove(struct platform_device *pdev)
{
printk("led_remove\n");
return 0;
}
struct of_device_id of_match_table = {
.compatible = "led"
};
static struct platform_driver led_platform_driver = {
.probe = led_probe,
.remove = led_remove,
.driver = {
.name = "led",
.owner = THIS_MODULE,
.of_match_table = &of_match_table,
}
};
static int __init led_init(void)
{
int ret;
ret = platform_driver_register(&led_platform_driver);
return ret;
}
static void __exit led_exit(void)
{
platform_driver_unregister(&led_platform_driver);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
这个文件只是把整个架构做出来,没有什么功能。
修改dts
在根节点添加led节点
bash
led: led{
compatible = "led";
status = "okay";
};
编译
在sdk的根目录下,通过命令编译
bash
./build.sh kernel
./build.sh driver
更新下位机的boot.img
测试
把sysdrv/drv_ko/out/led.ko 复制到下位机/oem/usr/ko文件夹中
bash
insmod ./led.ko
rmmod led.ko
得到输出
bash
[ 97.620096] led_probe enter
[ 97.620123] led_probe leave
[ 149.878684] led_remove
驱动加载和卸载成功