文章目录
前言
本文基于S3C2440开发板。
一、原理图
二、芯片手册
三、驱动程序
利用次设备号,分辨出是哪一个led,对单个led进行操作。
led_drv.c
#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>
static struct class *leds_class;
static struct class_device *leds_class_devs[4];
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)))
static int s3c2440_led_open(struct inode *inode, struct file *file)
{
//open核心代码,一般都是对一些硬件的初始化
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 ssize_t S3C2440_led_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
//read核心代码,一般都是对一些硬件数据寄存器,状态寄存器的读取
int minor = MINOR(file->f_dentry->d_inode->i_rdev);
char val;
switch (minor)
{
case 0: /* /dev/leds */
{
copy_to_user(buf, (const void *)&leds_status, 1);
break;
}
case 1: /* /dev/led1 */
{
down(&leds_lock);
val = leds_status & 0x1;
up(&leds_lock);
copy_to_user(buf, (const void *)&val, 1);
break;
}
case 2: /* /dev/led2 */
{
down(&leds_lock);
val = (leds_status>>1) & 0x1;
up(&leds_lock);
copy_to_user(buf, (const void *)&val, 1);
break;
}
case 3: /* /dev/led3 */
{
down(&leds_lock);
val = (leds_status>>2) & 0x1;
up(&leds_lock);
copy_to_user(buf, (const void *)&val, 1);
break;
}
}
return 1;
}
static ssize_t s3c2440_led_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;
}
static struct file_operations led_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = s3c2440_led_open,
.write = s3c2440_led_write,
.read = S3C2440_led_read,
};
int major;
static int s3c2440_led_init(void)
{
int ret;
int minor = 0;
gpio_va = ioremap(0x56000000, 0x100000);
if (!gpio_va)
{
return -EIO;
}
major = register_chrdev(0, "led_drv", &led_drv_fops ); // 注册, 告诉内核
leds_class = class_create(THIS_MODULE, "leddrv");
leds_class_devs[0] = class_device_create(leds_class, NULL, MKDEV(major, 0), NULL, "leds");
for (minor = 1; minor < 4; minor++)
{
leds_class_devs[minor] = class_device_create(leds_class, NULL, MKDEV(major, minor), NULL, "led%d", minor);
if (unlikely(IS_ERR(leds_class_devs[minor])))
return PTR_ERR(leds_class_devs[minor]);
}
return 0;
}
static void s3c2440_led_exit(void)
{
int minor;
unregister_chrdev(major, "led_drv"); // 卸载
for (minor = 0; minor < 4; minor++)
{
class_device_unregister(leds_class_devs[minor]);
}
class_destroy(leds_class);
iounmap(gpio_va);
}
module_init(s3c2440_led_init);
module_exit(s3c2440_led_exit);
MODULE_LICENSE("GPL");
makefile文件:
KERN_DIR = /home/book/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += led_drv.o
/home/book/system/linux-2.6.22.6 编译过的内核目录
四、驱动程序的测试
#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;
}