实验六 LED驱动的移植
【实验目的】
1. 驱动程序的编译
2. 应用程序如何打开/操作设备
【实验环境】
• 主机:ubuntu 12.04发行版
• 目标机:FS4412平台
• 交叉编译工具:arm-none-linux-gnueabi-gcc
【实验步骤】
1. 添加驱动文件
将实验代码Led_test/fs4412_led_drv.c拷贝到drivers/char下
2. 修改drivers/char/Kconfig
在menu "Character devices"下面
添加如下内容:
config FS4412_LED
tristate "FS4412 LED Device Support"
depends on ARCH_EXYNOS4
help
support led device on FS4412 develop board
3. 修改drivers/char/Makefile
在文件最后添加如下代码
obj-$(CONFIG_FS4412_LED) +=fs4412_led_drv.o
4. 将s5pv210_led_app.c拷贝到Linux任意目录下并交叉编译测试程序
$ arm-none-linux-gnueabi-gcc fs4412_led_app.c –o fs4412_led_app
sudo cp fs4412_led_app /source/rootfs
5. 编译LED驱动到内核中
Ø 配置内核时按“空格”选择,配置完成后保存退出
$make menuconfig
Device Drivers --->
Character devices --->
<*>FS4412 LED Device Support
Ø 保存退出,重新编译后把uImage拷贝到tftpboot下
$ make uImage
$ cp arch/arm/boot/uImage /tftpboot
重新启动开发板,加载内核并运行。在终端下执行下面操作
Ø 创建设备节点
# mknod /dev/led c 500 0
Ø 运行测试程序并观察现象
#./fs4412_led_app
6. 编译LED驱动为模块
Ø 配置内核时按“空格”选择,配置完成后保存退出
$ make menuconfig
Device Drivers --->
Character devices --->
<M>FS4412 LED Device Support
Ø 保存退出,重新编译后把zImage拷贝到tftpboot下,把驱动模块拷贝到/source/rootfs下
$ make uImage modules
$ cp arch/arm/boot/uImage /tftpboot/
$ cp drivers/char/fs4412_led_drv.ko /source/rootfs/
重新启动开发板,linux运行起来后在终端下操作
Ø 创建设备节点
# mknod /dev/led c 500 0
Ø 加载LED驱动模块
# insmod fs4412_led_drv.ko
Ø 运行测试程序并观察现象
#./fs4412_led_app
fs4412_led_drv.c源代码如下:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/uaccess.h>
MODULE_LICENSE("Dual BSD/GPL");
#define LED_MAGIC 'L'
/*
* need arg = 1/2
*/
#define LED_ON _IOW(LED_MAGIC, 0, int)
#define LED_OFF _IOW(LED_MAGIC, 1, int)
#define LED_MA 500
#define LED_MI 0
#define LED_NUM 1
#define FS4412_GPX2CON 0x11000C40
#define FS4412_GPX2DAT 0x11000C44
static unsigned int *gpx2con;
static unsigned int *gpx2dat;
struct cdev cdev;
static int s5pv210_led_open(struct inode *inode, struct file *file)
{
return 0;
}
static int s5pv210_led_release(struct inode *inode, struct file *file)
{
return 0;
}
static long s5pv210_led_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int nr;
switch (cmd) {
case LED_ON:
writel(readl(gpx2dat) | 1 << 7, gpx2dat);
break;
case LED_OFF:
writel(readl(gpx2dat) & ~(1 << 7), gpx2dat);
break;
}
return 0;
}
struct file_operations s5pv210_led_fops = {
.owner = THIS_MODULE,
.open = s5pv210_led_open,
.release = s5pv210_led_release,
.unlocked_ioctl = s5pv210_led_unlocked_ioctl,
};
static int s5pv210_led_init(void)
{
dev_t devno = MKDEV(LED_MA, LED_MI);
int ret;
ret = register_chrdev_region(devno, LED_NUM, "newled");
if (ret < 0) {
printk("register_chrdev_region\n");
return ret;
}
cdev_init(&cdev, &s5pv210_led_fops);
cdev.owner = THIS_MODULE;
ret = cdev_add(&cdev, devno, LED_NUM);
if (ret < 0) {
printk("cdev_add\n");
goto err1;
}
gpx2con = ioremap(FS4412_GPX2CON, 4);
if (gpx2con == NULL) {
printk("ioremap gpx2con\n");
ret = -ENOMEM;
goto err2;
}
gpx2dat = ioremap(FS4412_GPX2DAT, 4);
if (gpx2dat == NULL) {
printk("ioremap gpx2dat\n");
ret = -ENOMEM;
goto err3;
}
writel((readl(gpx2con) & ~(0xf << 28)) | (0x1 << 28), gpx2con);
writel(readl(gpx2dat) & ~(0x1<<7), gpx2dat);
printk("Led init\n");
return 0;
err3:
iounmap(gpx2con);
err2:
cdev_del(&cdev);
err1:
unregister_chrdev_region(devno, LED_NUM);
return ret;
}
static void s5pv210_led_exit(void)
{
dev_t devno = MKDEV(LED_MA, LED_MI);
iounmap(gpx2dat);
iounmap(gpx2con);
cdev_del(&cdev);
unregister_chrdev_region(devno, LED_NUM);
printk("Led exit\n");
}
module_init(s5pv210_led_init);
module_exit(s5pv210_led_exit);
fs34412_led_app.c源码如下:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#define LED_MAGIC 'L'
#define LED_ON _IOW(LED_MAGIC, 0, int)
#define LED_OFF _IOW(LED_MAGIC, 1, int)
int main(int argc, char **argv)
{
int fd;
fd = open("/dev/led", O_RDWR);
if (fd < 0) {
perror("open");
exit(1);
}
while(1)
{
ioctl(fd, LED_ON);
usleep(100000);
ioctl(fd, LED_OFF);
usleep(100000);
}
return 0;
}