Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the test method of using PWM to control the output of buzzer in android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian introduces in detail "what is the test method for controlling the output of the buzzer with PWM in android", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the test method for controlling the output of the buzzer with PWM in android" can help you solve your doubts.

Based on QtE system. 4418MCU provides a total of 5 PWM outputs, one of which is not led out, so there are 4 available PWM outputs.

Note: the routines provided in this document do not register devices and drivers, but are only configured in the driver entry and exit functions. If you need to generate a device node, you need to add the rest of it yourself, which can be found in the documentation for GPIO operation.

1 configure IO

Open the backplane circuit diagram and search for "beep". You can see that the network name of beep is "MCU_ISO7816_CLK", as shown in the following figure.

In the core board schematic, search for the keyword "MCU_ISO7816_CLK" and you can see its corresponding PWM2, as shown in the following figure.

So, next we will operate on PWM2. Before doing the following, we need to configure the kernel, cancel the driver of buzzer in the kernel, and remove the driver from the buzzer, as shown in the following figure.

Change this option to unchecked, as shown in the following illustration.

Next, compile and burn the kernel image (boot.img) to the development board. Then do the following.

2 write driver

There is a rule in the linux kernel that Linux kernel developers summarize all the common things, and personalized things leave interfaces. Similar to GPIO drivers, PWM drivers also provide corresponding interface functions in the kernel, and the interface functions provided by the kernel are declared in include/linux/pwm.h.

/ / apply for a PWM resource

Struct pwm_device * pwm_request (int pwm_id, const char * label)

/ / release a PWM resource

Void pwm_free (struct pwm_device * pwm)

/ / configure PWM

Int pwm_config (struct pwm_device * pwm, int duty_ns, int period_ns)

/ / the time taken to enable PWM,duty_ns is high, and the whole cycle of period_ns is the time spent, in nanoseconds.

Int pwm_enable (struct pwm_device * pwm)

/ / do not enable PWM

Void pwm_disable (struct pwm_device * pwm)

Based on the above, we can write a simple pwm output program to control the frequency of the buzzer. Create

The file named 4418x_pwm.c, the program code is shown below.

# include

# include

# include

# include

# include

# include

# include

# include

# include

/ * pwm for this buzzer*/

Struct pwm_device * pwm = NULL

Static int _ init buzzer_init (void)

{

Int ret

Printk ("check buzzer init.\ n")

Pwm = pwm_request (2, "buzzer")

If (pwm = = NULL) {

Printk ("buzzer open error.\ n")

}

/ / printk (KERN_EMERG "pwm_request d", pwm)

Ret=pwm_config (pwm,100000,200000); / / Sound with 1000Hz frequency set

Printk ("pwm_config d", ret)

Printk ("pwm_config d", ret)

Ret=pwm_enable (pwm)

Printk ("pwm_enable d", ret)

Printk (KERN_EMERG "done2.\ n")

Return 0

}

Static void _ exit buzzer_exit (void)

{

Pwm_config (pwm,0,0); / / disable buzzer output

Pwm_disable (pwm); / / close pwm

Pwm_free (pwm); / / release pwm resources

}

Module_init (buzzer_init)

Module_exit (buzzer_exit)

MODULE_DESCRIPTION ("pwm_buzzer driver")

MODULE_LICENSE ("GPL")

2.2 write Makefile

Next, write the Makefile file.

Export ARCH=arm

Obj-m + = 4418x_pwm.o

KDIR: = / home/topeet/4418/4G/20170914/android/kernel

PWD = $(shell pwd)

All:

Make-C $(KDIR) masking $(PWD) modules

Clean:

Rm-rf * .o modules.order * .ko * mod.c Module.symvers

In the script, export ARCH=arm means to set the target CPU category to arm, that is, the compiled dependent kernel and driver module target CPU is ARM.

Obj-m + = 4418x_pwm.o indicates that the compiled source file is 4418x_pwm.c. If the source file name changes, it needs to be changed to the corresponding file name.

The KDIR parameter points to the corresponding kernel source directory. The author's kernel source code is in the / home/topeet/4418/4G/20170914/android/kernel directory, and users need to modify it according to their own specific conditions.

2.3 compile and run

First set the environment variable to use the compiler in the source code at compile time. Use "cdkernel" in the source directory to enter the kernel directory. Then use the command "make menuconfig" to open the kernel default configuration interface, as shown in the following figure.

Enter the highlighted "General setup" in the figure, as shown in the following figure.

You can see the highlighted text in the picture, describing the current source code using the compiler "arm-eabi-". Let's go back to the source folder and use the command "find. /-name arm-eabi-*" to get the path of the compiler in the source code, as shown in the following figure.

In this way, the absolute path of the source compiler is the path of the source code plus the path of the red box in the image above, which is "/ home/topeet/4418/4G/20170914/android/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin/" in this article, because the author logs in with root, so open the file "/ root/.bashrc" to add such a content, as shown in the following figure.

Next, we can compile.

Put Makefile and C programs in the same directory on the Ubuntu system. This is shown in the following figure.

Type "make" in the current directory to start compilation and generate the kernel module file "4418x_pwm.ko", as shown in the following figure.

Copy the kernel module file to the development board, and then load the module in HyperTerminal with the command "insmod4418x_pwm.ko", as shown in the following figure.

The module loaded successfully and the buzzer sounded with high frequency sound. Then use the command "rmmod 4418x_pwm" to uninstall the driver, as shown in the following figure.

At this point, the buzzer stops playing high-frequency sound, and the PWM buzzer test routine ends here.

Read this, the "android with PWM control buzzer output test method is what" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report