原子锁实验

1.驱动代码

#include <linux/types.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/ide.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/errno.h> 
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <asm/mach/map.h>

#define GPIOLED_CNT     1
#define GPIOLED_NAME    "gpioled"
#define LEDOFF          0            /* 关灯 */
#define LEDON           1            /* 开灯 */

/* gpioled设备结构体 */
struct gpioled_dev{
    dev_t           devid;          /* 设备号 */
    struct cdev     cdev;           /* 字符设备 */
    struct class    *class;         /* 类 */
    struct device   *device;        /* 设备 */
    int             major;          /* 注设备号 */
    int             minor;          /* 次设备号 */
    struct device_node *nd;         /* 设备节点 */
    int             led_gpio;       /* 设备树里面的编号 */
    atomic_t        lock;           /* 原子变量的锁 */ /* 加载驱动的时候初始化 */
};

struct gpioled_dev gpioled; /* 定义LED结构体 */

void led_switch(struct gpioled_dev *dev, u8 sta)
{
    if(sta == LEDON) {
        gpio_set_value(dev->led_gpio, 0);
    }else if(sta == LEDOFF) {
        gpio_set_value(dev->led_gpio, 1);
    } else {
        printk("Error Number! \r\n");
    }
}

static int gpioled_open(struct inode *inode, struct file *filp)
{
    /* 判断这个led驱动有没有被其他应用使用 */
    /* 该函数是先将锁减1,如果为0返回真,锁的初始化是1,如果被用了就是0,再减去1就是-1 */
    if (!atomic_dec_and_test(&gpioled.lock))
    {
        /* 锁用了 */
        /* 在if里面已经减1了,所以要加回来 */
        atomic_inc(&gpioled.lock);
        return -EBUSY; /* 返回忙 */
    }

    /* 如果锁没人用,就直接运行到下面 */
    /* 注意此时再if里面已经减1了,再release函数里面必须加回来 */
    filp->private_data = &gpioled;

    return 0;
}

static int gpioled_release(struct inode *inode, struct file *filp)
{
    //struct gpioled_dev *dev = (struct gpioled_dev *)filp->private_data;
    /* 锁加回来 */
    atomic_inc(&gpioled.lock);
    
    return 0;
}

static ssize_t gpioled_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
    int retvalue;
    unsigned char databuf[1];
    // unsigned char ledstat; 
    struct gpioled_dev *dev = (struct gpioled_dev *)filp->private_data;

    retvalue = copy_from_user(databuf, buf, count);
    if(retvalue < 0) {
        printk("kernel write failed!\r\n");
        return -EFAULT;
    }
    led_switch(dev, databuf[0]);   
    
    return 0;
}

static ssize_t gpioled_read(struct file *filp, char __user *buf,
size_t cnt, loff_t *offt)
{
    return 0;
}

/* 字符设备操作集 */
static const struct file_operations gpioled_fops = {
    .owner = THIS_MODULE,
    .write = gpioled_write,
    .open = gpioled_open,
    .read = gpioled_read,
    .release = gpioled_release,
};

/* 模块入口函数 */
static int __init gpioled_init(void)
{
    /* 定义一些所需变量 */
    int ret = 0;

    /* 初始化锁 */
    atomic_set(&gpioled.lock, 1);

    /* 1. 注册字符设备驱动 */
    gpioled.major = 0;
    if(gpioled.major) {
        gpioled.devid = MKDEV(gpioled.major, 0);
        ret = register_chrdev_region(gpioled.devid, GPIOLED_CNT, GPIOLED_NAME);  
    } else {
        alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_CNT, GPIOLED_NAME);
        gpioled.major = MAJOR(gpioled.devid);
        gpioled.minor = MINOR(gpioled.devid);
    }
    if(ret < 0){
        goto fail_devid;
    }
    printk("Make devid success! \r\n");
    printk("major = %d, minor = %d \r\n", gpioled.major, gpioled.minor);

    /* 2. 初始化cdev */
    gpioled.cdev.owner = THIS_MODULE;
    cdev_init(&gpioled.cdev, &gpioled_fops);
    ret = cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT);
    if (ret < 0){
        goto fail_cdev;
    } else {
        printk("Cdev add sucess! \r\n");
    }

    /* 3. 自动创建设备节点 */
    gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME);
    if(IS_ERR(gpioled.class)) {
        ret = PTR_ERR(gpioled.class);
        goto fail_class;
    } else {
        printk("Class create sucess! \r\n");
    }

    gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);
    if(IS_ERR(gpioled.device)) {
        ret = PTR_ERR(gpioled.device);
        goto fail_device;
    } else {
        printk("Device create sucess! \r\n");
    }

    /* 1. 获取设备节点 */
    gpioled.nd = of_find_node_by_path("/gpioled");
    if( gpioled.nd == NULL){
        ret = -EINVAL;
        printk("gpioled node can not found!\r\n");
        goto fail_findnd;
    }

    /* 2. 获取设备所对应的gpio */
    /* 注意第三个参数,因为只有一个索引,所以是0 */
    gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "led-gpios", 0);
    if( gpioled.led_gpio < 0) {
        printk("Can't find led gpio \r\n");
        ret = -EINVAL;
        goto fail_findnd;
    }
    printk("Led gpio num = %d. \r\n", gpioled.led_gpio);
    
    /* 3. 获取到IO之后,申请IO */
    /* 作用是申请之后,就能知道该IO是否被其他函数占用 */
    /* 如果申请失败,说明被战用啦 */
    /* 解决方法:1.搜索所需管脚 2.在搜索复用管脚,例如 &gpio1 3 */
    ret = gpio_request(gpioled.led_gpio, "led-gpio");
    if(ret) {
        printk("Failed to request the led gpio! \r\n");
        ret = -EINVAL;
        goto fail_findnd;
    }

    /* 4. 使用IO */
    ret = gpio_direction_output(gpioled.led_gpio, 1); /* 设置输出,默认高电平不点亮 */
    if(ret < 0){
        printk("Failed to set input or output! \r\n");
        goto fail_set_output;
    }

    /* 5. 输出低电平,点亮LED */
    gpio_set_value(gpioled.led_gpio, 0);

    printk("gpioled init! \r\n");
    return 0;

/* 错误处理 */
fail_set_output:
    gpio_free(gpioled.led_gpio);
fail_findnd:
    device_destroy(gpioled.class, gpioled.devid);
fail_device:
    class_destroy(gpioled.class);
fail_class:
    cdev_del(&gpioled.cdev);
fail_cdev:
    unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
fail_devid:
    return ret;
}

/* 模块出口函数 */
static void __exit gpioled_exit(void)
{
    /* 关灯 */
    gpio_set_value(gpioled.led_gpio, 1);

    /* 1. 释放设备号 */
    cdev_del(&gpioled.cdev);
    /* 2. 注销设备号 */
    unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
    /* 3. 摧毁设备 */
    device_destroy(gpioled.class, gpioled.devid);
    /* 4.摧毁类 */
    class_destroy(gpioled.class);

    /* 释放IO, 和上面的申请IO对应 */
    gpio_free(gpioled.led_gpio);

    printk("gpioled exit! \r\n");
}

/* 模块入口和出口注册 */
module_init(gpioled_init);
module_exit(gpioled_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Shao Zheming");

2.应用代码

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

/* 
 * argc: 应用程序参数个数
 * argv[]: 参数是什么,具体的参数,说明参数是字符串的形式
 * .chrdevbaseApp <filename> <0:1> 0表示关灯,1表示开灯
 * .chrdevbaseApp /dev/led 0 关灯
 * .chrdevbaseApp /dev/led 1 开灯
 * */

#define LEDOFF      0   /* 关灯 */
#define LEDON       1   /* 开灯 */

int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        printf("Error Usage!\r\n");
        return -1;
    }
    
    char *filename;
    int fd, retValue;
    unsigned char dataBuf[1];
    unsigned char cnt = 0;          /* 计数 */

    filename = argv[1];
    fd = open(filename, O_RDWR);
    if(fd < 0)
    {
        printf("file %s open failed! \r\n", filename);
        return -1;
    }

    dataBuf[0] = atoi(argv[2]);
    retValue = write(fd, dataBuf, sizeof(dataBuf));
    if(retValue < 0){
        printf("Led Control Failed \r\n");
        close(fd);
        return -1;
    }

    /* 模拟占用25sLED */
    while(1){
        sleep(5);
        ++cnt;
        printf("App running times: %d \r\n", cnt);
        if(cnt > 4){
            break;
        }
    }

    printf("App running finished! \r\n");

    /* 关闭应用 */
    retValue = close(fd);
    if(retValue < 0) {
        printf("File close failed! \r\n");
        return -1;
    }
    
    return 0;
}

3.效果

原子锁实验

上一篇:独立按键控制LED状态


下一篇:led灯珠多少伏?照明led灯珠多少伏?