]> sigrok.org Git - libsigrok.git/blob - src/hardware/rdtech-dps/protocol.c
rdtech-dps: New driver for RDTech DPS/DPH series PSUs.
[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  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include "protocol.h"
22
23 SR_PRIV int rdtech_dps_get_reg(struct sr_modbus_dev_inst *modbus,
24                 uint16_t address, uint16_t *value)
25 {
26         uint16_t registers[1];
27         int ret = sr_modbus_read_holding_registers(modbus, address, 1, registers);
28         *value = RB16(registers + 0);
29         return ret;
30 }
31
32 SR_PRIV int rdtech_dps_set_reg(struct sr_modbus_dev_inst *modbus,
33                 uint16_t address, uint16_t value)
34 {
35         uint16_t registers[1];
36         WB16(registers, value);
37         return sr_modbus_write_multiple_registers(modbus, address, 1, registers);
38 }
39
40 SR_PRIV int rdtech_dps_get_model_version(struct sr_modbus_dev_inst *modbus,
41                 uint16_t *model, uint16_t *version)
42 {
43         uint16_t registers[2];
44         int ret;
45         ret = sr_modbus_read_holding_registers(modbus, REG_MODEL, 2, registers);
46         if (ret == SR_OK) {
47                 *model = RB16(registers + 0);
48                 *version = RB16(registers + 1);
49                 sr_info("RDTech PSU model: %d version: %d", *model, *version);
50         }
51         return ret;
52 }
53
54 static void send_value(const struct sr_dev_inst *sdi, struct sr_channel *ch,
55                 float value, enum sr_mq mq, enum sr_unit unit, int digits)
56 {
57         struct sr_datafeed_packet packet;
58         struct sr_datafeed_analog analog;
59         struct sr_analog_encoding encoding;
60         struct sr_analog_meaning meaning;
61         struct sr_analog_spec spec;
62
63         sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
64         analog.meaning->channels = g_slist_append(NULL, ch);
65         analog.num_samples = 1;
66         analog.data = &value;
67         analog.meaning->mq = mq;
68         analog.meaning->unit = unit;
69         analog.meaning->mqflags = SR_MQFLAG_DC;
70
71         packet.type = SR_DF_ANALOG;
72         packet.payload = &analog;
73         sr_session_send(sdi, &packet);
74         g_slist_free(analog.meaning->channels);
75 }
76
77 SR_PRIV int rdtech_dps_capture_start(const struct sr_dev_inst *sdi)
78 {
79         struct dev_context *devc;
80         struct sr_modbus_dev_inst *modbus;
81         int ret;
82
83         modbus = sdi->conn;
84         devc = sdi->priv;
85
86         if ((ret = sr_modbus_read_holding_registers(modbus, REG_UOUT, 3, NULL)) == SR_OK)
87                 devc->expecting_registers = 2;
88         return ret;
89 }
90
91 SR_PRIV int rdtech_dps_receive_data(int fd, int revents, void *cb_data)
92 {
93         struct sr_dev_inst *sdi;
94         struct dev_context *devc;
95         struct sr_modbus_dev_inst *modbus;
96         struct sr_datafeed_packet packet;
97         uint16_t registers[3];
98
99         (void)fd;
100         (void)revents;
101
102         if (!(sdi = cb_data))
103                 return TRUE;
104
105         modbus = sdi->conn;
106         devc = sdi->priv;
107
108         devc->expecting_registers = 0;
109         if (sr_modbus_read_holding_registers(modbus, -1, 3, registers) == SR_OK) {
110                 packet.type = SR_DF_FRAME_BEGIN;
111                 sr_session_send(sdi, &packet);
112
113                 send_value(sdi, sdi->channels->data,
114                         RB16(registers + 0) / 100.0f,
115                         SR_MQ_VOLTAGE, SR_UNIT_VOLT, 3);
116                 send_value(sdi, sdi->channels->next->data,
117                         RB16(registers + 1) / 1000.0f,
118                         SR_MQ_CURRENT, SR_UNIT_AMPERE, 4);
119                 send_value(sdi, sdi->channels->next->next->data,
120                         RB16(registers + 2) / 100.0f,
121                         SR_MQ_POWER, SR_UNIT_WATT, 3);
122
123                 packet.type = SR_DF_FRAME_END;
124                 sr_session_send(sdi, &packet);
125                 sr_sw_limits_update_samples_read(&devc->limits, 1);
126         }
127
128         if (sr_sw_limits_check(&devc->limits)) {
129                 sr_dev_acquisition_stop(sdi);
130                 return TRUE;
131         }
132
133         rdtech_dps_capture_start(sdi);
134         return TRUE;
135 }