andoid电阻触摸移植

Posted jhcelue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了andoid电阻触摸移植相关的知识,希望对你有一定的参考价值。

这里我使用的是210的开发板 系统android4.0.4 内核linux3.0.8

要用电阻屏一般都是使用tslib进行校准的 这里给个我在android上用的tslib

下载地址 http://download.csdn.net/detail/hclydao/8074069

直接把这个放在android源代码external文件夹下就可以 有几点须要注意的

tslib里我改了下Android.mk 曾经用的时候应该是模块名input被用了 所以我改成了input1

include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE:= input1
LOCAL_PRELINK_MODULE := false
LOCAL_CFLAGS = $(CFLAGS)
LOCAL_C_INCLUDES = $(C_INCLUDES)
LOCAL_SRC_FILES =  plugins/input-raw.c
LOCAL_SHARED_LIBRARIES += libts
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/ts
include $(BUILD_SHARED_LIBRARY)
所以对应的ts.conf里也要改 我的ts.conf内容例如以下


# Uncomment if you wish to use the linux input layer event interface
module_raw input1

# Uncomment if you‘re using a Sharp Zaurus SL-5500/SL-5000d
# module_raw collie

# Uncomment if you‘re using a Sharp Zaurus SL-C700/C750/C760/C860
# module_raw corgi

# Uncomment if you‘re using a device with a UCB1200/1300/1400 TS interface
# module_raw ucb1x00

# Uncomment if you‘re using an HP iPaq h3600 or similar
# module_raw h3600

# Uncomment if you‘re using a Hitachi Webpad
# module_raw mk712

# Uncomment if you‘re using an IBM Arctic II
# module_raw arctic2

module pthres pmin=1
module variance delta=30
module dejitter delta=100
module linear
直接放在system/etc/下就能够了 注意上面的 
module_raw input1

后面的input1是和上面模块名是一样的

ts.conf最好是系统编译完毕后 在拷贝过去 或者能够直接改tslib下的 然后制作文件系统的时候还须要添加对应的几个节点 例如以下

	mkdir rootfs_dir/dev/input
	mkdir rootfs_dir/dev/graphics
	mknod rootfs_dir/dev/graphics/fb0 c 29 0
	mknod rootfs_dir/dev/input/event1  c 13 65
由于我把校准放在了init之前 基本上设备节点都没有 所以这里我自己创建 我的电阻触摸驱动是event1所以这里要加上

然后是calibrate脚本

#!/bin/sh
export TSLIB_TSDEVICE=/dev/input/event1
export TSLIB_CALIBFILE=/system/etc/pointercal
export TSLIB_CONFFILE=/system/etc/ts.conf
export TSLIB_CONSOLEDEVICE=none
export TSLIB_FBDEVICE=/dev/graphics/fb0
export TSLIB_PLUGINDIR=/system/lib/ts
exec /sbin/ts_calibrate
这里的TSDEVICE和FBDEVICE详细依据你自己的改

然后就是校准后的数据回写到驱动里 校准完毕后在运行这个

/system/bin/cat /etc/pointercal > /proc/driver/micc_ts
后面的/proc/driver/micc_ts依据驱动里来的 这个你能够开个服务运行就能够了

然后就是加个idc这个直接拷贝一个 名字改成你的驱动里的名字即可了 我这里是s3c-ts

文件系统改动基本都是这样了 以下上驱动 

/* linux/drivers/input/touchscreen/s3c-ts.c
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Copyright (c) 2004 Arnaud Patard <[email protected]>
 * iPAQ H1940 touchscreen support
 *
 * ChangeLog
 *
 * 2004-09-05: Herbert Potzl <[email protected]>
 *	- added clock (de-)allocation code
 *
 * 2005-03-06: Arnaud Patard <[email protected]>
 *      - h1940_ -> s3c24xx (this driver is now also used on the n30
 *        machines :P)
 *      - Debug messages are now enabled with the config option
 *        TOUCHSCREEN_S3C_DEBUG
 *      - Changed the way the value are read
 *      - Input subsystem should now work
 *      - Use ioremap and readl/writel
 *
 * 2005-03-23: Arnaud Patard <[email protected]>
 *      - Make use of some undocumented features of the touchscreen
 *        controller
 *
 * 2006-09-05: Ryu Euiyoul <[email protected]>
 *      - added power management suspend and resume code
 *
 */

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/init.h>
#include <linux/serio.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif
#include <linux/io.h>
#include <asm/irq.h>
#include <mach/hardware.h>

#include <mach/regs-adc.h>
#include <mach/ts-s3c.h>
#include <mach/irqs.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>

/* For ts->dev.id.version */
#define S3C_TSVERSION	0x0101

#define WAIT4INT(x)	(((x)<<8) | 			S3C_ADCTSC_YM_SEN | S3C_ADCTSC_YP_SEN | 			S3C_ADCTSC_XP_SEN | S3C_ADCTSC_XY_PST(3))

#define AUTOPST	(S3C_ADCTSC_YM_SEN | S3C_ADCTSC_YP_SEN | 		 S3C_ADCTSC_XP_SEN | S3C_ADCTSC_AUTO_PST | 		 S3C_ADCTSC_XY_PST(0))

#ifdef CONFIG_HAS_EARLYSUSPEND
static void ts_early_suspend(struct early_suspend *h);
static void ts_late_resume(struct early_suspend *h);
#endif

/* Touchscreen default configuration */
struct s3c_ts_mach_info s3c_ts_default_cfg __initdata = {
	.delay			= 10000,
	.presc			= 49,
	.oversampling_shift	= 2,
	.resol_bit		= 10
};

/*
 * Definitions & global arrays.
 */
static char *s3c_ts_name = "s3c-ts";
static void __iomem		*ts_base;
static struct resource		*ts_mem;
static struct resource		*ts_irq;
static struct clk		*ts_clock;
static struct s3c_ts_info	*ts;

static struct proc_dir_entry *ts_proc_entry;

signed long pointercal[7] = {1,0,0,0,1,0,1};

static int SCREEN_X=800;
static int SCREEN_Y=480;

#define  TSNUM  8
static   int  ts_x[TSNUM];
static   int  ts_y[TSNUM];
//上层回写保存上层tslib校准后的数据
static int ts_proc_write(struct file *file, const char *buffer,
			     unsigned long count, void *data)
{
	char buf[256];
	unsigned long len;
	char *p = (char *)buf;
	int i,j;
	int flag = 1;
	memset(buf,0,256);
	len = min_t(unsigned long, sizeof(buf) - 1, count);

	if (copy_from_user(buf, buffer, len))
		return count;

	j = 0;
	for(i=0;i<len;i++)
	{
		if(flag)
		{
			pointercal[j] = simple_strtol(p, &p, 10);
			j++; 
			if(j>=7)
				break;
			flag = 0;
		}

		if(p[0] == ‘ ‘)
		{
			flag = 1;
		}
		if(p[0] == ‘\t‘)
		{
			flag = 1;
		}
		p++;
	}
	return count;
}

static void data_select() {
}

static void touch_timer_fire(unsigned long data)
{
	unsigned long data0;
	unsigned long data1;
	int updown;
	int x, y;
	long a,b,c,d,e,f,div;
	int i,j,tmp;

	data0 = readl(ts_base+S3C_ADCDAT0);
	data1 = readl(ts_base+S3C_ADCDAT1);

	updown = (!(data0 & S3C_ADCDAT0_UPDOWN)) &&
		 (!(data1 & S3C_ADCDAT1_UPDOWN));

	if (updown) {
		if (ts->count) {
			a = pointercal[0];
			b = pointercal[1];
			c = pointercal[2];
			d = pointercal[3];
			e = pointercal[4];
			f = pointercal[5];
			div = pointercal[6];
			for(j=(TSNUM-1);j>0;j--)
			  for(i = 0; i < j;i++)
			  {	
			      if (ts_x[i] < ts_x[i+1])
			      {
					tmp = ts_x[i+1];
					ts_x[i+1]  = ts_x[i];
					ts_x[i] = tmp;
			      }
			     if (ts_y[i] < ts_y[i+1])
			      {
					tmp = ts_y[i+1];
					ts_y[i+1]  = ts_y[i];
					ts_y[i] = tmp;
			      }
	
			 }  

			tmp = 0;
			for (i=TSNUM/4;i<(TSNUM*3/4);i++)
			   tmp+=ts_x[i];
			ts->xp = tmp*2/TSNUM;

			tmp = 0;
			for (i=TSNUM/4;i<(TSNUM*3/4);i++)
			   tmp+=ts_y[i];
			ts->yp  = tmp*2/TSNUM;
                        //校准后上报
			x = (a*ts->xp + b*ts->yp + c)/div;
			y = (d*ts->xp + e*ts->yp + f)/div;
			input_report_abs(ts->dev, ABS_X, x);
			input_report_abs(ts->dev, ABS_Y, y);

			input_report_key(ts->dev, BTN_TOUCH, 1);
			input_report_abs(ts->dev, ABS_PRESSURE, 1);//这个地方要注意前面是abs
			input_sync(ts->dev);
		}

		ts->xp = 0;
		ts->yp = 0;
		ts->count = 0;

		writel(S3C_ADCTSC_PULL_UP_DISABLE | AUTOPST,
				ts_base + S3C_ADCTSC);
		writel(readl(ts_base+S3C_ADCCON) | S3C_ADCCON_ENABLE_START,
				ts_base + S3C_ADCCON);
	} else {
		ts->count = 0;
		input_report_key(ts->dev, BTN_TOUCH, 0);
		input_report_abs(ts->dev, ABS_PRESSURE, 0);
		input_sync(ts->dev);
		writel(WAIT4INT(0), ts_base+S3C_ADCTSC);
	}
}

static struct timer_list touch_timer =
		TIMER_INITIALIZER(touch_timer_fire, 0, 0);

static irqreturn_t stylus_updown(int irqno, void *param)
{
	unsigned long data0;
	unsigned long data1;
	int updown;

	data0 = readl(ts_base + S3C_ADCDAT0);
	data1 = readl(ts_base + S3C_ADCDAT1);
	updown = (!(data0 & S3C_ADCDAT0_UPDOWN)) &&
		 (!(data1 & S3C_ADCDAT1_UPDOWN));

	/* TODO we should never get an interrupt with updown set while
	 * the timer is running, but maybe we ought to verify that the
	 * timer isn‘t running anyways. */

	if (updown)
		touch_timer_fire(0);

	if (ts->s3c_adc_con == ADC_TYPE_2) {
		__raw_writel(0x0, ts_base + S3C_ADCCLRWK);
		__raw_writel(0x0, ts_base + S3C_ADCCLRINT);
	}

	return IRQ_HANDLED;
}

static irqreturn_t stylus_action(int irqno, void *param)
{
	unsigned long data0;
	unsigned long data1;

	data0 = readl(ts_base + S3C_ADCDAT0);
	data1 = readl(ts_base + S3C_ADCDAT1);
	if (ts->resol_bit == 12) {
		ts_x[ts->count] =  (data0 & S3C_ADCDAT0_XPDATA_MASK_12BIT);
		ts_y[ts->count] =  data1 & S3C_ADCDAT1_YPDATA_MASK_12BIT;
	} else {
		ts_x[ts->count] = S3C_ADCDAT0_XPDATA_MASK_12BIT - (data0 & S3C_ADCDAT0_XPDATA_MASK_12BIT);
		ts_y[ts->count] = S3C_ADCDAT1_YPDATA_MASK_12BIT - (data1 & S3C_ADCDAT1_YPDATA_MASK_12BIT);
	}

	ts->count++;

	if (ts->count < TSNUM) {
		writel(S3C_ADCTSC_PULL_UP_DISABLE | AUTOPST,
				ts_base + S3C_ADCTSC);
		writel(readl(ts_base + S3C_ADCCON) | S3C_ADCCON_ENABLE_START,
				ts_base + S3C_ADCCON);
	} else {
		mod_timer(&touch_timer, jiffies + 1);
		writel(WAIT4INT(1), ts_base + S3C_ADCTSC);
	}

	if (ts->s3c_adc_con == ADC_TYPE_2) {
		__raw_writel(0x0, ts_base+S3C_ADCCLRWK);
		__raw_writel(0x0, ts_base+S3C_ADCCLRINT);
	}
	return IRQ_HANDLED;
}

static struct s3c_ts_mach_info *s3c_ts_get_platdata(struct device *dev)
{
	if (dev->platform_data != NULL)
		return (struct s3c_ts_mach_info *)dev->platform_data;

	return &s3c_ts_default_cfg;
}

/*
 * The functions for inserting/removing us as a module.
 */
static int __init s3c_ts_probe(struct platform_device *pdev)
{
	struct resource *res;
	struct device *dev;
	struct input_dev *input_dev;
	struct s3c_ts_mach_info *s3c_ts_cfg;
	int ret, size;
	int irq_flags = 0;
#if defined(CONFIG_FB_S3C_AT070)
		SCREEN_X = 800;
		SCREEN_Y = 480;
#endif
	dev = &pdev->dev;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res == NULL) {
		dev_err(dev, "no memory resource specified\n");
		return -ENOENT;
	}

	size = (res->end - res->start) + 1;
	ts_mem = request_mem_region(res->start, size, pdev->name);
	if (ts_mem == NULL) {
		dev_err(dev, "failed to get memory region\n");
		ret = -ENOENT;
		goto err_req;
	}

	ts_base = ioremap(res->start, size);
	if (ts_base == NULL) {
		dev_err(dev, "failed to ioremap() region\n");
		ret = -EINVAL;
		goto err_map;
	}

	ts_clock = clk_get(&pdev->dev, "adc");
	if (IS_ERR(ts_clock)) {
		dev_err(dev, "failed to find watchdog clock source\n");
		ret = PTR_ERR(ts_clock);
		goto err_clk;
	}

	clk_enable(ts_clock);

	s3c_ts_cfg = s3c_ts_get_platdata(&pdev->dev);
	if ((s3c_ts_cfg->presc & 0xff) > 0)
		writel(S3C_ADCCON_PRSCEN |
				S3C_ADCCON_PRSCVL(s3c_ts_cfg->presc & 0xFF),
				ts_base+S3C_ADCCON);
	else
		writel(0, ts_base + S3C_ADCCON);

	/* Initialise registers */
	if ((s3c_ts_cfg->delay & 0xffff) > 0)
		writel(s3c_ts_cfg->delay & 0xffff, ts_base + S3C_ADCDLY);

	if (s3c_ts_cfg->resol_bit == 12) {
		switch (s3c_ts_cfg->s3c_adc_con) {
		case ADC_TYPE_2:
			writel(readl(ts_base + S3C_ADCCON) |
					S3C_ADCCON_RESSEL_12BIT,
					ts_base + S3C_ADCCON);
			break;

		case ADC_TYPE_1:
			writel(readl(ts_base + S3C_ADCCON) |
					S3C_ADCCON_RESSEL_12BIT_1,
					ts_base + S3C_ADCCON);
			break;

		default:
			dev_err(dev, "this type of AP isn‘t supported !\n");
			break;
		}
	}

	writel(WAIT4INT(0), ts_base + S3C_ADCTSC);

	ts = kzalloc(sizeof(struct s3c_ts_info), GFP_KERNEL);

	input_dev = input_allocate_device();
	if (!input_dev) {
		ret = -ENOMEM;
		goto err_alloc;
	}

	ts->dev = input_dev;

	ts->dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
	ts->dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);

	if (s3c_ts_cfg->resol_bit == 12) {
		input_set_abs_params(ts->dev,ABS_X, 0, SCREEN_X, 0, 0);
		input_set_abs_params(ts->dev,ABS_Y, 0, SCREEN_Y, 0, 0);
	} else {
		input_set_abs_params(ts->dev, ABS_X, 0, SCREEN_X, 0, 0);
		input_set_abs_params(ts->dev, ABS_Y, 0, SCREEN_Y, 0, 0);
	}

	input_set_abs_params(ts->dev, ABS_PRESSURE, 0, 1, 0, 0);

	sprintf(ts->phys, "input(ts)");

	ts->dev->name = s3c_ts_name;//文件系统里idc名字与这个要一致
	ts->dev->phys = ts->phys;
	ts->dev->id.bustype = BUS_RS232;
	ts->dev->id.vendor = 0xDEAD;
	ts->dev->id.product = 0xBEEF;
	ts->dev->id.version = S3C_TSVERSION;

	ts->shift = s3c_ts_cfg->oversampling_shift;
	ts->resol_bit = s3c_ts_cfg->resol_bit;
	ts->s3c_adc_con = s3c_ts_cfg->s3c_adc_con;

#ifdef CONFIG_HAS_EARLYSUSPEND
	ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
	ts->early_suspend.suspend = ts_early_suspend;
	ts->early_suspend.resume = ts_late_resume;
	register_early_suspend(&ts->early_suspend);
#endif

	/* For IRQ_PENDUP */
	ts_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (ts_irq == NULL) {
		dev_err(dev, "no irq resource specified\n");
		ret = -ENOENT;
		goto err_irq;
	}

	ret = request_irq(ts_irq->start, stylus_updown, irq_flags,
			"s3c_updown", ts);
	if (ret != 0) {
		dev_err(dev, "s3c_ts.c: Could not allocate ts IRQ_PENDN !\n");
		ret = -EIO;
		goto err_irq;
	}

	/* For IRQ_ADC */
	ts_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
	if (ts_irq == NULL) {
		dev_err(dev, "no irq resource specified\n");
		ret = -ENOENT;
		goto err_irq;
	}

	ret = request_irq(ts_irq->start, stylus_action, irq_flags,
			"s3c_action", ts);
	if (ret != 0) {
		dev_err(dev, "s3c_ts.c: Could not allocate ts IRQ_ADC !\n");
		ret =  -EIO;
		goto err_irq;
	}

	printk(KERN_INFO "%s got loaded successfully : %d bits\n",
			s3c_ts_name, s3c_ts_cfg->resol_bit);

	/* All went ok, so register to the input system */
	ret = input_register_device(ts->dev);
	if (ret) {
		dev_err(dev, "Could not register input device(touchscreen)!\n");
		ret = -EIO;
		goto fail;
	}

	ts_proc_entry = create_proc_entry("driver/micc_ts", 0, NULL); //创建上层节点  
	if (ts_proc_entry) {   
		ts_proc_entry->write_proc = ts_proc_write;   
	}  

	return 0;

fail:
	free_irq(ts_irq->start, ts->dev);
	free_irq(ts_irq->end, ts->dev);

err_irq:
	input_free_device(input_dev);
	kfree(ts);

err_alloc:
	clk_disable(ts_clock);
	clk_put(ts_clock);

err_clk:
	iounmap(ts_base);

err_map:
	release_resource(ts_mem);
	kfree(ts_mem);

err_req:
	return ret;
}

static int s3c_ts_remove(struct platform_device *dev)
{
	printk(KERN_INFO "s3c_ts_remove() of TS called !\n");

	disable_irq(IRQ_ADC);
	disable_irq(IRQ_PENDN);

	free_irq(IRQ_PENDN, ts->dev);
	free_irq(IRQ_ADC, ts->dev);

	if (ts_clock) {
		clk_disable(ts_clock);
		clk_put(ts_clock);
		ts_clock = NULL;
	}

#ifdef CONFIG_HAS_EARLYSUSPEND
     unregister_early_suspend(&ts->early_suspend);
#endif

	input_unregister_device(ts->dev);
	iounmap(ts_base);

	return 0;
}

#ifdef CONFIG_PM
static unsigned int adccon, adctsc, adcdly;

static int s3c_ts_suspend(struct platform_device *dev, pm_message_t state)
{
	adccon = readl(ts_base+S3C_ADCCON);
	adctsc = readl(ts_base+S3C_ADCTSC);
	adcdly = readl(ts_base+S3C_ADCDLY);

	disable_irq(IRQ_ADC);
	disable_irq(IRQ_PENDN);

	clk_disable(ts_clock);

	return 0;
}

static int s3c_ts_resume(struct platform_device *pdev)
{
	clk_enable(ts_clock);

	writel(adccon, ts_base+S3C_ADCCON);
	writel(adctsc, ts_base+S3C_ADCTSC);
	writel(adcdly, ts_base+S3C_ADCDLY);
	writel(WAIT4INT(0), ts_base+S3C_ADCTSC);

	enable_irq(IRQ_ADC);
	enable_irq(IRQ_PENDN);

	return 0;
}
#else
#define s3c_ts_suspend NULL
#define s3c_ts_resume  NULL
#endif

#ifdef CONFIG_HAS_EARLYSUSPEND
static void ts_early_suspend(struct early_suspend *h)
{
	struct s3c_ts_info *ts;
	ts = container_of(h, struct s3c_ts_info, early_suspend);
	s3c_ts_suspend(NULL, PMSG_SUSPEND);
}

static void ts_late_resume(struct early_suspend *h)
{
	struct s3c_ts_info *ts;
	ts = container_of(h, struct s3c_ts_info, early_suspend);
	s3c_ts_resume(NULL);
}
#endif

static struct platform_driver s3c_ts_driver = {
       .probe          = s3c_ts_probe,
       .remove         = s3c_ts_remove,
       .suspend        = s3c_ts_suspend,
       .resume         = s3c_ts_resume,
       .driver		= {
		.owner	= THIS_MODULE,
		.name	= "s3c-ts",
	},
};

static char banner[] __initdata = KERN_INFO 		"S5P Touchscreen driver, (c) 2008 Samsung Electronics\n";

static int __init s3c_ts_init(void)
{
	printk(banner);
	return platform_driver_register(&s3c_ts_driver);
}

static void __exit s3c_ts_exit(void)
{
	platform_driver_unregister(&s3c_ts_driver);
}

module_init(s3c_ts_init);
module_exit(s3c_ts_exit);

MODULE_AUTHOR("Samsung AP");
MODULE_DESCRIPTION("S5P touchscreen driver");
MODULE_LICENSE("GPL");
仅仅是在ADC驱动里加了一些东西 详细ADC驱动就不说了 大致的一些解释都写上面了 基本上就这个样了

改动完毕后 測试效果还是不错地.



以上是关于andoid电阻触摸移植的主要内容,如果未能解决你的问题,请参考以下文章

STemwin图形界面库移植与运用(基于STM32F103ZET6)

第3版emWin教程第5章 emWin上手之电阻触摸和电容触摸

第3版emWin教程第5章 emWin上手之电阻触摸和电容触摸

触摸屏实验

XPT2046电阻触摸屏芯片的使用

基于STM32的触摸屏学习笔记