2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
25 #define LOG_PREFIX "soft-trigger"
28 SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
29 const struct sr_dev_inst *sdi, struct sr_trigger *trigger)
31 struct soft_trigger_logic *stl;
33 stl = g_malloc0(sizeof(struct soft_trigger_logic));
35 stl->trigger = trigger;
36 stl->unitsize = (g_slist_length(sdi->channels) + 7) / 8;
37 stl->prev_sample = g_malloc0(stl->unitsize);
42 SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *stl)
44 g_free(stl->prev_sample);
48 static gboolean logic_check_match(struct soft_trigger_logic *stl,
49 uint8_t *sample, struct sr_trigger_match *match)
56 bit = *(sample + match->channel->index / 8)
57 & (1 << (match->channel->index % 8));
58 if (match->match == SR_TRIGGER_ZERO)
60 else if (match->match == SR_TRIGGER_ONE)
65 /* First sample, don't have enough for an edge match yet. */
67 prev_bit = *(stl->prev_sample + match->channel->index / 8)
68 & (1 << (match->channel->index % 8));
69 if (match->match == SR_TRIGGER_RISING)
70 result = prev_bit == 0 && bit != 0;
71 else if (match->match == SR_TRIGGER_FALLING)
72 result = prev_bit != 0 && bit == 0;
73 else if (match->match == SR_TRIGGER_EDGE)
74 result = prev_bit != bit;
80 /* Returns the offset (in samples) within buf of where the trigger
81 * occurred, or -1 if not triggered. */
82 SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *stl,
83 uint8_t *buf, int len)
85 struct sr_datafeed_packet packet;
86 struct sr_trigger_stage *stage;
87 struct sr_trigger_match *match;
94 for (i = 0; i < len; i += stl->unitsize) {
95 l_stage = g_slist_nth(stl->trigger->stages, stl->cur_stage);
96 stage = l_stage->data;
98 /* No matches supplied, client error. */
102 for (l = stage->matches; l; l = l->next) {
104 if (!match->channel->enabled)
105 /* Ignore disabled channels with a trigger. */
107 if (!logic_check_match(stl, buf + i, match)) {
112 memcpy(stl->prev_sample, buf + i, stl->unitsize);
114 /* Matched on the current stage. */
116 /* Advance to next stage. */
119 /* Matched on last stage, fire trigger. */
120 offset = i / stl->unitsize;
122 packet.type = SR_DF_TRIGGER;
123 packet.payload = NULL;
124 sr_session_send(stl->sdi, &packet);
127 } else if (stl->cur_stage > 0) {
129 * We had a match at an earlier stage, but failed on the
130 * current stage. However, we may have a match on this
131 * stage in the next bit -- trigger on 0001 will fail on
132 * seeing 00001, so we need to go back to stage 0 -- but
133 * at the next sample from the one that matched originally,
134 * which the counter increment at the end of the loop
137 i -= stl->cur_stage * stl->unitsize;
139 i = -1; /* Oops, went back past this buffer. */
140 /* Reset trigger stage. */