4412驱动-sixth

xiaoxiao2021-02-27  359

并发-信号量

阻塞与非阻塞

1. 原子操作 原子操作指的是在执行过程中不会被别的代码路径所中断的操作。 常用原子操作函数举例: atomic_t v = ATOMIC_INIT(0);     //定义原子变量v并初始化为0 atomic_read(atomic_t *v);        //返回原子变量的值 void atomic_inc(atomic_t *v);    //原子变量增加1 void atomic_dec(atomic_t *v);    //原子变量减少1 int atomic_dec_and_test(atomic_t *v); //自减操作后测试其是否为0,为0则返回true,否则返回false。 2. 信号量 信号量(semaphore)是用于保护临界区的一种常用方法,只有得到信号量的进程才能执行临界区代码。 当获取不到信号量时,进程进入休眠等待状态。 定义信号量 struct semaphore sem; 初始化信号量 void sema_init (struct semaphore *sem, int val); void init_MUTEX(struct semaphore *sem);//初始化为0 static DECLARE_MUTEX(button_lock);     //定义互斥锁 获得信号量 void down(struct semaphore * sem); int down_interruptible(struct semaphore * sem);  int down_trylock(struct semaphore * sem); 释放信号量 void up(struct semaphore * sem); 3. 阻塞 阻塞操作     是指在执行设备操作时若不能获得资源则挂起进程,直到满足可操作的条件后再进行操作。 被挂起的进程进入休眠状态,被从调度器的运行队列移走,直到等待的条件被满足。 非阻塞操作   进程在不能进行设备操作时并不挂起,它或者放弃,或者不停地查询,直至可以进行操作为止。 fd = open("...", O_RDWR | O_NONBLOCK); 

驱动

#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/platform_device.h> #include <linux/fb.h> #include <linux/backlight.h> #include <linux/err.h> #include <linux/pwm.h> #include <linux/slab.h> #include <linux/miscdevice.h> #include <linux/delay.h> #include <linux/timer.h> /*timer*/ #include <asm/uaccess.h> /*jiffies*/ #include <linux/delay.h> #include <linux/interrupt.h> //request_irq #include <mach/irqs.h> //中断号,已包含plat/irqs.h #include <linux/fs.h> #include <linux/device.h> //class_create device_create #include <mach/regs-gpio.h> #include <linux/io.h> //ioremap ioread32 iowrite32 #include <linux/sched.h> #include <linux/of.h> #include <linux/of_device.h> #include <linux/poll.h> #include <mach/gpio.h> #include <linux/gpio.h> #include <mach/gpio.h> #include <plat/gpio-cfg.h> static struct class *sixthdrv_class; static struct class_device *sixthdrv_class_dev; static DECLARE_WAIT_QUEUE_HEAD(button_waitq); /* 中断事件标志, 中断服务程序将它置1,sixth_drv_read将它清0 */ static volatile int ev_press = 0; static struct fasync_struct *button_async; struct led_reg { u32 gpm4con; u8 gpm4dat; }; static struct led_reg *led_reg; struct key_reg { u32 gpm4con; u8 gpm4dat; }; static struct key_reg *key_reg; struct beep_reg { u32 gpm4con; u8 gpm4dat; }; static struct key_reg *beep_reg; struct pin_desc{ unsigned int pin; unsigned int key_val; }; /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */ /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */ static unsigned char key_val; struct pin_desc pins_desc[4] = { {EXYNOS4_GPX3(2), 0x01}, {EXYNOS4_GPX3(3), 0x02}, {EXYNOS4_GPX3(4), 0x03}, {EXYNOS4_GPX3(5), 0x04}, }; //static atomic_t canopen = ATOMIC_INIT(1); //定义原子变量并初始化为1 //static DECLARE_MUTEX(button_lock); //定义互斥锁 struct semaphore button_lock; /* * 确定按键值 */ static irqreturn_t buttons_irq(int irq, void *dev_id) { printk("buttons_irq\n"); struct pin_desc * pindesc = (struct pin_desc *)dev_id; unsigned int pinval; //pinval = s3c2410_gpio_getpin(pindesc->pin); //获取按键的键值,因为按键是从该寄存器的第二位开始的,所以需要左移2位,接着与上0xf---1111 //这样,如果用户按下按键,就会返回一个键值保存在key_val这个变量里 pinval = ((key_reg->gpm4dat) >> 2) & 0xf ; key_val=pinval; printk("buttons_irq :pinval = %d \n",pinval); #if 0 if (pinval) { /* 松开 */ key_val = 0x80 | pindesc->key_val; printk("buttons_irq :key_val = %d\n ",key_val); } else { /* 按下 */ key_val = pindesc->key_val; printk("buttons_irq :key_val = %d \n",key_val); } #endif ev_press = 1; /* 表示中断发生了 */ wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */ kill_fasync (&button_async, SIGIO, POLL_IN); return IRQ_RETVAL(IRQ_HANDLED); } static int sixth_drv_open(struct inode *inode, struct file *file) { int ret; printk("sixth_drv_open\n"); #if 0 if (!atomic_dec_and_test(&canopen)) { atomic_inc(&canopen); return -EBUSY; } #endif if (file->f_flags & O_NONBLOCK) { if (down_trylock(&button_lock)) return -EBUSY; } else { /* 获取信号量 */ down(&button_lock); } //配置4个按键为输入状态,因为按键是从GPXCON[2]开始的,所以要左移8位到对应的位置,将8位以后的16位清0 //这样的话就将按键配置的寄存器设置为输入状态了,因为输入是0x0 key_reg->gpm4con &= ~((0xf<<(2*4)) | (0xf<<(3*4)) | (0xf<<(4*4)) | (0xf<<(5*4))); //先对LED的端口进行清0操作 led_reg->gpm4con &= ~((0xf<<(3*4)) | (0xf<<(2*4)) | (0xf<<(1*4)) | (0xf<<(0*4))); //将4个IO口16位都设置为Output输出状态 led_reg->gpm4con |= ((0x1<<(3*4)) | (0x1<<(2*4)) | (0x1<<(1*4)) | (0x1<<(0*4))); //清寄存器 beep_reg->gpm4con &= ~(0xf); //设置io为输出 beep_reg->gpm4con |= (0x1); ret = request_irq(IRQ_EINT(26), buttons_irq, IRQF_TRIGGER_FALLING , "k1", &pins_desc[0]); ret =request_irq(IRQ_EINT(27), buttons_irq, IRQF_TRIGGER_FALLING , "k2", &pins_desc[1]); ret =request_irq(IRQ_EINT(28), buttons_irq, IRQF_TRIGGER_FALLING , "k3", &pins_desc[2]); ret =request_irq(IRQ_EINT(29), buttons_irq, IRQF_TRIGGER_FALLING , "k4", &pins_desc[3]); return 0; } ssize_t sixth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) { printk("sixth_drv_read\n"); if (size != 1) return -EINVAL; if (file->f_flags & O_NONBLOCK) { if (!ev_press) return -EAGAIN; } else { /* 如果没有按键动作, 休眠 */ wait_event_interruptible(button_waitq, ev_press); } /* 如果有按键动作, 返回键值 */ copy_to_user(buf, &key_val, 1); ev_press = 0; return 1; } int sixth_drv_write(struct file *filp , const char __user *buf , size_t count , loff_t *f_pos) { int val; printk("fifth_fasync_drv_write\n"); //注意,这里是在内核中进行操作,我们需要使用copy_from_user这个函数将用户态的内容拷贝到内核态 copy_from_user(&val, buf, count); switch(val) { case 7: printk(KERN_EMERG"led1_on\n"); led_reg->gpm4dat &= ~(1<<0) ; printk(KERN_EMERG"beep_on\n"); beep_reg->gpm4dat |= 0x1 ; break ; case 11: printk(KERN_EMERG"led2_on\n"); led_reg->gpm4dat &= ~(1<<1) ; printk(KERN_EMERG"beep_off\n"); beep_reg->gpm4dat &=~0x1 ; //蜂鸣器不响 break ; case 13: printk(KERN_EMERG"led3_on\n"); led_reg->gpm4dat &= ~(1<<2) ; printk(KERN_EMERG"beep_on\n"); beep_reg->gpm4dat |= 0x1 ; break ; case 14: printk(KERN_EMERG"led4_on\n"); led_reg->gpm4dat &= ~(1<<3) ; printk(KERN_EMERG"beep_off\n"); beep_reg->gpm4dat &=~0x1 ; //蜂鸣器不响 break ; } return 0; } int sixth_drv_close(struct inode *inode, struct file *file) { //atomic_inc(&canopen); led_reg->gpm4dat |= ((1<<0) | (1<<1) |(1<<2)| (1<<3)) ; beep_reg->gpm4dat &=~0x1 ; //蜂鸣器不响 free_irq(IRQ_EINT(26), &pins_desc[0]); free_irq(IRQ_EINT(27), &pins_desc[1]); free_irq(IRQ_EINT(28), &pins_desc[2]); free_irq(IRQ_EINT(29), &pins_desc[3]); up(&button_lock); return 0; } static unsigned sixth_drv_poll(struct file *file, poll_table *wait) { printk("sixth_drv_poll\n"); unsigned int mask = 0; poll_wait(file, &button_waitq, wait); // 不会立即休眠 if (ev_press) mask |= POLLIN | POLLRDNORM; return mask; } static int sixth_drv_fasync (int fd, struct file *filp, int on) { printk("driver: sixth_drv_fasync\n"); return fasync_helper (fd, filp, on, &button_async); } static struct file_operations sencod_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = sixth_drv_open, .read = sixth_drv_read, .release = sixth_drv_close, .poll = sixth_drv_poll, .fasync = sixth_drv_fasync, .write = sixth_drv_write, }; int major; static int sixth_drv_init(void) { printk("sixth_drv_init\n"); major = register_chrdev(0, "sixth_drv", &sencod_drv_fops); sixthdrv_class = class_create(THIS_MODULE, "sixth_drv"); sixthdrv_class_dev = device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "chenhaipan"); /* /dev/buttons */ led_reg = ioremap(0x110002e0, sizeof(struct led_reg)); beep_reg = ioremap(0x114000A0, sizeof(struct beep_reg)); key_reg = ioremap(0x11000C60, sizeof(struct key_reg)); sema_init(&button_lock, 1); return 0; } static void sixth_drv_exit(void) { printk("sixth_drv_exit\n"); unregister_chrdev(major, "sixth_drv"); device_destroy(sixthdrv_class, MKDEV(major, 0)); class_destroy(sixthdrv_class); iounmap(led_reg); iounmap(beep_reg); iounmap(key_reg); } module_init(sixth_drv_init); module_exit(sixth_drv_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("xiangtan da xue chenhaipan"); MODULE_VERSION("2017.5.4"); 应用

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <poll.h> #include <signal.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> /* sixthdrvtest */ int fd; void my_signal_fun(int signum) { unsigned char key_val; read(fd, &key_val, 1); printf("key_val: 0x%x\n", key_val); } int main(int argc, char **argv) { unsigned char key_val; int ret; int Oflags; //signal(SIGIO, my_signal_fun); fd = open("/dev/chenhaipan", O_RDWR | O_NONBLOCK); if (fd < 0) { printf("can't open!\n"); return -1; } //fcntl(fd, F_SETOWN, getpid()); //Oflags = fcntl(fd, F_GETFL); //fcntl(fd, F_SETFL, Oflags | FASYNC); while (1) { ret = read(fd, &key_val, 1); printf("key_val: 0x%x, ret = %d\n", key_val, ret); write(fd, &key_val,1); sleep(5); } return 0; }

非阻塞方式,没有按键值按下,程序立马返回; read 返回值 为 -1;

阻塞方式 open

如果不按键,就一直停留,等待,并不运行

总结:阻塞操作:           是指在执行设备操作时,若不能获得资源则挂起进程,直到满足可操作的条件后进行操作,           被挂起的进程进入睡眠状态,被从调度器的运行队列移走,直到等待的条件被满足. 非阻塞操作:           进程不能进行设备操作时并不挂起,他或者放弃,或者不停的查询,直到可以进行操作为止.

转载请注明原文地址: https://www.6miu.com/read-3932.html

最新回复(0)