Android平台中振动器系统详解

       

Email:wei7758@126.com

Blog:http://blog.csdn.net/yinwei520

Author:Yww

Time:2011-9-24

Update:

(转载请注明出处,谢谢)

 

 

这里引用《Android系统级深入开发——移植与调试》一书中的解释,关于真正的vibrator代码分析,是以我项目中的代码为基础。

 

一:  振动器系统结构和移植内容

振动器负责控制引动电话的振动功能,Android中的振动器系统是一个专供这方面功能的小系统,提供根据时间振动的功能。

振动器系统包含了驱动程序、硬件抽象层、JNI部分、Java框架类等几个部分,也向Java应用程序层提供了简单的API作为平台接口。

Android振动器系统的基本层次结构如图23-1所示。

图23-1  Android振动器系统的基本层次结构

1  振动器部分的结构

Android振动器系统自下而上包含了驱动程序、振动器系统硬件抽象层、振动器系统Java框架类、Java框架中振动器系统使用等几个部分,其结构如图23-2所示。

图23-2  Android振动器系统的结构

自下而上,Android的振动器系统分成了以下部分。

(1)驱动程序:特定硬件平台振动器的驱动程序,通常基于Android的Timed Output驱动框架实现

(2)硬件抽象层

光系统硬件抽象层接口路径为:hardware/libhardware_legacy/include/hardware_legacy/ vibrator.h

振动器系统的硬件抽象层在Android中已经具有默认实现,代码路径:

hardware/libhardware_legacy/vibrator/vibrator.c

振动器的硬件抽象层通常并不需要重新实现,是libhardware_legacy.so的一部分。

(3)JNI部分

代码路径:frameworks/base/services/jni/com_android_server_VibratorService.cpp

这个类是振动器的JNI部分,通过调用硬件抽象层向上层提供接口。

(4)Java部分

代码路径:

frameworks/base/services/java/com/android/server/VibratorService.java

frameworks/base/core/java/android/os/Vibrator.java

VibratorService.java通过调用,VibratorService JNI来实现com.android.server包中的VibratorService类。这个类不是平台的API,被Android系统Java框架中的一小部分调用。

Vibrator.java文件实现了android.os包中的Vibrator类,这是向Java层提供的API。

2  移植内容

针对特定的硬件平台,振动器系统的移植有两种方法。

 第一种方法(通常情况):由于已经具有硬件抽象层,振动器系统的移植只需要实现驱动程序即可。这个驱动程序需要基于Android内核中的Timed Output驱动框架。

 第二种方法:根据自己实现的驱动程序,重新实现振动器的硬件抽象层定义接口(需要在libhardware_legacy.so库中),由于振动器硬件抽象层的接口非常简单,因此这种实现方式也不会很复杂。

二:  移植与调试的要点    

1  驱动程序

Vibrator的驱动程序只需要实现振动的接口即可,这是一个输出设备,需要接受振动时间作为参数。由于比较简单,因此Vibrator的驱动程序可以使用多种方式来实现。

在Android中,推荐基于Android内核定义Timed Output驱动程序框架来实现Vibrator的驱动程序。

Timed Output的含义为定时输出,用于定时发出某个输出。实际上,这种驱动程序依然是基于sys文件系统来完成的。

drivers/staging/android/目录timed_output.h中定义timed_output_dev结构体,其中包含enable和get_time这两个函数指针,实现结构体后,使用timed_output_dev_register()和timed_output_dev_unregister()函数注册和注销即可。

Timed Output驱动程序框架将为每个设备在/sys/class/timed_output/目录中建立一个子目录,设备子目录中的enable文件就是设备的控制文件。读enable文件表示获得剩余时间,写这个文件表示根据时间振动。

Timed Output驱动的设备调试,通过sys文件系统即可。

对于Vibrator设备,其实现的Timed Output驱动程序的名称应该为“vibrator”。因此Vibrator设备在sys文件系统中的方法如下所示:

# echo "10000" > /sys/class/timed_output/vibrator/enable

# cat /sys/class/timed_output/vibrator/enable

3290

# echo "0"  > /sys/class/timed_output/vibrator/enable

对于enable文件,“写”表示使能指定的时间,“读”表示获取剩余时间。

2  硬件抽象层的内容

2.1 硬件抽象层的接口

Vibrator硬件抽象层的接口在hardware/libhardware_legacy/include/hardware_legacy/目录的vibrator.h文件中定义:

int vibrator_on(int timeout_ms);         // 开始振动

int vibrator_off();                         // 关闭振动

vibrator.h文件中定义两个接口,分别表示振动和关闭,振动开始以毫秒(ms)作为时间单位。

 提示:Timed Output类型驱动本身有获得剩余时间的能力(读enable文件),但是在Android Vibrator硬件抽象层以上的各层接口都没有使用这个功能。

2.2 标准硬件抽象层的实现

Vibrator硬件抽象层具有标准的实现,在hardware/libhardware_legacy/vibrator/目录的vibrator.c中。

其中实现的核心内容为sendit()函数,这个函数的内容如下所示:

#define THE_DEVICE "/sys/class/timed_output/vibrator/enable"

static int sendit(int timeout_ms)

{

    int nwr, ret, fd;

    char value[20];

#ifdef QEMU_HARDWARE                // 使用QEMU的情况

    if (qemu_check()) {

        return qemu_control_command( "vibrator:%d", timeout_ms );

    }

#endif

    fd = open(THE_DEVICE, O_RDWR);               // 读取sys文件系统中的内容

    if(fd < 0) return errno;

    nwr = sprintf(value, "%d\n", timeout_ms);

    ret = write(fd, value, nwr);

    close(fd);

    return (ret == nwr) ? 0 : -1;

}



 

sendit()函数负责根据时间“振动”:在真实的硬件中,通过sys文件系统的文件进行控制;如果是模拟器环境则通过QEMU发送命令。

vibrator_on()调用sendit()以时间作为参数,vibrator_on()调用sendit()以0作为参数。

 

上层的情况和注意事项

  frameworks/base/services/jni/目录中的com_android_server_VibratorService.cpp文件是Vibrator硬件抽象层的调用者,它同时也向Java提供JNI支持。

  其中,为JNI定义的方法列表如下所示:

  

 static JNINativeMethod method_table[] = {

  { "vibratorOn", "(J)V", (void*)vibratorOn }, // 振动器开

  { "vibratorOff", "()V", (void*)vibratorOff } // 振动器关

  };

  int register_android_server_VibratorService(JNIEnv *env) {

  return jniRegisterNativeMethods(env, "com/android/server/VibratorService",

  method_table, NELEM(method_table));

  }
  //vibratorOn()和vibratorOff()这两个函数的实现分别如下所示:

   

  static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms){

  vibrator_on(timeout_ms);

  }

  static void vibratorOff(JNIEnv *env, jobject clazz){

  vibrator_off();

  }


 

        frameworks/base/services/java/com/android/server/目录中的VibratorService.java通过调用VibratorService JNI来实现com.android.server包中的VibratorService类。

  frameworks/base/core/java/android/os/目录中的Vibrator.java文件实现了android.os包中的Vibrator类。它通过调用vibrator的Java服务来实现(获得名称为vibrator的服务),配合同目录中的IVibratorService.aidl文件向应用程序层提供Vibrator的相关API。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

我具体的驱动程序

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/workqueue.h>

#include "timed_output.h"
#include <linux/hrtimer.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>

#include <linux/jiffies.h>
#include <linux/timer.h>

#include <mach/mt_typedefs.h>
#include <mach/mt_pll.h>
#include <mach/mt_gpt.h>

#define VERSION					        "v 0.1"
#define VIB_DEVICE	        			"mt_vibrator"

#define COUNT_DOWN_TIME					50

#define VIBR_HRTIMER

#ifndef VIBR_HRTIMER
XGPT_NUM Vibrator_XGPT = XGPT7;
#endif

/******************************************************************************
Error Code No.
******************************************************************************/
#define RSUCCESS        0

/******************************************************************************
Debug Message Settings
******************************************************************************/

/* Debug message event */
#define DBG_EVT_NONE		0x00000000	/* No event */
#define DBG_EVT_INT			0x00000001	/* Interrupt related event */
#define DBG_EVT_TASKLET		0x00000002	/* Tasklet related event */

#define DBG_EVT_ALL			0xffffffff
 
#define DBG_EVT_MASK      	(DBG_EVT_TASKLET)

#if 1
#define MSG(evt, fmt, args...) \
do {	\
	if ((DBG_EVT_##evt) & DBG_EVT_MASK) { \
		printk(fmt, ##args); \
	} \
} while(0)

#define MSG_FUNC_ENTRY(f)	MSG(FUC, "<FUN_ENT>: %s\n", __FUNCTION__)
#else
#define MSG(evt, fmt, args...) do{}while(0)
#define MSG_FUNC_ENTRY(f)	   do{}while(0)
#endif

#define VIBR_CON0 ((volatile unsigned long*)(0xF702F7B0))

static int vibr_Enable(void)
{
    printk("[vibrator]vibr_Enable \n");
	hwPowerOn(MT_POWER_LDO_VIBR, VOL_2800 , "VIBR");
	return 0;
}

static int vibr_Disable(void)
{
    while((INREG32(VIBR_CON0)&1))
    {
        printk("[vibrator]vibr_Disable \n");
	hwPowerDown(MT_POWER_LDO_VIBR , "VIBR");
        //printk("[vibrator]vibr_Disable:VIBR_CON0=0x%x \r\n", INREG32(VIBR_CON0));
    }
	
	return 0;
}


/******************************************************************************
Global Definations
******************************************************************************/
//static struct work_struct vibrator_work;
static struct hrtimer vibe_timer;
static spinlock_t vibe_lock;


static int vibrator_get_time(struct timed_output_dev *dev)
{
	if (hrtimer_active(&vibe_timer)) 
	{
		ktime_t r = hrtimer_get_remaining(&vibe_timer);
		return r.tv.sec * 1000 + r.tv.nsec / 1000000;
	} 
	else
		return 0;
}

static void vibrator_enable(struct timed_output_dev *dev, int value)
{
        unsigned long   flags;

	spin_lock_irqsave(&vibe_lock, flags);

        #ifdef VIBR_HRTIMER
	while(hrtimer_cancel(&vibe_timer))
        {
            printk("[vibrator]vibrator_enable: try to cancel hrtimer \n");
        }
	#else
        XGPT_Reset(Vibrator_XGPT);
        #endif

	if (value == 0)
        {      
            printk("[vibrator]vibrator_enable: disable \n");
            vibr_Disable();
          
        }
	else 
	{
			value = ((value > 15000) ? 15000 : value);
            printk("[vibrator]vibrator_enable: vibrator start: %d \n", value); 
         
            #ifdef VIBR_HRTIMER
	    hrtimer_start(&vibe_timer, 
			ktime_set(value / 1000, (value % 1000) * 1000000),
			HRTIMER_MODE_REL);

            #else
            XGPT_CONFIG config;
			config.num = Vibrator_XGPT;
            config.clkDiv = 0;
            config.mode = XGPT_ONE_SHOT;
            config.bIrqEnable = TRUE;
            config.u4Compare = value*32768/1000;
            
            if(!XGPT_Config(config))
            {
                printk("[vibrator]vibrator_enable: config XGPT: %d fail!\n", value); 
            }
            
            XGPT_Start(Vibrator_XGPT);
            #endif
           
            vibr_Enable();
           
	}
	spin_unlock_irqrestore(&vibe_lock, flags);

}


#ifdef VIBR_HRTIMER
static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
{
     printk("[vibrator]vibrator_timer_func: vibrator will disable \n");

    vibr_Disable();
            
 	return HRTIMER_NORESTART;
}

#else
void vibrator_timer_func(UINT16 temp)
{
    printk("[vibrator]vibrator_timer_func: vibrator will disable \n");

    vibr_Disable();
}
#endif


static struct timed_output_dev mt_vibrator = 
{
	.name = "vibrator",
	.get_time = vibrator_get_time,
	.enable = vibrator_enable,
};

static int vib_probe(struct platform_device *pdev)
{
	return 0;
}

static int vib_remove(struct platform_device *pdev)
{
	return 0;
}

static void vib_shutdown(struct platform_device *pdev)
{
		vibr_Disable();

}
/******************************************************************************
Device driver structure
*****************************************************************************/
static struct platform_driver vibrator_driver = 
{
    .probe		= vib_probe,
	.remove	    = vib_remove,
    .shutdown = vib_shutdown,
    .driver     = {
    .name = VIB_DEVICE,
    },
};

static ssize_t store_vibr_on(struct device *dev,struct device_attribute *attr, const char *buf, size_t size)
{
	if(buf != NULL && size != 0)
	{
		printk("[vibrator]buf is %s and size is %d \n",buf,size);
		if(buf[0]== '0')
		{
			vibr_Disable();
		}else
		{
			vibr_Enable();
		}
	}
	return size;
}

static DEVICE_ATTR(vibr_on, 0664, NULL, store_vibr_on);

/******************************************************************************
 * vib_mod_init
 * 
 * DESCRIPTION:
 *   Register the vibrator device driver ! 
 * 
 * PARAMETERS: 
 *   None
 * 
 * RETURNS: 
 *   None
 * 
 * NOTES: 
 *   RSUCCESS : Success
 * 
 ******************************************************************************/

static s32 __devinit vib_mod_init(void)
{	
	s32 ret;

	printk("Mk MT vibrator driver register, version %s\n", VERSION);

	spin_lock_init(&vibe_lock);
    
        #ifdef VIBR_HRTIMER
	hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	vibe_timer.function = vibrator_timer_func;

        #else
        XGPT_Init(Vibrator_XGPT, vibrator_timer_func);
        #endif
    
	timed_output_dev_register(&mt_vibrator);

        ret = platform_driver_register(&vibrator_driver);

        if(ret) 
        {
		printk("[vibrator]Unable to register vibrator driver (%d)\n", ret);
		return ret;
        }	

	ret = device_create_file(mt_vibrator.dev,&dev_attr_vibr_on);
    if(ret)
    {
        printk("[vibrator]device_create_file vibr_on fail! \n");
    }
    
	printk("[vibrator]vib_mod_init Done \n");
 
    return RSUCCESS;
}

/******************************************************************************
 * vib_mod_exit
 * 
 * DESCRIPTION: 
 *   Free the device driver ! 
 * 
 * PARAMETERS: 
 *   None
 * 
 * RETURNS: 
 *   None
 * 
 * NOTES: 
 *   None
 * 
 ******************************************************************************/
 
static void __exit vib_mod_exit(void)
{
	printk("Mk MT vibrator driver unregister, version %s \n", VERSION);
	printk("[vibrator]vib_mod_exit Done \n");
}

module_init(vib_mod_init);
module_exit(vib_mod_exit);
MODULE_AUTHOR("Mk Inc.");
MODULE_DESCRIPTION("MT Vibrator Driver (VIB)");
MODULE_LICENSE("GPL");


 

 

 

 



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值