]> sigrok.org Git - libsigrok.git/blame - src/hardware/rdtech-dps/protocol.c
rdtech-dps: Send META package when states have changed.
[libsigrok.git] / src / hardware / rdtech-dps / protocol.c
CommitLineData
0549416e
JC
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2018 James Churchill <pelrun@gmail.com>
7c0891b0 5 * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
0549416e
JC
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <config.h>
22#include "protocol.h"
23
aff20941
FS
24SR_PRIV int rdtech_dps_read_holding_registers(struct sr_modbus_dev_inst *modbus,
25 int address, int nb_registers, uint16_t *registers)
26{
27 int i, ret;
28
29 i = 0;
30 do {
31 ret = sr_modbus_read_holding_registers(modbus,
32 address, nb_registers, registers);
33 ++i;
34 } while (ret != SR_OK && i < 3);
35
36 return ret;
37}
38
7c0891b0 39SR_PRIV int rdtech_dps_get_reg(const struct sr_dev_inst *sdi,
69b05583
JC
40 uint16_t address, uint16_t *value)
41{
7c0891b0
FS
42 struct dev_context *devc;
43 struct sr_modbus_dev_inst *modbus;
69b05583 44 uint16_t registers[1];
7c0891b0
FS
45 int ret;
46
47 devc = sdi->priv;
48 modbus = sdi->conn;
49
50 g_mutex_lock(&devc->rw_mutex);
aff20941 51 ret = rdtech_dps_read_holding_registers(modbus, address, 1, registers);
7c0891b0 52 g_mutex_unlock(&devc->rw_mutex);
69b05583
JC
53 *value = RB16(registers + 0);
54 return ret;
55}
56
7c0891b0 57SR_PRIV int rdtech_dps_set_reg(const struct sr_dev_inst *sdi,
69b05583
JC
58 uint16_t address, uint16_t value)
59{
7c0891b0
FS
60 struct dev_context *devc;
61 struct sr_modbus_dev_inst *modbus;
69b05583 62 uint16_t registers[1];
7c0891b0
FS
63 int ret;
64
65 devc = sdi->priv;
66 modbus = sdi->conn;
67
69b05583 68 WB16(registers, value);
7c0891b0
FS
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);
72 return ret;
69b05583
JC
73}
74
75SR_PRIV int rdtech_dps_get_model_version(struct sr_modbus_dev_inst *modbus,
76 uint16_t *model, uint16_t *version)
77{
78 uint16_t registers[2];
79 int ret;
7c0891b0
FS
80
81 /*
82 * No mutex here, because there is no sr_dev_inst when this function
83 * is called.
84 */
aff20941 85 ret = rdtech_dps_read_holding_registers(modbus, REG_MODEL, 2, registers);
69b05583
JC
86 if (ret == SR_OK) {
87 *model = RB16(registers + 0);
88 *version = RB16(registers + 1);
89 sr_info("RDTech PSU model: %d version: %d", *model, *version);
90 }
91 return ret;
92}
93
94static void send_value(const struct sr_dev_inst *sdi, struct sr_channel *ch,
c9b187a6
FS
95 float value, enum sr_mq mq, enum sr_mqflag mqflags,
96 enum sr_unit unit, int digits)
69b05583
JC
97{
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;
103
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;
c9b187a6 109 analog.meaning->mqflags = mqflags;
69b05583 110 analog.meaning->unit = unit;
69b05583
JC
111
112 packet.type = SR_DF_ANALOG;
113 packet.payload = &analog;
114 sr_session_send(sdi, &packet);
115 g_slist_free(analog.meaning->channels);
116}
117
0549416e
JC
118SR_PRIV int rdtech_dps_receive_data(int fd, int revents, void *cb_data)
119{
69b05583 120 struct sr_dev_inst *sdi;
0549416e 121 struct dev_context *devc;
69b05583
JC
122 struct sr_modbus_dev_inst *modbus;
123 struct sr_datafeed_packet packet;
dfdf4c83 124 uint16_t registers[8];
7c0891b0 125 int ret;
0549416e
JC
126
127 (void)fd;
69b05583 128 (void)revents;
0549416e
JC
129
130 if (!(sdi = cb_data))
131 return TRUE;
132
69b05583
JC
133 modbus = sdi->conn;
134 devc = sdi->priv;
135
7c0891b0 136 g_mutex_lock(&devc->rw_mutex);
aff20941
FS
137 /*
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.
140 */
dfdf4c83 141 ret = sr_modbus_read_holding_registers(modbus, REG_UOUT, 8, registers);
7c0891b0
FS
142 g_mutex_unlock(&devc->rw_mutex);
143
144 if (ret == SR_OK) {
dfdf4c83 145 /* Send channel values */
69b05583
JC
146 packet.type = SR_DF_FRAME_BEGIN;
147 sr_session_send(sdi, &packet);
148
149 send_value(sdi, sdi->channels->data,
cce6a8a1
FS
150 RB16(registers + 0) / devc->voltage_multiplier,
151 SR_MQ_VOLTAGE, SR_MQFLAG_DC, SR_UNIT_VOLT,
152 devc->model->voltage_digits);
69b05583 153 send_value(sdi, sdi->channels->next->data,
cce6a8a1
FS
154 RB16(registers + 1) / devc->current_multiplier,
155 SR_MQ_CURRENT, SR_MQFLAG_DC, SR_UNIT_AMPERE,
156 devc->model->current_digits);
69b05583
JC
157 send_value(sdi, sdi->channels->next->next->data,
158 RB16(registers + 2) / 100.0f,
cce6a8a1 159 SR_MQ_POWER, 0, SR_UNIT_WATT, 2);
0549416e 160
69b05583
JC
161 packet.type = SR_DF_FRAME_END;
162 sr_session_send(sdi, &packet);
dfdf4c83
FS
163
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));
169 }
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));
174 }
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"));
180 }
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));
185 }
186
69b05583
JC
187 sr_sw_limits_update_samples_read(&devc->limits, 1);
188 }
189
190 if (sr_sw_limits_check(&devc->limits)) {
191 sr_dev_acquisition_stop(sdi);
192 return TRUE;
0549416e
JC
193 }
194
195 return TRUE;
196}