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