English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
1. Prima di tutto, scrivere un modulo kernel LED per LINUX
#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 分配cdev结构 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() { //初始化GPIO控制寄存器 led_con = ioremap(GPM4CON, 4); //地址映射 led_dat = ioremap(GPM4DAT, 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());
2. It is necessary to generate header files.
To generate header files, the application is required. Therefore, use the app software provided by Guoqian. After unzipping, 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 app project 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
/opt/android-sdk-linux/platforms/android-23/android.jar è l'indirizzo nel SDK Android.
/home/my_Android/led/NDK/NDK_APP/app/build/intermediates/classes/debug/ è l'indirizzo del progetto di file sorgente dell'app Android corrispondente.
com.android.jack.ndk.happy.MainActivity è il nome del progetto Android.
Dopo aver eseguito il comando, verrà generato una cartella jni nella directory. Il file com_android_jack_ndk_happy_MainActivity.h è il file di header di cui abbiamo bisogno.
Le dichiarazioni dei file sono le funzioni che dobbiamo implementare.
Creare il file sorgente ndk_led.c e il file makefile Android.mk nel file jni
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := ndk_test_myled LOCAL_SRC_FILES := ndk_led.c include $(BUILD_SHARED_LIBRARY)
Se si desidera generare una libreria statica, modificare SHARED in STATIC.
Poi tornare al livello superiore della directory, eseguire il comando ndk-build. Verrà generato il file di libreria libndk_test_myled.so in libs/armeabi/
Ecco la raccolta di materiali per la scrittura di programmi LED-NDK per Android, grazie per il supporto al nostro sito!