⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ccio.c

📁 linux 嵌入式原代码
💻 C
字号:
/* *  Sample program for FR400 Companion Chip IO axLinux driver. *   *  Copyright (C) 2003  AXE,Inc. * */#include <stdio.h>#include <stdlib.h>#include <time.h>#include <fcntl.h>#include <unistd.h>#include <linux/fr400cc_io.h>static voidhelp(void){	printf("ccio -cmd <r | w | r-mask | w-mask | r-seq | w-seq>\n");	printf("     -offset 0XXX {-value <DDD | 0xHHH>}\n");	printf("     {-mask <DDD | 0xHHH>}\n");	printf("     {-size-bytes <DDD | 0xHHH}\n");	printf("     {-w-seq-data <DDD | 0xHH> <DDD | 0xHH> ... }\n");	printf("     {-w-seq-data4 <DDD | 0xHHHHHHHH> <DDD | 0xHHHHHHHH> ... }\n");	printf("ccio <-help | --help | -?>\n");}static intopt_chk(int ac, char **av, char *key){	int i;	for(i=1; i<ac; i++){		if(strcmp(av[i], key) == 0) return i;	}	return 0;}static intopt_chk_str(int ac, char **av, char *key, char **re){	int i;	i = opt_chk(ac, av, key);	if(i!=0 && i+1<ac){		*re = av[i+1];		return 0;	}	return -1;}static char *opt_list[] = {"cmd", "offset", "value", "mask", "size-bytes", "w-seq-data", "w-seq-data4","help","-help","?",};static intoptchk_top(int ac, char **av){	int i, j, n, ret;	ret = 0;	n = sizeof(opt_list) / sizeof(opt_list[0]);	for(i=1; i<ac; i++){		if(av[i][0] != '-') continue;		if('0' <= av[i][1] && av[i][1] <= '9') continue;		for(j=0; j<n; j++){			if(strcmp(&av[i][1], opt_list[j]) == 0) break;		}		if(j<n) continue;		fprintf(stderr, "ignore option '%s' ?\n", av[i]);		ret = -1;	}	return ret;}static intioc_cmd_get(char *str, int *rf){	if(str == NULL) return -1;	if(str[0] == 'r') *rf = 1;	if(strcmp(str, "r") == 0) return CCIO_READ;	if(strcmp(str, "w") == 0) return CCIO_WRITE;	if(strcmp(str, "r-mask") == 0) return CCIO_READ_MASK;	if(strcmp(str, "w-mask") == 0) return CCIO_WRITE_MASK;	if(strcmp(str, "r-seq") == 0) return CCIO_READ_SEQ;	if(strcmp(str, "w-seq") == 0) return CCIO_WRITE_SEQ;	return -1;}static ints2val(char *s, unsigned *re){	unsigned v;	int cnt, neg, d;	if(s == NULL) return -2;	if(strncmp(s, "0x", 2) == 0){		if((cnt = sscanf(s, "0x%x", &v)) != 1) return -1;		*re = v;		return 0;	}	neg = 0;	if(s[0] == '-'){		neg = 1;				s++;	}	if((cnt = sscanf(s, "%d", &d)) != 1) return -1;	if(neg) d = -d;	*re = (unsigned)d;	return 0;}static intopt_chk_val(int ac, char **av, char *key, unsigned *re){	char *str;	if(opt_chk_str(ac, av, key, &str) != 0) return -1;	if(s2val(str, re) != 0) return -2;	return 0;}static voiddump(unsigned char *buf, int sz, int offset){	int i;	unsigned v;	for(i=0; i<sz; ){		if(i % 16 == 0) printf("%08x : ", offset+i);		if(i+3 < sz){			bcopy(buf+i, &v, 4);			printf("%08x", v);			i += 4;		}else{			printf("%02x", buf[i]);			i++;		}		printf("%s", i%16==0 ? "\n" : " ");	}}main(int ac, char **av){	char *str;	int ioc_cmd, rf;	unsigned offset, value, mask, v;	int size_bytes;	int fd, open_flag, ret;	struct fr400cc_io prm;	unsigned char *buf;	if(optchk_top(ac, av) != 0) return -1;	if(opt_chk(ac, av, "-help") ||	   opt_chk(ac, av, "--help") ||	   opt_chk(ac, av, "-?")){		help();		return 0;	}	str = NULL;	if(opt_chk_str(ac, av, "-cmd", &str) != 0){		fprintf(stderr, "no -cmd option ?\n");		return -1;	}	if((ioc_cmd = ioc_cmd_get(str, &rf)) == -1){		fprintf(stderr, "bad -cmd option ?\n");		return -1;	}	str = NULL;	switch(opt_chk_val(ac, av, "-offset", &offset)){	case -1:		fprintf(stderr, "no -offset option ?\n");		return -1;	case -2:		fprintf(stderr, "-offset option bad value?\n");		return -1;	}	value = 0;	switch(opt_chk_val(ac, av, "-value", &value)){	case -2:		fprintf(stderr, "-value option bad balue?\n");		return -1;	}	mask = 0xffffffff;	switch(opt_chk_val(ac, av, "-mask", &mask)){	case -2:		fprintf(stderr, "-mask option bad balue?\n");		return -1;	}	size_bytes = 0;	switch(opt_chk_val(ac, av, "-size-bytes", &v)){	case -2:		fprintf(stderr, "-size-bytes option bad balue?\n");		return -1;	default:		size_bytes = (int)v;		break;	}		buf = NULL;	switch(ioc_cmd){	case CCIO_READ_SEQ:	case CCIO_WRITE_SEQ:		if((buf = malloc(size_bytes)) == NULL){			fprintf(stderr, "fail alloc %d bytes\n", size_bytes);			return -1;		}		break;	}	bzero(buf, size_bytes);	if(ioc_cmd == CCIO_WRITE_SEQ){		int idx, i, s4, mod;		if((idx = opt_chk(ac, av, "-w-seq-data4")) != 0){			s4 = size_bytes / 4;			mod = size_bytes % 4;			if(idx + s4 + (mod > 0) < ac){				for(i=0; i<s4; i++){					if(s2val(av[idx+1+i], &v) != 0){						fprintf(stderr, "-w-seq-data4 option bad value?\n");						return -1;					}					bcopy(&v, buf + i*4, 4);				}				if(mod > 0){					if(s2val(av[idx+1+i], &v) != 0){						fprintf(stderr, "-w-seq-data4 option bad value?\n");						return -1;					}					bcopy(&v, buf + i*4, mod);				}			}		}else if((idx = opt_chk(ac, av, "-w-seq-data")) != 0){			if(idx + size_bytes < ac){				for(i=0; i<size_bytes; i++){					if(s2val(av[idx+1+i], &v) != 0){						fprintf(stderr, "-w-seq-data option bad value?\n");						return -1;					}					buf[i] = (unsigned char)v;				}			}		}	}	open_flag = O_RDONLY;	if((fd = open("/dev/fr400cc_io", open_flag)) < 0){		fprintf(stderr, "open err fd=%d\n", fd);		return -1;	}	bzero(&prm, sizeof(prm));	prm.offset = offset;	prm.value = value;	prm.mask = mask;	prm.size_bytes = size_bytes;	prm.buffer = buf;	if((ret = ioctl(fd, ioc_cmd, &prm)) != 0){		fprintf(stderr, "ioctl err %d\n", ret);		return -1;	}	switch(ioc_cmd){	case CCIO_READ:	case CCIO_READ_MASK:		printf("offset=0x%08x value=0x%08x\n",			prm.offset, prm.value);		break;	case CCIO_READ_SEQ:		dump(buf, prm.size_bytes, prm.offset);		break;	}	close(fd);	printf("OK fin.\n");	return 0;}/* EOF */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -