~ubuntu-branches/ubuntu/feisty/comedilib/feisty

« back to all changes in this revision

Viewing changes to demo/eeprom_dump.c

  • Committer: Bazaar Package Importer
  • Author(s): David Schleef
  • Date: 2003-09-23 18:11:12 UTC
  • Revision ID: james.westby@ubuntu.com-20030923181112-sat05jyh702rb1at
Tags: upstream-0.7.21
ImportĀ upstreamĀ versionĀ 0.7.21

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Dumps eeproms
 
3
 */
 
4
 
 
5
#include <stdio.h>
 
6
#include <comedilib.h>
 
7
#include <fcntl.h>
 
8
#include <unistd.h>
 
9
#include <errno.h>
 
10
#include <getopt.h>
 
11
#include <ctype.h>
 
12
#include <stdlib.h>
 
13
#include "examples.h"
 
14
 
 
15
int read_eeprom(comedi_t *it,unsigned int **eeprom);
 
16
void dump_eeprom(unsigned int *eeprom,int len);
 
17
 
 
18
comedi_t *device;
 
19
 
 
20
int main(int argc, char *argv[])
 
21
{
 
22
        int len;
 
23
        unsigned int *eeprom;
 
24
 
 
25
        parse_options(argc,argv);
 
26
 
 
27
        device=comedi_open(filename);
 
28
        if(!device){
 
29
                comedi_perror(filename);
 
30
                exit(0);
 
31
        }
 
32
 
 
33
        len=read_eeprom(device,&eeprom);
 
34
        dump_eeprom(eeprom,len);
 
35
 
 
36
        return 0;
 
37
}
 
38
 
 
39
 
 
40
 
 
41
 
 
42
int read_eeprom(comedi_t *it,unsigned int **eeprom)
 
43
{
 
44
        int subd;
 
45
        int n,i,ret;
 
46
        lsampl_t data;
 
47
        unsigned int *ptr;
 
48
        lsampl_t maxdata;
 
49
 
 
50
        subd=comedi_find_subdevice_by_type(it,COMEDI_SUBD_MEMORY,0);
 
51
        if(subd<0){
 
52
                fprintf(stderr,"No memory subdevice\n");
 
53
                return 0;
 
54
        }
 
55
 
 
56
        n=comedi_get_n_channels(it,subd);
 
57
        maxdata=comedi_get_maxdata(it,subd,0);
 
58
 
 
59
        if(maxdata!=0xff){
 
60
                fprintf(stderr,"Memory subdevice has strange maxdata, aborting\n");
 
61
        }
 
62
 
 
63
        ptr=malloc(sizeof(unsigned int)*n);
 
64
 
 
65
        for(i=0;i<n;i++){
 
66
                ret=comedi_data_read(it,subd,i,0,0,&data);
 
67
                ptr[i]=data;
 
68
                if(ret<0){
 
69
                        comedi_perror("comedi_data_read");
 
70
                        return 0;
 
71
                }
 
72
        }
 
73
 
 
74
        *eeprom=ptr;
 
75
        return n;
 
76
 
 
77
}
 
78
 
 
79
void dump_eeprom(unsigned int *eeprom,int len)
 
80
{
 
81
        int i, j, c;
 
82
 
 
83
        for (i = 0; i < len - 16; i+=16) {
 
84
                printf("%04x: ",i);
 
85
                for (j = 0; j < 16; j++) {
 
86
                        printf("%02x", eeprom[i + j] & 0xff);
 
87
                }
 
88
                printf("  ");
 
89
                for (j = 0; j < 16; j++) {
 
90
                        c = eeprom[i + j] & 0xff;
 
91
                        printf("%c", isprint(c) ? c : '.');
 
92
                }
 
93
                printf("\n");
 
94
        }
 
95
        if(i==len)return;
 
96
        printf("%04x: ",i);
 
97
        for (j = 0; j < len-i; j++) {
 
98
                printf("%02x", eeprom[i + j] & 0xff);
 
99
        }
 
100
        for(;j<16;j++){
 
101
                printf("  ");
 
102
        }
 
103
        printf("  ");
 
104
        for (j = 0; j < len-i; j++) {
 
105
                c = eeprom[i + j] & 0xff;
 
106
                printf("%c", isprint(c) ? c : '.');
 
107
        }
 
108
        for(;j<16;j++){
 
109
                printf(" ");
 
110
        }
 
111
        printf("\n");
 
112
}
 
113