/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
1
#define _BSD_SOURCE
15 by Suren A. Chilingaryan
Infrastructure for event API
2
#define _IPECAMERA_MODEL_C
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
3
#include <sys/time.h>
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
4
#include <unistd.h>
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
5
#include <assert.h>
6
15 by Suren A. Chilingaryan
Infrastructure for event API
7
#include "../tools.h"
8
#include "../error.h"
9
#include "model.h"
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
10
11
#define ADDR_MASK 0x7F00
12
#define WRITE_BIT 0x8000
13
#define RETRIES 10
14
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
15
//ToDo: check bot 1 and 2 bits for READY
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
16
#define READ_READY_BIT 0x20000
17
#define READ_ERROR_BIT 0x40000
18
19
#define ipecamera_datacpy(dst, src, bank)   pcilib_datacpy(dst, src, 4, 1, bank->raw_endianess)
20
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
21
//#define IPECAMERA_SIMPLIFIED_READOUT
22
#define IPECAMERA_MULTIREAD
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
23
54 by Suren A. Chilingaryan
Support dynamic registers, support register offsets and multiregisters (bitmasks), list NWL DMA registers
24
//static pcilib_register_value_t ipecamera_bit_mask[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
25
54 by Suren A. Chilingaryan
Support dynamic registers, support register offsets and multiregisters (bitmasks), list NWL DMA registers
26
int ipecamera_read(pcilib_t *ctx, pcilib_register_bank_description_t *bank, pcilib_register_addr_t addr, pcilib_register_value_t *value) {
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
27
    uint32_t val, tmp[4];
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
28
    char *wr, *rd;
29
    struct timeval start, cur;
30
    int retries = RETRIES;
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
31
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
32
    assert(addr < 128);
33
    
39 by root
Move to new FPGA design
34
    wr =  pcilib_resolve_register_address(ctx, bank->bar, bank->write_addr);
35
    rd =  pcilib_resolve_register_address(ctx, bank->bar, bank->read_addr);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
36
    if ((!rd)||(!wr)) {
37
	pcilib_error("Error resolving addresses of read & write registers");
38
	return PCILIB_ERROR_INVALID_ADDRESS;
39
    }
40
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
41
    //printf("%i %x %p %p\n", addr,  val, wr, rd);
42
43
/*
44
#ifdef IPECAMERA_SIMPLIFIED_READOUT
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
45
    ipecamera_datacpy(tmp, rd, bank);
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
46
#endif
47
*/
48
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
49
retry:
50
    val = (addr << 8);
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
51
52
    ipecamera_datacpy(wr, &val, bank);
53
54
#ifdef IPECAMERA_SIMPLIFIED_READOUT
55
    usleep(PCILIB_REGISTER_TIMEOUT);
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
56
//    ipecamera_datacpy(tmp, rd, bank);
57
//    usleep(PCILIB_REGISTER_TIMEOUT);
58
    ipecamera_datacpy(wr, &val, bank);
59
    usleep(PCILIB_REGISTER_TIMEOUT);
60
//    ipecamera_datacpy(tmp, rd, bank);
61
//    usleep(PCILIB_REGISTER_TIMEOUT);
62
    ipecamera_datacpy(wr, &val, bank);
63
    usleep(PCILIB_REGISTER_TIMEOUT);
64
#endif /* IPECAMERA_SIMPLIFIED_READOUT */
65
    
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
66
    gettimeofday(&start, NULL);
67
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
68
#ifdef IPECAMERA_MULTIREAD
69
    usleep(PCILIB_REGISTER_TIMEOUT);
70
    pcilib_datacpy(tmp, rd, 4, 4, bank->raw_endianess);
71
    val = tmp[0];
72
#else /* IPECAMERA_MULTIREAD */
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
73
    ipecamera_datacpy(&val, rd, bank);
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
74
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
75
    while ((val & READ_READY_BIT) == 0) {
76
        gettimeofday(&cur, NULL);
77
	if (((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) > PCILIB_REGISTER_TIMEOUT) break;
78
	
79
	ipecamera_datacpy(&val, rd, bank);
80
    }
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
81
#endif /* IPECAMERA_MULTIREAD */
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
82
83
    if ((val & READ_READY_BIT) == 0) {
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
84
	if (--retries > 0) {
85
	    pcilib_warning("Timeout reading register value (CMOSIS %lu, status: %lx), retrying (try %i of %i)...", addr, val, RETRIES - retries, RETRIES);
86
	    goto retry;
87
	}
88
	pcilib_error("Timeout reading register value (CMOSIS %lu, status: %lx)", addr, val);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
89
	return PCILIB_ERROR_TIMEOUT;
90
    }
91
    
92
    if (val & READ_ERROR_BIT) {
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
93
/*	if (--retries > 0) {
94
	    pcilib_warning("Error reading register (CMOSIS %lu, status: %lx), retrying (try %i of %i)...", addr, val, RETRIES - retries, RETRIES);
95
	    goto retry;
96
	}*/
97
	pcilib_error("Error reading register value (CMOSIS %lu, status: %lx)", addr, val);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
98
	return PCILIB_ERROR_FAILED;
99
    }
100
101
    if (((val&ADDR_MASK) >> 8) != addr) {
102
	if (--retries > 0) {
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
103
	    pcilib_warning("Address verification failed during register read (CMOSIS %lu, status: %lx), retrying (try %i of %i)...", addr, val, RETRIES - retries, RETRIES);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
104
	    goto retry;
105
	}
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
106
	pcilib_error("Address verification failed during register read (CMOSIS %lu, status: %lx)", addr, val);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
107
	return PCILIB_ERROR_VERIFY;
108
    }
109
54 by Suren A. Chilingaryan
Support dynamic registers, support register offsets and multiregisters (bitmasks), list NWL DMA registers
110
//    *value = val&ipecamera_bit_mask[bits];
111
    *value = val&0xFF;
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
112
113
    return 0;
114
}
115
54 by Suren A. Chilingaryan
Support dynamic registers, support register offsets and multiregisters (bitmasks), list NWL DMA registers
116
int ipecamera_write(pcilib_t *ctx, pcilib_register_bank_description_t *bank, pcilib_register_addr_t addr, pcilib_register_value_t value) {
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
117
    uint32_t val, tmp[4];
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
118
    char *wr, *rd;
119
    struct timeval start, cur;
120
    int retries = RETRIES;
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
121
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
122
    assert(addr < 128);
123
    assert(value < 256);
124
    
39 by root
Move to new FPGA design
125
    wr =  pcilib_resolve_register_address(ctx, bank->bar, bank->write_addr);
126
    rd =  pcilib_resolve_register_address(ctx, bank->bar, bank->read_addr);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
127
    if ((!rd)||(!wr)) {
128
	pcilib_error("Error resolving addresses of read & write registers");
129
	return PCILIB_ERROR_INVALID_ADDRESS;
130
    }
131
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
132
    //printf("%i %x %p %p\n", addr,  val, wr, rd);
133
134
/*
135
#ifdef IPECAMERA_SIMPLIFIED_READOUT
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
136
    ipecamera_datacpy(tmp, rd, bank);
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
137
#endif
138
*/
139
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
140
retry:
141
    val = WRITE_BIT|(addr << 8)|(value&0xFF);
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
142
    ipecamera_datacpy(wr, &val, bank);
143
144
#ifdef IPECAMERA_SIMPLIFIED_READOUT
145
    usleep(PCILIB_REGISTER_TIMEOUT);
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
146
//    ipecamera_datacpy(tmp, rd, bank);
147
//    usleep(PCILIB_REGISTER_TIMEOUT);
148
    ipecamera_datacpy(wr, &val, bank);
149
    usleep(PCILIB_REGISTER_TIMEOUT);
150
//    ipecamera_datacpy(tmp, rd, bank);
151
//    usleep(PCILIB_REGISTER_TIMEOUT);
152
    ipecamera_datacpy(wr, &val, bank);
153
    usleep(PCILIB_REGISTER_TIMEOUT);
154
#endif /* IPECAMERA_SIMPLIFIED_READOUT */
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
155
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
156
    gettimeofday(&start, NULL);
157
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
158
#ifdef IPECAMERA_MULTIREAD
159
    usleep(PCILIB_REGISTER_TIMEOUT);
160
    pcilib_datacpy(tmp, rd, 4, 4, bank->raw_endianess);
161
    val = tmp[0];
162
#else /* IPECAMERA_MULTIREAD */
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
163
    ipecamera_datacpy(&val, rd, bank);
164
    while ((val & READ_READY_BIT) == 0) {
165
        gettimeofday(&cur, NULL);
166
	if (((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) > PCILIB_REGISTER_TIMEOUT) break;
167
	
168
	ipecamera_datacpy(&val, rd, bank);
169
    }
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
170
#endif /* IPECAMERA_MULTIREAD */
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
171
172
    if ((val & READ_READY_BIT) == 0) {
15 by Suren A. Chilingaryan
Infrastructure for event API
173
	if (--retries > 0) {
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
174
	    pcilib_warning("Timeout occured during register write (CMOSIS %lu, value: %lu, status: %lx), retrying (try %i of %i)...", addr, value, val, RETRIES - retries, RETRIES);
15 by Suren A. Chilingaryan
Infrastructure for event API
175
	    goto retry;
176
	}
177
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
178
	pcilib_error("Timeout writting register value (CMOSIS %lu, value: %lu, status: %lx)", addr, value, val);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
179
	return PCILIB_ERROR_TIMEOUT;
180
    }
181
    
182
    if (val & READ_ERROR_BIT) {
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
183
/*	if (--retries > 0) {
184
	    pcilib_warning("Register write has failed (CMOSIS %lu, value: %lu, status: %lx), retrying (try %i of %i)...", addr, value, val, RETRIES - retries, RETRIES);
185
	    goto retry;
186
	}*/
187
	pcilib_error("Error writting register value (CMOSIS %lu, value: %lu, status: %lx)", addr, value, val);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
188
	return PCILIB_ERROR_FAILED;
189
    }
190
191
    if (((val&ADDR_MASK) >> 8) != addr) {
192
	if (--retries > 0) {
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
193
	    pcilib_warning("Address verification failed during register write (CMOSIS %lu, value: %lu, status: %lx), retrying (try %i of %i)...", addr, value, val, RETRIES - retries, RETRIES);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
194
	    goto retry;
195
	}
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
196
	pcilib_error("Address verification failed during register write (CMOSIS %lu, value: %lu, status: %lx)", addr, value, val);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
197
	return PCILIB_ERROR_VERIFY;
198
    }
199
    
54 by Suren A. Chilingaryan
Support dynamic registers, support register offsets and multiregisters (bitmasks), list NWL DMA registers
200
    if ((val&0xFF/*&ipecamera_bit_mask[bits]*/) != value) {
201
	pcilib_error("Value verification failed during register read (CMOSIS %lu, value: %lu != %lu)", addr, val/*&ipecamera_bit_mask[bits]*/, value);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
202
	return PCILIB_ERROR_VERIFY;
203
    }
204
205
    //printf("%i\n", val&ipecamera_bit_mask[bits]);
206
207
    return 0;
208
}
15 by Suren A. Chilingaryan
Infrastructure for event API
209
210
211