• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libiio - Library for interfacing industrial I/O (IIO) devices
3  *
4  * Copyright (C) 2015 Analog Devices, Inc.
5  * Author: Paul Cercueil <paul.cercueil@analog.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * */
18 
19 #include <errno.h>
20 #include <iio.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 
write_reg(const char * name,unsigned long addr,unsigned long val)24 static int write_reg(const char *name, unsigned long addr, unsigned long val)
25 {
26 	struct iio_device *dev;
27 	struct iio_context *ctx;
28 	int ret;
29 
30 	ctx = iio_create_default_context();
31 	if (!ctx) {
32 		perror("Unable to create context");
33 		return EXIT_FAILURE;
34 	}
35 
36 	dev = iio_context_find_device(ctx, name);
37 	if (!dev) {
38 		errno = ENODEV;
39 		perror("Unable to find device");
40 		goto err_destroy_context;
41 	}
42 
43 	ret = iio_device_reg_write(dev, addr, val);
44 	if (ret < 0) {
45 		errno = -ret;
46 		perror("Unable to write register");
47 		goto err_destroy_context;
48 	}
49 
50 	iio_context_destroy(ctx);
51 	return EXIT_SUCCESS;
52 
53 err_destroy_context:
54 	iio_context_destroy(ctx);
55 	return EXIT_FAILURE;
56 }
57 
read_reg(const char * name,unsigned long addr)58 static int read_reg(const char *name, unsigned long addr)
59 {
60 	struct iio_device *dev;
61 	struct iio_context *ctx;
62 	uint32_t val;
63 	int ret;
64 
65 	ctx = iio_create_default_context();
66 	if (!ctx) {
67 		perror("Unable to create context");
68 		return EXIT_FAILURE;
69 	}
70 
71 	dev = iio_context_find_device(ctx, name);
72 	if (!dev) {
73 		errno = ENODEV;
74 		perror("Unable to find device");
75 		goto err_destroy_context;
76 	}
77 
78 	ret = iio_device_reg_read(dev, addr, &val);
79 	if (ret < 0) {
80 		errno = -ret;
81 		perror("Unable to read register");
82 		goto err_destroy_context;
83 	}
84 
85 	printf("0x%x\n", val);
86 	iio_context_destroy(ctx);
87 	return EXIT_SUCCESS;
88 
89 err_destroy_context:
90 	iio_context_destroy(ctx);
91 	return EXIT_FAILURE;
92 }
93 
main(int argc,char ** argv)94 int main(int argc, char **argv)
95 {
96 	unsigned long addr;
97 
98 	if (argc < 3 || argc > 4) {
99 		printf("Usage:\n\niio_reg <device> <register> [<value>]\n");
100 		return 0;
101 	}
102 
103 	addr = strtoul(argv[2], NULL, 0);
104 
105 	if (argc == 3) {
106 		return read_reg(argv[1], addr);
107 	} else {
108 		unsigned long val = strtoul(argv[3], NULL, 0);
109 		return write_reg(argv[1], addr, val);
110 	}
111 }
112