]>
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 | ||
7c0891b0 | 24 | SR_PRIV int rdtech_dps_get_reg(const struct sr_dev_inst *sdi, |
69b05583 JC |
25 | uint16_t address, uint16_t *value) |
26 | { | |
7c0891b0 FS |
27 | struct dev_context *devc; |
28 | struct sr_modbus_dev_inst *modbus; | |
69b05583 | 29 | uint16_t registers[1]; |
7c0891b0 FS |
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); | |
69b05583 JC |
38 | *value = RB16(registers + 0); |
39 | return ret; | |
40 | } | |
41 | ||
7c0891b0 | 42 | SR_PRIV int rdtech_dps_set_reg(const struct sr_dev_inst *sdi, |
69b05583 JC |
43 | uint16_t address, uint16_t value) |
44 | { | |
7c0891b0 FS |
45 | struct dev_context *devc; |
46 | struct sr_modbus_dev_inst *modbus; | |
69b05583 | 47 | uint16_t registers[1]; |
7c0891b0 FS |
48 | int ret; |
49 | ||
50 | devc = sdi->priv; | |
51 | modbus = sdi->conn; | |
52 | ||
69b05583 | 53 | WB16(registers, value); |
7c0891b0 FS |
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; | |
69b05583 JC |
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; | |
7c0891b0 FS |
65 | |
66 | /* | |
67 | * No mutex here, because there is no sr_dev_inst when this function | |
68 | * is called. | |
69 | */ | |
69b05583 JC |
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, | |
c9b187a6 FS |
80 | float value, enum sr_mq mq, enum sr_mqflag mqflags, |
81 | enum sr_unit unit, int digits) | |
69b05583 JC |
82 | { |
83 | struct sr_datafeed_packet packet; | |
84 | struct sr_datafeed_analog analog; | |
85 | struct sr_analog_encoding encoding; | |
86 | struct sr_analog_meaning meaning; | |
87 | struct sr_analog_spec spec; | |
88 | ||
89 | sr_analog_init(&analog, &encoding, &meaning, &spec, digits); | |
90 | analog.meaning->channels = g_slist_append(NULL, ch); | |
91 | analog.num_samples = 1; | |
92 | analog.data = &value; | |
93 | analog.meaning->mq = mq; | |
c9b187a6 | 94 | analog.meaning->mqflags = mqflags; |
69b05583 | 95 | analog.meaning->unit = unit; |
69b05583 JC |
96 | |
97 | packet.type = SR_DF_ANALOG; | |
98 | packet.payload = &analog; | |
99 | sr_session_send(sdi, &packet); | |
100 | g_slist_free(analog.meaning->channels); | |
101 | } | |
102 | ||
0549416e JC |
103 | SR_PRIV int rdtech_dps_receive_data(int fd, int revents, void *cb_data) |
104 | { | |
69b05583 | 105 | struct sr_dev_inst *sdi; |
0549416e | 106 | struct dev_context *devc; |
69b05583 JC |
107 | struct sr_modbus_dev_inst *modbus; |
108 | struct sr_datafeed_packet packet; | |
109 | uint16_t registers[3]; | |
7c0891b0 | 110 | int ret; |
0549416e JC |
111 | |
112 | (void)fd; | |
69b05583 | 113 | (void)revents; |
0549416e JC |
114 | |
115 | if (!(sdi = cb_data)) | |
116 | return TRUE; | |
117 | ||
69b05583 JC |
118 | modbus = sdi->conn; |
119 | devc = sdi->priv; | |
120 | ||
7c0891b0 FS |
121 | g_mutex_lock(&devc->rw_mutex); |
122 | ret = sr_modbus_read_holding_registers(modbus, REG_UOUT, 3, registers); | |
123 | g_mutex_unlock(&devc->rw_mutex); | |
124 | ||
125 | if (ret == SR_OK) { | |
69b05583 JC |
126 | packet.type = SR_DF_FRAME_BEGIN; |
127 | sr_session_send(sdi, &packet); | |
128 | ||
129 | send_value(sdi, sdi->channels->data, | |
130 | RB16(registers + 0) / 100.0f, | |
c9b187a6 | 131 | SR_MQ_VOLTAGE, SR_MQFLAG_DC, SR_UNIT_VOLT, 3); |
69b05583 JC |
132 | send_value(sdi, sdi->channels->next->data, |
133 | RB16(registers + 1) / 1000.0f, | |
c9b187a6 | 134 | SR_MQ_CURRENT, SR_MQFLAG_DC, SR_UNIT_AMPERE, 4); |
69b05583 JC |
135 | send_value(sdi, sdi->channels->next->next->data, |
136 | RB16(registers + 2) / 100.0f, | |
c9b187a6 | 137 | SR_MQ_POWER, 0, SR_UNIT_WATT, 3); |
0549416e | 138 | |
69b05583 JC |
139 | packet.type = SR_DF_FRAME_END; |
140 | sr_session_send(sdi, &packet); | |
141 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
142 | } | |
143 | ||
144 | if (sr_sw_limits_check(&devc->limits)) { | |
145 | sr_dev_acquisition_stop(sdi); | |
146 | return TRUE; | |
0549416e JC |
147 | } |
148 | ||
149 | return TRUE; | |
150 | } |