1wire_ts.c源码:
#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/clk.h"
#include "linux/miscdevice.h"
#include "linux/platform_device.h"
#include "linux/timer.h"
#include "linux/param.h"
#include "linux/poll.h"
#include "linux/proc_fs.h"
#include "linux/gpio.h"
#include "linux/cdev.h"
#include "linux/sched.h"
#include "linux/interrupt.h"
#include "linux/irq.h"
#include "linux/err.h"
#include "linux/io.h"
#include "asm/system.h"
#include "asm/leds.h"
#include "asm/mach-types.h"
#include "asm/irq.h"
#include "asm/mach/time.h"
#include "asm/io.h"
#include "asm/irq.h"
#include "asm/uaccess.h"
#include "mach/map.h"
#include "mach/regs-irq.h"
#include "mach/regs-clock.h"
#include "mach/regs-gpio.h"
#include "plat/regs-timer.h"
#include "plat/gpio-cfg.h"
#include "plat/clock.h"
#include "plat/cpu.h"
static DECLARE_WAIT_QUEUE_HEAD(ts_waitq);
#define MHZ (1000*1000)
#define PRINT_MHZ(m) ((m) / MHZ), ((m / 1000) % 1000)
#define TOUCH_DEVICE_NAME "tiny_ts"
#define SAMPLE_BPS 9600
#define SLOW_LOOP_FEQ 25
#define FAST_LOOP_FEQ 60
#define REQ_TS 0x40U
#define REQ_INFO 0x60U
enum {
IDLE,
START,
REQUEST,
WAITING,
RESPONSE,
STOPING,
} one_wire_status = IDLE;
struct timer_regs {
unsigned long tcfg0;
unsigned long tcfg1;
unsigned long tcon;
unsigned long reserved1[9];
unsigned long tcntb3;
unsigned long tcmpb3;
unsigned long tcnto3;
unsigned long reserved2[2];
unsigned long tint_cstat;
};
static struct timer_regs *timer_regs;
static unsigned long *gph1con;
static unsigned long *gph1dat;
static int ts_ready;
static unsigned ts_status;
static unsigned lcd_type, firmware_ver;
static unsigned total_received, total_error;
static unsigned last_req, last_res;
static unsigned long TCNT_FOR_SAMPLE_BIT;
static volatile unsigned int io_bit_count;
static volatile unsigned int io_data;
static volatile unsigned char one_wire_request;
static int exitting;
static struct timer_list ts_timer;
static inline void notify_ts_data(unsigned x, unsigned y, unsigned down)
{
if (!down && !(ts_status &(1U << 31))) {
// up repeat, give it up
return;
}
ts_status = ((x << 16) | (y)) | (down << 31);
ts_ready = 1;
wake_up_interruptible(&ts_waitq);
}
static ssize_t ts_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
unsigned long err;
if (!ts_ready) {
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
else
wait_event_interruptible(ts_waitq, ts_ready);
}
ts_ready = 0;
if (count < sizeof ts_status) {
return -EINVAL;
} else {
count = sizeof ts_status;
}
err = copy_to_user((void *)buffer, (const void *)(&ts_status), sizeof ts_status);
return err ? -EFAULT : sizeof ts_status;
}
static unsigned int ts_poll( struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &ts_waitq, wait);
if (ts_ready)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static struct file_operations ts_fops = {
owner: THIS_MODULE,
read: ts_read,
poll: ts_poll,
};
static struct miscdevice ts_misc = {
.minor = 181,
.name = TOUCH_DEVICE_NAME,
.fops = &ts_fops,
};
static inline void notify_info_data(unsigned char _lcd_type,unsigned char ver_year,
unsigned char week)
{
if (_lcd_type != 0xFF) {
lcd_type = _lcd_type;
firmware_ver = ver_year * 100 + week;
}
}
static inline void set_pin_value(int v)
{
if (v) {
*gph1dat |= 1<<2;
} else {
*gph1dat &= ~(1<<2);
}
}
static inline int get_pin_value(void)
{
if(*gph1dat & (1<<2))
return 1;
else
return 0;
}
// CRC
static const unsigned char crc8_tab[] = {
0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
0xE0, 0xE7,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06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB,
0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB,
0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3,
};
#define crc8_init(crc) ((crc) = 0XACU)
#define crc8(crc, v) ( (crc) = crc8_tab[(crc) ^(v)])
// once a session complete
static void one_wire_session_complete(unsigned char req, unsigned int res)
{
unsigned char crc;
const unsigned char *p = (const unsigned char*)&res;
total_received ++;
last_res = res;
crc8_init(crc);
crc8(crc, p[3]);
crc8(crc, p[2]);
crc8(crc, p[1]);
if (crc != p[0]) {
// CRC dismatch
if (total_received > 100) {
total_error++;
}
return;
}
switch(req) {
case REQ_TS:
{
unsigned short x,y;
unsigned pressed;
x = ((p[3] >> 4U) << 8U) + p[2];
y = ((p[3] & 0xFU) << 8U) + p[1];
pressed = (x != 0xFFFU) && (y != 0xFFFU);
notify_ts_data(x, y, pressed);
}
break;
case REQ_INFO:
notify_info_data(p[3], p[2], p[1]);
break;
default:
break;
}
}
// one-wire protocol core
static int init_timer3_for_ts(void)
{
struct clk *clk;
unsigned long pclk;
unsigned long prescaler;
unsigned long prescale1_value;
// 获得时钟,并使能时钟
clk = clk_get(NULL, "timers");
if (IS_ERR(clk)) {
printk("ERROR to get PCLK ");
return -EIO;
}
clk_enable(clk);
pclk = clk_get_rate(clk);
printk("Timer3 clock source : %ld.ld Mhz ", PRINT_MHZ(pclk));
// 获得定时器的预定标器的值,目的是为了不修改该值,因为定时器2,3,4共享它
prescaler = timer_regs->tcfg0;
prescale1_value = (prescaler >> 8) & 0xFF;
// calc the TCNT_FOR_SAMPLE_BIT, that is one of the goal
TCNT_FOR_SAMPLE_BIT = pclk / (prescale1_value + 1) / SAMPLE_BPS - 1;
// 定时器3的分频系数为1
timer_regs->tcfg1 &= (0xf<<12);
printk("TCNT_FOR_SAMPLE_BIT = %d ", TCNT_FOR_SAMPLE_BIT);
return 0;
}
static inline void stop_timer_for_1wire(void)
{
timer_regs->tcon &= ~(1<<16); // 停止定时器3
}
static irqreturn_t timer_for_1wire_interrupt(int irq, void *dev_id)
{
timer_regs->tint_cstat |= 0x100; // 清中断
io_bit_count--;
switch(one_wire_status)
{
case START:
if (io_bit_count == 0) {
io_bit_count = 16;
one_wire_status = REQUEST;
}
break;
case REQUEST:
// Send a bit
set_pin_value(io_data & (1U << 31));
io_data <<= 1;
if (io_bit_count == 0) {
io_bit_count = 2;
one_wire_status = WAITING;
}
break;
case WAITING:
if (io_bit_count == 0) {
io_bit_count = 32;
one_wire_status = RESPONSE;
}
if (io_bit_count == 1) {
*gph1con &= ~(1<<8);// 设置为输入
set_pin_value(1);
}
break;
case RESPONSE:
// Get a bit
io_data = (io_data << 1) | get_pin_value();
if (io_bit_count == 0) {
io_bit_count = 2;
one_wire_status = STOPING;
set_pin_value(1);
*gph1con |= 1<<8;// 设置为输出
one_wire_session_complete(one_wire_request, io_data);
}
break;
case STOPING:
if (io_bit_count == 0) {
one_wire_status = IDLE;
stop_timer_for_1wire();
}
break;
default:
stop_timer_for_1wire();
}
return IRQ_HANDLED;
}
static struct irqaction timer_for_1wire_irq = {
.name = "1-wire Timer Tick",
.flags = IRQF_DISABLED | IRQF_IRQPOLL,
.handler = timer_for_1wire_interrupt,
.dev_id = &timer_for_1wire_irq,
};
static void start_one_wire_session(unsigned char req)
{
unsigned long flags;
if (one_wire_status != IDLE) {
return;
}
one_wire_status = START;
set_pin_value(1);
*gph1con |= 1<<8; // 设置为输出
// IDLE to START
{
unsigned char crc;
crc8_init(crc);
crc8(crc, req);
io_data = (req << 8) + crc;
io_data <<= 16;
}
last_req = (io_data >> 16);
one_wire_request = req;
io_bit_count = 1;
*gph1con |= 1<<8; // 设置为输出
timer_regs->tcntb3 = TCNT_FOR_SAMPLE_BIT;
// 不手动装载,不电平反转,不自动装载
timer_regs->tcon &=~(0xF << 16);
timer_regs->tcon |= 1<<17; // 手动转载数据
local_irq_save(flags);
timer_regs->tcon |= 1<<16; // 开启定时器3,并开启自动装载
timer_regs->tcon |= 1<<19;
timer_regs->tcon &= ~(1<<17);// 关闭手动装载
set_pin_value(0);
local_irq_restore(flags);
}
void one_wire_timer_proc(unsigned long v)
{
unsigned char req;
if (exitting) {
return;
}
// 设置查询周期为20ms,并启动内核定时器
ts_timer.expires = jiffies + HZ / 50;
add_timer(&ts_timer);
if (lcd_type == 0) {
req = REQ_INFO;
}
else {
req = REQ_TS;
}
start_one_wire_session(req);
}
static int read_proc(char *buf, char **start, off_t offset, int count, int *eof, void *data)
{
int len;
len = sprintf(buf, "%u %u %u %u X X ",
lcd_type, firmware_ver, total_received, total_error, last_req, last_res);
*eof = 1;
return len;
}
static int dev_init(void)
{
int ret;
ret = misc_register(&ts_misc);
if (ret == 0) {
create_proc_read_entry("driver/one-wire-info", 0, NULL, read_proc, NULL);
printk (TOUCH_DEVICE_NAME" initialized ");
}
timer_regs = ioremap(0xE2500000, sizeof(struct timer_regs));
gph1con = ioremap(0xE0200C20, 8);
gph1dat = gph1con + 1;
// 设置GPH1_2为输出,并输出1
*gph1con |= 1<<8;
*gph1dat |= 1<<2;
setup_irq(IRQ_TIMER3, &timer_for_1wire_irq);
init_timer3_for_ts();
// 初始化内核的定时器
init_timer(&ts_timer);
ts_timer.function = one_wire_timer_proc;
one_wire_timer_proc(0);
// 使能定时器3中断,清中断
timer_regs->tint_cstat |= 0x108;
return ret;
}
static void dev_exit(void)
{
exitting = 1;
remove_proc_entry("driver/one-wire-info", NULL);
misc_deregister(&ts_misc);
del_timer_sync(&ts_timer);
free_irq(IRQ_TIMER3, &timer_for_1wire_irq);
iounmap(timer_regs);
iounmap(gph1con);
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
==============================================================
一线触摸屏介绍:
杂项设备:(由于在移植一线触摸屏驱动的时候,会用到杂项设备,下面分析下它的框架)
分析driverscharmisc.c的实现过程:
static const struct file_operations misc_fops = { //分配设置一个file_operations结构体
.owner = THIS_MODULE,
.open = misc_open,
};
static int __init misc_init(void) //入口函数
{
misc_class = class_create(THIS_MODULE, "misc"); //创建类
if (register_chrdev(MISC_MAJOR,"misc",&misc_fops)) //注册一个主设备号为10的字符设备
}
subsys_initcall(misc_init); //申明misc_init()函数到子系统初始化集
问:从上面的分析过程可以发现,该子系统初始化程序完全符号写一般的字符驱动程序的步骤,那么应用程序是怎么对通过misc_register()注册的设备进行相应的读,写等操作的呢?
答:过程如下:
app: open() 应用程序使用open函数
------------------------------------------------------------------------------------------------------------------
kernel:(内核最终会调用到misc.c中的file_operations结构体中的open函数)
问:当看上面的file_operations结构体的时候,发现里面只有一个open函数?那是怎么来实现一系列的其他的操作的呢(读,写...)?
答:可以猜测出,一定是在这个open函数里面做了一些设置,然后在新的设置里面来进行相关的操作。
static int misc_open(struct inode * inode, struct file * file)
{
int minor = iminor(inode); //获得次设备号
list_for_each_entry(c, &misc_list, list) { //从misc_list链表中取出每一项(即miscdevice结构体)
if (c->minor == minor) { //通过次设备号进行匹配
new_fops = fops_get(c->fops); //如果次设备号一样,则获得该项的file_operations结构体成员
}
}
if (!new_fops) { //如果上述操作,获得file_operations结构体没有成功,则再次获取
list_for_each_entry(c, &misc_list, list) {...}
}
file->f_op = new_fops; //重新初始化struct file结构中的成员f_op,让它指向新的file_operations!
if (file->f_op->open) { //如果新获得的file_operations结构体有open函数
err=file->f_op->open(inode,file); //则调用他的open函数
}
}
问:从分析以上open函数时,发现会从misc_list链表中取出某一项来获取一个miscdevice结构体,进而获取他的成员fops(即新的file_operations结构体),从而构建出了一个新的file_operations结构体,那么上述misc_list链表是由谁来设置的呢?即链表中的成员miscdevice结构体又由谁来传入呢?
答:收索最终发现,它在misc_register函数中被设置:
int misc_register(struct miscdevice * misc)
{
misc->this_device = device_create(misc_class, misc->parent, dev, misc, "%s", misc->name); //建设备节点
list_add(&misc->list, &misc_list); //将传入的参数miscdevice结构体放入misc_list链表
}
Tiny210的硬件原理:
问:Tiny210一线触摸屏到底是什么?
答:友善的人自以为傲的核心技术,设计了一个一线精准触摸电路,并集成到LCD 的驱动板上,它采用触摸屏控制芯片ADS7843(或者相类似),配合一个ATC的单片机(该单片机里面固话了程序),构成一个独立的四线电阻触摸屏采集电路,最后通过一个普通的GPIO口把处理过的数据发送出去,在开发板上与之相连的是GPH1_2,实现单总线通信,这就是一线的来源。
说白了,就是ATC单片机通过SPI和ADS7843通信,控制ADS7843去读写触摸屏的数据,然后将数据存在单片机里面的RAM中,然后再通过单片机的一个GPIO发给ARM。
问:能够自己写出一线触摸的驱动吗?
答:能使肯定的,但是有些烦躁:
(1).在开发板的原理图上面,与GPH1_2相连接的LCD管脚上面用的net为"PWM",让人误以为是用PWM来通信(当然不可能,PWM是输出,无法读入数据);
(2).上面说了,触摸屏的数据会经单片机处理后,然后通过一个IO管脚发个ARM,但是该IO发出的数据的格式是什么样子的呢(即时序,几秒代表1,几秒代表0),由于友善把其视为核心技术,他们不向外提供固话在单片机中的程序(这个我能理解),但是他们也不提供IO发出的数据的时序。但是大家可以去阅读他们提供的内核里面的触摸屏驱动,然后反推出时序是怎么样的。但是,我没有去弄了,我只是移植了。
最后说说,友善的一线触摸驱动是怎么写的,它在入口函数中注册了一个内核定时器,用于查询方式去读取IO上面的"一帧"数据,然后再启用一个定时器3去解码这些数据,具体怎么解码,请大家参考我移植好了的驱动,然后攻破它吧。写出文档,记得共享到群共享去,发扬互学互助的精神!!!
==============================================================
tslib编译使用_测试一线触摸屏:
tslib-one-wire.tar.bz2已经上传自同文件夹中,这是一个已经打过补丁,支持一线触摸屏测试的tslib
编译:
tar xjvf tslib-one-wire.tar.bz2
cd tslib-1.0.0
./autogen.sh
mkdir tmp
echo "ac_cv_func_malloc_0_nonnull=yes" >arm-linux.cache
./configure --host=arm-linux --cache-file=arm-linux.cache --prefix=$(pwd)/tmp
make
make install
修改ts.conf
echo "module_raw one_wire_ts_input" > ts.conf.bck
cat $PWD/tmp/etc/ts.conf >>ts.conf.bck
mv ts.conf.bck $PWD/tmp/etc/ts.conf
安装到开发板:
cd tmp
cp * -rfd /nfsroot
-----------------------------------------------------------------------------
注意:以上操作都是在PC上面执行
----------------------------------------------------------------------------
export TSLIB_CALIBFILE=/etc/pointercal
export TSLIB_CONFFILE=/etc/ts.conf
export TSLIB_PLUGINDIR=/lib/ts
export TSLIB_CONSOLEDEVICE=none
export TSLIB_FBDEVICE=/dev/fb0
export TSLIB_TSDEVICE=/dev/tiny_ts
ts_calibrate
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』