Linux系统LPT打印口批量产测工具

电子说

1.3w人已加入

描述

1 软件功能

该软件用于在Linux平台测试CH35X/CH38X(PCI/PCIe转串并口)的并口各引脚功能是否正常。方便对设备进行出厂测试。

2 并口测试硬件治具

在测试前,需要制作单独的硬件治具,按下表连接信号线:

PCIe

引脚连接示意图:

PCIe

PCIe

3 软件使用方法

  • 插入待测试PCI/PCIe转并口设备。
  • 输入lspci –v命令查看设备的枚举信息,若找到厂商ID为[1C00]或[4348]的设备,则表示已经正常识别到该硬件。

PCIePCIe

  • 通过lsmod命令查看设备关联驱动是否已加载,设备所需驱动包括:parport、parport_pc、lp驱动。驱动加载后,查看系统/dev目录,会多出parportX(X为数字)节点,示例:

PCIePCIe

  • 编译测试程序,生成目标可执行程序,输入./parport_test -D /dev/parport0 –h命令查看帮助,输出其他命令无效。运行程序:[可执行文件名] -D [设备节点名称] –s

PCIePCIe

4 测试错误码说明

根据输出的错误码和终端输出信息可判断故障信号线,下表为错误码和说明。

错误码 错误码说明
0 STB-D0通讯错误
1 AFD-D1通讯错误
2 INIT-D2通讯错误
3 SIN-D3通讯错误
4 D6-ERR通讯错误
5 D4-SELT通讯错误
6 D5-PE通讯错误
7 D6-ACK通讯错误
8 D7-BUSY通讯错误

5 测试实例

测试成功实例

PCIePCIe

测试错误实例

PCIePCIe

根据输出信息,D4-SELT、D5-PE、D6-ACK和D7-BUSY信号通讯存在错误。

PCIe

PCIe

根据输出信息,STB-D0信号通讯存在错误。

6、wchparporttest工具源码

/*
 * parport factory test utility.
 *
 * Copyright (C) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
 * Web:     http://wch.cn
 * Author:  WCH 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define GET_BIT(x, y)	     ((x >> y) & 0x01) /* get y bit value of x */
#define CTRL_REG_INITVAL_OUT 0xc0	       /* control reg value(allow D7-D0 output) */
#define CTRL_REG_INITVAL_IN  0xe0	       /* control reg value(forbid D7-D0 output) */

static unsigned char ctrlval = 0;
static char device_name[20];

/**
 * ctrl_reg_update: update PCR reg
 * @fd: file descriptor
 * @mask: bits to update
 * @val: 0->clear, 1->set
 */
void ctrl_reg_update(int fd, unsigned char mask, unsigned char val)
{
	int ret;

	ctrlval &= ~mask;
	ctrlval |= val & mask;

	ret = ioctl(fd, PPWCONTROL, &ctrlval);
	if (ret < 0) {
		perror("ioctl");
		return;
	}
}

/**
 * data_line_init: data signals direction initialization
 * @fd: file descriptor
 * @dir: direction value
 * 
 * The function return 0 if successful, others if fail.
 */
int data_line_init(int fd, int dir)
{
	int ret;

	if (dir == 0)
		ctrlval = CTRL_REG_INITVAL_IN;
	else
		ctrlval = CTRL_REG_INITVAL_OUT;

	ret = ioctl(fd, PPWCONTROL, &ctrlval);
	if (ret < 0) {
		perror("ioctl");
		return -1;
	}
	return 0;
}

/**
 * data_reg_read: read data signals
 * @fd: file descriptor
 * 
 * The function return positive if successful, negative if fail.
 */
unsigned char data_reg_read(int fd)
{
	int ret;
	unsigned char data_r;

	ret = ioctl(fd, PPRDATA, &data_r);
	if (ret < 0) {
		perror("ioctl");
		return -1;
	}
	return data_r;
}

/**
 * print_err_msg: print message according to error code
 * @err_code: error number
 * 
 */
static void print_err_msg(int err_code)
{
	switch (err_code) {
	case 0:
		printf("[error code: %d] STB-D0 ERROR
", err_code);
		break;
	case 1:
		printf("[error code: %d] AFD-D1 ERROR
", err_code);
		break;
	case 2:
		printf("[error code: %d] INIT-D2 ERROR
", err_code);
		break;
	case 3:
		printf("[error code: %d] SIN-D3 ERROR
", err_code);
		break;
	case 4:
		printf("[error code: %d] D6-ERR ERROR
", err_code);
		break;
	case 5:
		printf("[error code: %d] D4-SELT ERROR
", err_code);
		break;
	case 6:
		printf("[error code: %d] D5-PE ERROR
", err_code);
		break;
	case 7:
		printf("[error code: %d] D6-ACK ERROR
", err_code);
		break;
	case 8:
		printf("[error code: %d] D7-BUSY ERROR
", err_code);
		break;
	default:
		break;
	}
}

/**
 * check_result: detect whether the current register value matches the target value related bits, and print in case of error
 * @type: test type, 0x01: control line output, data line input, 0x02: data line output, status line input
 * @val: current register value
 * @cmp_val: target register value
 * 
 * The function return 0 if successful, negative if fail.
 */
static int check_result(int type, unsigned char val, unsigned char cmp_val)
{
	int i;
	int ret = 0;

	if (type == 0x01) {
		for (i = 0; i < 4; i++) {
			if (GET_BIT(val, i) != GET_BIT(cmp_val, i)) {
				ret = -1;
				print_err_msg(i);
			}
		}
	} else if (type == 0x02) {
		for (i = 3; i < 8; i++) {
			if (GET_BIT(val, i) != GET_BIT(cmp_val, i)) {
				ret = -1;
				print_err_msg(i + 1);
			}
		}
	} else {
		printf("type error.n");
		return -1;
	}

	return ret;
}

static const struct option lopts[] = {
	{ "device name", required_argument, 0, 'd' },
	{ "start test", no_argument, 0, 's' },
	{ "view the usage method", no_argument, 0, 'h' },
	{ NULL, 0, 0, 0 },
};

static void print_usage(const char *prog)
{
	printf("Usage: %s [-dsh]
", prog);
	puts("  -d device name
"
	     "  -s start test
"
	     "  -h view the usage method");
	exit(1);
}

static void help_msg(void)
{
	puts("Pin connection mode of parport test:
"
	     "test1(PDR OUT/PSR IN): D6-ERR D4-SELT D5-PE D6-ACK
"
	     "test2(PCR OUT/PIR IN): STB-D0 AFD-D1 INIT-D2 SIN-D3 D7-BUSY");
	exit(1);
}

static void parse_opts(int argc, char *argv[])
{
	char c;

	if (argc != 4)
		print_usage(argv[0]);

	while (1) {
		c = getopt_long(argc, argv, "d:sh", lopts, NULL);
		if (c == -1)
			break;

		switch (c) {
		case 'd':
			memcpy(device_name, argv[2], strlen(argv[2]));
			break;
		case 's':
			printf("start test...
");
			break;
		case 'h':
			help_msg();
			break;
		default:
			print_usage(argv[0]);
			break;
		}
	}
}

int main(int argc, char **argv)
{
	int fd;
	int retval;
	int ret_check1, ret_check2, ret_check3, ret_check4;
	unsigned char data_w, status_r;

	parse_opts(argc, argv);

	/* Open parport device */
	fd = open(device_name, O_RDWR);
	if (fd < 0) {
		perror("open");
		return -1;
	}

	/* Claim the port */
	if ((retval = ioctl(fd, PPCLAIM)) < 0) {
		perror("PPCLAIM failed");
		goto exit;
	}

	/* Set the direction of D0-D7 to output */
	if ((retval = data_line_init(fd, 0x01)) < 0)
		goto exit;

	/* Set D0-D7 output low level */
	data_w = 0x00;
	if ((retval = ioctl(fd, PPWDATA, &data_w)) < 0) {
		perror("PPWDATA ioctl");
		goto exit;
	}

	/* Read BUSY, ACK, PE, SELT and ERR signal, judge whether they are all low */
	if ((retval = ioctl(fd, PPRSTATUS, &status_r)) < 0) {
		perror("PPRSTATUS ioctl");
		goto exit;
	}
	ret_check1 = check_result(0x02, status_r, 0x87);

	/* Set D4-D7 output high level */
	data_w = 0xf0;
	if ((retval = ioctl(fd, PPWDATA, &data_w)) < 0) {
		perror("PPWDATA ioctl");
		goto exit;
	}

	/* Read BUSY, ACK, PE, SELT and ERR signal, judge whether they are all high */
	if ((retval = ioctl(fd, PPRSTATUS, &status_r)) < 0) {
		perror("PPRSTATUS ioctl");
		goto exit;
	}
	ret_check2 = check_result(0x02, status_r, 0x7b);

	/* Set the direction of D0-D7 to input */
	if ((retval = data_line_init(fd, 0x00)) < 0)
		goto exit;

	/* Set SIN, INIT, AFD and STB output low level */
	ctrl_reg_update(fd, 0x0f, 0x0b);

	/* Read D0-D4 signal, judge whether they are all low */
	ret_check3 = check_result(0x01, data_reg_read(fd), 0xf0);

	/* Set SIN, INIT, AFD and STB output high level */
	ctrl_reg_update(fd, 0x0f, 0x04);

	/* Read D0-D4 signal, judge whether they are all high */
	ret_check4 = check_result(0x01, data_reg_read(fd), 0xff);

	if ((ret_check1 == 0) && (ret_check2 == 0) && (ret_check3 == 0) && (ret_check4 == 0))
		printf("Parport pin test passed
");
	else
		printf("Parport pin test failed
");

exit:
	/* Release the port */
	if (ioctl(fd, PPRELEASE) < 0)
		perror("PPRELEASE failed");

	close(fd);

	return retval;
}

PCIe

​  审核编辑:汤梓红

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分