1. First write the Linux kernel module LED
#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");#defineGpm4con 0x110002e0#defineGpm4dat 0x110002e4#defineled_on _iow (' G ', 0,int)#defineLed_off _iow (' G ', 1,int)Static structCdev Dev;//1.1 Assigning Cdev structuresStaticdev_t dev_no;struct class*Led_class;StaticUnsignedint*Led_con;StaticUnsignedint*Led_dat;LongLed_ioctl (structFile *file, unsignedintCMD, unsignedLongArg) { Switch(cmd) { CaseLed_on:writel ((Readl (Led_dat)& (~ (0x1<< (arg-1))) , Led_dat); Break; CaseLed_off:writel ((Readl (Led_dat)| (0x1<< (arg-1)) , Led_dat); Break; default: return-EINVAL; Break; } return 0;}structFile_operations Led_fops ={. Owner=this_module,. Unlocked_ioctl=Led_ioctl,};Static voidHw_init () {//initializing Gpio control registersLed_con = Ioremap (Gpm4con,4);//Address MappingLed_dat = Ioremap (Gpm4dat,4 ); Writel ((Readl (Led_con)&~0xFFFF)|0x1111, Led_con); Writel (Readl (led_dat)|0xf, Led_dat); }Static intLed_init () {//1.2 Initializing the CDEV structureAlloc_chrdev_region (&dev_no,0,1,"my_led" ); Cdev_init (&dev, &led_fops); Dev.owner=This_module; //1.3 Registering the CDEV structureCdev_add (&dev, Dev_no,1 ); //2. Hardware initializationHw_init (); //3. Create a Device fileLed_class = Class_create (This_module,"my_led");//Create a device classDevice_create (Led_class, NULL, Dev_no,null,"%s","my_led"); PRINTK ("Init led device ok!\n"); return 0;}voidLed_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. The header file needs to be generated. The application is required to generate the header file. The application software is embedded in the country. 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 project file folder.
javah-d jni-classpath/opt/android-sdk-linux/platforms/android-/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 in the corresponding Android APP source file project.
Com.android.jack.ndk.happy.MainActivity is the name of the project for Android.
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 a function that 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_myledlocal_src_files:= Ndk_led.cinclude $ (build_shared_library)
If you want to generate a static library, modify shared to static.
Then go back to the top level directory to execute command ndk-build. The libs/armeabi/libndk_test_myled.so library file will be generated.
Android Driver Write LED-NDK program