/* madc.c Copyright (C) 2016 Pali Rohár 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 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include #include #define TWL4030_MADC_PATH "/dev/twl4030-adc" #define TWL4030_MADC_IOC_MAGIC '`' #define TWL4030_MADC_IOCX_ADC_RAW_READ _IO(TWL4030_MADC_IOC_MAGIC, 0) struct twl4030_madc_user_parms { int channel; int average; int status; unsigned short int result; }; int main(int argc, char *argv[]) { int fd; int ret; int channel; char *endptr; struct twl4030_madc_user_parms par; if (argc != 2) { fprintf(stderr, "Usage: %s channel\n", argv[0]); return 1; } channel = strtol(argv[1], &endptr, 10); if (channel < 0 || channel > 14 || *endptr != 0) { fprintf(stderr, "Incorrect channel number\n"); return 1; } fd = open(TWL4030_MADC_PATH, O_RDONLY); if (fd < 0) { perror("Cannot open file " TWL4030_MADC_PATH); return 1; } par.channel = channel; par.average = 1; ret = ioctl(fd, TWL4030_MADC_IOCX_ADC_RAW_READ, &par); close(fd); if (ret != 0 || par.status != 0) { fprintf(stderr, "Reading %d channel failed\n", channel); return 1; } printf("Channel %d: %d\n", channel, par.result); return 0; }