]> sigrok.org Git - libsigrok.git/blame - src/hardware/baylibre-acme/protocol.c
baylibre-acme: Driver implementation.
[libsigrok.git] / src / hardware / baylibre-acme / protocol.c
CommitLineData
dfaee1de
BG
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.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
6b80b80d
BG
20#include <string.h>
21#include <stdlib.h> /* strtol() */
22#include <errno.h>
23#include <fcntl.h> /* open(), etc... */
dfaee1de
BG
24#include "protocol.h"
25
6b80b80d
BG
26struct channel_group_priv {
27 int hwmon_num;
28};
29
30struct channel_priv {
31 int ch_type;
32 struct channel_group_priv *probe;
33};
34
35static const uint8_t enrg_i2c_addrs[] = {
36 0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47,
37};
38
39static const uint8_t temp_i2c_addrs[] = {
40 0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b,
41};
42
43SR_PRIV uint8_t bl_acme_get_enrg_addr(int index)
44{
45 return enrg_i2c_addrs[index];
46}
47
48SR_PRIV uint8_t bl_acme_get_temp_addr(int index)
49{
50 return temp_i2c_addrs[index];
51}
52
53SR_PRIV gboolean bl_acme_is_sane(void)
54{
55 gboolean status;
56
57 /*
58 * We expect sysfs to be present and mounted at /sys, ina226 and
59 * tmp435 sensors detected by the system and their appropriate
60 * drivers loaded and functional.
61 */
62 status = g_file_test("/sys", G_FILE_TEST_IS_DIR);
63 if (!status) {
64 sr_err("/sys/ directory not found - sysfs not mounted?");
65 return FALSE;
66 }
67
68 return TRUE;
69}
70
71static void probe_name_path(unsigned int addr, GString *path)
72{
73 g_string_printf(path,
74 "/sys/class/i2c-adapter/i2c-1/1-00%02x/name", addr);
75}
76
77/*
78 * For given address fill buf with the path to appropriate hwmon entry.
79 */
80static void probe_hwmon_path(unsigned int addr, GString *path)
81{
82 g_string_printf(path,
83 "/sys/class/i2c-adapter/i2c-1/1-00%02x/hwmon", addr);
84}
85
86SR_PRIV gboolean bl_acme_detect_probe(unsigned int addr,
87 int prb_num, const char *prb_name)
88{
89 gboolean ret = FALSE, status;
90 char *buf = NULL;
91 GString *path = g_string_sized_new(64);
92 GError *err = NULL;
93 gsize size;
94
95 probe_name_path(addr, path);
96 status = g_file_get_contents(path->str, &buf, &size, &err);
97 if (!status) {
98 sr_dbg("Name for probe %d can't be read: %s",
99 prb_num, err->message);
100 g_string_free(path, TRUE);
101 return ret;
102
103 }
104
105 if (strncmp(buf, prb_name, strlen(prb_name)) == 0) {
106 /*
107 * Correct driver registered on this address - but is
108 * there an actual probe connected?
109 */
110 probe_hwmon_path(addr, path);
111 status = g_file_test(path->str, G_FILE_TEST_IS_DIR);
112 if (status) {
113 /* We have found an ACME probe. */
114 ret = TRUE;
115 }
116 }
117
118 g_free(buf);
119 g_string_free(path, TRUE);
120
121 return ret;
122}
123
124static int get_hwmon_index(unsigned int addr)
125{
126 int status, hwmon;
127 GString *path = g_string_sized_new(64);
128 GError *err = NULL;
129 GDir *dir;
130
131 probe_hwmon_path(addr, path);
132 dir = g_dir_open(path->str, 0, &err);
133 if (dir == NULL) {
134 sr_err("Error opening %s: %s", path->str, err->message);
135 g_string_free(path, TRUE);
136 return -1;
137 }
138
139 g_string_free(path, TRUE);
140
141 /*
142 * The directory should contain a single file named hwmonX where X
143 * is the hwmon index.
144 */
145 status = sscanf(g_dir_read_name(dir), "hwmon%d", &hwmon);
146 g_dir_close(dir);
147 if (status != 1) {
148 sr_err("Unable to determine the hwmon entry");
149 return -1;
150 }
151
152 return hwmon;
153}
154
155static void append_channel(struct sr_dev_inst *sdi,
156 struct sr_channel_group *cg,
157 int index, int type)
158{
159 struct channel_priv *cp;
160 struct dev_context *devc;
161 struct sr_channel *ch;
162 char *name;
163
164 devc = sdi->priv;
165
166 switch (type) {
167 case ENRG_PWR:
168 name = g_strdup_printf("P%d_ENRG_PWR", index);
169 break;
170 case ENRG_CURR:
171 name = g_strdup_printf("P%d_ENRG_CURR", index);
172 break;
173 case ENRG_VOL:
174 name = g_strdup_printf("P%d_ENRG_VOL", index);
175 break;
176 case TEMP_IN:
177 name = g_strdup_printf("P%d_TEMP_IN", index);
178 break;
179 case TEMP_OUT:
180 name = g_strdup_printf("P%d_TEMP_OUT", index);
181 break;
182 default:
183 sr_err("Bug in the code: invalid channel type!");
184 return;
185 }
186
187 cp = g_malloc0(sizeof(struct channel_priv));
188 cp->ch_type = type;
189 cp->probe = cg->priv;
190
191 ch = sr_channel_new(devc->num_channels++,
192 SR_CHANNEL_ANALOG, TRUE, name);
193 g_free(name);
194
195 ch->priv = cp;
196 cg->channels = g_slist_append(cg->channels, ch);
197 sdi->channels = g_slist_append(sdi->channels, ch);
198}
199
200SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type,
201 unsigned int addr, int prb_num)
202{
203 struct sr_channel_group *cg;
204 struct channel_group_priv *cgp;
205 int hwmon;
206
207 /* Obtain the hwmon index. */
208 hwmon = get_hwmon_index(addr);
209 if (hwmon < 0)
210 return FALSE;
211
212 cg = g_malloc0(sizeof(struct sr_channel_group));
213 cgp = g_malloc0(sizeof(struct channel_group_priv));
214 cgp->hwmon_num = hwmon;
215 cg->name = g_strdup_printf("Probe_%d", prb_num);
216 cg->priv = cgp;
217
218 if (type == PROBE_ENRG) {
219 append_channel(sdi, cg, prb_num, ENRG_PWR);
220 append_channel(sdi, cg, prb_num, ENRG_CURR);
221 append_channel(sdi, cg, prb_num, ENRG_VOL);
222 } else if (type == PROBE_TEMP) {
223 append_channel(sdi, cg, prb_num, TEMP_IN);
224 append_channel(sdi, cg, prb_num, TEMP_OUT);
225 } else {
226 sr_err("Bug in the code: invalid probe type!");
227 }
228
229 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
230
231 return TRUE;
232}
233
234static int channel_to_mq(struct sr_channel *ch)
dfaee1de 235{
6b80b80d
BG
236 struct channel_priv *chp;
237
238 chp = ch->priv;
239
240 switch (chp->ch_type) {
241 case ENRG_PWR:
242 return SR_MQ_POWER;
243 case ENRG_CURR:
244 return SR_MQ_CURRENT;
245 case ENRG_VOL:
246 return SR_MQ_VOLTAGE;
247 case TEMP_IN:
248 case TEMP_OUT:
249 return SR_MQ_TEMPERATURE;
250 default:
251 return -1;
252 }
253}
254
255static int channel_to_unit(struct sr_channel *ch)
256{
257 struct channel_priv *chp;
258
259 chp = ch->priv;
260
261 switch (chp->ch_type) {
262 case ENRG_PWR:
263 return SR_UNIT_WATT;
264 case ENRG_CURR:
265 return SR_UNIT_AMPERE;
266 case ENRG_VOL:
267 return SR_UNIT_VOLT;
268 case TEMP_IN:
269 case TEMP_OUT:
270 return SR_UNIT_CELSIUS;
271 default:
272 return -1;
273 }
274}
275
276/* We need to scale measurements down from the units used by the drivers. */
277static float adjust_data(int val, int type)
278{
279 switch (type) {
280 case ENRG_PWR:
281 return ((float)val) / 1000000.0;
282 case ENRG_CURR:
283 case ENRG_VOL:
284 case TEMP_IN:
285 case TEMP_OUT:
286 return ((float)val) / 1000.0;
287 default:
288 return 0.0;
289 }
290}
291
292static float read_sample(struct sr_channel *ch)
293{
294 struct channel_priv *chp;
295 char path[64], *file, buf[16];
296 ssize_t len;
297 int fd;
298
299 chp = ch->priv;
300
301 switch (chp->ch_type) {
302 case ENRG_PWR: file = "power1_input"; break;
303 case ENRG_CURR: file = "curr1_input"; break;
304 case ENRG_VOL: file = "in1_input"; break;
305 case TEMP_IN: file = "temp1_input"; break;
306 case TEMP_OUT: file = "temp2_input"; break;
307 default:
308 sr_err("Bug in the code: invalid channel type!");
309 return -1.0;
310 }
311
312 snprintf(path, sizeof(path),
313 "/sys/class/hwmon/hwmon%d/%s",
314 chp->probe->hwmon_num, file);
315 fd = open(path, O_RDONLY);
316 if (fd < 0) {
317 sr_err("Error opening %s: %s", path, strerror(errno));
318 ch->enabled = FALSE;
319 return -1.0;
320 }
321
322 len = read(fd, buf, sizeof(buf));
323 close(fd);
324 if (len < 0) {
325 sr_err("error reading from %s: %s", path, strerror(errno));
326 ch->enabled = FALSE;
327 return -1.0;
328 }
329
330 return adjust_data(strtol(buf, NULL, 10), chp->ch_type);
331}
332
333SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data)
334{
335 uint32_t cur_time, elapsed_time, diff_time;
336 int64_t time_to_sleep;
337 struct sr_datafeed_packet packet, framep;
338 struct sr_datafeed_analog analog;
339 struct sr_dev_inst *sdi;
340 struct sr_channel *ch;
dfaee1de 341 struct dev_context *devc;
6b80b80d
BG
342 GSList *chl, chonly;
343 float valf;
dfaee1de
BG
344
345 (void)fd;
6b80b80d
BG
346 (void)revents;
347
348 sdi = cb_data;
349 if (!sdi)
350 return TRUE;
dfaee1de 351
6b80b80d
BG
352 devc = sdi->priv;
353 if (!devc)
dfaee1de
BG
354 return TRUE;
355
6b80b80d
BG
356 packet.type = SR_DF_ANALOG;
357 packet.payload = &analog;
358 memset(&analog, 0, sizeof(analog));
359 analog.data = &valf;
360
361 /*
362 * Reading from sysfs takes some time - try to
363 * keep up with samplerate.
364 */
365 if (devc->samples_read) {
366 cur_time = g_get_monotonic_time();
367 diff_time = cur_time - devc->last_sample_fin;
368 time_to_sleep = G_USEC_PER_SEC / devc->samplerate - diff_time;
369 if (time_to_sleep > 0)
370 g_usleep(time_to_sleep);
371 }
372
373 framep.type = SR_DF_FRAME_BEGIN;
374 sr_session_send(cb_data, &framep);
375
376 /*
377 * Due to different units used in each channel we're sending
378 * samples one-by-one.
379 */
380 for (chl = sdi->channels; chl; chl = chl->next) {
381 ch = chl->data;
382 if (!ch->enabled)
383 continue;
384 chonly.next = NULL;
385 chonly.data = ch;
386 analog.channels = &chonly;
387 analog.num_samples = 1;
388 analog.mq = channel_to_mq(chl->data);
389 analog.unit = channel_to_unit(ch);
390
391 valf = read_sample(ch);
392
393 sr_session_send(cb_data, &packet);
394 }
395
396 framep.type = SR_DF_FRAME_END;
397 sr_session_send(cb_data, &framep);
398
399 devc->samples_read++;
400 if (devc->limit_samples > 0 &&
401 devc->samples_read >= devc->limit_samples) {
402 sr_info("Requested number of samples reached.");
403 sdi->driver->dev_acquisition_stop(sdi, cb_data);
404 devc->last_sample_fin = g_get_monotonic_time();
dfaee1de 405 return TRUE;
6b80b80d
BG
406 } else if (devc->limit_msec > 0) {
407 cur_time = g_get_monotonic_time();
408 elapsed_time = cur_time - devc->start_time;
dfaee1de 409
6b80b80d
BG
410 if (elapsed_time >= devc->limit_msec) {
411 sr_info("Sampling time limit reached.");
412 sdi->driver->dev_acquisition_stop(sdi, cb_data);
413 devc->last_sample_fin = g_get_monotonic_time();
414 return TRUE;
415 }
dfaee1de
BG
416 }
417
6b80b80d 418 devc->last_sample_fin = g_get_monotonic_time();
dfaee1de
BG
419 return TRUE;
420}