您的位置:首页 > 其它

2013-10-08 实验之定时器控制Led等闪烁

2013-10-08 15:20 399 查看
实验描述:定时器控制Led等1s闪烁

注意事项:定时器的添加,更新,删除

内核版本:Linux 2.6.38

开发板:    Mini 6410

驱动程序

#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/kdev_t.h>
#include <linux/timer.h>
#include <asm/io.h>
#include <linux/fs.h>

/*
Kernel Version: Linux 2.6.38
Arm Version: Mini 6410
*/

#define MyPrintk  printk

static struct timer_list timer_leds;
static dev_t  Leds_Major ;
static char * DEVICE_NAME = "TimerLeds";
volatile unsigned long *gpkcon0 = NULL;
volatile unsigned long *gpkdat    = NULL;
static int current_state = 1;
static struct class *leds_class;

void leds_ctl(unsigned long value)
{
if( current_state == 1){
*gpkdat &= ~((1<<4) | (1<<5) | (1<<6) | (1<<7)) ;
}else{
*gpkdat |= (1<<4) | (1<<5) | (1<<6) | (1<<7) ;
}
timer_leds.data++;
MyPrintk (KERN_EMERG "timer count: %lu ,value:%lu\n", timer_leds.data, value);
current_state = ~current_state;
if (timer_leds.data <= 10 ){
mod_timer(&timer_leds, jiffies + HZ);
}
}

static struct file_operations  s3c64XX_leds_fops = {
.owner = THIS_MODULE,
};

static int myleds_init(void)
{
Leds_Major = register_chrdev(Leds_Major,DEVICE_NAME , &s3c64XX_leds_fops);
if(Leds_Major < 0){
MyPrintk (KERN_EMERG "Sorry, Can not register the leds device!\n");
}
MyPrintk (KERN_EMERG " Register the timer leds device\n");

leds_class = class_create(THIS_MODULE, "TimerLeds");
device_create(leds_class, NULL , MKDEV(Leds_Major, 0), NULL, "TimerLeds");

gpkcon0 = (volatile unsigned long *)ioremap(0x7F008800,12);
gpkdat = gpkcon0 + 2;

init_timer(&timer_leds);
timer_leds.expires = jiffies + HZ;
timer_leds.data = 0;
timer_leds.function = leds_ctl;
*gpkcon0 &= ~( (0xF<<4*4) | (0xF<<5*4) | (0xF<<6*4) | (0xF<<7*4));
*gpkcon0 |= ( (0x1<<4*4) |(0x1<<5*4) | (0x1<<6*4) | (0x1<<7*4));
*gpkdat |= (1<<4) | (1<<5) | (1<<6) | (1<<7) ;
add_timer(&timer_leds);
MyPrintk (KERN_EMERG "Timer Start\n");
return 0;
}

static void myleds_exit(void)
{
del_timer(&timer_leds);
unregister_chrdev(Leds_Major, DEVICE_NAME);
device_destroy(leds_class,  MKDEV(Leds_Major, 0));
class_destroy(leds_class);
iounmap(gpkcon0);
MyPrintk (KERN_EMERG "Timer Leds Linux Byebye\n");
}

module_init(myleds_init);
module_exit(myleds_exit);
MODULE_LICENSE("GPL");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: