/alps/ipecamera

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/ipecamera
277 by Suren A. Chilingaryan
Build RPM
1
#define _DEFAULT_SOURCE
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
2
#define _BSD_SOURCE
15 by Suren A. Chilingaryan
Infrastructure for event API
3
#define _IPECAMERA_MODEL_C
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
4
#include <stdio.h>
5
#include <stdlib.h>
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
6
#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
7
#include <unistd.h>
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
8
#include <assert.h>
9
245 by Suren A. Chilingaryan
First stand-alone ipecamera implementation
10
#include <pcilib.h>
11
#include <pcilib/tools.h>
12
#include <pcilib/error.h>
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
13
#include <pcilib/locking.h>
14
#include <pcilib/model.h>
276 by Suren A. Chilingaryan
Update to new version of pcitool
15
#include <pcilib/datacpy.h>
245 by Suren A. Chilingaryan
First stand-alone ipecamera implementation
16
17
#include "cmosis.h"
18
#include "private.h"
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
19
20
#define ADDR_MASK 0x7F00
21
#define WRITE_BIT 0x8000
22
#define RETRIES 10
23
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
24
//ToDo: check bot 1 and 2 bits for READY
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
25
#define READ_READY_BIT 0x20000
26
#define READ_ERROR_BIT 0x40000
27
28
#define ipecamera_datacpy(dst, src, bank)   pcilib_datacpy(dst, src, 4, 1, bank->raw_endianess)
29
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
30
//#define IPECAMERA_SIMPLIFIED_READOUT
282 by Suren A. Chilingaryan
Support HighFlex-based ipecamera
31
#define IPECAMERA_RETRY_ERRORS
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
32
#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
33
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
34
35
typedef struct ipecamera_cmosis_context_s ipecamera_cmosis_context_t;
36
37
struct ipecamera_cmosis_context_s {
38
    pcilib_register_bank_context_t bank_ctx;	/**< the bank context associated with the software registers */
39
    pcilib_lock_t *lock;			/**< the lock to serialize access through GPIO */
40
};
41
42
void ipecamera_cmosis_close(pcilib_t *ctx, pcilib_register_bank_context_t *reg_bank_ctx) {
43
	ipecamera_cmosis_context_t *bank_ctx = (ipecamera_cmosis_context_t*)reg_bank_ctx;
44
45
	if (bank_ctx->lock)
46
	    pcilib_return_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, bank_ctx->lock);
47
	free(bank_ctx);
48
}
49
50
pcilib_register_bank_context_t* ipecamera_cmosis_open(pcilib_t *ctx, pcilib_register_bank_t bank, const char* model, const void *args) {
51
	ipecamera_cmosis_context_t *bank_ctx;
52
53
	bank_ctx = calloc(1, sizeof(ipecamera_cmosis_context_t));
54
	if (!bank_ctx) {
55
	    pcilib_error("Memory allocation for bank context has failed");
56
	    return NULL;
57
	}
58
59
	bank_ctx->lock = pcilib_get_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, "cmosis");
60
	if (!bank_ctx->lock) {
61
	    ipecamera_cmosis_close(ctx, (pcilib_register_bank_context_t*)bank_ctx);
62
	    pcilib_error("Failed to initialize a lock to protect CMOSIS register bank");
63
	    return NULL;
64
	}
65
66
	return (pcilib_register_bank_context_t*)bank_ctx;
67
}
68
69
int ipecamera_cmosis_read(pcilib_t *ctx, pcilib_register_bank_context_t *reg_bank_ctx, pcilib_register_addr_t addr, pcilib_register_value_t *value) {
70
    int err;
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
71
    uint32_t val, tmp[4];
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
72
    char *wr, *rd;
252 by Suren A. Chilingaryan
Cleanup
73
    struct timeval start;
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
74
    int retries = RETRIES;
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
75
    ipecamera_cmosis_context_t *bank_ctx = (ipecamera_cmosis_context_t*)reg_bank_ctx;
76
    const pcilib_register_bank_description_t *bank = reg_bank_ctx->bank;
77
    
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
78
    assert(addr < 128);
79
    
276 by Suren A. Chilingaryan
Update to new version of pcitool
80
    wr =  pcilib_resolve_bar_address(ctx, bank->bar, bank->write_addr);
81
    rd =  pcilib_resolve_bar_address(ctx, bank->bar, bank->read_addr);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
82
    if ((!rd)||(!wr)) {
83
	pcilib_error("Error resolving addresses of read & write registers");
84
	return PCILIB_ERROR_INVALID_ADDRESS;
85
    }
86
87
retry:
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
88
    err = pcilib_lock(bank_ctx->lock);
89
    if (err) {
90
	pcilib_error("Error (%i) obtaining a lock to serialize access to CMOSIS registers ", err);
91
	return err;
92
    }
93
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
94
    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
95
96
    ipecamera_datacpy(wr, &val, bank);
97
98
#ifdef IPECAMERA_SIMPLIFIED_READOUT
252 by Suren A. Chilingaryan
Cleanup
99
    usleep(IPECAMERA_SPI_REGISTER_DELAY);
100
    ipecamera_datacpy(wr, &val, bank);
101
    usleep(IPECAMERA_SPI_REGISTER_DELAY);
102
    ipecamera_datacpy(wr, &val, bank);
103
    usleep(IPECAMERA_SPI_REGISTER_DELAY);
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
104
#endif /* IPECAMERA_SIMPLIFIED_READOUT */
105
    
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
106
    gettimeofday(&start, NULL);
107
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
108
#ifdef IPECAMERA_MULTIREAD
252 by Suren A. Chilingaryan
Cleanup
109
    usleep(IPECAMERA_SPI_REGISTER_DELAY);
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
110
    pcilib_datacpy(tmp, rd, 4, 4, bank->raw_endianess);
111
    val = tmp[0];
112
#else /* IPECAMERA_MULTIREAD */
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
113
    ipecamera_datacpy(&val, rd, bank);
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
114
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
115
    while ((val & READ_READY_BIT) == 0) {
116
        gettimeofday(&cur, NULL);
252 by Suren A. Chilingaryan
Cleanup
117
	if (((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) > IPECAMERA_SPI_REGISTER_DELAY) break;
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
118
	
119
	ipecamera_datacpy(&val, rd, bank);
120
    }
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
121
#endif /* IPECAMERA_MULTIREAD */
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
122
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
123
    pcilib_unlock(bank_ctx->lock);
124
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
125
    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
126
	if (--retries > 0) {
127
	    pcilib_warning("Timeout reading register value (CMOSIS %lu, status: %lx), retrying (try %i of %i)...", addr, val, RETRIES - retries, RETRIES);
128
	    goto retry;
129
	}
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
130
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
131
	pcilib_error("Timeout reading register value (CMOSIS %lu, status: %lx)", addr, val);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
132
	return PCILIB_ERROR_TIMEOUT;
133
    }
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
134
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
135
    if (val & READ_ERROR_BIT) {
252 by Suren A. Chilingaryan
Cleanup
136
#ifdef IPECAMERA_RETRY_ERRORS
137
	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
138
	    pcilib_warning("Error reading register (CMOSIS %lu, status: %lx), retrying (try %i of %i)...", addr, val, RETRIES - retries, RETRIES);
139
	    goto retry;
252 by Suren A. Chilingaryan
Cleanup
140
	}
141
#endif /* IPECAMERA_RETRY_ERRORS */
142
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
143
	pcilib_error("Error reading register value (CMOSIS %lu, status: %lx)", addr, val);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
144
	return PCILIB_ERROR_FAILED;
145
    }
146
147
    if (((val&ADDR_MASK) >> 8) != addr) {
252 by Suren A. Chilingaryan
Cleanup
148
#ifdef IPECAMERA_RETRY_ERRORS
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
149
	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
150
	    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
151
	    goto retry;
152
	}
252 by Suren A. Chilingaryan
Cleanup
153
#endif /* IPECAMERA_RETRY_ERRORS */
154
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
155
	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
156
	return PCILIB_ERROR_VERIFY;
157
    }
158
54 by Suren A. Chilingaryan
Support dynamic registers, support register offsets and multiregisters (bitmasks), list NWL DMA registers
159
    *value = val&0xFF;
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
160
161
    return 0;
162
}
163
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
164
int ipecamera_cmosis_write(pcilib_t *ctx, pcilib_register_bank_context_t *reg_bank_ctx, pcilib_register_addr_t addr, pcilib_register_value_t value) {
165
    int err;
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
166
    uint32_t val, tmp[4];
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
167
    char *wr, *rd;
252 by Suren A. Chilingaryan
Cleanup
168
    struct timeval start;
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
169
    int retries = RETRIES;
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
170
    ipecamera_cmosis_context_t *bank_ctx = (ipecamera_cmosis_context_t*)reg_bank_ctx;
171
    const pcilib_register_bank_description_t *bank = reg_bank_ctx->bank;
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
172
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
173
    assert(addr < 128);
174
    assert(value < 256);
175
    
276 by Suren A. Chilingaryan
Update to new version of pcitool
176
    wr =  pcilib_resolve_bar_address(ctx, bank->bar, bank->write_addr);
177
    rd =  pcilib_resolve_bar_address(ctx, bank->bar, bank->read_addr);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
178
    if ((!rd)||(!wr)) {
179
	pcilib_error("Error resolving addresses of read & write registers");
180
	return PCILIB_ERROR_INVALID_ADDRESS;
181
    }
182
183
retry:
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
184
    err = pcilib_lock(bank_ctx->lock);
185
    if (err) {
186
	pcilib_error("Error (%i) obtaining a lock to serialize access to CMOSIS registers ", err);
187
	return err;
188
    }
189
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
190
    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
191
    ipecamera_datacpy(wr, &val, bank);
192
193
#ifdef IPECAMERA_SIMPLIFIED_READOUT
252 by Suren A. Chilingaryan
Cleanup
194
    usleep(IPECAMERA_SPI_REGISTER_DELAY);
195
    ipecamera_datacpy(wr, &val, bank);
196
    usleep(IPECAMERA_SPI_REGISTER_DELAY);
197
    ipecamera_datacpy(wr, &val, bank);
198
    usleep(IPECAMERA_SPI_REGISTER_DELAY);
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
199
#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
200
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
201
    gettimeofday(&start, NULL);
202
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
203
#ifdef IPECAMERA_MULTIREAD
252 by Suren A. Chilingaryan
Cleanup
204
    usleep(IPECAMERA_SPI_REGISTER_DELAY);
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
205
    pcilib_datacpy(tmp, rd, 4, 4, bank->raw_endianess);
206
    val = tmp[0];
207
#else /* IPECAMERA_MULTIREAD */
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
208
    ipecamera_datacpy(&val, rd, bank);
209
    while ((val & READ_READY_BIT) == 0) {
210
        gettimeofday(&cur, NULL);
252 by Suren A. Chilingaryan
Cleanup
211
	if (((cur.tv_sec - start.tv_sec)*1000000 + (cur.tv_usec - start.tv_usec)) > IPECAMERA_SPI_REGISTER_DELAY) break;
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
212
	
213
	ipecamera_datacpy(&val, rd, bank);
214
    }
33 by Suren A. Chilingaryan
Alternative way to overcome problem with address verification of CMOSIS registers
215
#endif /* IPECAMERA_MULTIREAD */
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
216
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
217
    pcilib_unlock(bank_ctx->lock);
218
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
219
    if ((val & READ_READY_BIT) == 0) {
252 by Suren A. Chilingaryan
Cleanup
220
#ifdef IPECAMERA_RETRY_ERRORS
15 by Suren A. Chilingaryan
Infrastructure for event API
221
	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
222
	    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
223
	    goto retry;
224
	}
252 by Suren A. Chilingaryan
Cleanup
225
#endif /* IPECAMERA_RETRY_ERRORS */
15 by Suren A. Chilingaryan
Infrastructure for event API
226
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
227
	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
228
	return PCILIB_ERROR_TIMEOUT;
229
    }
230
    
231
    if (val & READ_ERROR_BIT) {
252 by Suren A. Chilingaryan
Cleanup
232
#ifdef IPECAMERA_RETRY_ERRORS
233
	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
234
	    pcilib_warning("Register write has failed (CMOSIS %lu, value: %lu, status: %lx), retrying (try %i of %i)...", addr, value, val, RETRIES - retries, RETRIES);
235
	    goto retry;
252 by Suren A. Chilingaryan
Cleanup
236
	}
237
#endif /* IPECAMERA_RETRY_ERRORS */
238
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
239
	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
240
	return PCILIB_ERROR_FAILED;
241
    }
242
243
    if (((val&ADDR_MASK) >> 8) != addr) {
244
	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
245
	    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
246
	    goto retry;
247
	}
26 by Suren A. Chilingaryan
Support simplified mode (slow) of writting CMOSIS sensors: issue 3 writes + delays and when start looking for status
248
	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
249
	return PCILIB_ERROR_VERIFY;
250
    }
264 by Suren A. Chilingaryan
Serialize access to CMOSIS registers
251
252 by Suren A. Chilingaryan
Cleanup
252
    if ((val&0xFF) != value) {
54 by Suren A. Chilingaryan
Support dynamic registers, support register offsets and multiregisters (bitmasks), list NWL DMA registers
253
	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
254
	return PCILIB_ERROR_VERIFY;
255
    }
256
257
    return 0;
258
}
15 by Suren A. Chilingaryan
Infrastructure for event API
259
260
261