]>
Commit | Line | Data |
---|---|---|
df92e5cf | 1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include "protocol.h" | |
21 | ||
22 | extern SR_PRIV struct sr_dev_driver link_mso19_driver_info; | |
23 | static struct sr_dev_driver *di = &link_mso19_driver_info; | |
24 | ||
25 | SR_PRIV int mso_parse_serial(const char *iSerial, const char *iProduct, | |
26 | struct dev_context *devc) | |
27 | { | |
28 | unsigned int u1, u2, u3, u4, u5, u6; | |
29 | ||
30 | iProduct = iProduct; | |
31 | /* FIXME: This code is in the original app, but I think its | |
32 | * used only for the GUI */ | |
33 | /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03")) | |
34 | devc->num_sample_rates = 0x16; | |
35 | else | |
36 | devc->num_sample_rates = 0x10; */ | |
37 | ||
38 | ||
39 | /* parse iSerial */ | |
40 | if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u", | |
41 | &u1, &u2, &u3, &u4, &u5, &u6) != 6) | |
42 | return SR_ERR; | |
43 | devc->hwmodel = u4; | |
44 | devc->hwrev = u5; | |
45 | devc->serial = u6; | |
46 | devc->vbit = u1 / 10000; | |
47 | if (devc->vbit == 0) | |
48 | devc->vbit = 4.19195; | |
49 | devc->dac_offset = u2; | |
50 | if (devc->dac_offset == 0) | |
51 | devc->dac_offset = 0x1ff; | |
52 | devc->offset_range = u3; | |
53 | if (devc->offset_range == 0) | |
54 | devc->offset_range = 0x17d; | |
55 | ||
56 | /* | |
57 | * FIXME: There is more code on the original software to handle | |
58 | * bigger iSerial strings, but as I can't test on my device | |
59 | * I will not implement it yet | |
60 | */ | |
61 | ||
62 | return SR_OK; | |
63 | } | |
64 | ||
65 | SR_PRIV int mso_send_control_message(struct sr_serial_dev_inst *serial, | |
66 | uint16_t payload[], int n) | |
67 | { | |
68 | int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot); | |
69 | char *p, *buf; | |
70 | ||
71 | ret = SR_ERR; | |
72 | ||
73 | if (serial->fd < 0) | |
74 | goto ret; | |
75 | ||
76 | if (!(buf = g_try_malloc(s))) { | |
77 | sr_err("Failed to malloc message buffer."); | |
78 | ret = SR_ERR_MALLOC; | |
79 | goto ret; | |
80 | } | |
81 | ||
82 | p = buf; | |
83 | memcpy(p, mso_head, sizeof(mso_head)); | |
84 | p += sizeof(mso_head); | |
85 | ||
86 | for (i = 0; i < n; i++) { | |
87 | *(uint16_t *) p = htons(payload[i]); | |
88 | p += 2; | |
89 | } | |
90 | memcpy(p, mso_foot, sizeof(mso_foot)); | |
91 | ||
92 | w = 0; | |
93 | while (w < s) { | |
94 | ret = serial_write(serial, buf + w, s - w); | |
95 | if (ret < 0) { | |
96 | ret = SR_ERR; | |
97 | goto free; | |
98 | } | |
99 | w += ret; | |
100 | } | |
101 | ret = SR_OK; | |
102 | free: | |
103 | g_free(buf); | |
104 | ret: | |
105 | return ret; | |
106 | } | |
107 | ||
108 | SR_PRIV int mso_reset_adc(struct sr_dev_inst *sdi) | |
109 | { | |
110 | struct dev_context *devc = sdi->priv; | |
111 | uint16_t ops[2]; | |
112 | ||
113 | ops[0] = mso_trans(REG_CTL1, (devc->ctlbase1 | BIT_CTL1_RESETADC)); | |
114 | ops[1] = mso_trans(REG_CTL1, devc->ctlbase1); | |
115 | devc->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4; | |
116 | ||
117 | sr_dbg("Requesting ADC reset."); | |
118 | return mso_send_control_message(devc->serial, ARRAY_AND_SIZE(ops)); | |
119 | } | |
120 | ||
121 | SR_PRIV void stop_acquisition(const struct sr_dev_inst *sdi) | |
122 | { | |
123 | struct sr_datafeed_packet packet; | |
124 | struct dev_context *devc; | |
125 | ||
126 | devc = sdi->priv; | |
127 | sr_source_remove(devc->serial->fd); | |
128 | ||
129 | /* Terminate session */ | |
130 | packet.type = SR_DF_END; | |
131 | sr_session_send(sdi, &packet); | |
132 | } | |
133 | ||
134 | SR_PRIV int mso_clkrate_out(struct sr_serial_dev_inst *serial, uint16_t val) | |
135 | { | |
136 | uint16_t ops[] = { | |
137 | mso_trans(REG_CLKRATE1, (val >> 8) & 0xff), | |
138 | mso_trans(REG_CLKRATE2, val & 0xff), | |
139 | }; | |
140 | ||
141 | sr_dbg("Setting clkrate word to 0x%x.", val); | |
142 | return mso_send_control_message(serial, ARRAY_AND_SIZE(ops)); | |
143 | } | |
144 | ||
145 | SR_PRIV int mso_configure_rate(struct sr_dev_inst *sdi, uint32_t rate) | |
146 | { | |
147 | struct dev_context *devc = sdi->priv; | |
148 | unsigned int i; | |
149 | int ret = SR_ERR; | |
150 | ||
151 | for (i = 0; i < ARRAY_SIZE(rate_map); i++) { | |
152 | if (rate_map[i].rate == rate) { | |
153 | devc->ctlbase2 = rate_map[i].slowmode; | |
154 | ret = mso_clkrate_out(sdi, rate_map[i].val); | |
155 | if (ret == SR_OK) | |
156 | devc->cur_rate = rate; | |
157 | return ret; | |
158 | } | |
159 | } | |
160 | return ret; | |
161 | } | |
162 | ||
163 | ||
164 | ||
165 | ||
166 | ||
167 | SR_PRIV int mso_check_trigger(struct sr_serial_dev_inst *serial, uint8_t *info) | |
168 | { | |
169 | uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) }; | |
170 | int ret; | |
171 | ||
172 | sr_dbg("Requesting trigger state."); | |
173 | ret = mso_send_control_message(serial, ARRAY_AND_SIZE(ops)); | |
174 | if (info == NULL || ret != SR_OK) | |
175 | return ret; | |
176 | ||
177 | ||
178 | uint8_t buf = 0; | |
179 | if (serial_read(serial, &buf, 1) != 1) /* FIXME: Need timeout */ | |
180 | ret = SR_ERR; | |
181 | *info = buf; | |
182 | ||
183 | sr_dbg("Trigger state is: 0x%x.", *info); | |
184 | return ret; | |
185 | } | |
186 | ||
187 | SR_PRIV int mso_receive_data(int fd, int revents, void *cb_data) | |
188 | { | |
189 | ||
190 | struct sr_datafeed_packet packet; | |
191 | struct sr_datafeed_logic logic; | |
192 | struct sr_dev_inst *sdi; | |
193 | struct drv_context *drvc; | |
194 | struct dev_context *devc; | |
195 | GSList *l; | |
196 | int num_channels, offset, i, j; | |
197 | unsigned char byte; | |
198 | ||
199 | drvc = di->priv; | |
200 | ||
201 | /* Find this device's devc struct by its fd. */ | |
202 | devc = NULL; | |
203 | for (l = drvc->instances; l; l = l->next) { | |
204 | sdi = l->data; | |
205 | devc = sdi->priv; | |
206 | if (devc->serial->fd == fd) | |
207 | break; | |
208 | devc = NULL; | |
209 | } | |
210 | if (!devc) | |
211 | /* Shouldn't happen. */ | |
212 | return TRUE; | |
213 | ||
214 | (void)revents; | |
215 | ||
216 | uint8_t in[1024]; | |
217 | size_t s = serial_read(devc->serial, in, sizeof(in)); | |
218 | if (s <= 0) | |
219 | return FALSE; | |
220 | ||
221 | /* No samples */ | |
222 | if (devc->trigger_state != MSO_TRIGGER_DATAREADY) { | |
223 | devc->trigger_state = in[0]; | |
224 | if (devc->trigger_state == MSO_TRIGGER_DATAREADY) { | |
225 | mso_read_buffer(sdi); | |
226 | devc->buffer_n = 0; | |
227 | } else { | |
228 | mso_check_trigger(devc->serial, NULL); | |
229 | } | |
230 | return FALSE; | |
231 | } | |
232 | ||
233 | /* the hardware always dumps 1024 samples, 24bits each */ | |
234 | if (devc->buffer_n < 3072) { | |
235 | memcpy(devc->buffer + devc->buffer_n, in, s); | |
236 | devc->buffer_n += s; | |
237 | } | |
238 | if (devc->buffer_n < 3072) | |
239 | return FALSE; | |
240 | ||
241 | /* do the conversion */ | |
242 | uint8_t logic_out[1024]; | |
243 | double analog_out[1024]; | |
244 | for (i = 0; i < 1024; i++) { | |
245 | /* FIXME: Need to do conversion to mV */ | |
246 | analog_out[i] = (devc->buffer[i * 3] & 0x3f) | | |
247 | ((devc->buffer[i * 3 + 1] & 0xf) << 6); | |
248 | logic_out[i] = ((devc->buffer[i * 3 + 1] & 0x30) >> 4) | | |
249 | ((devc->buffer[i * 3 + 2] & 0x3f) << 2); | |
250 | } | |
251 | ||
252 | packet.type = SR_DF_LOGIC; | |
253 | packet.payload = &logic; | |
254 | logic.length = 1024; | |
255 | logic.unitsize = 1; | |
256 | logic.data = logic_out; | |
257 | sr_session_send(cb_data, &packet); | |
258 | ||
259 | // Dont bother fixing this yet, keep it "old style" | |
260 | /* | |
261 | packet.type = SR_DF_ANALOG; | |
262 | packet.length = 1024; | |
263 | packet.unitsize = sizeof(double); | |
264 | packet.payload = analog_out; | |
265 | sr_session_send(ctx->session_dev_id, &packet); | |
266 | */ | |
267 | ||
268 | packet.type = SR_DF_END; | |
269 | sr_session_send(devc->session_dev_id, &packet); | |
270 | ||
271 | } |