comedilib/demo/eeprom_dump.c

120 lines
2.2 KiB
C
Raw Normal View History

2000-02-02 05:14:23 +00:00
/*
Dumps eeproms
*/
#include <stdio.h>
#include <comedilib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#include <stdlib.h>
2000-10-19 06:28:27 +00:00
#include "examples.h"
2000-02-02 05:14:23 +00:00
int read_eeprom(comedi_t *it,unsigned int **eeprom, struct parsed_options options);
2000-02-02 05:14:23 +00:00
void dump_eeprom(unsigned int *eeprom,int len);
comedi_t *device;
2000-02-02 05:14:23 +00:00
int main(int argc, char *argv[])
{
int len;
unsigned int *eeprom;
struct parsed_options options;
2000-02-02 05:14:23 +00:00
init_parsed_options(&options);
options.subdevice = -1;
parse_options(&options, argc, argv);
2000-02-02 05:14:23 +00:00
device = comedi_open(options.filename);
2000-02-02 05:14:23 +00:00
if(!device){
comedi_perror(options.filename);
exit(-1);
2000-02-02 05:14:23 +00:00
}
len = read_eeprom(device, &eeprom, options);
2000-02-02 05:14:23 +00:00
dump_eeprom(eeprom,len);
return 0;
}
int read_eeprom(comedi_t *it, unsigned int **eeprom, struct parsed_options options)
2000-02-02 05:14:23 +00:00
{
int n,i,ret;
2000-02-02 05:14:23 +00:00
lsampl_t data;
unsigned int *ptr;
lsampl_t maxdata;
if(options.subdevice < 0)
{
options.subdevice = comedi_find_subdevice_by_type(it, COMEDI_SUBD_MEMORY, 0);
if(options.subdevice < 0){
fprintf(stderr,"No memory subdevice\n");
return 0;
}
2000-02-02 05:14:23 +00:00
}
n = comedi_get_n_channels(it, options.subdevice);
maxdata = comedi_get_maxdata(it, options.subdevice, 0);
2000-02-02 05:14:23 +00:00
if(maxdata != 0xff){
fprintf(stderr,"Demo only supports 8-bit memory subdevice has strange maxdata, aborting\n");
exit(-1);
2000-02-02 05:14:23 +00:00
}
ptr = malloc(sizeof(unsigned int) * n);
2000-02-02 05:14:23 +00:00
for(i = 0; i < n; i++){
ret = comedi_data_read(it, options.subdevice, i, 0, 0, &data);
ptr[i] = data;
if(ret < 0){
2000-02-02 05:14:23 +00:00
comedi_perror("comedi_data_read");
return 0;
}
}
*eeprom=ptr;
return n;
}
void dump_eeprom(unsigned int *eeprom,int len)
{
int i, j, c;
for (i = 0; i < len - 16; i+=16) {
printf("%04x: ",i);
for (j = 0; j < 16; j++) {
printf("%02x", eeprom[i + j] & 0xff);
}
printf(" ");
for (j = 0; j < 16; j++) {
c = eeprom[i + j] & 0xff;
printf("%c", isprint(c) ? c : '.');
}
printf("\n");
}
if(i==len)return;
printf("%04x: ",i);
for (j = 0; j < len-i; j++) {
printf("%02x", eeprom[i + j] & 0xff);
}
for(;j<16;j++){
printf(" ");
}
printf(" ");
for (j = 0; j < len-i; j++) {
c = eeprom[i + j] & 0xff;
printf("%c", isprint(c) ? c : '.');
}
for(;j<16;j++){
printf(" ");
}
printf("\n");
}