2019-2020-1 20175221 20175225 20175226 实验四 外设驱动程序设计
实验目的
- 学习资源中全课中的“hqyj.嵌入式Linux应用程序开发标准教程.pdf”中的第十一章
- 在Ubuntu完成资源中全课中的“hqyj.嵌入式Linux应用程序开发标准教程.pdf”中的第十一章的test试验
提交编译,加载模块,卸载模块,测试运行的截图
实验步骤
-
(一)笔记
- 1.阅读pdf的十一章,学习笔记如下图所示:
-
(二)test试验
-
test_drv_unload
-
#!/bin/sh module="test_drv" device="test_dev" # invoke rmmod with all arguments we got /sbin/rmmod $module $* || exit 1 # remove nodes rm -f /dev/${device} exit 0
-
Makefile
-
ifeq ($(KERNELRELEASE),) KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules modules_install: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install clean: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions .PHONY: modules modules_install clean else obj-m := test_drv.o endif
-
test.c
-
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> #define TEST_DEVICE_FILENAME "/dev/test_dev" #define BUFF_SZ 1024 int main() { int fd, nwrite, nread; char buff[BUFF_SZ]; fd = open(TEST_DEVICE_FILENAME, O_RDWR); if (fd < 0) { perror("open"); exit(1); } do { printf("Input some words to kernel(enter 'quit' to exit):"); memset(buff, 0, BUFF_SZ); if (fgets(buff, BUFF_SZ, stdin) == NULL) { perror("fgets"); break; } buff[strlen(buff) - 1] = '