2 * This file is part of the libsigrok project.
4 * Copyright (C) 2020 Timo Kokkonen <tjko@iki.fi>
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.
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.
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/>.
24 SR_PRIV uint8_t itech_it8500_checksum(const uint8_t *packet)
35 for (i = 0; i < IT8500_PACKET_LEN - 1; i++)
41 SR_PRIV const char *itech_it8500_mode_to_string(enum itech_it8500_modes mode)
57 SR_PRIV int itech_it8500_string_to_mode(const char *modename,
58 enum itech_it8500_modes *mode)
63 for (i = 0; i < IT8500_MODES; i++) {
64 s = itech_it8500_mode_to_string(i);
65 if (strncmp(modename, s, strlen(s)) == 0) {
74 SR_PRIV int itech_it8500_send_cmd(struct sr_serial_dev_inst *serial,
75 struct itech_it8500_cmd_packet *cmd,
76 struct itech_it8500_cmd_packet **response)
78 struct itech_it8500_cmd_packet *resp;
79 uint8_t *cmd_buf, *resp_buf, checksum;
82 if (!serial || !cmd || !response)
85 cmd_buf = g_malloc0(IT8500_PACKET_LEN);
86 resp_buf = g_malloc0(IT8500_PACKET_LEN);
87 resp = g_malloc0(sizeof(*resp));
88 if (!cmd_buf || !resp_buf || !resp)
92 * Construct request from: preamble, address, command, data,
95 cmd_buf[0] = IT8500_PREAMBLE;
96 cmd_buf[1] = cmd->address;
97 cmd_buf[2] = cmd->command;
98 memcpy(&cmd_buf[3], cmd->data, IT8500_DATA_LEN);
99 cmd_buf[IT8500_PACKET_LEN - 1] = itech_it8500_checksum(cmd_buf);
101 sr_spew("%s: Sending command: %02x", __func__, cmd->command);
102 ret = serial_write_blocking(serial, cmd_buf, IT8500_PACKET_LEN,
103 serial_timeout(serial, IT8500_PACKET_LEN));
104 if (ret < IT8500_PACKET_LEN) {
105 sr_dbg("%s: Error sending command 0x%02x: %d", __func__,
112 read_len = serial_read_blocking(serial, resp_buf, IT8500_PACKET_LEN,
114 if (read_len < IT8500_PACKET_LEN) {
115 sr_dbg("%s: Timeout waiting response to command: %d",
120 if (resp_buf[0] != IT8500_PREAMBLE) {
121 sr_dbg("%s: Invalid packet received (first byte: %02x)",
122 __func__, resp_buf[0]);
126 checksum = itech_it8500_checksum(resp_buf);
127 if (resp_buf[IT8500_PACKET_LEN - 1] != checksum) {
128 sr_dbg("%s: Invalid packet received: checksum mismatch",
133 resp->address = resp_buf[1];
134 resp->command = resp_buf[2];
135 memcpy(resp->data, &resp_buf[3], IT8500_DATA_LEN);
136 sr_spew("%s: Response packet received: cmd=%02x", __func__,
139 if (resp->command == CMD_RESPONSE) {
140 if (resp->data[0] != STS_COMMAND_SUCCESSFUL) {
141 sr_dbg("%s: Command (%02x) failed: status=%02x",
142 __func__, cmd->command, resp->data[0]);
146 if (resp->command != cmd->command) {
147 sr_dbg("%s: Invalid response received: %02x"
149 __func__, resp->command, cmd->command);
168 SR_PRIV int itech_it8500_cmd(const struct sr_dev_inst *sdi,
169 struct itech_it8500_cmd_packet *cmd,
170 struct itech_it8500_cmd_packet **response)
172 struct dev_context *devc;
173 struct sr_serial_dev_inst *serial;
180 if (!devc || !serial)
183 g_mutex_lock(&devc->mutex);
184 ret = itech_it8500_send_cmd(serial, cmd, response);
185 g_mutex_unlock(&devc->mutex);
190 SR_PRIV void itech_it8500_status_change(const struct sr_dev_inst *sdi,
191 uint8_t old_os, uint8_t new_os,
192 uint16_t old_ds, uint16_t new_ds,
193 enum itech_it8500_modes old_m, enum itech_it8500_modes new_m)
195 gboolean old_bit, new_bit;
198 /* Check it output status has changed. */
199 old_bit = old_os & OS_OUT_FLAG;
200 new_bit = new_os & OS_OUT_FLAG;
201 if (old_bit != new_bit)
202 sr_session_send_meta(sdi,
204 g_variant_new_boolean(new_bit));
206 /* Check if OVP status has changed. */
207 old_bit = old_ds & DS_OV_FLAG;
208 new_bit = new_ds & DS_OV_FLAG;
209 if (old_bit != new_bit)
210 sr_session_send_meta(sdi,
211 SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE,
212 g_variant_new_boolean(new_bit));
214 /* Check if OCP status has changed. */
215 old_bit = old_ds & DS_OC_FLAG;
216 new_bit = new_ds & DS_OC_FLAG;
217 if (old_bit != new_bit)
218 sr_session_send_meta(sdi,
219 SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE,
220 g_variant_new_boolean(new_bit));
222 /* Check if OTP status has changed. */
223 old_bit = old_ds & DS_OT_FLAG;
224 new_bit = new_ds & DS_OT_FLAG;
225 if (old_bit != new_bit)
226 sr_session_send_meta(sdi,
227 SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE,
228 g_variant_new_boolean(new_bit));
230 /* Check if operating mode has changed. */
231 if (old_m != new_m) {
232 mode = itech_it8500_mode_to_string(new_m);
233 sr_session_send_meta(sdi, SR_CONF_REGULATION,
234 g_variant_new_string(mode));
238 SR_PRIV int itech_it8500_get_status(const struct sr_dev_inst *sdi)
240 struct dev_context *devc;
241 struct itech_it8500_cmd_packet *cmd;
242 struct itech_it8500_cmd_packet *resp;
243 double voltage, current, power;
244 uint8_t operation_state;
245 uint16_t demand_state;
246 enum itech_it8500_modes mode;
258 cmd = g_malloc0(sizeof(*cmd));
260 return SR_ERR_MALLOC;
261 cmd->address = devc->address;
262 cmd->command = CMD_GET_STATE;
265 ret = itech_it8500_cmd(sdi, cmd, &resp);
268 voltage = read_u32le_inc(&p) / 1000.0;
269 current = read_u32le_inc(&p) / 10000.0;
270 power = read_u32le_inc(&p) / 1000.0;
271 operation_state = read_u8_inc(&p);
272 demand_state = read_u16le_inc(&p);
274 if (demand_state & DS_CC_MODE_FLAG)
276 else if (demand_state & DS_CV_MODE_FLAG)
278 else if (demand_state & DS_CW_MODE_FLAG)
280 else if (demand_state & DS_CR_MODE_FLAG)
284 load_on = operation_state & OS_OUT_FLAG;
286 sr_dbg("Load status: V=%.4f, I=%.4f, P=%.3f, State=%s, "
287 "Mode=%s (op=0x%02x, demand=0x%04x)",
288 voltage, current, power, (load_on ? "ON": "OFF"),
289 itech_it8500_mode_to_string(mode),
290 operation_state, demand_state);
292 /* Check for status change only after scan() has completed. */
294 itech_it8500_status_change(sdi, devc->operation_state,
295 operation_state, devc->demand_state,
296 demand_state, devc->mode, mode);
298 devc->voltage = voltage;
299 devc->current = current;
301 devc->operation_state = operation_state;
302 devc->demand_state = demand_state;
304 devc->load_on = load_on;
313 SR_PRIV int itech_it8500_get_int(const struct sr_dev_inst *sdi,
314 enum itech_it8500_command command, int *result)
316 struct dev_context *devc;
317 struct itech_it8500_cmd_packet *cmd;
318 struct itech_it8500_cmd_packet *resp;
325 cmd = g_malloc0(sizeof(*cmd));
327 return SR_ERR_MALLOC;
328 cmd->address = devc->address;
329 cmd->command = command;
332 ret = itech_it8500_cmd(sdi, cmd, &resp);
334 *result = RL32(&resp->data[0]);
342 SR_PRIV void itech_it8500_channel_send_value(const struct sr_dev_inst *sdi,
343 struct sr_channel *ch, double value, enum sr_mq mq,
344 enum sr_unit unit, int digits)
346 struct sr_datafeed_packet packet;
347 struct sr_datafeed_analog analog;
348 struct sr_analog_encoding encoding;
349 struct sr_analog_meaning meaning;
350 struct sr_analog_spec spec;
354 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
355 analog.meaning->channels = g_slist_append(NULL, ch);
356 analog.num_samples = 1;
358 analog.encoding->unitsize = sizeof(val);
359 analog.encoding->is_float = TRUE;
360 analog.meaning->mq = mq;
361 analog.meaning->unit = unit;
362 analog.meaning->mqflags = SR_MQFLAG_DC;
364 packet.type = SR_DF_ANALOG;
365 packet.payload = &analog;
366 sr_session_send(sdi, &packet);
367 g_slist_free(analog.meaning->channels);
370 SR_PRIV int itech_it8500_receive_data(int fd, int revents, void *cb_data)
372 struct sr_dev_inst *sdi;
373 struct dev_context *devc;
379 if (!(sdi = cb_data))
382 if (!(devc = sdi->priv))
385 if (itech_it8500_get_status(sdi) != SR_OK)
388 std_session_send_df_frame_begin(sdi);
390 l = g_slist_nth(sdi->channels, 0);
391 itech_it8500_channel_send_value(sdi, l->data, devc->voltage,
392 SR_MQ_VOLTAGE, SR_UNIT_VOLT, 5);
394 l = g_slist_nth(sdi->channels, 1);
395 itech_it8500_channel_send_value(sdi, l->data, devc->current,
396 SR_MQ_CURRENT, SR_UNIT_AMPERE, 5);
398 l = g_slist_nth(sdi->channels, 2);
399 itech_it8500_channel_send_value(sdi, l->data, devc->power,
400 SR_MQ_POWER, SR_UNIT_WATT, 5);
402 std_session_send_df_frame_end(sdi);
404 sr_sw_limits_update_samples_read(&devc->limits, 1);
405 if (sr_sw_limits_check(&devc->limits))
406 sr_dev_acquisition_stop(sdi);