赞
踩
从软件的角度考虑,蜂鸣器驱动和 LED 灯驱动是一样的,都是控制 IO 输出高低电平。本实验来编写蜂鸣器的 Linux 驱动,也算是 pinctrl 和 gpio 子系统的巩固。
1 蜂鸣器驱动原理
3 实验程序编写
3.1 修改设备树文件
1、添加 pinctrl 节点
I.MX6U-ALPHA开发板上的BEEP使用了SNVS_TAMPER1这个PIN,打开imx6ull-alientek-emmc.dts,在 iomuxc 节点的 imx6ul-evk 子节点下创建一个名为“pinctrl_beep”的子节点,节点 内容如下所示:
- pinctrl_beep: beepprp{
- fsl,pins = <
- MX6ULL_PAD_SNVS_TAMPER1__GPIO5_IO01 0x10b0
- >;
- };
2、添加 BEEP 设备节点
- beep{
- compatible = "alientek,beep";
- pinctrl-names = "default";
- pinctrl-0 = <&pinctrl_beep>;
- beep-gpios = <&gpio5 1 GPIO_ACTIVE_HIGH>;
- status= "okay";
- };
编译设备树
新建vscode工程,工程创建好以后新建 beep.c 文件,在 beep.c 里面输入如下内容:
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/fs.h>
- #include <linux/slab.h>
- #include <linux/uaccess.h>
- #include <linux/io.h>
- #include <linux/cdev.h>
- #include <linux/device.h>
- #include <linux/of.h>
- #include <linux/of_address.h>
- #include <linux/of_irq.h>
- #include <linux/gpio.h>
- #include <linux/of_gpio.h>
-
- #define BEEP_CNT 1
- #define BEEP_NAME "beep"
- #define BEEPOFF 0
- #define BEEPON 1
-
- /*beep 设备结构体*/
- struct beep_dev
- {
- dev_t devid;
- int major;
- int minor;
- struct cdev cdev;
- struct class *class;
- struct device *device;
- struct device_node *nd;
- int beep_gpio;
- };
-
- struct beep_dev beep; /*BEEP*/
-
- static int beep_open(struct inode *inode, struct file *filp)
- {
- filp->private_data = &beep;
- return 0;
- }
-
- static int beep_release(struct inode *inode, struct file *filp)
- {
-
- return 0;
- }
-
- static ssize_t beep_write(struct file *filp, const char __user *buf,
- size_t count, loff_t *ppos)
- {
- int ret;
- unsigned char databuf[1];
- struct beep_dev *dev = filp->private_data; //得到私有数据就是得到beep结构体
- ret = copy_from_user(databuf, buf, count);
- if (ret < 0)
- {
- return -EFAULT;
- }
-
- if (databuf[0] == BEEPON)
- {
- gpio_set_value(dev->beep_gpio, 0); //打开蜂鸣器,低电平
- }
- else if (databuf[0] == BEEPOFF)
- {
- gpio_set_value(dev->beep_gpio, 1); //关闭蜂鸣器,高电平
- }
-
- return 0;
- }
-
- /*操作集*/
- static const 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.major = 0;
- if (beep.major)
- { /*给定主设备号*/
- beep.devid = MKDEV(beep.major, 0);
- ret = register_chrdev_region(beep.devid, BEEP_CNT, "BEEP_NAME");
- }
- else
- {
- ret = alloc_chrdev_region(&beep.devid, 0, BEEP_CNT, "BEEP_NAME");
- beep.major = MAJOR(beep.devid);
- beep.minor = MINOR(beep.devid);
- }
- if (ret < 0)
- {
- goto fail_devid;
- }
- printk("beep major =%d, minor =%d \r\n", beep.major, beep.minor);
-
- /*2,初始化cdev*/
- beep.cdev.owner = THIS_MODULE;
- cdev_init(&beep.cdev, &beep_fops);
-
- /*3,添加cdev*/
- ret = cdev_add(&beep.cdev, beep.devid, BEEP_CNT);
- if (ret)
- goto fail_cdevadd;
-
- /*4,创建类*/
- beep.class = class_create(THIS_MODULE, BEEP_NAME);
- if (IS_ERR(beep.class))
- {
- ret = PTR_ERR(beep.class);
- goto fail_class;
- }
-
- /*5,创建设备*/
- beep.device = device_create(beep.class, NULL, beep.devid, NULL, BEEP_NAME);
- if (IS_ERR(beep.device))
- {
- ret = PTR_ERR(beep.device);
- goto fail_device;
- }
-
- /**************设置 BEEP 所使用的 GPIO ***************/
- /* 1、获取设备节点:beep */
- beep.nd = of_find_node_by_path("/beep");
- if (beep.nd == NULL)
- {
- ret = -EINVAL;
- goto fail_nd;
- }
-
- /* 2、 获取设备树中的 gpio 属性,得到 BEEP 所使用的 GPIO 编号 */
- beep.beep_gpio = of_get_named_gpio(beep.nd, "beep-gpios", 0);
-
- if (beep.beep_gpio < 0)
- {
- ret = -EINVAL;
- goto fail_nd;
- }
-
- /*3,申请BEEP所使用的GPIO引脚*/
- ret = gpio_request(beep.beep_gpio, "beep-gpio");
- if (ret)
- {
- printk("Can't request beep gpio \r\n");
- goto fail_nd;
- }
- /* 4、设置 GPIO5_IO01 为输出,并且输出高电平,默认关闭 BEEP */
- ret = gpio_direction_output(beep.beep_gpio, 0);
- if (ret < 0)
- {
- goto fail_set;
- }
-
- /*5,输出低电平,关闭BEEP*/
- gpio_set_value(beep.beep_gpio, 0);
-
- return 0;
-
- fail_set:
- gpio_free(beep.beep_gpio);
- fail_nd:
- device_destroy(beep.class, beep.devid);
- fail_device:
- class_destroy(beep.class);
- fail_class:
- cdev_del(&beep.cdev);
- fail_cdevadd:
- unregister_chrdev_region(beep.devid, BEEP_CNT);
- fail_devid:
- return ret;
- }
-
- /*驱动出口函数*/
- static void __exit beep_exit(void)
- {
- /*关闭蜂鸣器*/
- gpio_set_value(beep.beep_gpio, 1);
-
- /*注销字符设备驱动*/
- cdev_del(&beep.cdev);
- unregister_chrdev_region(beep.devid, BEEP_CNT);
- device_destroy(beep.class, beep.devid);
- class_destroy(beep.class);
-
- gpio_free(beep.beep_gpio);
- }
-
- module_init(beep_init);
- module_exit(beep_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("supersmart");
- #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[]:具体的参数内容,字符串形式
- * ./beepAPP <filename> <0:1> 0 关蜂鸣器,1 开蜂鸣器
- * ./beepAPP /dev/beep 0 关蜂鸣器
- * ./beepAPP /dev/beep 1 开蜂鸣器
- */
-
- #define BEEPOFF 0
- #define BEEPON 1
- int main(int argc, char *argv[])
- {
- int fd, retvalue;
- char *filename;
- unsigned char databuf[1];
-
- if (argc != 3)
- {
- printf("Error Usage!\r\n");
- return -1;
- }
-
- 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("BEEP Control Failed ! \r\n");
- close(fd);
- return -1;
- }
-
- close(fd);
-
- return 0;
- }
- KERNELDIR := /home/znn/linux/IMX6ULL/linux/linux-imx-rel_imx_4.1.15_2.1.0_ga
- CURRENT_PAHT := $(shell pwd)
- obj-m := beep.o
-
- build :kernel_modules
-
- kernel_modules:
- $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PAHT) modules
-
- clean:
- $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PAHT) clean
编译驱动模块
arm-linux-gnueabihf-gcc beepAPP.c -o beepAPP
4.2 运行测试
打开蜂鸣器,蜂鸣器鸣叫
关闭蜂鸣器,蜂鸣器停止鸣叫
卸载驱动
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。