1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
* serial.c
*
* Serial digita support
*
* Copyright 1999-2000 Johannes Erdfelt
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <netinet/in.h>
#include "digita.h"
#include <gpio.h>
int gpio_serial_set_baudrate(gpio_device * dev);
struct beacon {
unsigned short intro;
unsigned short vendorid;
unsigned short deviceid;
unsigned char checksum;
} __attribute__ ((packed));
struct beacon_ack {
unsigned short intro;
unsigned char intftype;
unsigned int cf_reserved:4;
unsigned int cf_pod_receive_mode:2;
unsigned int cf_host_receive_mode:2;
unsigned int dataspeed;
unsigned short deviceframesize;
unsigned short hostframesize;
unsigned char checksum;
} __attribute__ ((packed));
struct beacon_comp {
unsigned char result;
unsigned int cf_reserved:4;
unsigned int cf_pod_receive_mode:2;
unsigned int cf_host_receive_mode:2;
unsigned int dataspeed;
unsigned short deviceframesize;
unsigned short hostframesize;
} __attribute__ ((packed));
#define POLL_LENGTH_MASK 0x03FF
#define POLL_BOB 0x0400
#define POLL_EOB 0x0800
#define POLL_CMD 0x1000
#define POLL_POLL_MASK 0xE000
#define POLL_POLL_SHIFT 13
#define POLL_ACK 0x01
#define POLL_NAK 0x02
static unsigned int checksum(const unsigned char *buffer, int len)
{
int i;
int limit = len - 1;
unsigned int sum = 0;
for (i = 0; i < limit; i++)
sum += *(buffer++);
return sum & 0xff;
}
static int poll_and_wait(gpio_device *dev, int length, int bob, int eob)
{
unsigned short s, poll, poll_reply;
do {
poll = (1 << POLL_POLL_SHIFT) | POLL_CMD | length |
(bob ? POLL_BOB : 0) | (eob ? POLL_EOB : 0);
s = htons(poll);
if (gpio_write(dev, (void *)&s, sizeof(s)) < 0)
return -1;
if (gpio_read(dev, (void *)&s, sizeof(s)) < 0)
return -1;
poll_reply = ntohs(s);
if (poll_reply & POLL_ACK)
printf("POLL_ACK\n");
if (poll_reply & POLL_NAK)
printf("POLL_NAK\n");
} while (poll_reply & POLL_NAK);
return 0;
}
static int digita_serial_send(struct digita_device *dev, void *_buffer, int len)
{
unsigned char *buffer = _buffer;
unsigned short s;
int sent = 0, size;
while (sent < len) {
if ((len - sent) > dev->deviceframesize)
size = dev->deviceframesize;
else
size = len - sent;
poll_and_wait(dev->gpdev, size, sent == 0, (size + sent) == len);
if (gpio_write(dev->gpdev, buffer + sent, size) < 0)
return -1;
sent += size;
}
s = 0;
if (gpio_write(dev->gpdev, (void *)&s, sizeof(s)) < 0)
return -1;
return len;
}
static int poll_and_reply(gpio_device *dev, int *length, int *eob, int nak)
{
unsigned short s, poll, poll_reply;
if (gpio_read(dev, (void *)&s, sizeof(s)) < 0)
return -1;
poll = ntohs(s);
if (length)
*length = poll & POLL_LENGTH_MASK;
if (eob)
*eob = poll & POLL_EOB;
if (nak)
poll_reply = POLL_NAK;
else
poll_reply = POLL_ACK;
s = htons(poll_reply);
if (gpio_write(dev, (void *)&s, sizeof(s)) < 0)
return -1;
return 0;
}
static int digita_serial_read(struct digita_device *dev, void *_buffer, int len)
{
unsigned char *buffer = _buffer;
unsigned short s;
int received = 0, size, eob;
while (received < len) {
poll_and_reply(dev->gpdev, &size, &eob, 0);
if (gpio_read(dev->gpdev, buffer + received, size) < 0)
return -1;
received += size;
if (eob)
break;
}
if (gpio_read(dev->gpdev, (void *)&s, sizeof(s)) < 0)
return -1;
return received;
}
int digita_serial_open(struct digita_device *dev, Camera *camera)
{
gpio_device_settings settings;
struct beacon beacon;
struct beacon_ack beacon_ack;
struct beacon_comp beacon_comp;
dev->gpdev = gpio_new(GPIO_DEVICE_SERIAL);
if (!dev->gpdev)
return -1;
strcpy(settings.serial.port,camera->port->path);
settings.serial.speed = 9600;
settings.serial.bits = 8;
settings.serial.parity = 0;
settings.serial.stopbits = 1;
digita_send = digita_serial_send;
digita_read = digita_serial_read;
gpio_set_settings(dev->gpdev, settings);
if (gpio_open(dev->gpdev) < 0) {
fprintf(stderr, "error opening device\n");
return 0;
}
tcsendbreak(dev->gpdev->device_fd, 4);
dev->gpdev->settings.serial.speed = 0;
gpio_serial_set_baudrate(dev->gpdev);
usleep(50);
dev->gpdev->settings.serial.speed = camera->port->speed;
gpio_serial_set_baudrate(dev->gpdev);
usleep(2000);
memset((void *)&beacon, 0, sizeof(beacon));
if (gpio_read(dev->gpdev, (void *)&beacon, sizeof(beacon)) < 0) {
perror("reading beacon");
return 0;
}
printf("%04X %04X %04X %02X\n",
beacon.intro, beacon.vendorid,
beacon.deviceid, beacon.checksum);
beacon_ack.intro = htons(0x5aa5);
beacon_ack.intftype = 0x55;
beacon_ack.cf_reserved = 0;
beacon_ack.cf_pod_receive_mode = 0;
beacon_ack.cf_host_receive_mode = 0;
beacon_ack.dataspeed = htonl(camera->port->speed);
beacon_ack.deviceframesize = htons(1023);
beacon_ack.hostframesize = htons(1023);
beacon_ack.checksum = 0;
beacon_ack.checksum = checksum((void *)&beacon_ack, sizeof(beacon_ack));
if (gpio_write(dev->gpdev, (void *)&beacon_ack, sizeof(beacon_ack)) < 0) {
perror("writing beacon_ack");
return -1;
}
if (gpio_read(dev->gpdev, (void *)&beacon_comp, sizeof(beacon_comp)) < 0) {
perror("reading beacon_comp");
return -1;
}
printf("%d\n", ntohl(beacon_comp.dataspeed));
usleep(100000);
dev->deviceframesize = ntohs(beacon_comp.deviceframesize);
dev->gpdev->settings.serial.speed = ntohl(beacon_comp.dataspeed);
gpio_serial_set_baudrate(dev->gpdev);
usleep(100000);
return 0;
}
|