博主使用的板子是天嵌科技的335xB
内核版本:linux3.2
驱动demo:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MY_MAJOR 231
#define DEVICE_NAME "myModule"
#define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio))
static struct omap_dm_timer *timer;
int irq;
static int mydemo_open(struct inode *inode, struct file *file){
printk(KERN_EMERG "hello open.\n");
return 0;
}
static int mydemo_write(struct file *file, const char __user * buf, size_t count, loff_t *ppos){
printk(KERN_EMERG "hello write.\n");
return 0;
}
static struct file_operations mydemo_flops = {
.owner = THIS_MODULE,
.open = mydemo_open,
.write = mydemo_write,
};
static irqreturn_t mytimer_interrupt(int irq, void *dev){
//printk(KERN_ERR"TIMER 7 is_running !!!!!!!!!!!!\n");
char gpio_v = !(gpio_get_value(GPIO_TO_PIN(3, 21)));
gpio_set_value(GPIO_TO_PIN(3, 21), gpio_v);
return IRQ_HANDLED;
}
static int __init mydemo_init(void){
int ret;
ret = register_chrdev(MY_MAJOR,DEVICE_NAME, &mydemo_flops);
if (ret < 0) {
printk(KERN_EMERG DEVICE_NAME " can't register major number.\n");
return ret;
}
printk(KERN_EMERG DEVICE_NAME " initialized.\n");
gpio_request(GPIO_TO_PIN(3,21),"gpio117");
gpio_direction_output(GPIO_TO_PIN(3,21), 0);
timer = omap_dm_timer_request_specific(7); //得到定时器7
irq = omap_dm_timer_get_irq(timer); //得到中断号
ret = request_irq(irq,mytimer_interrupt, IRQF_DISABLED | IRQF_TIMER , "heat_timer_init",NULL);//注册中断
omap_dm_timer_set_load(timer, 0, 0xFFFFFFFF-500);
omap_dm_timer_write_counter(timer, 0xFFFFFFFF-500);
/* Enable overflow interrupt */
omap_dm_timer_set_int_enable(timer, OMAP_TIMER_INT_OVERFLOW);
/*
* Set counter value to overflow counter after
* one tick and start timer.
*/
//omap_dm_timer_set_load_start(timer, 0, load);
// start the timer!
omap_dm_timer_start(timer);
// stop the timer!
//omap_dm_timer_stop(timer);
return 0;
}
static void __exit mydemo_exit(void){
unregister_chrdev(MY_MAJOR, DEVICE_NAME);
omap_dm_timer_free(timer);
free_irq(irq,NULL);
printk(KERN_EMERG DEVICE_NAME " removed.\n");
}
module_init(mydemo_init);
module_exit(mydemo_exit);
MODULE_LICENSE("GPL");