2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
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.
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.
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/>.
21 #include <stdlib.h> /* strtol() */
23 #include <fcntl.h> /* open(), etc... */
26 struct channel_group_priv {
32 struct channel_group_priv *probe;
35 static const uint8_t enrg_i2c_addrs[] = {
36 0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47,
39 static const uint8_t temp_i2c_addrs[] = {
40 0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b,
43 SR_PRIV uint8_t bl_acme_get_enrg_addr(int index)
45 return enrg_i2c_addrs[index];
48 SR_PRIV uint8_t bl_acme_get_temp_addr(int index)
50 return temp_i2c_addrs[index];
53 SR_PRIV gboolean bl_acme_is_sane(void)
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.
62 status = g_file_test("/sys", G_FILE_TEST_IS_DIR);
64 sr_err("/sys/ directory not found - sysfs not mounted?");
71 static void probe_name_path(unsigned int addr, GString *path)
74 "/sys/class/i2c-adapter/i2c-1/1-00%02x/name", addr);
78 * For given address fill buf with the path to appropriate hwmon entry.
80 static void probe_hwmon_path(unsigned int addr, GString *path)
83 "/sys/class/i2c-adapter/i2c-1/1-00%02x/hwmon", addr);
86 SR_PRIV gboolean bl_acme_detect_probe(unsigned int addr,
87 int prb_num, const char *prb_name)
89 gboolean ret = FALSE, status;
91 GString *path = g_string_sized_new(64);
95 probe_name_path(addr, path);
96 status = g_file_get_contents(path->str, &buf, &size, &err);
98 sr_dbg("Name for probe %d can't be read: %s",
99 prb_num, err->message);
100 g_string_free(path, TRUE);
105 if (strncmp(buf, prb_name, strlen(prb_name)) == 0) {
107 * Correct driver registered on this address - but is
108 * there an actual probe connected?
110 probe_hwmon_path(addr, path);
111 status = g_file_test(path->str, G_FILE_TEST_IS_DIR);
113 /* We have found an ACME probe. */
119 g_string_free(path, TRUE);
124 static int get_hwmon_index(unsigned int addr)
127 GString *path = g_string_sized_new(64);
131 probe_hwmon_path(addr, path);
132 dir = g_dir_open(path->str, 0, &err);
134 sr_err("Error opening %s: %s", path->str, err->message);
135 g_string_free(path, TRUE);
139 g_string_free(path, TRUE);
142 * The directory should contain a single file named hwmonX where X
143 * is the hwmon index.
145 status = sscanf(g_dir_read_name(dir), "hwmon%d", &hwmon);
148 sr_err("Unable to determine the hwmon entry");
155 static void append_channel(struct sr_dev_inst *sdi,
156 struct sr_channel_group *cg,
159 struct channel_priv *cp;
160 struct dev_context *devc;
161 struct sr_channel *ch;
168 name = g_strdup_printf("P%d_ENRG_PWR", index);
171 name = g_strdup_printf("P%d_ENRG_CURR", index);
174 name = g_strdup_printf("P%d_ENRG_VOL", index);
177 name = g_strdup_printf("P%d_TEMP_IN", index);
180 name = g_strdup_printf("P%d_TEMP_OUT", index);
183 sr_err("Bug in the code: invalid channel type!");
187 cp = g_malloc0(sizeof(struct channel_priv));
189 cp->probe = cg->priv;
191 ch = sr_channel_new(devc->num_channels++,
192 SR_CHANNEL_ANALOG, TRUE, name);
196 cg->channels = g_slist_append(cg->channels, ch);
197 sdi->channels = g_slist_append(sdi->channels, ch);
200 SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type,
201 unsigned int addr, int prb_num)
203 struct sr_channel_group *cg;
204 struct channel_group_priv *cgp;
207 /* Obtain the hwmon index. */
208 hwmon = get_hwmon_index(addr);
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);
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);
226 sr_err("Bug in the code: invalid probe type!");
229 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
234 static int channel_to_mq(struct sr_channel *ch)
236 struct channel_priv *chp;
240 switch (chp->ch_type) {
244 return SR_MQ_CURRENT;
246 return SR_MQ_VOLTAGE;
249 return SR_MQ_TEMPERATURE;
255 static int channel_to_unit(struct sr_channel *ch)
257 struct channel_priv *chp;
261 switch (chp->ch_type) {
265 return SR_UNIT_AMPERE;
270 return SR_UNIT_CELSIUS;
276 /* We need to scale measurements down from the units used by the drivers. */
277 static float adjust_data(int val, int type)
281 return ((float)val) / 1000000.0;
286 return ((float)val) / 1000.0;
292 static float read_sample(struct sr_channel *ch)
294 struct channel_priv *chp;
295 char path[64], *file, buf[16];
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;
308 sr_err("Bug in the code: invalid channel type!");
312 snprintf(path, sizeof(path),
313 "/sys/class/hwmon/hwmon%d/%s",
314 chp->probe->hwmon_num, file);
315 fd = open(path, O_RDONLY);
317 sr_err("Error opening %s: %s", path, strerror(errno));
322 len = read(fd, buf, sizeof(buf));
325 sr_err("error reading from %s: %s", path, strerror(errno));
330 return adjust_data(strtol(buf, NULL, 10), chp->ch_type);
333 SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data)
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;
341 struct dev_context *devc;
356 packet.type = SR_DF_ANALOG;
357 packet.payload = &analog;
358 memset(&analog, 0, sizeof(analog));
362 * Reading from sysfs takes some time - try to
363 * keep up with samplerate.
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);
373 framep.type = SR_DF_FRAME_BEGIN;
374 sr_session_send(cb_data, &framep);
377 * Due to different units used in each channel we're sending
378 * samples one-by-one.
380 for (chl = sdi->channels; chl; chl = chl->next) {
386 analog.channels = &chonly;
387 analog.num_samples = 1;
388 analog.mq = channel_to_mq(chl->data);
389 analog.unit = channel_to_unit(ch);
391 valf = read_sample(ch);
393 sr_session_send(cb_data, &packet);
396 framep.type = SR_DF_FRAME_END;
397 sr_session_send(cb_data, &framep);
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();
406 } else if (devc->limit_msec > 0) {
407 cur_time = g_get_monotonic_time();
408 elapsed_time = cur_time - devc->start_time;
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();
418 devc->last_sample_fin = g_get_monotonic_time();