Android系统移植(六)

一、APK源码示例

/*
监听按钮LED ON  LED OFF 
按下的时候文本改变      
*/

package com.up;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;

public class BunflyActivity extends Activity
{
        TextView txt;
        Button btn_on,btn_off;
        OnClickListener listener;

        native int ledon();
        native int ledoff();
        static{
            System.loadLibrary("4412led");
            };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txt.setText("please client button");
        btn_on.setText("LED ON");
        btn_off.setText("LED OFF");

        listener = new OnClickListener(){
                public void onClick(View v){
                        if(v == btn_on){
                                int a = ledon();
                                if(-1 == a){
                                        txt.setText("open error");
                                }else if(-2 == a){
                                        txt.setText("ioctl error");
                                }else {
                                        txt.setText("Led On");}
                        }else if(v == btn_off){
                                int b = ledoff();
                                if(-1 == b){
                                        txt.setText("open error");
                                }else if(-2 == b){
                                        txt.setText("ioctl error");
                                }else {
                                        txt.setText("Led 0ff");}
                        }
                }
        };
        btn_on.setOnClickListener(listener);
        btn_off.setOnClickListener(listener);
    }
}

二、APK的Makefile

#APK调用动态库,再调用底层驱动

LOCAL_PATH      := $(call my-dir)

include $(CLEAR_VARS)
#路径比较长用 call
LOCAL_SRC_FILES := $(call all-subdir-java-files)

#编译出来的东西   bunfly.apk
LOCAL_PACKAGE_NAME :=  bunfly

#依赖的动态库
LOCAL_JAVA_SHARED_LIBRARIES :=  bunflyled

#编译成应用程序
include $(BUILD_PACKAGE)

三、JNI源码示例

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


int Java_com_up_BunflyActivity_ledon(JNIEnv *env,jclass thiz) {
        int fd = 0;
        fd = open("/dev/bunflyled",O_RDWR);
        if(fd < 0){
                return -1;
        }

        int rev = 0;

        rev = ioctl(fd,555);
        if(rev < 0)
        {
                return -2;
        }
        return 1;
}
int Java_com_up_BunflyActivity_ledoff(JNIEnv *env,jclass thiz) {
        int fd2 = 0;
        fd2 = open("/dev/bunflyled",O_RDWR);
        if(fd2 < 0){
                return -1;
        }
        int r = 0;
        r = ioctl(fd2,666);
        if(r < 0)
        {
                return -2;
        }

        return 1;
}

四、JNI的Makefile

LOCAL_PATH := $(call my-dir)

LOCAL_SRC_FILES := bunflyled.c

LOCAL_MODULE := lib4412led

LOCAL_SHARED_LIBRARIES := libc

include $(BUILD_SHARED_LIBRARY)

五、LED驱动程序示例

#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/thread_info.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/clk.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>

#define LED_ON  555 
#define LED_OFF 666
#define DEVICE_NAME "bunflyled"

MODULE_LICENSE("GPL");


void led_on();
void init_led();
void led_off();
int led_open(struct inode *no, struct file *fp);
long led_unlocked_ioctl(struct file *fp, unsigned int cmd, unsigned long args);

unsigned long led_init;
unsigned long *GPM4CON,*GPM4DAT;
struct cdev bunflyLed;
struct class *cdev_class;

struct file_operations op = {
        .open = led_open,
        .unlocked_ioctl = led_unlocked_ioctl,
};
dev_t devno;

int bunfly_init()
{
        int result;
        int err;

        result = alloc_chrdev_region(&devno,0,1,DEVICE_NAME);
        if(result){
                printk("alloc devno failure.\n");
                unregister_chrdev_region(devno,1);
                return result;
        }else{
                printk("alloc devno success.\n");
        }

        cdev_init(&bunflyLed,&op);

        err = cdev_add(&bunflyLed,devno,1);
        if(err){
                printk("add error.\n");
                unregister_chrdev_region(devno,1);
                return err;
        }
        cdev_class = class_create(THIS_MODULE, DEVICE_NAME);//动态创建设备结点
        device_create(cdev_class,NULL, devno, 0, DEVICE_NAME);

        printk("bunflyled driver init success.\n");

        return 0;
}

void bunfly_exit()
{
        device_destroy(cdev_class, devno);
        class_destroy(cdev_class);
        unregister_chrdev_region(devno,1);
        printk("bunflyled driver delete success.\n");
}


module_init(bunfly_init);
module_exit(bunfly_exit);


int led_open(struct inode *no, struct file *fp)
{
        printk("bunfly led driver open.\n ");

        return 0;
}
long led_unlocked_ioctl(struct file *fp, unsigned int cmd, unsigned long args)
{
        init_led();
        switch(cmd)
        {
                case LED_ON:
                        led_on();
                        break;
                case LED_OFF:
                        led_off();
                        break;
                default:
                        printk("unkown command\n");
                        break;
        }

        return 0;
}
void init_led()
{
        led_init = ioremap(0x11000000,SZ_4K);
        GPM4CON = led_init + 0x2e0;
        GPM4DAT = led_init + 0x2e4;

        *GPM4CON = 0x1111;
        *GPM4DAT = 0xf;
}

void led_on()
{
        *GPM4CON = 0x1111;
        *GPM4DAT = 0x0;
}
void led_off()
{
        *GPM4CON = 0x1111;
        *GPM4DAT = 0xf;
}

至此,Android系统移植项目完结。

总结:
1.项目的进度有点慢,LCD、触摸屏的驱动还在调试中
2.写了不少文档,发现越写越乱,文笔有待提高
3.通过该项目,深刻理解Android框架,熟悉了上层与底层的调用流程

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值