]>
Commit | Line | Data |
---|---|---|
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 |
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 | ||
7c0891b0 | 39 | SR_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 | 57 | SR_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 | ||
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; | |
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 | ||
94 | static 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 |
118 | SR_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; | |
124 | uint16_t registers[3]; | |
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 | */ | |
7c0891b0 FS |
141 | ret = sr_modbus_read_holding_registers(modbus, REG_UOUT, 3, registers); |
142 | g_mutex_unlock(&devc->rw_mutex); | |
143 | ||
144 | if (ret == SR_OK) { | |
69b05583 JC |
145 | packet.type = SR_DF_FRAME_BEGIN; |
146 | sr_session_send(sdi, &packet); | |
147 | ||
148 | send_value(sdi, sdi->channels->data, | |
149 | RB16(registers + 0) / 100.0f, | |
c9b187a6 | 150 | SR_MQ_VOLTAGE, SR_MQFLAG_DC, SR_UNIT_VOLT, 3); |
69b05583 JC |
151 | send_value(sdi, sdi->channels->next->data, |
152 | RB16(registers + 1) / 1000.0f, | |
c9b187a6 | 153 | SR_MQ_CURRENT, SR_MQFLAG_DC, SR_UNIT_AMPERE, 4); |
69b05583 JC |
154 | send_value(sdi, sdi->channels->next->next->data, |
155 | RB16(registers + 2) / 100.0f, | |
c9b187a6 | 156 | SR_MQ_POWER, 0, SR_UNIT_WATT, 3); |
0549416e | 157 | |
69b05583 JC |
158 | packet.type = SR_DF_FRAME_END; |
159 | sr_session_send(sdi, &packet); | |
160 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
161 | } | |
162 | ||
163 | if (sr_sw_limits_check(&devc->limits)) { | |
164 | sr_dev_acquisition_stop(sdi); | |
165 | return TRUE; | |
0549416e JC |
166 | } |
167 | ||
168 | return TRUE; | |
169 | } |