TQ210搭载Android4.0.3系统构建之ADC从驱动到HAL到JNI到应用程序(HAL篇)
其实ADC的HAL层和BEEP、LED的就HAL层很像,所以注释就很少了,详情见BEEP、LED的HAL层
直接上源码吧
adc_under_hal.h
#ifndef __ADC_UNDER_H
#define __ADC_UNDER_H
#include <hardware/hardware.h>
#include <stdint.h>
#include <sys/cdefs.h>
__BEGIN_DECLS //采用C语言的方式编译和连接变量与函数
#define ADC_UNDER_ID "adc_unders"
static struct adc_hw_module_t
{
struct hw_module_t common;
};
static struct adc_hw_device_t
{
struct hw_device_t common;
int (*read)(struct adc_hw_device_t *device);
};
__END_DECLS
#endif
adc_under_hal.c
#include <hardware/hardware.h>
#include <hardware/adc_under_hal.h>
#include <android/log.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#define DEVICE_NAME "/dev/adc_unders" //设备文件
int fd=-1;
int adc_open() //打开设备文件
{
fd=open(DEVICE_NAME,O_RDWR);
if(fd<0)
{
__android_log_print(ANDROID_LOG_DEBUG, "msg", "can not open fd,fd=%d.\n",fd);
return -1;
}
__android_log_print(ANDROID_LOG_DEBUG,"msg", "open fd success,fd=%d\n",fd);
return 0;
}
int adc_read(struct adc_hw_device_t *dev) //读取adc数据
{
int data=0,ret;
if(fd==-1) return -1;
ret=read(fd,&data,sizeof(&data));
if(ret<0) __android_log_print(ANDROID_LOG_DEBUG,"msg", "can not read data from kernel.\n");
return data;
}
int adc_close(struct hw_device_t *dev)
{
if(fd!=-1)
{
close(fd);
if(dev) free(dev);
}
return 0;
}
int adc_init(struct hw_module_t *module,const char *id,struct hw_device_t **device) //adc的初始化函数,将设备带回给device
{
struct adc_hw_device_t *dev;
dev=(struct adc_hw_device_t *)malloc(sizeof(struct adc_hw_device_t));
if(dev==NULL)
{
__android_log_print(ANDROID_LOG_DEBUG, "msg", "can not alloc mem.\n");
return -1;
}
memset(dev,0,sizeof(*dev));
dev->common.tag=HARDWARE_DEVICE_TAG; //初始化设备信息
dev->common.version=1;
dev->common.module=module;
dev->common.close=(int (*)(struct hw_device_t *))adc_close;
*device=(struct hw_device_t *)&dev->common;
dev->read=adc_read;
if(adc_open()==-1) //打开设备文件
{
free(dev);
dev=NULL;
return -1;
}
return 0;
}
static struct hw_module_methods_t adc_module_methods_t=
{
open:adc_init
};
const struct adc_hw_module_t HAL_MODULE_INFO_SYM= //模块的入口,HMI符号
{
common:
{
tag:HARDWARE_MODULE_TAG,
version_major:1,
version_minor:0,
id:ADC_UNDER_ID, //组成模块名称的ID
name:"adc stub",
author:"undergrowth",
methods:&adc_module_methods_t,
}
};
编译文件 Android.mk
LOCAL_PATH :=$(call my-dir) include $(CLEAR_VARS) LOCAL_PRELINK_MODULE :=false LOCAL_SHARED_LIBRARIES :=libutils LOCAL_SRC_FILES :=adc_under_hal.c LOCAL_MODULE :=adc_unders.$(TARGET_BOARD_PLATFORM) LOCAL_MODULE_PATH :=$(LOCAL_PATH) LOCAL_MODULE_TAGS :=optional include $(BUILD_SHARED_LIBRARY)
posted on 2013-07-01 23:00 liangxinzhi 阅读(169) 评论(0) 收藏 举报
浙公网安备 33010602011771号