1. 程式人生 > >新路程------ hi3516 test 工廠功能測試

新路程------ hi3516 test 工廠功能測試

/*
 * 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;
}