目录
1.分析:完善硬件操作
①.看原理图,确定引脚。
②.看 2440 的芯片手册。查如何操作引脚。
从原理图上看, GPF4~6 引脚输出 0 , LED 会亮。
1.查原理图看到三个 LED 灯接的引脚是 GPF4~6,则再看 2440 芯片手册操作相应的寄存
器。在芯片手册中搜索 GPFCON 寄存器。
2. GPF4~6 配置成Output输出引脚
③.写代码。
写单片机时是直接操作那个物理地址。
驱动程序中不能直接操作物理地址了,是操作虚拟地址。用 ioremap 函数来将物理地址
映射成虚拟地址。
配置引脚:操作 GPFCON 寄存器。(放到 open 函数中做)
设置:设备 GPFDAT 寄存器,让引脚输出高、低电平。(放到 wirte 函数中做)
Linux 系统中使用的全部是虚拟地址,是经过 MMU 映射后的地址。
Linux 系统要访问物理地址必须先把物理地址转换成虚拟地址。
内核提供一个地址转换函数: ioremap
//asm/io.h 中定义, 用于把物理址映射 成虚拟地址, cookie--物理地址, size--虚拟地址,
//#define ioremap(cookie,size) __arm_ioremap((cookie), (size), MT_DEVICE)
原型: ioremap(cookie,size)
功能: 把 cookie 开头的物理地址, size 大小的空间, 映射成虚拟地址
参数: cookie--物理地址, size--虚拟地址,
返回值: void __iomem*类型,表示映射后的虚拟地址。
内核可以通过映射后虚拟地址操作对应的物理地址空间。
和 ioremap 函数相反的一个函数是 iounmap 函数,用于释放映射后的虚拟地址空间。
原型: void iounmap(void __iomem *addr)
addr : 要释放的虚拟地址指针 。
1,映射物理地址:
先定义两个变量,和寄存器相对应。他们分别的物理地址要分别映射成虚拟地址。
在入口函数中映射(避免打开一次要映射一次)。
用ioremap (开始地址,结束大小 )
GPFCON 寄存器的地址,后面 16 是 16 字节,这里刚好有 4 个寄存器,每个 4 字节,所以就定成 16 字节了。这里加 1 不是加了 4 字节,指针的操作是以指针所指向的长度为单位的,这里的指针是:“(volatile unsigned long *)”
在入口函数中用 ioreamp() ,则在出口函数中要用 iounmap(),将建立的映射去掉。
iounmap(gpfcon)
以上就是物理地址与虚拟地址的映射,以后要操作寄存器时,就用 gpfcon 和 gpfdat
两个指针来操作。这两个指针指向的是虚拟地址。
2.先清零在配置IO为输出
3,将用户空间的数据传到内核空间:
val 如何传进来,就是 *buf ,应用程序中调用的时候:
从用户空间到内核空间的数据传递函数 copy_from_user()。
从内核空间向用户空间传递数据用函数 copy_to_user();
将传来的 buf 拷贝到 val(定义的)空间,拷贝的长度是 count(应用程序传进来的)。
接着根据这个值判断:如果这个值 val==1,打开 LED(输出低电平)。否则关掉 LED 灯(输
出高电平)。
打开和关闭 LED,就是操作寄存器。
2.驱动源码:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static ssize_t first_leds_write(struct file *pfile, const char __user *buf, size_t count, loff_t *off)
{
int val;
copy_from_user(&val, buf, count);
if(val == 1)
{
*gpfdat &= ~((1 << 4) || (1 << 5) || (1 << 6));
}
else
{
*gpfdat |= ((1 << 4) || (1 << 5) || (1 << 6));
}
return 0;
}
static int first_leds_open(struct inode *pinode, struct file *pfile)
{
/* 配置GPF4,5,6为输出 */
*gpfcon &= ~((0x3 << (4*2)) || (0x3 << (5*2)) || (0x3 << (6*2)));
*gpfcon |= ~((0x1 << (4*2)) || (0x1 << (5*2)) || (0x1 << (6*2)));
return 0;
}
static const struct file_operations leds_fops = {
.owner = THIS_MODULE,
.open = first_leds_open,
.write = first_leds_write,
};
static struct class *leds_class;
static struct class_device *leds_devices;
static unsigned int major = 0;
static int __init myleds_drv_init()
{
int ret = -1;
int minor = 0;
major = register_chrdev(0, "leds_drv", &leds_fops);
if(major < 0)
{
printk(KERN_EMERG"register_chrdev error\n");
return -1;
}
printk(KERN_EMERG"register_chrdev ok\n");
leds_class = class_create(THIS_MODULE, "leds");
if (IS_ERR(leds_class)) {
//printk(KERN_ERR "%s: couldn't create class\n", __FILE__);
return PTR_ERR(leds_class);
}
leds_devices = class_device_create(leds_class, NULL, MKDEV(major, 0), NULL, "myleds");
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
static void __exit myleds_drv_exit()
{
int ret = -1;
ret = unregister_chrdev(major, "leds_drv");
if(ret < -1)
{
printk(KERN_EMERG"unregister_chrdev error\n");
return ;
}
class_device_unregister(leds_devices);
class_destroy(leds_class);
iounmap(gpfcon);
}
module_init(myleds_drv_init);
module_exit(myleds_drv_exit);
MODULE_LICENSE("GPL");
3.测试源码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int fd = -1;
int val = 0;
fd = open("/dev/leds_drv", O_RDWR);
if(fd < -1)
{
printf("open error\n");
return -1;
}
if(argc != 2)
{
printf("Usage :\n");
printf("%s on | off\n", argv[0]);
return 0;
}
if(strcmp(argv[1], "on") == 0)
{
val = 1;
}
else
{
val = 0;
}
write(fd, &val, 4);
return 0;
}
4.内核提供的驱动源码
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#define DEVICE_NAME "leds" /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define LED_MAJOR 231 /* 主设备号 */
static struct class *leds_class;
static struct class_device *leds_class_devs[4];
/* bit0<=>D10, 0:亮, 1:灭
* bit1<=>D11, 0:亮, 1:灭
* bit2<=>D12, 0:亮, 1:灭
*/
static char leds_status = 0x0;
static DECLARE_MUTEX(leds_lock); // 定义赋值
//static int minor;
static unsigned long gpio_va;
#define GPIO_OFT(x) ((x) - 0x56000000)
#define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))
#define GPFDAT (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000054)))
/* 应用程序对设备文件/dev/leds执行open(...)时,
* 就会调用s3c24xx_leds_open函数
*/
static int s3c24xx_leds_open(struct inode *inode, struct file *file)
{
int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);
switch(minor)
{
case 0: /* /dev/leds */
{
// 配置3引脚为输出
//s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
GPFCON &= ~(0x3<<(4*2));
GPFCON |= (1<<(4*2));
//s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
GPFCON &= ~(0x3<<(5*2));
GPFCON |= (1<<(5*2));
//s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
GPFCON &= ~(0x3<<(6*2));
GPFCON |= (1<<(6*2));
// 都输出0
//s3c2410_gpio_setpin(S3C2410_GPF4, 0);
GPFDAT &= ~(1<<4);
//s3c2410_gpio_setpin(S3C2410_GPF5, 0);
GPFDAT &= ~(1<<5);
//s3c2410_gpio_setpin(S3C2410_GPF6, 0);
GPFDAT &= ~(1<<6);
down(&leds_lock);
leds_status = 0x0;
up(&leds_lock);
break;
}
case 1: /* /dev/led1 */
{
s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF4, 0);
down(&leds_lock);
leds_status &= ~(1<<0);
up(&leds_lock);
break;
}
case 2: /* /dev/led2 */
{
s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF5, 0);
leds_status &= ~(1<<1);
break;
}
case 3: /* /dev/led3 */
{
s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF6, 0);
down(&leds_lock);
leds_status &= ~(1<<2);
up(&leds_lock);
break;
}
}
return 0;
}
static int s3c24xx_leds_read(struct file *filp, char __user *buff,
size_t count, loff_t *offp)
{
int minor = MINOR(filp->f_dentry->d_inode->i_rdev);
char val;
switch (minor)
{
case 0: /* /dev/leds */
{
copy_to_user(buff, (const void *)&leds_status, 1);
break;
}
case 1: /* /dev/led1 */
{
down(&leds_lock);
val = leds_status & 0x1;
up(&leds_lock);
copy_to_user(buff, (const void *)&val, 1);
break;
}
case 2: /* /dev/led2 */
{
down(&leds_lock);
val = (leds_status>>1) & 0x1;
up(&leds_lock);
copy_to_user(buff, (const void *)&val, 1);
break;
}
case 3: /* /dev/led3 */
{
down(&leds_lock);
val = (leds_status>>2) & 0x1;
up(&leds_lock);
copy_to_user(buff, (const void *)&val, 1);
break;
}
}
return 1;
}
static ssize_t s3c24xx_leds_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
//int minor = MINOR(inode->i_rdev); //MINOR(inode->i_cdev);
int minor = MINOR(file->f_dentry->d_inode->i_rdev);
char val;
copy_from_user(&val, buf, 1);
switch (minor)
{
case 0: /* /dev/leds */
{
s3c2410_gpio_setpin(S3C2410_GPF4, (val & 0x1));
s3c2410_gpio_setpin(S3C2410_GPF5, (val & 0x1));
s3c2410_gpio_setpin(S3C2410_GPF6, (val & 0x1));
down(&leds_lock);
leds_status = val;
up(&leds_lock);
break;
}
case 1: /* /dev/led1 */
{
s3c2410_gpio_setpin(S3C2410_GPF4, val);
if (val == 0)
{
down(&leds_lock);
leds_status &= ~(1<<0);
up(&leds_lock);
}
else
{
down(&leds_lock);
leds_status |= (1<<0);
up(&leds_lock);
}
break;
}
case 2: /* /dev/led2 */
{
s3c2410_gpio_setpin(S3C2410_GPF5, val);
if (val == 0)
{
down(&leds_lock);
leds_status &= ~(1<<1);
up(&leds_lock);
}
else
{
down(&leds_lock);
leds_status |= (1<<1);
up(&leds_lock);
}
break;
}
case 3: /* /dev/led3 */
{
s3c2410_gpio_setpin(S3C2410_GPF6, val);
if (val == 0)
{
down(&leds_lock);
leds_status &= ~(1<<2);
up(&leds_lock);
}
else
{
down(&leds_lock);
leds_status |= (1<<2);
up(&leds_lock);
}
break;
}
}
return 1;
}
/* 这个结构是字符设备驱动程序的核心
* 当应用程序操作设备文件时所调用的open、read、write等函数,
* 最终会调用这个结构中指定的对应函数
*/
static struct file_operations s3c24xx_leds_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = s3c24xx_leds_open,
.read = s3c24xx_leds_read,
.write = s3c24xx_leds_write,
};
/*
* 执行insmod命令时就会调用这个函数
*/
static int __init s3c24xx_leds_init(void)
//static int __init init_module(void)
{
int ret;
int minor = 0;
gpio_va = ioremap(0x56000000, 0x100000);
if (!gpio_va) {
return -EIO;
}
/* 注册字符设备
* 参数为主设备号、设备名字、file_operations结构;
* 这样,主设备号就和具体的file_operations结构联系起来了,
* 操作主设备为LED_MAJOR的设备文件时,就会调用s3c24xx_leds_fops中的相关成员函数
* LED_MAJOR可以设为0,表示由内核自动分配主设备号
*/
ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);
if (ret < 0) {
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
leds_class = class_create(THIS_MODULE, "leds");
if (IS_ERR(leds_class))
return PTR_ERR(leds_class);
leds_class_devs[0] = class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, 0), NULL, "leds"); /* /dev/leds */
for (minor = 1; minor < 4; minor++) /* /dev/led1,2,3 */
{
leds_class_devs[minor] = class_device_create(leds_class, NULL, MKDEV(LED_MAJOR, minor), NULL, "led%d", minor);
if (unlikely(IS_ERR(leds_class_devs[minor])))
return PTR_ERR(leds_class_devs[minor]);
}
printk(DEVICE_NAME " initialized\n");
return 0;
}
/*
* 执行rmmod命令时就会调用这个函数
*/
static void __exit s3c24xx_leds_exit(void)
{
int minor;
/* 卸载驱动程序 */
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
for (minor = 0; minor < 4; minor++)
{
class_device_unregister(leds_class_devs[minor]);
}
class_destroy(leds_class);
iounmap(gpio_va);
}
/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(s3c24xx_leds_init);
module_exit(s3c24xx_leds_exit);
/* 描述驱动程序的一些信息,不是必须的 */
MODULE_AUTHOR("http://www.100ask.net");
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("S3C2410/S3C2440 LED Driver");
MODULE_LICENSE("GPL");
测试源码
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/*
* ledtest <dev> <on|off>
*/
void print_usage(char *file)
{
printf("Usage:\n");
printf("%s <dev> <on|off>\n",file);
printf("eg. \n");
printf("%s /dev/leds on\n", file);
printf("%s /dev/leds off\n", file);
printf("%s /dev/led1 on\n", file);
printf("%s /dev/led1 off\n", file);
}
int main(int argc, char **argv)
{
int fd;
char* filename;
char val;
if (argc != 3)
{
print_usage(argv[0]);
return 0;
}
filename = argv[1];
fd = open(filename, O_RDWR);
if (fd < 0)
{
printf("error, can't open %s\n", filename);
return 0;
}
if (!strcmp("on", argv[2]))
{
// 亮灯
val = 0;
write(fd, &val, 1);
}
else if (!strcmp("off", argv[2]))
{
// 灭灯
val = 1;
write(fd, &val, 1);
}
else
{
print_usage(argv[0]);
return 0;
}
return 0;
}