2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 James Churchill <pelrun@gmail.com>
5 * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 SR_PRIV int rdtech_dps_get_reg(const struct sr_dev_inst *sdi,
25 uint16_t address, uint16_t *value)
27 struct dev_context *devc;
28 struct sr_modbus_dev_inst *modbus;
29 uint16_t registers[1];
35 g_mutex_lock(&devc->rw_mutex);
36 ret = sr_modbus_read_holding_registers(modbus, address, 1, registers);
37 g_mutex_unlock(&devc->rw_mutex);
38 *value = RB16(registers + 0);
42 SR_PRIV int rdtech_dps_set_reg(const struct sr_dev_inst *sdi,
43 uint16_t address, uint16_t value)
45 struct dev_context *devc;
46 struct sr_modbus_dev_inst *modbus;
47 uint16_t registers[1];
53 WB16(registers, value);
54 g_mutex_lock(&devc->rw_mutex);
55 ret = sr_modbus_write_multiple_registers(modbus, address, 1, registers);
56 g_mutex_unlock(&devc->rw_mutex);
60 SR_PRIV int rdtech_dps_get_model_version(struct sr_modbus_dev_inst *modbus,
61 uint16_t *model, uint16_t *version)
63 uint16_t registers[2];
67 * No mutex here, because there is no sr_dev_inst when this function
70 ret = sr_modbus_read_holding_registers(modbus, REG_MODEL, 2, registers);
72 *model = RB16(registers + 0);
73 *version = RB16(registers + 1);
74 sr_info("RDTech PSU model: %d version: %d", *model, *version);
79 static void send_value(const struct sr_dev_inst *sdi, struct sr_channel *ch,
80 float value, enum sr_mq mq, enum sr_unit unit, int digits)
82 struct sr_datafeed_packet packet;
83 struct sr_datafeed_analog analog;
84 struct sr_analog_encoding encoding;
85 struct sr_analog_meaning meaning;
86 struct sr_analog_spec spec;
88 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
89 analog.meaning->channels = g_slist_append(NULL, ch);
90 analog.num_samples = 1;
92 analog.meaning->mq = mq;
93 analog.meaning->unit = unit;
94 analog.meaning->mqflags = SR_MQFLAG_DC;
96 packet.type = SR_DF_ANALOG;
97 packet.payload = &analog;
98 sr_session_send(sdi, &packet);
99 g_slist_free(analog.meaning->channels);
102 SR_PRIV int rdtech_dps_receive_data(int fd, int revents, void *cb_data)
104 struct sr_dev_inst *sdi;
105 struct dev_context *devc;
106 struct sr_modbus_dev_inst *modbus;
107 struct sr_datafeed_packet packet;
108 uint16_t registers[3];
114 if (!(sdi = cb_data))
120 g_mutex_lock(&devc->rw_mutex);
121 ret = sr_modbus_read_holding_registers(modbus, REG_UOUT, 3, registers);
122 g_mutex_unlock(&devc->rw_mutex);
125 packet.type = SR_DF_FRAME_BEGIN;
126 sr_session_send(sdi, &packet);
128 send_value(sdi, sdi->channels->data,
129 RB16(registers + 0) / 100.0f,
130 SR_MQ_VOLTAGE, SR_UNIT_VOLT, 3);
131 send_value(sdi, sdi->channels->next->data,
132 RB16(registers + 1) / 1000.0f,
133 SR_MQ_CURRENT, SR_UNIT_AMPERE, 4);
134 send_value(sdi, sdi->channels->next->next->data,
135 RB16(registers + 2) / 100.0f,
136 SR_MQ_POWER, SR_UNIT_WATT, 3);
138 packet.type = SR_DF_FRAME_END;
139 sr_session_send(sdi, &packet);
140 sr_sw_limits_update_samples_read(&devc->limits, 1);
143 if (sr_sw_limits_check(&devc->limits)) {
144 sr_dev_acquisition_stop(sdi);