]> sigrok.org Git - libsigrok.git/blob - src/hardware/rdtech-dps/protocol.c
rdtech-dps: Synchronize read and write operations.
[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_get_reg(const struct sr_dev_inst *sdi,
25                 uint16_t address, uint16_t *value)
26 {
27         struct dev_context *devc;
28         struct sr_modbus_dev_inst *modbus;
29         uint16_t registers[1];
30         int ret;
31
32         devc = sdi->priv;
33         modbus = sdi->conn;
34
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);
39         return ret;
40 }
41
42 SR_PRIV int rdtech_dps_set_reg(const struct sr_dev_inst *sdi,
43                 uint16_t address, uint16_t value)
44 {
45         struct dev_context *devc;
46         struct sr_modbus_dev_inst *modbus;
47         uint16_t registers[1];
48         int ret;
49
50         devc = sdi->priv;
51         modbus = sdi->conn;
52
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);
57         return ret;
58 }
59
60 SR_PRIV int rdtech_dps_get_model_version(struct sr_modbus_dev_inst *modbus,
61                 uint16_t *model, uint16_t *version)
62 {
63         uint16_t registers[2];
64         int ret;
65
66         /*
67          * No mutex here, because there is no sr_dev_inst when this function
68          * is called.
69          */
70         ret = sr_modbus_read_holding_registers(modbus, REG_MODEL, 2, registers);
71         if (ret == SR_OK) {
72                 *model = RB16(registers + 0);
73                 *version = RB16(registers + 1);
74                 sr_info("RDTech PSU model: %d version: %d", *model, *version);
75         }
76         return ret;
77 }
78
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)
81 {
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;
87
88         sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
89         analog.meaning->channels = g_slist_append(NULL, ch);
90         analog.num_samples = 1;
91         analog.data = &value;
92         analog.meaning->mq = mq;
93         analog.meaning->unit = unit;
94         analog.meaning->mqflags = SR_MQFLAG_DC;
95
96         packet.type = SR_DF_ANALOG;
97         packet.payload = &analog;
98         sr_session_send(sdi, &packet);
99         g_slist_free(analog.meaning->channels);
100 }
101
102 SR_PRIV int rdtech_dps_receive_data(int fd, int revents, void *cb_data)
103 {
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];
109         int ret;
110
111         (void)fd;
112         (void)revents;
113
114         if (!(sdi = cb_data))
115                 return TRUE;
116
117         modbus = sdi->conn;
118         devc = sdi->priv;
119
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);
123
124         if (ret == SR_OK) {
125                 packet.type = SR_DF_FRAME_BEGIN;
126                 sr_session_send(sdi, &packet);
127
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);
137
138                 packet.type = SR_DF_FRAME_END;
139                 sr_session_send(sdi, &packet);
140                 sr_sw_limits_update_samples_read(&devc->limits, 1);
141         }
142
143         if (sr_sw_limits_check(&devc->limits)) {
144                 sr_dev_acquisition_stop(sdi);
145                 return TRUE;
146         }
147
148         return TRUE;
149 }