蜂鸣器实验

warren
2024-11-02 / 0 评论 / 2 阅读 / 正在检测是否收录...

蜂鸣器实验是对pinctrl和gpio的复习。

设备树修改

添加pinctrl节点

在设备树文件的iomuxc节点的imx6ul-evk子节点下创建一个pinctrl_beep子节点

  pinctrl_beep: beepgrp{
    fsl,pins = <
      MUX6ULL_PAD_SNVS_TAMPER1__GPIO5_IO01  0x10B0
    >;
  };

添加beep节点

在设备树文件的/节点下创建一个beep节点

  beep :{
    #address-cells = <1>;
    #size-cells = <1>;
    compatible = "warren-beep";
    pinctrl-name = "default";/*添加pinctrl信息*/
    pinctrl-0 = <&pinctrl_beep>;  
    beep-gpio = <&gpio5 1 GPIO_ACTIVE_HIGHT>; /*添加GPIO属性信息*/
    status = "okay";
  };

驱动程序编写

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



#define BEEP_CNT        1        /* 设备号个数 */
#define BEEP_NAME        "beep" /* 名字 */
#define BEEPOFF         0       /* 关蜂鸣器 */
#define BEEPON          1       /* 开蜂鸣器 */

struct beep_dev{
    dev_t  devid;           /*设备号*/
    struct cdev cdev;       /*字符设备cdev*/
    struct class *class;    /*类*/
    struct device *device;  /*设备*/
    int major;              /*主设备号*/
    int minor;              /*次设备号*/
    struct device_node *nd; /*设备节点*/
    int beep_gpio;          /*beep所使用的GPIO编号*/
};

struct beep_dev beep;

static int beep_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &beep;
    return 0;
}

static ssize_t beep_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int retvalue;
    unsigned char databuf[4];
    unsigned char beepstat;
    struct beep_dev *dev = filp->private_data;

    retvalue = copy_from_user(databuf, buf, cnt);
    if(retvalue < 0){
        printk("kernel write failed!\r\n");
        return -EFAULT;
    }
    beepstat = databuf[0];
    if(beepstat == BEEPON){
        gpio_set_value(dev->beep_gpio, 0);
    }else{
        gpio_set_value(dev->beep_gpio, 1);
    }
    return 0;
}

static int beep_release(struct inode *inode, struct file *filp)
{
    return 0;
}



static struct file_operations beep_fops = {
    .owner   = THIS_MODULE,
    .write   = beep_write,
    .open    = beep_open,
    .release = beep_release,
};

static int __init beep_init(void)
{
    int ret = 0;

    /*1. 从设备数文件中获取beep节点信息*/
    beep.nd = of_find_node_by_path("/beep");
    if(beep.nd == NULL){
        printk("beep node not find!\r\n");
        return -EINVAL;
    }else{
        printk("beep node find success!\r\n");
    }

    /*根据beep节点信息来获取BEEP所使用的GPIO编号*/
    beep.beep_gpio = of_get_named_gpio(beep.nd, "beep-gpio", 0);
    if(beep.beep_gpio < 0){
        printk("can't get beep-gpio");
        return -EINVAL;
    }
    printk("led-gpio num = %d\r\n", beep.beep_gpio);

    /*配置beep-gpio为输出高电平,默认关闭BEEP*/
    ret = gpio_direction_output(beep.beep_gpio, 1);
    if(ret < 0){
        printk("can't set beep gpio!\r\n");
    }

    /*创建设备号*/
    if(beep.major){
        beep.devid = MKDEV(beep.major, 0);
        register_chrdev_region(beep.devid, BEEP_CNT, BEEP_NAME);
    }else{
        alloc_chrdev_region(&beep.devid, 0, BEEP_CNT, BEEP_NAME);
        beep.major = MAJOR(beep.devid);
        beep.minor = MINOR(beep.devid);
    }
    printk("beep major=%d,minor=%d\r\n", beep.major, beep.minor);

    /*初始化cdev*/
    beep.cdev.owner = THIS_MODULE;
    cdev_init(&beep.cdev, &beep_fops);

    /*添加cdev*/
    cdev_add(&beep.cdev, beep.devid, BEEP_CNT);
    
    /*创建类*/
    beep.class = class_create(THIS_MODULE, BEEP_NAME);
    if(IS_ERR(beep.class)){
        return PTR_ERR(beep.class);
    }

    /*创建设备*/
    beep.device = device_create(beep.class, NULL, beep.devid, NULL, BEEP_NAME);
    if(IS_ERR(beep.device)){
        return PTR_ERR(beep.device);
    }

    return 0;
}

static void __exit beep_exit(void)
{
    cdev_del(&beep.cdev);
    unregister_chrdev_region(beep.devid, BEEP_CNT);
    device_destroy(beep.class, beep.devid);
    class_destroy(beep.class);
}





module_init(beep_init);
module_exit(beep_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("warren");

测试程序编写

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






int main(int argc, int *argv[])
{
    int fd = 0;
    int retvalue = 0;
    char *filename=NULL;
    unsigned char buff[4];

    if(argc != 3){
        printf("Error Usage!\r\n");
        return -1;
    }
    filename = (char *)argv[1];

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

    buff[0] = atoi((char *)argv[2]);
    retvalue = write(fd, buff, 1);
    if(retvalue < 0){
        printf("beep control failed!\r\n");
    }
    retvalue = close(fd);
    if(retvalue < 0){
        printf("file close failed!\r\n");
        return -1;
    }
    return 0;
}

实验现象

m2zi5sjr.png

0

评论 (0)

取消