/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
1 by Suren A. Chilingaryan
Initial import
1
#include <stdio.h>
2
#include <string.h>
3
#include <unistd.h>
4
#include <stdint.h>
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
5
#include <assert.h>
7.1.5 by Suren A. Chilingaryan
Support for FPGA registers
6
#include <ctype.h>
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
7
#include <arpa/inet.h>
117 by Suren A. Chilingaryan
new event architecture, first trial
8
#include <sys/time.h>
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
9
10
#include "tools.h"
11
7.1.5 by Suren A. Chilingaryan
Support for FPGA registers
12
int pcilib_isnumber(const char *str) {
13
    int i = 0;
14
    for (i = 0; str[i]; i++) 
15
	if (!isdigit(str[i])) return 0;
16
    return 1;
17
}
18
19
int pcilib_isxnumber(const char *str) {
20
    int i = 0;
12 by Suren A. Chilingaryan
Correctly detect hex numbers starting from 0x
21
    
22
    if ((str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
23
    
24
    for (; str[i]; i++) 
7.1.5 by Suren A. Chilingaryan
Support for FPGA registers
25
	if (!isxdigit(str[i])) return 0;
12 by Suren A. Chilingaryan
Correctly detect hex numbers starting from 0x
26
7.1.5 by Suren A. Chilingaryan
Support for FPGA registers
27
    return 1;
28
}
29
62 by Suren A. Chilingaryan
Suppport DMA modes in console application (not functional yet)
30
int pcilib_isnumber_n(const char *str, size_t len) {
31
    int i = 0;
32
    for (i = 0; (str[i])&&(i < len); i++) 
33
	if (!isdigit(str[i])) return 0;
34
    return 1;
35
}
36
37
int pcilib_isxnumber_n(const char *str, size_t len) {
38
    int i = 0;
39
    
40
    if ((len > 1)&&(str[0] == '0')&&((str[1] == 'x')||(str[1] == 'X'))) i += 2;
41
    
42
    for (; (str[i])&&(i < len); i++) 
43
	if (!isxdigit(str[i])) return 0;
44
45
    return 1;
46
}
47
7.1.5 by Suren A. Chilingaryan
Support for FPGA registers
48
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
49
uint16_t pcilib_swap16(uint16_t x) {
50
    return (((x<<8)&0xFFFF) | ((x>>8)&0xFFFF));
51
}
52
53
uint32_t pcilib_swap32(uint32_t x) {
54
    return ((x & 0xFF) << 24) | \
55
	((x & 0xFF00) << 8) | \
56
	((x & 0xFF0000) >> 8) | \
57
        ((x & 0xFF000000) >> 24); 
58
}
59
 
60
uint64_t pcilib_swap64(uint64_t x) {
61
    return (((uint64_t)(x) << 56) | \
62
        (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
63
        (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
64
        (((uint64_t)(x) << 8)  & 0xff00000000ULL) | \
65
        (((uint64_t)(x) >> 8)  & 0xff000000ULL) | \
66
        (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
67
        (((uint64_t)(x) >> 40) & 0xff00ULL) | \
68
        ((uint64_t)(x)  >> 56));
69
}
70
71
void pcilib_swap(void *dst, void *src, size_t size, size_t n) {
72
    int i;
73
    switch (size) {
74
	case 1:
75
	    if (src != dst) memcpy(dst, src, n);
76
	break;
77
	case 2:
78
	    for (i = 0; i < n; i++) {
79
		((uint16_t*)dst)[i] = pcilib_swap16(((uint16_t*)src)[i]);
80
	    }    
81
	break;
82
	case 4:
83
	    for (i = 0; i < n; i++) {
84
		((uint32_t*)dst)[i] = pcilib_swap32(((uint32_t*)src)[i]);
85
	    }    
86
	break;
87
	case 8:
88
	    for (i = 0; i < n; i++) {
89
		((uint64_t*)dst)[i] = pcilib_swap64(((uint64_t*)src)[i]);
90
	    }    
91
	break;
92
	default:
93
	    pcilib_error("Invalid word size: %i", size);
94
    }
95
}
96
97
void *pcilib_memcpy8(void * dst, void const * src, size_t len) {
1 by Suren A. Chilingaryan
Initial import
98
    int i;
99
    for (i = 0; i < len; i++) ((char*)dst)[i] = ((char*)src)[i];
100
    return dst;
101
}
102
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
103
void *pcilib_memcpy32(void * dst, void const * src, size_t len) {
1 by Suren A. Chilingaryan
Initial import
104
    uint32_t * plDst = (uint32_t *) dst;
105
    uint32_t const * plSrc = (uint32_t const *) src;
106
107
    while (len >= 4) {
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
108
//        *plDst = ntohl(*plSrc);
109
	*plDst = *plSrc;
7 by Matthias Vogelgesang
Write LSB first and fix memory allocation bug
110
        plSrc++;
111
        plDst++;
1 by Suren A. Chilingaryan
Initial import
112
        len -= 4;
113
    }
114
115
    char * pcDst = (char *) plDst;
116
    char const * pcSrc = (char const *) plSrc;
117
118
    while (len--) {
119
        *pcDst++ = *pcSrc++;
120
    }
121
122
    return (dst);
123
} 
124
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
125
126
void *pcilib_memcpy64(void * dst, void const * src, size_t len) {
3 by Suren A. Chilingaryan
Print a bit more details
127
    uint64_t * plDst = (uint64_t *) dst;
128
    uint64_t const * plSrc = (uint64_t const *) src;
129
130
    while (len >= 8) {
131
        *plDst++ = *plSrc++;
4 by Suren A. Chilingaryan
Commented out unsuccesfull attempts to make 64 bit transfers
132
        len -= 8;
133
    }
134
135
    char * pcDst = (char *) plDst;
136
    char const * pcSrc = (char const *) plSrc;
137
138
    while (len--) {
139
        *pcDst++ = *pcSrc++;
140
    }
141
142
    return (dst);
143
} 
144
145
/*
146
void *memcpy128(void * dst, void const * src, size_t len) {
147
148
    long pos = - (len>>2);
149
    char * plDst = (char *) dst - 4 * pos;
150
    char const * plSrc = (char const *) src - 4 * pos;
151
152
    if (pos) {
153
        __asm__ __volatile__ (
154
            "1:						\n\t"
155
            "mov	(%0,%2,4), %%edi		\n\t"
156
            "mov	%%edi, (%1,%2,4)		\n\t"
157
            "inc	%2				\n\t"
158
            "jnz 	1b				\n\t"
159
	: 
160
	: "r" (plSrc), "r" (plDst), "r" (pos)
161
	: "%edi"
162
        );
163
    }
164
165
166
167
    long pos = - ((len>>4)<<4);
168
    char * plDst = (char *) dst - pos;
169
    char const * plSrc = (char const *) src - pos;
170
171
    if (pos) {
172
        __asm__ __volatile__ (
173
            "1:						\n\t"
174
//            "movdqa	(%0,%2), %%xmm0			\n\t"
175
            "mov	(%0,%2), %%esi			\n\t"
176
            "movd	%%esi, %%xmm0			\n\t"
177
            "mov	4(%0,%2), %%esi			\n\t"
178
            "movd	%%esi, %%xmm1			\n\t"
179
            "mov	8(%0,%2), %%esi			\n\t"
180
            "movd	%%esi, %%xmm2			\n\t"
181
            "mov	12(%0,%2), %%esi		\n\t"
182
            "movd	%%esi, %%xmm3			\n\t"
183
	    "pslldq	$4, %%xmm1			\n\t"
184
	    "por	%%xmm1, %%xmm0			\n\t"
185
	    "pslldq	$8, %%xmm2			\n\t"
186
	    "por	%%xmm2, %%xmm0			\n\t"
187
	    "pslldq	$12, %%xmm3			\n\t"
188
	    "por	%%xmm3, %%xmm0			\n\t"
189
	    
190
            "movntdq	%%xmm0, (%1,%2)			\n\t"
191
            "add	$16, %2				\n\t"
192
            "jnz 	1b				\n\t"
193
	: 
194
	: "r" (plSrc), "r" (plDst), "r" (pos)
195
	: "%rsi"
196
        );
197
    }
198
199
200
201
    len &= 0x3;
202
203
    char * pcDst = (char *) plDst;
204
    char const * pcSrc = (char const *) plSrc;
205
206
    while (len--) {
207
        *pcDst++ = *pcSrc++;
208
    }
209
210
    return (dst);
211
} 
212
*/
3 by Suren A. Chilingaryan
Print a bit more details
213
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
214
void *pcilib_datacpy32(void * dst, void const * src, uint8_t size, size_t n, pcilib_endianess_t endianess) {
215
    uint32_t * plDst = (uint32_t *) dst;
216
    uint32_t const * plSrc = (uint32_t const *) src;
217
16 by Suren A. Chilingaryan
Prototype of IPECamera image protocol
218
    int swap = 0;
34 by Suren A. Chilingaryan
A bit faster datacpy
219
220
    if (endianess) 
221
        swap = (endianess == PCILIB_BIG_ENDIAN)?(ntohs(1)!=1):(ntohs(1)==1);
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
222
223
    assert(size == 4);	// only 32 bit at the moment
224
34 by Suren A. Chilingaryan
A bit faster datacpy
225
    if (swap) {
226
        while (n > 0) {
227
            *plDst = ntohl(*plSrc);
228
            ++plSrc;
229
            ++plDst;
230
            --n;
231
        }
232
    } else {
233
        while (n > 0) {
234
            *plDst = *plSrc;
235
            ++plSrc;
236
            ++plDst;
237
            --n;
238
        }
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
239
    }
240
} 
241
242
int pcilib_get_page_mask() {
1 by Suren A. Chilingaryan
Initial import
243
    int pagesize,pagemask,temp;
244
7.1.1 by Suren A. Chilingaryan
Initial support of IPECamera protocol
245
    pagesize = sysconf(_SC_PAGESIZE);
1 by Suren A. Chilingaryan
Initial import
246
247
    for( pagemask=0, temp = pagesize; temp != 1; ) {
248
	temp = (temp >> 1);
249
	pagemask = (pagemask << 1)+1;
250
    }
251
    return pagemask;
252
}
117 by Suren A. Chilingaryan
new event architecture, first trial
253
254
int calc_deadline(struct timeval *tv, pcilib_timeout_t timeout) {
255
    gettimeofday(tv, NULL);
256
    tv->tv_usec += timeout%1000000;
257
    if (tv->tv_usec > 999999) {
258
	tv->tv_usec -= 1000000;
259
	tv->tv_sec = 1 + timeout/1000000;
260
    } else {
261
	tv->tv_sec = timeout/1000000;
262
    }
263
    
264
    return 0;
265
}
266
267
int check_deadline(struct timeval *tve, pcilib_timeout_t timeout) {
268
    int64_t res;
269
    struct timeval tvs;
270
    
271
    if (!tve->tv_sec) return 0;
272
    
273
    gettimeofday(&tvs, NULL);
274
    res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
275
    if (res < timeout) return 1;
276
277
    return 0;
278
}
279
280
pcilib_timeout_t calc_time_to_deadline(struct timeval *tve) {
281
    int64_t res;
282
    struct timeval tvs;
283
    
284
    gettimeofday(&tvs, NULL);
285
    res = ((tve->tv_sec - tvs.tv_sec)*1000000 + (tve->tv_usec - tvs.tv_usec));
286
    
287
    if (res < 0) return 0;
288
    return res;
289
}