这几天在调试串口触摸屏,网上找了下似乎说的都不大清楚,这里记录下.
实现方法网上说有好几种,这里是将串口作为一个serio总线设备,利用linux内核提供serio总线驱动,通过设置对应的串口,调用serport提供的函数将串口当做serio总线设备,在驱动里面需要按照serio总线设备驱动的框架来实现,在内核源码drivers/input/touchscreen下提供了两个例子,touchright.c和touchit123.c,可以参考这两个例子编写驱动.我的驱动是在网上抄的,源码如下
/* *Egalax serial touchscreen driver * * Copyright (c) 2004-2016 * * Based on Touchright driver (drivers/input/touchscreen/touchit213.c) * * Zolt??n B??sz??rm??nyi <zboszor@xxxxx> */ /* * 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/errno.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/input.h> #include <linux/serio.h> #include <linux/init.h> #define DRIVER_DESC "EETI Egalax serial touchscreen driver" MODULE_AUTHOR("Zolt??n B??sz??rm??nyi <zboszor@xxxxx>"); MODULE_DESCRIPTION(DRIVER_DESC); MODULE_LICENSE("GPL"); /* * Definitions & global arrays. */ #define SERIO_EGALAX 0x3f #define EGALAX_FORMAT_MAX_LENGTH 6 #define EGALAX_RESPONSE_BEGIN_BYTE 0x80 #define EGALAX_FORMAT_PRESSURE_BIT 0x40 #define EGALAX_FORMAT_TOUCH_BIT 0x01 #define EGALAX_FORMAT_RESOLUTION 0x06 #define EGALAX_MIN_XC 0 #define EGALAX_MAX_XC 0x4000 #define EGALAX_MIN_YC 0 #define EGALAX_MAX_YC 0x4000 #define EGALAX_GET_XC(data, resbits, shift) ((((data[1] & (resbits)) << 7) | (data[2] & 0x7f)) << shift) #define EGALAX_GET_YC(data, resbits, shift) ((((data[3] & (resbits)) << 7) | (data[4] & 0x7f)) << shift) #define EGALAX_GET_TOUCHED(data) (EGALAX_FORMAT_TOUCH_BIT & data[0]) /* * Per-touchscreen data. */ struct egalax { struct input_dev *dev; struct serio *serio; int idx; int bytes; int resbits; int shift; unsigned char data[EGALAX_FORMAT_MAX_LENGTH]; char phys[32]; }; static void egalax_process_data(struct egalax *pegalax) { struct input_dev *dev = pegalax->dev; if (++pegalax->idx == pegalax->bytes) { input_report_abs(dev, ABS_X, EGALAX_GET_XC(pegalax->data, pegalax->resbits, pegalax->shift)); input_report_abs(dev, ABS_Y, EGALAX_GET_YC(pegalax->data, pegalax->resbits, pegalax->shift)); //input_report_key(dev, BTN_TOUCH, EGALAX_GET_TOUCHED(pegalax->data)); input_report_abs(dev, ABS_PRESSURE, EGALAX_GET_TOUCHED(pegalax->data)); input_sync(dev); pegalax->idx = 0; } } static irqreturn_t egalax_interrupt(struct serio *serio, unsigned char data, unsigned int flags) { struct egalax *pegalax = serio_get_drvdata(serio); pegalax->data[pegalax->idx] = data; if (EGALAX_RESPONSE_BEGIN_BYTE & pegalax->data[0]) { pegalax->bytes = (EGALAX_FORMAT_PRESSURE_BIT & pegalax->data[0] ? 6 : 5); switch ((EGALAX_FORMAT_RESOLUTION & pegalax->data[0]) >> 1) { case 0: pegalax->resbits = 0x0f; pegalax->shift = 3; break; case 1: pegalax->resbits = 0x1f; pegalax->shift = 2; break; case 2: pegalax->resbits = 0x3f; pegalax->shift = 1; break; default: pegalax->resbits = 0x7f; pegalax->shift = 0; break; } egalax_process_data(pegalax); } else dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n", pegalax->data[0]); return IRQ_HANDLED; } static void egalax_disconnect(struct serio *serio) { struct egalax *pegalax = serio_get_drvdata(serio); input_get_device(pegalax->dev); input_unregister_device(pegalax->dev); serio_close(serio); serio_set_drvdata(serio, NULL); input_put_device(pegalax->dev); kfree(pegalax); } /* * egalax_connect() is the routine that is called when someone adds a * new serio device that supports egalax protocol and registers it as * an input device. This is usually accomplished using inputattach. */ static int egalax_connect(struct serio *serio, struct serio_driver *drv) { struct egalax *pegalax; struct input_dev *input_dev; int err; pegalax = kzalloc(sizeof(struct egalax), GFP_KERNEL); input_dev = input_allocate_device(); if (!pegalax || !input_dev) { err = -ENOMEM; goto fail1; } pegalax->serio = serio; pegalax->dev = input_dev; snprintf(pegalax->phys, sizeof(pegalax->phys), "%s/input1", serio->phys); input_dev->name = "EETI eGalaxTouch Serial TouchScreen"; input_dev->phys = pegalax->phys; input_dev->id.bustype = BUS_RS232; input_dev->id.vendor = SERIO_EGALAX; input_dev->id.product = 0; input_dev->id.version = 0x0001; input_dev->dev.parent = &serio->dev; input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); //input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); input_set_abs_params(pegalax->dev, ABS_X, EGALAX_MIN_XC, EGALAX_MAX_XC, 0, 0); input_set_abs_params(pegalax->dev, ABS_Y, EGALAX_MIN_YC, EGALAX_MAX_YC, 0, 0); input_set_abs_params(pegalax->dev, ABS_PRESSURE, 0, 1, 0, 0); serio_set_drvdata(serio, pegalax); err = serio_open(serio, drv); if (err) goto fail2; err = input_register_device(pegalax->dev); if (err) goto fail3; return 0; fail3: serio_close(serio); fail2: serio_set_drvdata(serio, NULL); fail1: input_free_device(input_dev); kfree(pegalax); return err; } /* * The serio driver structure. */ static struct serio_device_id egalax_serio_ids[] = { { .type = SERIO_RS232, .proto = SERIO_EGALAX, .id = SERIO_ANY, .extra = SERIO_ANY, }, { 0 } }; MODULE_DEVICE_TABLE(serio, egalax_serio_ids); static struct serio_driver egalax_drv = { .driver = { .name = "egalax", }, .description = DRIVER_DESC, .id_table = egalax_serio_ids, .interrupt = egalax_interrupt, .connect = egalax_connect, .disconnect = egalax_disconnect, }; /* * The functions for inserting/removing us as a module. */ static int __init egalax_init(void) { return serio_register_driver(&egalax_drv); } static void __exit egalax_exit(void) { serio_unregister_driver(&egalax_drv); } module_init(egalax_init); module_exit(egalax_exit);只是修改了最后一点,其中egalax_serio_ids中的几个参数在应用程序中需要用到,加上驱动编译内核更新后,此时这个驱动是没有起作用的,需要通过应用程序将串口与之绑定,后才能正常工作.
应用程序如下,这个代码网上也有
/************************************* NAME:seriotouch.c *************************************/ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <fcntl.h> #include <linux/fs.h> #include <errno.h> #include <string.h> #include <linux/fb.h> #include <malloc.h> #include <sys/mman.h> #include <termio.h> #include <linux/serio.h> #define SERIO_ANY 0xff #define SERIO_EGALAX 0x3f int main(int argc, char* argv[]) { int dev,ret; char comdev[20]; int ldisc; unsigned long type; struct termios option; int fd = -1; if(argc != 2) { printf("-----------------------------------------\n"); printf("\nUsage: %s [serial device(1-5)]\n", argv[0]); printf("Example: %s 5 n", argv[0]); printf("-----------------------------------------\n"); return 0; } dev = strtol(argv[1],NULL,10); sprintf(comdev,"/dev/ttyAMA%d",dev); fd = open(comdev,O_RDWR|O_NONBLOCK|O_NOCTTY); if (fd < 0) { perror(comdev); exit(1); } tcgetattr(fd, &option); option.c_iflag = IGNPAR | IGNBRK; option.c_cflag = HUPCL | CS8 | CREAD | CLOCAL | B9600; option.c_cc[VMIN] = 1; option.c_cc[VTIME] = 0; cfsetispeed(&option,B9600); cfsetospeed(&option,B9600); ret = tcsetattr(fd, TCSANOW, &option); if(ret < 0) { perror("TCSANOW"); exit(1); } ldisc = N_MOUSE; ret = ioctl(fd,TIOCSETD,&ldisc); if(ret) { perror("TIOCSETD"); } type = SERIO_EGALAX | (SERIO_ANY << 8) | (SERIO_ANY << 16); ret = ioctl(fd,SPIOCSTYPE, &type); if(ret) { perror("SPIOCSTYPE"); } read(fd,NULL,0); //close(fd); return 0; }
需要注意的是,应用程序最后这个read一定要执行,不然驱动里不会进行连接.
编译生成可执行文件seriotouch
应用程序配制完串口后需要与之前的驱动连接,看设置spiocstype的设置,需要保证这里的几个参数与驱动进行对应。
系统启动后执行seriotouch 5 &
后面这个5对应的是串口号,执行这个后会看到驱动里的connect执行了,同时会生成一个input节点,如下图
此时将tslib里的tsdevice修改为这个,进行校准后,就可以正常使用了