??上一篇博客中我们借助 pinctrl 和 gpio 子系统编写了 LED 灯驱动,笔者的I.MX6ULL开发板上还有一个蜂鸣器,从软件的角度考虑,蜂鸣器驱动和 LED 灯驱动其实是一摸一样的,都是控制 IO 输出高低电平。本篇文章我们就来学习编写蜂鸣器的 Linux 驱动,也算是对上一章讲解的 pinctrl和 gpio 子系统的巩固。
蜂鸣器驱动原理
??开发板上的蜂鸣器是通过SNVS_TAMPER1引脚来控制,我们来看一下如果在 Linux 下编写蜂鸣器驱动需要做哪些工作: ??①、在设备树中添加 SNVS_TAMPER1 引脚的 pinctrl 信息。 ??②、在设备树中创建蜂鸣器节点,在蜂鸣器节点中加入 GPIO 信息。 ??③、编写驱动程序和测试 APP。
程序编写
1.修改设备树文件
1.1 添加pinctrl节点
??打开imx6ull-14x14-evk.dts,在 iomuxc 节点的 imx6ul-evk 子节点下创建一个名为“pinctrl_beep”的子节点,节点内容如下所示:
pinctrl_beep: beepgrp {
fsl,pins = <
MX6ULL_PAD_SNVS_TAMPER1__GPIO5_IO01 0x10B0
>;
};
??将SNVS_TAMPER1这个PIN复用为GPIO5_IO01,宏MX6ULL_PAD_SNVS_TAMPER1__GPIO5_IO01定义在 arch/arm/boot/dts/imx6ull-pinfunc-snvs.h文件中。
1.2 添加 BEEP 设备节点
??在根节点“/”下创建 BEEP 节点,节点名为“beep”,节点内容如下:
beep {
#address-cells = <1>;
#size-cells = <1>;
compatible = "atkalpha-beep";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_beep>;
beep-gpio = <&gpio5 1 GPIO_ACTIVE_HIGH>;
status = "okay";
};
1.3 检查 PIN 是否被其他外设使用
??在实验中蜂鸣器使用的PIN为 SNVS_TAMPER1,因此先检查 PIN 为 SNVS_TAMPER1这个 PIN 有没有被其他的 pinctrl 节点使用,如果有使用的话就要屏蔽掉,然后再检查GPIO5_IO01 这个 GPIO 有没有被其他外设使用,如果有的话也要屏蔽掉。 ??设备树编写完成以后使用“ make dtbs”命令重新编译设备树,然后使用新编译出来的imx6ull-14x14-evk.dtb文件启动 Linux 系统。启动成功以后进入“/proc/device-tree”目录中查看“beep”节点是否存在,如果存在的话就说明设备树基本修改成功,结果如图所示:
2. 蜂鸣器驱动程序编写
2.1 beep.c
#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/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;
struct class *class;
struct device *device;
int major;
int minor;
struct device_node *nd;
int 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[1];
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 if(beepstat == BEEPOFF) {
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,
.open = beep_open,
.write = beep_write,
.release = beep_release,
};
static int __init beep_init(void)
{
int ret = 0;
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!\r\n");
}
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);
ret = gpio_direction_output(beep.beep_gpio, 1);
if(ret < 0) {
printk("can't set 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);
beep.cdev.owner = THIS_MODULE;
cdev_init(&beep.cdev, &beep_fops);
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("jiajia2020");
2.2 beepApp.c
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#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", argv[1]);
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;
}
retvalue = close(fd);
if(retvalue < 0){
printf("file %s close failed!\r\n", argv[1]);
return -1;
}
return 0;
}
|