comparison spandsp-0.0.3/spandsp-0.0.3/tests/async_tests.c @ 5:f762bf195c4b

import spandsp-0.0.3
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
4:26cd8f1ef0b1 5:f762bf195c4b
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * async_tests.c - Tests for asynchronous serial processing.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2004 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: async_tests.c,v 1.12 2006/11/19 14:07:26 steveu Exp $
26 */
27
28 /*! \file */
29
30 /*! \page async_tests_page Asynchronous bit stream tests
31 \section async_tests_page_sec_1 What does it do?
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <inttypes.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #if defined(HAVE_TGMATH_H)
43 #include <tgmath.h>
44 #endif
45 #if defined(HAVE_MATH_H)
46 #include <math.h>
47 #endif
48 #include <assert.h>
49 #include <audiofile.h>
50 #include <tiffio.h>
51
52 #include "spandsp.h"
53
54 async_rx_state_t rx_async;
55 async_tx_state_t tx_async;
56
57 int full_len;
58 uint8_t old_buf[1000];
59 uint8_t new_buf[1000];
60
61 volatile int tx_async_chars;
62 volatile int rx_async_chars;
63 volatile int rx_async_char_mask;
64
65 int v14_test_async_tx_get_bit(void *user_data);
66
67 int v14_test_async_tx_get_bit(void *user_data)
68 {
69 async_tx_state_t *s;
70 int bit;
71 static int destuff = 0;
72
73 /* Special routine to test V.14 rate adaption, by randomly skipping
74 stop bits. */
75 s = (async_tx_state_t *) user_data;
76 if (s->bitpos == 0)
77 {
78 /* Start bit */
79 bit = 0;
80 s->byte_in_progress = s->get_byte(s->user_data);
81 s->parity_bit = 0;
82 s->bitpos++;
83 }
84 else if (s->bitpos <= s->data_bits)
85 {
86 bit = s->byte_in_progress & 1;
87 s->byte_in_progress >>= 1;
88 s->parity_bit ^= bit;
89 s->bitpos++;
90 if (!s->parity && s->bitpos == s->data_bits + 1)
91 {
92 /* Drop the stop bit on every fourth character for V.14 simulation*/
93 if ((++destuff & 3) == 0)
94 s->bitpos = 0;
95 }
96 }
97 else if (s->parity && s->bitpos == s->data_bits + 1)
98 {
99 if (s->parity == ASYNC_PARITY_ODD)
100 s->parity_bit ^= 1;
101 bit = s->parity_bit;
102 s->bitpos++;
103 /* Drop the stop bit on every fourth character for V.14 simulation */
104 if ((++destuff & 3) == 0)
105 s->bitpos = 0;
106 }
107 else
108 {
109 /* Stop bit(s) */
110 bit = 1;
111 s->bitpos++;
112 if (s->bitpos > s->data_bits + s->stop_bits)
113 s->bitpos = 0;
114 }
115 return bit;
116 }
117 /*- End of function --------------------------------------------------------*/
118
119 static int test_get_async_byte(void *user_data)
120 {
121 int byte;
122
123 byte = tx_async_chars & 0xFF;
124 tx_async_chars++;
125 return byte;
126 }
127 /*- End of function --------------------------------------------------------*/
128
129 static void test_put_async_byte(void *user_data, int byte)
130 {
131 if ((rx_async_chars & rx_async_char_mask) != byte)
132 printf("Received byte is 0x%X (expected 0x%X)\n", byte, rx_async_chars);
133 rx_async_chars++;
134 }
135 /*- End of function --------------------------------------------------------*/
136
137 int main(int argc, char *argv[])
138 {
139 int bit;
140
141 printf("Test with async 8N1\n");
142 async_tx_init(&tx_async, 8, ASYNC_PARITY_NONE, 1, FALSE, test_get_async_byte, NULL);
143 async_rx_init(&rx_async, 8, ASYNC_PARITY_NONE, 1, FALSE, test_put_async_byte, NULL);
144 tx_async_chars = 0;
145 rx_async_chars = 0;
146 rx_async_char_mask = 0xFF;
147 while (rx_async_chars < 1000)
148 {
149 bit = async_tx_get_bit(&tx_async);
150 async_rx_put_bit(&rx_async, bit);
151 }
152 printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
153 if (tx_async_chars != rx_async_chars
154 ||
155 rx_async.parity_errors
156 ||
157 rx_async.framing_errors)
158 {
159 printf("Test failed.\n");
160 exit(2);
161 }
162
163 printf("Test with async 7E1\n");
164 async_tx_init(&tx_async, 7, ASYNC_PARITY_EVEN, 1, FALSE, test_get_async_byte, NULL);
165 async_rx_init(&rx_async, 7, ASYNC_PARITY_EVEN, 1, FALSE, test_put_async_byte, NULL);
166 tx_async_chars = 0;
167 rx_async_chars = 0;
168 rx_async_char_mask = 0x7F;
169 while (rx_async_chars < 1000)
170 {
171 bit = async_tx_get_bit(&tx_async);
172 async_rx_put_bit(&rx_async, bit);
173 }
174 printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
175 if (tx_async_chars != rx_async_chars
176 ||
177 rx_async.parity_errors
178 ||
179 rx_async.framing_errors)
180 {
181 printf("Test failed.\n");
182 exit(2);
183 }
184
185 printf("Test with async 8O1\n");
186 async_tx_init(&tx_async, 8, ASYNC_PARITY_ODD, 1, FALSE, test_get_async_byte, NULL);
187 async_rx_init(&rx_async, 8, ASYNC_PARITY_ODD, 1, FALSE, test_put_async_byte, NULL);
188 tx_async_chars = 0;
189 rx_async_chars = 0;
190 rx_async_char_mask = 0xFF;
191 while (rx_async_chars < 1000)
192 {
193 bit = async_tx_get_bit(&tx_async);
194 async_rx_put_bit(&rx_async, bit);
195 }
196 printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
197 if (tx_async_chars != rx_async_chars
198 ||
199 rx_async.parity_errors
200 ||
201 rx_async.framing_errors)
202 {
203 printf("Test failed.\n");
204 exit(2);
205 }
206
207 printf("Test with async 8O1 and V.14\n");
208 async_tx_init(&tx_async, 8, ASYNC_PARITY_ODD, 1, TRUE, test_get_async_byte, NULL);
209 async_rx_init(&rx_async, 8, ASYNC_PARITY_ODD, 1, TRUE, test_put_async_byte, NULL);
210 tx_async_chars = 0;
211 rx_async_chars = 0;
212 rx_async_char_mask = 0xFF;
213 while (rx_async_chars < 1000)
214 {
215 bit = v14_test_async_tx_get_bit(&tx_async);
216 async_rx_put_bit(&rx_async, bit);
217 }
218 printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
219 if (tx_async_chars != rx_async_chars + 1
220 ||
221 rx_async.parity_errors
222 ||
223 rx_async.framing_errors)
224 {
225 printf("Test failed.\n");
226 exit(2);
227 }
228
229 printf("Test with async 5N2\n");
230 async_tx_init(&tx_async, 5, ASYNC_PARITY_NONE, 2, FALSE, test_get_async_byte, NULL);
231 async_rx_init(&rx_async, 5, ASYNC_PARITY_NONE, 2, FALSE, test_put_async_byte, NULL);
232 tx_async_chars = 0;
233 rx_async_chars = 0;
234 rx_async_char_mask = 0x1F;
235 while (rx_async_chars < 1000)
236 {
237 bit = async_tx_get_bit(&tx_async);
238 async_rx_put_bit(&rx_async, bit);
239 }
240 printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
241 if (tx_async_chars != rx_async_chars
242 ||
243 rx_async.parity_errors
244 ||
245 rx_async.framing_errors)
246 {
247 printf("Test failed.\n");
248 exit(2);
249 }
250
251 printf("Tests passed.\n");
252 return 0;
253 }
254 /*- End of function --------------------------------------------------------*/
255 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.