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_read_holding_registers(struct sr_modbus_dev_inst *modbus,
25 int address, int nb_registers, uint16_t *registers)
31 ret = sr_modbus_read_holding_registers(modbus,
32 address, nb_registers, registers);
34 } while (ret != SR_OK && i < 3);
39 SR_PRIV int rdtech_dps_get_reg(const struct sr_dev_inst *sdi,
40 uint16_t address, uint16_t *value)
42 struct dev_context *devc;
43 struct sr_modbus_dev_inst *modbus;
44 uint16_t registers[1];
50 g_mutex_lock(&devc->rw_mutex);
51 ret = rdtech_dps_read_holding_registers(modbus, address, 1, registers);
52 g_mutex_unlock(&devc->rw_mutex);
53 *value = RB16(registers + 0);
57 SR_PRIV int rdtech_dps_set_reg(const struct sr_dev_inst *sdi,
58 uint16_t address, uint16_t value)
60 struct dev_context *devc;
61 struct sr_modbus_dev_inst *modbus;
62 uint16_t registers[1];
68 WB16(registers, value);
69 g_mutex_lock(&devc->rw_mutex);
70 ret = sr_modbus_write_multiple_registers(modbus, address, 1, registers);
71 g_mutex_unlock(&devc->rw_mutex);
75 SR_PRIV int rdtech_dps_get_model_version(struct sr_modbus_dev_inst *modbus,
76 uint16_t *model, uint16_t *version)
78 uint16_t registers[2];
82 * No mutex here, because there is no sr_dev_inst when this function
85 ret = rdtech_dps_read_holding_registers(modbus, REG_MODEL, 2, registers);
87 *model = RB16(registers + 0);
88 *version = RB16(registers + 1);
89 sr_info("RDTech PSU model: %d version: %d", *model, *version);
94 static void send_value(const struct sr_dev_inst *sdi, struct sr_channel *ch,
95 float value, enum sr_mq mq, enum sr_mqflag mqflags,
96 enum sr_unit unit, int digits)
98 struct sr_datafeed_packet packet;
99 struct sr_datafeed_analog analog;
100 struct sr_analog_encoding encoding;
101 struct sr_analog_meaning meaning;
102 struct sr_analog_spec spec;
104 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
105 analog.meaning->channels = g_slist_append(NULL, ch);
106 analog.num_samples = 1;
107 analog.data = &value;
108 analog.meaning->mq = mq;
109 analog.meaning->mqflags = mqflags;
110 analog.meaning->unit = unit;
112 packet.type = SR_DF_ANALOG;
113 packet.payload = &analog;
114 sr_session_send(sdi, &packet);
115 g_slist_free(analog.meaning->channels);
118 SR_PRIV int rdtech_dps_receive_data(int fd, int revents, void *cb_data)
120 struct sr_dev_inst *sdi;
121 struct dev_context *devc;
122 struct sr_modbus_dev_inst *modbus;
123 struct sr_datafeed_packet packet;
124 uint16_t registers[8];
130 if (!(sdi = cb_data))
136 g_mutex_lock(&devc->rw_mutex);
138 * Using the libsigrok function here, because it doesn't matter if the
139 * reading fails. It will be done again in the next acquision cycle anyways.
141 ret = sr_modbus_read_holding_registers(modbus, REG_UOUT, 8, registers);
142 g_mutex_unlock(&devc->rw_mutex);
145 /* Send channel values */
146 packet.type = SR_DF_FRAME_BEGIN;
147 sr_session_send(sdi, &packet);
149 send_value(sdi, sdi->channels->data,
150 RB16(registers + 0) / devc->voltage_multiplier,
151 SR_MQ_VOLTAGE, SR_MQFLAG_DC, SR_UNIT_VOLT,
152 devc->model->voltage_digits);
153 send_value(sdi, sdi->channels->next->data,
154 RB16(registers + 1) / devc->current_multiplier,
155 SR_MQ_CURRENT, SR_MQFLAG_DC, SR_UNIT_AMPERE,
156 devc->model->current_digits);
157 send_value(sdi, sdi->channels->next->next->data,
158 RB16(registers + 2) / 100.0f,
159 SR_MQ_POWER, 0, SR_UNIT_WATT, 2);
161 packet.type = SR_DF_FRAME_END;
162 sr_session_send(sdi, &packet);
164 /* Check for state changes */
165 if (devc->actual_ovp_state != (RB16(registers + 5) == STATE_OVP)) {
166 devc->actual_ovp_state = RB16(registers + 5) == STATE_OVP;
167 sr_session_send_meta(sdi, SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE,
168 g_variant_new_boolean(devc->actual_ovp_state));
170 if (devc->actual_ocp_state != (RB16(registers + 5) == STATE_OCP)) {
171 devc->actual_ocp_state = RB16(registers + 5) == STATE_OCP;
172 sr_session_send_meta(sdi, SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE,
173 g_variant_new_boolean(devc->actual_ocp_state));
175 if (devc->actual_regulation_state != RB16(registers + 6)) {
176 devc->actual_regulation_state = RB16(registers + 6);
177 sr_session_send_meta(sdi, SR_CONF_REGULATION,
178 g_variant_new_string(
179 devc->actual_regulation_state == MODE_CC ? "CC" : "CV"));
181 if (devc->actual_output_state != RB16(registers + 7)) {
182 devc->actual_output_state = RB16(registers + 7);
183 sr_session_send_meta(sdi, SR_CONF_ENABLED,
184 g_variant_new_boolean(devc->actual_output_state));
187 sr_sw_limits_update_samples_read(&devc->limits, 1);
190 if (sr_sw_limits_check(&devc->limits)) {
191 sr_dev_acquisition_stop(sdi);