2
0

async_tests.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /*! \file */
  26. /*! \page async_tests_page Asynchronous bit stream tests
  27. \section async_tests_page_sec_1 What does it do?
  28. */
  29. #if defined(HAVE_CONFIG_H)
  30. #include "config.h"
  31. #endif
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <sndfile.h>
  36. #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  37. #include "spandsp.h"
  38. async_rx_state_t rx_async;
  39. async_tx_state_t tx_async;
  40. int full_len;
  41. uint8_t old_buf[1000];
  42. uint8_t new_buf[1000];
  43. volatile int tx_async_chars;
  44. volatile int rx_async_chars;
  45. volatile int rx_async_char_mask;
  46. int v14_test_async_tx_get_bit(void *user_data);
  47. int v14_test_async_tx_get_bit(void *user_data)
  48. {
  49. async_tx_state_t *s;
  50. int bit;
  51. int parity_bit;
  52. static int destuff = 0;
  53. /* Special routine to test V.14 rate adaption, by randomly skipping
  54. stop bits. */
  55. s = (async_tx_state_t *) user_data;
  56. if (s->bitpos == 0)
  57. {
  58. s->byte_in_progress = s->get_byte(s->user_data);
  59. s->byte_in_progress &= (0xFFFF >> (16 - s->data_bits));
  60. if (s->parity)
  61. {
  62. parity_bit = parity8(s->byte_in_progress);
  63. if (s->parity == ASYNC_PARITY_ODD)
  64. parity_bit ^= 1;
  65. s->byte_in_progress |= (parity_bit << s->data_bits);
  66. s->byte_in_progress |= (0xFFFF << (s->data_bits + 1));
  67. }
  68. else
  69. {
  70. s->byte_in_progress |= (0xFFFF << s->data_bits);
  71. }
  72. /* Start bit */
  73. bit = 0;
  74. s->bitpos++;
  75. }
  76. else
  77. {
  78. bit = s->byte_in_progress & 1;
  79. s->byte_in_progress >>= 1;
  80. /* Drop the stop bit on every fourth character for V.14 simulation */
  81. if ((++destuff & 3) == 0)
  82. {
  83. if (++s->bitpos > s->total_bits - 1)
  84. s->bitpos = 0;
  85. }
  86. else
  87. {
  88. if (++s->bitpos > s->total_bits)
  89. s->bitpos = 0;
  90. }
  91. }
  92. return bit;
  93. }
  94. /*- End of function --------------------------------------------------------*/
  95. static int test_get_async_byte(void *user_data)
  96. {
  97. int byte;
  98. byte = tx_async_chars & 0xFF;
  99. tx_async_chars++;
  100. return byte;
  101. }
  102. /*- End of function --------------------------------------------------------*/
  103. static void test_put_async_byte(void *user_data, int byte)
  104. {
  105. if ((rx_async_chars & rx_async_char_mask) != byte)
  106. printf("Received byte is 0x%X (expected 0x%X)\n", byte, rx_async_chars);
  107. rx_async_chars++;
  108. }
  109. /*- End of function --------------------------------------------------------*/
  110. int main(int argc, char *argv[])
  111. {
  112. int bit;
  113. printf("Test with async 8N1\n");
  114. async_tx_init(&tx_async, 8, ASYNC_PARITY_NONE, 1, false, test_get_async_byte, NULL);
  115. async_rx_init(&rx_async, 8, ASYNC_PARITY_NONE, 1, false, test_put_async_byte, NULL);
  116. tx_async_chars = 0;
  117. rx_async_chars = 0;
  118. rx_async_char_mask = 0xFF;
  119. while (rx_async_chars < 1000)
  120. {
  121. bit = async_tx_get_bit(&tx_async);
  122. async_rx_put_bit(&rx_async, bit);
  123. }
  124. printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
  125. if (tx_async_chars != rx_async_chars
  126. ||
  127. rx_async.parity_errors
  128. ||
  129. rx_async.framing_errors)
  130. {
  131. printf("Test failed.\n");
  132. exit(2);
  133. }
  134. printf("Test with async 7E1\n");
  135. async_tx_init(&tx_async, 7, ASYNC_PARITY_EVEN, 1, false, test_get_async_byte, NULL);
  136. async_rx_init(&rx_async, 7, ASYNC_PARITY_EVEN, 1, false, test_put_async_byte, NULL);
  137. tx_async_chars = 0;
  138. rx_async_chars = 0;
  139. rx_async_char_mask = 0x7F;
  140. while (rx_async_chars < 1000)
  141. {
  142. bit = async_tx_get_bit(&tx_async);
  143. async_rx_put_bit(&rx_async, bit);
  144. }
  145. printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
  146. if (tx_async_chars != rx_async_chars
  147. ||
  148. rx_async.parity_errors
  149. ||
  150. rx_async.framing_errors)
  151. {
  152. printf("Test failed.\n");
  153. exit(2);
  154. }
  155. printf("Test with async 8O1\n");
  156. async_tx_init(&tx_async, 8, ASYNC_PARITY_ODD, 1, false, test_get_async_byte, NULL);
  157. async_rx_init(&rx_async, 8, ASYNC_PARITY_ODD, 1, false, test_put_async_byte, NULL);
  158. tx_async_chars = 0;
  159. rx_async_chars = 0;
  160. rx_async_char_mask = 0xFF;
  161. while (rx_async_chars < 1000)
  162. {
  163. bit = async_tx_get_bit(&tx_async);
  164. async_rx_put_bit(&rx_async, bit);
  165. }
  166. printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
  167. if (tx_async_chars != rx_async_chars
  168. ||
  169. rx_async.parity_errors
  170. ||
  171. rx_async.framing_errors)
  172. {
  173. printf("Test failed.\n");
  174. exit(2);
  175. }
  176. printf("Test with async 8O1 and V.14\n");
  177. async_tx_init(&tx_async, 8, ASYNC_PARITY_ODD, 1, true, test_get_async_byte, NULL);
  178. async_rx_init(&rx_async, 8, ASYNC_PARITY_ODD, 1, true, test_put_async_byte, NULL);
  179. tx_async_chars = 0;
  180. rx_async_chars = 0;
  181. rx_async_char_mask = 0xFF;
  182. while (rx_async_chars < 1000)
  183. {
  184. bit = v14_test_async_tx_get_bit(&tx_async);
  185. async_rx_put_bit(&rx_async, bit);
  186. }
  187. printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
  188. if (tx_async_chars != rx_async_chars
  189. ||
  190. rx_async.parity_errors
  191. ||
  192. rx_async.framing_errors)
  193. {
  194. printf("Test failed.\n");
  195. exit(2);
  196. }
  197. printf("Test with async 5N2\n");
  198. async_tx_init(&tx_async, 5, ASYNC_PARITY_NONE, 2, false, test_get_async_byte, NULL);
  199. async_rx_init(&rx_async, 5, ASYNC_PARITY_NONE, 2, false, test_put_async_byte, NULL);
  200. tx_async_chars = 0;
  201. rx_async_chars = 0;
  202. rx_async_char_mask = 0x1F;
  203. while (rx_async_chars < 1000)
  204. {
  205. bit = async_tx_get_bit(&tx_async);
  206. async_rx_put_bit(&rx_async, bit);
  207. }
  208. printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
  209. if (tx_async_chars != rx_async_chars
  210. ||
  211. rx_async.parity_errors
  212. ||
  213. rx_async.framing_errors)
  214. {
  215. printf("Test failed.\n");
  216. exit(2);
  217. }
  218. printf("Tests passed.\n");
  219. return 0;
  220. }
  221. /*- End of function --------------------------------------------------------*/
  222. /*- End of file ------------------------------------------------------------*/