]> sigrok.org Git - libsigrok.git/blame - src/soft-trigger.c
session_driver: Quickfix to prevent looped analog data reads.
[libsigrok.git] / src / soft-trigger.c
CommitLineData
ac136b57
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.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
6ec6c43b 20#include <config.h>
ac136b57 21#include <string.h>
c1aae900 22#include <libsigrok/libsigrok.h>
ac136b57
BV
23#include "libsigrok-internal.h"
24
25/* @cond PRIVATE */
26#define LOG_PREFIX "soft-trigger"
27/* @endcond */
28
29SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
fe5a7355
AJ
30 const struct sr_dev_inst *sdi, struct sr_trigger *trigger,
31 int pre_trigger_samples)
ac136b57
BV
32{
33 struct soft_trigger_logic *stl;
34
35 stl = g_malloc0(sizeof(struct soft_trigger_logic));
36 stl->sdi = sdi;
37 stl->trigger = trigger;
38 stl->unitsize = (g_slist_length(sdi->channels) + 7) / 8;
39 stl->prev_sample = g_malloc0(stl->unitsize);
fe5a7355
AJ
40 stl->pre_trigger_size = stl->unitsize * pre_trigger_samples;
41 stl->pre_trigger_buffer = g_malloc(stl->pre_trigger_size);
42 stl->pre_trigger_head = stl->pre_trigger_buffer;
43
98fec29e 44 if (stl->pre_trigger_size > 0 && !stl->pre_trigger_buffer) {
fe5a7355
AJ
45 soft_trigger_logic_free(stl);
46 return NULL;
47 }
ac136b57
BV
48
49 return stl;
50}
51
52SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *stl)
53{
fe5a7355 54 g_free(stl->pre_trigger_buffer);
ac136b57
BV
55 g_free(stl->prev_sample);
56 g_free(stl);
57}
58
fe5a7355
AJ
59static void pre_trigger_append(struct soft_trigger_logic *stl,
60 uint8_t *buf, int len)
61{
62 /* Avoid uselessly copying more than the pre-trigger size. */
63 if (len > stl->pre_trigger_size) {
64 buf += len - stl->pre_trigger_size;
65 len = stl->pre_trigger_size;
66 }
67
68 /* Update the filling level of the pre-trigger circular buffer. */
69 stl->pre_trigger_fill = MIN(stl->pre_trigger_fill + len,
70 stl->pre_trigger_size);
71
72 /* Actually copy data to the pre-trigger circular buffer. */
73 while (len > 0) {
74 size_t size = MIN(stl->pre_trigger_buffer + stl->pre_trigger_size
75 - stl->pre_trigger_head, len);
76 memcpy(stl->pre_trigger_head, buf, size);
77 stl->pre_trigger_head += size;
78 if (stl->pre_trigger_head >= stl->pre_trigger_buffer
79 + stl->pre_trigger_size)
80 stl->pre_trigger_head = stl->pre_trigger_buffer;
81 buf += size;
82 len -= size;
83 }
84}
85
86static void pre_trigger_send(struct soft_trigger_logic *stl,
87 int *pre_trigger_samples)
88{
89 struct sr_datafeed_packet packet;
90 struct sr_datafeed_logic logic;
91
92 packet.type = SR_DF_LOGIC;
93 packet.payload = &logic;
94 logic.unitsize = stl->unitsize;
95
96 if (pre_trigger_samples)
97 *pre_trigger_samples = 0;
98
99 /* If pre-trigger buffer not full, rewind head to the first valid sample. */
100 if (stl->pre_trigger_fill < stl->pre_trigger_size)
101 stl->pre_trigger_head = stl->pre_trigger_buffer;
102
103 /* Send logic packets for the pre-trigger circular buffer content. */
104 while (stl->pre_trigger_fill > 0) {
105 size_t size = MIN(stl->pre_trigger_buffer + stl->pre_trigger_size
106 - stl->pre_trigger_head, stl->pre_trigger_fill);
107 logic.length = size;
108 logic.data = stl->pre_trigger_head;
109 sr_session_send(stl->sdi, &packet);
110 stl->pre_trigger_head = stl->pre_trigger_buffer;
111 stl->pre_trigger_fill -= size;
112 if (pre_trigger_samples)
113 *pre_trigger_samples += size / stl->unitsize;
114 }
115}
116
ac136b57
BV
117static gboolean logic_check_match(struct soft_trigger_logic *stl,
118 uint8_t *sample, struct sr_trigger_match *match)
119{
120 int bit, prev_bit;
121 gboolean result;
122
123 stl->count++;
124 result = FALSE;
125 bit = *(sample + match->channel->index / 8)
126 & (1 << (match->channel->index % 8));
127 if (match->match == SR_TRIGGER_ZERO)
128 result = bit == 0;
129 else if (match->match == SR_TRIGGER_ONE)
130 result = bit != 0;
131 else {
132 /* Edge matches. */
133 if (stl->count == 1)
134 /* First sample, don't have enough for an edge match yet. */
135 return FALSE;
136 prev_bit = *(stl->prev_sample + match->channel->index / 8)
137 & (1 << (match->channel->index % 8));
138 if (match->match == SR_TRIGGER_RISING)
139 result = prev_bit == 0 && bit != 0;
140 else if (match->match == SR_TRIGGER_FALLING)
141 result = prev_bit != 0 && bit == 0;
142 else if (match->match == SR_TRIGGER_EDGE)
143 result = prev_bit != bit;
144 }
145
146 return result;
147}
148
149/* Returns the offset (in samples) within buf of where the trigger
150 * occurred, or -1 if not triggered. */
151SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *stl,
fe5a7355 152 uint8_t *buf, int len, int *pre_trigger_samples)
ac136b57
BV
153{
154 struct sr_datafeed_packet packet;
155 struct sr_trigger_stage *stage;
156 struct sr_trigger_match *match;
157 GSList *l, *l_stage;
158 int offset;
159 int i;
160 gboolean match_found;
161
162 offset = -1;
163 for (i = 0; i < len; i += stl->unitsize) {
164 l_stage = g_slist_nth(stl->trigger->stages, stl->cur_stage);
165 stage = l_stage->data;
166 if (!stage->matches)
167 /* No matches supplied, client error. */
168 return SR_ERR_ARG;
169
170 match_found = TRUE;
171 for (l = stage->matches; l; l = l->next) {
172 match = l->data;
173 if (!match->channel->enabled)
174 /* Ignore disabled channels with a trigger. */
175 continue;
176 if (!logic_check_match(stl, buf + i, match)) {
177 match_found = FALSE;
178 break;
179 }
180 }
181 memcpy(stl->prev_sample, buf + i, stl->unitsize);
182 if (match_found) {
183 /* Matched on the current stage. */
184 if (l_stage->next) {
185 /* Advance to next stage. */
186 stl->cur_stage++;
187 } else {
fe5a7355
AJ
188 /* Matched on last stage, send pre-trigger data. */
189 pre_trigger_append(stl, buf, i);
190 pre_trigger_send(stl, pre_trigger_samples);
191
192 /* Fire trigger. */
ac136b57
BV
193 offset = i / stl->unitsize;
194
195 packet.type = SR_DF_TRIGGER;
196 packet.payload = NULL;
197 sr_session_send(stl->sdi, &packet);
198 break;
199 }
200 } else if (stl->cur_stage > 0) {
201 /*
202 * We had a match at an earlier stage, but failed on the
203 * current stage. However, we may have a match on this
204 * stage in the next bit -- trigger on 0001 will fail on
205 * seeing 00001, so we need to go back to stage 0 -- but
206 * at the next sample from the one that matched originally,
207 * which the counter increment at the end of the loop
208 * takes care of.
209 */
210 i -= stl->cur_stage * stl->unitsize;
211 if (i < -1)
212 i = -1; /* Oops, went back past this buffer. */
213 /* Reset trigger stage. */
214 stl->cur_stage = 0;
215 }
216 }
217
fe5a7355
AJ
218 if (offset == -1)
219 pre_trigger_append(stl, buf, len);
220
ac136b57
BV
221 return offset;
222}