source: multiio/pcmmio/original/main_pcmmio_dac.c @ d7d35da

Last change on this file since d7d35da was f89686d, checked in by Joel Sherrill <joel.sherrill@…>, on 07/22/09 at 18:24:22

2009-07-22 Joel Sherrill <joel.sherrill@…>

  • README, main_pcmmio_dac.c, rtems_config.c: Add mode to pcmmio_dac command where it can write a pattern.
  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 *  pcmmio_dac 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 "pcmmio_commands.h"
15#include <rtems/string2.h>
16
17#include <stdlib.h>
18
19#if defined(TESTING)
20  #define set_dac_voltage(_dac, _voltage) \
21    /* printf( "Testing: Write %6.4f to to dac %d\n", _voltage, _dac ); */
22#endif
23
24#define VALIDATE_VOLTAGE(_v) \
25  if ( (_v) < -10.0 || (_v) > 10.0 ) { \
26    printf( "Voltage must be between -10.0V and +10.0V\n" ); \
27    fail = true; \
28  }
29
30char pcmmio_dac_usage[] =
31  "Usage: %s dac voltage\n"
32  "       %s dac low high step time_per_step maximum_time\n"
33  "\n"
34  "Where: dac must be 0-7\n"
35  "       voltages and step must be -10V to +10V\n"
36  "       times are in milliseconds\n"
37  "  First form is a single write.\n"
38  "  Second form writes a pattern.\n";
39
40#define PRINT_USAGE() \
41   printf( pcmmio_dac_usage, argv[0], argv[0] );
42
43int main_pcmmio_dac(int argc, char **argv)
44{
45  int       dac;
46  float     low_voltage;
47  float     high_voltage;
48  float     step_voltage;
49  float     current_voltage;
50  float     current_step;
51  int       step_time;
52  int       maximum_time;
53  uint32_t  step_ticks;
54  bool      fail = false;
55  int       elapsed;
56
57  /*
58   *  Verify that we have the right number of arguments.
59   */
60  if ( (argc != 3) && (argc != 7) ) {
61    printf( "Incorrect number of arguments\n" );
62    PRINT_USAGE();
63    return -1;
64  }
65
66  /*
67   *  Convert the string arguments into number values
68   */
69  if ( !rtems_string_to_int( argv[1], &dac, NULL, 0 ) ) {
70    printf( "DAC (%s) is not a number\n", argv[1] );
71    fail = true;
72  }
73
74  if ( !rtems_string_to_float( argv[2], &low_voltage, NULL ) ) {
75    printf( "Voltage (%s) is not a number\n", argv[2] );
76    fail = true;
77  }
78
79  /*
80   *  Validate the output dac and voltage.
81   */
82  if ( dac < 0 || dac > 7 ) {
83    puts( "DAC number must be 0-7" );
84    fail = true;
85  }
86
87  VALIDATE_VOLTAGE( low_voltage );
88
89  /*
90   *  Now do a single write to the DAC
91   */
92  if ( argc == 3 ) {
93    if ( fail ) {
94      PRINT_USAGE();
95      return -1;
96    }
97    printf( "Write %6.4f to to dac %d\n", low_voltage, dac );
98    set_dac_voltage(dac, low_voltage);
99  }
100
101  /*
102   *  Finish parsing the arguments to do a pattern
103   */
104  fail = false;
105
106  if ( !rtems_string_to_float( argv[3], &high_voltage, NULL ) ) {
107    printf( "Voltage (%s) is not a number\n", argv[3] );
108    fail = true;
109  }
110
111  VALIDATE_VOLTAGE( high_voltage );
112
113  if ( !rtems_string_to_float( argv[4], &step_voltage, NULL ) ) {
114    printf( "Step voltage (%s) is not a number\n", argv[4] );
115    fail = true;
116  }
117
118  VALIDATE_VOLTAGE( step_voltage );
119
120  if ( step_voltage < 0.0 ) {
121    printf( "Step voltage must be greater than 0\n" );
122    fail = true;
123  }
124
125  if ( !rtems_string_to_int( argv[5], &step_time, NULL, 0 ) ) {
126    printf( "Step time (%s) is not a number\n", argv[5] );
127    fail = true;
128  }
129
130  if ( !rtems_string_to_int( argv[6], &maximum_time, NULL, 0 ) ) {
131    printf( "Maximum time (%s) is not a number\n", argv[6] );
132    fail = true;
133  }
134
135  if ( step_time >= maximum_time ) {
136    printf(
137      "Step time (%d) must be less than maximum time (%d)\n",
138      step_time,
139      maximum_time
140    );
141    fail = true;
142  }
143
144  if ( step_time < 0 ) {
145    printf( "Step time must be greater than 0\n" );
146    fail = true;
147  }
148
149  if ( maximum_time < 0 ) {
150    printf( "Maximum time must be greater than 0\n" );
151    fail = true;
152  }
153
154  /*
155   *  Now write the pattern to the DAC
156   */
157
158  if ( fail ) {
159    PRINT_USAGE();
160    return -1;
161  }
162
163  printf(
164    "Write %6.4f-%6.4f step=%6.4f stepTime=%d msecs dac=%d max=%d msecs\n",
165    low_voltage,
166    high_voltage,
167    step_voltage,
168    step_time,
169    dac,
170    maximum_time
171  );
172
173  elapsed         = 0;
174  step_ticks      = RTEMS_MILLISECONDS_TO_TICKS(step_time);
175  current_voltage = low_voltage;
176  current_step    = step_voltage;
177
178  if ( low_voltage > high_voltage )
179    current_step    *= -1.0;
180
181  while (1) {
182 
183    #if defined(TESTING)
184      printf( "%d: Write %6.4f to to dac %d\n", elapsed, current_voltage, dac );
185    #endif
186    set_dac_voltage(dac, current_voltage);
187
188    current_voltage += current_step;
189    if ( current_voltage < low_voltage ) {
190      current_step    = step_voltage;
191      current_voltage = low_voltage;
192    } else if ( current_voltage > high_voltage ) {
193      current_step    = -1.0 * step_voltage;
194      current_voltage = high_voltage;
195    }
196
197    elapsed += step_time;
198    if ( elapsed > maximum_time )
199      break;
200
201    rtems_task_wake_after( step_ticks );
202  }
203   
204  return 0;
205}
206
207rtems_shell_cmd_t Shell_PCMMIO_DAC_Command = {
208  "pcmmio_dac",                                    /* name */
209  "Read PCMMIO Analog Inputs",                     /* usage */
210  "pcmmio",                                        /* topic */
211  main_pcmmio_dac,                                 /* command */
212  NULL,                                            /* alias */
213  NULL                                             /* next */
214};
215
216rtems_shell_alias_t Shell_PCMMIO_DAC_Alias = {
217  "pcmmio_dac",          /* command */
218  "dac"                  /* alias */
219};
220
Note: See TracBrowser for help on using the repository browser.