Android Driver writes LED-NDK program _android

Source: Internet
Author: User

1. First write Linux kernel module LEDs

#include <linux/kernel.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/slab.h > #include <linux/device.h> #include <asm/io.h> #include <asm/uaccess.h> #include <linux/

Cdev.h> module_license ("GPL"); #define GPM4CON 0x110002e0 #define GPM4DAT 0x110002e4 #define LED_ON _iow (' G ', 0,int) #define Led_off _iow (' G ', 1,int) St
atic struct Cdev dev;//1.1 distribution Cdev structure static dev_t dev_no;

struct class *led_class;
static unsigned int *led_con;

static unsigned int *led_dat; Long led_ioctl (struct file *file, unsigned int cmd, unsigned long arg) {switch (cmd) {case Led_on:writ
      El ((Readl (Led_dat) & (~ (0x1<< (arg-1))), Led_dat);

    Break Case Led_off:writel (Readl (Led_dat) | (
      0x1<< (arg-1))), Led_dat);

    Break    
      Default:return-einval;
  Break
return 0;


} struct File_operations led_fops = {. Owner = This_module,. unlocked_ioctl = Led_ioctl,}; Static VOID Hw_init () {//Initialize GPIO control Register Led_con = Ioremap (Gpm4con, 4);
  Address Map Led_dat = Ioremap (Gpm4dat, 4);  
  Writel ((Readl (Led_con) &~0xffff) |0x1111,led_con);  
  
Writel (Readl (Led_dat) |0xf,led_dat);
  The static int led_init () {//1.2 initializes the CDEV structure alloc_chrdev_region (&dev_no, 0, 1, "my_led");  
  Cdev_init (&dev, &led_fops);
  Dev.owner = This_module;
  1.3 Registered CDEV Structure Cdev_add (&dev, Dev_no, 1);
  2. Hw_init of hardware initialization ();        3. Create device file Led_class = Class_create (This_module, "my_led");

  Create device Class Device_create (Led_class, NULL, Dev_no,null, "%s", "my_led");

  PRINTK ("Init led device ok!\n");
return 0;
  } void Led_exit () {Device_destroy (led_class,dev_no);

  Class_destroy (Led_class);
  Iounmap (Led_con);

  Iounmap (Led_dat);
  Cdev_del (&dev);
Unregister_chrdev_region (dev_no,1);
} module_init (Led_init);

 Module_exit (Led_exit);

2. A header file needs to be generated.

The build header file requires an application. Therefore, the use of state-embedded software provided by the app. After decompression, modify the corresponding address of the Local.properties file.

Open the studio.sh and recompile the project.

After compiling the project, execute the command in the app's engineering file folder.

Javah-d jni-classpath/opt/android-sdk-linux/platforms/android-23/android.jar:/home/my_android/led\
/ndk/ndk_app/app/build/intermediates/classes/debug/com.android.jack.ndk.happy.mainactivity

Where/opt/android-sdk-linux/platforms/android-23/android.jar is the address in the Android SDK.

/home/my_android/led/ndk/ndk_app/app/build/intermediates/classes/debug/is the address of the corresponding Android APP source file project.

Com.android.jack.ndk.happy.MainActivity is the name of the Android project.

After the command is run, the Jni folder is generated in the directory. Where Com_android_jack_ndk_happy_mainactivity.h is the header file we need.

The file is declared as the function we need to implement.

Create NDK_LED.C source files and makefile files in the Jni file android.mk

Local_path: = $ (call My-dir)
include $ (clear_vars)
local_module: = ndk_test_myled local_src_files
: = Ndk_ LED.C
include $ (build_shared_library)

Modify shared as static if you want to generate a static library.

It then returns to the previous level of the directory Execution command ndk-build. The libs/armeabi/libndk_test_myled.so library file will be generated.

The above is the Android driver to write LED-NDK program data collation, thank you for your support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.