Android JNI is used for driver testing and androidjni

Source: Internet
Author: User
Tags skia

Android JNI is used for driver testing and androidjni

Hardware Platform: cloud6410

Operating Systems: Ubuntu and windows

Board System: Android

Development tools: jdk, ndk, and eclipse

This test starts from the compilation of the Linux kernel module. The following uses the pwm driver of as an example.

Pwm_6410.c:

#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/poll.h>#include <linux/clk.h>#include <linux/cdev.h>#include <linux/device.h>#include <linux/miscdevice.h>#include <linux/interrupt.h>#include <plat/regs-timer.h>#include <plat/gpio-cfg.h>#include <asm/irq.h>#include <asm/io.h>#include <asm/mach/time.h>#include <asm/uaccess.h>#include <mach/map.h>#include <mach/regs-clock.h>#include <mach/regs-gpio.h>#include <mach/hardware.h>#include <mach/gpio-bank-e.h>#include <mach/gpio-bank-f.h>#include <mach/gpio-bank-k.h>#include <mach/regs-irq.h>#define DEVICE_NAME  "pwm"static struct semaphore lock;static void PWM_Set_Freq( unsigned long freq ){unsigned long tcon;unsigned long tcnt;unsigned long tcfg1;unsigned long tcfg0;unsigned long pclk;unsigned tmp;struct clk *clk_p;    printk ("Freq is %d",freq);tmp = readl(S3C64XX_GPFCON);//PWM GPF15    tmp &= ~(0x3U << 30);// Timer1    tmp |=  (0x2U << 30);writel(tmp, S3C64XX_GPFCON);tcon  = __raw_readl(S3C_TCON);tcfg1 = __raw_readl(S3C_TCFG1);tcfg0 = __raw_readl(S3C_TCFG0);tcfg0 &= ~S3C_TCFG_PRESCALER0_MASK;tcfg0 |= (50 - 1);     tcfg1 &= ~S3C_TCFG1_MUX1_MASK;    tcfg1 |= S3C_TCFG1_MUX1_DIV16;__raw_writel(tcfg1, S3C_TCFG1);__raw_writel(tcfg0, S3C_TCFG0);clk_p = clk_get(NULL, "pclk");pclk  = clk_get_rate(clk_p);tcnt  = (pclk/50/16)/freq;__raw_writel(tcnt, S3C_TCNTB(1));__raw_writel(tcnt/2, S3C_TCMPB(1));tcon &= ~(0xf << 8);tcon |= (0xb << 8);__raw_writel(tcon, S3C_TCON);tcon &= ~(2 << 8);__raw_writel(tcon, S3C_TCON);}void PWM_Stop( void ){unsigned tmp;tmp = readl(S3C64XX_GPFCON);tmp &= ~(0x3U << 30);// set GPF15writel(tmp, S3C64XX_GPFCON);}static int s3c64xx_pwm_open(struct inode *inode, struct file *file){if (!down_trylock(&lock))return 0;elsereturn -EBUSY;}static int s3c64xx_pwm_close(struct inode *inode, struct file *file){up(&lock);return 0;}static long s3c64xx_pwm_ioctl(struct file *filep, unsigned int cmd, unsigned long arg){switch (cmd) {case 1:if (arg == 0)return -EINVAL;PWM_Set_Freq(arg);break;case 0:PWM_Stop();break;}return 0;}static struct file_operations dev_fops = {    .owner= THIS_MODULE,    .open= s3c64xx_pwm_open,    .release= s3c64xx_pwm_close,     .unlocked_ioctl= s3c64xx_pwm_ioctl,};static struct miscdevice misc = {    .minor = MISC_DYNAMIC_MINOR,    .name  = DEVICE_NAME,    .fops  = &dev_fops,};static int __init dev_init(void){int ret;init_MUTEX(&lock);ret = misc_register(&misc);printk (DEVICE_NAME"\tinitialized\n");    return ret;}static void __exit dev_exit(void){misc_deregister(&misc);}MODULE_LICENSE("GPL");module_init(dev_init);module_exit(dev_exit);
Add Makefile:

obj-$(CONFIG_PWM_S3C6410)        += pwm_6410.o
Add Kconfig:

config PWM_S3C6410tristate "pwm"depends on CPU_S3C6410

Make menuconfig

Make zImage and then start the Android system

Ls/dev will see the device driver named pwm

If the driver has been loaded, you need to compile a test program for Android. JNI is an abbreviation of Java Native Interface. It is called locally by Java. It allows java code to interact with code written in other languages. Use JNI to write the test program.

Eclipse creates a new application project named pwm and the package named com. example. pwm.

The default java code: PwmActivity. java

package com.example.pwm;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;public class PwmActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_pwm);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_pwm, menu);return true;}}
Add local method:

Package com. example. pwm; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; public class PwmActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_pwm) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. activity_pwm, menu); return true;} public static native int pwm_set_freq (int I, int j); static {System. loadLibrary ("pwm"); // Add the C/C ++ dynamic library import method }}

Edit activity_pwm.xml IN res/layout

Add button control

    <Button        android:id="@+id/pwm_on"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/textView1"        android:layout_alignLeft="@+id/textView1"        android:layout_marginBottom="39dp"        android:onClick="onPwmOnClicked"        android:text="@string/pwm" />
Edit strings. xml under res/values

Add

<string name="pwm">pwm</string>
Add the following to PwmActivity. java:

    public void onPwmOnClicked(View v){    pwm_set_freq(1,200);    }
PwmActivity. java:

Package com. example. pwm; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; public class PwmActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_pwm) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items The action bar if it is present. getMenuInflater (). inflate (R. menu. activity_pwm, menu); return true;} public void onPwmOnClicked (View v) {pwm_set_freq (1,200); // press the button to output the waveform} public static native int pwm_set_freq (int I, int j); static {System. loadLibrary ("pwm"); // Add the C/C ++ dynamic library import method. This library must be compiled and generated using the NDK tool .} }
After completing the preceding steps, compile the project and copy it to Ubuntu.

Create a jni folder under the project directory:


Use the javah command to generate the jni header file

Note that there is no space after the colon

Generated header file:


Com_example_pwm_PwmActivity.h:

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_example_pwm_PwmActivity */#ifndef _Included_com_example_pwm_PwmActivity#define _Included_com_example_pwm_PwmActivity#ifdef __cplusplusextern "C" {#endif#undef com_example_pwm_PwmActivity_MODE_PRIVATE#define com_example_pwm_PwmActivity_MODE_PRIVATE 0L#undef com_example_pwm_PwmActivity_MODE_WORLD_READABLE#define com_example_pwm_PwmActivity_MODE_WORLD_READABLE 1L#undef com_example_pwm_PwmActivity_MODE_WORLD_WRITEABLE#define com_example_pwm_PwmActivity_MODE_WORLD_WRITEABLE 2L#undef com_example_pwm_PwmActivity_MODE_APPEND#define com_example_pwm_PwmActivity_MODE_APPEND 32768L#undef com_example_pwm_PwmActivity_MODE_MULTI_PROCESS#define com_example_pwm_PwmActivity_MODE_MULTI_PROCESS 4L#undef com_example_pwm_PwmActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING#define com_example_pwm_PwmActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L#undef com_example_pwm_PwmActivity_BIND_AUTO_CREATE#define com_example_pwm_PwmActivity_BIND_AUTO_CREATE 1L#undef com_example_pwm_PwmActivity_BIND_DEBUG_UNBIND#define com_example_pwm_PwmActivity_BIND_DEBUG_UNBIND 2L#undef com_example_pwm_PwmActivity_BIND_NOT_FOREGROUND#define com_example_pwm_PwmActivity_BIND_NOT_FOREGROUND 4L#undef com_example_pwm_PwmActivity_BIND_ABOVE_CLIENT#define com_example_pwm_PwmActivity_BIND_ABOVE_CLIENT 8L#undef com_example_pwm_PwmActivity_BIND_ALLOW_OOM_MANAGEMENT#define com_example_pwm_PwmActivity_BIND_ALLOW_OOM_MANAGEMENT 16L#undef com_example_pwm_PwmActivity_BIND_WAIVE_PRIORITY#define com_example_pwm_PwmActivity_BIND_WAIVE_PRIORITY 32L#undef com_example_pwm_PwmActivity_BIND_IMPORTANT#define com_example_pwm_PwmActivity_BIND_IMPORTANT 64L#undef com_example_pwm_PwmActivity_BIND_ADJUST_WITH_ACTIVITY#define com_example_pwm_PwmActivity_BIND_ADJUST_WITH_ACTIVITY 128L#undef com_example_pwm_PwmActivity_CONTEXT_INCLUDE_CODE#define com_example_pwm_PwmActivity_CONTEXT_INCLUDE_CODE 1L#undef com_example_pwm_PwmActivity_CONTEXT_IGNORE_SECURITY#define com_example_pwm_PwmActivity_CONTEXT_IGNORE_SECURITY 2L#undef com_example_pwm_PwmActivity_CONTEXT_RESTRICTED#define com_example_pwm_PwmActivity_CONTEXT_RESTRICTED 4L#undef com_example_pwm_PwmActivity_RESULT_CANCELED#define com_example_pwm_PwmActivity_RESULT_CANCELED 0L#undef com_example_pwm_PwmActivity_RESULT_OK#define com_example_pwm_PwmActivity_RESULT_OK -1L#undef com_example_pwm_PwmActivity_RESULT_FIRST_USER#define com_example_pwm_PwmActivity_RESULT_FIRST_USER 1L#undef com_example_pwm_PwmActivity_DEFAULT_KEYS_DISABLE#define com_example_pwm_PwmActivity_DEFAULT_KEYS_DISABLE 0L#undef com_example_pwm_PwmActivity_DEFAULT_KEYS_DIALER#define com_example_pwm_PwmActivity_DEFAULT_KEYS_DIALER 1L#undef com_example_pwm_PwmActivity_DEFAULT_KEYS_SHORTCUT#define com_example_pwm_PwmActivity_DEFAULT_KEYS_SHORTCUT 2L#undef com_example_pwm_PwmActivity_DEFAULT_KEYS_SEARCH_LOCAL#define com_example_pwm_PwmActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L#undef com_example_pwm_PwmActivity_DEFAULT_KEYS_SEARCH_GLOBAL#define com_example_pwm_PwmActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L/* * Class:     com_example_pwm_PwmActivity * Method:    pwm_set_freq * Signature: (II)I */JNIEXPORT jint JNICALL Java_com_example_pwm_PwmActivity_pwm_1set_1freq  (JNIEnv *, jclass, jint, jint);#ifdef __cplusplus}#endif#endif

Copy the header file to the jni directory, and create pwm. c In the jni directory:

# Include <jni. h> # include <stdio. h> # include <stdlib. h> # include <fcntl. h> # include <unistd. h> # include <sys/ioctl. h> # include <android/log. h> # define LOG_TAG "PWM" // android logcat # define LOGI (...) _ android_log_print (ANDROID_LOG_INFO, LOG_TAG ,__ VA_ARGS _) # define LOGE (...) _ android_log_print (ANDROID_LOG_ERROR, LOG_TAG ,__ VA_ARGS _) jint JNICALL encode (JNIEnv * env, jclass thiz, jint cmd, jint freq) {// The function name must be consistent with the int fd; fd = open ("/dev/pwm", O_RDWR); if (fd <0) {printf ("Open/dev/pwm file error \ n"); return-1;} ioctl (fd, 1,200); close (fd); return 0 ;}

Create Android. mk in the jni directory

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := pwmLOCAL_SRC_FILES := pwm.cLOCAL_LDLIBS := -llogLOCAL_C_INCLUDES := $(MY_ANDROID_SOURCE)/frameworks/base/core/jni/android/graphics \$(MY_ANDROID_SOURCE)/external/skia/include/core \$(MY_ANDROID_SOURCE)/external/skia/include/images \$(MY_ANDROID_SOURCE)/frameworks/base/include \$(MY_ANDROID_SOURCE)/system/core/include include $(BUILD_SHARED_LIBRARY)
Run the ndk-build command. If the project directory does not contain libs/armeabi, create armeabi.


Libpwm. so is the library file we need in the Android Application project.

Static {System. loadLibrary ("pwm"); // Add the C/C ++ dynamic library import method}

Upload the libpwm. so project files from Ubuntu to windows. Open the project in eclipse and compile and generate the apk.

Pwm.apk

Install on the board and enter the following command on the control terminal:

Pm install-f pwm.apk

After the successful installation prompts success, open the software and click the pwm button.

The oscilloscope outputs a 50% Hz waveform and the test is successful.



Android driver development: I want to know how an application calls the underlying hardware driver, JNI, and hardware driver module. The specific process (PATH)

Same request, fjbperfect@gmail.com
 
JNI development and mobile Drive Development

It depends on whether your jni directly calls other people's things or implements it by yourself.

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.