]> sigrok.org Git - libsigrok.git/blob - src/hardware/rdtech-dps/protocol.c
rdtech-dps: Send META package when states have changed.
[libsigrok.git] / src / hardware / rdtech-dps / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2018 James Churchill <pelrun@gmail.com>
5  * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
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
24 SR_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
39 SR_PRIV int rdtech_dps_get_reg(const struct sr_dev_inst *sdi,
40                 uint16_t address, uint16_t *value)
41 {
42         struct dev_context *devc;
43         struct sr_modbus_dev_inst *modbus;
44         uint16_t registers[1];
45         int ret;
46
47         devc = sdi->priv;
48         modbus = sdi->conn;
49
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);
54         return ret;
55 }
56
57 SR_PRIV int rdtech_dps_set_reg(const struct sr_dev_inst *sdi,
58                 uint16_t address, uint16_t value)
59 {
60         struct dev_context *devc;
61         struct sr_modbus_dev_inst *modbus;
62         uint16_t registers[1];
63         int ret;
64
65         devc = sdi->priv;
66         modbus = sdi->conn;
67
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);
72         return ret;
73 }
74
75 SR_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;
80
81         /*
82          * No mutex here, because there is no sr_dev_inst when this function
83          * is called.
84          */
85         ret = rdtech_dps_read_holding_registers(modbus, REG_MODEL, 2, registers);
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
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)
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;
109         analog.meaning->mqflags = mqflags;
110         analog.meaning->unit = unit;
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
118 SR_PRIV int rdtech_dps_receive_data(int fd, int revents, void *cb_data)
119 {
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];
125         int ret;
126
127         (void)fd;
128         (void)revents;
129
130         if (!(sdi = cb_data))
131                 return TRUE;
132
133         modbus = sdi->conn;
134         devc = sdi->priv;
135
136         g_mutex_lock(&devc->rw_mutex);
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          */
141         ret = sr_modbus_read_holding_registers(modbus, REG_UOUT, 8, registers);
142         g_mutex_unlock(&devc->rw_mutex);
143
144         if (ret == SR_OK) {
145                 /* Send channel values */
146                 packet.type = SR_DF_FRAME_BEGIN;
147                 sr_session_send(sdi, &packet);
148
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);
160
161                 packet.type = SR_DF_FRAME_END;
162                 sr_session_send(sdi, &packet);
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
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;
193         }
194
195         return TRUE;
196 }