source: multiio/commands/main_multiio_irq.c @ 0ecf946

Last change on this file since 0ecf946 was e38d987, checked in by Joel Sherrill <joel.sherrill@…>, on 03/18/11 at 13:19:33

2011-03-18 Joel Sherrill <joel.sherrill@…>

  • ChangeLog?, Makefile, commands/Makefile, commands/main_multiio_adc.c, commands/main_multiio_benchmark.c, commands/main_multiio_dac.c, commands/main_multiio_din.c, commands/main_multiio_dout.c, commands/main_multiio_irq.c, commands/multiio_commands.h, include/multiio.h, stub/multio_stub.c: New files.
  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 *  multiio_irq command
3 *
4 *  COPYRIGHT (c) 1989-2009.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <rtems.h>
17#include <rtems/shell.h>
18#include <rtems/stringto.h>
19#include "multiio.h"
20
21#define __need_getopt_newlib
22#include <getopt.h>
23
24char multiio_irq_usage[] =
25  "Usage: %s [-i iterations] [-p period] [-v] [-d|-D DAC|-a ADC]\n"
26  "Where: maximum iterations defaults to 1\n"
27  "       the period is in milliseconds and defaults to 1000\n";
28
29#define PRINT_USAGE() \
30   printf( multiio_irq_usage, argv[0] )
31
32static int to_usecs(
33  struct timespec *start,
34  struct timespec *end
35)
36{
37  int usecs;
38  struct timespec took;
39
40  _Timespec_Subtract( start, end, &took );
41  usecs = _Timespec_Get_seconds( &took ) * 1000000;
42  usecs += _Timespec_Get_nanoseconds( &took ) / 1000;
43  return usecs;
44}
45
46int main_multiio_irq(int argc, char **argv)
47{
48  int                 milliseconds;
49  int                 maximum;
50  int                 iterations;
51  int                 sc;
52  char                ch;
53  bool                verbose;
54  struct getopt_data  getopt_reent;
55  const  char        *s;
56  bool                do_dac = false;
57  bool                do_adc = false;
58  bool                do_din = false;
59  int                 dac = -1;
60  int                 adc = -1;
61  int                 selected;
62  const char         *irq = "";
63  int                 elapsed;
64  int                 interrupts;
65  struct timespec     previousTimestamp;
66  struct timespec     timestamp;
67
68  /*
69   * Parse arguments here
70   */
71  milliseconds = 1000;
72  maximum = 1;
73  verbose = false;
74  _Timespec_Set_to_zero( &previousTimestamp );
75
76  memset(&getopt_reent, 0, sizeof(getopt_data));
77  while ((ch = getopt_r(argc, argv, "i:p:vdD:a:", &getopt_reent)) != -1) {
78    switch (ch) {
79      case 'i': /* maximum iterations */
80        s = getopt_reent.optarg;
81        if ( rtems_string_to_int( s, &maximum, NULL, 0 ) ) {
82          printf( "Maximum iterations (%s) is not a number\n", s );
83          PRINT_USAGE();
84          return -1;
85        }
86
87        break;
88      case 'p': /* sampling period */
89        s = getopt_reent.optarg;
90        if ( rtems_string_to_int( s, &milliseconds, NULL, 0 ) ) {
91          printf( "Sampling period (%s) is not a number\n", s );
92          PRINT_USAGE();
93          return -1;
94        }
95        if ( milliseconds == 0 ) {
96          printf( "Sampling period (%d) is 0\n", milliseconds );
97          PRINT_USAGE();
98          return -1;
99        }
100        break;
101      case 'd': /* DIN enable */
102        do_din = true;
103        break;
104      case 'D': /* DAC enable */
105        s = getopt_reent.optarg;
106        if ( rtems_string_to_int( s, &dac, NULL, 0 ) ) {
107          printf( "DAC (%s) is not a number\n", s );
108          PRINT_USAGE();
109          return -1;
110        }
111
112        if ( dac < 0 || dac > 7 ) {
113          puts( "DAC number must be 0-7" );
114          PRINT_USAGE();
115          return -1;
116        }
117
118        do_dac = true;
119        break;
120      case 'a': /* ADC enable */
121        s = getopt_reent.optarg;
122        if ( rtems_string_to_int( s, &adc, NULL, 0 ) ) {
123          printf( "ADC (%s) is not a number\n", s );
124          PRINT_USAGE();
125          return -1;
126        }
127
128        if ( adc < 0 || adc > 7 ) {
129          puts( "ADC number must be 0-7" );
130          PRINT_USAGE();
131          return -1;
132        }
133        do_adc = true;
134        break;
135      case 'v': /* verbose */
136        verbose = true;
137        break;
138      default:
139        printf( multiio_irq_usage, argv[0] );
140        return -1;
141    }
142  }
143
144  /*
145   *  Did they select one item and ONLY one item?
146   */
147  selected = 0;
148  if ( do_din == true ) selected++;
149  if ( do_dac == true ) selected++;
150  if ( do_adc == true ) selected++;
151
152  if ( selected == 0 ) {
153    puts( "No IRQ Sources selected" );
154    return -1;
155  }
156
157  if ( selected > 1 ) {
158    puts( "More than 1 IRQ Sources selected" );
159    return -1;
160  }
161
162  if ( do_din == true ) {
163    irq = "DIN";
164  } else if ( do_dac == true ) {
165    irq = "DAC";
166  } else if ( do_adc == true ) {
167    irq = "ADC";
168  }
169  if ( maximum != 1 )
170    printf(
171      "Polling for %s IRQ for %d iterations with %d msec period\n",
172      irq,
173      maximum,
174      milliseconds
175    );
176
177  /*
178   *  Now sample in the loop
179   */
180  elapsed    = 0;
181  iterations = 1;
182  interrupts = 0;
183
184  rtems_din_flush_buffered_interrupts();
185  while (1) {
186    sc = 0;
187   
188    if ( do_din == true ) {
189      sc = rtems_din_wait_interrupt_with_timestamp(milliseconds, &timestamp);
190    } else if ( do_dac == true ) {
191      ; /* sc = wait_dac_int_with_timeout(dac, milliseconds); */
192    } else if ( do_adc == true ) {
193      ; /* sc = wait_dac_int_with_timeout(adc, milliseconds); */
194    }
195
196    if ( sc != -1 ) {
197      interrupts++;
198      if ( do_din == true ) {
199        printf(
200          "%d %s irq pin %d @ %ld:%ld (%d usecs since last)\n",
201          elapsed,
202          irq,
203          sc - 1,
204          timestamp.tv_sec, timestamp.tv_nsec,
205          to_usecs( &previousTimestamp, &timestamp )
206        );
207        previousTimestamp = timestamp;
208      } else {
209        printf( "%d %s irq\n", elapsed, irq );
210      }
211    }
212
213    elapsed += milliseconds;
214    if (iterations++ >= maximum )
215      break;
216  }
217  printf(
218    "%d total interrupts from %s in %d milliseconds\n",
219    interrupts,
220    irq,
221    elapsed
222  );
223  return 0;
224}
225
226rtems_shell_cmd_t Shell_MULTIIO_IRQ_Command = {
227  "multiio_irq",                                /* name */
228  "Wait for DIN Interrupts",                    /* usage */
229  "multiio",                                    /* topic */
230  main_multiio_irq,                             /* command */
231  NULL,                                         /* alias */
232  NULL                                          /* next */
233};
Note: See TracBrowser for help on using the repository browser.