element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
RIoTboard
  • Products
  • Dev Tools
  • Single-Board Computers
  • RIoTboard
  • More
  • Cancel
RIoTboard
Forum Piezo Buzzer Control: Kernel, User Space or Android level?
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join RIoTboard to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 2 replies
  • Subscribers 23 subscribers
  • Views 1123 views
  • Users 0 members are here
  • android
  • buzzer
  • riotboard
  • piezo
Related

Piezo Buzzer Control: Kernel, User Space or Android level?

Former Member
Former Member over 10 years ago

Hey guys,

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

RIoTboard Piezo Buzzer Test from Luke Marvin on Vimeo.

 

In this post I was hoping to discuss more about the "division of labour" across the different levels of software. I'm relatively new to kernels and what-not so maybe someone here can lend me a hand.


In my test I have the buzzer connected through a 1k ohm resistor, to PWM 2 and GND.

image


In my 3.0.35 kernel I have made the following changes as suggested by Eric Nelson (https://community.freescale.com/message/436421):


  1. I updated the leds-pwm.c driver with the changes made in this commit. https://github.com/boundarydevices/linux-imx6/commit/63b106e30a42e5ee89c4c7d0644e853f013276db
  2. I updated my /kernel_imx/arch/arm/configs/imx6_android_defconfig file to include the leds-pwm.c driver. "CONFIG_LEDS_PWM=y"
  3. I added a new platform device to my /arch/arm/mach-mx6/board-mx6solo_RIoTboard.c file using this file as an example. https://github.com/boundarydevices/linux-imx6/blob/boundary-imx_3.0.35_4.1.0/arch/arm/mach-mx6/board-mx6_r.c#L803
  4. I recompiled my kernel and boot image, programmed the board and booted it up.

 

Now I have a device located at /sys/class/leds/buzzer which I can echo values into, which then plays a sound.

 

I wrote the following C file to play the wailing sound from the video.

#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/delay.h>


#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))


static void pabort(const char *s)
{
  perror(s);
  abort();
}
static int note_fd;
static int frequency_fd;
static int period_fd;
static char *note = "/sys/class/leds/buzzer/note";
static char *frequency = "/sys/class/leds/buzzer/frequency";
static char *period = "/sys/class/leds/buzzer/period";
static char buff;
static char stop = ' ';






static void play_note(char* f, unsigned int length)
{
  write(frequency_fd,f,sizeof(f));
  usleep(length);
  write(frequency_fd,"0",1);
}


int main(int argc, char *argv[])
{


  int ret = 0;
  char* firstNote = "2000";
  unsigned int length = 250000;
  frequency_fd = open(frequency,O_RDWR);
  if (note_fd < 0)
  pabort("lcd reset pin unavailable");


  play_note("4096",150000);
  play_note("2048",150000);


  play_note("4096",150000);
  play_note("2048",150000);


  play_note("4096",150000);
  play_note("2048",150000);


  play_note("4096",150000);
  play_note("2048",150000);


  close(note_fd);


  return ret;
}


I cross compiled the file on my linux computer.

 

'/home/riot/android-imx6-jb4.3-1.0.0/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/arm-linux-androideabi-gcc-4.6.x-google' --sysroot='/home/riot/android-imx6-jb4.3-1.0.0/prebuilts/ndk/8/platforms/android-14/arch-arm'  -o wail wail.c

 

 

Then I used Dropbox to send the binary to my Windows PC where I upload the file to the board using ADB. Finally I chmod the file to make it executable and I execute it.

 

Phewf. It works.

 

Then I whipped up an Android app that has a Buzzer class.

public class Buzzer {


  InputStream buzzerInputStream;
  OutputStream buzzerOutputStream;
  PrintStream buzzerPrintStream;
  String BUZZER_PATH = "/sys/class/leds/buzzer/frequency";

  Handler stopBuzzHandler = new Handler();
  Runnable stopBuzzRunnable = new Runnable() {
        @Override
        public void run() {
        buzzerPrintStream.print("0");
        }
  };

  public Buzzer() throws IOException {
// buzzerInputStream = socket.getInputStream();
  buzzerOutputStream = new FileOutputStream(BUZZER_PATH);
  buzzerPrintStream = new PrintStream(buzzerOutputStream);
  }

  public void buzz(int length){
  buzzerPrintStream.print("1000");
  stopBuzzHandler.postDelayed(stopBuzzRunnable, (long)length);
  }
}

 

As long as I have chmod'd the file I can access it directly from the Android app and use the buzzer.

 

In conclusion, here is my plan. Let me know if it sounds reasonable to you guys.

  1. Modify the pwm-leds.c driver to include a new "attribute" file called wail. When I write a 1 to it, it starts wailing. When I write a 0 to it, it stops.
  2. Set the permissions of the file at boot time.
  3. In my Android app, when there is an alarm I write a 1 to the file. Then write a 0 to cancel it.
  • Sign in to reply
  • Cancel
  • Former Member
    0 Former Member over 10 years ago

    More specifically I was wondering if it is safe to do any sort of loops within the kernel module. I need to alternate between two frequencies for my alarm sound.

     

    Do I need to make a thread within the module? Or do I just make a user space library that does the looping and call it with the NDK? I really don't know.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • tusharp
    0 tusharp over 10 years ago in reply to Former Member

    Or do I just make a user space library that does the looping and call it with the NDK?

           userspace libs seems to be good option than modules.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube