English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
1Firstly, write a LINUX kernel module for 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"); #define GPM4CON 0X110002E0 #define GPM4DAT 0X110002E4 #define LED_ON _IOW('G',0,int) #define LED_OFF _IOW('G',1,int) static struct cdev dev;//1.1 Allocate 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: writel((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(GPM,4CON, 4 ); //Address mapping led_dat = ioremap(GPM,4DAT, 4 ); writel((readl(led_con)&~0xffff)|0x1111,led_con); writel(readl(led_dat)|0xf,led_dat); } static int led_init() { //1.2 Initialize cdev structure alloc_chrdev_region(&dev_no, 0, 1, "my_led" ); cdev_init(&dev, &led_fops); dev.owner = THIS_MODULE; //1.3 Register cdev structure cdev_add(&dev, dev_no, 1 ); //2.Hardware initialization hw_init(); //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());
2Need to generate header files.
The generation of header files requires the application. Therefore, use the app software provided by Guoqian. After unpacking, modify the corresponding address in the local.properties file.
Open studio.sh and recompile the project.
After compiling the project, execute the command in the folder of the app project file.
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
Among them/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/ It is the address in the corresponding Android app source file project.
com.android.jack.ndk.happy.MainActivity is the name of the Android project.
After running the command, a jni folder will be generated in the directory. The header file we need is com_android_jack_ndk_happy_MainActivity.h.
The file declarations are the functions we need to implement.
Create the ndk_led.c source file and makefile file Android.mk in the jni file
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := ndk_test_myled LOCAL_SRC_FILES := ndk_led.c include $(BUILD_SHARED_LIBRARY)
If you want to generate a static library, you can change SHARED to STATIC.
Then go back to the upper-level directory and execute the command ndk-build. It will generate libs/armeabi/libndk_test_myled.so library file.
The above is the LED driver writing for Android-This is the material compilation of NDK programs, thank you all for your support to this site!