Android驱动入门-Led控制+app+ndk库+底层驱动

硬件平台: FriendlyARM Tiny4412 Cortex-A9

操作系统: UBUNTU 14.04 LTS

时间:2016-09-20  21:56:48

本次实验使用的是 安卓APP + NDK库 + Linux底层驱动。

一、 首先在 Android Studio 上编写APP。

对软件进行布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.zws.test_led.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">
            <CheckBox
                android:id="@+id/checkbox_cmd_led1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Led1"/>  <!--id在判断复选框状态时使用-->

            <CheckBox
                android:id="@+id/checkbox_cmd_led2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Led2"/>

            <CheckBox
                android:id="@+id/checkbox_cmd_led3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Led3"/>

            <CheckBox
                android:id="@+id/checkbox_cmd_led4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Led4"/>
        </LinearLayout>
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="发送命令"
            android:onClick="on_click_cmd"/>    <!--按钮响应函数名称-->
    </LinearLayout>

</RelativeLayout>

其中, android:id="@+id/checkbox_cmd_led1"在程序中识别复选框的状态时使用。

     android:onClick="on_click_cmd"   on_click_cmd为在按下按钮时,调用的函数名称。

编写app程序。

package com.example.zws.test_led;

import android.app.Activity;        //可更改
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

import java.util.zip.CheckedInputStream;


public class MainActivity extends Activity {    //需和import android.app.Activity;一致

    private CheckBox[] cbCmdLeds  = new CheckBox[4];    //创建按钮数组
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cbCmdLeds[0] = (CheckBox)findViewById(R.id.checkbox_cmd_led1);  //需要强制转化
        cbCmdLeds[1] = (CheckBox)findViewById(R.id.checkbox_cmd_led2);
        cbCmdLeds[2] = (CheckBox)findViewById(R.id.checkbox_cmd_led3);
        cbCmdLeds[3] = (CheckBox)findViewById(R.id.checkbox_cmd_led4);
    }

    public void on_click_cmd( View view )
    {
        for( int i=0; i<4; i++ )
        {
            if( cbCmdLeds[i].isChecked() )   //如复选框被选中
                CmdLeds( 1,i ); //此函数在下面有声明。
            else
                CmdLeds( 0,i );
        }
    }

    public native void CmdLeds( int cmd, int arg ); //需要调用的外部函数可自己设定

    static
    {
        System.loadLibrary("ndk_test_myled");       //外部库的名称,可自己设定
    }
}

编写完成后需要在工程的app/src/main目录下创建目录jniLibs.这里将放ndk生成的库文件。

然后在Build->make project.

二、编写底层驱动。

#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    //io口控制寄存器硬件地址
#define GPM4DAT 0X110002E4    //io口数据寄存器硬件地址

#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 初始化cdev结构
    alloc_chrdev_region( &dev_no, 0, 1, "my_led" );
    cdev_init( &dev, &led_fops );    
    dev.owner = THIS_MODULE;
    //1.3 注册cdev结构
    cdev_add( &dev, dev_no, 1 );
    //2.硬件初始化
    hw_init();
    //3.创建设备文件
    led_class = class_create(THIS_MODULE,"my_led");                //创建设备类
    device_create( led_class, NULL, dev_no,NULL,"%s","my_led");  // 创建设备文件 my_led

    printk("init led device ok!
");

    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 );

编写Makefile。

obj-m := led.o
KDIR := /home/share/linux-3.0.86  //内核地址

all:
    make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=arm-linux- ARCH=arm
clean:
    rm -f *.ko *.o

然后make即可。将生成led.ko文件。

通过adb将文件穿发送到安卓设备。这里将ko文件发送到安卓设备的/data/local目录下。

adb push led.ko  /data/local/  

三、 NDK程序库编写。

首先生成之前app中写的接口头文件。

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是安卓sdk中的地址。

/home/my_Android/led/NDK/NDK_APP/app/build/intermediates/classes/debug/ 是相应安卓app源文件工程中的地址。

com.android.jack.ndk.happy.MainActivity为安卓的项目名称。

运行命令后,会在目录中生成jni文件夹。其中com_android_jack_ndk_happy_MainActivity.h为我们需要的头文件

创建ndk_led.c文件,编写相应的接口。

#include "com_example_zws_test_led_MainActivity.h"  //生成的头文件

#include <jni.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

#define LED_ON _IOW('G',0,int)
#define LED_OFF _IOW('G',1,int)

JNIEXPORT void JNICALL Java_com_example_zws_test_1led_MainActivity_CmdLeds
  (JNIEnv *env, jobject this, jint cmd, jint arg)    //头文件中的接口函数  此函数名称为系统自动生成的  勿修改。
{
    int fd;
    int tmp_cmd;
    
    fd = open("/dev/my_led",O_WRONLY);        //此为在写驱动程序时,生成的设备文件名称

    if( cmd == 1 )
        tmp_cmd = LED_ON;
    else if(cmd==0)
        tmp_cmd = LED_OFF;

    ioctl(fd,tmp_cmd,arg+1);
    close(fd);
}

创建编写 Android.mk 文件

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndk_test_myled    //这个是库的名称 可自己设定
LOCAL_SRC_FILES := ndk_led.c    //之前写的c文件名称
include $(BUILD_SHARED_LIBRARY)

编写完成后,回到本层目录的上一层。

执行命令生成ndk库。

ndk-build

即会生成 libs/armeabi/libndk_test_myled.so

将armeabi/libndk_test_myled.so复制到安卓app目录下的 app/src/mian/jinLibs中。

重新编译工程。

连接开发板的串口进入控制台。

执行一下命令获取root权限并安装内核模块。

$ su
$ insmod /data/local/led.ko
$ chmod 777 /dev/my_led

led.ko为在编写NDK时,生成的KO模块名称。

my_led为在设备安装后生成的设备文件名称,将其权限改为任何人均可访问。

在studio上点击run app ,让app在tiny4412上运行即可。



原文地址:https://www.cnblogs.com/ynxf/p/5890417.html