移植ads7846驅動到tiny6410(tiny6410)(ads7846)(移植)(觸摸屏)(抖動)

目前,市面上很多6410開發板都存在觸摸屏抖動的問題,tiny6410也不例外,友善的解決方法是採用一線觸摸,即在LCD板上,用一個單片機控制ADS7846芯片AD轉換,再將數據通過單總線的方式與6410通訊。可是,我這裏沒有一線觸摸板,於是就開始移植ADS7846驅動到tiny6410。

介紹:

 可能有人會問,6410的觸摸屏爲什麼會抖動呢,是不是濾波沒做好,或者是硬件走線的原因?是不是硬件的原因,我無法驗證。我嘗試過通過濾波來改善,但效果一般。不說別的,我在單獨測試AD的時候,發現在輸入不變的情況下,AD採樣的跳幅最大達到25(0~1024範圍),可見,跳動還是很大的,這樣的情況下,即使再濾波也只是浪費時間。

mini6410-ts.c觸摸屏驅動見:

(原創)6410觸摸屏驅動分析(s3c-ts.c)(Linux)(分析)
先說說我的硬件:

TS_PEN ----- GPN9 EINT9

TS_MISO ---- GPC0 MISO0

TS_MOSI ---- GPC2 MOSI0

TS_SCK ----- GPC1 SCK0

TS_CS ----- GPC3 CS0

這些端子在覈心板的CON1上可以找到,我這裏是用的IO模擬的SPI。

下面的開始介紹移植的過程,代碼,我在下面會全部帖出來,裏面註釋也很詳細,一般都能看明白。

1、複製/opt/FriendlyARM/mini6410/linux-2.6.38/drivers/input/touchscreen/mini6410-ts.c爲mini6410-ads7846.c。

修改後,如下:

 /* 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
*
* a misc driver for mini6410 touch screen
* by FriendlyARM 2010
*
* Based on following software:
*
** 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
*
* 2011-6-23: ADS7846觸摸屏驅動程序 by liu_xf
* http://www.cnblogs.com/liu_xf
*
* 2011-6-29 改進濾波,並將AD連續轉換8次的間隔縮小了
*/

#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>
#include <linux/fs.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>

#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <mach/hardware.h>


//#include <mach/irqs.h>
#include <mach/map.h>
#include <mach/regs-clock.h>
#include <mach/regs-gpio.h>
#include <plat/gpio-cfg.h>
#include <mach/gpio-bank-n.h>
#include <mach/gpio-bank-c.h>


#define CONFIG_TOUCHSCREEN_ADS7846_DEBUG
#undef CONFIG_TOUCHSCREEN_ADS7846_DEBUG
#define DEBUG_LVL KERN_DEBUG


/*
* Definitions & global arrays.
*/
#define DEVICE_NAME "touchscreen"
static DECLARE_WAIT_QUEUE_HEAD(ts_waitq); //定義並初始化一個等待隊列

typedef unsigned TS_EVENT;
#define NR_EVENTS 64 //觸摸屏fifo大小


static TS_EVENT events[NR_EVENTS];
static int evt_head, evt_tail; //fifo的頭的尾
//驅動寫fifo時evt_head++,應用讀fifo時 evt_tail++

#define ts_evt_pending() ((volatile u8)(evt_head != evt_tail)) //相等就表示滿了
#define ts_evt_get() (events + evt_tail)
#define ts_evt_pull() (evt_tail = (evt_tail + 1) & (NR_EVENTS - 1))
#define ts_evt_clear() (evt_head = evt_tail = 0)

#define ADS7846_CNV_NBR 8 //ADC連續轉換的次數
struct ads7846_ts_info {

struct input_dev *dev;

unsigned int xp; //x方向位置
unsigned int yp; //y方向位置
unsigned int count; //adc轉換次數
unsigned int cnv_nbr;
unsigned int x_buf[ADS7846_CNV_NBR]; //ad轉換buf
unsigned int y_buf[ADS7846_CNV_NBR];

};

static struct ads7846_ts_info *ts;




//ADS7846底層操作========================================================================
#define ADS7846_GPIO_MISO 0 //gpc0
#define ADS7846_GPIO_MOSI 2 //gpc2
#define ADS7846_GPIO_CLK 1 //gpc1
#define ADS7846_GPIO_CS 3 //gpc3

// ADS7846 Control Byte bit defines
#define ADS7846_CMD_START 0x0080
#define ADS7846_ADDR_BIT 4
#define ADS7846_ADDR_MASK (0x7<<ADS7846_ADDR_BIT)
#define ADS7846_MEASURE_X (0x5<<ADS7846_ADDR_BIT)
#define ADS7846_MEASURE_Y (0x1<<ADS7846_ADDR_BIT)
#define ADS7846_MEASURE_Z1 (0x3<<ADS7846_ADDR_BIT)
#define ADS7846_MEASURE_Z2 (0x4<<ADS7846_ADDR_BIT)
#define ADS7846_8BITS (1<<3)
#define ADS7846_12BITS 0
#define ADS7846_SER (1<<2)
#define ADS7846_DFR 0
#define ADS7846_PWR_BIT 0
#define ADS7846_PD 0
#define ADS7846_ADC_ON (0x1<<ADS7846_PWR_BIT)
#define ADS7846_REF_ON (0x2<<ADS7846_PWR_BIT)
#define ADS7846_REF_ADC_ON (0x3<<ADS7846_PWR_BIT)

#define MEASURE_8BIT_X\
(unsigned short)(ADS7846_CMD_START | ADS7846_MEASURE_X | ADS7846_8BITS | ADS7846_DFR | ADS7846_PD)
#define MEASURE_8BIT_Y\
(unsigned short)(ADS7846_CMD_START | ADS7846_MEASURE_Y | ADS7846_8BITS | ADS7846_DFR | ADS7846_PD)

#define MEASURE_12BIT_X \
(unsigned short)(ADS7846_CMD_START | ADS7846_MEASURE_X | ADS7846_12BITS | ADS7846_DFR | ADS7846_PD)
#define MEASURE_12BIT_Y \
(unsigned short)(ADS7846_CMD_START | ADS7846_MEASURE_Y | ADS7846_12BITS | ADS7846_DFR | ADS7846_PD)
#define MEASURE_12BIT_Z1 \
(unsigned char)(ADS7846_MEASURE_Z1 | ADS7846_12BITS | ADS7846_DFR | ADS7846_PD)
#define MEASURE_12BIT_Z2 \
(unsigned char)(ADS7846_MEASURE_Z2 | ADS7846_12BITS | ADS7846_DFR | ADS7846_PD)

// Pin access
//
static inline void set_miso_as_up(void)//gpc0
{
unsigned long tmp;
tmp = readl(S3C64XX_GPCPUD);
tmp &= ~(3U <<0);
tmp |= (2U << 0);
writel(tmp, S3C64XX_GPCPUD);
}

static inline void set_miso_as_input(void)//gpc0
{
unsigned long tmp;
tmp = readl(S3C64XX_GPCCON);
tmp &= ~(0xf << 0);
writel(tmp, S3C64XX_GPCCON);
}

static inline void set_cs_mosi_clk_as_output(void)//gpc1 2 3
{
unsigned long tmp;
tmp = readl(S3C64XX_GPCCON);
tmp = (tmp & ~0xfff0) | (0x1110);
writel(tmp, S3C64XX_GPCCON);
}

static inline void set_cs_mosi_clk_as_up(void)//gpc1 2 3
{
unsigned long tmp;
tmp = readl(S3C64XX_GPCPUD);
tmp &= ~((3U <<2)|(3U <<4)|(3U <<6));
tmp |= (2U << 2) | (2U << 4) | (2U << 6);
writel(tmp, S3C64XX_GPCPUD);
}


static inline void set_gpcx_value(int pinx ,int v)
{
unsigned long tmp;
tmp = readl(S3C64XX_GPCDAT);
if (v) {
tmp |= (1 << pinx);
} else {
tmp &= ~(1<<pinx);
}
writel(tmp, S3C64XX_GPCDAT);
}

static inline int get_gpcx_value(int pinx)
{
int v;
unsigned long tmp;
tmp = readl(S3C64XX_GPCDAT);
v = !!(tmp & (1<<pinx));
return v;
}

//讀12bit
static unsigned int ads7846_Read_Data(void)
{
unsigned int i,temp=0x00;
for(i=0;i<12;i++)
{
temp <<=1;
set_gpcx_value(ADS7846_GPIO_CLK, 1); udelay(10);
set_gpcx_value(ADS7846_GPIO_CLK, 0); udelay(10);
if(get_gpcx_value(ADS7846_GPIO_MISO) != 0)temp++;
}
return (temp);
}
//寫8bit
static void ads7846_Write_Data(unsigned int n)
{
unsigned char i;
set_gpcx_value(ADS7846_GPIO_CLK, 0);
for(i=0;i<8;i++)
{
if((n&0x80)==0x80)
set_gpcx_value(ADS7846_GPIO_MOSI, 1);
else
set_gpcx_value(ADS7846_GPIO_MOSI, 0);

n <<= 1;
set_gpcx_value(ADS7846_GPIO_CLK, 1); udelay(10);
set_gpcx_value(ADS7846_GPIO_CLK, 0); udelay(10);
}
}


//ADS7846轉換==
//保存在ts->buf 當中
static void ads7846_conver_start(void)
{
int i;
unsigned int cmd[2];
unsigned int data[2];

set_gpcx_value(ADS7846_GPIO_CS, 0);
//開讀
cmd[0] = MEASURE_12BIT_X;
cmd[1] = MEASURE_12BIT_Y;

/* CS# Low */
set_gpcx_value(ADS7846_GPIO_CS, 0);

//連續轉換
for(ts->count=0; ts->count<ts->cnv_nbr;)
{
//分別讀出x y座標==
for(i=0; i<2; i++){
ads7846_Write_Data(cmd[i]);
udelay(40);
data[i] = ads7846_Read_Data();
}



//保存轉換結果
ts->x_buf[ts->count]= data[0];
ts->y_buf[ts->count]= data[1];
ts->count++;
}
/* CS# High */
set_gpcx_value(ADS7846_GPIO_CS, 1);

#ifdef CONFIG_TOUCHSCREEN_ADS7846_DEBUG
//printk("ts read pos: x = %d, y = %d\n", data[0], data[1]);
#endif

}


//-----------------------------------------------------------------------------

//觸摸屏數據濾波算法
//觸摸屏AD連續轉換N次後,按升序排列,再取中間幾位值求平均
#define TS_AD_NBR_PJ 4 //取中間4位求平均值
#define TS_AD_NBR_MAX_CZ 10 //最大與最小的差值
static inline bool touch_ad_data_filter(unsigned int *buf0, unsigned int *buf1)
{
unsigned int i,j,k,temp,temp1,nbr=(ADS7846_CNV_NBR);
//將轉換結果升序排列
//buf0
for (j= 0; j< nbr; j++)
for (i = 0; i < nbr; i++)
{
if(buf0[i]>buf0[i+1])//升序排列
{
temp=buf0[i+1];
buf0[i+1]=buf0[i];
buf0[i]=temp;
}
}

//buf1
for (j= 0; j< nbr; j++)
for (i = 0; i < nbr; i++)
{
if(buf1[i]>buf1[i+1])//升序排列
{
temp=buf1[i+1];
buf1[i+1]=buf1[i];
buf1[i]=temp;
}
}

//串口調試查看結果
//for (j= 0; j< nbr; j++)
// printk("buf0[%2d]=%4d, buf1[%2d]=%4d,\r\n",j,buf0[j],j,buf1[j]);
//取中間值求平均==
k=((nbr-TS_AD_NBR_PJ)>>1);
temp = 0;temp1 = 0;
//檢查值是否有效==
if((buf0[k+TS_AD_NBR_PJ-1]-buf0[k]>TS_AD_NBR_MAX_CZ)||(buf1[k+TS_AD_NBR_PJ-1]-buf1[k]>TS_AD_NBR_MAX_CZ)) //無效值
{

return 0;
}
//--
//將中間指定位數累加
for(i=0;i<TS_AD_NBR_PJ;i++)
{
temp += buf0[k+i];
temp1 += buf1[k+i];
}
//求平均值,將結果保存在最低位
buf0[0]=temp/TS_AD_NBR_PJ;
buf1[0] = temp1/TS_AD_NBR_PJ;
//--
return 1;

}


//將AD轉換的值放入FIFO
//這裏是一個先進先出的fifo
//只要有數據被添加進來,就會喚醒ts_waitq進程
static void ts_evt_add(unsigned x, unsigned y, unsigned down) {
unsigned ts_event;
int next_head;

ts_event = ((x << 16) | (y)) | (down << 31);
next_head = (evt_head + 1) & (NR_EVENTS - 1);
//沒滿就裝入
if (next_head != evt_tail) {
events[evt_head] = ts_event;
evt_head = next_head;
#ifdef CONFIG_TOUCHSCREEN_ADS7846_DEBUG
printk("====>Add ... [ %4d, %4d ]%s\n", x, y, down ? "":" ~~~");
#endif
/* wake up any read call */
if (waitqueue_active(&ts_waitq)) { //判斷等待隊列是否有進程睡眠
wake_up_interruptible(&ts_waitq); //喚醒ts_waitq等待隊列中所有interruptible類型的進程
}
} else {
/* drop the event and try to wakeup readers */
printk(KERN_WARNING "mini6410-ads7846: touch event buffer full");
wake_up_interruptible(&ts_waitq);
}
}

static unsigned int ads7846_ts_poll( struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;

//將ts_waitq等待隊列添加到poll_table裏去
poll_wait(file, &ts_waitq, wait);
//返回掩碼
if (ts_evt_pending())
mask |= POLLIN | POLLRDNORM; //返回設備可讀

return mask;
}

//讀 系統調用==
static int ads7846_ts_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
DECLARE_WAITQUEUE(wait, current); //把當前進程加到定義的等待隊列頭wait中
char *ptr = buff;
int err = 0;

add_wait_queue(&ts_waitq, &wait); //把wait入到等待隊列頭中。該隊列會在進程等待的條件滿足時喚醒它。
//我們必須在其他地方寫相關代碼,在事件發生時,對等的隊列執行wake_up()操作。
//這裏是在ts_evt_add裏wake_up
while (count >= sizeof(TS_EVENT)) {
err = -ERESTARTSYS;
if (signal_pending(current)) //如果是信號喚醒 參考http://www.360doc.com/content/10/1009/17/1317564_59632874.shtml
break;

if (ts_evt_pending()) {
TS_EVENT *evt = ts_evt_get();

err = copy_to_user(ptr, evt, sizeof(TS_EVENT));
ts_evt_pull();

if (err)
break;

ptr += sizeof(TS_EVENT);
count -= sizeof(TS_EVENT);
continue;
}

set_current_state(TASK_INTERRUPTIBLE); //改變進程狀態爲可中斷的睡眠
err = -EAGAIN;
if (filp->f_flags & O_NONBLOCK) //如果上層調用是非阻塞方式,則不阻塞該進程,直接返回EAGAIN
break;
schedule(); //本進程在此處交出CPU控制權,等待被喚醒
//進程調度的意思側重於把當前任務從CPU拿掉,再從就緒隊列中按照調度算法取一就緒進程佔用CPU
}
current->state = TASK_RUNNING;
remove_wait_queue(&ts_waitq, &wait);

return ptr == buff ? err : ptr - buff;
}
//--

static int ads7846_ts_open(struct inode *inode, struct file *filp) {
/* flush event queue */
ts_evt_clear();

return 0;
}

//當應用程序操作設備文件時調用的open read等函數,最終會調用這個結構體中對應的函數
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.read = ads7846_ts_read,
.poll = ads7846_ts_poll, //select系統調用
.open = ads7846_ts_open,
};

//設備號,設備名,註冊的時候用到這個數組
//混雜設備主設備號爲10
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR, //自動分配次設置號
//.minor = 180,
.name = DEVICE_NAME,
.fops = &dev_fops,
};






/**
* get_down - return the down state of the pen
* 獲取pen引腳狀態,爲0表示down
* GPN9 EINT9 TS_PEN
*/
static inline bool get_down(void)
{
int tmp,down;
tmp = readl(S3C64XX_GPNDAT);
down = !(tmp & (1<<9));
return (down);
}


/*===========================================================================================
touch_timer_get_value這個函數的調用:

1、 觸摸筆開始點擊的時候, 在中斷函數touch_down裏面被調用,不是直接調用,而是設置定時器超時後調用
這樣是爲了去抖動

2、 touch_timer_get_value被調用一次後,如果pendown信號有效,則touch_timer_get_value會被持續調用
也是通過定時器實現的

touch_timer_get_value這個函數的功能:
啓動7846轉換,直到連續轉換8次後,再濾波處理,獲得有效值,並向上報告觸摸屏事件

============================================================================================*/
static void touch_timer_get_value(unsigned long data);

static DEFINE_TIMER(touch_timer, touch_timer_get_value, 0, 0);

static void touch_timer_get_value(unsigned long data) {

int pendown;

pendown = get_down();

#ifdef CONFIG_TOUCHSCREEN_ADS7846_DEBUG
// if(pendown)
// {
// printk("touch is down!\n");
// }
// else
// printk("touch is up!\n");
#endif

if (pendown) {

//關中斷===
disable_irq(IRQ_EINT(9));
//--
//啓動ADS7846轉換==
ads7846_conver_start();
//開中斷==
enable_irq(IRQ_EINT(9));
//--
//如果轉換了8次,就寫入FIFO保存
//if (ts->count == ( ts->cnv_nbr)) {
if(touch_ad_data_filter(ts->x_buf,ts->y_buf)) //濾波處理
{

ts->xp = ts->x_buf[0];
ts->yp = ts->y_buf[0];

ts_evt_add(ts->xp, ts->yp, 1); //向上報告觸摸屏事件
}


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

mod_timer(&touch_timer, jiffies + 2); //重新設置系統定時器,超時後,又會調用touch_timer_get_value
//jiffies變量記錄了系統啓動以來,系統定時器已經觸發的次數。內核每秒鐘將jiffies變量增加HZ次。
//因此,對於HZ值爲100的系統,1個jiffy等於10ms,而對於HZ爲1000的系統,1個jiffy僅爲1ms
//這裏系統配置提HZ是100

} else { //如果是鬆開,報告其觸摸筆狀態
ts->xp = 0;
ts->yp = 0;
ts->count = 0;

ts_evt_add(0, 0, 0);


}

}



//觸摸屏按下中斷服務==
//雙邊沿中斷
static irqreturn_t touch_down(int irqno, void *param)
{


/* Clear interrupt */
//__raw_writel(0x0, ts_base + S3C_ADCCLRWK);
//__raw_writel(0x0, ts_base + S3C_ADCCLRINT);

//稍後調用touch_timer_get_value,去抖動
mod_timer(&touch_timer, jiffies + 2); //等ADS7846轉換完成了再讀
//同時還可以防抖動,如果定時器沒有超時的這段時間裏,發生了擡起和按下中斷,則定時器的值會被重設,不會超時
//內核配置時HZ值設爲100,即1個jiffy等於10ms,
//touch_timer_get_value(1); //直接調用會有抖動

return IRQ_RETVAL(IRQ_HANDLED);
}




//-------------------------------------------


/*
* The functions for inserting/removing us as a module.
*/
static int __init ads7846_ts_probe(struct platform_device *pdev)
{
struct device *dev;
int ret = 0;

dev = &pdev->dev;
#ifdef CONFIG_TOUCHSCREEN_ADS7846_DEBUG
printk("ads7846_ts_probe start!\n");
#endif
//給ads7846_ts_info指針分配內存==
ts = kzalloc(sizeof(struct ads7846_ts_info), GFP_KERNEL);
ts->cnv_nbr = ADS7846_CNV_NBR;
ts->xp = 0;
ts->yp = 0;
ts->count = 0;


//申請中斷==
//TS_PEN雙邊沿觸發 EINT9 GPN9
ret = request_irq(IRQ_EINT(9), touch_down,IRQ_TYPE_EDGE_BOTH,
"ads7864_touch", ts);

if (ret != 0) {
dev_err(dev,"ads7846_ts.c: Could not allocate ts IRQ_EINT !\n");
ret = -EIO;
goto fail;
}




ret = misc_register(&misc); //註冊這混雜字符設備
if (ret) {
dev_err(dev, "ads7846_ts.c: Could not register device(mini6410 touchscreen)!\n");
ret = -EIO;
goto fail;
}
//初始化GPIO==
set_miso_as_up();
set_miso_as_input();
set_cs_mosi_clk_as_up();
set_cs_mosi_clk_as_output();
set_gpcx_value(ADS7846_GPIO_MOSI ,1);
set_gpcx_value(ADS7846_GPIO_CS ,1);
set_gpcx_value(ADS7846_GPIO_CLK ,1);
//--

#ifdef CONFIG_TOUCHSCREEN_ADS7846_DEBUG
printk("ads7846_ts_probe end!\n");
#endif

return 0;

fail:
disable_irq(IRQ_EINT(9));
free_irq(IRQ_EINT(9), ts);

return ret;

}

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

disable_irq(IRQ_EINT(9));
free_irq(IRQ_EINT(9), ts);
return 0;
}

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

static int ads7846_ts_suspend(struct platform_device *dev, pm_message_t state)
{

return 0;
}

static int ads7846_ts_resume(struct platform_device *pdev)
{
return 0;
}
#else
#define ads7846_ts_suspend NULL
#define ads7846_ts_resume NULL
#endif

static struct platform_driver ads7846_ts_driver = {
.probe = ads7846_ts_probe,
.remove = ads7846_ts_remove,
.suspend = ads7846_ts_suspend,
.resume = ads7846_ts_resume,
.driver = {
.owner = THIS_MODULE,
.name = "ads7846-ts",
},
};

static char banner[] __initdata = KERN_INFO "mini6410 ads7846 Touchscreen driver,by liu_xf 20110622,\n";

static int __init ads7846_ts_init(void)
{
printk(banner);
return platform_driver_register(&ads7846_ts_driver);
}

static void __exit ads7846_ts_exit(void)
{
platform_driver_unregister(&ads7846_ts_driver);
}

module_init(ads7846_ts_init);
module_exit(ads7846_ts_exit);

MODULE_AUTHOR("Hunan Create Inc.");
MODULE_LICENSE("GPL");

2、修改當前目錄下的Kconfig和makefile,讓它在menuconfig裏可見,並能被編譯。

在 Kconfig裏添加:

?1
2
3
4
5
6
7
8
9
10
11
12 config TOUCHSCREEN_MINI6410_ADS7846
tristate "ADS7846 touchscreen driver for Mini6410"
depends on MACH_MINI6410
default y
help
Say Y here to enable the driver for the ADS7846 touchscreen on the
FriendlyARM Mini6410 development board.

If unsure, say N.

To compile this driver as a module, choose M here: the
module will be called mini6410-ads7846.

makefile裏添加

?obj-$(CONFIG_TOUCHSCREEN_MINI6410_ADS7846) += mini6410-ads7846.o<BR>

3、 ads7846_device_ts設備定義

複製 /opt/FriendlyARM/mini6410/linux-2.6.38/arch/arm/mach-s3c64xx/dev-ts-mini6410.c爲dev-ads7846-mini6410.c,更改代碼爲
 /* linux/arch/arm/mach-s3c64xx/dev-ts-mini6410.c
*
* Copyright (c) 2008 Simtec Electronics
* Ben Dooks <[email protected]>
* http://armlinux.simtec.co.uk/
*
* S3C series device definition for touchscreen devices
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/

#include <linux/kernel.h>
#include <linux/platform_device.h>

#include <mach/map.h>
#include <mach/irqs.h>
#include <plat/devs.h>
#include <plat/cpu.h>
#include <mach/ts.h>

/* Touch srcreen */
//resource沒有用,先留在這,不管它
static struct resource ads7846_ts_resource[] = {
[0] = {
.start = SAMSUNG_PA_ADC,
.end = SAMSUNG_PA_ADC + SZ_256 - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_PENDN,
.end = IRQ_PENDN,
.flags = IORESOURCE_IRQ,
},
[2] = {
.start = IRQ_ADC,
.end = IRQ_ADC,
.flags = IORESOURCE_IRQ,
}
};

struct platform_device ads7846_device_ts = {
.name = "ads7846-ts",
.id = -1,
.num_resources = ARRAY_SIZE(ads7846_ts_resource),
.resource = ads7846_ts_resource,
};
/*
void __init ads7846_ts_set_platdata(struct s3c_ts_mach_info *pd)
{
printk(KERN_ERR "%s: no platform data\n", __func__);

}
*/

4、在/opt/FriendlyARM/mini6410/linux-2.6.38/arch/arm/mach-s3c64xx/mach-mini6410.c的platform_device添加ads7846_device_ts設備(s3c_device_ts附近),註冊設備時要用到它

?#ifdef CONFIG_TOUCHSCREEN_MINI6410_ADS7846
&ads7846_device_ts,
#endif

5、添加設備聲明

mach-mini6410裏註冊ads7846_device_ts設備,但在哪裏mach-mini6410並不知道,需要在devs.h裏聲明,打開當前目錄下的devs.h,添加如下代碼:




?extern struct platform_device ads7846_device_ts;<BR>



6、然後再make,放到SD卡里運行

make menuconfig,在device drives-> input device support -> touch screens選擇ADS7846 touchscreen driver for Mini6410 ,然後再make zImage。

完成後,再放到SD卡里運行,怎麼樣,一點也不抖動了吧!

結語:

 這個驅動是由mini6410-ts.c修改而來的,那些系統調用的函數都沒有改,只是將獲取觸摸屏數據的方式變了,可以直接用原來的tslib。經測試,效果良好。但是在移植的過程中,有一個問題還是沒搞明白,之前,我是將TS_PEN接在EINT6上在的,但一接上去,就一直中斷,把系統都弄死了,要按下觸摸屏才能停止中斷,不知道是什麼原因,可能是EINT6被其它地方設置了,還是怎麼回事,後來換成EINT9就好了,正好兩個端子相鄰。知道的大俠還請告訴我一聲。

源代碼下載(下載後將後綴改爲tar.gz)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章