该软件用于在Linux平台测试CH35X/CH38X(PCI/PCIe转串并口)的并口各引脚功能是否正常。方便对设备进行出厂测试。
在测试前,需要制作单独的硬件治具,按下表连接信号线:
25针并口座子堵头硬件连线 |
||||
引脚名 |
引脚号 |
引脚名 |
引脚号 |
|
BUSY |
11 |
---------------------- |
D7 |
9 |
ACK |
10 |
---------------------- |
D6 |
8 |
PE |
12 |
---------------------- |
D5 |
7 |
SELT |
13 |
---------------------- |
D4 |
6 |
ERR |
15 |
---------------------- |
D6 |
8 |
SIN |
17 |
---------------------- |
D3 |
5 |
INIT |
16 |
---------------------- |
D2 |
4 |
AFD |
14 |
---------------------- |
D1 |
3 |
STB |
1 |
---------------------- |
D0 |
2 |
引脚连接示意图:
根据输出的错误码和终端输出信息可判断故障信号线,下表为错误码和说明。
错误码 |
错误码说明 |
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通讯错误 |
测试成功实例
测试错误实例
根据输出信息,D4-SELT、D5-PE、D6-ACK和D7-BUSY信号通讯存在错误。
根据输出信息,STB-D0信号通讯存在错误。
/*
* parport factory test utility.
*
* Copyright (C) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
* Web: http://wch.cn
* Author: WCH
*
* 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.
*
* Cross-compile with cross-gcc -I /path/to/cross-kernel/include
*
* V1.0 - initial version
*
*/
#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\n", err_code);
break;
case 1:
printf("[error code: %d] AFD-D1 ERROR\n", err_code);
break;
case 2:
printf("[error code: %d] INIT-D2 ERROR\n", err_code);
break;
case 3:
printf("[error code: %d] SIN-D3 ERROR\n", err_code);
break;
case 4:
printf("[error code: %d] D6-ERR ERROR\n", err_code);
break;
case 5:
printf("[error code: %d] D4-SELT ERROR\n", err_code);
break;
case 6:
printf("[error code: %d] D5-PE ERROR\n", err_code);
break;
case 7:
printf("[error code: %d] D6-ACK ERROR\n", err_code);
break;
case 8:
printf("[error code: %d] D7-BUSY ERROR\n", 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]\n", prog);
puts(" -d device name\n"
" -s start test\n"
" -h view the usage method");
exit(1);
}
static void help_msg(void)
{
puts("Pin connection mode of parport test:\n"
"test1(PDR OUT/PSR IN): D6-ERR D4-SELT D5-PE D6-ACK\n"
"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...\n");
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\n");
else
printf("Parport pin test failed\n");
exit:
/* Release the port */
if (ioctl(fd, PPRELEASE) < 0)
perror("PPRELEASE failed");
close(fd);
return retval;
}