测试环境: CPU: AT91SAM9X35 Linux: Atmel提供的linux-at91-linux4sam_5.3 (Linux-4.1.0)
转载请注明: 凌云物网智科嵌入式实验室: http://iot-yun.com/ 郭文学<[email protected]>
最新的atmel代码使用了Device Tree替换了以前的platform虚拟总线架构,该文章介绍了最新的Linux内核如何添加使能PWM控制蜂鸣器的全过程。目录结构:
1, DTS修改
2, Make menuconfig选项
3, 应用程序
4,测试
1, DTS修改
Linux内核里关于pwm-beeper驱动的DTS说明文档:
[[email protected] linux-at91-linux4sam_5.3]$ cat Documentation/devicetree/bindings/input/pwm-beeper.txt * PWM beeper device tree bindings Registers a PWM device as beeper. Required properties: - compatible: should be "pwm-beeper" - pwms: phandle to the physical PWM device
在Linux内核的dts文件中,只有mini6410的dts文件中使用了pwm-beeper驱动,所以我们需要参考他来修改:
[[email protected] linux-at91-linux4sam_5.3]$ vim arch/arm/boot/dts/s3c6410-mini6410.dts buzzer { compatible = "pwm-beeper"; pwms = <&pwm 0 1000000 0>; pinctrl-names = "default"; pinctrl-0 = <&pwm0_out>; };
修改at91sam9x35ek的DTS文件,添加pwm-beeper设备
[[email protected] linux-at91-linux4sam_5.3]$ vim arch/arm/boot/dts/at91sam9x35ek.dts ahb { apb { macb0: [email protected] { phy-mode = "rmii"; status = "okay"; }; pwm0: [email protected] { pinctrl-names = "default"; pinctrl-0 = <&pinctrl_pwm0_pwm2_0>; status = "okay"; }; ............... buzzer { compatible = "pwm-beeper"; pwms = <&pwm0 2 1000000 0>; pinctrl-names = "default"; pinctrl-0 = <&pinctrl_pwm0_pwm2_0>; status = "okay"; }; ...............
2, 内核make menuconfig
Linux内核自带PWM的beeper驱动drivers/input/misc/pwm-beeper.c,我们需要在make menuconfig中选中并使能它。
Device Drivers ---> Input device support ---> [*] Miscellaneous devices ---> <*> PWM beeper support
内核编译启动后,我们可以看到新的蜂鸣器设备:
~ >: cat /proc/bus/input/ devices handlers ~ >: cat /proc/bus/input/devices I: Bus=0019 Vendor=001f Product=0001 Version=0100 N: Name="pwm-beeper" P: Phys=pwm/input0 S: Sysfs=/devices/soc0/buzzer/input/input0 U: Uniq= H: Handlers=kbd event0 B: PROP=0 B: EV=40001 B: SND=6 ~ >: ls /sys/class/input/event0/ dev device power subsystem uevent ~ >: ls /dev/input/event0 /dev/input/event0
3, 编写蜂鸣器测试应用程序
/********************************************************************************* * Copyright: (C) 2016 Guo Wenxue<[email protected]> * All rights reserved. * * Filename: test_buzzer.c * Description: This file * * Version: 1.0.0(08/06/2016) * Author: Guo Wenxue <[email protected]> * ChangeLog: 1, Release initial version on "08/06/2016 03:16:54 PM" * ********************************************************************************/ #include <stdio.h> #include <unistd.h> #include <stdint.h> #include <fcntl.h> #include <linux/input.h> #include <errno.h> #include <string.h> #define DEV_BUZZER "/dev/input/event0" #define BUZZER_FREQ 2700 /* 2.7KHz */ int main(int argc, char **argv) { int fd = -1; int version ; int rv; struct input_event event; int freq = BUZZER_FREQ; if( argc != 2 ) { printf("Usage: %s [freq<2700/0>]\n", argv[0]); return -1; } freq = atoi(argv[1]); if( (fd=open(DEV_BUZZER, O_RDWR)) < 0) { printf("open buzzer ‘%s‘ failure: %s\n", DEV_BUZZER, strerror(errno)); return -2; } event.type = EV_SND; event.code = SND_TONE; event.value = freq; write(fd, &event, sizeof(struct input_event)); close(fd); return 0; }
4, 编译和测试
PC端编译:
[[email protected] utils]$ /opt/buildroot-2012.08/arm926t/usr/bin/arm-linux-gcc test_buzzer.c -o test_buzzer [[email protected] utils]$ file test_buzzer test_buzzer: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
ARM端下载测试
~ >: tftp -gr test_buzzer 192.168.2.18 test_buzzer 100% |*******************************| 5712 0:00:00 ETA ~ >: chmod a+x test_buzzer ~ >: ./test_buzzer Usage: ./test_buzzer [freq<2700/0>] ~ >: ./test_buzzer 2700 蜂鸣器的工作频率是2.7KHz,开启蜂鸣器 ~ >: ./test_buzzer 0 传参数0则关闭蜂鸣器
时间: 2024-10-09 19:49:35