-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoft-trigger.c
More file actions
147 lines (132 loc) · 4.09 KB
/
soft-trigger.c
File metadata and controls
147 lines (132 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* This file is part of the libsigrok project.
*
* Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "libsigrok.h"
#include "libsigrok-internal.h"
/* @cond PRIVATE */
#define LOG_PREFIX "soft-trigger"
/* @endcond */
SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new(
const struct sr_dev_inst *sdi, struct sr_trigger *trigger)
{
struct soft_trigger_logic *stl;
stl = g_malloc0(sizeof(struct soft_trigger_logic));
stl->sdi = sdi;
stl->trigger = trigger;
stl->unitsize = (g_slist_length(sdi->channels) + 7) / 8;
stl->prev_sample = g_malloc0(stl->unitsize);
return stl;
}
SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *stl)
{
g_free(stl->prev_sample);
g_free(stl);
}
static gboolean logic_check_match(struct soft_trigger_logic *stl,
uint8_t *sample, struct sr_trigger_match *match)
{
int bit, prev_bit;
gboolean result;
stl->count++;
result = FALSE;
bit = *(sample + match->channel->index / 8)
& (1 << (match->channel->index % 8));
if (match->match == SR_TRIGGER_ZERO)
result = bit == 0;
else if (match->match == SR_TRIGGER_ONE)
result = bit != 0;
else {
/* Edge matches. */
if (stl->count == 1)
/* First sample, don't have enough for an edge match yet. */
return FALSE;
prev_bit = *(stl->prev_sample + match->channel->index / 8)
& (1 << (match->channel->index % 8));
if (match->match == SR_TRIGGER_RISING)
result = prev_bit == 0 && bit != 0;
else if (match->match == SR_TRIGGER_FALLING)
result = prev_bit != 0 && bit == 0;
else if (match->match == SR_TRIGGER_EDGE)
result = prev_bit != bit;
}
return result;
}
/* Returns the offset (in samples) within buf of where the trigger
* occurred, or -1 if not triggered. */
SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *stl,
uint8_t *buf, int len)
{
struct sr_datafeed_packet packet;
struct sr_trigger_stage *stage;
struct sr_trigger_match *match;
GSList *l, *l_stage;
int offset;
int i;
gboolean match_found;
offset = -1;
for (i = 0; i < len; i += stl->unitsize) {
l_stage = g_slist_nth(stl->trigger->stages, stl->cur_stage);
stage = l_stage->data;
if (!stage->matches)
/* No matches supplied, client error. */
return SR_ERR_ARG;
match_found = TRUE;
for (l = stage->matches; l; l = l->next) {
match = l->data;
if (!match->channel->enabled)
/* Ignore disabled channels with a trigger. */
continue;
if (!logic_check_match(stl, buf + i, match)) {
match_found = FALSE;
break;
}
}
memcpy(stl->prev_sample, buf + i, stl->unitsize);
if (match_found) {
/* Matched on the current stage. */
if (l_stage->next) {
/* Advance to next stage. */
stl->cur_stage++;
} else {
/* Matched on last stage, fire trigger. */
offset = i / stl->unitsize;
packet.type = SR_DF_TRIGGER;
packet.payload = NULL;
sr_session_send(stl->sdi, &packet);
break;
}
} else if (stl->cur_stage > 0) {
/*
* We had a match at an earlier stage, but failed on the
* current stage. However, we may have a match on this
* stage in the next bit -- trigger on 0001 will fail on
* seeing 00001, so we need to go back to stage 0 -- but
* at the next sample from the one that matched originally,
* which the counter increment at the end of the loop
* takes care of.
*/
i -= stl->cur_stage * stl->unitsize;
if (i < -1)
i = -1; /* Oops, went back past this buffer. */
/* Reset trigger stage. */
stl->cur_stage = 0;
}
}
return offset;
}