/*
* RTC sample&test code.
*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include "test.h"
int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,
115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, };
void writeFile(int result)
{
char s[] = "result.txt";
int fd = open(s, O_RDWR | O_APPEND);
char buf[100];
memset(buf, 0, sizeof(buf));
if(result==1)
{
strcpy(buf, "LED PASS\n");
}else{
strcpy(buf, "LED FAIL \n");
}
write(fd, buf, strlen(buf));
memset(buf, 0, sizeof(buf));
close(fd);
}
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
/**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄
*@param databits 类型 int 数据位 取值 为 7 或者8
*@param stopbits 类型 int 停止位 取值为 1 或者2
*@param parity 类型 int 效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity,int speed)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) {
perror("SetupSerial 1");
return(FALSE);
}
set_speed(fd,speed);
options.c_cflag &= ~CSIZE; //用数据位掩码清空数据位设置
options.c_cflag |= CS8; /*设置数据位数8bit*/
options.c_cflag |= CLOCAL |CREAD; //通过位掩码的方式激活本地连接和接受使能选项
//设置校验方式
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* 清空parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
/* 设置停止位1byte*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*使用原始输出,就是禁用输出处理,使数据能不经过处理、过滤地完整地输出到串口接口。*/
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
//只是串口传输数据,而不需要串口来处理,那么使用原始模式(Raw Mode)方式来通讯,设置方式如下:
//在原始模式下,串口输入数据是不经过处理的,在串口接口接收的数据被完整保留。要使串口设备工作在原始模式,需要关闭ICANON、ECHO、ECHOE和ISIG选项,其操作方法如下:
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*使用原始输出,就是禁用输出处理,使数据能不经过处理、过滤地完整地输出到串口接口。*/
return (TRUE);
}
//
static int _atoul(const char *str, unsigned char *pvalue)
{
unsigned int result=0;
while (*str)
{
if (isdigit((int)*str))
{
if ((result<429496729) || ((result==429496729) && (*str<'6')))
{
result = result*10 + (*str)-48;
}
else
{
*pvalue = (char)result;
return -1;
}
}
else
{
*pvalue=result;
return -1;
}
str++;
}
*pvalue=result;
return 0;
}
#define ASC2NUM(ch) (ch - '0')
#define HEXASC2NUM(ch) (ch - 'A' + 10)
static int _atoulx(const char *str, unsigned char *pvalue)
{
unsigned int result=0;
unsigned char ch;
while (*str)
{
ch=toupper(*str);
if (isdigit(ch) || ((ch >= 'A') && (ch <= 'F' )))
{
if (result < 0x10000000)
{
result = (result << 4) + ((ch<='9')?(ASC2NUM(ch)):(HEXASC2NUM(ch)));
}
else
{
*pvalue=result;
return -1;
}
}
else
{
*pvalue=result;
return -1;
}
str++;
}
*pvalue=result;
return 0;
}
/*used for convert hex value from string to int*/
static int str_to_num(const char *str, unsigned char *pvalue)
{
if ( *str == '0' && (*(str+1) == 'x' || *(str+1) == 'X') ){
if (*(str+2) == '\0'){
return -1;
}
else{
return _atoulx(str+2, pvalue);
}
}
else {
return _atoul(str,pvalue);
}
}
/*used for convert time frome string to struct rtc_time_t*/
static int parse_string(char *string, rtc_time_t *p_tm)
{
char *comma, *head;
int value[10];
int i;
if (!string || !p_tm)
return -1;
if (!strchr(string, '/'))
return -1;
head = string;
i = 0;
comma = NULL;
for(;;) {
comma = strchr(head, '/');
if (!comma){
value[i++] = atoi(head);
break;
}
*comma = '\0';
value[i++] = atoi(head);
head = comma+1;
}
if (i < 5)
return -1;
p_tm->year = value[0];
p_tm->month = value[1];
p_tm->date = value[2];
p_tm->hour = value[3];
p_tm->minute = value[4];
p_tm->second = value[5];
p_tm->weekday = 0;
return 0;
}
static int test_rtc(int num,const char * time)
{
rtc_time_t tm;
reg_data_t regv;
reg_temp_mode_t mode;
int ret = -1;
int fd = -1;
const char *dev_name = "/dev/hi_rtc";
char string[50] = {0};
memset(&tm, 0, sizeof(tm));
fd = open(dev_name, O_RDWR);
if (fd < 0) {
printf("open %s failed\n", dev_name);
return -1;
}
if(num == 0){
strncpy(string, time, sizeof(string)-1);
ret = parse_string(string, &tm);
ret = ioctl(fd, HI_RTC_SET_TIME, &tm);
} else {
ret = ioctl(fd, HI_RTC_RD_TIME, &tm);
printf("%d%d%d%d%d%d%d\n", tm.year, tm.month, tm.weekday, tm.date, tm.hour, tm.minute,tm.second);
}
close(fd);
}
static int test_gps(void)
{
int fd, res;
char *date = NULL;
char *value = "GPRMC";
date=(char *)malloc(256);
const char *dev_name = "/dev/hi_rtc";
fd = open(dev_name, O_RDWR);
if (fd < 0) {
return 1 ;
}
res = read(fd, date, 255);
if (date!=NULL)
{
//writeFile(1);
printf("PASS\n");
}
else
{
//writeFile(0);
printf("FAIL\n");
}
close(fd);
return 0;
}
static int test_rs485(void)
{
int fd, c=0, res,flag,val;
char *date = NULL;
char *p =NULL;
char *value = "U";
fd = open(UART_DEVICE, O_RDWR);
if (set_Parity(fd,8,1,'N',9600) == FALSE) {
printf("Set Parity Error\n");
return 1 ;
}
date=(char *)malloc(32);
ioctl(fd, TIOCMGET, &flag);
flag|= TIOCM_RTS;
ioctl(fd, TIOCMSET, &flag);
res = read(fd, date, 31);
p=strstr(date,value);
if (p!=NULL)
{
printf("PASS\n");
} else {
printf("FAIL\n");
}
close(fd);
return 0;
}
static int test_led(void)
{
int ret, fd,val,result;
fd = open("/dev/gpio", 0);
if (fd < 0)
{
return -1;
}
result=0;
ret = ioctl(fd, GET_LED3_STATE,&val);
//printf("val=%d\n",val);
result= (result | val );
ret = ioctl(fd, GET_LED4_STATE,&val);
//printf("val=%d\n",val);
result= (result | val );
ret = ioctl(fd, GET_LED_RUN_STATE,&val);
//printf("val=%d\n",val);
result= (result | val );
if(result==1){
writeFile(0);
}else{
writeFile(1);
}
close(fd);
}
static void test_key(void )
{
int fd = -1;
int ret,i,val,fd1;
struct input_event t;
fd = open("/dev/input/event0", O_RDWR);
fd1 = open("/dev/gpio", 0);
//printf("please press key !\n");
for(i=0;i<10;i++){
ret = read(fd,&t,sizeof(t));
if (ret< 0) {
perror("read key:");
}
//printf("key_type = %d\n", t.type);
//printf("key_value = %d\n", t.value);//1: key up 0:key down
//printf("key_code = %d\n", t.code);// 250 : key2 249 : key1 0: report finish
switch (t.code) {
case KEY1:
if (t.value) {
printf("KEY 1 up !\n");
//ret = ioctl(fd1, GET_KEY1_STATE,&val);
} else {
printf("KEY 1 down !\n");
//ret = ioctl(fd1, GET_KEY1_STATE,&val);
}
break;
case KEY2:
if (t.value) {
printf("KEY 2 up !\n");
//ret = ioctl(fd1, GET_KEY2_STATE,&val);
} else {
printf("KEY 2 down !\n");
//ret = ioctl(fd1, GET_KEY2_STATE,&val);
}
break;
case KEY_SYNC:
break;
default:
printf("Get key info error , please check again!\n");
}
}
//writeFile(1);
close(fd);
}
static void light_led(void)
{
int ret, fd,val,result;
fd = open("/dev/gpio", 0);
ret = ioctl(fd, LED3_ON,&val);
ret = ioctl(fd, LED4_ON,&val);
ret = ioctl(fd, LED_RUN_ON,&val);
sleep(8);
ret = ioctl(fd, LED3_OFF,&val);
ret = ioctl(fd, LED4_OFF,&val);
ret = ioctl(fd, LED_RUN_OFF,&val);
}
int main(int argc, const char *argv[])
{
static int ret = -1;
const char* time = "ok";
switch (*argv[1])
{
case '0':/* rtc*/
if (!strcmp(argv[2],"-s")) {
ret = test_rtc(0,argv[3]);
}else{
ret = test_rtc(1,time);
}
break;
case '1':
test_gps();
break;
case '2':
test_rs485();
break;
case '3':
test_led();
break;
case '4':
test_key();
break;
case '5':
light_led();
break;
default:
printf("the index is invaild!\n");
}
return 0;
}
新路程------ hi3516 test 工厂功能测试
最新推荐文章于 2020-07-15 09:44:38 发布