编写嵌入式linux驱动时,如何才能自动加载设备并生成节点

目前没啥进展

普通linux下使用 udev即可

struct class* my_class=class_creat(THIS_MODULE,“dev_name”);

my_device = device_create(my_class,NULL,MKDEV(my_major,0).NULL,"dev_name");

但是这个udev是生成设备文件,但是如果不mknod一下,寄存器读出来的都是fffff.

没办法,查找懒兔子的blog发现他的方法可以用,同时也完成了开机自启动

第一步,备份ramdisk8M.image.gz

第二步,加载rootfs镜像文件:

cd /
mkdir sdcard
ls
mount /dev/mmcblk0p1 /sdcard
cd sdcard/
cp /sdcard/ramdisk8M.image.gz /tmp/
gunzip /tmp/ramdisk8M.image.gz
mount -o loop /tmp/ramdisk8M.image /mnt/
/**完成以后rootfs镜像就被加载到/mnt目录下了,可以通过指令查看一下效果:**/
cd /mnt
ls

第三步,修改/etc/init.d/rcS文件,可以看到Demo板的各个外围设备开机配置(如FTPHTTP服务器等)都是通过这个文件实现的,在文件末尾加入指令,让驱动模块自动加载,并运行我们的App测试程序。

mount /dev/sda2 /mnt     
cd mnt                     //sda2 是usb设备编号
insmod my_dev.ko
mknod /dev/my_dev c 251 0  // 设备文件  设备类型  主设备号  子设备号
./my_dev_app.out

(注:修改rcS文件可以通过cat指令,先要复制rcS文件原有内容哦~~)

最后一步,重新打包根文件系统镜像,覆盖到SD卡中。

umount -l /mnt
gzip -9 /tmp/ramdisk8M.image
mv /tmp/ramdisk8M.image.gz /sdcard/
umount -l /sdcard/

重上电,驱动自动加载,软件自动运行。That’s it.

另外Geek还制作了一个脚本,只要两条指令就能完成上面加载和重打包的工作,脚本可以在这里下载:

#!/bin/sh

#
# tool: mount_sd_and_ramdisk
#
# simple script to mount both the sdcard and ramdisk image
# for the Avnet Zedboard
#
# Written by ZynqGeek
#

# start

# create our two mount point directorys
echo Creating Directories ...
cd /
mkdir sdcard
mkdir ramdisk
echo Done

# mount our sd card
echo Mounting SD-Card ...
mount /dev/mmcblk0 /sdcard
echo ... Done

# make a copy of the compressed ramdisk image locally
echo Copying Ram Disk Image to /tmp ...
cp /sdcard/ramdisk8M.image.gz /tmp/
... Done

# unzip the image, so we can mount it
echo Unzipping the image ...
gunzip /tmp/ramdisk8M.image.gz
echo ... Done

# mount the image to the directory we created
echo Mounting the image to /ramdisk ...
mount -o loop /tmp/ramdisk8M.image /ramdisk
echo ... Done

# all done!
mount

下面是un

#!/bin/sh

#
# tool: umount_sd_and_ramdisk
#
# simple script to unmount, recompress, and move over the ramdisk image,
# to the sd-card as well as unmount the sd-card
#
# Written by ZynqGeek
#

# start

# unmount ramdisk image
echo Unmounting Ram Disk ...
umount -l /ramdisk
echo ... Done

# compress ramdisk image
echo Compressing Ram Disk Image ... this make take a few seconds ...
gzip -9 /tmp/ramdisk8M.image
echo ... done.

# make a back-up on the SD-Card
echo Making Copy of Existing Ram Disk Image ...
cp /sdcard/ramdisk8M.image.gz /sdcard/ramdisk8M.image.gz.orig
echo ... Done

# move the compressed image back to the sd-card
echo Overwriting Existing Compressed Image on SD-Card ...
mv /tmp/ramdisk8M.image.gz /sdcard/ramdisk8M.image.gz.new
echo ... Done

# unmount the sd-card
echo Unmounting SD-Card ...
umount -l /sdcard
echo ... Done
原文地址:https://www.cnblogs.com/puck/p/3023869.html