//---------------------LED驱动程序--------------------------------
//----------------------------------------------------------------
#include<linux/config.h>//配置头文件
#include<linux/kernel.h>//配置内核
#include<linux/sched.h>
#include<linux/timer.h>//时钟配置
#include<linux/init.h>//用户定义模块的初始化函数配置
#include<linux/module.h>//模块加载头文件
#include<linux/hardware.h>
#define GPIO_LED_MAJOR 97 //主设备号 97
#define LED1 (GPF4)
#define LED2 (GPF5)
#define LED3 (GPF6)
#define LED4 (GPF7)
#define LED_ON 0 // led灯的状态
#define LED_OFF 1
#define ctl_GPIO_LED1 1
struct file_operations GPIO_LED_ctl_ops={
open: GPIO_LED_open,
read: GPIO_LED_read,
write: GPIO_LED_write,
ioctl: GPIO_LED_ioctl,
release: GPIO_LED_release,
};
//----------------------------READ-------------------------------
//---------------------------------------------------------------
ssize_t GPIO_LED_read(struct file *file,char *buf,size_t count,loff_t *f_ops)
{
return count;
}
//----------------------------wirte-----------------------------
//--------------------------------------------------------------
ssize_t GPIO_LED_write(struct file *file,const char *buf,size_t count,loff_t *f_ops)
{
retuen count;
}
//----------------------------ioctl-----------------------------
//--------------------------------------------------------------
ssize_t GPIO_LED_ioctl(struct inode *inode,struct file *file,unsigned int cmd,long data)
{
switch(cmd)
{
case:LED_ON:{GPFDAT=(GPFDAT&~(0xf<<4))|((~0x0f&0xf)<<4);break;}
case:LED_OFF:{GPFDAT=(GPFDAT&~(0xf<<4))|((~0x00&0xf)<<4);break;}
default:{printk("led ioctl :no cmd run [--kernel--]\n");
return (-EINVAL);
}
}
return 0;
}
//-------------------------open----------------------------------
//---------------------------------------------------------------
ssize_t GPIO_LED_open(struct inode *inode,struct file *file)
{
MOD_INC_USE_COUNT;
return 0;
}
//-----------------------release/close----------------------------
//----------------------------------------------------------------
ssize_t GPIO_LED_release(struct inode *inode,struct file *file)
{
MOD_DEC_USE_COUNT;
return 0;
}
//-------------------init------------------------------------------
//-----------------------------------------------------------------
static int GPIO_LED_CTL_init(void)
{
int ret="-ENODEV";
printk("-----------------------------\n\n");
GPFCON=0x55aa;
GPFUP=0xff;
GPFDAT=0xf;
ret=devfs_register_chrdev(GPIO_LED_MAJOR,"gpio_led_ctl",&GPIO_LED_ctl_ops);
if(ret<0)
{
printk("s3c2410:init_module failed with %d\n",ret);
return ret;
}
else
{
printk("s3c2410:gpio_led_driver register success!!!\n");
}
return ret;
}
static int_init s3c2410_GPIO_LED_CTL_init(void)
{
init ret="-ENODEV";
ret=GPIO_LED_CTL_init();
if(ret)
return ret;
return 0;
}
static void_exit cleanup_GPIO_LED_ctl(void)
{
devfs_unregister_chrdev(GPIO_LED_MAJOR,"gpio_led_ctl");
}
module_init(s3c2410_GPIO_LED_CTL_init);
module_exit(cleanup_GPIO_LED_ctl);
//------------------------led_test---------------------
//-----------------------------------------------------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fcnt1.h>
#include<unistd.h>
#define DEVICE_NAME "/dev/gpio_led_ctl"
#define LED_ON 0;
#define LED_OFF 1;
int main(void)
{
int fd;
int ret;
char *i;
printf("\nstarts gpio_led_driver test\n\n");
fd=open(DEVICE_NAME,O_RDWR);
printf("fd=%d\n",fd);
if(fd==-1)
{
printf("open device %s error\n",DEVICE_NAME);
}
else
{
while(1)
{
ioctl(fd,LED_OFF);
sleep(1);
ioctl(fd,LED_ON);
sleep(1);
}
ret=close(fd);
printf("ret=%d\n,ret");
printf("close gpio_led_driver test\n");
}
return 0;
}
文章评论(0条评论)
登录后参与讨论