comparison spandsp-0.0.6pre17/tests/async_tests.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
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.19 2009/05/30 15:23:13 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 #if defined(HAVE_CONFIG_H)
35 #include "config.h"
36 #endif
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <sndfile.h>
42
43 //#if defined(WITH_SPANDSP_INTERNALS)
44 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
45 //#endif
46
47 #include "spandsp.h"
48
49 async_rx_state_t rx_async;
50 async_tx_state_t tx_async;
51
52 int full_len;
53 uint8_t old_buf[1000];
54 uint8_t new_buf[1000];
55
56 volatile int tx_async_chars;
57 volatile int rx_async_chars;
58 volatile int rx_async_char_mask;
59
60 int v14_test_async_tx_get_bit(void *user_data);
61
62 int v14_test_async_tx_get_bit(void *user_data)
63 {
64 async_tx_state_t *s;
65 int bit;
66 static int destuff = 0;
67
68 /* Special routine to test V.14 rate adaption, by randomly skipping
69 stop bits. */
70 s = (async_tx_state_t *) user_data;
71 if (s->bitpos == 0)
72 {
73 /* Start bit */
74 bit = 0;
75 s->byte_in_progress = s->get_byte(s->user_data);
76 s->parity_bit = 0;
77 s->bitpos++;
78 }
79 else if (s->bitpos <= s->data_bits)
80 {
81 bit = s->byte_in_progress & 1;
82 s->byte_in_progress >>= 1;
83 s->parity_bit ^= bit;
84 s->bitpos++;
85 if (!s->parity && s->bitpos == s->data_bits + 1)
86 {
87 /* Drop the stop bit on every fourth character for V.14 simulation*/
88 if ((++destuff & 3) == 0)
89 s->bitpos = 0;
90 }
91 }
92 else if (s->parity && s->bitpos == s->data_bits + 1)
93 {
94 if (s->parity == ASYNC_PARITY_ODD)
95 s->parity_bit ^= 1;
96 bit = s->parity_bit;
97 s->bitpos++;
98 /* Drop the stop bit on every fourth character for V.14 simulation */
99 if ((++destuff & 3) == 0)
100 s->bitpos = 0;
101 }
102 else
103 {
104 /* Stop bit(s) */
105 bit = 1;
106 s->bitpos++;
107 if (s->bitpos > s->data_bits + s->stop_bits)
108 s->bitpos = 0;
109 }
110 return bit;
111 }
112 /*- End of function --------------------------------------------------------*/
113
114 static int test_get_async_byte(void *user_data)
115 {
116 int byte;
117
118 byte = tx_async_chars & 0xFF;
119 tx_async_chars++;
120 return byte;
121 }
122 /*- End of function --------------------------------------------------------*/
123
124 static void test_put_async_byte(void *user_data, int byte)
125 {
126 if ((rx_async_chars & rx_async_char_mask) != byte)
127 printf("Received byte is 0x%X (expected 0x%X)\n", byte, rx_async_chars);
128 rx_async_chars++;
129 }
130 /*- End of function --------------------------------------------------------*/
131
132 int main(int argc, char *argv[])
133 {
134 int bit;
135
136 printf("Test with async 8N1\n");
137 async_tx_init(&tx_async, 8, ASYNC_PARITY_NONE, 1, FALSE, test_get_async_byte, NULL);
138 async_rx_init(&rx_async, 8, ASYNC_PARITY_NONE, 1, FALSE, test_put_async_byte, NULL);
139 tx_async_chars = 0;
140 rx_async_chars = 0;
141 rx_async_char_mask = 0xFF;
142 while (rx_async_chars < 1000)
143 {
144 bit = async_tx_get_bit(&tx_async);
145 async_rx_put_bit(&rx_async, bit);
146 }
147 printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
148 if (tx_async_chars != rx_async_chars
149 ||
150 rx_async.parity_errors
151 ||
152 rx_async.framing_errors)
153 {
154 printf("Test failed.\n");
155 exit(2);
156 }
157
158 printf("Test with async 7E1\n");
159 async_tx_init(&tx_async, 7, ASYNC_PARITY_EVEN, 1, FALSE, test_get_async_byte, NULL);
160 async_rx_init(&rx_async, 7, ASYNC_PARITY_EVEN, 1, FALSE, test_put_async_byte, NULL);
161 tx_async_chars = 0;
162 rx_async_chars = 0;
163 rx_async_char_mask = 0x7F;
164 while (rx_async_chars < 1000)
165 {
166 bit = async_tx_get_bit(&tx_async);
167 async_rx_put_bit(&rx_async, bit);
168 }
169 printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
170 if (tx_async_chars != rx_async_chars
171 ||
172 rx_async.parity_errors
173 ||
174 rx_async.framing_errors)
175 {
176 printf("Test failed.\n");
177 exit(2);
178 }
179
180 printf("Test with async 8O1\n");
181 async_tx_init(&tx_async, 8, ASYNC_PARITY_ODD, 1, FALSE, test_get_async_byte, NULL);
182 async_rx_init(&rx_async, 8, ASYNC_PARITY_ODD, 1, FALSE, test_put_async_byte, NULL);
183 tx_async_chars = 0;
184 rx_async_chars = 0;
185 rx_async_char_mask = 0xFF;
186 while (rx_async_chars < 1000)
187 {
188 bit = async_tx_get_bit(&tx_async);
189 async_rx_put_bit(&rx_async, bit);
190 }
191 printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
192 if (tx_async_chars != rx_async_chars
193 ||
194 rx_async.parity_errors
195 ||
196 rx_async.framing_errors)
197 {
198 printf("Test failed.\n");
199 exit(2);
200 }
201
202 printf("Test with async 8O1 and V.14\n");
203 async_tx_init(&tx_async, 8, ASYNC_PARITY_ODD, 1, TRUE, test_get_async_byte, NULL);
204 async_rx_init(&rx_async, 8, ASYNC_PARITY_ODD, 1, TRUE, test_put_async_byte, NULL);
205 tx_async_chars = 0;
206 rx_async_chars = 0;
207 rx_async_char_mask = 0xFF;
208 while (rx_async_chars < 1000)
209 {
210 bit = v14_test_async_tx_get_bit(&tx_async);
211 async_rx_put_bit(&rx_async, bit);
212 }
213 printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
214 if (tx_async_chars != rx_async_chars + 1
215 ||
216 rx_async.parity_errors
217 ||
218 rx_async.framing_errors)
219 {
220 printf("Test failed.\n");
221 exit(2);
222 }
223
224 printf("Test with async 5N2\n");
225 async_tx_init(&tx_async, 5, ASYNC_PARITY_NONE, 2, FALSE, test_get_async_byte, NULL);
226 async_rx_init(&rx_async, 5, ASYNC_PARITY_NONE, 2, FALSE, test_put_async_byte, NULL);
227 tx_async_chars = 0;
228 rx_async_chars = 0;
229 rx_async_char_mask = 0x1F;
230 while (rx_async_chars < 1000)
231 {
232 bit = async_tx_get_bit(&tx_async);
233 async_rx_put_bit(&rx_async, bit);
234 }
235 printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
236 if (tx_async_chars != rx_async_chars
237 ||
238 rx_async.parity_errors
239 ||
240 rx_async.framing_errors)
241 {
242 printf("Test failed.\n");
243 exit(2);
244 }
245
246 printf("Tests passed.\n");
247 return 0;
248 }
249 /*- End of function --------------------------------------------------------*/
250 /*- End of file ------------------------------------------------------------*/

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